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