Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 1 | /* |
| 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 Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | #include <unistd.h> |
| 19 | #include <sys/wait.h> |
| 20 | |
Apollon Oikonomopoulos | b3fce6e | 2014-04-17 16:39:28 +0300 | [diff] [blame] | 21 | #define REEXEC_FLAG "HAPROXY_SYSTEMD_REEXEC" |
Apollon Oikonomopoulos | 6b6f3a0 | 2014-04-17 16:39:29 +0300 | [diff] [blame] | 22 | #define SD_DEBUG "<7>" |
| 23 | #define SD_NOTICE "<5>" |
Apollon Oikonomopoulos | b3fce6e | 2014-04-17 16:39:28 +0300 | [diff] [blame] | 24 | |
Conrad Hoffmann | 62c8565 | 2014-07-28 23:52:20 +0200 | [diff] [blame] | 25 | static volatile sig_atomic_t caught_signal; |
| 26 | |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 27 | static char *pid_file = "/run/haproxy.pid"; |
Apollon Oikonomopoulos | b3fce6e | 2014-04-17 16:39:28 +0300 | [diff] [blame] | 28 | static int wrapper_argc; |
| 29 | static char **wrapper_argv; |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 30 | |
Willy Tarreau | c2bdae6 | 2016-10-25 16:49:31 +0200 | [diff] [blame] | 31 | static void setup_signal_handler(); |
| 32 | static void pause_signal_handler(); |
| 33 | static void reset_signal_handler(); |
| 34 | |
| 35 | |
Willy Tarreau | afbfc27 | 2014-09-19 15:42:30 +0200 | [diff] [blame] | 36 | /* 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önlund | 1b6e75f | 2013-11-22 11:06:34 +0100 | [diff] [blame] | 39 | static void locate_haproxy(char *buffer, size_t buffer_size) |
| 40 | { |
Willy Tarreau | e5eddaf | 2014-04-14 15:34:34 +0200 | [diff] [blame] | 41 | char *end = NULL; |
Willy Tarreau | afbfc27 | 2014-09-19 15:42:30 +0200 | [diff] [blame] | 42 | int len; |
Willy Tarreau | e5eddaf | 2014-04-14 15:34:34 +0200 | [diff] [blame] | 43 | |
Willy Tarreau | afbfc27 | 2014-09-19 15:42:30 +0200 | [diff] [blame] | 44 | len = readlink("/proc/self/exe", buffer, buffer_size - 1); |
| 45 | if (len == -1) |
| 46 | goto fail; |
Willy Tarreau | e5eddaf | 2014-04-14 15:34:34 +0200 | [diff] [blame] | 47 | |
Willy Tarreau | afbfc27 | 2014-09-19 15:42:30 +0200 | [diff] [blame] | 48 | 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 Tarreau | e5eddaf | 2014-04-14 15:34:34 +0200 | [diff] [blame] | 55 | return; |
| 56 | } |
Willy Tarreau | afbfc27 | 2014-09-19 15:42:30 +0200 | [diff] [blame] | 57 | |
Kristoffer Grönlund | 1b6e75f | 2013-11-22 11:06:34 +0100 | [diff] [blame] | 58 | end[1] = '\0'; |
Willy Tarreau | e5eddaf | 2014-04-14 15:34:34 +0200 | [diff] [blame] | 59 | strncpy(end + 1, "haproxy", buffer + buffer_size - (end + 1)); |
| 60 | buffer[buffer_size - 1] = '\0'; |
Willy Tarreau | afbfc27 | 2014-09-19 15:42:30 +0200 | [diff] [blame] | 61 | return; |
| 62 | fail: |
| 63 | strncpy(buffer, "/usr/sbin/haproxy", buffer_size); |
| 64 | buffer[buffer_size - 1] = '\0'; |
| 65 | return; |
Kristoffer Grönlund | 1b6e75f | 2013-11-22 11:06:34 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Marc-Antoine Perennou | 47f922d | 2013-04-02 13:53:21 +0200 | [diff] [blame] | 68 | static void spawn_haproxy(char **pid_strv, int nb_pid) |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 69 | { |
Kristoffer Grönlund | 1b6e75f | 2013-11-22 11:06:34 +0100 | [diff] [blame] | 70 | char haproxy_bin[512]; |
| 71 | pid_t pid; |
Apollon Oikonomopoulos | b3fce6e | 2014-04-17 16:39:28 +0300 | [diff] [blame] | 72 | int main_argc; |
| 73 | char **main_argv; |
| 74 | |
| 75 | main_argc = wrapper_argc - 1; |
| 76 | main_argv = wrapper_argv + 1; |
Kristoffer Grönlund | 1b6e75f | 2013-11-22 11:06:34 +0100 | [diff] [blame] | 77 | |
Willy Tarreau | 575e299 | 2014-09-24 12:59:25 +0200 | [diff] [blame] | 78 | pid = fork(); |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 79 | if (!pid) { |
Willy Tarreau | 81a0e09 | 2016-10-25 17:05:56 +0200 | [diff] [blame^] | 80 | char **argv; |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 81 | int i; |
| 82 | int argno = 0; |
Willy Tarreau | c2bdae6 | 2016-10-25 16:49:31 +0200 | [diff] [blame] | 83 | |
Willy Tarreau | 81a0e09 | 2016-10-25 17:05:56 +0200 | [diff] [blame^] | 84 | /* 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 Tarreau | c2bdae6 | 2016-10-25 16:49:31 +0200 | [diff] [blame] | 91 | reset_signal_handler(); |
Kristoffer Grönlund | 1b6e75f | 2013-11-22 11:06:34 +0100 | [diff] [blame] | 92 | locate_haproxy(haproxy_bin, 512); |
| 93 | argv[argno++] = haproxy_bin; |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 94 | 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önlund | f65194a | 2013-11-22 11:11:54 +0100 | [diff] [blame] | 103 | |
Apollon Oikonomopoulos | 6b6f3a0 | 2014-04-17 16:39:29 +0300 | [diff] [blame] | 104 | fprintf(stderr, SD_DEBUG "haproxy-systemd-wrapper: executing "); |
Kristoffer Grönlund | f65194a | 2013-11-22 11:11:54 +0100 | [diff] [blame] | 105 | for (i = 0; argv[i]; ++i) |
Apollon Oikonomopoulos | 6b6f3a0 | 2014-04-17 16:39:29 +0300 | [diff] [blame] | 106 | fprintf(stderr, "%s ", argv[i]); |
| 107 | fprintf(stderr, "\n"); |
Kristoffer Grönlund | f65194a | 2013-11-22 11:11:54 +0100 | [diff] [blame] | 108 | |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 109 | execv(argv[0], argv); |
Willy Tarreau | 9e43e2e | 2016-10-25 15:50:47 +0200 | [diff] [blame] | 110 | exit(1); |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 111 | } |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static int read_pids(char ***pid_strv) |
| 115 | { |
| 116 | FILE *f = fopen(pid_file, "r"); |
| 117 | int read = 0, allocated = 8; |
| 118 | char pid_str[10]; |
| 119 | |
| 120 | if (!f) |
| 121 | return 0; |
| 122 | |
| 123 | *pid_strv = malloc(allocated * sizeof(char *)); |
| 124 | while (1 == fscanf(f, "%s\n", pid_str)) { |
| 125 | if (read == allocated) { |
| 126 | allocated *= 2; |
| 127 | *pid_strv = realloc(*pid_strv, allocated * sizeof(char *)); |
| 128 | } |
| 129 | (*pid_strv)[read++] = strdup(pid_str); |
| 130 | } |
| 131 | |
| 132 | fclose(f); |
| 133 | |
| 134 | return read; |
| 135 | } |
| 136 | |
Conrad Hoffmann | 62c8565 | 2014-07-28 23:52:20 +0200 | [diff] [blame] | 137 | static void signal_handler(int signum) |
| 138 | { |
Willy Tarreau | a24f95d | 2016-02-27 08:26:14 +0100 | [diff] [blame] | 139 | if (caught_signal != SIGINT && caught_signal != SIGTERM) |
| 140 | caught_signal = signum; |
Conrad Hoffmann | 62c8565 | 2014-07-28 23:52:20 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Willy Tarreau | c2bdae6 | 2016-10-25 16:49:31 +0200 | [diff] [blame] | 143 | static void setup_signal_handler() |
| 144 | { |
| 145 | struct sigaction sa; |
| 146 | |
| 147 | memset(&sa, 0, sizeof(struct sigaction)); |
| 148 | sa.sa_handler = &signal_handler; |
| 149 | sigaction(SIGUSR2, &sa, NULL); |
| 150 | sigaction(SIGHUP, &sa, NULL); |
| 151 | sigaction(SIGINT, &sa, NULL); |
| 152 | sigaction(SIGTERM, &sa, NULL); |
| 153 | } |
| 154 | |
| 155 | static void pause_signal_handler() |
| 156 | { |
| 157 | signal(SIGUSR2, SIG_IGN); |
| 158 | signal(SIGHUP, SIG_IGN); |
| 159 | signal(SIGINT, SIG_DFL); |
| 160 | signal(SIGTERM, SIG_DFL); |
| 161 | } |
| 162 | |
| 163 | static void reset_signal_handler() |
| 164 | { |
| 165 | signal(SIGUSR2, SIG_DFL); |
| 166 | signal(SIGHUP, SIG_DFL); |
| 167 | signal(SIGINT, SIG_DFL); |
| 168 | signal(SIGTERM, SIG_DFL); |
| 169 | } |
| 170 | |
Willy Tarreau | 60278b9 | 2016-02-27 08:18:04 +0100 | [diff] [blame] | 171 | /* handles SIGUSR2 and SIGHUP only */ |
| 172 | static void do_restart(int sig) |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 173 | { |
Apollon Oikonomopoulos | b3fce6e | 2014-04-17 16:39:28 +0300 | [diff] [blame] | 174 | setenv(REEXEC_FLAG, "1", 1); |
Willy Tarreau | 60278b9 | 2016-02-27 08:18:04 +0100 | [diff] [blame] | 175 | fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: re-executing on %s.\n", |
| 176 | sig == SIGUSR2 ? "SIGUSR2" : "SIGHUP"); |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 177 | |
Willy Tarreau | c2bdae6 | 2016-10-25 16:49:31 +0200 | [diff] [blame] | 178 | /* don't let the other process take one of those signals by accident */ |
| 179 | pause_signal_handler(); |
Apollon Oikonomopoulos | b3fce6e | 2014-04-17 16:39:28 +0300 | [diff] [blame] | 180 | execv(wrapper_argv[0], wrapper_argv); |
Willy Tarreau | c2bdae6 | 2016-10-25 16:49:31 +0200 | [diff] [blame] | 181 | /* failed, let's reinstall the signal handler and continue */ |
| 182 | setup_signal_handler(); |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Willy Tarreau | 60278b9 | 2016-02-27 08:18:04 +0100 | [diff] [blame] | 185 | /* handles SIGTERM and SIGINT only */ |
| 186 | static void do_shutdown(int sig) |
Kristoffer Grönlund | 66fd1d8 | 2013-11-22 11:09:39 +0100 | [diff] [blame] | 187 | { |
| 188 | int i, pid; |
| 189 | char **pid_strv = NULL; |
| 190 | int nb_pid = read_pids(&pid_strv); |
| 191 | for (i = 0; i < nb_pid; ++i) { |
| 192 | pid = atoi(pid_strv[i]); |
| 193 | if (pid > 0) { |
Willy Tarreau | 60278b9 | 2016-02-27 08:18:04 +0100 | [diff] [blame] | 194 | fprintf(stderr, SD_DEBUG "haproxy-systemd-wrapper: %s -> %d.\n", |
| 195 | sig == SIGTERM ? "SIGTERM" : "SIGINT", pid); |
Willy Tarreau | 081753c | 2016-02-27 08:20:17 +0100 | [diff] [blame] | 196 | kill(pid, sig); |
Kristoffer Grönlund | 66fd1d8 | 2013-11-22 11:09:39 +0100 | [diff] [blame] | 197 | free(pid_strv[i]); |
| 198 | } |
| 199 | } |
| 200 | free(pid_strv); |
| 201 | } |
| 202 | |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 203 | static void init(int argc, char **argv) |
| 204 | { |
| 205 | while (argc > 1) { |
Conrad Hoffmann | 715e9b8 | 2014-07-28 23:22:43 +0200 | [diff] [blame] | 206 | if ((*argv)[0] == '-' && (*argv)[1] == 'p') { |
| 207 | pid_file = *(argv + 1); |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 208 | } |
| 209 | --argc; ++argv; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | int main(int argc, char **argv) |
| 214 | { |
Kristoffer Grönlund | f65194a | 2013-11-22 11:11:54 +0100 | [diff] [blame] | 215 | int status; |
Willy Tarreau | c2bdae6 | 2016-10-25 16:49:31 +0200 | [diff] [blame] | 216 | |
| 217 | setup_signal_handler(); |
Kristoffer Grönlund | f65194a | 2013-11-22 11:11:54 +0100 | [diff] [blame] | 218 | |
Apollon Oikonomopoulos | b3fce6e | 2014-04-17 16:39:28 +0300 | [diff] [blame] | 219 | wrapper_argc = argc; |
| 220 | wrapper_argv = argv; |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 221 | |
Apollon Oikonomopoulos | b3fce6e | 2014-04-17 16:39:28 +0300 | [diff] [blame] | 222 | --argc; ++argv; |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 223 | init(argc, argv); |
| 224 | |
Apollon Oikonomopoulos | b3fce6e | 2014-04-17 16:39:28 +0300 | [diff] [blame] | 225 | if (getenv(REEXEC_FLAG) != NULL) { |
| 226 | /* We are being re-executed: restart HAProxy gracefully */ |
| 227 | int i; |
| 228 | char **pid_strv = NULL; |
| 229 | int nb_pid = read_pids(&pid_strv); |
Apollon Oikonomopoulos | b3fce6e | 2014-04-17 16:39:28 +0300 | [diff] [blame] | 230 | |
| 231 | unsetenv(REEXEC_FLAG); |
| 232 | spawn_haproxy(pid_strv, nb_pid); |
| 233 | |
Apollon Oikonomopoulos | b3fce6e | 2014-04-17 16:39:28 +0300 | [diff] [blame] | 234 | for (i = 0; i < nb_pid; ++i) |
| 235 | free(pid_strv[i]); |
| 236 | free(pid_strv); |
| 237 | } |
| 238 | else { |
| 239 | /* Start a fresh copy of HAProxy */ |
| 240 | spawn_haproxy(NULL, 0); |
| 241 | } |
| 242 | |
Kristoffer Grönlund | f65194a | 2013-11-22 11:11:54 +0100 | [diff] [blame] | 243 | status = -1; |
Willy Tarreau | 1e7893c | 2016-02-27 07:58:50 +0100 | [diff] [blame] | 244 | while (caught_signal || wait(&status) != -1 || errno == EINTR) { |
Willy Tarreau | 60278b9 | 2016-02-27 08:18:04 +0100 | [diff] [blame] | 245 | int sig = caught_signal; |
| 246 | |
Matt Robenolt | 6bb7bf7 | 2014-09-11 05:19:30 +0000 | [diff] [blame] | 247 | if (caught_signal == SIGUSR2 || caught_signal == SIGHUP) { |
Conrad Hoffmann | 62c8565 | 2014-07-28 23:52:20 +0200 | [diff] [blame] | 248 | caught_signal = 0; |
Willy Tarreau | 60278b9 | 2016-02-27 08:18:04 +0100 | [diff] [blame] | 249 | do_restart(sig); |
Conrad Hoffmann | 62c8565 | 2014-07-28 23:52:20 +0200 | [diff] [blame] | 250 | } |
Matt Robenolt | 6bb7bf7 | 2014-09-11 05:19:30 +0000 | [diff] [blame] | 251 | else if (caught_signal == SIGINT || caught_signal == SIGTERM) { |
Conrad Hoffmann | 62c8565 | 2014-07-28 23:52:20 +0200 | [diff] [blame] | 252 | caught_signal = 0; |
Willy Tarreau | 60278b9 | 2016-02-27 08:18:04 +0100 | [diff] [blame] | 253 | do_shutdown(sig); |
Conrad Hoffmann | 62c8565 | 2014-07-28 23:52:20 +0200 | [diff] [blame] | 254 | } |
| 255 | } |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 256 | |
Apollon Oikonomopoulos | 6b6f3a0 | 2014-04-17 16:39:29 +0300 | [diff] [blame] | 257 | fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: exit, haproxy RC=%d\n", |
| 258 | status); |
Apollon Oikonomopoulos | e8ea598 | 2014-04-17 16:39:30 +0300 | [diff] [blame] | 259 | return status; |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 260 | } |