blob: 446f28f0bb265a8ad97243c5aa06337aaf248899 [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
Conrad Hoffmann62c85652014-07-28 23:52:20 +020025static volatile sig_atomic_t caught_signal;
26
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010027static char *pid_file = "/run/haproxy.pid";
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +030028static int wrapper_argc;
29static char **wrapper_argv;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010030
Willy Tarreauafbfc272014-09-19 15:42:30 +020031/* returns the path to the haproxy binary into <buffer>, whose size indicated
32 * in <buffer_size> must be at least 1 byte long.
33 */
Kristoffer Grönlund1b6e75f2013-11-22 11:06:34 +010034static void locate_haproxy(char *buffer, size_t buffer_size)
35{
Willy Tarreaue5eddaf2014-04-14 15:34:34 +020036 char *end = NULL;
Willy Tarreauafbfc272014-09-19 15:42:30 +020037 int len;
Willy Tarreaue5eddaf2014-04-14 15:34:34 +020038
Willy Tarreauafbfc272014-09-19 15:42:30 +020039 len = readlink("/proc/self/exe", buffer, buffer_size - 1);
40 if (len == -1)
41 goto fail;
Willy Tarreaue5eddaf2014-04-14 15:34:34 +020042
Willy Tarreauafbfc272014-09-19 15:42:30 +020043 buffer[len] = 0;
44 end = strrchr(buffer, '/');
45 if (end == NULL)
46 goto fail;
47
48 if (strcmp(end + strlen(end) - 16, "-systemd-wrapper") == 0) {
49 end[strlen(end) - 16] = '\0';
Willy Tarreaue5eddaf2014-04-14 15:34:34 +020050 return;
51 }
Willy Tarreauafbfc272014-09-19 15:42:30 +020052
Kristoffer Grönlund1b6e75f2013-11-22 11:06:34 +010053 end[1] = '\0';
Willy Tarreaue5eddaf2014-04-14 15:34:34 +020054 strncpy(end + 1, "haproxy", buffer + buffer_size - (end + 1));
55 buffer[buffer_size - 1] = '\0';
Willy Tarreauafbfc272014-09-19 15:42:30 +020056 return;
57 fail:
58 strncpy(buffer, "/usr/sbin/haproxy", buffer_size);
59 buffer[buffer_size - 1] = '\0';
60 return;
Kristoffer Grönlund1b6e75f2013-11-22 11:06:34 +010061}
62
Marc-Antoine Perennou47f922d2013-04-02 13:53:21 +020063static void spawn_haproxy(char **pid_strv, int nb_pid)
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010064{
Kristoffer Grönlund1b6e75f2013-11-22 11:06:34 +010065 char haproxy_bin[512];
66 pid_t pid;
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +030067 int main_argc;
68 char **main_argv;
69
70 main_argc = wrapper_argc - 1;
71 main_argv = wrapper_argv + 1;
Kristoffer Grönlund1b6e75f2013-11-22 11:06:34 +010072
Willy Tarreauafbfc272014-09-19 15:42:30 +020073 //pid = fork();
74 pid=0;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010075 if (!pid) {
76 /* 3 for "haproxy -Ds -sf" */
77 char **argv = calloc(4 + main_argc + nb_pid + 1, sizeof(char *));
78 int i;
79 int argno = 0;
Kristoffer Grönlund1b6e75f2013-11-22 11:06:34 +010080 locate_haproxy(haproxy_bin, 512);
81 argv[argno++] = haproxy_bin;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010082 for (i = 0; i < main_argc; ++i)
83 argv[argno++] = main_argv[i];
84 argv[argno++] = "-Ds";
85 if (nb_pid > 0) {
86 argv[argno++] = "-sf";
87 for (i = 0; i < nb_pid; ++i)
88 argv[argno++] = pid_strv[i];
89 }
90 argv[argno] = NULL;
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +010091
Apollon Oikonomopoulos6b6f3a02014-04-17 16:39:29 +030092 fprintf(stderr, SD_DEBUG "haproxy-systemd-wrapper: executing ");
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +010093 for (i = 0; argv[i]; ++i)
Apollon Oikonomopoulos6b6f3a02014-04-17 16:39:29 +030094 fprintf(stderr, "%s ", argv[i]);
95 fprintf(stderr, "\n");
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +010096
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +010097 execv(argv[0], argv);
98 exit(0);
99 }
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100100}
101
102static int read_pids(char ***pid_strv)
103{
104 FILE *f = fopen(pid_file, "r");
105 int read = 0, allocated = 8;
106 char pid_str[10];
107
108 if (!f)
109 return 0;
110
111 *pid_strv = malloc(allocated * sizeof(char *));
112 while (1 == fscanf(f, "%s\n", pid_str)) {
113 if (read == allocated) {
114 allocated *= 2;
115 *pid_strv = realloc(*pid_strv, allocated * sizeof(char *));
116 }
117 (*pid_strv)[read++] = strdup(pid_str);
118 }
119
120 fclose(f);
121
122 return read;
123}
124
Conrad Hoffmann62c85652014-07-28 23:52:20 +0200125static void signal_handler(int signum)
126{
127 caught_signal = signum;
128}
129
130static void do_restart(void)
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100131{
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300132 setenv(REEXEC_FLAG, "1", 1);
Apollon Oikonomopoulos6b6f3a02014-04-17 16:39:29 +0300133 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: re-executing\n");
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100134
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300135 execv(wrapper_argv[0], wrapper_argv);
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100136}
137
Conrad Hoffmann62c85652014-07-28 23:52:20 +0200138static void do_shutdown(void)
Kristoffer Grönlund66fd1d82013-11-22 11:09:39 +0100139{
140 int i, pid;
141 char **pid_strv = NULL;
142 int nb_pid = read_pids(&pid_strv);
143 for (i = 0; i < nb_pid; ++i) {
144 pid = atoi(pid_strv[i]);
145 if (pid > 0) {
Apollon Oikonomopoulos6b6f3a02014-04-17 16:39:29 +0300146 fprintf(stderr, SD_DEBUG "haproxy-systemd-wrapper: SIGINT -> %d\n", pid);
Kristoffer Grönlund66fd1d82013-11-22 11:09:39 +0100147 kill(pid, SIGINT);
148 free(pid_strv[i]);
149 }
150 }
151 free(pid_strv);
152}
153
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100154static void init(int argc, char **argv)
155{
156 while (argc > 1) {
Conrad Hoffmann715e9b82014-07-28 23:22:43 +0200157 if ((*argv)[0] == '-' && (*argv)[1] == 'p') {
158 pid_file = *(argv + 1);
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100159 }
160 --argc; ++argv;
161 }
162}
163
164int main(int argc, char **argv)
165{
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +0100166 int status;
167
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300168 wrapper_argc = argc;
169 wrapper_argv = argv;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100170
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300171 --argc; ++argv;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100172 init(argc, argv);
173
Conrad Hoffmann62c85652014-07-28 23:52:20 +0200174 struct sigaction sa;
175 memset(&sa, 0, sizeof(struct sigaction));
176 sa.sa_handler = &signal_handler;
177 sigaction(SIGUSR2, &sa, NULL);
Matt Robenolt6bb7bf72014-09-11 05:19:30 +0000178 sigaction(SIGHUP, &sa, NULL);
Conrad Hoffmann62c85652014-07-28 23:52:20 +0200179 sigaction(SIGINT, &sa, NULL);
Matt Robenolt6bb7bf72014-09-11 05:19:30 +0000180 sigaction(SIGTERM, &sa, NULL);
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100181
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300182 if (getenv(REEXEC_FLAG) != NULL) {
183 /* We are being re-executed: restart HAProxy gracefully */
184 int i;
185 char **pid_strv = NULL;
186 int nb_pid = read_pids(&pid_strv);
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300187
188 unsetenv(REEXEC_FLAG);
189 spawn_haproxy(pid_strv, nb_pid);
190
Apollon Oikonomopoulosb3fce6e2014-04-17 16:39:28 +0300191 for (i = 0; i < nb_pid; ++i)
192 free(pid_strv[i]);
193 free(pid_strv);
194 }
195 else {
196 /* Start a fresh copy of HAProxy */
197 spawn_haproxy(NULL, 0);
198 }
199
Kristoffer Grönlundf65194a2013-11-22 11:11:54 +0100200 status = -1;
Conrad Hoffmann62c85652014-07-28 23:52:20 +0200201 while (-1 != wait(&status) || errno == EINTR) {
Matt Robenolt6bb7bf72014-09-11 05:19:30 +0000202 if (caught_signal == SIGUSR2 || caught_signal == SIGHUP) {
Conrad Hoffmann62c85652014-07-28 23:52:20 +0200203 caught_signal = 0;
204 do_restart();
205 }
Matt Robenolt6bb7bf72014-09-11 05:19:30 +0000206 else if (caught_signal == SIGINT || caught_signal == SIGTERM) {
Conrad Hoffmann62c85652014-07-28 23:52:20 +0200207 caught_signal = 0;
208 do_shutdown();
209 }
210 }
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100211
Apollon Oikonomopoulos6b6f3a02014-04-17 16:39:29 +0300212 fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: exit, haproxy RC=%d\n",
213 status);
Apollon Oikonomopoulose8ea5982014-04-17 16:39:30 +0300214 return status;
Marc-Antoine Perennoued9803e2013-02-12 10:53:53 +0100215}