blob: b69c1adcb243c261e06c95e04508bb54a15a7130 [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
Bertrand Jacquin25439de2021-01-21 01:31:46 +000013#define _GNU_SOURCE
14
William Lallemand3fa724d2019-04-01 11:29:55 +020015#include <errno.h>
16#include <fcntl.h>
17#include <signal.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +020018#include <stdlib.h>
19#include <string.h>
William Lallemande25473c2019-04-01 11:29:56 +020020#include <sys/wait.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +020021
Willy Tarreaub2551052020-06-09 09:07:15 +020022#if defined(USE_SYSTEMD)
23#include <systemd/sd-daemon.h>
24#endif
25
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020026#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020027#include <haproxy/cfgparse.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020028#include <haproxy/cli.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020029#include <haproxy/errors.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020030#include <haproxy/fd.h>
31#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020032#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020033#include <haproxy/listener.h>
Willy Tarreaub5abe5b2020-06-04 14:07:37 +020034#include <haproxy/mworker.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +020035#include <haproxy/peers.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020036#include <haproxy/proxy-t.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +020037#include <haproxy/signal.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020038#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020039#include <haproxy/stream_interface.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020040#include <haproxy/version.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +020041
William Lallemand48dfbbd2019-04-01 11:29:53 +020042
William Lallemande25473c2019-04-01 11:29:56 +020043static int exitcode = -1;
William Lallemand27edc4b2019-05-07 17:49:33 +020044static int max_reloads = -1; /* number max of reloads a worker can have until they are killed */
Willy Tarreau15f9ac32021-05-08 12:30:50 +020045struct mworker_proc *proc_self = NULL; /* process structure of current process */
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
Willy Tarreau2b718102021-04-21 07:32:39 +0200188 LIST_APPEND(&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
Willy Tarreau2b718102021-04-21 07:32:39 +0200280 LIST_DELETE(&child->list);
William Lallemande25473c2019-04-01 11:29:56 +0200281 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
Willy Tarreaua74cb382020-10-15 21:29:49 +0200340 * listener_accept() function.
341 */
William Lallemand3fa724d2019-04-01 11:29:55 +0200342void 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) {
Willy Tarreaua74cb382020-10-15 21:29:49 +0200358 struct listener *l = fdtab[fd].owner;
359
360 if (l)
361 listener_accept(l);
William Lallemand3fa724d2019-04-01 11:29:55 +0200362 return;
363 } else if (ret == 0) {
364 /* At this step the master is down before
365 * this worker perform a 'normal' exit.
366 * So we want to exit with an error but
367 * other threads could currently process
368 * some stuff so we can't perform a clean
369 * deinit().
370 */
371 exit(EXIT_FAILURE);
372 }
373 }
374 return;
375}
376
377/*
Willy Tarreau619a95f2019-05-20 11:12:15 +0200378 * This function registers the accept wrapper for the sockpair of the master
379 * worker. It's only handled by worker thread #0. Other threads and master do
380 * nothing here. It always returns 1 (success).
William Lallemand3fa724d2019-04-01 11:29:55 +0200381 */
Willy Tarreau619a95f2019-05-20 11:12:15 +0200382static int mworker_pipe_register_per_thread()
William Lallemand3fa724d2019-04-01 11:29:55 +0200383{
Willy Tarreau619a95f2019-05-20 11:12:15 +0200384 if (!(global.mode & MODE_MWORKER) || master)
385 return 1;
386
387 if (tid != 0)
388 return 1;
William Lallemand3fa724d2019-04-01 11:29:55 +0200389
390 fcntl(proc_self->ipc_fd[1], F_SETFL, O_NONBLOCK);
391 /* In multi-tread, we need only one thread to process
392 * events on the pipe with master
393 */
Willy Tarreau619a95f2019-05-20 11:12:15 +0200394 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 +0200395 fd_want_recv(proc_self->ipc_fd[1]);
Willy Tarreau619a95f2019-05-20 11:12:15 +0200396 return 1;
William Lallemand3fa724d2019-04-01 11:29:55 +0200397}
William Lallemand9001ce82019-04-01 11:29:57 +0200398
Willy Tarreau619a95f2019-05-20 11:12:15 +0200399REGISTER_PER_THREAD_INIT(mworker_pipe_register_per_thread);
400
William Lallemand9001ce82019-04-01 11:29:57 +0200401/* ----- proxies ----- */
402/*
403 * Upon a reload, the master worker needs to close all listeners FDs but the mworker_pipe
404 * fd, and the FD provided by fd@
405 */
406void mworker_cleanlisteners()
407{
408 struct listener *l, *l_next;
409 struct proxy *curproxy;
410 struct peers *curpeers;
411
412 /* we might have to unbind some peers sections from some processes */
413 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
414 if (!curpeers->peers_fe)
415 continue;
416
417 stop_proxy(curpeers->peers_fe);
418 /* disable this peer section so that it kills itself */
419 signal_unregister_handler(curpeers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200420 task_destroy(curpeers->sync_task);
William Lallemand9001ce82019-04-01 11:29:57 +0200421 curpeers->sync_task = NULL;
Olivier Houchard3f795f72019-04-17 22:51:06 +0200422 task_destroy(curpeers->peers_fe->task);
William Lallemand9001ce82019-04-01 11:29:57 +0200423 curpeers->peers_fe->task = NULL;
424 curpeers->peers_fe = NULL;
425 }
426
427 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
428 int listen_in_master = 0;
429
430 list_for_each_entry_safe(l, l_next, &curproxy->conf.listeners, by_fe) {
431 /* remove the listener, but not those we need in the master... */
Willy Tarreau18c20d22020-10-09 16:11:46 +0200432 if (!(l->rx.flags & RX_F_MWORKER)) {
Willy Tarreau75c98d12020-10-09 15:55:23 +0200433 unbind_listener(l);
William Lallemand9001ce82019-04-01 11:29:57 +0200434 delete_listener(l);
435 } else {
436 listen_in_master = 1;
437 }
438 }
439 /* if the proxy shouldn't be in the master, we stop it */
440 if (!listen_in_master)
Willy Tarreauc3914d42020-09-24 08:39:22 +0200441 curproxy->disabled = 1;
William Lallemand9001ce82019-04-01 11:29:57 +0200442 }
William Lallemand88dc7c52019-04-01 11:30:01 +0200443}
444
445/* Displays workers and processes */
446static int cli_io_handler_show_proc(struct appctx *appctx)
447{
448 struct stream_interface *si = appctx->owner;
449 struct mworker_proc *child;
450 int old = 0;
451 int up = now.tv_sec - proc_self->timestamp;
William Lallemande8669fc2019-06-12 18:21:17 +0200452 char *uptime = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200453
454 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
455 return 1;
456
457 chunk_reset(&trash);
458
William Lallemand1dc69632019-06-12 19:11:33 +0200459 chunk_printf(&trash, "#%-14s %-15s %-15s %-15s %-15s %-15s\n", "<PID>", "<type>", "<relative PID>", "<reloads>", "<uptime>", "<version>");
William Lallemande8669fc2019-06-12 18:21:17 +0200460 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
Willy Tarreau76a80c72019-06-22 07:41:38 +0200461 chunk_appendf(&trash, "%-15u %-15s %-15u %-15d %-15s %-15s\n", (unsigned int)getpid(), "master", 0, proc_self->reloads, uptime, haproxy_version);
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100462 ha_free(&uptime);
William Lallemand88dc7c52019-04-01 11:30:01 +0200463
464 /* displays current processes */
465
466 chunk_appendf(&trash, "# workers\n");
467 list_for_each_entry(child, &proc_list, list) {
468 up = now.tv_sec - child->timestamp;
469
William Lallemand8f7069a2019-04-12 16:09:23 +0200470 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200471 continue;
472
William Lallemand45286112019-04-12 16:09:21 +0200473 if (child->options & PROC_O_LEAVING) {
William Lallemand88dc7c52019-04-01 11:30:01 +0200474 old++;
475 continue;
476 }
William Lallemande8669fc2019-06-12 18:21:17 +0200477 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200478 chunk_appendf(&trash, "%-15u %-15s %-15u %-15d %-15s %-15s\n", child->pid, "worker", child->relative_pid, child->reloads, uptime, child->version);
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100479 ha_free(&uptime);
William Lallemand88dc7c52019-04-01 11:30:01 +0200480 }
481
482 /* displays old processes */
483
484 if (old) {
485 char *msg = NULL;
486
487 chunk_appendf(&trash, "# old workers\n");
488 list_for_each_entry(child, &proc_list, list) {
489 up = now.tv_sec - child->timestamp;
490
William Lallemand8f7069a2019-04-12 16:09:23 +0200491 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200492 continue;
493
William Lallemand45286112019-04-12 16:09:21 +0200494 if (child->options & PROC_O_LEAVING) {
William Lallemand88dc7c52019-04-01 11:30:01 +0200495 memprintf(&msg, "[was: %u]", child->relative_pid);
William Lallemande8669fc2019-06-12 18:21:17 +0200496 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200497 chunk_appendf(&trash, "%-15u %-15s %-15s %-15d %-15s %-15s\n", child->pid, "worker", msg, child->reloads, uptime, child->version);
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100498 ha_free(&uptime);
William Lallemand88dc7c52019-04-01 11:30:01 +0200499 }
500 }
501 free(msg);
502 }
503
William Lallemandad53d6d2019-04-01 11:30:03 +0200504 /* displays external process */
505 chunk_appendf(&trash, "# programs\n");
506 old = 0;
507 list_for_each_entry(child, &proc_list, list) {
508 up = now.tv_sec - child->timestamp;
509
William Lallemand8f7069a2019-04-12 16:09:23 +0200510 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200511 continue;
512
William Lallemand45286112019-04-12 16:09:21 +0200513 if (child->options & PROC_O_LEAVING) {
William Lallemandad53d6d2019-04-01 11:30:03 +0200514 old++;
515 continue;
516 }
William Lallemande8669fc2019-06-12 18:21:17 +0200517 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200518 chunk_appendf(&trash, "%-15u %-15s %-15s %-15d %-15s %-15s\n", child->pid, child->id, "-", child->reloads, uptime, "-");
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100519 ha_free(&uptime);
William Lallemandad53d6d2019-04-01 11:30:03 +0200520 }
521
522 if (old) {
523 chunk_appendf(&trash, "# old programs\n");
524 list_for_each_entry(child, &proc_list, list) {
525 up = now.tv_sec - child->timestamp;
526
William Lallemand8f7069a2019-04-12 16:09:23 +0200527 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200528 continue;
529
William Lallemand45286112019-04-12 16:09:21 +0200530 if (child->options & PROC_O_LEAVING) {
William Lallemande8669fc2019-06-12 18:21:17 +0200531 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200532 chunk_appendf(&trash, "%-15u %-15s %-15s %-15d %-15s %-15s\n", child->pid, child->id, "-", child->reloads, uptime, "-");
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100533 ha_free(&uptime);
William Lallemandad53d6d2019-04-01 11:30:03 +0200534 }
535 }
536 }
537
538
539
William Lallemand88dc7c52019-04-01 11:30:01 +0200540 if (ci_putchk(si_ic(si), &trash) == -1) {
541 si_rx_room_blk(si);
542 return 0;
543 }
544
545 /* dump complete */
546 return 1;
William Lallemand9001ce82019-04-01 11:29:57 +0200547}
William Lallemand88dc7c52019-04-01 11:30:01 +0200548
549/* reload the master process */
550static int cli_parse_reload(char **args, char *payload, struct appctx *appctx, void *private)
551{
552 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
553 return 1;
554
555 mworker_reload();
556
557 return 1;
558}
559
560
William Lallemand27edc4b2019-05-07 17:49:33 +0200561static int mworker_parse_global_max_reloads(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +0100562 const struct proxy *defpx, const char *file, int linenum, char **err)
William Lallemand27edc4b2019-05-07 17:49:33 +0200563{
564
565 int err_code = 0;
566
567 if (alertif_too_many_args(1, file, linenum, args, &err_code))
568 goto out;
569
570 if (*(args[1]) == 0) {
571 memprintf(err, "%sparsing [%s:%d] : '%s' expects an integer argument.\n", *err, file, linenum, args[0]);
572 err_code |= ERR_ALERT | ERR_FATAL;
573 goto out;
574 }
575
576 max_reloads = atol(args[1]);
577 if (max_reloads < 0) {
578 memprintf(err, "%sparsing [%s:%d] '%s' : invalid value %d, must be >= 0", *err, file, linenum, args[0], max_reloads);
579 err_code |= ERR_ALERT | ERR_FATAL;
580 goto out;
581 }
582
583out:
584 return err_code;
585}
586
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200587void mworker_free_child(struct mworker_proc *child)
588{
589 if (child == NULL)
590 return;
591
592 if (child->command) {
593 int i;
594
595 for (i = 0; child->command[i]; i++) {
596 if (child->command[i]) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100597 ha_free(&child->command[i]);
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200598 }
William Lallemande8669fc2019-06-12 18:21:17 +0200599
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200600 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100601 ha_free(&child->command);
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200602 }
603 if (child->id) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100604 ha_free(&child->id);
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200605 }
William Lallemand1dc69632019-06-12 19:11:33 +0200606 if (child->version) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100607 ha_free(&child->version);
William Lallemand1dc69632019-06-12 19:11:33 +0200608 }
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);