void postfork(void) { libxl_postfork_child_noexec(ctx); /* in case we don't exit/exec */ ctx = 0; xl_ctx_alloc(); }
int main(int argc, char **argv) { int opt = 0; char *cmd = 0; struct cmd_spec *cspec; int ret; void *config_data = 0; int config_len = 0; while ((opt = getopt(argc, argv, "+vftN")) >= 0) { switch (opt) { case 'v': if (minmsglevel > 0) minmsglevel--; break; case 'N': dryrun_only = 1; break; case 'f': force_execution = 1; break; case 't': progress_use_cr = 1; break; default: fprintf(stderr, "unknown global option\n"); exit(EXIT_FAILURE); } } cmd = argv[optind]; if (!cmd) { help(NULL); exit(EXIT_FAILURE); } opterr = 0; logger = xtl_createlogger_stdiostream(stderr, minmsglevel, (progress_use_cr ? XTL_STDIOSTREAM_PROGRESS_USE_CR : 0)); if (!logger) exit(EXIT_FAILURE); atexit(xl_ctx_free); xl_ctx_alloc(); ret = libxl_read_file_contents(ctx, XL_GLOBAL_CONFIG, &config_data, &config_len); if (ret) fprintf(stderr, "Failed to read config file: %s: %s\n", XL_GLOBAL_CONFIG, strerror(errno)); parse_global_config(XL_GLOBAL_CONFIG, config_data, config_len); free(config_data); /* Reset options for per-command use of getopt. */ argv += optind; argc -= optind; optind = 1; cspec = cmdtable_lookup(cmd); if (cspec) { if (dryrun_only && !cspec->can_dryrun) { fprintf(stderr, "command does not implement -N (dryrun) option\n"); ret = EXIT_FAILURE; goto xit; } ret = cspec->cmd_impl(argc, argv); } else if (!strcmp(cmd, "help")) { help(argv[1]); ret = EXIT_SUCCESS; } else { fprintf(stderr, "command not implemented\n"); ret = EXIT_FAILURE; } xit: return ret; }
int main(int argc, char **argv) { int opt = 0; char *cmd = 0; struct cmd_spec *cspec; int ret; void *config_data = 0; int config_len = 0; const char *locks[] = XEND_LOCK; while ((opt = getopt(argc, argv, "+vfN")) >= 0) { switch (opt) { case 'v': if (minmsglevel > 0) minmsglevel--; break; case 'N': dryrun_only = 1; break; case 'f': force_execution = 1; break; default: fprintf(stderr, "unknown global option\n"); exit(2); } } cmd = argv[optind]; if (!cmd) { help(NULL); exit(1); } opterr = 0; logger = xtl_createlogger_stdiostream(stderr, minmsglevel, 0); if (!logger) exit(1); atexit(xl_ctx_free); xl_ctx_alloc(); ret = libxl_read_file_contents(ctx, XL_GLOBAL_CONFIG, &config_data, &config_len); if (ret) fprintf(stderr, "Failed to read config file: %s: %s\n", XL_GLOBAL_CONFIG, strerror(errno)); parse_global_config(XL_GLOBAL_CONFIG, config_data, config_len); free(config_data); /* Reset options for per-command use of getopt. */ argv += optind; argc -= optind; optind = 1; cspec = cmdtable_lookup(cmd); if (cspec) { if (dryrun_only && !cspec->can_dryrun) { fprintf(stderr, "command does not implement -N (dryrun) option\n"); ret = 1; goto xit; } if (cspec->modifies && !dryrun_only) { for (int i = 0; i < sizeof(locks)/sizeof(locks[0]); i++) { if (!access(locks[i], F_OK) && !force_execution) { fprintf(stderr, "xend is running, which may cause unpredictable results when using\n" "this xl command. Please shut down xend before continuing.\n\n" "(This check can be overridden with the -f option.)\n" ); ret = 1; goto xit; } } } ret = cspec->cmd_impl(argc, argv); } else if (!strcmp(cmd, "help")) { help(argv[1]); ret = 0; } else { fprintf(stderr, "command not implemented\n"); ret = 1; } xit: return ret; }