blob: d847ee9006f0afeb609e5a3ca63df26750dfb11c [file] [log] [blame]
William Lallemand48dfbbd2019-04-01 11:29:53 +02001/*
2 * Master Worker
3 *
4 * Copyright HAProxy Technologies 2019 - William Lallemand <wlallemand@haproxy.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
William Lallemand3fa724d2019-04-01 11:29:55 +020013#include <errno.h>
14#include <fcntl.h>
15#include <signal.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +020016#include <stdlib.h>
17#include <string.h>
William Lallemande25473c2019-04-01 11:29:56 +020018#include <sys/wait.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +020019
William Lallemand88dc7c52019-04-01 11:30:01 +020020#include <common/initcall.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +020021#include <common/mini-clist.h>
22
William Lallemand88dc7c52019-04-01 11:30:01 +020023#include <types/cli.h>
24#include <types/global.h>
25#include <types/peers.h>
26#include <types/signal.h>
27
28#include <proto/cli.h>
William Lallemand3fa724d2019-04-01 11:29:55 +020029#include <proto/fd.h>
30#include <proto/listener.h>
William Lallemande25473c2019-04-01 11:29:56 +020031#include <proto/log.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +020032#include <proto/mworker.h>
William Lallemand88dc7c52019-04-01 11:30:01 +020033#include <proto/proxy.h>
William Lallemand3cd95d22019-04-01 11:29:54 +020034#include <proto/signal.h>
William Lallemand88dc7c52019-04-01 11:30:01 +020035#include <proto/stream.h>
36#include <proto/stream_interface.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +020037
William Lallemand48dfbbd2019-04-01 11:29:53 +020038
William Lallemande25473c2019-04-01 11:29:56 +020039#if defined(USE_SYSTEMD)
40#include <systemd/sd-daemon.h>
41#endif
42
43static int exitcode = -1;
44
William Lallemande25473c2019-04-01 11:29:56 +020045/* ----- children processes handling ----- */
William Lallemand48dfbbd2019-04-01 11:29:53 +020046
William Lallemand48dfbbd2019-04-01 11:29:53 +020047/*
William Lallemande25473c2019-04-01 11:29:56 +020048 * Send signal to every known children.
49 */
50
51static void mworker_kill(int sig)
52{
William Lallemand3f128872019-04-01 11:29:59 +020053 struct mworker_proc *child;
William Lallemande25473c2019-04-01 11:29:56 +020054
William Lallemand3f128872019-04-01 11:29:59 +020055 list_for_each_entry(child, &proc_list, list) {
56 /* careful there, we must be sure that the pid > 0, we don't want to emit a kill -1 */
57 if ((child->type == 'w' || child->type == 'e') && (child->reloads == 0) && (child->pid > 0))
58 kill(child->pid, sig);
William Lallemande25473c2019-04-01 11:29:56 +020059 }
60}
61
62
63/* return 1 if a pid is a current child otherwise 0 */
William Lallemand3f128872019-04-01 11:29:59 +020064int mworker_current_child(int pid)
William Lallemande25473c2019-04-01 11:29:56 +020065{
William Lallemand3f128872019-04-01 11:29:59 +020066 struct mworker_proc *child;
William Lallemande25473c2019-04-01 11:29:56 +020067
William Lallemand3f128872019-04-01 11:29:59 +020068 list_for_each_entry(child, &proc_list, list) {
69 if ((child->type == 'w' || child->type == 'e') && (child->reloads == 0) && (child->pid == pid))
William Lallemande25473c2019-04-01 11:29:56 +020070 return 1;
71 }
72 return 0;
73}
74
William Lallemand3f128872019-04-01 11:29:59 +020075/*
76 * Return the number of new and old children (including workers and external
77 * processes)
78 */
79int mworker_child_nb()
80{
81 struct mworker_proc *child;
82 int ret = 0;
83
84 list_for_each_entry(child, &proc_list, list) {
85 if ((child->type == 'w' || child->type == 'e'))
86 ret++;
87 }
88
89 return ret;
90}
91
92
William Lallemande25473c2019-04-01 11:29:56 +020093/*
William Lallemand48dfbbd2019-04-01 11:29:53 +020094 * serialize the proc list and put it in the environment
95 */
96void mworker_proc_list_to_env()
97{
98 char *msg = NULL;
99 struct mworker_proc *child;
100
101 list_for_each_entry(child, &proc_list, list) {
102 if (child->pid > -1)
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200103 memprintf(&msg, "%s|type=%c;fd=%d;pid=%d;rpid=%d;reloads=%d;timestamp=%d;id=%s", msg ? msg : "", child->type, child->ipc_fd[0], child->pid, child->relative_pid, child->reloads, child->timestamp, child->id ? child->id : "");
William Lallemand48dfbbd2019-04-01 11:29:53 +0200104 }
105 if (msg)
106 setenv("HAPROXY_PROCESSES", msg, 1);
107}
108
109/*
110 * unserialize the proc list from the environment
111 */
112void mworker_env_to_proc_list()
113{
114 char *msg, *token = NULL, *s1;
115
116 msg = getenv("HAPROXY_PROCESSES");
117 if (!msg)
118 return;
119
120 while ((token = strtok_r(msg, "|", &s1))) {
121 struct mworker_proc *child;
122 char *subtoken = NULL;
123 char *s2;
124
125 msg = NULL;
126
127 child = calloc(1, sizeof(*child));
128
129 while ((subtoken = strtok_r(token, ";", &s2))) {
130
131 token = NULL;
132
133 if (strncmp(subtoken, "type=", 5) == 0) {
134 child->type = *(subtoken+5);
135 if (child->type == 'm') /* we are in the master, assign it */
136 proc_self = child;
137 } else if (strncmp(subtoken, "fd=", 3) == 0) {
138 child->ipc_fd[0] = atoi(subtoken+3);
139 } else if (strncmp(subtoken, "pid=", 4) == 0) {
140 child->pid = atoi(subtoken+4);
141 } else if (strncmp(subtoken, "rpid=", 5) == 0) {
142 child->relative_pid = atoi(subtoken+5);
143 } else if (strncmp(subtoken, "reloads=", 8) == 0) {
144 /* we reloaded this process once more */
145 child->reloads = atoi(subtoken+8) + 1;
146 } else if (strncmp(subtoken, "timestamp=", 10) == 0) {
147 child->timestamp = atoi(subtoken+10);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200148 } else if (strncmp(subtoken, "id=", 3) == 0) {
149 child->id = strdup(subtoken+3);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200150 }
151 }
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200152 if (child->pid) {
William Lallemand48dfbbd2019-04-01 11:29:53 +0200153 LIST_ADDQ(&proc_list, &child->list);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200154 } else {
155 free(child->id);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200156 free(child);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200157
158 }
William Lallemand48dfbbd2019-04-01 11:29:53 +0200159 }
160
161 unsetenv("HAPROXY_PROCESSES");
162}
William Lallemand3cd95d22019-04-01 11:29:54 +0200163
164/* Signal blocking and unblocking */
165
166void mworker_block_signals()
167{
168 sigset_t set;
169
170 sigemptyset(&set);
171 sigaddset(&set, SIGUSR1);
172 sigaddset(&set, SIGUSR2);
173 sigaddset(&set, SIGHUP);
174 sigaddset(&set, SIGCHLD);
175 ha_sigmask(SIG_SETMASK, &set, NULL);
176}
177
178void mworker_unblock_signals()
179{
180 haproxy_unblock_signals();
181}
William Lallemand3fa724d2019-04-01 11:29:55 +0200182
William Lallemande25473c2019-04-01 11:29:56 +0200183/* ----- mworker signal handlers ----- */
184
185/*
186 * When called, this function reexec haproxy with -sf followed by current
187 * children PIDs and possibly old children PIDs if they didn't leave yet.
188 */
189void mworker_catch_sighup(struct sig_handler *sh)
190{
191 mworker_reload();
192}
193
194void mworker_catch_sigterm(struct sig_handler *sh)
195{
196 int sig = sh->arg;
197
198#if defined(USE_SYSTEMD)
199 if (global.tune.options & GTUNE_USE_SYSTEMD) {
200 sd_notify(0, "STOPPING=1");
201 }
202#endif
203 ha_warning("Exiting Master process...\n");
204 mworker_kill(sig);
205}
206
207/*
208 * Wait for every children to exit
209 */
210
211void mworker_catch_sigchld(struct sig_handler *sh)
212{
213 int exitpid = -1;
214 int status = 0;
215 struct mworker_proc *child, *it;
216 int childfound;
217
218restart_wait:
219
220 childfound = 0;
221
222 exitpid = waitpid(-1, &status, WNOHANG);
223 if (exitpid > 0) {
224 if (WIFEXITED(status))
225 status = WEXITSTATUS(status);
226 else if (WIFSIGNALED(status))
227 status = 128 + WTERMSIG(status);
228 else if (WIFSTOPPED(status))
229 status = 128 + WSTOPSIG(status);
230 else
231 status = 255;
232
William Lallemand3f128872019-04-01 11:29:59 +0200233 /* delete the child from the process list */
William Lallemande25473c2019-04-01 11:29:56 +0200234 list_for_each_entry_safe(child, it, &proc_list, list) {
235 if (child->pid != exitpid)
236 continue;
237
238 LIST_DEL(&child->list);
239 close(child->ipc_fd[0]);
240 childfound = 1;
241 break;
242 }
243
William Lallemand3f128872019-04-01 11:29:59 +0200244 if (!childfound) {
245 /* We didn't find the PID in the list, that shouldn't happen but we can emit a warning */
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200246 ha_warning("Process %d exited with code %d (%s)\n", exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
William Lallemande25473c2019-04-01 11:29:56 +0200247 } else {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200248 /* check if exited child is a current child */
William Lallemand3f128872019-04-01 11:29:59 +0200249 if (child->reloads == 0) {
250 if (child->type == 'w')
251 ha_alert("Current worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200252 else if (child->type == 'e')
253 ha_alert("Current program '%s' (%d) exited with code %d (%s)\n", child->id, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
William Lallemand3f128872019-04-01 11:29:59 +0200254
William Lallemande25473c2019-04-01 11:29:56 +0200255 if (status != 0 && status != 130 && status != 143
256 && !(global.tune.options & GTUNE_NOEXIT_ONFAILURE)) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200257 ha_alert("exit-on-failure: killing every processes with SIGTERM\n");
William Lallemande25473c2019-04-01 11:29:56 +0200258 if (exitcode < 0)
259 exitcode = status;
260 mworker_kill(SIGTERM);
261 }
262 } else {
William Lallemand3f128872019-04-01 11:29:59 +0200263 if (child->type == 'w') {
264 ha_warning("Former worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
265 delete_oldpid(exitpid);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200266 } else if (child->type == 'e') {
267 ha_warning("Former program '%s' (%d) exited with code %d (%s)\n", child->id, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
William Lallemand3f128872019-04-01 11:29:59 +0200268 }
William Lallemande25473c2019-04-01 11:29:56 +0200269 }
270 free(child);
271 }
272
273 /* do it again to check if it was the last worker */
274 goto restart_wait;
275 }
276 /* Better rely on the system than on a list of process to check if it was the last one */
277 else if (exitpid == -1 && errno == ECHILD) {
278 ha_warning("All workers exited. Exiting... (%d)\n", (exitcode > 0) ? exitcode : status);
279 atexit_flag = 0;
280 if (exitcode > 0)
281 exit(exitcode);
282 exit(status); /* parent must leave using the latest status code known */
283 }
284
285}
286
William Lallemand3fa724d2019-04-01 11:29:55 +0200287/* ----- IPC FD (sockpair) related ----- */
288
289/* This wrapper is called from the workers. It is registered instead of the
290 * normal listener_accept() so the worker can exit() when it detects that the
291 * master closed the IPC FD. If it's not a close, we just call the regular
292 * listener_accept() function */
293void mworker_accept_wrapper(int fd)
294{
295 char c;
296 int ret;
297
298 while (1) {
299 ret = recv(fd, &c, 1, MSG_PEEK);
300 if (ret == -1) {
301 if (errno == EINTR)
302 continue;
303 if (errno == EAGAIN) {
304 fd_cant_recv(fd);
305 return;
306 }
307 break;
308 } else if (ret > 0) {
309 listener_accept(fd);
310 return;
311 } else if (ret == 0) {
312 /* At this step the master is down before
313 * this worker perform a 'normal' exit.
314 * So we want to exit with an error but
315 * other threads could currently process
316 * some stuff so we can't perform a clean
317 * deinit().
318 */
319 exit(EXIT_FAILURE);
320 }
321 }
322 return;
323}
324
325/*
326 * This function register the accept wrapper for the sockpair of the master worker
327 */
328void mworker_pipe_register()
329{
330 /* The iocb should be already initialized with listener_accept */
331 if (fdtab[proc_self->ipc_fd[1]].iocb == mworker_accept_wrapper)
332 return;
333
334 fcntl(proc_self->ipc_fd[1], F_SETFL, O_NONBLOCK);
335 /* In multi-tread, we need only one thread to process
336 * events on the pipe with master
337 */
338 fd_insert(proc_self->ipc_fd[1], fdtab[proc_self->ipc_fd[1]].owner, mworker_accept_wrapper, 1);
339 fd_want_recv(proc_self->ipc_fd[1]);
340}
William Lallemand9001ce82019-04-01 11:29:57 +0200341
342/* ----- proxies ----- */
343/*
344 * Upon a reload, the master worker needs to close all listeners FDs but the mworker_pipe
345 * fd, and the FD provided by fd@
346 */
347void mworker_cleanlisteners()
348{
349 struct listener *l, *l_next;
350 struct proxy *curproxy;
351 struct peers *curpeers;
352
353 /* we might have to unbind some peers sections from some processes */
354 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
355 if (!curpeers->peers_fe)
356 continue;
357
358 stop_proxy(curpeers->peers_fe);
359 /* disable this peer section so that it kills itself */
360 signal_unregister_handler(curpeers->sighandler);
361 task_delete(curpeers->sync_task);
362 task_free(curpeers->sync_task);
363 curpeers->sync_task = NULL;
364 task_free(curpeers->peers_fe->task);
365 curpeers->peers_fe->task = NULL;
366 curpeers->peers_fe = NULL;
367 }
368
369 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
370 int listen_in_master = 0;
371
372 list_for_each_entry_safe(l, l_next, &curproxy->conf.listeners, by_fe) {
373 /* remove the listener, but not those we need in the master... */
374 if (!(l->options & LI_O_MWORKER)) {
375 /* unbind the listener but does not close if
376 the FD is inherited with fd@ from the parent
377 process */
378 if (l->options & LI_O_INHERITED)
379 unbind_listener_no_close(l);
380 else
381 unbind_listener(l);
382 delete_listener(l);
383 } else {
384 listen_in_master = 1;
385 }
386 }
387 /* if the proxy shouldn't be in the master, we stop it */
388 if (!listen_in_master)
389 curproxy->state = PR_STSTOPPED;
390 }
William Lallemand88dc7c52019-04-01 11:30:01 +0200391}
392
393/* Displays workers and processes */
394static int cli_io_handler_show_proc(struct appctx *appctx)
395{
396 struct stream_interface *si = appctx->owner;
397 struct mworker_proc *child;
398 int old = 0;
399 int up = now.tv_sec - proc_self->timestamp;
400
401 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
402 return 1;
403
404 chunk_reset(&trash);
405
406 chunk_printf(&trash, "#%-14s %-15s %-15s %-15s %s\n", "<PID>", "<type>", "<relative PID>", "<reloads>", "<uptime>");
407 chunk_appendf(&trash, "%-15u %-15s %-15u %-15d %dd %02dh%02dm%02ds\n", getpid(), "master", 0, proc_self->reloads, up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
408
409 /* displays current processes */
410
411 chunk_appendf(&trash, "# workers\n");
412 list_for_each_entry(child, &proc_list, list) {
413 up = now.tv_sec - child->timestamp;
414
415 if (child->type != 'w')
416 continue;
417
418 if (child->reloads > 0) {
419 old++;
420 continue;
421 }
422 chunk_appendf(&trash, "%-15u %-15s %-15u %-15d %dd %02dh%02dm%02ds\n", child->pid, "worker", child->relative_pid, child->reloads, up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
423 }
424
425 /* displays old processes */
426
427 if (old) {
428 char *msg = NULL;
429
430 chunk_appendf(&trash, "# old workers\n");
431 list_for_each_entry(child, &proc_list, list) {
432 up = now.tv_sec - child->timestamp;
433
434 if (child->type != 'w')
435 continue;
436
437 if (child->reloads > 0) {
438 memprintf(&msg, "[was: %u]", child->relative_pid);
439 chunk_appendf(&trash, "%-15u %-15s %-15s %-15d %dd %02dh%02dm%02ds\n", child->pid, "worker", msg, child->reloads, up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
440 }
441 }
442 free(msg);
443 }
444
445 if (ci_putchk(si_ic(si), &trash) == -1) {
446 si_rx_room_blk(si);
447 return 0;
448 }
449
450 /* dump complete */
451 return 1;
William Lallemand9001ce82019-04-01 11:29:57 +0200452}
William Lallemand88dc7c52019-04-01 11:30:01 +0200453
454/* reload the master process */
455static int cli_parse_reload(char **args, char *payload, struct appctx *appctx, void *private)
456{
457 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
458 return 1;
459
460 mworker_reload();
461
462 return 1;
463}
464
465
466/* register cli keywords */
467static struct cli_kw_list cli_kws = {{ },{
468 { { "@<relative pid>", NULL }, "@<relative pid> : send a command to the <relative pid> process", NULL, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
469 { { "@!<pid>", NULL }, "@!<pid> : send a command to the <pid> process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
470 { { "@master", NULL }, "@master : send a command to the master process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
471 { { "show", "proc", NULL }, "show proc : show processes status", cli_parse_default, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
472 { { "reload", NULL }, "reload : reload haproxy", cli_parse_reload, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
473 {{},}
474}};
475
476INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);