From fb49182c29ac708275a3584b1309e6898e9e3b76 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 31 Jul 2016 16:34:55 +0900 Subject: [PATCH] nghttpx: Move original_argv, argv, argc, and cmdcfgs to StartupConfig --- src/shrpx.cc | 63 +++++++++++++++++++++++++++------------------- src/shrpx_config.h | 4 --- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/src/shrpx.cc b/src/shrpx.cc index 77fb9814..0a4fcfe8 100644 --- a/src/shrpx.cc +++ b/src/shrpx.cc @@ -130,6 +130,25 @@ constexpr auto ENV_ACCEPT_PREFIX = StringRef::from_lit("NGHTTPX_ACCEPT_"); #endif #endif +// This configuration is fixed at the first startup of the main +// process, and does not change after subsequent reloadings. +struct StartupConfig { + // This contains all options given in command-line. + std::vector> cmdcfgs; + // The current working directory where this process started. + char *cwd; + // The pointer to original argv (not sure why we have this?) + char **original_argv; + // The pointer to argv, this is a deep copy of original argv. + char **argv; + // The number of elements in argv. + int argc; +}; + +namespace { +StartupConfig suconfig; +} // namespace + struct InheritedAddr { // IP address if TCP socket. Otherwise, UNIX domain socket path. ImmutableString host; @@ -144,12 +163,6 @@ namespace { std::random_device rd; } // namespace -namespace { -// This contains all options given in command-line. Make it static so -// that we can use it in reloading. -std::vector> cmdcfgs; -} // namespace - namespace { void signal_cb(struct ev_loop *loop, ev_signal *w, int revents); } // namespace @@ -388,21 +401,21 @@ void exec_binary(WorkerProcess *wp) { _Exit(EXIT_FAILURE); } - auto exec_path = util::get_exec_path(get_config()->argc, get_config()->argv, - get_config()->cwd); + auto exec_path = + util::get_exec_path(suconfig.argc, suconfig.argv, suconfig.cwd); if (!exec_path) { LOG(ERROR) << "Could not resolve the executable path"; _Exit(EXIT_FAILURE); } - auto argv = make_unique(get_config()->argc + 1); + auto argv = make_unique(suconfig.argc + 1); argv[0] = exec_path; - for (int i = 1; i < get_config()->argc; ++i) { - argv[i] = get_config()->argv[i]; + for (int i = 1; i < suconfig.argc; ++i) { + argv[i] = suconfig.argv[i]; } - argv[get_config()->argc] = nullptr; + argv[suconfig.argc] = nullptr; size_t envlen = 0; for (char **p = environ; *p; ++p, ++envlen) @@ -2504,19 +2517,15 @@ void reload_config(WorkerProcess *wp) { fill_default_config(new_config.get()); new_config->conf_path = cur_config->conf_path; - new_config->argc = cur_config->argc; - new_config->argv = cur_config->argv; - new_config->original_argv = cur_config->original_argv; - new_config->cwd = cur_config->cwd; + // daemon option is ignored here. + new_config->daemon = cur_config->daemon; - rv = process_options(new_config.get(), cmdcfgs); + rv = process_options(new_config.get(), suconfig.cmdcfgs); if (rv != 0) { LOG(ERROR) << "Failed to process new configuration"; return; } - // daemon option is ignored here. - auto iaddrs = get_inherited_addr_from_config(cur_config); if (create_acceptor_socket(new_config.get(), iaddrs) != 0) { @@ -2582,28 +2591,30 @@ int main(int argc, char **argv) { // log errors/warnings while reading configuration files. reopen_log_files(); - mod_config()->original_argv = argv; + suconfig.original_argv = argv; // We have to copy argv, since getopt_long may change its content. - mod_config()->argc = argc; - mod_config()->argv = new char *[argc]; + suconfig.argc = argc; + suconfig.argv = new char *[argc]; for (int i = 0; i < argc; ++i) { - mod_config()->argv[i] = strdup(argv[i]); - if (mod_config()->argv[i] == nullptr) { + suconfig.argv[i] = strdup(argv[i]); + if (suconfig.argv[i] == nullptr) { auto error = errno; LOG(FATAL) << "failed to copy argv: " << strerror(error); exit(EXIT_FAILURE); } } - mod_config()->cwd = getcwd(nullptr, 0); - if (mod_config()->cwd == nullptr) { + suconfig.cwd = getcwd(nullptr, 0); + if (suconfig.cwd == nullptr) { auto error = errno; LOG(FATAL) << "failed to get current working directory: errno=" << error; exit(EXIT_FAILURE); } + auto &cmdcfgs = suconfig.cmdcfgs; + while (1) { static int flag = 0; static option long_options[] = { diff --git a/src/shrpx_config.h b/src/shrpx_config.h index d3c664ac..e4a4c36b 100644 --- a/src/shrpx_config.h +++ b/src/shrpx_config.h @@ -717,13 +717,9 @@ struct Config { ImmutableString conf_path; ImmutableString user; ImmutableString mruby_file; - char **original_argv; - char **argv; - char *cwd; size_t num_worker; size_t padding; size_t rlimit_nofile; - int argc; uid_t uid; gid_t gid; pid_t pid;