int main(int argc, char *argv[], char *envp[]) { act_t action = -1; int verbose = 0; int verbose_custom = 0; int quiet = 0; int veid, ret, skiplock = 0; char buf[256]; vps_param *gparam = NULL, *vps_p = NULL, *cmd_p = NULL; const char *action_nm; struct sigaction act; char *name = NULL, *opt; _proc_title = argv[0]; _proc_title_len = envp[0] - argv[0]; gparam = init_vps_param(); vps_p = init_vps_param(); cmd_p = init_vps_param(); sigemptyset(&act.sa_mask); act.sa_handler = SIG_IGN; act.sa_flags = 0; sigaction(SIGPIPE, &act, NULL); while (argc > 1) { opt = argv[1]; if (!strcmp(opt, "--verbose")) { verbose++; verbose_custom = 1; } else if (!strcmp(opt, "--quiet")) quiet = 1; else if (!strcmp(opt, "--version")) { version(stdout); exit(0); } else if (!strcmp(opt, "--skiplock")) skiplock = YES; else break; argc--; argv++; } if (argc <= 1) usage(VZ_INVALID_PARAMETER_SYNTAX); action_nm = argv[1]; init_log(NULL, 0, 1, verbose, quiet, NULL); if (!strcmp(argv[1], "set")) { init_modules(&g_action, "set"); action = ACTION_SET; } else if (!strcmp(argv[1], "create")) { init_modules(&g_action, "create"); action = ACTION_CREATE; } else if (!strcmp(argv[1], "start")) { init_modules(&g_action, "set"); action = ACTION_START; } else if (!strcmp(argv[1], "stop")) { init_modules(&g_action, "set"); action = ACTION_STOP; } else if (!strcmp(argv[1], "restart")) { action = ACTION_RESTART; } else if (!strcmp(argv[1], "destroy") || !strcmp(argv[1], "delete")) { action = ACTION_DESTROY; } else if (!strcmp(argv[1], "mount")) { action = ACTION_MOUNT; } else if (!strcmp(argv[1], "umount")) { action = ACTION_UMOUNT; } else if (!strcmp(argv[1], "exec3")) { action = ACTION_EXEC3; } else if (!strcmp(argv[1], "exec2")) { action = ACTION_EXEC2; } else if (!strcmp(argv[1], "exec")) { action = ACTION_EXEC; } else if (!strcmp(argv[1], "runscript")) { action = ACTION_RUNSCRIPT; } else if (!strcmp(argv[1], "enter")) { action = ACTION_ENTER; } else if (!strcmp(argv[1], "console")) { action = ACTION_CONSOLE; #ifdef HAVE_PLOOP } else if (!strcmp(argv[1], "convert")) { action = ACTION_CONVERT; } else if (!strcmp(argv[1], "compact")) { action = ACTION_COMPACT; #endif } else if (!strcmp(argv[1], "status")) { action = ACTION_STATUS; quiet = 1; } else if (!strcmp(argv[1], "suspend") || !strcmp(argv[1], "chkpnt")) { action = ACTION_SUSPEND; } else if (!strcmp(argv[1], "resume") || !strcmp(argv[1], "restore")) { action = ACTION_RESUME; } else if (!strcmp(argv[1], "quotaon")) { action = ACTION_QUOTAON; } else if (!strcmp(argv[1], "quotaoff")) { action = ACTION_QUOTAOFF; } else if (!strcmp(argv[1], "quotainit")) { action = ACTION_QUOTAINIT; #ifdef HAVE_PLOOP } else if (!strcmp(argv[1], "snapshot")) { action = ACTION_SNAPSHOT_CREATE; } else if (!strcmp(argv[1], "snapshot-switch")) { action = ACTION_SNAPSHOT_SWITCH; } else if (!strcmp(argv[1], "snapshot-delete")) { action = ACTION_SNAPSHOT_DELETE; } else if (!strcmp(argv[1], "snapshot-list")) { action = ACTION_SNAPSHOT_LIST; } else if (!strcmp(argv[1], "snapshot-mount")) { action = ACTION_SNAPSHOT_MOUNT; } else if (!strcmp(argv[1], "snapshot-umount")) { action = ACTION_SNAPSHOT_UMOUNT; #endif } else if (!strcmp(argv[1], "--help")) { usage(0); } else { init_modules(&g_action, action_nm); action = ACTION_CUSTOM; if (!g_action.mod_count) { fprintf(stderr, "Bad command: %s\n", argv[1]); ret = VZ_INVALID_PARAMETER_SYNTAX; goto error; } } if (argc < 3) { fprintf(stderr, "CT ID missing\n"); ret = VZ_INVALID_PARAMETER_VALUE; goto error; } if (parse_int(argv[2], &veid)) { name = strdup(argv[2]); veid = get_veid_by_name(name); } if (veid < 0 || veid > VEID_MAX) { fprintf(stderr, "Bad CT ID %s\n", argv[2]); ret = VZ_INVALID_PARAMETER_VALUE; goto error; } argc -= 2; argv += 2; /* getopt_long() prints argv[0] when reporting errors */ argv[0] = _proc_title; /* Read global config file */ if (vps_parse_config(veid, GLOBAL_CFG, gparam, &g_action)) { ret = VZ_NOCONFIG; goto error; } init_log(gparam->log.log_file, veid, gparam->log.enable != NO, gparam->log.level, quiet, "vzctl"); /* Set verbose level from global config if not overwriten by --verbose */ if (!verbose_custom && gparam->log.verbose != NULL) { verbose = *gparam->log.verbose; verbose_custom = 1; } if (verbose < -1) verbose = -1; if (verbose_custom) set_log_verbose(verbose); if ((ret = parse_action_opt(veid, action, argc, argv, cmd_p, action_nm))) { goto error; } if (veid == 0 && action != ACTION_SET) { fprintf(stderr, "Only set actions are allowed for CT0\n"); ret = VZ_INVALID_PARAMETER_VALUE; goto error; } else if (veid < 0) { fprintf(stderr, "Bad CT ID %d\n", veid); ret = VZ_INVALID_PARAMETER_VALUE; goto error; } get_vps_conf_path(veid, buf, sizeof(buf)); if (stat_file(buf) == 1) { if (vps_parse_config(veid, buf, vps_p, &g_action)) { ret = VZ_NOCONFIG; goto error; } if (name != NULL && vps_p->res.name.name != NULL && strcmp(name, vps_p->res.name.name)) { logger(-1, 0, "Unable to find container by name %s", name); ret = VZ_INVALID_PARAMETER_VALUE; goto error; } } else if (action != ACTION_CREATE && action != ACTION_STATUS && action != ACTION_SET) { logger(-1, 0, "Container config file does not exist"); ret = VZ_NOVECONFIG; goto error; } merge_vps_param(gparam, vps_p); merge_global_param(cmd_p, gparam); ret = run_action(veid, action, gparam, vps_p, cmd_p, argc, argv, skiplock); error: free_modules(&g_action); free_vps_param(gparam); free_vps_param(vps_p); free_vps_param(cmd_p); free_log(); free(name); return ret; }
int main(int argc, char **argv){ int status = 0; init_globals(); atexit(cleanup); /* * PROBLEM * We used to call init_signals() only after init_daemon(). But in * that case, when started with -F or -D -D, the signals are * not caught in Linunx and OSX (they are caught in FreeBSD). nbspd and * npemwind die, but leave the pid file and the web server. * [It seems that the signals are not blocked in the main thread as * the code in signal.c should ensure.] * Adding this call here * * status = init_signals(); * * makes OSX and Linux respond well when the daemon is run in the foreground. * If the call is made after the tcl configure(), the problem repeats; * it has to be before the configure() function. * * The problem is that in FreeBSD-7.1, when init_signals() is called here, * then no threads are spawned afterwards. * * The solution was to split init_signals() in two parts, one that * block the signals and the other spawns the thread. I don't fully * understand what in tcl is causing this (Fri Mar 13 11:43:09 AST 2009). */ status = init_signals_block(); if(status == 0){ /* * This will configure it with the default configuration * file, if it exists. * * This note and Tcl... code line is taken from the nbsp source code. * [First initialize the tcl library once and for all. It was not * necessary to call this in unix, but cygwin needs it or EvalFile * seg faults.] */ Tcl_FindExecutable(argv[0]); status = configure(); } if(status == 0) status = parse_args(argc, argv); if(status == 0){ if(g.configfile != NULL){ /* * This will reconfigure it with the user-supplied config file */ status = configure(); } } /* * if [-C] was given, print the configuration and exit. */ if(status == 0){ if(g.option_C == 1){ print_confoptions(); return(0); } } if(status == 0) status = validate_configuration(); /* * user and group are configurable so this must be done after reading * configuration options. */ if(status == 0) status = drop_privs(); if(status == 0) status = init_server_list(); if(status == 0) status = init_directories(); /* * The last configuration step, just before becoming a daemon. */ if(status == 0) status = exec_startscript(); if((status == 0) && (g.f_ndaemon == 0)) status = init_daemon(); set_log_debug(g.f_debug); set_log_verbose(g.f_verbose); if(status == 0) status = init_signals_thread(); /* * This has to be done after daemon() so that the lock file contains the * daemon's pid, not the starting program's. */ if(status == 0) status = init_lock(); /* * There are no shared queues in npemwin, otherwise the initialization * would go here. * * if(status == 0) * status = init_queues(); */ if(status == 0){ if(g.serverprotocol != PROTOCOL_NONE) { g.f_server_enabled = 1; status = init_server(); } } if(status == 0){ if(g.httpd_enable > 0){ status = spawn_httpd_server(); } } if(status == 0){ if(g.bbserver_enable > 0){ status = spawn_bbregistrar(); } } if(status == 0) status = init_emwin_qfiles(); if(status == 0) init_periodic(); /* * If there are initialization errors, ask all threads to quit. */ if(status != 0) set_quit_flag(); while(get_quit_flag() == 0){ status = loop(); } if(status != 0) status = EXIT_FAILURE; return(status); }
void init_log() { set_log_verbose(0); set_log_level(LOG_INFO_LEVEL); set_log_filename("output.log"); }
void init_log_custom(int level, int verbose, char* log_filename) { set_log_level(level); set_log_verbose(verbose); set_log_filename(log_filename); }
int main(int argc, char *argv[], char *envp[]) { int action = 0; int verbose = 0; int verbose_tmp; int verbose_custom = 0; int quiet = 0; int veid, ret, skiplock = 0; char buf[256]; vps_param *gparam = NULL, *vps_p = NULL, *cmd_p = NULL; const char *action_nm; struct sigaction act; char *name = NULL, *opt; _proc_title = argv[0]; _proc_title_len = envp[0] - argv[0]; gparam = init_vps_param(); vps_p = init_vps_param(); cmd_p = init_vps_param(); sigemptyset(&act.sa_mask); act.sa_handler = SIG_IGN; act.sa_flags = 0; sigaction(SIGPIPE, &act, NULL); while (argc > 1) { opt = argv[1]; if (!strcmp(opt, "--verbose")) { if (argc > 2 && !parse_int(argv[2], &verbose_tmp)) { verbose += verbose_tmp; argc--; argv++; } else { verbose++; } verbose_custom = 1; } else if (!strncmp(opt, "--verbose=", 10)) { if (parse_int(opt + 10, &verbose_tmp)) { fprintf(stderr, "Invalid value for" " --verbose\n"); exit(VZ_INVALID_PARAMETER_VALUE); } verbose += verbose_tmp; verbose_custom = 1; } else if (!strcmp(opt, "--quiet")) quiet = 1; else if (!strcmp(opt, "--version")) { version(stdout); exit(0); } else if (!strcmp(opt, "--skiplock")) skiplock = YES; else break; argc--; argv++; } if (argc <= 1) usage(VZ_INVALID_PARAMETER_SYNTAX); action_nm = argv[1]; init_log(NULL, 0, 1, verbose, 0, NULL); if (!strcmp(argv[1], "set")) { init_modules(&g_action, "set"); action = ACTION_SET; // status = ST_SET; } else if (!strcmp(argv[1], "create")) { init_modules(&g_action, "create"); action = ACTION_CREATE; // status = ST_CREATE; } else if (!strcmp(argv[1], "start")) { init_modules(&g_action, "set"); action = ACTION_START; // status = ST_START; } else if (!strcmp(argv[1], "stop")) { init_modules(&g_action, "set"); action = ACTION_STOP; // status = ST_STOP; } else if (!strcmp(argv[1], "restart")) { action = ACTION_RESTART; // status = ST_RESTART; } else if (!strcmp(argv[1], "destroy")) { action = ACTION_DESTROY; // status = ST_DESTROY; } else if (!strcmp(argv[1], "mount")) { action = ACTION_MOUNT; // status = ST_MOUNT; } else if (!strcmp(argv[1], "umount")) { action = ACTION_UMOUNT; // status = ST_UMOUNT; } else if (!strcmp(argv[1], "exec3")) { action = ACTION_EXEC3; } else if (!strcmp(argv[1], "exec2")) { action = ACTION_EXEC2; } else if (!strcmp(argv[1], "exec")) { action = ACTION_EXEC; } else if (!strcmp(argv[1], "runscript")) { action = ACTION_RUNSCRIPT; } else if (!strcmp(argv[1], "enter")) { action = ACTION_ENTER; } else if (!strcmp(argv[1], "status")) { action = ACTION_STATUS; quiet = 1; } else if (!strcmp(argv[1], "chkpnt")) { action = ACTION_CHKPNT; } else if (!strcmp(argv[1], "restore")) { action = ACTION_RESTORE; } else if (!strcmp(argv[1], "quotaon")) { action = ACTION_QUOTAON; } else if (!strcmp(argv[1], "quotaoff")) { action = ACTION_QUOTAOFF; } else if (!strcmp(argv[1], "quotainit")) { action = ACTION_QUOTAINIT; } else if (!strcmp(argv[1], "--help")) { usage(0); } else { init_modules(&g_action, action_nm); action = ACTION_CUSTOM; if (!g_action.mod_count) { fprintf(stderr, "Bad command: %s\n", argv[1]); ret = VZ_INVALID_PARAMETER_SYNTAX; goto error; } } if (argc < 3) { fprintf(stderr, "CT ID missing\n"); ret = VZ_INVALID_PARAMETER_VALUE; goto error; } if (parse_int(argv[2], &veid)) { name = strdup(argv[2]); veid = get_veid_by_name(name); if (veid < 0) { fprintf(stderr, "Bad CT ID %s\n", argv[2]); ret = VZ_INVALID_PARAMETER_VALUE; goto error; } } argc -= 2; argv += 2; /* Read global config file */ if (vps_parse_config(veid, GLOBAL_CFG, gparam, &g_action)) { fprintf(stderr, "Global configuration file %s not found\n", GLOBAL_CFG); ret = VZ_NOCONFIG; goto error; } init_log(gparam->log.log_file, veid, gparam->log.enable != NO, gparam->log.level, quiet, "vzctl"); /* Set verbose level from global config if not overwriten by --verbose */ if (!verbose_custom && gparam->log.verbose != NULL) { verbose = *gparam->log.verbose; verbose_custom = 1; } if (verbose < -1) verbose = -1; if (verbose_custom) set_log_verbose(verbose); if ((ret = parse_action_opt(veid, action, argc, argv, cmd_p, action_nm))) { goto error; } if (veid == 0 && action != ACTION_SET) { fprintf(stderr, "Only set actions are allowed for CT0\n"); ret = VZ_INVALID_PARAMETER_VALUE; goto error; } else if (veid < 0) { fprintf(stderr, "Bad CT ID %d\n", veid); ret = VZ_INVALID_PARAMETER_VALUE; goto error; } get_vps_conf_path(veid, buf, sizeof(buf)); if (stat_file(buf)) { if (vps_parse_config(veid, buf, vps_p, &g_action)) { logger(-1, 0, "Error in config file %s", buf); ret = VZ_NOCONFIG; goto error; } if (name != NULL && vps_p->res.name.name != NULL && strcmp(name, vps_p->res.name.name)) { logger(-1, 0, "Unable to find container by name %s", name); ret = VZ_INVALID_PARAMETER_VALUE; goto error; } } else if (action != ACTION_CREATE && action != ACTION_STATUS && action != ACTION_SET) { logger(-1, 0, "Container config file does not exist"); ret = VZ_NOVECONFIG; goto error; } merge_vps_param(gparam, vps_p); merge_global_param(cmd_p, gparam); ret = run_action(veid, action, gparam, vps_p, cmd_p, argc-1, argv+1, skiplock); error: free_modules(&g_action); free_vps_param(gparam); free_vps_param(vps_p); free_vps_param(cmd_p); free_log(); if (name != NULL) free(name); return ret; }