blob: ba07ebe01ccc75a723edab575492c8fe149586ca [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) {
133 if (**argv == '-') {
134 char *flag = *argv + 1;
135 --argc; ++argv;
136 if (*flag == 'p')
137 pid_file = *argv;
138 }
139 --argc; ++argv;
140 }
141}
142
143int main(int argc, char **argv)
144{
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +0100145 int status;
146
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300147 wrapper_argc = argc;
148 wrapper_argv = argv;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100149
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300150 --argc; ++argv;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100151 init(argc, argv);
152
Kristoffer Grönlund66fd1d82013-11-22 11:09:39 +0100153 signal(SIGINT, &sigint_handler);
154 signal(SIGUSR2, &sigusr2_handler);
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100155
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300156 if (getenv(REEXEC_FLAG) != NULL) {
157 /* We are being re-executed: restart HAProxy gracefully */
158 int i;
159 char **pid_strv = NULL;
160 int nb_pid = read_pids(&pid_strv);
161 sigset_t sigs;
162
163 unsetenv(REEXEC_FLAG);
164 spawn_haproxy(pid_strv, nb_pid);
165
166 /* Unblock SIGUSR2 which was blocked by the signal handler
167 * before re-exec */
168 sigprocmask(SIG_BLOCK, NULL, &sigs);
169 sigdelset(&sigs, SIGUSR2);
170 sigprocmask(SIG_SETMASK, &sigs, NULL);
171
172 for (i = 0; i < nb_pid; ++i)
173 free(pid_strv[i]);
174 free(pid_strv);
175 }
176 else {
177 /* Start a fresh copy of HAProxy */
178 spawn_haproxy(NULL, 0);
179 }
180
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +0100181 status = -1;
182 while (-1 != wait(&status) || errno == EINTR)
183 ;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100184
Apollon Oikonomopoulos6b6f3a02014-04-17 16:39:29 +0300185 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: exit, haproxy RC=%d\n",
186 status);
Apollon Oikonomopoulose8ea5982014-04-17 16:39:30 +0300187 return status;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100188}