-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathhaproxy-systemd-wrapper.c
More file actions
214 lines (181 loc) · 4.51 KB
/
haproxy-systemd-wrapper.c
File metadata and controls
214 lines (181 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* Wrapper to make haproxy systemd-compliant.
*
* Copyright 2013 Marc-Antoine Perennou <Marc-Antoine@Perennou.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
*/
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#define REEXEC_FLAG "HAPROXY_SYSTEMD_REEXEC"
#define SD_DEBUG "<7>"
#define SD_NOTICE "<5>"
static volatile sig_atomic_t caught_signal;
static char *pid_file = "/run/haproxy.pid";
static int wrapper_argc;
static char **wrapper_argv;
/* returns the path to the haproxy binary into <buffer>, whose size indicated
* in <buffer_size> must be at least 1 byte long.
*/
static void locate_haproxy(char *buffer, size_t buffer_size)
{
char *end = NULL;
int len;
len = readlink("/proc/self/exe", buffer, buffer_size - 1);
if (len == -1)
goto fail;
buffer[len] = 0;
end = strrchr(buffer, '/');
if (end == NULL)
goto fail;
if (strcmp(end + strlen(end) - 16, "-systemd-wrapper") == 0) {
end[strlen(end) - 16] = '\0';
return;
}
end[1] = '\0';
strncpy(end + 1, "haproxy", buffer + buffer_size - (end + 1));
buffer[buffer_size - 1] = '\0';
return;
fail:
strncpy(buffer, "/usr/sbin/haproxy", buffer_size);
buffer[buffer_size - 1] = '\0';
return;
}
static void spawn_haproxy(char **pid_strv, int nb_pid)
{
char haproxy_bin[512];
pid_t pid;
int main_argc;
char **main_argv;
main_argc = wrapper_argc - 1;
main_argv = wrapper_argv + 1;
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;
locate_haproxy(haproxy_bin, 512);
argv[argno++] = haproxy_bin;
argv[argno++] = "-Ds";
for (i = 0; i < main_argc; ++i)
argv[argno++] = main_argv[i];
if (nb_pid > 0) {
argv[argno++] = "-sf";
for (i = 0; i < nb_pid; ++i)
argv[argno++] = pid_strv[i];
}
argv[argno] = NULL;
fprintf(stderr, SD_DEBUG "haproxy-systemd-wrapper: executing ");
for (i = 0; argv[i]; ++i)
fprintf(stderr, "%s ", argv[i]);
fprintf(stderr, "\n");
execv(argv[0], argv);
exit(0);
}
}
static int read_pids(char ***pid_strv)
{
FILE *f = fopen(pid_file, "r");
int read = 0, allocated = 8;
char pid_str[10];
if (!f)
return 0;
*pid_strv = malloc(allocated * sizeof(char *));
while (1 == fscanf(f, "%s\n", pid_str)) {
if (read == allocated) {
allocated *= 2;
*pid_strv = realloc(*pid_strv, allocated * sizeof(char *));
}
(*pid_strv)[read++] = strdup(pid_str);
}
fclose(f);
return read;
}
static void signal_handler(int signum)
{
caught_signal = signum;
}
static void do_restart(void)
{
setenv(REEXEC_FLAG, "1", 1);
fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: re-executing\n");
execv(wrapper_argv[0], wrapper_argv);
}
static void do_shutdown(void)
{
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) {
fprintf(stderr, SD_DEBUG "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) {
if ((*argv)[0] == '-' && (*argv)[1] == 'p') {
pid_file = *(argv + 1);
}
--argc; ++argv;
}
}
int main(int argc, char **argv)
{
int status;
struct sigaction sa;
wrapper_argc = argc;
wrapper_argv = argv;
--argc; ++argv;
init(argc, argv);
memset(&sa, 0, sizeof(struct sigaction));
sa.sa_handler = &signal_handler;
sigaction(SIGUSR2, &sa, NULL);
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
if (getenv(REEXEC_FLAG) != NULL) {
/* We are being re-executed: restart HAProxy gracefully */
int i;
char **pid_strv = NULL;
int nb_pid = read_pids(&pid_strv);
unsetenv(REEXEC_FLAG);
spawn_haproxy(pid_strv, nb_pid);
for (i = 0; i < nb_pid; ++i)
free(pid_strv[i]);
free(pid_strv);
}
else {
/* Start a fresh copy of HAProxy */
spawn_haproxy(NULL, 0);
}
status = -1;
while (-1 != wait(&status) || errno == EINTR) {
if (caught_signal == SIGUSR2 || caught_signal == SIGHUP) {
caught_signal = 0;
do_restart();
}
else if (caught_signal == SIGINT || caught_signal == SIGTERM) {
caught_signal = 0;
do_shutdown();
}
}
fprintf(stderr, SD_NOTICE "haproxy-systemd-wrapper: exit, haproxy RC=%d\n",
status);
return status;
}