1 From 62c8565cd5bbda6ac0dd818fa26922eeaef1605c Mon Sep 17 00:00:00 2001
2 From: Conrad Hoffmann <conrad@soundcloud.com>
3 Date: Mon, 28 Jul 2014 23:52:20 +0200
4 Subject: [PATCH 03/13] MEDIUM: Improve signal handling in systemd wrapper.
6 Move all code out of the signal handlers, since this is potentially
7 dangerous. To make sure the signal handlers behave as expected, use
8 sigaction() instead of signal(). That also obsoletes messing with
9 the signal mask after restart.
11 Signed-off-by: Conrad Hoffmann <conrad@soundcloud.com>
12 (cherry picked from commit 5b5ea9c93384da49eea0f67ebed0966d4167b17a)
14 src/haproxy-systemd-wrapper.c | 37 ++++++++++++++++++++++++-------------
15 1 file changed, 24 insertions(+), 13 deletions(-)
17 diff --git a/src/haproxy-systemd-wrapper.c b/src/haproxy-systemd-wrapper.c
18 index 529b213..90a94ce 100644
19 --- a/src/haproxy-systemd-wrapper.c
20 +++ b/src/haproxy-systemd-wrapper.c
22 #define SD_DEBUG "<7>"
23 #define SD_NOTICE "<5>"
25 +static volatile sig_atomic_t caught_signal;
27 static char *pid_file = "/run/haproxy.pid";
28 static int wrapper_argc;
29 static char **wrapper_argv;
30 @@ -103,7 +105,12 @@ static int read_pids(char ***pid_strv)
34 -static void sigusr2_handler(int signum __attribute__((unused)))
35 +static void signal_handler(int signum)
37 + caught_signal = signum;
40 +static void do_restart(void)
42 setenv(REEXEC_FLAG, "1", 1);
43 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: re-executing\n");
44 @@ -111,7 +118,7 @@ static void sigusr2_handler(int signum __attribute__((unused)))
45 execv(wrapper_argv[0], wrapper_argv);
48 -static void sigint_handler(int signum __attribute__((unused)))
49 +static void do_shutdown(void)
52 char **pid_strv = NULL;
53 @@ -147,25 +154,21 @@ int main(int argc, char **argv)
57 - signal(SIGINT, &sigint_handler);
58 - signal(SIGUSR2, &sigusr2_handler);
59 + struct sigaction sa;
60 + memset(&sa, 0, sizeof(struct sigaction));
61 + sa.sa_handler = &signal_handler;
62 + sigaction(SIGUSR2, &sa, NULL);
63 + sigaction(SIGINT, &sa, NULL);
65 if (getenv(REEXEC_FLAG) != NULL) {
66 /* We are being re-executed: restart HAProxy gracefully */
68 char **pid_strv = NULL;
69 int nb_pid = read_pids(&pid_strv);
72 unsetenv(REEXEC_FLAG);
73 spawn_haproxy(pid_strv, nb_pid);
75 - /* Unblock SIGUSR2 which was blocked by the signal handler
77 - sigprocmask(SIG_BLOCK, NULL, &sigs);
78 - sigdelset(&sigs, SIGUSR2);
79 - sigprocmask(SIG_SETMASK, &sigs, NULL);
81 for (i = 0; i < nb_pid; ++i)
84 @@ -176,8 +179,16 @@ int main(int argc, char **argv)
88 - while (-1 != wait(&status) || errno == EINTR)
90 + while (-1 != wait(&status) || errno == EINTR) {
91 + if (caught_signal == SIGUSR2) {
95 + else if (caught_signal == SIGINT) {
101 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: exit, haproxy RC=%d\n",