blob: 457f5bd57133f5bd7b625e03425c236688b26494 [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 Hoffmann5b5ea9c2014-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 Tarreau4351ea62016-10-25 16:49:31 +020031static void setup_signal_handler();
32static void pause_signal_handler();
33static void reset_signal_handler();
34
35
Willy Tarreauceaf2ae2014-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 Tarreauceaf2ae2014-09-19 15:42:30 +020042 int len;
Willy Tarreaue5eddaf2014-04-14 15:34:34 +020043
Willy Tarreauceaf2ae2014-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 Tarreauceaf2ae2014-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 Tarreauceaf2ae2014-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 Tarreauceaf2ae2014-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 Tarreaub9571092016-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 Tarreaub9571092016-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 Tarreaub9571092016-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 Tarreaua55bbc62014-09-24 12:59:25 +020092 pid = fork();
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010093 if (!pid) {
Willy Tarreau3747ea02016-10-25 17:05:56 +020094 char **argv;
Olivier Houchard2c9744f2017-04-09 16:28:10 +020095 char *stats_socket = NULL;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010096 int i;
97 int argno = 0;
Willy Tarreau4351ea62016-10-25 16:49:31 +020098
Willy Tarreau3747ea02016-10-25 17:05:56 +020099 /* 3 for "haproxy -Ds -sf" */
Olivier Houchard2c9744f2017-04-09 16:28:10 +0200100 if (nb_pid > 0)
101 stats_socket = getenv("HAPROXY_STATS_SOCKET");
102 argv = calloc(4 + main_argc + nb_pid + 1 +
103 (stats_socket != NULL ? 2 : 0), sizeof(char *));
Willy Tarreau3747ea02016-10-25 17:05:56 +0200104 if (!argv) {
105 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: failed to calloc(), please try again later.\n");
106 exit(1);
107 }
108
Willy Tarreau4351ea62016-10-25 16:49:31 +0200109 reset_signal_handler();
Willy Tarreaub9571092016-10-25 17:20:24 +0200110
111 close(pipefd[0]); /* close the read side */
112
113 snprintf(fdstr, sizeof(fdstr), "%d", pipefd[1]);
114 if (setenv("HAPROXY_WRAPPER_FD", fdstr, 1) != 0) {
115 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: failed to setenv(), please try again later.\n");
116 exit(1);
117 }
118
Kristoffer Grönlund1b6e75f2013-11-22 11:06:34 +0100119 locate_haproxy(haproxy_bin, 512);
120 argv[argno++] = haproxy_bin;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100121 for (i = 0; i < main_argc; ++i)
122 argv[argno++] = main_argv[i];
123 argv[argno++] = "-Ds";
124 if (nb_pid > 0) {
125 argv[argno++] = "-sf";
126 for (i = 0; i < nb_pid; ++i)
127 argv[argno++] = pid_strv[i];
Olivier Houchard2c9744f2017-04-09 16:28:10 +0200128 if (stats_socket != NULL) {
129 argv[argno++] = "-x";
130 argv[argno++] = stats_socket;
131 }
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100132 }
133 argv[argno] = NULL;
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +0100134
Apollon Oikonomopoulos6b6f3a02014-04-17 16:39:29 +0300135 fprintf(stderr, SD_DEBUG "haproxy-systemd-wrapper: executing ");
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +0100136 for (i = 0; argv[i]; ++i)
Apollon Oikonomopoulos6b6f3a02014-04-17 16:39:29 +0300137 fprintf(stderr, "%s ", argv[i]);
138 fprintf(stderr, "\n");
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +0100139
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100140 execv(argv[0], argv);
Willy Tarreaua7852692016-10-25 16:51:40 +0200141 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: execv(%s) failed, please try again later.\n", argv[0]);
Willy Tarreau7643d092016-10-25 15:50:47 +0200142 exit(1);
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100143 }
Willy Tarreaua7852692016-10-25 16:51:40 +0200144 else if (pid == -1) {
145 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: failed to fork(), please try again later.\n");
146 }
Willy Tarreaub9571092016-10-25 17:20:24 +0200147
148 /* The parent closes the write side and waits for the child to close it
149 * as well. Also deal the case where the fd would unexpectedly be 1 or 2
150 * by silently draining all data.
151 */
152 close(pipefd[1]);
153
154 do {
155 char c;
156 ret = read(pipefd[0], &c, sizeof(c));
157 } while ((ret > 0) || (ret == -1 && errno == EINTR));
158 /* the child has finished starting up */
159 close(pipefd[0]);
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100160}
161
162static int read_pids(char ***pid_strv)
163{
164 FILE *f = fopen(pid_file, "r");
165 int read = 0, allocated = 8;
166 char pid_str[10];
167
168 if (!f)
169 return 0;
170
171 *pid_strv = malloc(allocated * sizeof(char *));
172 while (1 == fscanf(f, "%s\n", pid_str)) {
173 if (read == allocated) {
174 allocated *= 2;
175 *pid_strv = realloc(*pid_strv, allocated * sizeof(char *));
176 }
177 (*pid_strv)[read++] = strdup(pid_str);
178 }
179
180 fclose(f);
181
182 return read;
183}
184
Conrad Hoffmann5b5ea9c2014-07-28 23:52:20 +0200185static void signal_handler(int signum)
186{
Willy Tarreaua3aa9e62016-02-27 08:26:14 +0100187 if (caught_signal != SIGINT && caught_signal != SIGTERM)
188 caught_signal = signum;
Conrad Hoffmann5b5ea9c2014-07-28 23:52:20 +0200189}
190
Willy Tarreau4351ea62016-10-25 16:49:31 +0200191static void setup_signal_handler()
192{
193 struct sigaction sa;
194
195 memset(&sa, 0, sizeof(struct sigaction));
196 sa.sa_handler = &signal_handler;
197 sigaction(SIGUSR2, &sa, NULL);
198 sigaction(SIGHUP, &sa, NULL);
199 sigaction(SIGINT, &sa, NULL);
200 sigaction(SIGTERM, &sa, NULL);
201}
202
203static void pause_signal_handler()
204{
205 signal(SIGUSR2, SIG_IGN);
206 signal(SIGHUP, SIG_IGN);
207 signal(SIGINT, SIG_DFL);
208 signal(SIGTERM, SIG_DFL);
209}
210
211static void reset_signal_handler()
212{
213 signal(SIGUSR2, SIG_DFL);
214 signal(SIGHUP, SIG_DFL);
215 signal(SIGINT, SIG_DFL);
216 signal(SIGTERM, SIG_DFL);
217}
218
Willy Tarreau2fadbe52016-02-27 08:18:04 +0100219/* handles SIGUSR2 and SIGHUP only */
220static void do_restart(int sig)
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100221{
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300222 setenv(REEXEC_FLAG, "1", 1);
Willy Tarreau2fadbe52016-02-27 08:18:04 +0100223 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: re-executing on %s.\n",
224 sig == SIGUSR2 ? "SIGUSR2" : "SIGHUP");
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100225
Willy Tarreau4351ea62016-10-25 16:49:31 +0200226 /* don't let the other process take one of those signals by accident */
227 pause_signal_handler();
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300228 execv(wrapper_argv[0], wrapper_argv);
Willy Tarreau4351ea62016-10-25 16:49:31 +0200229 /* failed, let's reinstall the signal handler and continue */
230 setup_signal_handler();
Willy Tarreaua7852692016-10-25 16:51:40 +0200231 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: re-exec(%s) failed.\n", wrapper_argv[0]);
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100232}
233
Willy Tarreau2fadbe52016-02-27 08:18:04 +0100234/* handles SIGTERM and SIGINT only */
235static void do_shutdown(int sig)
Kristoffer Grönlund66fd1d82013-11-22 11:09:39 +0100236{
237 int i, pid;
238 char **pid_strv = NULL;
239 int nb_pid = read_pids(&pid_strv);
240 for (i = 0; i < nb_pid; ++i) {
241 pid = atoi(pid_strv[i]);
242 if (pid > 0) {
Willy Tarreau2fadbe52016-02-27 08:18:04 +0100243 fprintf(stderr, SD_DEBUG "haproxy-systemd-wrapper: %s -> %d.\n",
244 sig == SIGTERM ? "SIGTERM" : "SIGINT", pid);
Willy Tarreau6c2f7952016-02-27 08:20:17 +0100245 kill(pid, sig);
Kristoffer Grönlund66fd1d82013-11-22 11:09:39 +0100246 free(pid_strv[i]);
247 }
248 }
249 free(pid_strv);
250}
251
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100252static void init(int argc, char **argv)
253{
254 while (argc > 1) {
Conrad Hoffmanneb2cf452014-07-28 23:22:43 +0200255 if ((*argv)[0] == '-' && (*argv)[1] == 'p') {
256 pid_file = *(argv + 1);
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100257 }
258 --argc; ++argv;
259 }
260}
261
262int main(int argc, char **argv)
263{
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +0100264 int status;
Willy Tarreau4351ea62016-10-25 16:49:31 +0200265
266 setup_signal_handler();
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +0100267
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300268 wrapper_argc = argc;
269 wrapper_argv = argv;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100270
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300271 --argc; ++argv;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100272 init(argc, argv);
273
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300274 if (getenv(REEXEC_FLAG) != NULL) {
275 /* We are being re-executed: restart HAProxy gracefully */
276 int i;
277 char **pid_strv = NULL;
278 int nb_pid = read_pids(&pid_strv);
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300279
280 unsetenv(REEXEC_FLAG);
281 spawn_haproxy(pid_strv, nb_pid);
282
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300283 for (i = 0; i < nb_pid; ++i)
284 free(pid_strv[i]);
285 free(pid_strv);
286 }
287 else {
288 /* Start a fresh copy of HAProxy */
289 spawn_haproxy(NULL, 0);
290 }
291
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +0100292 status = -1;
Willy Tarreau1ab5e862016-02-27 07:58:50 +0100293 while (caught_signal || wait(&status) != -1 || errno == EINTR) {
Willy Tarreau2fadbe52016-02-27 08:18:04 +0100294 int sig = caught_signal;
295
Matt Robenoltc54bdd22014-09-11 05:19:30 +0000296 if (caught_signal == SIGUSR2 || caught_signal == SIGHUP) {
Conrad Hoffmann5b5ea9c2014-07-28 23:52:20 +0200297 caught_signal = 0;
Willy Tarreau2fadbe52016-02-27 08:18:04 +0100298 do_restart(sig);
Conrad Hoffmann5b5ea9c2014-07-28 23:52:20 +0200299 }
Matt Robenoltc54bdd22014-09-11 05:19:30 +0000300 else if (caught_signal == SIGINT || caught_signal == SIGTERM) {
Conrad Hoffmann5b5ea9c2014-07-28 23:52:20 +0200301 caught_signal = 0;
Willy Tarreau2fadbe52016-02-27 08:18:04 +0100302 do_shutdown(sig);
Conrad Hoffmann5b5ea9c2014-07-28 23:52:20 +0200303 }
304 }
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100305
Willy Tarreauf7659cb2016-11-03 20:31:40 +0100306 /* return either exit code or signal+128 */
307 if (WIFEXITED(status))
308 status = WEXITSTATUS(status);
309 else if (WIFSIGNALED(status))
310 status = 128 + WTERMSIG(status);
311 else if (WIFSTOPPED(status))
312 status = 128 + WSTOPSIG(status);
313 else
314 status = 255;
315
Apollon Oikonomopoulos6b6f3a02014-04-17 16:39:29 +0300316 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: exit, haproxy RC=%d\n",
317 status);
Apollon Oikonomopoulose8ea5982014-04-17 16:39:30 +0300318 return status;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100319}