blob: 4b2a7afafd70ab8d7918a553c2e3ffcf36a2f3f4 [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
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020020#include <haproxy/api.h>
William Lallemand27edc4b2019-05-07 17:49:33 +020021#include <common/cfgparse.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020022#include <haproxy/list.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020023#include <haproxy/version.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +020024
William Lallemand88dc7c52019-04-01 11:30:01 +020025#include <types/cli.h>
26#include <types/global.h>
27#include <types/peers.h>
28#include <types/signal.h>
29
30#include <proto/cli.h>
William Lallemand3fa724d2019-04-01 11:29:55 +020031#include <proto/fd.h>
32#include <proto/listener.h>
William Lallemande25473c2019-04-01 11:29:56 +020033#include <proto/log.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +020034#include <proto/mworker.h>
William Lallemand88dc7c52019-04-01 11:30:01 +020035#include <proto/proxy.h>
William Lallemand3cd95d22019-04-01 11:29:54 +020036#include <proto/signal.h>
William Lallemand88dc7c52019-04-01 11:30:01 +020037#include <proto/stream.h>
38#include <proto/stream_interface.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +020039
William Lallemand48dfbbd2019-04-01 11:29:53 +020040
William Lallemande25473c2019-04-01 11:29:56 +020041#if defined(USE_SYSTEMD)
42#include <systemd/sd-daemon.h>
43#endif
44
45static int exitcode = -1;
William Lallemand27edc4b2019-05-07 17:49:33 +020046static int max_reloads = -1; /* number max of reloads a worker can have until they are killed */
William Lallemande25473c2019-04-01 11:29:56 +020047
William Lallemande25473c2019-04-01 11:29:56 +020048/* ----- children processes handling ----- */
William Lallemand48dfbbd2019-04-01 11:29:53 +020049
William Lallemand48dfbbd2019-04-01 11:29:53 +020050/*
William Lallemande25473c2019-04-01 11:29:56 +020051 * Send signal to every known children.
52 */
53
54static void mworker_kill(int sig)
55{
William Lallemand3f128872019-04-01 11:29:59 +020056 struct mworker_proc *child;
William Lallemande25473c2019-04-01 11:29:56 +020057
William Lallemand3f128872019-04-01 11:29:59 +020058 list_for_each_entry(child, &proc_list, list) {
59 /* careful there, we must be sure that the pid > 0, we don't want to emit a kill -1 */
William Lallemand32b69012019-04-16 17:42:42 +020060 if ((child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG)) && (child->pid > 0))
William Lallemand3f128872019-04-01 11:29:59 +020061 kill(child->pid, sig);
William Lallemande25473c2019-04-01 11:29:56 +020062 }
63}
64
William Lallemand27edc4b2019-05-07 17:49:33 +020065void mworker_kill_max_reloads(int sig)
66{
67 struct mworker_proc *child;
68
69 list_for_each_entry(child, &proc_list, list) {
70 if (max_reloads != -1 && (child->options & PROC_O_TYPE_WORKER) &&
71 (child->pid > 0) && (child->reloads > max_reloads))
72 kill(child->pid, sig);
73 }
74}
William Lallemande25473c2019-04-01 11:29:56 +020075
76/* return 1 if a pid is a current child otherwise 0 */
William Lallemand3f128872019-04-01 11:29:59 +020077int mworker_current_child(int pid)
William Lallemande25473c2019-04-01 11:29:56 +020078{
William Lallemand3f128872019-04-01 11:29:59 +020079 struct mworker_proc *child;
William Lallemande25473c2019-04-01 11:29:56 +020080
William Lallemand3f128872019-04-01 11:29:59 +020081 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +020082 if ((child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG)) && (!(child->options & PROC_O_LEAVING)) && (child->pid == pid))
William Lallemande25473c2019-04-01 11:29:56 +020083 return 1;
84 }
85 return 0;
86}
87
William Lallemand3f128872019-04-01 11:29:59 +020088/*
89 * Return the number of new and old children (including workers and external
90 * processes)
91 */
92int mworker_child_nb()
93{
94 struct mworker_proc *child;
95 int ret = 0;
96
97 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +020098 if (child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG))
William Lallemand3f128872019-04-01 11:29:59 +020099 ret++;
100 }
101
102 return ret;
103}
104
105
William Lallemande25473c2019-04-01 11:29:56 +0200106/*
William Lallemand48dfbbd2019-04-01 11:29:53 +0200107 * serialize the proc list and put it in the environment
108 */
109void mworker_proc_list_to_env()
110{
111 char *msg = NULL;
112 struct mworker_proc *child;
113
114 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200115 char type = '?';
116
117 if (child->options & PROC_O_TYPE_MASTER)
118 type = 'm';
119 else if (child->options & PROC_O_TYPE_PROG)
120 type = 'e';
121 else if (child->options &= PROC_O_TYPE_WORKER)
122 type = 'w';
123
William Lallemand48dfbbd2019-04-01 11:29:53 +0200124 if (child->pid > -1)
William Lallemand1dc69632019-06-12 19:11:33 +0200125 memprintf(&msg, "%s|type=%c;fd=%d;pid=%d;rpid=%d;reloads=%d;timestamp=%d;id=%s;version=%s", msg ? msg : "", type, child->ipc_fd[0], child->pid, child->relative_pid, child->reloads, child->timestamp, child->id ? child->id : "", child->version);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200126 }
127 if (msg)
128 setenv("HAPROXY_PROCESSES", msg, 1);
129}
130
131/*
132 * unserialize the proc list from the environment
133 */
134void mworker_env_to_proc_list()
135{
136 char *msg, *token = NULL, *s1;
137
138 msg = getenv("HAPROXY_PROCESSES");
139 if (!msg)
140 return;
141
142 while ((token = strtok_r(msg, "|", &s1))) {
143 struct mworker_proc *child;
144 char *subtoken = NULL;
145 char *s2;
146
147 msg = NULL;
148
149 child = calloc(1, sizeof(*child));
150
151 while ((subtoken = strtok_r(token, ";", &s2))) {
152
153 token = NULL;
154
155 if (strncmp(subtoken, "type=", 5) == 0) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200156 char type;
157
158 type = *(subtoken+5);
159 if (type == 'm') { /* we are in the master, assign it */
William Lallemand48dfbbd2019-04-01 11:29:53 +0200160 proc_self = child;
William Lallemand8f7069a2019-04-12 16:09:23 +0200161 child->options |= PROC_O_TYPE_MASTER;
162 } else if (type == 'e') {
163 child->options |= PROC_O_TYPE_PROG;
164 } else if (type == 'w') {
165 child->options |= PROC_O_TYPE_WORKER;
166 }
167
William Lallemand48dfbbd2019-04-01 11:29:53 +0200168 } else if (strncmp(subtoken, "fd=", 3) == 0) {
169 child->ipc_fd[0] = atoi(subtoken+3);
170 } else if (strncmp(subtoken, "pid=", 4) == 0) {
171 child->pid = atoi(subtoken+4);
172 } else if (strncmp(subtoken, "rpid=", 5) == 0) {
173 child->relative_pid = atoi(subtoken+5);
174 } else if (strncmp(subtoken, "reloads=", 8) == 0) {
175 /* we reloaded this process once more */
176 child->reloads = atoi(subtoken+8) + 1;
177 } else if (strncmp(subtoken, "timestamp=", 10) == 0) {
178 child->timestamp = atoi(subtoken+10);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200179 } else if (strncmp(subtoken, "id=", 3) == 0) {
180 child->id = strdup(subtoken+3);
William Lallemand1dc69632019-06-12 19:11:33 +0200181 } else if (strncmp(subtoken, "version=", 8) == 0) {
182 child->version = strdup(subtoken+8);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200183 }
184 }
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200185 if (child->pid) {
William Lallemand920fc8b2019-05-14 11:15:18 +0200186 /* this is a process inherited from a reload that should be leaving */
187 child->options |= PROC_O_LEAVING;
188
William Lallemand48dfbbd2019-04-01 11:29:53 +0200189 LIST_ADDQ(&proc_list, &child->list);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200190 } else {
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200191 mworker_free_child(child);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200192 }
William Lallemand48dfbbd2019-04-01 11:29:53 +0200193 }
194
195 unsetenv("HAPROXY_PROCESSES");
196}
William Lallemand3cd95d22019-04-01 11:29:54 +0200197
198/* Signal blocking and unblocking */
199
200void mworker_block_signals()
201{
202 sigset_t set;
203
204 sigemptyset(&set);
205 sigaddset(&set, SIGUSR1);
206 sigaddset(&set, SIGUSR2);
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100207 sigaddset(&set, SIGTTIN);
208 sigaddset(&set, SIGTTOU);
William Lallemand3cd95d22019-04-01 11:29:54 +0200209 sigaddset(&set, SIGHUP);
210 sigaddset(&set, SIGCHLD);
211 ha_sigmask(SIG_SETMASK, &set, NULL);
212}
213
214void mworker_unblock_signals()
215{
216 haproxy_unblock_signals();
217}
William Lallemand3fa724d2019-04-01 11:29:55 +0200218
William Lallemande25473c2019-04-01 11:29:56 +0200219/* ----- mworker signal handlers ----- */
220
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100221/* broadcast the configured signal to the workers */
222void mworker_broadcast_signal(struct sig_handler *sh)
223{
224 mworker_kill(sh->arg);
225}
226
William Lallemande25473c2019-04-01 11:29:56 +0200227/*
228 * When called, this function reexec haproxy with -sf followed by current
229 * children PIDs and possibly old children PIDs if they didn't leave yet.
230 */
231void mworker_catch_sighup(struct sig_handler *sh)
232{
233 mworker_reload();
234}
235
236void mworker_catch_sigterm(struct sig_handler *sh)
237{
238 int sig = sh->arg;
239
240#if defined(USE_SYSTEMD)
241 if (global.tune.options & GTUNE_USE_SYSTEMD) {
242 sd_notify(0, "STOPPING=1");
243 }
244#endif
245 ha_warning("Exiting Master process...\n");
246 mworker_kill(sig);
247}
248
249/*
250 * Wait for every children to exit
251 */
252
253void mworker_catch_sigchld(struct sig_handler *sh)
254{
255 int exitpid = -1;
256 int status = 0;
William Lallemande25473c2019-04-01 11:29:56 +0200257 int childfound;
258
259restart_wait:
260
261 childfound = 0;
262
263 exitpid = waitpid(-1, &status, WNOHANG);
264 if (exitpid > 0) {
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200265 struct mworker_proc *child, *it;
266
William Lallemande25473c2019-04-01 11:29:56 +0200267 if (WIFEXITED(status))
268 status = WEXITSTATUS(status);
269 else if (WIFSIGNALED(status))
270 status = 128 + WTERMSIG(status);
271 else if (WIFSTOPPED(status))
272 status = 128 + WSTOPSIG(status);
273 else
274 status = 255;
275
William Lallemand3f128872019-04-01 11:29:59 +0200276 /* delete the child from the process list */
William Lallemande25473c2019-04-01 11:29:56 +0200277 list_for_each_entry_safe(child, it, &proc_list, list) {
278 if (child->pid != exitpid)
279 continue;
280
281 LIST_DEL(&child->list);
282 close(child->ipc_fd[0]);
283 childfound = 1;
284 break;
285 }
286
William Lallemand3f128872019-04-01 11:29:59 +0200287 if (!childfound) {
288 /* 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 +0200289 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 +0200290 } else {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200291 /* check if exited child is a current child */
William Lallemand45286112019-04-12 16:09:21 +0200292 if (!(child->options & PROC_O_LEAVING)) {
William Lallemanda655ba42020-05-06 17:27:03 +0200293 if (child->options & PROC_O_TYPE_WORKER) {
294 if (status < 128)
295 ha_warning("Current worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, "Exit");
296 else
297 ha_alert("Current worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, strsignal(status - 128));
298 }
William Lallemand8f7069a2019-04-12 16:09:23 +0200299 else if (child->options & PROC_O_TYPE_PROG)
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200300 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 +0200301
William Lallemande25473c2019-04-01 11:29:56 +0200302 if (status != 0 && status != 130 && status != 143
303 && !(global.tune.options & GTUNE_NOEXIT_ONFAILURE)) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200304 ha_alert("exit-on-failure: killing every processes with SIGTERM\n");
William Lallemande25473c2019-04-01 11:29:56 +0200305 mworker_kill(SIGTERM);
306 }
William Lallemand74f0ec32019-04-16 17:42:44 +0200307 /* 0 & SIGTERM (143) are normal, but we should report SIGINT (130) and other signals */
308 if (exitcode < 0 && status != 0 && status != 143)
309 exitcode = status;
William Lallemande25473c2019-04-01 11:29:56 +0200310 } else {
William Lallemand8f7069a2019-04-12 16:09:23 +0200311 if (child->options & PROC_O_TYPE_WORKER) {
William Lallemand3f128872019-04-01 11:29:59 +0200312 ha_warning("Former worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
313 delete_oldpid(exitpid);
William Lallemand8f7069a2019-04-12 16:09:23 +0200314 } else if (child->options & PROC_O_TYPE_PROG) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200315 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 +0200316 }
William Lallemande25473c2019-04-01 11:29:56 +0200317 }
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200318 mworker_free_child(child);
319 child = NULL;
William Lallemande25473c2019-04-01 11:29:56 +0200320 }
321
322 /* do it again to check if it was the last worker */
323 goto restart_wait;
324 }
325 /* Better rely on the system than on a list of process to check if it was the last one */
326 else if (exitpid == -1 && errno == ECHILD) {
William Lallemand4cf4b332019-04-16 17:42:43 +0200327 ha_warning("All workers exited. Exiting... (%d)\n", (exitcode > 0) ? exitcode : EXIT_SUCCESS);
William Lallemande25473c2019-04-01 11:29:56 +0200328 atexit_flag = 0;
329 if (exitcode > 0)
William Lallemand4cf4b332019-04-16 17:42:43 +0200330 exit(exitcode); /* parent must leave using the status code that provoked the exit */
331 exit(EXIT_SUCCESS);
William Lallemande25473c2019-04-01 11:29:56 +0200332 }
333
334}
335
William Lallemand3fa724d2019-04-01 11:29:55 +0200336/* ----- IPC FD (sockpair) related ----- */
337
338/* This wrapper is called from the workers. It is registered instead of the
339 * normal listener_accept() so the worker can exit() when it detects that the
340 * master closed the IPC FD. If it's not a close, we just call the regular
341 * listener_accept() function */
342void mworker_accept_wrapper(int fd)
343{
344 char c;
345 int ret;
346
347 while (1) {
348 ret = recv(fd, &c, 1, MSG_PEEK);
349 if (ret == -1) {
350 if (errno == EINTR)
351 continue;
352 if (errno == EAGAIN) {
353 fd_cant_recv(fd);
354 return;
355 }
356 break;
357 } else if (ret > 0) {
358 listener_accept(fd);
359 return;
360 } else if (ret == 0) {
361 /* At this step the master is down before
362 * this worker perform a 'normal' exit.
363 * So we want to exit with an error but
364 * other threads could currently process
365 * some stuff so we can't perform a clean
366 * deinit().
367 */
368 exit(EXIT_FAILURE);
369 }
370 }
371 return;
372}
373
374/*
Willy Tarreau619a95f2019-05-20 11:12:15 +0200375 * This function registers the accept wrapper for the sockpair of the master
376 * worker. It's only handled by worker thread #0. Other threads and master do
377 * nothing here. It always returns 1 (success).
William Lallemand3fa724d2019-04-01 11:29:55 +0200378 */
Willy Tarreau619a95f2019-05-20 11:12:15 +0200379static int mworker_pipe_register_per_thread()
William Lallemand3fa724d2019-04-01 11:29:55 +0200380{
Willy Tarreau619a95f2019-05-20 11:12:15 +0200381 if (!(global.mode & MODE_MWORKER) || master)
382 return 1;
383
384 if (tid != 0)
385 return 1;
William Lallemand3fa724d2019-04-01 11:29:55 +0200386
387 fcntl(proc_self->ipc_fd[1], F_SETFL, O_NONBLOCK);
388 /* In multi-tread, we need only one thread to process
389 * events on the pipe with master
390 */
Willy Tarreau619a95f2019-05-20 11:12:15 +0200391 fd_insert(proc_self->ipc_fd[1], fdtab[proc_self->ipc_fd[1]].owner, mworker_accept_wrapper, tid_bit);
William Lallemand3fa724d2019-04-01 11:29:55 +0200392 fd_want_recv(proc_self->ipc_fd[1]);
Willy Tarreau619a95f2019-05-20 11:12:15 +0200393 return 1;
William Lallemand3fa724d2019-04-01 11:29:55 +0200394}
William Lallemand9001ce82019-04-01 11:29:57 +0200395
Willy Tarreau619a95f2019-05-20 11:12:15 +0200396REGISTER_PER_THREAD_INIT(mworker_pipe_register_per_thread);
397
William Lallemand9001ce82019-04-01 11:29:57 +0200398/* ----- proxies ----- */
399/*
400 * Upon a reload, the master worker needs to close all listeners FDs but the mworker_pipe
401 * fd, and the FD provided by fd@
402 */
403void mworker_cleanlisteners()
404{
405 struct listener *l, *l_next;
406 struct proxy *curproxy;
407 struct peers *curpeers;
408
409 /* we might have to unbind some peers sections from some processes */
410 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
411 if (!curpeers->peers_fe)
412 continue;
413
414 stop_proxy(curpeers->peers_fe);
415 /* disable this peer section so that it kills itself */
416 signal_unregister_handler(curpeers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200417 task_destroy(curpeers->sync_task);
William Lallemand9001ce82019-04-01 11:29:57 +0200418 curpeers->sync_task = NULL;
Olivier Houchard3f795f72019-04-17 22:51:06 +0200419 task_destroy(curpeers->peers_fe->task);
William Lallemand9001ce82019-04-01 11:29:57 +0200420 curpeers->peers_fe->task = NULL;
421 curpeers->peers_fe = NULL;
422 }
423
424 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
425 int listen_in_master = 0;
426
427 list_for_each_entry_safe(l, l_next, &curproxy->conf.listeners, by_fe) {
428 /* remove the listener, but not those we need in the master... */
429 if (!(l->options & LI_O_MWORKER)) {
430 /* unbind the listener but does not close if
431 the FD is inherited with fd@ from the parent
432 process */
433 if (l->options & LI_O_INHERITED)
434 unbind_listener_no_close(l);
435 else
436 unbind_listener(l);
437 delete_listener(l);
438 } else {
439 listen_in_master = 1;
440 }
441 }
442 /* if the proxy shouldn't be in the master, we stop it */
443 if (!listen_in_master)
444 curproxy->state = PR_STSTOPPED;
445 }
William Lallemand88dc7c52019-04-01 11:30:01 +0200446}
447
448/* Displays workers and processes */
449static int cli_io_handler_show_proc(struct appctx *appctx)
450{
451 struct stream_interface *si = appctx->owner;
452 struct mworker_proc *child;
453 int old = 0;
454 int up = now.tv_sec - proc_self->timestamp;
William Lallemande8669fc2019-06-12 18:21:17 +0200455 char *uptime = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200456
457 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
458 return 1;
459
460 chunk_reset(&trash);
461
William Lallemand1dc69632019-06-12 19:11:33 +0200462 chunk_printf(&trash, "#%-14s %-15s %-15s %-15s %-15s %-15s\n", "<PID>", "<type>", "<relative PID>", "<reloads>", "<uptime>", "<version>");
William Lallemande8669fc2019-06-12 18:21:17 +0200463 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
Willy Tarreau76a80c72019-06-22 07:41:38 +0200464 chunk_appendf(&trash, "%-15u %-15s %-15u %-15d %-15s %-15s\n", (unsigned int)getpid(), "master", 0, proc_self->reloads, uptime, haproxy_version);
William Lallemande8669fc2019-06-12 18:21:17 +0200465 free(uptime);
466 uptime = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200467
468 /* displays current processes */
469
470 chunk_appendf(&trash, "# workers\n");
471 list_for_each_entry(child, &proc_list, list) {
472 up = now.tv_sec - child->timestamp;
473
William Lallemand8f7069a2019-04-12 16:09:23 +0200474 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200475 continue;
476
William Lallemand45286112019-04-12 16:09:21 +0200477 if (child->options & PROC_O_LEAVING) {
William Lallemand88dc7c52019-04-01 11:30:01 +0200478 old++;
479 continue;
480 }
William Lallemande8669fc2019-06-12 18:21:17 +0200481 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200482 chunk_appendf(&trash, "%-15u %-15s %-15u %-15d %-15s %-15s\n", child->pid, "worker", child->relative_pid, child->reloads, uptime, child->version);
William Lallemande8669fc2019-06-12 18:21:17 +0200483 free(uptime);
484 uptime = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200485 }
486
487 /* displays old processes */
488
489 if (old) {
490 char *msg = NULL;
491
492 chunk_appendf(&trash, "# old workers\n");
493 list_for_each_entry(child, &proc_list, list) {
494 up = now.tv_sec - child->timestamp;
495
William Lallemand8f7069a2019-04-12 16:09:23 +0200496 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200497 continue;
498
William Lallemand45286112019-04-12 16:09:21 +0200499 if (child->options & PROC_O_LEAVING) {
William Lallemand88dc7c52019-04-01 11:30:01 +0200500 memprintf(&msg, "[was: %u]", child->relative_pid);
William Lallemande8669fc2019-06-12 18:21:17 +0200501 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200502 chunk_appendf(&trash, "%-15u %-15s %-15s %-15d %-15s %-15s\n", child->pid, "worker", msg, child->reloads, uptime, child->version);
William Lallemande8669fc2019-06-12 18:21:17 +0200503 free(uptime);
504 uptime = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200505 }
506 }
507 free(msg);
508 }
509
William Lallemandad53d6d2019-04-01 11:30:03 +0200510 /* displays external process */
511 chunk_appendf(&trash, "# programs\n");
512 old = 0;
513 list_for_each_entry(child, &proc_list, list) {
514 up = now.tv_sec - child->timestamp;
515
William Lallemand8f7069a2019-04-12 16:09:23 +0200516 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200517 continue;
518
William Lallemand45286112019-04-12 16:09:21 +0200519 if (child->options & PROC_O_LEAVING) {
William Lallemandad53d6d2019-04-01 11:30:03 +0200520 old++;
521 continue;
522 }
William Lallemande8669fc2019-06-12 18:21:17 +0200523 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200524 chunk_appendf(&trash, "%-15u %-15s %-15s %-15d %-15s %-15s\n", child->pid, child->id, "-", child->reloads, uptime, "-");
William Lallemande8669fc2019-06-12 18:21:17 +0200525 free(uptime);
526 uptime = NULL;
William Lallemandad53d6d2019-04-01 11:30:03 +0200527 }
528
529 if (old) {
530 chunk_appendf(&trash, "# old programs\n");
531 list_for_each_entry(child, &proc_list, list) {
532 up = now.tv_sec - child->timestamp;
533
William Lallemand8f7069a2019-04-12 16:09:23 +0200534 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200535 continue;
536
William Lallemand45286112019-04-12 16:09:21 +0200537 if (child->options & PROC_O_LEAVING) {
William Lallemande8669fc2019-06-12 18:21:17 +0200538 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200539 chunk_appendf(&trash, "%-15u %-15s %-15s %-15d %-15s %-15s\n", child->pid, child->id, "-", child->reloads, uptime, "-");
William Lallemande8669fc2019-06-12 18:21:17 +0200540 free(uptime);
541 uptime = NULL;
William Lallemandad53d6d2019-04-01 11:30:03 +0200542 }
543 }
544 }
545
546
547
William Lallemand88dc7c52019-04-01 11:30:01 +0200548 if (ci_putchk(si_ic(si), &trash) == -1) {
549 si_rx_room_blk(si);
550 return 0;
551 }
552
553 /* dump complete */
554 return 1;
William Lallemand9001ce82019-04-01 11:29:57 +0200555}
William Lallemand88dc7c52019-04-01 11:30:01 +0200556
557/* reload the master process */
558static int cli_parse_reload(char **args, char *payload, struct appctx *appctx, void *private)
559{
560 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
561 return 1;
562
563 mworker_reload();
564
565 return 1;
566}
567
568
William Lallemand27edc4b2019-05-07 17:49:33 +0200569static int mworker_parse_global_max_reloads(char **args, int section_type, struct proxy *curpx,
570 struct proxy *defpx, const char *file, int linenum, char **err)
571{
572
573 int err_code = 0;
574
575 if (alertif_too_many_args(1, file, linenum, args, &err_code))
576 goto out;
577
578 if (*(args[1]) == 0) {
579 memprintf(err, "%sparsing [%s:%d] : '%s' expects an integer argument.\n", *err, file, linenum, args[0]);
580 err_code |= ERR_ALERT | ERR_FATAL;
581 goto out;
582 }
583
584 max_reloads = atol(args[1]);
585 if (max_reloads < 0) {
586 memprintf(err, "%sparsing [%s:%d] '%s' : invalid value %d, must be >= 0", *err, file, linenum, args[0], max_reloads);
587 err_code |= ERR_ALERT | ERR_FATAL;
588 goto out;
589 }
590
591out:
592 return err_code;
593}
594
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200595void mworker_free_child(struct mworker_proc *child)
596{
597 if (child == NULL)
598 return;
599
600 if (child->command) {
601 int i;
602
603 for (i = 0; child->command[i]; i++) {
604 if (child->command[i]) {
605 free(child->command[i]);
606 child->command[i] = NULL;
607 }
William Lallemande8669fc2019-06-12 18:21:17 +0200608
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200609 }
610 free(child->command);
611 child->command = NULL;
612 }
613 if (child->id) {
614 free(child->id);
615 child->id = NULL;
616 }
William Lallemand1dc69632019-06-12 19:11:33 +0200617 if (child->version) {
618 free(child->version);
619 child->version = NULL;
620 }
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200621 free(child);
622}
William Lallemand27edc4b2019-05-07 17:49:33 +0200623
624static struct cfg_kw_list mworker_kws = {{ }, {
625 { CFG_GLOBAL, "mworker-max-reloads", mworker_parse_global_max_reloads },
626 { 0, NULL, NULL },
627}};
628
629INITCALL1(STG_REGISTER, cfg_register_keywords, &mworker_kws);
630
631
William Lallemand88dc7c52019-04-01 11:30:01 +0200632/* register cli keywords */
633static struct cli_kw_list cli_kws = {{ },{
634 { { "@<relative pid>", NULL }, "@<relative pid> : send a command to the <relative pid> process", NULL, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
635 { { "@!<pid>", NULL }, "@!<pid> : send a command to the <pid> process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
636 { { "@master", NULL }, "@master : send a command to the master process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
637 { { "show", "proc", NULL }, "show proc : show processes status", cli_parse_default, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
638 { { "reload", NULL }, "reload : reload haproxy", cli_parse_reload, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
639 {{},}
640}};
641
642INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);