blob: 091a6dfb04164e7f94cf47a5da1e3a341afe3a04 [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 Tarreau213e9902020-06-04 14:58:24 +020023#include <haproxy/listener.h>
Willy Tarreaub5abe5b2020-06-04 14:07:37 +020024#include <haproxy/mworker.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +020025#include <haproxy/peers.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +020026#include <haproxy/signal.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020027#include <haproxy/version.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +020028
William Lallemand88dc7c52019-04-01 11:30:01 +020029#include <types/cli.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020030#include <haproxy/global.h>
William Lallemand88dc7c52019-04-01 11:30:01 +020031
32#include <proto/cli.h>
Willy Tarreau0f6ffd62020-06-03 19:33:00 +020033#include <haproxy/fd.h>
William Lallemande25473c2019-04-01 11:29:56 +020034#include <proto/log.h>
William Lallemand88dc7c52019-04-01 11:30:01 +020035#include <proto/proxy.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 Lallemand1dc69632019-06-12 19:11:33 +0200124 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 +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 Lallemand1dc69632019-06-12 19:11:33 +0200180 } else if (strncmp(subtoken, "version=", 8) == 0) {
181 child->version = strdup(subtoken+8);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200182 }
183 }
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200184 if (child->pid) {
William Lallemand920fc8b2019-05-14 11:15:18 +0200185 /* this is a process inherited from a reload that should be leaving */
186 child->options |= PROC_O_LEAVING;
187
William Lallemand48dfbbd2019-04-01 11:29:53 +0200188 LIST_ADDQ(&proc_list, &child->list);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200189 } else {
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200190 mworker_free_child(child);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200191 }
William Lallemand48dfbbd2019-04-01 11:29:53 +0200192 }
193
194 unsetenv("HAPROXY_PROCESSES");
195}
William Lallemand3cd95d22019-04-01 11:29:54 +0200196
197/* Signal blocking and unblocking */
198
199void mworker_block_signals()
200{
201 sigset_t set;
202
203 sigemptyset(&set);
204 sigaddset(&set, SIGUSR1);
205 sigaddset(&set, SIGUSR2);
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100206 sigaddset(&set, SIGTTIN);
207 sigaddset(&set, SIGTTOU);
William Lallemand3cd95d22019-04-01 11:29:54 +0200208 sigaddset(&set, SIGHUP);
209 sigaddset(&set, SIGCHLD);
210 ha_sigmask(SIG_SETMASK, &set, NULL);
211}
212
213void mworker_unblock_signals()
214{
215 haproxy_unblock_signals();
216}
William Lallemand3fa724d2019-04-01 11:29:55 +0200217
William Lallemande25473c2019-04-01 11:29:56 +0200218/* ----- mworker signal handlers ----- */
219
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100220/* broadcast the configured signal to the workers */
221void mworker_broadcast_signal(struct sig_handler *sh)
222{
223 mworker_kill(sh->arg);
224}
225
William Lallemande25473c2019-04-01 11:29:56 +0200226/*
227 * When called, this function reexec haproxy with -sf followed by current
228 * children PIDs and possibly old children PIDs if they didn't leave yet.
229 */
230void mworker_catch_sighup(struct sig_handler *sh)
231{
232 mworker_reload();
233}
234
235void mworker_catch_sigterm(struct sig_handler *sh)
236{
237 int sig = sh->arg;
238
239#if defined(USE_SYSTEMD)
240 if (global.tune.options & GTUNE_USE_SYSTEMD) {
241 sd_notify(0, "STOPPING=1");
242 }
243#endif
244 ha_warning("Exiting Master process...\n");
245 mworker_kill(sig);
246}
247
248/*
249 * Wait for every children to exit
250 */
251
252void mworker_catch_sigchld(struct sig_handler *sh)
253{
254 int exitpid = -1;
255 int status = 0;
William Lallemande25473c2019-04-01 11:29:56 +0200256 int childfound;
257
258restart_wait:
259
260 childfound = 0;
261
262 exitpid = waitpid(-1, &status, WNOHANG);
263 if (exitpid > 0) {
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200264 struct mworker_proc *child, *it;
265
William Lallemande25473c2019-04-01 11:29:56 +0200266 if (WIFEXITED(status))
267 status = WEXITSTATUS(status);
268 else if (WIFSIGNALED(status))
269 status = 128 + WTERMSIG(status);
270 else if (WIFSTOPPED(status))
271 status = 128 + WSTOPSIG(status);
272 else
273 status = 255;
274
William Lallemand3f128872019-04-01 11:29:59 +0200275 /* delete the child from the process list */
William Lallemande25473c2019-04-01 11:29:56 +0200276 list_for_each_entry_safe(child, it, &proc_list, list) {
277 if (child->pid != exitpid)
278 continue;
279
280 LIST_DEL(&child->list);
281 close(child->ipc_fd[0]);
282 childfound = 1;
283 break;
284 }
285
William Lallemand3f128872019-04-01 11:29:59 +0200286 if (!childfound) {
287 /* 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 +0200288 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 +0200289 } else {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200290 /* check if exited child is a current child */
William Lallemand45286112019-04-12 16:09:21 +0200291 if (!(child->options & PROC_O_LEAVING)) {
William Lallemanda655ba42020-05-06 17:27:03 +0200292 if (child->options & PROC_O_TYPE_WORKER) {
293 if (status < 128)
294 ha_warning("Current worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, "Exit");
295 else
296 ha_alert("Current worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, strsignal(status - 128));
297 }
William Lallemand8f7069a2019-04-12 16:09:23 +0200298 else if (child->options & PROC_O_TYPE_PROG)
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200299 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 +0200300
William Lallemande25473c2019-04-01 11:29:56 +0200301 if (status != 0 && status != 130 && status != 143
302 && !(global.tune.options & GTUNE_NOEXIT_ONFAILURE)) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200303 ha_alert("exit-on-failure: killing every processes with SIGTERM\n");
William Lallemande25473c2019-04-01 11:29:56 +0200304 mworker_kill(SIGTERM);
305 }
William Lallemand74f0ec32019-04-16 17:42:44 +0200306 /* 0 & SIGTERM (143) are normal, but we should report SIGINT (130) and other signals */
307 if (exitcode < 0 && status != 0 && status != 143)
308 exitcode = status;
William Lallemande25473c2019-04-01 11:29:56 +0200309 } else {
William Lallemand8f7069a2019-04-12 16:09:23 +0200310 if (child->options & PROC_O_TYPE_WORKER) {
William Lallemand3f128872019-04-01 11:29:59 +0200311 ha_warning("Former worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
312 delete_oldpid(exitpid);
William Lallemand8f7069a2019-04-12 16:09:23 +0200313 } else if (child->options & PROC_O_TYPE_PROG) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200314 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 +0200315 }
William Lallemande25473c2019-04-01 11:29:56 +0200316 }
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200317 mworker_free_child(child);
318 child = NULL;
William Lallemande25473c2019-04-01 11:29:56 +0200319 }
320
321 /* do it again to check if it was the last worker */
322 goto restart_wait;
323 }
324 /* Better rely on the system than on a list of process to check if it was the last one */
325 else if (exitpid == -1 && errno == ECHILD) {
William Lallemand4cf4b332019-04-16 17:42:43 +0200326 ha_warning("All workers exited. Exiting... (%d)\n", (exitcode > 0) ? exitcode : EXIT_SUCCESS);
William Lallemande25473c2019-04-01 11:29:56 +0200327 atexit_flag = 0;
328 if (exitcode > 0)
William Lallemand4cf4b332019-04-16 17:42:43 +0200329 exit(exitcode); /* parent must leave using the status code that provoked the exit */
330 exit(EXIT_SUCCESS);
William Lallemande25473c2019-04-01 11:29:56 +0200331 }
332
333}
334
William Lallemand3fa724d2019-04-01 11:29:55 +0200335/* ----- IPC FD (sockpair) related ----- */
336
337/* This wrapper is called from the workers. It is registered instead of the
338 * normal listener_accept() so the worker can exit() when it detects that the
339 * master closed the IPC FD. If it's not a close, we just call the regular
340 * listener_accept() function */
341void mworker_accept_wrapper(int fd)
342{
343 char c;
344 int ret;
345
346 while (1) {
347 ret = recv(fd, &c, 1, MSG_PEEK);
348 if (ret == -1) {
349 if (errno == EINTR)
350 continue;
351 if (errno == EAGAIN) {
352 fd_cant_recv(fd);
353 return;
354 }
355 break;
356 } else if (ret > 0) {
357 listener_accept(fd);
358 return;
359 } else if (ret == 0) {
360 /* At this step the master is down before
361 * this worker perform a 'normal' exit.
362 * So we want to exit with an error but
363 * other threads could currently process
364 * some stuff so we can't perform a clean
365 * deinit().
366 */
367 exit(EXIT_FAILURE);
368 }
369 }
370 return;
371}
372
373/*
Willy Tarreau619a95f2019-05-20 11:12:15 +0200374 * This function registers the accept wrapper for the sockpair of the master
375 * worker. It's only handled by worker thread #0. Other threads and master do
376 * nothing here. It always returns 1 (success).
William Lallemand3fa724d2019-04-01 11:29:55 +0200377 */
Willy Tarreau619a95f2019-05-20 11:12:15 +0200378static int mworker_pipe_register_per_thread()
William Lallemand3fa724d2019-04-01 11:29:55 +0200379{
Willy Tarreau619a95f2019-05-20 11:12:15 +0200380 if (!(global.mode & MODE_MWORKER) || master)
381 return 1;
382
383 if (tid != 0)
384 return 1;
William Lallemand3fa724d2019-04-01 11:29:55 +0200385
386 fcntl(proc_self->ipc_fd[1], F_SETFL, O_NONBLOCK);
387 /* In multi-tread, we need only one thread to process
388 * events on the pipe with master
389 */
Willy Tarreau619a95f2019-05-20 11:12:15 +0200390 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 +0200391 fd_want_recv(proc_self->ipc_fd[1]);
Willy Tarreau619a95f2019-05-20 11:12:15 +0200392 return 1;
William Lallemand3fa724d2019-04-01 11:29:55 +0200393}
William Lallemand9001ce82019-04-01 11:29:57 +0200394
Willy Tarreau619a95f2019-05-20 11:12:15 +0200395REGISTER_PER_THREAD_INIT(mworker_pipe_register_per_thread);
396
William Lallemand9001ce82019-04-01 11:29:57 +0200397/* ----- proxies ----- */
398/*
399 * Upon a reload, the master worker needs to close all listeners FDs but the mworker_pipe
400 * fd, and the FD provided by fd@
401 */
402void mworker_cleanlisteners()
403{
404 struct listener *l, *l_next;
405 struct proxy *curproxy;
406 struct peers *curpeers;
407
408 /* we might have to unbind some peers sections from some processes */
409 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
410 if (!curpeers->peers_fe)
411 continue;
412
413 stop_proxy(curpeers->peers_fe);
414 /* disable this peer section so that it kills itself */
415 signal_unregister_handler(curpeers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200416 task_destroy(curpeers->sync_task);
William Lallemand9001ce82019-04-01 11:29:57 +0200417 curpeers->sync_task = NULL;
Olivier Houchard3f795f72019-04-17 22:51:06 +0200418 task_destroy(curpeers->peers_fe->task);
William Lallemand9001ce82019-04-01 11:29:57 +0200419 curpeers->peers_fe->task = NULL;
420 curpeers->peers_fe = NULL;
421 }
422
423 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
424 int listen_in_master = 0;
425
426 list_for_each_entry_safe(l, l_next, &curproxy->conf.listeners, by_fe) {
427 /* remove the listener, but not those we need in the master... */
428 if (!(l->options & LI_O_MWORKER)) {
429 /* unbind the listener but does not close if
430 the FD is inherited with fd@ from the parent
431 process */
432 if (l->options & LI_O_INHERITED)
433 unbind_listener_no_close(l);
434 else
435 unbind_listener(l);
436 delete_listener(l);
437 } else {
438 listen_in_master = 1;
439 }
440 }
441 /* if the proxy shouldn't be in the master, we stop it */
442 if (!listen_in_master)
443 curproxy->state = PR_STSTOPPED;
444 }
William Lallemand88dc7c52019-04-01 11:30:01 +0200445}
446
447/* Displays workers and processes */
448static int cli_io_handler_show_proc(struct appctx *appctx)
449{
450 struct stream_interface *si = appctx->owner;
451 struct mworker_proc *child;
452 int old = 0;
453 int up = now.tv_sec - proc_self->timestamp;
William Lallemande8669fc2019-06-12 18:21:17 +0200454 char *uptime = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200455
456 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
457 return 1;
458
459 chunk_reset(&trash);
460
William Lallemand1dc69632019-06-12 19:11:33 +0200461 chunk_printf(&trash, "#%-14s %-15s %-15s %-15s %-15s %-15s\n", "<PID>", "<type>", "<relative PID>", "<reloads>", "<uptime>", "<version>");
William Lallemande8669fc2019-06-12 18:21:17 +0200462 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
Willy Tarreau76a80c72019-06-22 07:41:38 +0200463 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 +0200464 free(uptime);
465 uptime = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200466
467 /* displays current processes */
468
469 chunk_appendf(&trash, "# workers\n");
470 list_for_each_entry(child, &proc_list, list) {
471 up = now.tv_sec - child->timestamp;
472
William Lallemand8f7069a2019-04-12 16:09:23 +0200473 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200474 continue;
475
William Lallemand45286112019-04-12 16:09:21 +0200476 if (child->options & PROC_O_LEAVING) {
William Lallemand88dc7c52019-04-01 11:30:01 +0200477 old++;
478 continue;
479 }
William Lallemande8669fc2019-06-12 18:21:17 +0200480 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200481 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 +0200482 free(uptime);
483 uptime = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200484 }
485
486 /* displays old processes */
487
488 if (old) {
489 char *msg = NULL;
490
491 chunk_appendf(&trash, "# old workers\n");
492 list_for_each_entry(child, &proc_list, list) {
493 up = now.tv_sec - child->timestamp;
494
William Lallemand8f7069a2019-04-12 16:09:23 +0200495 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200496 continue;
497
William Lallemand45286112019-04-12 16:09:21 +0200498 if (child->options & PROC_O_LEAVING) {
William Lallemand88dc7c52019-04-01 11:30:01 +0200499 memprintf(&msg, "[was: %u]", child->relative_pid);
William Lallemande8669fc2019-06-12 18:21:17 +0200500 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200501 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 +0200502 free(uptime);
503 uptime = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200504 }
505 }
506 free(msg);
507 }
508
William Lallemandad53d6d2019-04-01 11:30:03 +0200509 /* displays external process */
510 chunk_appendf(&trash, "# programs\n");
511 old = 0;
512 list_for_each_entry(child, &proc_list, list) {
513 up = now.tv_sec - child->timestamp;
514
William Lallemand8f7069a2019-04-12 16:09:23 +0200515 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200516 continue;
517
William Lallemand45286112019-04-12 16:09:21 +0200518 if (child->options & PROC_O_LEAVING) {
William Lallemandad53d6d2019-04-01 11:30:03 +0200519 old++;
520 continue;
521 }
William Lallemande8669fc2019-06-12 18:21:17 +0200522 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200523 chunk_appendf(&trash, "%-15u %-15s %-15s %-15d %-15s %-15s\n", child->pid, child->id, "-", child->reloads, uptime, "-");
William Lallemande8669fc2019-06-12 18:21:17 +0200524 free(uptime);
525 uptime = NULL;
William Lallemandad53d6d2019-04-01 11:30:03 +0200526 }
527
528 if (old) {
529 chunk_appendf(&trash, "# old programs\n");
530 list_for_each_entry(child, &proc_list, list) {
531 up = now.tv_sec - child->timestamp;
532
William Lallemand8f7069a2019-04-12 16:09:23 +0200533 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200534 continue;
535
William Lallemand45286112019-04-12 16:09:21 +0200536 if (child->options & PROC_O_LEAVING) {
William Lallemande8669fc2019-06-12 18:21:17 +0200537 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200538 chunk_appendf(&trash, "%-15u %-15s %-15s %-15d %-15s %-15s\n", child->pid, child->id, "-", child->reloads, uptime, "-");
William Lallemande8669fc2019-06-12 18:21:17 +0200539 free(uptime);
540 uptime = NULL;
William Lallemandad53d6d2019-04-01 11:30:03 +0200541 }
542 }
543 }
544
545
546
William Lallemand88dc7c52019-04-01 11:30:01 +0200547 if (ci_putchk(si_ic(si), &trash) == -1) {
548 si_rx_room_blk(si);
549 return 0;
550 }
551
552 /* dump complete */
553 return 1;
William Lallemand9001ce82019-04-01 11:29:57 +0200554}
William Lallemand88dc7c52019-04-01 11:30:01 +0200555
556/* reload the master process */
557static int cli_parse_reload(char **args, char *payload, struct appctx *appctx, void *private)
558{
559 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
560 return 1;
561
562 mworker_reload();
563
564 return 1;
565}
566
567
William Lallemand27edc4b2019-05-07 17:49:33 +0200568static int mworker_parse_global_max_reloads(char **args, int section_type, struct proxy *curpx,
569 struct proxy *defpx, const char *file, int linenum, char **err)
570{
571
572 int err_code = 0;
573
574 if (alertif_too_many_args(1, file, linenum, args, &err_code))
575 goto out;
576
577 if (*(args[1]) == 0) {
578 memprintf(err, "%sparsing [%s:%d] : '%s' expects an integer argument.\n", *err, file, linenum, args[0]);
579 err_code |= ERR_ALERT | ERR_FATAL;
580 goto out;
581 }
582
583 max_reloads = atol(args[1]);
584 if (max_reloads < 0) {
585 memprintf(err, "%sparsing [%s:%d] '%s' : invalid value %d, must be >= 0", *err, file, linenum, args[0], max_reloads);
586 err_code |= ERR_ALERT | ERR_FATAL;
587 goto out;
588 }
589
590out:
591 return err_code;
592}
593
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200594void mworker_free_child(struct mworker_proc *child)
595{
596 if (child == NULL)
597 return;
598
599 if (child->command) {
600 int i;
601
602 for (i = 0; child->command[i]; i++) {
603 if (child->command[i]) {
604 free(child->command[i]);
605 child->command[i] = NULL;
606 }
William Lallemande8669fc2019-06-12 18:21:17 +0200607
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200608 }
609 free(child->command);
610 child->command = NULL;
611 }
612 if (child->id) {
613 free(child->id);
614 child->id = NULL;
615 }
William Lallemand1dc69632019-06-12 19:11:33 +0200616 if (child->version) {
617 free(child->version);
618 child->version = NULL;
619 }
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200620 free(child);
621}
William Lallemand27edc4b2019-05-07 17:49:33 +0200622
623static struct cfg_kw_list mworker_kws = {{ }, {
624 { CFG_GLOBAL, "mworker-max-reloads", mworker_parse_global_max_reloads },
625 { 0, NULL, NULL },
626}};
627
628INITCALL1(STG_REGISTER, cfg_register_keywords, &mworker_kws);
629
630
William Lallemand88dc7c52019-04-01 11:30:01 +0200631/* register cli keywords */
632static struct cli_kw_list cli_kws = {{ },{
633 { { "@<relative pid>", NULL }, "@<relative pid> : send a command to the <relative pid> process", NULL, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
634 { { "@!<pid>", NULL }, "@!<pid> : send a command to the <pid> process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
635 { { "@master", NULL }, "@master : send a command to the master process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
636 { { "show", "proc", NULL }, "show proc : show processes status", cli_parse_default, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
637 { { "reload", NULL }, "reload : reload haproxy", cli_parse_reload, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
638 {{},}
639}};
640
641INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);