From: Valentine Krasnobaeva Date: Mon, 7 Oct 2024 09:28:30 +0000 (+0200) Subject: MINOR: startup: encapsulate master's code in run_master X-Git-Tag: v3.1-dev10~18 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1cee184145d3c4552ea71146882e448e05cd5501;p=thirdparty%2Fhaproxy.git MINOR: startup: encapsulate master's code in run_master Let's encapsulate master's code (steps which it does before entering in its polling loop and deinitialization routines after) in a separate run_master() function. This makes the code of main() more readable. In future we plan to put in run_master() more master process related code, in order to clean completely init_step_2(), init_step_3() and init_step_4(). --- diff --git a/src/haproxy.c b/src/haproxy.c index 54ba2bb6a7..89ba8e3a71 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -2022,6 +2022,45 @@ static void prepare_master() LIST_APPEND(&proc_list, &tmproc->list); } +static void run_master() +{ + struct mworker_proc *child, *it; + + if ((!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && + (global.mode & MODE_DAEMON)) { + /* detach from the tty, this is required to properly daemonize. */ + if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)) + stdio_quiet(-1); + global.mode &= ~MODE_VERBOSE; + global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */ + } + proc_self->failedreloads = 0; /* reset the number of failure */ + mworker_loop(); +#if defined(USE_OPENSSL) && !defined(OPENSSL_NO_DH) + ssl_free_dh(); +#endif + master = 0; + /* close useless master sockets */ + mworker_cli_proxy_stop(); + + /* free proc struct of other processes */ + list_for_each_entry_safe(child, it, &proc_list, list) { + /* close the FD of the master side for all + * workers, we don't need to close the worker + * side of other workers since it's done with + * the bind_proc */ + if (child->ipc_fd[0] >= 0) { + close(child->ipc_fd[0]); + child->ipc_fd[0] = -1; + } + LIST_DELETE(&child->list); + mworker_free_child(child); + child = NULL; + } + /* master must leave */ + exit(0); +} + /* * This function does daemonization fork. It only returns if everything is OK. * If something fails, it exits. @@ -3704,44 +3743,13 @@ int main(int argc, char **argv) */ step_init_4(); + /* Master enters in its polling loop */ if (master) { - struct mworker_proc *child, *it; - - if ((!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && - (global.mode & MODE_DAEMON)) { - /* detach from the tty, this is required to properly daemonize. */ - if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)) - stdio_quiet(-1); - global.mode &= ~MODE_VERBOSE; - global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */ - } - proc_self->failedreloads = 0; /* reset the number of failure */ - mworker_loop(); -#if defined(USE_OPENSSL) && !defined(OPENSSL_NO_DH) - ssl_free_dh(); -#endif - master = 0; - /* close useless master sockets */ - mworker_cli_proxy_stop(); - - /* free proc struct of other processes */ - list_for_each_entry_safe(child, it, &proc_list, list) { - /* close the FD of the master side for all - * workers, we don't need to close the worker - * side of other workers since it's done with - * the bind_proc */ - if (child->ipc_fd[0] >= 0) { - close(child->ipc_fd[0]); - child->ipc_fd[0] = -1; - } - LIST_DELETE(&child->list); - mworker_free_child(child); - child = NULL; - } - /* master must leave */ - exit(0); + run_master(); + /* never get there in master context */ } + /* End of initialization for standalone and worker modes */ if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { devnullfd = open("/dev/null", (O_RDWR | O_CLOEXEC), 0); if (devnullfd < 0) {