blob: 214dc79ebd24185a3d9e83ea831dbe2e0c78e124 [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) {
William Lallemand45286112019-04-12 16:09:21 +020069 if ((child->type == 'w' || child->type == 'e') && (!(child->options & PROC_O_LEAVING)) && (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 Lallemand45286112019-04-12 16:09:21 +0200159 /* this is a process inherited from a reload that should be leaving */
160 child->options |= PROC_O_LEAVING;
William Lallemand48dfbbd2019-04-01 11:29:53 +0200161 }
162
163 unsetenv("HAPROXY_PROCESSES");
164}
William Lallemand3cd95d22019-04-01 11:29:54 +0200165
166/* Signal blocking and unblocking */
167
168void mworker_block_signals()
169{
170 sigset_t set;
171
172 sigemptyset(&set);
173 sigaddset(&set, SIGUSR1);
174 sigaddset(&set, SIGUSR2);
175 sigaddset(&set, SIGHUP);
176 sigaddset(&set, SIGCHLD);
177 ha_sigmask(SIG_SETMASK, &set, NULL);
178}
179
180void mworker_unblock_signals()
181{
182 haproxy_unblock_signals();
183}
William Lallemand3fa724d2019-04-01 11:29:55 +0200184
William Lallemande25473c2019-04-01 11:29:56 +0200185/* ----- mworker signal handlers ----- */
186
187/*
188 * When called, this function reexec haproxy with -sf followed by current
189 * children PIDs and possibly old children PIDs if they didn't leave yet.
190 */
191void mworker_catch_sighup(struct sig_handler *sh)
192{
193 mworker_reload();
194}
195
196void mworker_catch_sigterm(struct sig_handler *sh)
197{
198 int sig = sh->arg;
199
200#if defined(USE_SYSTEMD)
201 if (global.tune.options & GTUNE_USE_SYSTEMD) {
202 sd_notify(0, "STOPPING=1");
203 }
204#endif
205 ha_warning("Exiting Master process...\n");
206 mworker_kill(sig);
207}
208
209/*
210 * Wait for every children to exit
211 */
212
213void mworker_catch_sigchld(struct sig_handler *sh)
214{
215 int exitpid = -1;
216 int status = 0;
217 struct mworker_proc *child, *it;
218 int childfound;
219
220restart_wait:
221
222 childfound = 0;
223
224 exitpid = waitpid(-1, &status, WNOHANG);
225 if (exitpid > 0) {
226 if (WIFEXITED(status))
227 status = WEXITSTATUS(status);
228 else if (WIFSIGNALED(status))
229 status = 128 + WTERMSIG(status);
230 else if (WIFSTOPPED(status))
231 status = 128 + WSTOPSIG(status);
232 else
233 status = 255;
234
William Lallemand3f128872019-04-01 11:29:59 +0200235 /* delete the child from the process list */
William Lallemande25473c2019-04-01 11:29:56 +0200236 list_for_each_entry_safe(child, it, &proc_list, list) {
237 if (child->pid != exitpid)
238 continue;
239
240 LIST_DEL(&child->list);
241 close(child->ipc_fd[0]);
242 childfound = 1;
243 break;
244 }
245
William Lallemand3f128872019-04-01 11:29:59 +0200246 if (!childfound) {
247 /* 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 +0200248 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 +0200249 } else {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200250 /* check if exited child is a current child */
William Lallemand45286112019-04-12 16:09:21 +0200251 if (!(child->options & PROC_O_LEAVING)) {
William Lallemand3f128872019-04-01 11:29:59 +0200252 if (child->type == 'w')
253 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 +0200254 else if (child->type == 'e')
255 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 +0200256
William Lallemande25473c2019-04-01 11:29:56 +0200257 if (status != 0 && status != 130 && status != 143
258 && !(global.tune.options & GTUNE_NOEXIT_ONFAILURE)) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200259 ha_alert("exit-on-failure: killing every processes with SIGTERM\n");
William Lallemande25473c2019-04-01 11:29:56 +0200260 if (exitcode < 0)
261 exitcode = status;
262 mworker_kill(SIGTERM);
263 }
264 } else {
William Lallemand3f128872019-04-01 11:29:59 +0200265 if (child->type == 'w') {
266 ha_warning("Former worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
267 delete_oldpid(exitpid);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200268 } else if (child->type == 'e') {
269 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 +0200270 }
William Lallemande25473c2019-04-01 11:29:56 +0200271 }
272 free(child);
273 }
274
275 /* do it again to check if it was the last worker */
276 goto restart_wait;
277 }
278 /* Better rely on the system than on a list of process to check if it was the last one */
279 else if (exitpid == -1 && errno == ECHILD) {
280 ha_warning("All workers exited. Exiting... (%d)\n", (exitcode > 0) ? exitcode : status);
281 atexit_flag = 0;
282 if (exitcode > 0)
283 exit(exitcode);
284 exit(status); /* parent must leave using the latest status code known */
285 }
286
287}
288
William Lallemand3fa724d2019-04-01 11:29:55 +0200289/* ----- IPC FD (sockpair) related ----- */
290
291/* This wrapper is called from the workers. It is registered instead of the
292 * normal listener_accept() so the worker can exit() when it detects that the
293 * master closed the IPC FD. If it's not a close, we just call the regular
294 * listener_accept() function */
295void mworker_accept_wrapper(int fd)
296{
297 char c;
298 int ret;
299
300 while (1) {
301 ret = recv(fd, &c, 1, MSG_PEEK);
302 if (ret == -1) {
303 if (errno == EINTR)
304 continue;
305 if (errno == EAGAIN) {
306 fd_cant_recv(fd);
307 return;
308 }
309 break;
310 } else if (ret > 0) {
311 listener_accept(fd);
312 return;
313 } else if (ret == 0) {
314 /* At this step the master is down before
315 * this worker perform a 'normal' exit.
316 * So we want to exit with an error but
317 * other threads could currently process
318 * some stuff so we can't perform a clean
319 * deinit().
320 */
321 exit(EXIT_FAILURE);
322 }
323 }
324 return;
325}
326
327/*
328 * This function register the accept wrapper for the sockpair of the master worker
329 */
330void mworker_pipe_register()
331{
332 /* The iocb should be already initialized with listener_accept */
333 if (fdtab[proc_self->ipc_fd[1]].iocb == mworker_accept_wrapper)
334 return;
335
336 fcntl(proc_self->ipc_fd[1], F_SETFL, O_NONBLOCK);
337 /* In multi-tread, we need only one thread to process
338 * events on the pipe with master
339 */
340 fd_insert(proc_self->ipc_fd[1], fdtab[proc_self->ipc_fd[1]].owner, mworker_accept_wrapper, 1);
341 fd_want_recv(proc_self->ipc_fd[1]);
342}
William Lallemand9001ce82019-04-01 11:29:57 +0200343
344/* ----- proxies ----- */
345/*
346 * Upon a reload, the master worker needs to close all listeners FDs but the mworker_pipe
347 * fd, and the FD provided by fd@
348 */
349void mworker_cleanlisteners()
350{
351 struct listener *l, *l_next;
352 struct proxy *curproxy;
353 struct peers *curpeers;
354
355 /* we might have to unbind some peers sections from some processes */
356 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
357 if (!curpeers->peers_fe)
358 continue;
359
360 stop_proxy(curpeers->peers_fe);
361 /* disable this peer section so that it kills itself */
362 signal_unregister_handler(curpeers->sighandler);
363 task_delete(curpeers->sync_task);
364 task_free(curpeers->sync_task);
365 curpeers->sync_task = NULL;
366 task_free(curpeers->peers_fe->task);
367 curpeers->peers_fe->task = NULL;
368 curpeers->peers_fe = NULL;
369 }
370
371 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
372 int listen_in_master = 0;
373
374 list_for_each_entry_safe(l, l_next, &curproxy->conf.listeners, by_fe) {
375 /* remove the listener, but not those we need in the master... */
376 if (!(l->options & LI_O_MWORKER)) {
377 /* unbind the listener but does not close if
378 the FD is inherited with fd@ from the parent
379 process */
380 if (l->options & LI_O_INHERITED)
381 unbind_listener_no_close(l);
382 else
383 unbind_listener(l);
384 delete_listener(l);
385 } else {
386 listen_in_master = 1;
387 }
388 }
389 /* if the proxy shouldn't be in the master, we stop it */
390 if (!listen_in_master)
391 curproxy->state = PR_STSTOPPED;
392 }
William Lallemand88dc7c52019-04-01 11:30:01 +0200393}
394
395/* Displays workers and processes */
396static int cli_io_handler_show_proc(struct appctx *appctx)
397{
398 struct stream_interface *si = appctx->owner;
399 struct mworker_proc *child;
400 int old = 0;
401 int up = now.tv_sec - proc_self->timestamp;
402
403 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
404 return 1;
405
406 chunk_reset(&trash);
407
408 chunk_printf(&trash, "#%-14s %-15s %-15s %-15s %s\n", "<PID>", "<type>", "<relative PID>", "<reloads>", "<uptime>");
409 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));
410
411 /* displays current processes */
412
413 chunk_appendf(&trash, "# workers\n");
414 list_for_each_entry(child, &proc_list, list) {
415 up = now.tv_sec - child->timestamp;
416
417 if (child->type != 'w')
418 continue;
419
William Lallemand45286112019-04-12 16:09:21 +0200420 if (child->options & PROC_O_LEAVING) {
William Lallemand88dc7c52019-04-01 11:30:01 +0200421 old++;
422 continue;
423 }
424 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));
425 }
426
427 /* displays old processes */
428
429 if (old) {
430 char *msg = NULL;
431
432 chunk_appendf(&trash, "# old workers\n");
433 list_for_each_entry(child, &proc_list, list) {
434 up = now.tv_sec - child->timestamp;
435
436 if (child->type != 'w')
437 continue;
438
William Lallemand45286112019-04-12 16:09:21 +0200439 if (child->options & PROC_O_LEAVING) {
William Lallemand88dc7c52019-04-01 11:30:01 +0200440 memprintf(&msg, "[was: %u]", child->relative_pid);
441 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));
442 }
443 }
444 free(msg);
445 }
446
William Lallemandad53d6d2019-04-01 11:30:03 +0200447 /* displays external process */
448 chunk_appendf(&trash, "# programs\n");
449 old = 0;
450 list_for_each_entry(child, &proc_list, list) {
451 up = now.tv_sec - child->timestamp;
452
453 if (child->type != 'e')
454 continue;
455
William Lallemand45286112019-04-12 16:09:21 +0200456 if (child->options & PROC_O_LEAVING) {
William Lallemandad53d6d2019-04-01 11:30:03 +0200457 old++;
458 continue;
459 }
460 chunk_appendf(&trash, "%-15u %-15s %-15s %-15d %dd %02dh%02dm%02ds\n", child->pid, child->id, "-", child->reloads, up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
461 }
462
463 if (old) {
464 chunk_appendf(&trash, "# old programs\n");
465 list_for_each_entry(child, &proc_list, list) {
466 up = now.tv_sec - child->timestamp;
467
468 if (child->type != 'e')
469 continue;
470
William Lallemand45286112019-04-12 16:09:21 +0200471 if (child->options & PROC_O_LEAVING) {
William Lallemandad53d6d2019-04-01 11:30:03 +0200472 chunk_appendf(&trash, "%-15u %-15s %-15s %-15d %dd %02dh%02dm%02ds\n", child->pid, child->id, "-", child->reloads, up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
473 }
474 }
475 }
476
477
478
William Lallemand88dc7c52019-04-01 11:30:01 +0200479 if (ci_putchk(si_ic(si), &trash) == -1) {
480 si_rx_room_blk(si);
481 return 0;
482 }
483
484 /* dump complete */
485 return 1;
William Lallemand9001ce82019-04-01 11:29:57 +0200486}
William Lallemand88dc7c52019-04-01 11:30:01 +0200487
488/* reload the master process */
489static int cli_parse_reload(char **args, char *payload, struct appctx *appctx, void *private)
490{
491 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
492 return 1;
493
494 mworker_reload();
495
496 return 1;
497}
498
499
500/* register cli keywords */
501static struct cli_kw_list cli_kws = {{ },{
502 { { "@<relative pid>", NULL }, "@<relative pid> : send a command to the <relative pid> process", NULL, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
503 { { "@!<pid>", NULL }, "@!<pid> : send a command to the <pid> process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
504 { { "@master", NULL }, "@master : send a command to the master process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
505 { { "show", "proc", NULL }, "show proc : show processes status", cli_parse_default, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
506 { { "reload", NULL }, "reload : reload haproxy", cli_parse_reload, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
507 {{},}
508}};
509
510INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);