Hi, this is the next step in my ongoing quest to give some lovin' to the systemd wrapper.
It's against 1.6, I guess there is no reason to backport this to 1.5. Does it look acceptable? Cheers, Conrad -- Conrad Hoffmann Traffic Engineer SoundCloud Ltd. | Rheinsberger Str. 76/77, 10115 Berlin, Germany Managing Director: Alexander Ljung | Incorporated in England & Wales with Company No. 6343600 | Local Branch Office | AG Charlottenburg | HRB 110657B
>From b7f869424c6a75dc0b7ff734b61a98080448e5ad Mon Sep 17 00:00:00 2001 From: Conrad Hoffmann <[email protected]> Date: Thu, 21 Aug 2014 07:44:12 +0200 Subject: [PATCH] Remove more global state from systemd wrapper. Now that the signal handling is reduced to a minimum, all state can be kept locally, making functions easier to read and debug. Signed-off-by: Conrad Hoffmann <[email protected]> --- src/haproxy-systemd-wrapper.c | 54 +++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/src/haproxy-systemd-wrapper.c b/src/haproxy-systemd-wrapper.c index 90a94ce..97e80e5 100644 --- a/src/haproxy-systemd-wrapper.c +++ b/src/haproxy-systemd-wrapper.c @@ -19,15 +19,12 @@ #include <sys/wait.h> #define REEXEC_FLAG "HAPROXY_SYSTEMD_REEXEC" +#define DEFAULT_PID_FILE "/run/haproxy.pid" #define SD_DEBUG "<7>" #define SD_NOTICE "<5>" static volatile sig_atomic_t caught_signal; -static char *pid_file = "/run/haproxy.pid"; -static int wrapper_argc; -static char **wrapper_argv; - static void locate_haproxy(char *buffer, size_t buffer_size) { char *end = NULL; @@ -44,7 +41,7 @@ static void locate_haproxy(char *buffer, size_t buffer_size) buffer[buffer_size - 1] = '\0'; } -static void spawn_haproxy(char **pid_strv, int nb_pid) +static void spawn_haproxy(int wrapper_argc, char **wrapper_argv, int pidc, char **pidv) { char haproxy_bin[512]; pid_t pid; @@ -57,7 +54,7 @@ static void spawn_haproxy(char **pid_strv, int nb_pid) pid = fork(); if (!pid) { /* 3 for "haproxy -Ds -sf" */ - char **argv = calloc(4 + main_argc + nb_pid + 1, sizeof(char *)); + char **argv = calloc(4 + main_argc + pidc + 1, sizeof(char *)); int i; int argno = 0; locate_haproxy(haproxy_bin, 512); @@ -65,10 +62,10 @@ static void spawn_haproxy(char **pid_strv, int nb_pid) for (i = 0; i < main_argc; ++i) argv[argno++] = main_argv[i]; argv[argno++] = "-Ds"; - if (nb_pid > 0) { + if (pidc > 0) { argv[argno++] = "-sf"; - for (i = 0; i < nb_pid; ++i) - argv[argno++] = pid_strv[i]; + for (i = 0; i < pidc; ++i) + argv[argno++] = pidv[i]; } argv[argno] = NULL; @@ -82,7 +79,7 @@ static void spawn_haproxy(char **pid_strv, int nb_pid) } } -static int read_pids(char ***pid_strv) +static int read_pids(const char *pid_file, char ***pid_strv) { FILE *f = fopen(pid_file, "r"); int read = 0, allocated = 8; @@ -110,19 +107,19 @@ static void signal_handler(int signum) caught_signal = signum; } -static void do_restart(void) +static void do_restart(char *const *argv) { setenv(REEXEC_FLAG, "1", 1); fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: re-executing\n"); - execv(wrapper_argv[0], wrapper_argv); + execv(argv[0], argv); } -static void do_shutdown(void) +static void do_shutdown(const char *pid_file) { int i, pid; char **pid_strv = NULL; - int nb_pid = read_pids(&pid_strv); + int nb_pid = read_pids(pid_file, &pid_strv); for (i = 0; i < nb_pid; ++i) { pid = atoi(pid_strv[i]); if (pid > 0) { @@ -134,25 +131,22 @@ static void do_shutdown(void) free(pid_strv); } -static void init(int argc, char **argv) +static char* get_pid_file(int argc, char **argv) { + char *pid_file = DEFAULT_PID_FILE; while (argc > 1) { if ((*argv)[0] == '-' && (*argv)[1] == 'p') { pid_file = *(argv + 1); } --argc; ++argv; } + return pid_file; } int main(int argc, char **argv) { int status; - - wrapper_argc = argc; - wrapper_argv = argv; - - --argc; ++argv; - init(argc, argv); + char *pid_file = get_pid_file(argc, argv); struct sigaction sa; memset(&sa, 0, sizeof(struct sigaction)); @@ -163,30 +157,30 @@ int main(int argc, char **argv) if (getenv(REEXEC_FLAG) != NULL) { /* We are being re-executed: restart HAProxy gracefully */ int i; - char **pid_strv = NULL; - int nb_pid = read_pids(&pid_strv); + char **pidv = NULL; + int pidc = read_pids(pid_file, &pidv); unsetenv(REEXEC_FLAG); - spawn_haproxy(pid_strv, nb_pid); + spawn_haproxy(argc, argv, pidc, pidv); - for (i = 0; i < nb_pid; ++i) - free(pid_strv[i]); - free(pid_strv); + for (i = 0; i < pidc; ++i) + free(pidv[i]); + free(pidv); } else { /* Start a fresh copy of HAProxy */ - spawn_haproxy(NULL, 0); + spawn_haproxy(argc, argv, 0, NULL); } status = -1; while (-1 != wait(&status) || errno == EINTR) { if (caught_signal == SIGUSR2) { caught_signal = 0; - do_restart(); + do_restart(argv); } else if (caught_signal == SIGINT) { caught_signal = 0; - do_shutdown(); + do_shutdown(pid_file); } } -- 2.0.4

