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