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 | afbfc27 | 2014-09-19 15:42:30 +0200 | [diff] [blame] | 31 | /* returns the path to the haproxy binary into <buffer>, whose size indicated |
| 32 | * in <buffer_size> must be at least 1 byte long. |
| 33 | */ |
Kristoffer Grönlund | 1b6e75f | 2013-11-22 11:06:34 +0100 | [diff] [blame] | 34 | static void locate_haproxy(char *buffer, size_t buffer_size) |
| 35 | { |
Willy Tarreau | e5eddaf | 2014-04-14 15:34:34 +0200 | [diff] [blame] | 36 | char *end = NULL; |
Willy Tarreau | afbfc27 | 2014-09-19 15:42:30 +0200 | [diff] [blame] | 37 | int len; |
Willy Tarreau | e5eddaf | 2014-04-14 15:34:34 +0200 | [diff] [blame] | 38 | |
Willy Tarreau | afbfc27 | 2014-09-19 15:42:30 +0200 | [diff] [blame] | 39 | len = readlink("/proc/self/exe", buffer, buffer_size - 1); |
| 40 | if (len == -1) |
| 41 | goto fail; |
Willy Tarreau | e5eddaf | 2014-04-14 15:34:34 +0200 | [diff] [blame] | 42 | |
Willy Tarreau | afbfc27 | 2014-09-19 15:42:30 +0200 | [diff] [blame] | 43 | buffer[len] = 0; |
| 44 | end = strrchr(buffer, '/'); |
| 45 | if (end == NULL) |
| 46 | goto fail; |
| 47 | |
| 48 | if (strcmp(end + strlen(end) - 16, "-systemd-wrapper") == 0) { |
| 49 | end[strlen(end) - 16] = '\0'; |
Willy Tarreau | e5eddaf | 2014-04-14 15:34:34 +0200 | [diff] [blame] | 50 | return; |
| 51 | } |
Willy Tarreau | afbfc27 | 2014-09-19 15:42:30 +0200 | [diff] [blame] | 52 | |
Kristoffer Grönlund | 1b6e75f | 2013-11-22 11:06:34 +0100 | [diff] [blame] | 53 | end[1] = '\0'; |
Willy Tarreau | e5eddaf | 2014-04-14 15:34:34 +0200 | [diff] [blame] | 54 | strncpy(end + 1, "haproxy", buffer + buffer_size - (end + 1)); |
| 55 | buffer[buffer_size - 1] = '\0'; |
Willy Tarreau | afbfc27 | 2014-09-19 15:42:30 +0200 | [diff] [blame] | 56 | return; |
| 57 | fail: |
| 58 | strncpy(buffer, "/usr/sbin/haproxy", buffer_size); |
| 59 | buffer[buffer_size - 1] = '\0'; |
| 60 | return; |
Kristoffer Grönlund | 1b6e75f | 2013-11-22 11:06:34 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Marc-Antoine Perennou | 47f922d | 2013-04-02 13:53:21 +0200 | [diff] [blame] | 63 | static void spawn_haproxy(char **pid_strv, int nb_pid) |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 64 | { |
Kristoffer Grönlund | 1b6e75f | 2013-11-22 11:06:34 +0100 | [diff] [blame] | 65 | char haproxy_bin[512]; |
| 66 | pid_t pid; |
Apollon Oikonomopoulos | b3fce6e | 2014-04-17 16:39:28 +0300 | [diff] [blame] | 67 | int main_argc; |
| 68 | char **main_argv; |
| 69 | |
| 70 | main_argc = wrapper_argc - 1; |
| 71 | main_argv = wrapper_argv + 1; |
Kristoffer Grönlund | 1b6e75f | 2013-11-22 11:06:34 +0100 | [diff] [blame] | 72 | |
Willy Tarreau | 575e299 | 2014-09-24 12:59:25 +0200 | [diff] [blame] | 73 | pid = fork(); |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 74 | if (!pid) { |
| 75 | /* 3 for "haproxy -Ds -sf" */ |
| 76 | char **argv = calloc(4 + main_argc + nb_pid + 1, sizeof(char *)); |
| 77 | int i; |
| 78 | int argno = 0; |
Kristoffer Grönlund | 1b6e75f | 2013-11-22 11:06:34 +0100 | [diff] [blame] | 79 | locate_haproxy(haproxy_bin, 512); |
| 80 | argv[argno++] = haproxy_bin; |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 81 | for (i = 0; i < main_argc; ++i) |
| 82 | argv[argno++] = main_argv[i]; |
| 83 | argv[argno++] = "-Ds"; |
| 84 | if (nb_pid > 0) { |
| 85 | argv[argno++] = "-sf"; |
| 86 | for (i = 0; i < nb_pid; ++i) |
| 87 | argv[argno++] = pid_strv[i]; |
| 88 | } |
| 89 | argv[argno] = NULL; |
Kristoffer Grönlund | f65194a | 2013-11-22 11:11:54 +0100 | [diff] [blame] | 90 | |
Apollon Oikonomopoulos | 6b6f3a0 | 2014-04-17 16:39:29 +0300 | [diff] [blame] | 91 | fprintf(stderr, SD_DEBUG "haproxy-systemd-wrapper: executing "); |
Kristoffer Grönlund | f65194a | 2013-11-22 11:11:54 +0100 | [diff] [blame] | 92 | for (i = 0; argv[i]; ++i) |
Apollon Oikonomopoulos | 6b6f3a0 | 2014-04-17 16:39:29 +0300 | [diff] [blame] | 93 | fprintf(stderr, "%s ", argv[i]); |
| 94 | fprintf(stderr, "\n"); |
Kristoffer Grönlund | f65194a | 2013-11-22 11:11:54 +0100 | [diff] [blame] | 95 | |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 96 | execv(argv[0], argv); |
| 97 | exit(0); |
| 98 | } |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static int read_pids(char ***pid_strv) |
| 102 | { |
| 103 | FILE *f = fopen(pid_file, "r"); |
| 104 | int read = 0, allocated = 8; |
| 105 | char pid_str[10]; |
| 106 | |
| 107 | if (!f) |
| 108 | return 0; |
| 109 | |
| 110 | *pid_strv = malloc(allocated * sizeof(char *)); |
| 111 | while (1 == fscanf(f, "%s\n", pid_str)) { |
| 112 | if (read == allocated) { |
| 113 | allocated *= 2; |
| 114 | *pid_strv = realloc(*pid_strv, allocated * sizeof(char *)); |
| 115 | } |
| 116 | (*pid_strv)[read++] = strdup(pid_str); |
| 117 | } |
| 118 | |
| 119 | fclose(f); |
| 120 | |
| 121 | return read; |
| 122 | } |
| 123 | |
Conrad Hoffmann | 62c8565 | 2014-07-28 23:52:20 +0200 | [diff] [blame] | 124 | static void signal_handler(int signum) |
| 125 | { |
Willy Tarreau | a24f95d | 2016-02-27 08:26:14 +0100 | [diff] [blame] | 126 | if (caught_signal != SIGINT && caught_signal != SIGTERM) |
| 127 | caught_signal = signum; |
Conrad Hoffmann | 62c8565 | 2014-07-28 23:52:20 +0200 | [diff] [blame] | 128 | } |
| 129 | |
Willy Tarreau | 60278b9 | 2016-02-27 08:18:04 +0100 | [diff] [blame] | 130 | /* handles SIGUSR2 and SIGHUP only */ |
| 131 | static void do_restart(int sig) |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 132 | { |
Apollon Oikonomopoulos | b3fce6e | 2014-04-17 16:39:28 +0300 | [diff] [blame] | 133 | setenv(REEXEC_FLAG, "1", 1); |
Willy Tarreau | 60278b9 | 2016-02-27 08:18:04 +0100 | [diff] [blame] | 134 | fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: re-executing on %s.\n", |
| 135 | sig == SIGUSR2 ? "SIGUSR2" : "SIGHUP"); |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 136 | |
Apollon Oikonomopoulos | b3fce6e | 2014-04-17 16:39:28 +0300 | [diff] [blame] | 137 | execv(wrapper_argv[0], wrapper_argv); |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Willy Tarreau | 60278b9 | 2016-02-27 08:18:04 +0100 | [diff] [blame] | 140 | /* handles SIGTERM and SIGINT only */ |
| 141 | static void do_shutdown(int sig) |
Kristoffer Grönlund | 66fd1d8 | 2013-11-22 11:09:39 +0100 | [diff] [blame] | 142 | { |
| 143 | int i, pid; |
| 144 | char **pid_strv = NULL; |
| 145 | int nb_pid = read_pids(&pid_strv); |
| 146 | for (i = 0; i < nb_pid; ++i) { |
| 147 | pid = atoi(pid_strv[i]); |
| 148 | if (pid > 0) { |
Willy Tarreau | 60278b9 | 2016-02-27 08:18:04 +0100 | [diff] [blame] | 149 | fprintf(stderr, SD_DEBUG "haproxy-systemd-wrapper: %s -> %d.\n", |
| 150 | sig == SIGTERM ? "SIGTERM" : "SIGINT", pid); |
Willy Tarreau | 081753c | 2016-02-27 08:20:17 +0100 | [diff] [blame] | 151 | kill(pid, sig); |
Kristoffer Grönlund | 66fd1d8 | 2013-11-22 11:09:39 +0100 | [diff] [blame] | 152 | free(pid_strv[i]); |
| 153 | } |
| 154 | } |
| 155 | free(pid_strv); |
| 156 | } |
| 157 | |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 158 | static void init(int argc, char **argv) |
| 159 | { |
| 160 | while (argc > 1) { |
Conrad Hoffmann | 715e9b8 | 2014-07-28 23:22:43 +0200 | [diff] [blame] | 161 | if ((*argv)[0] == '-' && (*argv)[1] == 'p') { |
| 162 | pid_file = *(argv + 1); |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 163 | } |
| 164 | --argc; ++argv; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | int main(int argc, char **argv) |
| 169 | { |
Kristoffer Grönlund | f65194a | 2013-11-22 11:11:54 +0100 | [diff] [blame] | 170 | int status; |
| 171 | |
Apollon Oikonomopoulos | b3fce6e | 2014-04-17 16:39:28 +0300 | [diff] [blame] | 172 | wrapper_argc = argc; |
| 173 | wrapper_argv = argv; |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 174 | |
Apollon Oikonomopoulos | b3fce6e | 2014-04-17 16:39:28 +0300 | [diff] [blame] | 175 | --argc; ++argv; |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 176 | init(argc, argv); |
| 177 | |
Conrad Hoffmann | 62c8565 | 2014-07-28 23:52:20 +0200 | [diff] [blame] | 178 | struct sigaction sa; |
| 179 | memset(&sa, 0, sizeof(struct sigaction)); |
| 180 | sa.sa_handler = &signal_handler; |
| 181 | sigaction(SIGUSR2, &sa, NULL); |
Matt Robenolt | 6bb7bf7 | 2014-09-11 05:19:30 +0000 | [diff] [blame] | 182 | sigaction(SIGHUP, &sa, NULL); |
Conrad Hoffmann | 62c8565 | 2014-07-28 23:52:20 +0200 | [diff] [blame] | 183 | sigaction(SIGINT, &sa, NULL); |
Matt Robenolt | 6bb7bf7 | 2014-09-11 05:19:30 +0000 | [diff] [blame] | 184 | sigaction(SIGTERM, &sa, NULL); |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 185 | |
Apollon Oikonomopoulos | b3fce6e | 2014-04-17 16:39:28 +0300 | [diff] [blame] | 186 | if (getenv(REEXEC_FLAG) != NULL) { |
| 187 | /* We are being re-executed: restart HAProxy gracefully */ |
| 188 | int i; |
| 189 | char **pid_strv = NULL; |
| 190 | int nb_pid = read_pids(&pid_strv); |
Apollon Oikonomopoulos | b3fce6e | 2014-04-17 16:39:28 +0300 | [diff] [blame] | 191 | |
| 192 | unsetenv(REEXEC_FLAG); |
| 193 | spawn_haproxy(pid_strv, nb_pid); |
| 194 | |
Apollon Oikonomopoulos | b3fce6e | 2014-04-17 16:39:28 +0300 | [diff] [blame] | 195 | for (i = 0; i < nb_pid; ++i) |
| 196 | free(pid_strv[i]); |
| 197 | free(pid_strv); |
| 198 | } |
| 199 | else { |
| 200 | /* Start a fresh copy of HAProxy */ |
| 201 | spawn_haproxy(NULL, 0); |
| 202 | } |
| 203 | |
Kristoffer Grönlund | f65194a | 2013-11-22 11:11:54 +0100 | [diff] [blame] | 204 | status = -1; |
Willy Tarreau | 1e7893c | 2016-02-27 07:58:50 +0100 | [diff] [blame] | 205 | while (caught_signal || wait(&status) != -1 || errno == EINTR) { |
Willy Tarreau | 60278b9 | 2016-02-27 08:18:04 +0100 | [diff] [blame] | 206 | int sig = caught_signal; |
| 207 | |
Matt Robenolt | 6bb7bf7 | 2014-09-11 05:19:30 +0000 | [diff] [blame] | 208 | if (caught_signal == SIGUSR2 || caught_signal == SIGHUP) { |
Conrad Hoffmann | 62c8565 | 2014-07-28 23:52:20 +0200 | [diff] [blame] | 209 | caught_signal = 0; |
Willy Tarreau | 60278b9 | 2016-02-27 08:18:04 +0100 | [diff] [blame] | 210 | do_restart(sig); |
Conrad Hoffmann | 62c8565 | 2014-07-28 23:52:20 +0200 | [diff] [blame] | 211 | } |
Matt Robenolt | 6bb7bf7 | 2014-09-11 05:19:30 +0000 | [diff] [blame] | 212 | else if (caught_signal == SIGINT || caught_signal == SIGTERM) { |
Conrad Hoffmann | 62c8565 | 2014-07-28 23:52:20 +0200 | [diff] [blame] | 213 | caught_signal = 0; |
Willy Tarreau | 60278b9 | 2016-02-27 08:18:04 +0100 | [diff] [blame] | 214 | do_shutdown(sig); |
Conrad Hoffmann | 62c8565 | 2014-07-28 23:52:20 +0200 | [diff] [blame] | 215 | } |
| 216 | } |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 217 | |
Apollon Oikonomopoulos | 6b6f3a0 | 2014-04-17 16:39:29 +0300 | [diff] [blame] | 218 | fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: exit, haproxy RC=%d\n", |
| 219 | status); |
Apollon Oikonomopoulos | e8ea598 | 2014-04-17 16:39:30 +0300 | [diff] [blame] | 220 | return status; |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 221 | } |