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 | |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 21 | static char *pid_file = "/run/haproxy.pid"; |
| 22 | static int main_argc; |
| 23 | static char **main_argv; |
| 24 | |
Kristoffer Grönlund | 1b6e75f | 2013-11-22 11:06:34 +0100 | [diff] [blame] | 25 | static void locate_haproxy(char *buffer, size_t buffer_size) |
| 26 | { |
| 27 | char* end; |
| 28 | readlink("/proc/self/exe", buffer, buffer_size); |
| 29 | end = strrchr(buffer, '/'); |
| 30 | if (end == NULL) |
| 31 | strncpy(buffer, "/usr/sbin/haproxy", buffer_size); |
| 32 | end[1] = '\0'; |
| 33 | strncat(buffer, "haproxy", buffer_size); |
| 34 | } |
| 35 | |
Marc-Antoine Perennou | 47f922d | 2013-04-02 13:53:21 +0200 | [diff] [blame] | 36 | static void spawn_haproxy(char **pid_strv, int nb_pid) |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 37 | { |
Kristoffer Grönlund | 1b6e75f | 2013-11-22 11:06:34 +0100 | [diff] [blame] | 38 | char haproxy_bin[512]; |
| 39 | pid_t pid; |
| 40 | |
| 41 | pid = fork(); |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 42 | if (!pid) { |
| 43 | /* 3 for "haproxy -Ds -sf" */ |
| 44 | char **argv = calloc(4 + main_argc + nb_pid + 1, sizeof(char *)); |
| 45 | int i; |
| 46 | int argno = 0; |
Kristoffer Grönlund | 1b6e75f | 2013-11-22 11:06:34 +0100 | [diff] [blame] | 47 | locate_haproxy(haproxy_bin, 512); |
| 48 | argv[argno++] = haproxy_bin; |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 49 | for (i = 0; i < main_argc; ++i) |
| 50 | argv[argno++] = main_argv[i]; |
| 51 | argv[argno++] = "-Ds"; |
| 52 | if (nb_pid > 0) { |
| 53 | argv[argno++] = "-sf"; |
| 54 | for (i = 0; i < nb_pid; ++i) |
| 55 | argv[argno++] = pid_strv[i]; |
| 56 | } |
| 57 | argv[argno] = NULL; |
| 58 | execv(argv[0], argv); |
| 59 | exit(0); |
| 60 | } |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static int read_pids(char ***pid_strv) |
| 64 | { |
| 65 | FILE *f = fopen(pid_file, "r"); |
| 66 | int read = 0, allocated = 8; |
| 67 | char pid_str[10]; |
| 68 | |
| 69 | if (!f) |
| 70 | return 0; |
| 71 | |
| 72 | *pid_strv = malloc(allocated * sizeof(char *)); |
| 73 | while (1 == fscanf(f, "%s\n", pid_str)) { |
| 74 | if (read == allocated) { |
| 75 | allocated *= 2; |
| 76 | *pid_strv = realloc(*pid_strv, allocated * sizeof(char *)); |
| 77 | } |
| 78 | (*pid_strv)[read++] = strdup(pid_str); |
| 79 | } |
| 80 | |
| 81 | fclose(f); |
| 82 | |
| 83 | return read; |
| 84 | } |
| 85 | |
Kristoffer Grönlund | 66fd1d8 | 2013-11-22 11:09:39 +0100 | [diff] [blame^] | 86 | static void sigusr2_handler(int signum __attribute__((unused))) |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 87 | { |
| 88 | int i; |
| 89 | char **pid_strv = NULL; |
| 90 | int nb_pid = read_pids(&pid_strv); |
| 91 | |
Marc-Antoine Perennou | 47f922d | 2013-04-02 13:53:21 +0200 | [diff] [blame] | 92 | spawn_haproxy(pid_strv, nb_pid); |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 93 | |
| 94 | for (i = 0; i < nb_pid; ++i) |
| 95 | free(pid_strv[i]); |
| 96 | free(pid_strv); |
| 97 | } |
| 98 | |
Kristoffer Grönlund | 66fd1d8 | 2013-11-22 11:09:39 +0100 | [diff] [blame^] | 99 | static void sigint_handler(int signum __attribute__((unused))) |
| 100 | { |
| 101 | int i, pid; |
| 102 | char **pid_strv = NULL; |
| 103 | int nb_pid = read_pids(&pid_strv); |
| 104 | for (i = 0; i < nb_pid; ++i) { |
| 105 | pid = atoi(pid_strv[i]); |
| 106 | if (pid > 0) { |
| 107 | kill(pid, SIGINT); |
| 108 | free(pid_strv[i]); |
| 109 | } |
| 110 | } |
| 111 | free(pid_strv); |
| 112 | } |
| 113 | |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 114 | static void init(int argc, char **argv) |
| 115 | { |
| 116 | while (argc > 1) { |
| 117 | if (**argv == '-') { |
| 118 | char *flag = *argv + 1; |
| 119 | --argc; ++argv; |
| 120 | if (*flag == 'p') |
| 121 | pid_file = *argv; |
| 122 | } |
| 123 | --argc; ++argv; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | int main(int argc, char **argv) |
| 128 | { |
| 129 | --argc; ++argv; |
| 130 | main_argc = argc; |
| 131 | main_argv = argv; |
| 132 | |
| 133 | init(argc, argv); |
| 134 | |
Kristoffer Grönlund | 66fd1d8 | 2013-11-22 11:09:39 +0100 | [diff] [blame^] | 135 | signal(SIGINT, &sigint_handler); |
| 136 | signal(SIGUSR2, &sigusr2_handler); |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 137 | |
Marc-Antoine Perennou | 47f922d | 2013-04-02 13:53:21 +0200 | [diff] [blame] | 138 | spawn_haproxy(NULL, 0); |
| 139 | while (-1 != wait(NULL) || errno == EINTR); |
Marc-Antoine Perennou | ed9803e | 2013-02-12 10:53:53 +0100 | [diff] [blame] | 140 | |
| 141 | return EXIT_SUCCESS; |
| 142 | } |