blob: 511d9617dc41fe89053a6e4500e8aaeec015e402 [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>
William Lallemand1dc69632019-06-12 19:11:33 +020023#include <common/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);
207 sigaddset(&set, SIGHUP);
208 sigaddset(&set, SIGCHLD);
209 ha_sigmask(SIG_SETMASK, &set, NULL);
210}
211
212void mworker_unblock_signals()
213{
214 haproxy_unblock_signals();
215}
William Lallemand3fa724d2019-04-01 11:29:55 +0200216
William Lallemande25473c2019-04-01 11:29:56 +0200217/* ----- mworker signal handlers ----- */
218
219/*
220 * When called, this function reexec haproxy with -sf followed by current
221 * children PIDs and possibly old children PIDs if they didn't leave yet.
222 */
223void mworker_catch_sighup(struct sig_handler *sh)
224{
225 mworker_reload();
226}
227
228void mworker_catch_sigterm(struct sig_handler *sh)
229{
230 int sig = sh->arg;
231
232#if defined(USE_SYSTEMD)
233 if (global.tune.options & GTUNE_USE_SYSTEMD) {
234 sd_notify(0, "STOPPING=1");
235 }
236#endif
237 ha_warning("Exiting Master process...\n");
238 mworker_kill(sig);
239}
240
241/*
242 * Wait for every children to exit
243 */
244
245void mworker_catch_sigchld(struct sig_handler *sh)
246{
247 int exitpid = -1;
248 int status = 0;
William Lallemande25473c2019-04-01 11:29:56 +0200249 int childfound;
250
251restart_wait:
252
253 childfound = 0;
254
255 exitpid = waitpid(-1, &status, WNOHANG);
256 if (exitpid > 0) {
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200257 struct mworker_proc *child, *it;
258
William Lallemande25473c2019-04-01 11:29:56 +0200259 if (WIFEXITED(status))
260 status = WEXITSTATUS(status);
261 else if (WIFSIGNALED(status))
262 status = 128 + WTERMSIG(status);
263 else if (WIFSTOPPED(status))
264 status = 128 + WSTOPSIG(status);
265 else
266 status = 255;
267
William Lallemand3f128872019-04-01 11:29:59 +0200268 /* delete the child from the process list */
William Lallemande25473c2019-04-01 11:29:56 +0200269 list_for_each_entry_safe(child, it, &proc_list, list) {
270 if (child->pid != exitpid)
271 continue;
272
273 LIST_DEL(&child->list);
274 close(child->ipc_fd[0]);
275 childfound = 1;
276 break;
277 }
278
William Lallemand3f128872019-04-01 11:29:59 +0200279 if (!childfound) {
280 /* 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 +0200281 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 +0200282 } else {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200283 /* check if exited child is a current child */
William Lallemand45286112019-04-12 16:09:21 +0200284 if (!(child->options & PROC_O_LEAVING)) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200285 if (child->options & PROC_O_TYPE_WORKER)
William Lallemand3f128872019-04-01 11:29:59 +0200286 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 +0200287 else if (child->options & PROC_O_TYPE_PROG)
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200288 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 +0200289
William Lallemande25473c2019-04-01 11:29:56 +0200290 if (status != 0 && status != 130 && status != 143
291 && !(global.tune.options & GTUNE_NOEXIT_ONFAILURE)) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200292 ha_alert("exit-on-failure: killing every processes with SIGTERM\n");
William Lallemande25473c2019-04-01 11:29:56 +0200293 mworker_kill(SIGTERM);
294 }
William Lallemand74f0ec32019-04-16 17:42:44 +0200295 /* 0 & SIGTERM (143) are normal, but we should report SIGINT (130) and other signals */
296 if (exitcode < 0 && status != 0 && status != 143)
297 exitcode = status;
William Lallemande25473c2019-04-01 11:29:56 +0200298 } else {
William Lallemand8f7069a2019-04-12 16:09:23 +0200299 if (child->options & PROC_O_TYPE_WORKER) {
William Lallemand3f128872019-04-01 11:29:59 +0200300 ha_warning("Former worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
301 delete_oldpid(exitpid);
William Lallemand8f7069a2019-04-12 16:09:23 +0200302 } else if (child->options & PROC_O_TYPE_PROG) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200303 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 +0200304 }
William Lallemande25473c2019-04-01 11:29:56 +0200305 }
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200306 mworker_free_child(child);
307 child = NULL;
William Lallemande25473c2019-04-01 11:29:56 +0200308 }
309
310 /* do it again to check if it was the last worker */
311 goto restart_wait;
312 }
313 /* Better rely on the system than on a list of process to check if it was the last one */
314 else if (exitpid == -1 && errno == ECHILD) {
William Lallemand4cf4b332019-04-16 17:42:43 +0200315 ha_warning("All workers exited. Exiting... (%d)\n", (exitcode > 0) ? exitcode : EXIT_SUCCESS);
William Lallemande25473c2019-04-01 11:29:56 +0200316 atexit_flag = 0;
317 if (exitcode > 0)
William Lallemand4cf4b332019-04-16 17:42:43 +0200318 exit(exitcode); /* parent must leave using the status code that provoked the exit */
319 exit(EXIT_SUCCESS);
William Lallemande25473c2019-04-01 11:29:56 +0200320 }
321
322}
323
William Lallemand3fa724d2019-04-01 11:29:55 +0200324/* ----- IPC FD (sockpair) related ----- */
325
326/* This wrapper is called from the workers. It is registered instead of the
327 * normal listener_accept() so the worker can exit() when it detects that the
328 * master closed the IPC FD. If it's not a close, we just call the regular
329 * listener_accept() function */
330void mworker_accept_wrapper(int fd)
331{
332 char c;
333 int ret;
334
335 while (1) {
336 ret = recv(fd, &c, 1, MSG_PEEK);
337 if (ret == -1) {
338 if (errno == EINTR)
339 continue;
340 if (errno == EAGAIN) {
341 fd_cant_recv(fd);
342 return;
343 }
344 break;
345 } else if (ret > 0) {
346 listener_accept(fd);
347 return;
348 } else if (ret == 0) {
349 /* At this step the master is down before
350 * this worker perform a 'normal' exit.
351 * So we want to exit with an error but
352 * other threads could currently process
353 * some stuff so we can't perform a clean
354 * deinit().
355 */
356 exit(EXIT_FAILURE);
357 }
358 }
359 return;
360}
361
362/*
Willy Tarreau619a95f2019-05-20 11:12:15 +0200363 * This function registers the accept wrapper for the sockpair of the master
364 * worker. It's only handled by worker thread #0. Other threads and master do
365 * nothing here. It always returns 1 (success).
William Lallemand3fa724d2019-04-01 11:29:55 +0200366 */
Willy Tarreau619a95f2019-05-20 11:12:15 +0200367static int mworker_pipe_register_per_thread()
William Lallemand3fa724d2019-04-01 11:29:55 +0200368{
Willy Tarreau619a95f2019-05-20 11:12:15 +0200369 if (!(global.mode & MODE_MWORKER) || master)
370 return 1;
371
372 if (tid != 0)
373 return 1;
William Lallemand3fa724d2019-04-01 11:29:55 +0200374
375 fcntl(proc_self->ipc_fd[1], F_SETFL, O_NONBLOCK);
376 /* In multi-tread, we need only one thread to process
377 * events on the pipe with master
378 */
Willy Tarreau619a95f2019-05-20 11:12:15 +0200379 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 +0200380 fd_want_recv(proc_self->ipc_fd[1]);
Willy Tarreau619a95f2019-05-20 11:12:15 +0200381 return 1;
William Lallemand3fa724d2019-04-01 11:29:55 +0200382}
William Lallemand9001ce82019-04-01 11:29:57 +0200383
Willy Tarreau619a95f2019-05-20 11:12:15 +0200384REGISTER_PER_THREAD_INIT(mworker_pipe_register_per_thread);
385
William Lallemand9001ce82019-04-01 11:29:57 +0200386/* ----- proxies ----- */
387/*
388 * Upon a reload, the master worker needs to close all listeners FDs but the mworker_pipe
389 * fd, and the FD provided by fd@
390 */
391void mworker_cleanlisteners()
392{
393 struct listener *l, *l_next;
394 struct proxy *curproxy;
395 struct peers *curpeers;
396
397 /* we might have to unbind some peers sections from some processes */
398 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
399 if (!curpeers->peers_fe)
400 continue;
401
402 stop_proxy(curpeers->peers_fe);
403 /* disable this peer section so that it kills itself */
404 signal_unregister_handler(curpeers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200405 task_destroy(curpeers->sync_task);
William Lallemand9001ce82019-04-01 11:29:57 +0200406 curpeers->sync_task = NULL;
Olivier Houchard3f795f72019-04-17 22:51:06 +0200407 task_destroy(curpeers->peers_fe->task);
William Lallemand9001ce82019-04-01 11:29:57 +0200408 curpeers->peers_fe->task = NULL;
409 curpeers->peers_fe = NULL;
410 }
411
412 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
413 int listen_in_master = 0;
414
415 list_for_each_entry_safe(l, l_next, &curproxy->conf.listeners, by_fe) {
416 /* remove the listener, but not those we need in the master... */
417 if (!(l->options & LI_O_MWORKER)) {
418 /* unbind the listener but does not close if
419 the FD is inherited with fd@ from the parent
420 process */
421 if (l->options & LI_O_INHERITED)
422 unbind_listener_no_close(l);
423 else
424 unbind_listener(l);
425 delete_listener(l);
426 } else {
427 listen_in_master = 1;
428 }
429 }
430 /* if the proxy shouldn't be in the master, we stop it */
431 if (!listen_in_master)
432 curproxy->state = PR_STSTOPPED;
433 }
William Lallemand88dc7c52019-04-01 11:30:01 +0200434}
435
436/* Displays workers and processes */
437static int cli_io_handler_show_proc(struct appctx *appctx)
438{
439 struct stream_interface *si = appctx->owner;
440 struct mworker_proc *child;
441 int old = 0;
442 int up = now.tv_sec - proc_self->timestamp;
William Lallemande8669fc2019-06-12 18:21:17 +0200443 char *uptime = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200444
445 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
446 return 1;
447
448 chunk_reset(&trash);
449
William Lallemand1dc69632019-06-12 19:11:33 +0200450 chunk_printf(&trash, "#%-14s %-15s %-15s %-15s %-15s %-15s\n", "<PID>", "<type>", "<relative PID>", "<reloads>", "<uptime>", "<version>");
William Lallemande8669fc2019-06-12 18:21:17 +0200451 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
Willy Tarreau76a80c72019-06-22 07:41:38 +0200452 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 +0200453 free(uptime);
454 uptime = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200455
456 /* displays current processes */
457
458 chunk_appendf(&trash, "# workers\n");
459 list_for_each_entry(child, &proc_list, list) {
460 up = now.tv_sec - child->timestamp;
461
William Lallemand8f7069a2019-04-12 16:09:23 +0200462 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200463 continue;
464
William Lallemand45286112019-04-12 16:09:21 +0200465 if (child->options & PROC_O_LEAVING) {
William Lallemand88dc7c52019-04-01 11:30:01 +0200466 old++;
467 continue;
468 }
William Lallemande8669fc2019-06-12 18:21:17 +0200469 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200470 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 +0200471 free(uptime);
472 uptime = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200473 }
474
475 /* displays old processes */
476
477 if (old) {
478 char *msg = NULL;
479
480 chunk_appendf(&trash, "# old workers\n");
481 list_for_each_entry(child, &proc_list, list) {
482 up = now.tv_sec - child->timestamp;
483
William Lallemand8f7069a2019-04-12 16:09:23 +0200484 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200485 continue;
486
William Lallemand45286112019-04-12 16:09:21 +0200487 if (child->options & PROC_O_LEAVING) {
William Lallemand88dc7c52019-04-01 11:30:01 +0200488 memprintf(&msg, "[was: %u]", child->relative_pid);
William Lallemande8669fc2019-06-12 18:21:17 +0200489 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200490 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 +0200491 free(uptime);
492 uptime = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200493 }
494 }
495 free(msg);
496 }
497
William Lallemandad53d6d2019-04-01 11:30:03 +0200498 /* displays external process */
499 chunk_appendf(&trash, "# programs\n");
500 old = 0;
501 list_for_each_entry(child, &proc_list, list) {
502 up = now.tv_sec - child->timestamp;
503
William Lallemand8f7069a2019-04-12 16:09:23 +0200504 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200505 continue;
506
William Lallemand45286112019-04-12 16:09:21 +0200507 if (child->options & PROC_O_LEAVING) {
William Lallemandad53d6d2019-04-01 11:30:03 +0200508 old++;
509 continue;
510 }
William Lallemande8669fc2019-06-12 18:21:17 +0200511 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200512 chunk_appendf(&trash, "%-15u %-15s %-15s %-15d %-15s %-15s\n", child->pid, child->id, "-", child->reloads, uptime, "-");
William Lallemande8669fc2019-06-12 18:21:17 +0200513 free(uptime);
514 uptime = NULL;
William Lallemandad53d6d2019-04-01 11:30:03 +0200515 }
516
517 if (old) {
518 chunk_appendf(&trash, "# old programs\n");
519 list_for_each_entry(child, &proc_list, list) {
520 up = now.tv_sec - child->timestamp;
521
William Lallemand8f7069a2019-04-12 16:09:23 +0200522 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200523 continue;
524
William Lallemand45286112019-04-12 16:09:21 +0200525 if (child->options & PROC_O_LEAVING) {
William Lallemande8669fc2019-06-12 18:21:17 +0200526 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200527 chunk_appendf(&trash, "%-15u %-15s %-15s %-15d %-15s %-15s\n", child->pid, child->id, "-", child->reloads, uptime, "-");
William Lallemande8669fc2019-06-12 18:21:17 +0200528 free(uptime);
529 uptime = NULL;
William Lallemandad53d6d2019-04-01 11:30:03 +0200530 }
531 }
532 }
533
534
535
William Lallemand88dc7c52019-04-01 11:30:01 +0200536 if (ci_putchk(si_ic(si), &trash) == -1) {
537 si_rx_room_blk(si);
538 return 0;
539 }
540
541 /* dump complete */
542 return 1;
William Lallemand9001ce82019-04-01 11:29:57 +0200543}
William Lallemand88dc7c52019-04-01 11:30:01 +0200544
545/* reload the master process */
546static int cli_parse_reload(char **args, char *payload, struct appctx *appctx, void *private)
547{
548 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
549 return 1;
550
551 mworker_reload();
552
553 return 1;
554}
555
556
William Lallemand27edc4b2019-05-07 17:49:33 +0200557static int mworker_parse_global_max_reloads(char **args, int section_type, struct proxy *curpx,
558 struct proxy *defpx, const char *file, int linenum, char **err)
559{
560
561 int err_code = 0;
562
563 if (alertif_too_many_args(1, file, linenum, args, &err_code))
564 goto out;
565
566 if (*(args[1]) == 0) {
567 memprintf(err, "%sparsing [%s:%d] : '%s' expects an integer argument.\n", *err, file, linenum, args[0]);
568 err_code |= ERR_ALERT | ERR_FATAL;
569 goto out;
570 }
571
572 max_reloads = atol(args[1]);
573 if (max_reloads < 0) {
574 memprintf(err, "%sparsing [%s:%d] '%s' : invalid value %d, must be >= 0", *err, file, linenum, args[0], max_reloads);
575 err_code |= ERR_ALERT | ERR_FATAL;
576 goto out;
577 }
578
579out:
580 return err_code;
581}
582
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200583void mworker_free_child(struct mworker_proc *child)
584{
585 if (child == NULL)
586 return;
587
588 if (child->command) {
589 int i;
590
591 for (i = 0; child->command[i]; i++) {
592 if (child->command[i]) {
593 free(child->command[i]);
594 child->command[i] = NULL;
595 }
William Lallemande8669fc2019-06-12 18:21:17 +0200596
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200597 }
598 free(child->command);
599 child->command = NULL;
600 }
601 if (child->id) {
602 free(child->id);
603 child->id = NULL;
604 }
William Lallemand1dc69632019-06-12 19:11:33 +0200605 if (child->version) {
606 free(child->version);
607 child->version = NULL;
608 }
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200609 free(child);
610}
William Lallemand27edc4b2019-05-07 17:49:33 +0200611
612static struct cfg_kw_list mworker_kws = {{ }, {
613 { CFG_GLOBAL, "mworker-max-reloads", mworker_parse_global_max_reloads },
614 { 0, NULL, NULL },
615}};
616
617INITCALL1(STG_REGISTER, cfg_register_keywords, &mworker_kws);
618
619
William Lallemand88dc7c52019-04-01 11:30:01 +0200620/* register cli keywords */
621static struct cli_kw_list cli_kws = {{ },{
622 { { "@<relative pid>", NULL }, "@<relative pid> : send a command to the <relative pid> process", NULL, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
623 { { "@!<pid>", NULL }, "@!<pid> : send a command to the <pid> process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
624 { { "@master", NULL }, "@master : send a command to the master process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
625 { { "show", "proc", NULL }, "show proc : show processes status", cli_parse_default, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
626 { { "reload", NULL }, "reload : reload haproxy", cli_parse_reload, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
627 {{},}
628}};
629
630INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);