blob: 529b2136df29286d5ce246b3229095b6914217d6 [file] [log] [blame]
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +01001/*
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 Perennoued9803e2013-02-12 10:53:53 +010015#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <unistd.h>
19#include <sys/wait.h>
20
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +030021#define REEXEC_FLAG "HAPROXY_SYSTEMD_REEXEC"
Apollon Oikonomopoulos6b6f3a02014-04-17 16:39:29 +030022#define SD_DEBUG "<7>"
23#define SD_NOTICE "<5>"
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +030024
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010025static char *pid_file = "/run/haproxy.pid";
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +030026static int wrapper_argc;
27static char **wrapper_argv;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010028
Kristoffer Grönlund1b6e75f2013-11-22 11:06:34 +010029static void locate_haproxy(char *buffer, size_t buffer_size)
30{
Willy Tarreaue5eddaf2014-04-14 15:34:34 +020031 char *end = NULL;
32
Lukas Tribus439cfde2013-12-10 08:32:56 +010033 if (readlink("/proc/self/exe", buffer, buffer_size) > 0)
34 end = strrchr(buffer, '/');
Willy Tarreaue5eddaf2014-04-14 15:34:34 +020035
36 if (end == NULL) {
Kristoffer Grönlund1b6e75f2013-11-22 11:06:34 +010037 strncpy(buffer, "/usr/sbin/haproxy", buffer_size);
Willy Tarreaue5eddaf2014-04-14 15:34:34 +020038 return;
39 }
Kristoffer Grönlund1b6e75f2013-11-22 11:06:34 +010040 end[1] = '\0';
Willy Tarreaue5eddaf2014-04-14 15:34:34 +020041 strncpy(end + 1, "haproxy", buffer + buffer_size - (end + 1));
42 buffer[buffer_size - 1] = '\0';
Kristoffer Grönlund1b6e75f2013-11-22 11:06:34 +010043}
44
Marc-Antoine Perennou47f922d2013-04-02 13:53:21 +020045static void spawn_haproxy(char **pid_strv, int nb_pid)
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010046{
Kristoffer Grönlund1b6e75f2013-11-22 11:06:34 +010047 char haproxy_bin[512];
48 pid_t pid;
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +030049 int main_argc;
50 char **main_argv;
51
52 main_argc = wrapper_argc - 1;
53 main_argv = wrapper_argv + 1;
Kristoffer Grönlund1b6e75f2013-11-22 11:06:34 +010054
55 pid = fork();
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010056 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önlund1b6e75f2013-11-22 11:06:34 +010061 locate_haproxy(haproxy_bin, 512);
62 argv[argno++] = haproxy_bin;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010063 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önlundf65194a2013-11-22 11:11:54 +010072
Apollon Oikonomopoulos6b6f3a02014-04-17 16:39:29 +030073 fprintf(stderr, SD_DEBUG "haproxy-systemd-wrapper: executing ");
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +010074 for (i = 0; argv[i]; ++i)
Apollon Oikonomopoulos6b6f3a02014-04-17 16:39:29 +030075 fprintf(stderr, "%s ", argv[i]);
76 fprintf(stderr, "\n");
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +010077
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010078 execv(argv[0], argv);
79 exit(0);
80 }
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010081}
82
83static 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önlund66fd1d82013-11-22 11:09:39 +0100106static void sigusr2_handler(int signum __attribute__((unused)))
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100107{
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300108 setenv(REEXEC_FLAG, "1", 1);
Apollon Oikonomopoulos6b6f3a02014-04-17 16:39:29 +0300109 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: re-executing\n");
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100110
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300111 execv(wrapper_argv[0], wrapper_argv);
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100112}
113
Kristoffer Grönlund66fd1d82013-11-22 11:09:39 +0100114static 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 Oikonomopoulos6b6f3a02014-04-17 16:39:29 +0300122 fprintf(stderr, SD_DEBUG "haproxy-systemd-wrapper: SIGINT -> %d\n", pid);
Kristoffer Grönlund66fd1d82013-11-22 11:09:39 +0100123 kill(pid, SIGINT);
124 free(pid_strv[i]);
125 }
126 }
127 free(pid_strv);
128}
129
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100130static void init(int argc, char **argv)
131{
132 while (argc > 1) {
Conrad Hoffmanneb2cf452014-07-28 23:22:43 +0200133 if ((*argv)[0] == '-' && (*argv)[1] == 'p') {
134 pid_file = *(argv + 1);
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100135 }
136 --argc; ++argv;
137 }
138}
139
140int main(int argc, char **argv)
141{
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +0100142 int status;
143
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300144 wrapper_argc = argc;
145 wrapper_argv = argv;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100146
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300147 --argc; ++argv;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100148 init(argc, argv);
149
Kristoffer Grönlund66fd1d82013-11-22 11:09:39 +0100150 signal(SIGINT, &sigint_handler);
151 signal(SIGUSR2, &sigusr2_handler);
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100152
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300153 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önlundf65194a2013-11-22 11:11:54 +0100178 status = -1;
179 while (-1 != wait(&status) || errno == EINTR)
180 ;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100181
Apollon Oikonomopoulos6b6f3a02014-04-17 16:39:29 +0300182 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: exit, haproxy RC=%d\n",
183 status);
Apollon Oikonomopoulose8ea5982014-04-17 16:39:30 +0300184 return status;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100185}