b745406f685fb3f735f15c49cd8197bf69e150d6
[feed/packages.git] / net / haproxy / patches / 0003-MEDIUM-Improve-signal-handling-in-systemd-wrapper.patch
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.
5
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.
10
11 Signed-off-by: Conrad Hoffmann <conrad@soundcloud.com>
12 (cherry picked from commit 5b5ea9c93384da49eea0f67ebed0966d4167b17a)
13 ---
14 src/haproxy-systemd-wrapper.c | 37 ++++++++++++++++++++++++-------------
15 1 file changed, 24 insertions(+), 13 deletions(-)
16
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
21 @@ -22,6 +22,8 @@
22 #define SD_DEBUG "<7>"
23 #define SD_NOTICE "<5>"
24
25 +static volatile sig_atomic_t caught_signal;
26 +
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)
31 return read;
32 }
33
34 -static void sigusr2_handler(int signum __attribute__((unused)))
35 +static void signal_handler(int signum)
36 +{
37 + caught_signal = signum;
38 +}
39 +
40 +static void do_restart(void)
41 {
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);
46 }
47
48 -static void sigint_handler(int signum __attribute__((unused)))
49 +static void do_shutdown(void)
50 {
51 int i, pid;
52 char **pid_strv = NULL;
53 @@ -147,25 +154,21 @@ int main(int argc, char **argv)
54 --argc; ++argv;
55 init(argc, argv);
56
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);
64
65 if (getenv(REEXEC_FLAG) != NULL) {
66 /* We are being re-executed: restart HAProxy gracefully */
67 int i;
68 char **pid_strv = NULL;
69 int nb_pid = read_pids(&pid_strv);
70 - sigset_t sigs;
71
72 unsetenv(REEXEC_FLAG);
73 spawn_haproxy(pid_strv, nb_pid);
74
75 - /* Unblock SIGUSR2 which was blocked by the signal handler
76 - * before re-exec */
77 - sigprocmask(SIG_BLOCK, NULL, &sigs);
78 - sigdelset(&sigs, SIGUSR2);
79 - sigprocmask(SIG_SETMASK, &sigs, NULL);
80 -
81 for (i = 0; i < nb_pid; ++i)
82 free(pid_strv[i]);
83 free(pid_strv);
84 @@ -176,8 +179,16 @@ int main(int argc, char **argv)
85 }
86
87 status = -1;
88 - while (-1 != wait(&status) || errno == EINTR)
89 - ;
90 + while (-1 != wait(&status) || errno == EINTR) {
91 + if (caught_signal == SIGUSR2) {
92 + caught_signal = 0;
93 + do_restart();
94 + }
95 + else if (caught_signal == SIGINT) {
96 + caught_signal = 0;
97 + do_shutdown();
98 + }
99 + }
100
101 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: exit, haproxy RC=%d\n",
102 status);
103 --
104 1.8.5.5
105