blob: 8df748d3fac38c8b07783b3af1f2c9844b177d91 [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 Lallemand27edc4b2019-05-07 17:49:33 +020020#include <common/cfgparse.h>
William Lallemand88dc7c52019-04-01 11:30:01 +020021#include <common/initcall.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +020022#include <common/mini-clist.h>
23
William Lallemand88dc7c52019-04-01 11:30:01 +020024#include <types/cli.h>
25#include <types/global.h>
26#include <types/peers.h>
27#include <types/signal.h>
28
29#include <proto/cli.h>
William Lallemand3fa724d2019-04-01 11:29:55 +020030#include <proto/fd.h>
31#include <proto/listener.h>
William Lallemande25473c2019-04-01 11:29:56 +020032#include <proto/log.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +020033#include <proto/mworker.h>
William Lallemand88dc7c52019-04-01 11:30:01 +020034#include <proto/proxy.h>
William Lallemand3cd95d22019-04-01 11:29:54 +020035#include <proto/signal.h>
William Lallemand88dc7c52019-04-01 11:30:01 +020036#include <proto/stream.h>
37#include <proto/stream_interface.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +020038
William Lallemand48dfbbd2019-04-01 11:29:53 +020039
William Lallemande25473c2019-04-01 11:29:56 +020040#if defined(USE_SYSTEMD)
41#include <systemd/sd-daemon.h>
42#endif
43
44static int exitcode = -1;
William Lallemand27edc4b2019-05-07 17:49:33 +020045static int max_reloads = -1; /* number max of reloads a worker can have until they are killed */
William Lallemande25473c2019-04-01 11:29:56 +020046
William Lallemande25473c2019-04-01 11:29:56 +020047/* ----- children processes handling ----- */
William Lallemand48dfbbd2019-04-01 11:29:53 +020048
William Lallemand48dfbbd2019-04-01 11:29:53 +020049/*
William Lallemande25473c2019-04-01 11:29:56 +020050 * Send signal to every known children.
51 */
52
53static void mworker_kill(int sig)
54{
William Lallemand3f128872019-04-01 11:29:59 +020055 struct mworker_proc *child;
William Lallemande25473c2019-04-01 11:29:56 +020056
William Lallemand3f128872019-04-01 11:29:59 +020057 list_for_each_entry(child, &proc_list, list) {
58 /* 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 +020059 if ((child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG)) && (child->pid > 0))
William Lallemand3f128872019-04-01 11:29:59 +020060 kill(child->pid, sig);
William Lallemande25473c2019-04-01 11:29:56 +020061 }
62}
63
William Lallemand27edc4b2019-05-07 17:49:33 +020064void mworker_kill_max_reloads(int sig)
65{
66 struct mworker_proc *child;
67
68 list_for_each_entry(child, &proc_list, list) {
69 if (max_reloads != -1 && (child->options & PROC_O_TYPE_WORKER) &&
70 (child->pid > 0) && (child->reloads > max_reloads))
71 kill(child->pid, sig);
72 }
73}
William Lallemande25473c2019-04-01 11:29:56 +020074
75/* return 1 if a pid is a current child otherwise 0 */
William Lallemand3f128872019-04-01 11:29:59 +020076int mworker_current_child(int pid)
William Lallemande25473c2019-04-01 11:29:56 +020077{
William Lallemand3f128872019-04-01 11:29:59 +020078 struct mworker_proc *child;
William Lallemande25473c2019-04-01 11:29:56 +020079
William Lallemand3f128872019-04-01 11:29:59 +020080 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +020081 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 +020082 return 1;
83 }
84 return 0;
85}
86
William Lallemand3f128872019-04-01 11:29:59 +020087/*
88 * Return the number of new and old children (including workers and external
89 * processes)
90 */
91int mworker_child_nb()
92{
93 struct mworker_proc *child;
94 int ret = 0;
95
96 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +020097 if (child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG))
William Lallemand3f128872019-04-01 11:29:59 +020098 ret++;
99 }
100
101 return ret;
102}
103
104
William Lallemande25473c2019-04-01 11:29:56 +0200105/*
William Lallemand48dfbbd2019-04-01 11:29:53 +0200106 * serialize the proc list and put it in the environment
107 */
108void mworker_proc_list_to_env()
109{
110 char *msg = NULL;
111 struct mworker_proc *child;
112
113 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200114 char type = '?';
115
116 if (child->options & PROC_O_TYPE_MASTER)
117 type = 'm';
118 else if (child->options & PROC_O_TYPE_PROG)
119 type = 'e';
120 else if (child->options &= PROC_O_TYPE_WORKER)
121 type = 'w';
122
William Lallemand48dfbbd2019-04-01 11:29:53 +0200123 if (child->pid > -1)
William Lallemand8f7069a2019-04-12 16:09:23 +0200124 memprintf(&msg, "%s|type=%c;fd=%d;pid=%d;rpid=%d;reloads=%d;timestamp=%d;id=%s", msg ? msg : "", 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 +0200125 }
126 if (msg)
127 setenv("HAPROXY_PROCESSES", msg, 1);
128}
129
130/*
131 * unserialize the proc list from the environment
132 */
133void mworker_env_to_proc_list()
134{
135 char *msg, *token = NULL, *s1;
136
137 msg = getenv("HAPROXY_PROCESSES");
138 if (!msg)
139 return;
140
141 while ((token = strtok_r(msg, "|", &s1))) {
142 struct mworker_proc *child;
143 char *subtoken = NULL;
144 char *s2;
145
146 msg = NULL;
147
148 child = calloc(1, sizeof(*child));
149
150 while ((subtoken = strtok_r(token, ";", &s2))) {
151
152 token = NULL;
153
154 if (strncmp(subtoken, "type=", 5) == 0) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200155 char type;
156
157 type = *(subtoken+5);
158 if (type == 'm') { /* we are in the master, assign it */
William Lallemand48dfbbd2019-04-01 11:29:53 +0200159 proc_self = child;
William Lallemand8f7069a2019-04-12 16:09:23 +0200160 child->options |= PROC_O_TYPE_MASTER;
161 } else if (type == 'e') {
162 child->options |= PROC_O_TYPE_PROG;
163 } else if (type == 'w') {
164 child->options |= PROC_O_TYPE_WORKER;
165 }
166
William Lallemand48dfbbd2019-04-01 11:29:53 +0200167 } else if (strncmp(subtoken, "fd=", 3) == 0) {
168 child->ipc_fd[0] = atoi(subtoken+3);
169 } else if (strncmp(subtoken, "pid=", 4) == 0) {
170 child->pid = atoi(subtoken+4);
171 } else if (strncmp(subtoken, "rpid=", 5) == 0) {
172 child->relative_pid = atoi(subtoken+5);
173 } else if (strncmp(subtoken, "reloads=", 8) == 0) {
174 /* we reloaded this process once more */
175 child->reloads = atoi(subtoken+8) + 1;
176 } else if (strncmp(subtoken, "timestamp=", 10) == 0) {
177 child->timestamp = atoi(subtoken+10);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200178 } else if (strncmp(subtoken, "id=", 3) == 0) {
179 child->id = strdup(subtoken+3);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200180 }
181 }
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200182 if (child->pid) {
William Lallemand48dfbbd2019-04-01 11:29:53 +0200183 LIST_ADDQ(&proc_list, &child->list);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200184 } else {
185 free(child->id);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200186 free(child);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200187
188 }
William Lallemand45286112019-04-12 16:09:21 +0200189 /* this is a process inherited from a reload that should be leaving */
190 child->options |= PROC_O_LEAVING;
William Lallemand48dfbbd2019-04-01 11:29:53 +0200191 }
192
193 unsetenv("HAPROXY_PROCESSES");
194}
William Lallemand3cd95d22019-04-01 11:29:54 +0200195
196/* Signal blocking and unblocking */
197
198void mworker_block_signals()
199{
200 sigset_t set;
201
202 sigemptyset(&set);
203 sigaddset(&set, SIGUSR1);
204 sigaddset(&set, SIGUSR2);
205 sigaddset(&set, SIGHUP);
206 sigaddset(&set, SIGCHLD);
207 ha_sigmask(SIG_SETMASK, &set, NULL);
208}
209
210void mworker_unblock_signals()
211{
212 haproxy_unblock_signals();
213}
William Lallemand3fa724d2019-04-01 11:29:55 +0200214
William Lallemande25473c2019-04-01 11:29:56 +0200215/* ----- mworker signal handlers ----- */
216
217/*
218 * When called, this function reexec haproxy with -sf followed by current
219 * children PIDs and possibly old children PIDs if they didn't leave yet.
220 */
221void mworker_catch_sighup(struct sig_handler *sh)
222{
223 mworker_reload();
224}
225
226void mworker_catch_sigterm(struct sig_handler *sh)
227{
228 int sig = sh->arg;
229
230#if defined(USE_SYSTEMD)
231 if (global.tune.options & GTUNE_USE_SYSTEMD) {
232 sd_notify(0, "STOPPING=1");
233 }
234#endif
235 ha_warning("Exiting Master process...\n");
236 mworker_kill(sig);
237}
238
239/*
240 * Wait for every children to exit
241 */
242
243void mworker_catch_sigchld(struct sig_handler *sh)
244{
245 int exitpid = -1;
246 int status = 0;
247 struct mworker_proc *child, *it;
248 int childfound;
249
250restart_wait:
251
252 childfound = 0;
253
254 exitpid = waitpid(-1, &status, WNOHANG);
255 if (exitpid > 0) {
256 if (WIFEXITED(status))
257 status = WEXITSTATUS(status);
258 else if (WIFSIGNALED(status))
259 status = 128 + WTERMSIG(status);
260 else if (WIFSTOPPED(status))
261 status = 128 + WSTOPSIG(status);
262 else
263 status = 255;
264
William Lallemand3f128872019-04-01 11:29:59 +0200265 /* delete the child from the process list */
William Lallemande25473c2019-04-01 11:29:56 +0200266 list_for_each_entry_safe(child, it, &proc_list, list) {
267 if (child->pid != exitpid)
268 continue;
269
270 LIST_DEL(&child->list);
271 close(child->ipc_fd[0]);
272 childfound = 1;
273 break;
274 }
275
William Lallemand3f128872019-04-01 11:29:59 +0200276 if (!childfound) {
277 /* 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 +0200278 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 +0200279 } else {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200280 /* check if exited child is a current child */
William Lallemand45286112019-04-12 16:09:21 +0200281 if (!(child->options & PROC_O_LEAVING)) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200282 if (child->options & PROC_O_TYPE_WORKER)
William Lallemand3f128872019-04-01 11:29:59 +0200283 ha_alert("Current worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
William Lallemand8f7069a2019-04-12 16:09:23 +0200284 else if (child->options & PROC_O_TYPE_PROG)
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200285 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 +0200286
William Lallemande25473c2019-04-01 11:29:56 +0200287 if (status != 0 && status != 130 && status != 143
288 && !(global.tune.options & GTUNE_NOEXIT_ONFAILURE)) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200289 ha_alert("exit-on-failure: killing every processes with SIGTERM\n");
William Lallemande25473c2019-04-01 11:29:56 +0200290 mworker_kill(SIGTERM);
291 }
William Lallemand74f0ec32019-04-16 17:42:44 +0200292 /* 0 & SIGTERM (143) are normal, but we should report SIGINT (130) and other signals */
293 if (exitcode < 0 && status != 0 && status != 143)
294 exitcode = status;
William Lallemande25473c2019-04-01 11:29:56 +0200295 } else {
William Lallemand8f7069a2019-04-12 16:09:23 +0200296 if (child->options & PROC_O_TYPE_WORKER) {
William Lallemand3f128872019-04-01 11:29:59 +0200297 ha_warning("Former worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
298 delete_oldpid(exitpid);
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_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 +0200301 }
William Lallemande25473c2019-04-01 11:29:56 +0200302 }
303 free(child);
304 }
305
306 /* do it again to check if it was the last worker */
307 goto restart_wait;
308 }
309 /* Better rely on the system than on a list of process to check if it was the last one */
310 else if (exitpid == -1 && errno == ECHILD) {
William Lallemand4cf4b332019-04-16 17:42:43 +0200311 ha_warning("All workers exited. Exiting... (%d)\n", (exitcode > 0) ? exitcode : EXIT_SUCCESS);
William Lallemande25473c2019-04-01 11:29:56 +0200312 atexit_flag = 0;
313 if (exitcode > 0)
William Lallemand4cf4b332019-04-16 17:42:43 +0200314 exit(exitcode); /* parent must leave using the status code that provoked the exit */
315 exit(EXIT_SUCCESS);
William Lallemande25473c2019-04-01 11:29:56 +0200316 }
317
318}
319
William Lallemand3fa724d2019-04-01 11:29:55 +0200320/* ----- IPC FD (sockpair) related ----- */
321
322/* This wrapper is called from the workers. It is registered instead of the
323 * normal listener_accept() so the worker can exit() when it detects that the
324 * master closed the IPC FD. If it's not a close, we just call the regular
325 * listener_accept() function */
326void mworker_accept_wrapper(int fd)
327{
328 char c;
329 int ret;
330
331 while (1) {
332 ret = recv(fd, &c, 1, MSG_PEEK);
333 if (ret == -1) {
334 if (errno == EINTR)
335 continue;
336 if (errno == EAGAIN) {
337 fd_cant_recv(fd);
338 return;
339 }
340 break;
341 } else if (ret > 0) {
342 listener_accept(fd);
343 return;
344 } else if (ret == 0) {
345 /* At this step the master is down before
346 * this worker perform a 'normal' exit.
347 * So we want to exit with an error but
348 * other threads could currently process
349 * some stuff so we can't perform a clean
350 * deinit().
351 */
352 exit(EXIT_FAILURE);
353 }
354 }
355 return;
356}
357
358/*
359 * This function register the accept wrapper for the sockpair of the master worker
360 */
361void mworker_pipe_register()
362{
363 /* The iocb should be already initialized with listener_accept */
364 if (fdtab[proc_self->ipc_fd[1]].iocb == mworker_accept_wrapper)
365 return;
366
367 fcntl(proc_self->ipc_fd[1], F_SETFL, O_NONBLOCK);
368 /* In multi-tread, we need only one thread to process
369 * events on the pipe with master
370 */
371 fd_insert(proc_self->ipc_fd[1], fdtab[proc_self->ipc_fd[1]].owner, mworker_accept_wrapper, 1);
372 fd_want_recv(proc_self->ipc_fd[1]);
373}
William Lallemand9001ce82019-04-01 11:29:57 +0200374
375/* ----- proxies ----- */
376/*
377 * Upon a reload, the master worker needs to close all listeners FDs but the mworker_pipe
378 * fd, and the FD provided by fd@
379 */
380void mworker_cleanlisteners()
381{
382 struct listener *l, *l_next;
383 struct proxy *curproxy;
384 struct peers *curpeers;
385
386 /* we might have to unbind some peers sections from some processes */
387 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
388 if (!curpeers->peers_fe)
389 continue;
390
391 stop_proxy(curpeers->peers_fe);
392 /* disable this peer section so that it kills itself */
393 signal_unregister_handler(curpeers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200394 task_destroy(curpeers->sync_task);
William Lallemand9001ce82019-04-01 11:29:57 +0200395 curpeers->sync_task = NULL;
Olivier Houchard3f795f72019-04-17 22:51:06 +0200396 task_destroy(curpeers->peers_fe->task);
William Lallemand9001ce82019-04-01 11:29:57 +0200397 curpeers->peers_fe->task = NULL;
398 curpeers->peers_fe = NULL;
399 }
400
401 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
402 int listen_in_master = 0;
403
404 list_for_each_entry_safe(l, l_next, &curproxy->conf.listeners, by_fe) {
405 /* remove the listener, but not those we need in the master... */
406 if (!(l->options & LI_O_MWORKER)) {
407 /* unbind the listener but does not close if
408 the FD is inherited with fd@ from the parent
409 process */
410 if (l->options & LI_O_INHERITED)
411 unbind_listener_no_close(l);
412 else
413 unbind_listener(l);
414 delete_listener(l);
415 } else {
416 listen_in_master = 1;
417 }
418 }
419 /* if the proxy shouldn't be in the master, we stop it */
420 if (!listen_in_master)
421 curproxy->state = PR_STSTOPPED;
422 }
William Lallemand88dc7c52019-04-01 11:30:01 +0200423}
424
425/* Displays workers and processes */
426static int cli_io_handler_show_proc(struct appctx *appctx)
427{
428 struct stream_interface *si = appctx->owner;
429 struct mworker_proc *child;
430 int old = 0;
431 int up = now.tv_sec - proc_self->timestamp;
432
433 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
434 return 1;
435
436 chunk_reset(&trash);
437
438 chunk_printf(&trash, "#%-14s %-15s %-15s %-15s %s\n", "<PID>", "<type>", "<relative PID>", "<reloads>", "<uptime>");
439 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));
440
441 /* displays current processes */
442
443 chunk_appendf(&trash, "# workers\n");
444 list_for_each_entry(child, &proc_list, list) {
445 up = now.tv_sec - child->timestamp;
446
William Lallemand8f7069a2019-04-12 16:09:23 +0200447 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200448 continue;
449
William Lallemand45286112019-04-12 16:09:21 +0200450 if (child->options & PROC_O_LEAVING) {
William Lallemand88dc7c52019-04-01 11:30:01 +0200451 old++;
452 continue;
453 }
454 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));
455 }
456
457 /* displays old processes */
458
459 if (old) {
460 char *msg = NULL;
461
462 chunk_appendf(&trash, "# old workers\n");
463 list_for_each_entry(child, &proc_list, list) {
464 up = now.tv_sec - child->timestamp;
465
William Lallemand8f7069a2019-04-12 16:09:23 +0200466 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200467 continue;
468
William Lallemand45286112019-04-12 16:09:21 +0200469 if (child->options & PROC_O_LEAVING) {
William Lallemand88dc7c52019-04-01 11:30:01 +0200470 memprintf(&msg, "[was: %u]", child->relative_pid);
471 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));
472 }
473 }
474 free(msg);
475 }
476
William Lallemandad53d6d2019-04-01 11:30:03 +0200477 /* displays external process */
478 chunk_appendf(&trash, "# programs\n");
479 old = 0;
480 list_for_each_entry(child, &proc_list, list) {
481 up = now.tv_sec - child->timestamp;
482
William Lallemand8f7069a2019-04-12 16:09:23 +0200483 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200484 continue;
485
William Lallemand45286112019-04-12 16:09:21 +0200486 if (child->options & PROC_O_LEAVING) {
William Lallemandad53d6d2019-04-01 11:30:03 +0200487 old++;
488 continue;
489 }
490 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));
491 }
492
493 if (old) {
494 chunk_appendf(&trash, "# old programs\n");
495 list_for_each_entry(child, &proc_list, list) {
496 up = now.tv_sec - child->timestamp;
497
William Lallemand8f7069a2019-04-12 16:09:23 +0200498 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200499 continue;
500
William Lallemand45286112019-04-12 16:09:21 +0200501 if (child->options & PROC_O_LEAVING) {
William Lallemandad53d6d2019-04-01 11:30:03 +0200502 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));
503 }
504 }
505 }
506
507
508
William Lallemand88dc7c52019-04-01 11:30:01 +0200509 if (ci_putchk(si_ic(si), &trash) == -1) {
510 si_rx_room_blk(si);
511 return 0;
512 }
513
514 /* dump complete */
515 return 1;
William Lallemand9001ce82019-04-01 11:29:57 +0200516}
William Lallemand88dc7c52019-04-01 11:30:01 +0200517
518/* reload the master process */
519static int cli_parse_reload(char **args, char *payload, struct appctx *appctx, void *private)
520{
521 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
522 return 1;
523
524 mworker_reload();
525
526 return 1;
527}
528
529
William Lallemand27edc4b2019-05-07 17:49:33 +0200530static int mworker_parse_global_max_reloads(char **args, int section_type, struct proxy *curpx,
531 struct proxy *defpx, const char *file, int linenum, char **err)
532{
533
534 int err_code = 0;
535
536 if (alertif_too_many_args(1, file, linenum, args, &err_code))
537 goto out;
538
539 if (*(args[1]) == 0) {
540 memprintf(err, "%sparsing [%s:%d] : '%s' expects an integer argument.\n", *err, file, linenum, args[0]);
541 err_code |= ERR_ALERT | ERR_FATAL;
542 goto out;
543 }
544
545 max_reloads = atol(args[1]);
546 if (max_reloads < 0) {
547 memprintf(err, "%sparsing [%s:%d] '%s' : invalid value %d, must be >= 0", *err, file, linenum, args[0], max_reloads);
548 err_code |= ERR_ALERT | ERR_FATAL;
549 goto out;
550 }
551
552out:
553 return err_code;
554}
555
556
557static struct cfg_kw_list mworker_kws = {{ }, {
558 { CFG_GLOBAL, "mworker-max-reloads", mworker_parse_global_max_reloads },
559 { 0, NULL, NULL },
560}};
561
562INITCALL1(STG_REGISTER, cfg_register_keywords, &mworker_kws);
563
564
William Lallemand88dc7c52019-04-01 11:30:01 +0200565/* register cli keywords */
566static struct cli_kw_list cli_kws = {{ },{
567 { { "@<relative pid>", NULL }, "@<relative pid> : send a command to the <relative pid> process", NULL, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
568 { { "@!<pid>", NULL }, "@!<pid> : send a command to the <pid> process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
569 { { "@master", NULL }, "@master : send a command to the master process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
570 { { "show", "proc", NULL }, "show proc : show processes status", cli_parse_default, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
571 { { "reload", NULL }, "reload : reload haproxy", cli_parse_reload, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
572 {{},}
573}};
574
575INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);