blob: f4fcab106ea07960cc8fd1cf2e287b99b2da9bd6 [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
Marc-Antoine Perennou47f922d2013-04-02 13:53:21 +020068static void spawn_haproxy(char **pid_strv, int nb_pid)
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010069{
Kristoffer Grönlund1b6e75f2013-11-22 11:06:34 +010070 char haproxy_bin[512];
71 pid_t pid;
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +030072 int main_argc;
73 char **main_argv;
74
75 main_argc = wrapper_argc - 1;
76 main_argv = wrapper_argv + 1;
Kristoffer Grönlund1b6e75f2013-11-22 11:06:34 +010077
Willy Tarreaua55bbc62014-09-24 12:59:25 +020078 pid = fork();
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010079 if (!pid) {
Willy Tarreau3747ea02016-10-25 17:05:56 +020080 char **argv;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010081 int i;
82 int argno = 0;
Willy Tarreau4351ea62016-10-25 16:49:31 +020083
Willy Tarreau3747ea02016-10-25 17:05:56 +020084 /* 3 for "haproxy -Ds -sf" */
85 argv = calloc(4 + main_argc + nb_pid + 1, sizeof(char *));
86 if (!argv) {
87 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: failed to calloc(), please try again later.\n");
88 exit(1);
89 }
90
Willy Tarreau4351ea62016-10-25 16:49:31 +020091 reset_signal_handler();
Kristoffer Grönlund1b6e75f2013-11-22 11:06:34 +010092 locate_haproxy(haproxy_bin, 512);
93 argv[argno++] = haproxy_bin;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010094 for (i = 0; i < main_argc; ++i)
95 argv[argno++] = main_argv[i];
96 argv[argno++] = "-Ds";
97 if (nb_pid > 0) {
98 argv[argno++] = "-sf";
99 for (i = 0; i < nb_pid; ++i)
100 argv[argno++] = pid_strv[i];
101 }
102 argv[argno] = NULL;
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +0100103
Apollon Oikonomopoulos6b6f3a02014-04-17 16:39:29 +0300104 fprintf(stderr, SD_DEBUG "haproxy-systemd-wrapper: executing ");
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +0100105 for (i = 0; argv[i]; ++i)
Apollon Oikonomopoulos6b6f3a02014-04-17 16:39:29 +0300106 fprintf(stderr, "%s ", argv[i]);
107 fprintf(stderr, "\n");
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +0100108
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100109 execv(argv[0], argv);
Willy Tarreaua7852692016-10-25 16:51:40 +0200110 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: execv(%s) failed, please try again later.\n", argv[0]);
Willy Tarreau7643d092016-10-25 15:50:47 +0200111 exit(1);
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100112 }
Willy Tarreaua7852692016-10-25 16:51:40 +0200113 else if (pid == -1) {
114 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: failed to fork(), please try again later.\n");
115 }
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100116}
117
118static int read_pids(char ***pid_strv)
119{
120 FILE *f = fopen(pid_file, "r");
121 int read = 0, allocated = 8;
122 char pid_str[10];
123
124 if (!f)
125 return 0;
126
127 *pid_strv = malloc(allocated * sizeof(char *));
128 while (1 == fscanf(f, "%s\n", pid_str)) {
129 if (read == allocated) {
130 allocated *= 2;
131 *pid_strv = realloc(*pid_strv, allocated * sizeof(char *));
132 }
133 (*pid_strv)[read++] = strdup(pid_str);
134 }
135
136 fclose(f);
137
138 return read;
139}
140
Conrad Hoffmann5b5ea9c2014-07-28 23:52:20 +0200141static void signal_handler(int signum)
142{
Willy Tarreaua3aa9e62016-02-27 08:26:14 +0100143 if (caught_signal != SIGINT && caught_signal != SIGTERM)
144 caught_signal = signum;
Conrad Hoffmann5b5ea9c2014-07-28 23:52:20 +0200145}
146
Willy Tarreau4351ea62016-10-25 16:49:31 +0200147static void setup_signal_handler()
148{
149 struct sigaction sa;
150
151 memset(&sa, 0, sizeof(struct sigaction));
152 sa.sa_handler = &signal_handler;
153 sigaction(SIGUSR2, &sa, NULL);
154 sigaction(SIGHUP, &sa, NULL);
155 sigaction(SIGINT, &sa, NULL);
156 sigaction(SIGTERM, &sa, NULL);
157}
158
159static void pause_signal_handler()
160{
161 signal(SIGUSR2, SIG_IGN);
162 signal(SIGHUP, SIG_IGN);
163 signal(SIGINT, SIG_DFL);
164 signal(SIGTERM, SIG_DFL);
165}
166
167static void reset_signal_handler()
168{
169 signal(SIGUSR2, SIG_DFL);
170 signal(SIGHUP, SIG_DFL);
171 signal(SIGINT, SIG_DFL);
172 signal(SIGTERM, SIG_DFL);
173}
174
Willy Tarreau2fadbe52016-02-27 08:18:04 +0100175/* handles SIGUSR2 and SIGHUP only */
176static void do_restart(int sig)
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100177{
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300178 setenv(REEXEC_FLAG, "1", 1);
Willy Tarreau2fadbe52016-02-27 08:18:04 +0100179 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: re-executing on %s.\n",
180 sig == SIGUSR2 ? "SIGUSR2" : "SIGHUP");
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100181
Willy Tarreau4351ea62016-10-25 16:49:31 +0200182 /* don't let the other process take one of those signals by accident */
183 pause_signal_handler();
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300184 execv(wrapper_argv[0], wrapper_argv);
Willy Tarreau4351ea62016-10-25 16:49:31 +0200185 /* failed, let's reinstall the signal handler and continue */
186 setup_signal_handler();
Willy Tarreaua7852692016-10-25 16:51:40 +0200187 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: re-exec(%s) failed.\n", wrapper_argv[0]);
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100188}
189
Willy Tarreau2fadbe52016-02-27 08:18:04 +0100190/* handles SIGTERM and SIGINT only */
191static void do_shutdown(int sig)
Kristoffer Grönlund66fd1d82013-11-22 11:09:39 +0100192{
193 int i, pid;
194 char **pid_strv = NULL;
195 int nb_pid = read_pids(&pid_strv);
196 for (i = 0; i < nb_pid; ++i) {
197 pid = atoi(pid_strv[i]);
198 if (pid > 0) {
Willy Tarreau2fadbe52016-02-27 08:18:04 +0100199 fprintf(stderr, SD_DEBUG "haproxy-systemd-wrapper: %s -> %d.\n",
200 sig == SIGTERM ? "SIGTERM" : "SIGINT", pid);
Willy Tarreau6c2f7952016-02-27 08:20:17 +0100201 kill(pid, sig);
Kristoffer Grönlund66fd1d82013-11-22 11:09:39 +0100202 free(pid_strv[i]);
203 }
204 }
205 free(pid_strv);
206}
207
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100208static void init(int argc, char **argv)
209{
210 while (argc > 1) {
Conrad Hoffmanneb2cf452014-07-28 23:22:43 +0200211 if ((*argv)[0] == '-' && (*argv)[1] == 'p') {
212 pid_file = *(argv + 1);
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100213 }
214 --argc; ++argv;
215 }
216}
217
218int main(int argc, char **argv)
219{
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +0100220 int status;
Willy Tarreau4351ea62016-10-25 16:49:31 +0200221
222 setup_signal_handler();
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +0100223
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300224 wrapper_argc = argc;
225 wrapper_argv = argv;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100226
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300227 --argc; ++argv;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100228 init(argc, argv);
229
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300230 if (getenv(REEXEC_FLAG) != NULL) {
231 /* We are being re-executed: restart HAProxy gracefully */
232 int i;
233 char **pid_strv = NULL;
234 int nb_pid = read_pids(&pid_strv);
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300235
236 unsetenv(REEXEC_FLAG);
237 spawn_haproxy(pid_strv, nb_pid);
238
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300239 for (i = 0; i < nb_pid; ++i)
240 free(pid_strv[i]);
241 free(pid_strv);
242 }
243 else {
244 /* Start a fresh copy of HAProxy */
245 spawn_haproxy(NULL, 0);
246 }
247
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +0100248 status = -1;
Willy Tarreau1ab5e862016-02-27 07:58:50 +0100249 while (caught_signal || wait(&status) != -1 || errno == EINTR) {
Willy Tarreau2fadbe52016-02-27 08:18:04 +0100250 int sig = caught_signal;
251
Matt Robenoltc54bdd22014-09-11 05:19:30 +0000252 if (caught_signal == SIGUSR2 || caught_signal == SIGHUP) {
Conrad Hoffmann5b5ea9c2014-07-28 23:52:20 +0200253 caught_signal = 0;
Willy Tarreau2fadbe52016-02-27 08:18:04 +0100254 do_restart(sig);
Conrad Hoffmann5b5ea9c2014-07-28 23:52:20 +0200255 }
Matt Robenoltc54bdd22014-09-11 05:19:30 +0000256 else if (caught_signal == SIGINT || caught_signal == SIGTERM) {
Conrad Hoffmann5b5ea9c2014-07-28 23:52:20 +0200257 caught_signal = 0;
Willy Tarreau2fadbe52016-02-27 08:18:04 +0100258 do_shutdown(sig);
Conrad Hoffmann5b5ea9c2014-07-28 23:52:20 +0200259 }
260 }
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100261
Apollon Oikonomopoulos6b6f3a02014-04-17 16:39:29 +0300262 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: exit, haproxy RC=%d\n",
263 status);
Apollon Oikonomopoulose8ea5982014-04-17 16:39:30 +0300264 return status;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100265}