2
* Wrapper to make haproxy systemd-compliant.
4
* Copyright 2013 Marc-Antoine Perennou <Marc-Antoine@Perennou.com>
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.
21
static char *pid_file = "/run/haproxy.pid";
23
static char **main_argv;
25
static void locate_haproxy(char *buffer, size_t buffer_size)
28
if (readlink("/proc/self/exe", buffer, buffer_size) > 0)
29
end = strrchr(buffer, '/');
31
strncpy(buffer, "/usr/sbin/haproxy", buffer_size);
33
strncat(buffer, "haproxy", buffer_size);
36
static void spawn_haproxy(char **pid_strv, int nb_pid)
38
char haproxy_bin[512];
43
/* 3 for "haproxy -Ds -sf" */
44
char **argv = calloc(4 + main_argc + nb_pid + 1, sizeof(char *));
47
locate_haproxy(haproxy_bin, 512);
48
argv[argno++] = haproxy_bin;
49
for (i = 0; i < main_argc; ++i)
50
argv[argno++] = main_argv[i];
51
argv[argno++] = "-Ds";
53
argv[argno++] = "-sf";
54
for (i = 0; i < nb_pid; ++i)
55
argv[argno++] = pid_strv[i];
59
printf("%s", "haproxy-systemd-wrapper: executing ");
60
for (i = 0; argv[i]; ++i)
61
printf("%s ", argv[i]);
69
static int read_pids(char ***pid_strv)
71
FILE *f = fopen(pid_file, "r");
72
int read = 0, allocated = 8;
78
*pid_strv = malloc(allocated * sizeof(char *));
79
while (1 == fscanf(f, "%s\n", pid_str)) {
80
if (read == allocated) {
82
*pid_strv = realloc(*pid_strv, allocated * sizeof(char *));
84
(*pid_strv)[read++] = strdup(pid_str);
92
static void sigusr2_handler(int signum __attribute__((unused)))
95
char **pid_strv = NULL;
96
int nb_pid = read_pids(&pid_strv);
98
spawn_haproxy(pid_strv, nb_pid);
100
for (i = 0; i < nb_pid; ++i)
105
static void sigint_handler(int signum __attribute__((unused)))
108
char **pid_strv = NULL;
109
int nb_pid = read_pids(&pid_strv);
110
for (i = 0; i < nb_pid; ++i) {
111
pid = atoi(pid_strv[i]);
113
printf("haproxy-systemd-wrapper: SIGINT -> %d\n", pid);
121
static void init(int argc, char **argv)
125
char *flag = *argv + 1;
134
int main(int argc, char **argv)
144
signal(SIGINT, &sigint_handler);
145
signal(SIGUSR2, &sigusr2_handler);
147
spawn_haproxy(NULL, 0);
149
while (-1 != wait(&status) || errno == EINTR)
152
printf("haproxy-systemd-wrapper: exit, haproxy RC=%d\n", status);