blob: f6a9c852b277b5f193d6a952119a149b4cfe3e8b [file] [log] [blame]
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +01001/*
2 * Wrapper to make haproxy systemd-compliant.
3 *
4 * Copyright 2013 Marc-Antoine Perennou <Marc-Antoine@Perennou.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <signal.h>
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010015#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <unistd.h>
19#include <sys/wait.h>
20
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +030021#define REEXEC_FLAG "HAPROXY_SYSTEMD_REEXEC"
Apollon Oikonomopoulos6b6f3a02014-04-17 16:39:29 +030022#define SD_DEBUG "<7>"
23#define SD_NOTICE "<5>"
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +030024
Conrad Hoffmann62c85652014-07-28 23:52:20 +020025static volatile sig_atomic_t caught_signal;
26
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010027static char *pid_file = "/run/haproxy.pid";
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +030028static int wrapper_argc;
29static char **wrapper_argv;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010030
Willy Tarreauc2bdae62016-10-25 16:49:31 +020031static void setup_signal_handler();
32static void pause_signal_handler();
33static void reset_signal_handler();
34
35
Willy Tarreauafbfc272014-09-19 15:42:30 +020036/* returns the path to the haproxy binary into <buffer>, whose size indicated
37 * in <buffer_size> must be at least 1 byte long.
38 */
Kristoffer Grönlund1b6e75f2013-11-22 11:06:34 +010039static void locate_haproxy(char *buffer, size_t buffer_size)
40{
Willy Tarreaue5eddaf2014-04-14 15:34:34 +020041 char *end = NULL;
Willy Tarreauafbfc272014-09-19 15:42:30 +020042 int len;
Willy Tarreaue5eddaf2014-04-14 15:34:34 +020043
Willy Tarreauafbfc272014-09-19 15:42:30 +020044 len = readlink("/proc/self/exe", buffer, buffer_size - 1);
45 if (len == -1)
46 goto fail;
Willy Tarreaue5eddaf2014-04-14 15:34:34 +020047
Willy Tarreauafbfc272014-09-19 15:42:30 +020048 buffer[len] = 0;
49 end = strrchr(buffer, '/');
50 if (end == NULL)
51 goto fail;
52
53 if (strcmp(end + strlen(end) - 16, "-systemd-wrapper") == 0) {
54 end[strlen(end) - 16] = '\0';
Willy Tarreaue5eddaf2014-04-14 15:34:34 +020055 return;
56 }
Willy Tarreauafbfc272014-09-19 15:42:30 +020057
Kristoffer Grönlund1b6e75f2013-11-22 11:06:34 +010058 end[1] = '\0';
Willy Tarreaue5eddaf2014-04-14 15:34:34 +020059 strncpy(end + 1, "haproxy", buffer + buffer_size - (end + 1));
60 buffer[buffer_size - 1] = '\0';
Willy Tarreauafbfc272014-09-19 15:42:30 +020061 return;
62 fail:
63 strncpy(buffer, "/usr/sbin/haproxy", buffer_size);
64 buffer[buffer_size - 1] = '\0';
65 return;
Kristoffer Grönlund1b6e75f2013-11-22 11:06:34 +010066}
67
Willy Tarreau838191d2016-10-25 17:20:24 +020068/* Note: this function must not exit in case of error (except in the child), as
69 * it is only dedicated the starting a new haproxy process. By keeping the
70 * process alive it will ensure that future signal delivery may get rid of
71 * the issue. If the first startup fails, the wrapper will notice it and
72 * return an error thanks to wait() returning ECHILD.
73 */
Marc-Antoine Perennou47f922d2013-04-02 13:53:21 +020074static void spawn_haproxy(char **pid_strv, int nb_pid)
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010075{
Kristoffer Grönlund1b6e75f2013-11-22 11:06:34 +010076 char haproxy_bin[512];
77 pid_t pid;
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +030078 int main_argc;
79 char **main_argv;
Willy Tarreau838191d2016-10-25 17:20:24 +020080 int pipefd[2];
81 char fdstr[20];
82 int ret;
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +030083
84 main_argc = wrapper_argc - 1;
85 main_argv = wrapper_argv + 1;
Kristoffer Grönlund1b6e75f2013-11-22 11:06:34 +010086
Willy Tarreau838191d2016-10-25 17:20:24 +020087 if (pipe(pipefd) != 0) {
88 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: failed to create a pipe, please try again later.\n");
89 return;
90 }
91
Willy Tarreau575e2992014-09-24 12:59:25 +020092 pid = fork();
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010093 if (!pid) {
Willy Tarreau81a0e092016-10-25 17:05:56 +020094 char **argv;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010095 int i;
96 int argno = 0;
Willy Tarreauc2bdae62016-10-25 16:49:31 +020097
Willy Tarreau81a0e092016-10-25 17:05:56 +020098 /* 3 for "haproxy -Ds -sf" */
99 argv = calloc(4 + main_argc + nb_pid + 1, sizeof(char *));
100 if (!argv) {
101 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: failed to calloc(), please try again later.\n");
102 exit(1);
103 }
104
Willy Tarreauc2bdae62016-10-25 16:49:31 +0200105 reset_signal_handler();
Willy Tarreau838191d2016-10-25 17:20:24 +0200106
107 close(pipefd[0]); /* close the read side */
108
109 snprintf(fdstr, sizeof(fdstr), "%d", pipefd[1]);
110 if (setenv("HAPROXY_WRAPPER_FD", fdstr, 1) != 0) {
111 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: failed to setenv(), please try again later.\n");
112 exit(1);
113 }
114
Kristoffer Grönlund1b6e75f2013-11-22 11:06:34 +0100115 locate_haproxy(haproxy_bin, 512);
116 argv[argno++] = haproxy_bin;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100117 for (i = 0; i < main_argc; ++i)
118 argv[argno++] = main_argv[i];
119 argv[argno++] = "-Ds";
120 if (nb_pid > 0) {
121 argv[argno++] = "-sf";
122 for (i = 0; i < nb_pid; ++i)
123 argv[argno++] = pid_strv[i];
124 }
125 argv[argno] = NULL;
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +0100126
Apollon Oikonomopoulos6b6f3a02014-04-17 16:39:29 +0300127 fprintf(stderr, SD_DEBUG "haproxy-systemd-wrapper: executing ");
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +0100128 for (i = 0; argv[i]; ++i)
Apollon Oikonomopoulos6b6f3a02014-04-17 16:39:29 +0300129 fprintf(stderr, "%s ", argv[i]);
130 fprintf(stderr, "\n");
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +0100131
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100132 execv(argv[0], argv);
Willy Tarreaud019fe22016-10-25 16:51:40 +0200133 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: execv(%s) failed, please try again later.\n", argv[0]);
Willy Tarreau9e43e2e2016-10-25 15:50:47 +0200134 exit(1);
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100135 }
Willy Tarreaud019fe22016-10-25 16:51:40 +0200136 else if (pid == -1) {
137 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: failed to fork(), please try again later.\n");
138 }
Willy Tarreau838191d2016-10-25 17:20:24 +0200139
140 /* The parent closes the write side and waits for the child to close it
141 * as well. Also deal the case where the fd would unexpectedly be 1 or 2
142 * by silently draining all data.
143 */
144 close(pipefd[1]);
145
146 do {
147 char c;
148 ret = read(pipefd[0], &c, sizeof(c));
149 } while ((ret > 0) || (ret == -1 && errno == EINTR));
150 /* the child has finished starting up */
151 close(pipefd[0]);
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100152}
153
154static int read_pids(char ***pid_strv)
155{
156 FILE *f = fopen(pid_file, "r");
157 int read = 0, allocated = 8;
158 char pid_str[10];
159
160 if (!f)
161 return 0;
162
163 *pid_strv = malloc(allocated * sizeof(char *));
164 while (1 == fscanf(f, "%s\n", pid_str)) {
165 if (read == allocated) {
166 allocated *= 2;
167 *pid_strv = realloc(*pid_strv, allocated * sizeof(char *));
168 }
169 (*pid_strv)[read++] = strdup(pid_str);
170 }
171
172 fclose(f);
173
174 return read;
175}
176
Conrad Hoffmann62c85652014-07-28 23:52:20 +0200177static void signal_handler(int signum)
178{
Willy Tarreaua24f95d2016-02-27 08:26:14 +0100179 if (caught_signal != SIGINT && caught_signal != SIGTERM)
180 caught_signal = signum;
Conrad Hoffmann62c85652014-07-28 23:52:20 +0200181}
182
Willy Tarreauc2bdae62016-10-25 16:49:31 +0200183static void setup_signal_handler()
184{
185 struct sigaction sa;
186
187 memset(&sa, 0, sizeof(struct sigaction));
188 sa.sa_handler = &signal_handler;
189 sigaction(SIGUSR2, &sa, NULL);
190 sigaction(SIGHUP, &sa, NULL);
191 sigaction(SIGINT, &sa, NULL);
192 sigaction(SIGTERM, &sa, NULL);
193}
194
195static void pause_signal_handler()
196{
197 signal(SIGUSR2, SIG_IGN);
198 signal(SIGHUP, SIG_IGN);
199 signal(SIGINT, SIG_DFL);
200 signal(SIGTERM, SIG_DFL);
201}
202
203static void reset_signal_handler()
204{
205 signal(SIGUSR2, SIG_DFL);
206 signal(SIGHUP, SIG_DFL);
207 signal(SIGINT, SIG_DFL);
208 signal(SIGTERM, SIG_DFL);
209}
210
Willy Tarreau60278b92016-02-27 08:18:04 +0100211/* handles SIGUSR2 and SIGHUP only */
212static void do_restart(int sig)
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100213{
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300214 setenv(REEXEC_FLAG, "1", 1);
Willy Tarreau60278b92016-02-27 08:18:04 +0100215 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: re-executing on %s.\n",
216 sig == SIGUSR2 ? "SIGUSR2" : "SIGHUP");
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100217
Willy Tarreauc2bdae62016-10-25 16:49:31 +0200218 /* don't let the other process take one of those signals by accident */
219 pause_signal_handler();
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300220 execv(wrapper_argv[0], wrapper_argv);
Willy Tarreauc2bdae62016-10-25 16:49:31 +0200221 /* failed, let's reinstall the signal handler and continue */
222 setup_signal_handler();
Willy Tarreaud019fe22016-10-25 16:51:40 +0200223 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: re-exec(%s) failed.\n", wrapper_argv[0]);
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100224}
225
Willy Tarreau60278b92016-02-27 08:18:04 +0100226/* handles SIGTERM and SIGINT only */
227static void do_shutdown(int sig)
Kristoffer Grönlund66fd1d82013-11-22 11:09:39 +0100228{
229 int i, pid;
230 char **pid_strv = NULL;
231 int nb_pid = read_pids(&pid_strv);
232 for (i = 0; i < nb_pid; ++i) {
233 pid = atoi(pid_strv[i]);
234 if (pid > 0) {
Willy Tarreau60278b92016-02-27 08:18:04 +0100235 fprintf(stderr, SD_DEBUG "haproxy-systemd-wrapper: %s -> %d.\n",
236 sig == SIGTERM ? "SIGTERM" : "SIGINT", pid);
Willy Tarreau081753c2016-02-27 08:20:17 +0100237 kill(pid, sig);
Kristoffer Grönlund66fd1d82013-11-22 11:09:39 +0100238 free(pid_strv[i]);
239 }
240 }
241 free(pid_strv);
242}
243
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100244static void init(int argc, char **argv)
245{
246 while (argc > 1) {
Conrad Hoffmann715e9b82014-07-28 23:22:43 +0200247 if ((*argv)[0] == '-' && (*argv)[1] == 'p') {
248 pid_file = *(argv + 1);
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100249 }
250 --argc; ++argv;
251 }
252}
253
254int main(int argc, char **argv)
255{
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +0100256 int status;
Willy Tarreauc2bdae62016-10-25 16:49:31 +0200257
258 setup_signal_handler();
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +0100259
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300260 wrapper_argc = argc;
261 wrapper_argv = argv;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100262
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300263 --argc; ++argv;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100264 init(argc, argv);
265
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300266 if (getenv(REEXEC_FLAG) != NULL) {
267 /* We are being re-executed: restart HAProxy gracefully */
268 int i;
269 char **pid_strv = NULL;
270 int nb_pid = read_pids(&pid_strv);
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300271
272 unsetenv(REEXEC_FLAG);
273 spawn_haproxy(pid_strv, nb_pid);
274
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300275 for (i = 0; i < nb_pid; ++i)
276 free(pid_strv[i]);
277 free(pid_strv);
278 }
279 else {
280 /* Start a fresh copy of HAProxy */
281 spawn_haproxy(NULL, 0);
282 }
283
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +0100284 status = -1;
Willy Tarreau1e7893c2016-02-27 07:58:50 +0100285 while (caught_signal || wait(&status) != -1 || errno == EINTR) {
Willy Tarreau60278b92016-02-27 08:18:04 +0100286 int sig = caught_signal;
287
Matt Robenolt6bb7bf72014-09-11 05:19:30 +0000288 if (caught_signal == SIGUSR2 || caught_signal == SIGHUP) {
Conrad Hoffmann62c85652014-07-28 23:52:20 +0200289 caught_signal = 0;
Willy Tarreau60278b92016-02-27 08:18:04 +0100290 do_restart(sig);
Conrad Hoffmann62c85652014-07-28 23:52:20 +0200291 }
Matt Robenolt6bb7bf72014-09-11 05:19:30 +0000292 else if (caught_signal == SIGINT || caught_signal == SIGTERM) {
Conrad Hoffmann62c85652014-07-28 23:52:20 +0200293 caught_signal = 0;
Willy Tarreau60278b92016-02-27 08:18:04 +0100294 do_shutdown(sig);
Conrad Hoffmann62c85652014-07-28 23:52:20 +0200295 }
296 }
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100297
Willy Tarreau424ab792016-11-03 20:31:40 +0100298 /* return either exit code or signal+128 */
299 if (WIFEXITED(status))
300 status = WEXITSTATUS(status);
301 else if (WIFSIGNALED(status))
302 status = 128 + WTERMSIG(status);
303 else if (WIFSTOPPED(status))
304 status = 128 + WSTOPSIG(status);
305 else
306 status = 255;
307
Apollon Oikonomopoulos6b6f3a02014-04-17 16:39:29 +0300308 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: exit, haproxy RC=%d\n",
309 status);
Apollon Oikonomopoulose8ea5982014-04-17 16:39:30 +0300310 return status;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100311}