bud_error_t bud_config_reload(bud_config_t* config) { bud_error_t err; bud_config_t* loaded; bud_config_t restore; loaded = bud_config_load(config->path, config->inlined, &err); if (!bud_is_ok(err)) return err; memset(&restore, 0, sizeof(restore)); bud_config_copy(&restore, config); bud_config_copy(config, loaded); /* Initialize config with new params */ err = bud_config_init(config); /* Restore everything on failure */ if (!bud_is_ok(err)) { bud_config_copy(config, &restore); bud_config_free(loaded); return err; } free(loaded); bud_config_destroy(&restore); return bud_ok(); }
int main(int argc, char** argv) { bud_config_t* config; bud_error_t err; #ifndef _WIN32 /* Ignore SIGPIPE */ signal(SIGPIPE, SIG_IGN); #endif /* !_WIN32 */ bud_init_openssl(); config = bud_config_cli_load(argc, argv, &err); /* NOTE: bud_config_load will print everything itself */ if (config == NULL) goto fatal; if (config->is_worker) err = bud_worker(config); else err = bud_master(config); if (bud_is_ok(err)) uv_run(config->loop, UV_RUN_DEFAULT); /* Finalize server */ if (config->server != NULL) { if (config->is_worker) err = bud_worker_finalize(config); else err = bud_master_finalize(config); } uv_run(config->loop, UV_RUN_NOWAIT); fatal: if (config != NULL) bud_config_free(config); if (!bud_is_ok(err)) { bud_error_print(stderr, err); return -1; } return 0; }
int main(int argc, char** argv) { bud_config_t* config; bud_error_t err; bud_init_openssl(); config = bud_config_cli_load(uv_default_loop(), argc, argv, &err); /* NOTE: bud_config_load will print everything itself */ if (config == NULL) goto fatal; if (config->is_worker) err = bud_worker(config); else err = bud_master(config); if (bud_is_ok(err)) uv_run(config->loop, UV_RUN_DEFAULT); /* Finalize server */ if (config->server != NULL) { if (!config->is_worker) err = bud_master_finalize(config); bud_server_free(config); } uv_run(config->loop, UV_RUN_ONCE); fatal: if (config != NULL) bud_config_free(config); if (!bud_is_ok(err)) { bud_error_print(stderr, err); return -1; } return 0; }
bud_error_t bud_config_new(int argc, char** argv, bud_config_t** out) { bud_error_t err; bud_config_t* config; int i; int r; size_t path_len; int c; int index; int loaded; config = calloc(1, sizeof(*config)); if (config == NULL) return bud_error_str(kBudErrNoMem, "bud_config_t"); loaded = 0; do { index = 0; c = getopt_long(argc, argv, bud_long_flags, bud_long_options, &index); switch (c) { case 'v': bud_print_version(); err = bud_error(kBudErrSkip); goto fatal; #ifndef _WIN32 case 'd': config->is_daemon = 1; #endif /* !_WIN32 */ break; case 'p': case 'i': case 'c': if (loaded) { err = bud_error(kBudErrMultipleConfigs); goto fatal; } loaded = 1; if (c == 'p') { config->piped = 1; config->path = kPipedConfigPath; } else { config->piped = 0; config->path = optarg; config->inlined = c == 'i'; } break; case 1000: config->is_worker = 1; break; case 1001: bud_config_print_default(); err = bud_error(kBudErrSkip); goto fatal; default: if (loaded) break; bud_print_help(argc, argv); goto no_config; } } while (c != -1); if (!config->piped) { config->piped_index = -1; } else { /* get_opt does not provide the argc offset so must manually retrieve it */ for (i = 0; i < argc; i++) { if (strcmp(argv[i], "--piped-config") == 0 || strcmp(argv[i], "-p") == 0) { config->piped_index = i; break; } } } /* CLI options */ config->argc = argc; config->argv = argv; /* Get executable path */ path_len = sizeof(config->exepath); r = uv_exepath(config->exepath, &path_len); ASSERT(path_len < sizeof(config->exepath), "Exepath OOB"); if (r != 0) { bud_config_free(config); config = NULL; return bud_error_num(kBudErrExePath, r); } err = bud_hashmap_init(&config->files.hashmap, kBudFileCacheSize); if (!bud_is_ok(err)) goto fatal; *out = config; return bud_ok(); no_config: free(config); return bud_error(kBudErrNoConfig); fatal: free(config); return err; }
bud_config_t* bud_config_cli_load(uv_loop_t* loop, int argc, char** argv, bud_error_t* err) { int c; int r; int index; int is_daemon; int is_worker; size_t path_len; bud_config_t* config; struct option long_options[] = { { "version", 0, NULL, 'v' }, { "config", 1, NULL, 'c' }, #ifndef _WIN32 { "daemonize", 0, NULL, 'd' }, #endif /* !_WIN32 */ { "worker", 0, NULL, 1000 }, { "default-config", 0, NULL, 1001 }, { NULL, 0, NULL, 0 } }; *err = bud_ok(); config = NULL; is_daemon = 0; is_worker = 0; do { index = 0; c = getopt_long(argc, argv, "vc:d", long_options, &index); switch (c) { case 'v': bud_print_version(); break; case 'c': config = bud_config_load(loop,optarg, err); if (config == NULL) { ASSERT(!bud_is_ok(*err), "Config load failed without error"); c = -1; break; } if (is_daemon) config->is_daemon = 1; if (is_worker) config->is_worker = 1; break; #ifndef _WIN32 case 'd': is_daemon = 1; if (config != NULL) config->is_daemon = 1; #endif /* !_WIN32 */ break; case 1000: is_worker = 1; if (config != NULL) config->is_worker = 1; break; case 1001: bud_config_print_default(); c = -1; break; default: if (config == NULL) bud_print_help(argc, argv); c = -1; break; } } while (c != -1); if (config != NULL) { /* CLI options */ config->argc = argc; config->argv = argv; /* Get executable path */ path_len = sizeof(config->exepath); r = uv_exepath(config->exepath, &path_len); ASSERT(path_len < sizeof(config->exepath), "Exepath OOB"); config->exepath[path_len] = 0; if (r != 0) { bud_config_free(config); config = NULL; *err = bud_error_num(kBudErrExePath, r); } /* Initialize config */ *err = bud_config_init(config); if (!bud_is_ok(*err)) { bud_config_free(config); return NULL; } } return config; }