From bf1c645a89c2c6df3c35cf489b3dba53e91e69c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristoffer=20Gr=C3=B6nlund?= Date: Fri, 22 Nov 2013 10:47:27 +0100 Subject: [PATCH] MEDIUM: haproxy-systemd-wrapper: Revised implementation * Locate haproxy using location of the wrapper * Kill children when killed * Write information to stdout --- src/haproxy-systemd-wrapper.c | 57 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/src/haproxy-systemd-wrapper.c b/src/haproxy-systemd-wrapper.c index fb1a7fd92724..4ca86dd3b8c0 100644 --- a/src/haproxy-systemd-wrapper.c +++ b/src/haproxy-systemd-wrapper.c @@ -22,15 +22,30 @@ static char *pid_file = "/run/haproxy.pid"; static int main_argc; static char **main_argv; +static void locate_haproxy(char *buffer, size_t buffer_size) +{ + char* end; + readlink("/proc/self/exe", buffer, buffer_size); + end = strrchr(buffer, '/'); + if (end == NULL) + strncpy(buffer, "/usr/sbin/haproxy", buffer_size); + end[1] = '\0'; + strncat(buffer, "haproxy", buffer_size); +} + static void spawn_haproxy(char **pid_strv, int nb_pid) { - pid_t pid = fork(); + char haproxy_bin[512]; + pid_t pid; + + pid = fork(); if (!pid) { /* 3 for "haproxy -Ds -sf" */ char **argv = calloc(4 + main_argc + nb_pid + 1, sizeof(char *)); int i; int argno = 0; - argv[argno++] = SBINDIR"/haproxy"; + locate_haproxy(haproxy_bin, 512); + argv[argno++] = haproxy_bin; for (i = 0; i < main_argc; ++i) argv[argno++] = main_argv[i]; argv[argno++] = "-Ds"; @@ -40,6 +55,12 @@ static void spawn_haproxy(char **pid_strv, int nb_pid) argv[argno++] = pid_strv[i]; } argv[argno] = NULL; + + printf("%s", "haproxy-systemd-wrapper: executing "); + for (i = 0; argv[i]; ++i) + printf("%s ", argv[i]); + puts(""); + execv(argv[0], argv); exit(0); } @@ -68,7 +89,7 @@ static int read_pids(char ***pid_strv) return read; } -static void signal_handler(int signum __attribute__((unused))) +static void sigusr2_handler(int signum __attribute__((unused))) { int i; char **pid_strv = NULL; @@ -81,6 +102,22 @@ static void signal_handler(int signum __attribute__((unused))) free(pid_strv); } +static void sigint_handler(int signum __attribute__((unused))) +{ + int i, pid; + char **pid_strv = NULL; + int nb_pid = read_pids(&pid_strv); + for (i = 0; i < nb_pid; ++i) { + pid = atoi(pid_strv[i]); + if (pid > 0) { + printf("haproxy-systemd-wrapper: SIGINT -> %d\n", pid); + kill(pid, SIGINT); + free(pid_strv[i]); + } + } + free(pid_strv); +} + static void init(int argc, char **argv) { while (argc > 1) { @@ -96,16 +133,22 @@ static void init(int argc, char **argv) int main(int argc, char **argv) { + int status; + --argc; ++argv; - main_argc = argc; - main_argv = argv; + main_argc = argc; + main_argv = argv; init(argc, argv); - signal(SIGUSR2, &signal_handler); + signal(SIGINT, &sigint_handler); + signal(SIGUSR2, &sigusr2_handler); spawn_haproxy(NULL, 0); - while (-1 != wait(NULL) || errno == EINTR); + status = -1; + while (-1 != wait(&status) || errno == EINTR) + ; + printf("haproxy-systemd-wrapper: exit, haproxy RC=%d\n", status); return EXIT_SUCCESS; } -- 1.8.4