blob: 39e28b26d454ba62cdee4ae38657ddbeb239f7c4 [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 Jacquinc3f67cd2021-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 */
William Lallemande25473c2019-04-01 11:29:56 +020045
William Lallemande25473c2019-04-01 11:29:56 +020046/* ----- children processes handling ----- */
William Lallemand48dfbbd2019-04-01 11:29:53 +020047
William Lallemand48dfbbd2019-04-01 11:29:53 +020048/*
William Lallemande25473c2019-04-01 11:29:56 +020049 * Send signal to every known children.
50 */
51
52static void mworker_kill(int sig)
53{
William Lallemand3f128872019-04-01 11:29:59 +020054 struct mworker_proc *child;
William Lallemande25473c2019-04-01 11:29:56 +020055
William Lallemand3f128872019-04-01 11:29:59 +020056 list_for_each_entry(child, &proc_list, list) {
57 /* 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 +020058 if ((child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG)) && (child->pid > 0))
William Lallemand3f128872019-04-01 11:29:59 +020059 kill(child->pid, sig);
William Lallemande25473c2019-04-01 11:29:56 +020060 }
61}
62
William Lallemand27edc4b2019-05-07 17:49:33 +020063void mworker_kill_max_reloads(int sig)
64{
65 struct mworker_proc *child;
66
67 list_for_each_entry(child, &proc_list, list) {
68 if (max_reloads != -1 && (child->options & PROC_O_TYPE_WORKER) &&
69 (child->pid > 0) && (child->reloads > max_reloads))
70 kill(child->pid, sig);
71 }
72}
William Lallemande25473c2019-04-01 11:29:56 +020073
74/* return 1 if a pid is a current child otherwise 0 */
William Lallemand3f128872019-04-01 11:29:59 +020075int mworker_current_child(int pid)
William Lallemande25473c2019-04-01 11:29:56 +020076{
William Lallemand3f128872019-04-01 11:29:59 +020077 struct mworker_proc *child;
William Lallemande25473c2019-04-01 11:29:56 +020078
William Lallemand3f128872019-04-01 11:29:59 +020079 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +020080 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 +020081 return 1;
82 }
83 return 0;
84}
85
William Lallemand3f128872019-04-01 11:29:59 +020086/*
87 * Return the number of new and old children (including workers and external
88 * processes)
89 */
90int mworker_child_nb()
91{
92 struct mworker_proc *child;
93 int ret = 0;
94
95 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +020096 if (child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG))
William Lallemand3f128872019-04-01 11:29:59 +020097 ret++;
98 }
99
100 return ret;
101}
102
103
William Lallemande25473c2019-04-01 11:29:56 +0200104/*
William Lallemand48dfbbd2019-04-01 11:29:53 +0200105 * serialize the proc list and put it in the environment
106 */
107void mworker_proc_list_to_env()
108{
109 char *msg = NULL;
110 struct mworker_proc *child;
111
112 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200113 char type = '?';
114
115 if (child->options & PROC_O_TYPE_MASTER)
116 type = 'm';
117 else if (child->options & PROC_O_TYPE_PROG)
118 type = 'e';
119 else if (child->options &= PROC_O_TYPE_WORKER)
120 type = 'w';
121
William Lallemand48dfbbd2019-04-01 11:29:53 +0200122 if (child->pid > -1)
William Lallemand1dc69632019-06-12 19:11:33 +0200123 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 +0200124 }
125 if (msg)
126 setenv("HAPROXY_PROCESSES", msg, 1);
127}
128
129/*
130 * unserialize the proc list from the environment
131 */
Remi Tricot-Le Breton80dc1452021-05-19 10:45:12 +0200132int mworker_env_to_proc_list()
William Lallemand48dfbbd2019-04-01 11:29:53 +0200133{
134 char *msg, *token = NULL, *s1;
135
136 msg = getenv("HAPROXY_PROCESSES");
137 if (!msg)
Remi Tricot-Le Breton80dc1452021-05-19 10:45:12 +0200138 return 0;
William Lallemand48dfbbd2019-04-01 11:29:53 +0200139
140 while ((token = strtok_r(msg, "|", &s1))) {
141 struct mworker_proc *child;
142 char *subtoken = NULL;
143 char *s2;
144
145 msg = NULL;
146
147 child = calloc(1, sizeof(*child));
Remi Tricot-Le Breton80dc1452021-05-19 10:45:12 +0200148 if (!child) {
149 ha_alert("Out of memory while trying to allocate a worker process structure.");
150 return -1;
151 }
William Lallemand48dfbbd2019-04-01 11:29:53 +0200152
153 while ((subtoken = strtok_r(token, ";", &s2))) {
154
155 token = NULL;
156
157 if (strncmp(subtoken, "type=", 5) == 0) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200158 char type;
159
160 type = *(subtoken+5);
161 if (type == 'm') { /* we are in the master, assign it */
William Lallemand48dfbbd2019-04-01 11:29:53 +0200162 proc_self = child;
William Lallemand8f7069a2019-04-12 16:09:23 +0200163 child->options |= PROC_O_TYPE_MASTER;
164 } else if (type == 'e') {
165 child->options |= PROC_O_TYPE_PROG;
166 } else if (type == 'w') {
167 child->options |= PROC_O_TYPE_WORKER;
168 }
169
William Lallemand48dfbbd2019-04-01 11:29:53 +0200170 } else if (strncmp(subtoken, "fd=", 3) == 0) {
171 child->ipc_fd[0] = atoi(subtoken+3);
172 } else if (strncmp(subtoken, "pid=", 4) == 0) {
173 child->pid = atoi(subtoken+4);
174 } else if (strncmp(subtoken, "rpid=", 5) == 0) {
175 child->relative_pid = atoi(subtoken+5);
176 } else if (strncmp(subtoken, "reloads=", 8) == 0) {
177 /* we reloaded this process once more */
178 child->reloads = atoi(subtoken+8) + 1;
179 } else if (strncmp(subtoken, "timestamp=", 10) == 0) {
180 child->timestamp = atoi(subtoken+10);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200181 } else if (strncmp(subtoken, "id=", 3) == 0) {
182 child->id = strdup(subtoken+3);
William Lallemand1dc69632019-06-12 19:11:33 +0200183 } else if (strncmp(subtoken, "version=", 8) == 0) {
184 child->version = strdup(subtoken+8);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200185 }
186 }
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200187 if (child->pid) {
William Lallemand920fc8b2019-05-14 11:15:18 +0200188 /* this is a process inherited from a reload that should be leaving */
189 child->options |= PROC_O_LEAVING;
190
William Lallemand48dfbbd2019-04-01 11:29:53 +0200191 LIST_ADDQ(&proc_list, &child->list);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200192 } else {
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200193 mworker_free_child(child);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200194 }
William Lallemand48dfbbd2019-04-01 11:29:53 +0200195 }
196
197 unsetenv("HAPROXY_PROCESSES");
Remi Tricot-Le Breton80dc1452021-05-19 10:45:12 +0200198
199 return 0;
William Lallemand48dfbbd2019-04-01 11:29:53 +0200200}
William Lallemand3cd95d22019-04-01 11:29:54 +0200201
202/* Signal blocking and unblocking */
203
204void mworker_block_signals()
205{
206 sigset_t set;
207
208 sigemptyset(&set);
209 sigaddset(&set, SIGUSR1);
210 sigaddset(&set, SIGUSR2);
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100211 sigaddset(&set, SIGTTIN);
212 sigaddset(&set, SIGTTOU);
William Lallemand3cd95d22019-04-01 11:29:54 +0200213 sigaddset(&set, SIGHUP);
214 sigaddset(&set, SIGCHLD);
215 ha_sigmask(SIG_SETMASK, &set, NULL);
216}
217
218void mworker_unblock_signals()
219{
220 haproxy_unblock_signals();
221}
William Lallemand3fa724d2019-04-01 11:29:55 +0200222
William Lallemande25473c2019-04-01 11:29:56 +0200223/* ----- mworker signal handlers ----- */
224
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100225/* broadcast the configured signal to the workers */
226void mworker_broadcast_signal(struct sig_handler *sh)
227{
228 mworker_kill(sh->arg);
229}
230
William Lallemande25473c2019-04-01 11:29:56 +0200231/*
232 * When called, this function reexec haproxy with -sf followed by current
233 * children PIDs and possibly old children PIDs if they didn't leave yet.
234 */
235void mworker_catch_sighup(struct sig_handler *sh)
236{
237 mworker_reload();
238}
239
240void mworker_catch_sigterm(struct sig_handler *sh)
241{
242 int sig = sh->arg;
243
244#if defined(USE_SYSTEMD)
245 if (global.tune.options & GTUNE_USE_SYSTEMD) {
246 sd_notify(0, "STOPPING=1");
247 }
248#endif
249 ha_warning("Exiting Master process...\n");
250 mworker_kill(sig);
251}
252
253/*
254 * Wait for every children to exit
255 */
256
257void mworker_catch_sigchld(struct sig_handler *sh)
258{
259 int exitpid = -1;
260 int status = 0;
William Lallemande25473c2019-04-01 11:29:56 +0200261 int childfound;
262
263restart_wait:
264
265 childfound = 0;
266
267 exitpid = waitpid(-1, &status, WNOHANG);
268 if (exitpid > 0) {
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200269 struct mworker_proc *child, *it;
270
William Lallemande25473c2019-04-01 11:29:56 +0200271 if (WIFEXITED(status))
272 status = WEXITSTATUS(status);
273 else if (WIFSIGNALED(status))
274 status = 128 + WTERMSIG(status);
275 else if (WIFSTOPPED(status))
276 status = 128 + WSTOPSIG(status);
277 else
278 status = 255;
279
William Lallemand3f128872019-04-01 11:29:59 +0200280 /* delete the child from the process list */
William Lallemande25473c2019-04-01 11:29:56 +0200281 list_for_each_entry_safe(child, it, &proc_list, list) {
282 if (child->pid != exitpid)
283 continue;
284
285 LIST_DEL(&child->list);
286 close(child->ipc_fd[0]);
287 childfound = 1;
288 break;
289 }
290
William Lallemand3f128872019-04-01 11:29:59 +0200291 if (!childfound) {
292 /* 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 +0200293 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 +0200294 } else {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200295 /* check if exited child is a current child */
William Lallemand45286112019-04-12 16:09:21 +0200296 if (!(child->options & PROC_O_LEAVING)) {
William Lallemanda655ba42020-05-06 17:27:03 +0200297 if (child->options & PROC_O_TYPE_WORKER) {
298 if (status < 128)
299 ha_warning("Current worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, "Exit");
300 else
301 ha_alert("Current worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, strsignal(status - 128));
302 }
William Lallemand8f7069a2019-04-12 16:09:23 +0200303 else if (child->options & PROC_O_TYPE_PROG)
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200304 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 +0200305
William Lallemande25473c2019-04-01 11:29:56 +0200306 if (status != 0 && status != 130 && status != 143
307 && !(global.tune.options & GTUNE_NOEXIT_ONFAILURE)) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200308 ha_alert("exit-on-failure: killing every processes with SIGTERM\n");
William Lallemande25473c2019-04-01 11:29:56 +0200309 mworker_kill(SIGTERM);
310 }
William Lallemand74f0ec32019-04-16 17:42:44 +0200311 /* 0 & SIGTERM (143) are normal, but we should report SIGINT (130) and other signals */
312 if (exitcode < 0 && status != 0 && status != 143)
313 exitcode = status;
William Lallemande25473c2019-04-01 11:29:56 +0200314 } else {
William Lallemand8f7069a2019-04-12 16:09:23 +0200315 if (child->options & PROC_O_TYPE_WORKER) {
William Lallemand3f128872019-04-01 11:29:59 +0200316 ha_warning("Former worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
317 delete_oldpid(exitpid);
William Lallemand8f7069a2019-04-12 16:09:23 +0200318 } else if (child->options & PROC_O_TYPE_PROG) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200319 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 +0200320 }
William Lallemande25473c2019-04-01 11:29:56 +0200321 }
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200322 mworker_free_child(child);
323 child = NULL;
William Lallemande25473c2019-04-01 11:29:56 +0200324 }
325
326 /* do it again to check if it was the last worker */
327 goto restart_wait;
328 }
329 /* Better rely on the system than on a list of process to check if it was the last one */
330 else if (exitpid == -1 && errno == ECHILD) {
William Lallemand4cf4b332019-04-16 17:42:43 +0200331 ha_warning("All workers exited. Exiting... (%d)\n", (exitcode > 0) ? exitcode : EXIT_SUCCESS);
William Lallemande25473c2019-04-01 11:29:56 +0200332 atexit_flag = 0;
333 if (exitcode > 0)
William Lallemand4cf4b332019-04-16 17:42:43 +0200334 exit(exitcode); /* parent must leave using the status code that provoked the exit */
335 exit(EXIT_SUCCESS);
William Lallemande25473c2019-04-01 11:29:56 +0200336 }
337
338}
339
William Lallemand3fa724d2019-04-01 11:29:55 +0200340/* ----- IPC FD (sockpair) related ----- */
341
342/* This wrapper is called from the workers. It is registered instead of the
343 * normal listener_accept() so the worker can exit() when it detects that the
344 * master closed the IPC FD. If it's not a close, we just call the regular
Willy Tarreaua74cb382020-10-15 21:29:49 +0200345 * listener_accept() function.
346 */
William Lallemand3fa724d2019-04-01 11:29:55 +0200347void mworker_accept_wrapper(int fd)
348{
349 char c;
350 int ret;
351
352 while (1) {
353 ret = recv(fd, &c, 1, MSG_PEEK);
354 if (ret == -1) {
355 if (errno == EINTR)
356 continue;
357 if (errno == EAGAIN) {
358 fd_cant_recv(fd);
359 return;
360 }
361 break;
362 } else if (ret > 0) {
Willy Tarreaua74cb382020-10-15 21:29:49 +0200363 struct listener *l = fdtab[fd].owner;
364
365 if (l)
366 listener_accept(l);
William Lallemand3fa724d2019-04-01 11:29:55 +0200367 return;
368 } else if (ret == 0) {
369 /* At this step the master is down before
370 * this worker perform a 'normal' exit.
371 * So we want to exit with an error but
372 * other threads could currently process
373 * some stuff so we can't perform a clean
374 * deinit().
375 */
376 exit(EXIT_FAILURE);
377 }
378 }
379 return;
380}
381
382/*
Willy Tarreau619a95f2019-05-20 11:12:15 +0200383 * This function registers the accept wrapper for the sockpair of the master
384 * worker. It's only handled by worker thread #0. Other threads and master do
385 * nothing here. It always returns 1 (success).
William Lallemand3fa724d2019-04-01 11:29:55 +0200386 */
Willy Tarreau619a95f2019-05-20 11:12:15 +0200387static int mworker_pipe_register_per_thread()
William Lallemand3fa724d2019-04-01 11:29:55 +0200388{
Willy Tarreau619a95f2019-05-20 11:12:15 +0200389 if (!(global.mode & MODE_MWORKER) || master)
390 return 1;
391
392 if (tid != 0)
393 return 1;
William Lallemand3fa724d2019-04-01 11:29:55 +0200394
395 fcntl(proc_self->ipc_fd[1], F_SETFL, O_NONBLOCK);
396 /* In multi-tread, we need only one thread to process
397 * events on the pipe with master
398 */
Willy Tarreau619a95f2019-05-20 11:12:15 +0200399 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 +0200400 fd_want_recv(proc_self->ipc_fd[1]);
Willy Tarreau619a95f2019-05-20 11:12:15 +0200401 return 1;
William Lallemand3fa724d2019-04-01 11:29:55 +0200402}
William Lallemand9001ce82019-04-01 11:29:57 +0200403
Willy Tarreau619a95f2019-05-20 11:12:15 +0200404REGISTER_PER_THREAD_INIT(mworker_pipe_register_per_thread);
405
William Lallemand9001ce82019-04-01 11:29:57 +0200406/* ----- proxies ----- */
407/*
408 * Upon a reload, the master worker needs to close all listeners FDs but the mworker_pipe
409 * fd, and the FD provided by fd@
410 */
411void mworker_cleanlisteners()
412{
413 struct listener *l, *l_next;
414 struct proxy *curproxy;
415 struct peers *curpeers;
416
417 /* we might have to unbind some peers sections from some processes */
418 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
419 if (!curpeers->peers_fe)
420 continue;
421
422 stop_proxy(curpeers->peers_fe);
423 /* disable this peer section so that it kills itself */
424 signal_unregister_handler(curpeers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200425 task_destroy(curpeers->sync_task);
William Lallemand9001ce82019-04-01 11:29:57 +0200426 curpeers->sync_task = NULL;
Olivier Houchard3f795f72019-04-17 22:51:06 +0200427 task_destroy(curpeers->peers_fe->task);
William Lallemand9001ce82019-04-01 11:29:57 +0200428 curpeers->peers_fe->task = NULL;
429 curpeers->peers_fe = NULL;
430 }
431
432 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
433 int listen_in_master = 0;
434
435 list_for_each_entry_safe(l, l_next, &curproxy->conf.listeners, by_fe) {
436 /* remove the listener, but not those we need in the master... */
Willy Tarreau18c20d22020-10-09 16:11:46 +0200437 if (!(l->rx.flags & RX_F_MWORKER)) {
Willy Tarreau75c98d12020-10-09 15:55:23 +0200438 unbind_listener(l);
William Lallemand9001ce82019-04-01 11:29:57 +0200439 delete_listener(l);
440 } else {
441 listen_in_master = 1;
442 }
443 }
444 /* if the proxy shouldn't be in the master, we stop it */
445 if (!listen_in_master)
Willy Tarreauc3914d42020-09-24 08:39:22 +0200446 curproxy->disabled = 1;
William Lallemand9001ce82019-04-01 11:29:57 +0200447 }
William Lallemand88dc7c52019-04-01 11:30:01 +0200448}
449
450/* Displays workers and processes */
451static int cli_io_handler_show_proc(struct appctx *appctx)
452{
453 struct stream_interface *si = appctx->owner;
454 struct mworker_proc *child;
455 int old = 0;
456 int up = now.tv_sec - proc_self->timestamp;
William Lallemande8669fc2019-06-12 18:21:17 +0200457 char *uptime = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200458
459 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
460 return 1;
461
462 chunk_reset(&trash);
463
William Lallemand1dc69632019-06-12 19:11:33 +0200464 chunk_printf(&trash, "#%-14s %-15s %-15s %-15s %-15s %-15s\n", "<PID>", "<type>", "<relative PID>", "<reloads>", "<uptime>", "<version>");
William Lallemande8669fc2019-06-12 18:21:17 +0200465 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
Willy Tarreau76a80c72019-06-22 07:41:38 +0200466 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 +0200467 free(uptime);
468 uptime = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200469
470 /* displays current processes */
471
472 chunk_appendf(&trash, "# workers\n");
473 list_for_each_entry(child, &proc_list, list) {
474 up = now.tv_sec - child->timestamp;
475
William Lallemand8f7069a2019-04-12 16:09:23 +0200476 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200477 continue;
478
William Lallemand45286112019-04-12 16:09:21 +0200479 if (child->options & PROC_O_LEAVING) {
William Lallemand88dc7c52019-04-01 11:30:01 +0200480 old++;
481 continue;
482 }
William Lallemande8669fc2019-06-12 18:21:17 +0200483 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200484 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 +0200485 free(uptime);
486 uptime = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200487 }
488
489 /* displays old processes */
490
491 if (old) {
492 char *msg = NULL;
493
494 chunk_appendf(&trash, "# old workers\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_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200499 continue;
500
William Lallemand45286112019-04-12 16:09:21 +0200501 if (child->options & PROC_O_LEAVING) {
William Lallemand88dc7c52019-04-01 11:30:01 +0200502 memprintf(&msg, "[was: %u]", child->relative_pid);
William Lallemande8669fc2019-06-12 18:21:17 +0200503 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200504 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 +0200505 free(uptime);
506 uptime = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200507 }
508 }
509 free(msg);
510 }
511
William Lallemandad53d6d2019-04-01 11:30:03 +0200512 /* displays external process */
513 chunk_appendf(&trash, "# programs\n");
514 old = 0;
515 list_for_each_entry(child, &proc_list, list) {
516 up = now.tv_sec - child->timestamp;
517
William Lallemand8f7069a2019-04-12 16:09:23 +0200518 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200519 continue;
520
William Lallemand45286112019-04-12 16:09:21 +0200521 if (child->options & PROC_O_LEAVING) {
William Lallemandad53d6d2019-04-01 11:30:03 +0200522 old++;
523 continue;
524 }
William Lallemande8669fc2019-06-12 18:21:17 +0200525 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200526 chunk_appendf(&trash, "%-15u %-15s %-15s %-15d %-15s %-15s\n", child->pid, child->id, "-", child->reloads, uptime, "-");
William Lallemande8669fc2019-06-12 18:21:17 +0200527 free(uptime);
528 uptime = NULL;
William Lallemandad53d6d2019-04-01 11:30:03 +0200529 }
530
531 if (old) {
532 chunk_appendf(&trash, "# old programs\n");
533 list_for_each_entry(child, &proc_list, list) {
534 up = now.tv_sec - child->timestamp;
535
William Lallemand8f7069a2019-04-12 16:09:23 +0200536 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200537 continue;
538
William Lallemand45286112019-04-12 16:09:21 +0200539 if (child->options & PROC_O_LEAVING) {
William Lallemande8669fc2019-06-12 18:21:17 +0200540 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200541 chunk_appendf(&trash, "%-15u %-15s %-15s %-15d %-15s %-15s\n", child->pid, child->id, "-", child->reloads, uptime, "-");
William Lallemande8669fc2019-06-12 18:21:17 +0200542 free(uptime);
543 uptime = NULL;
William Lallemandad53d6d2019-04-01 11:30:03 +0200544 }
545 }
546 }
547
548
549
William Lallemand88dc7c52019-04-01 11:30:01 +0200550 if (ci_putchk(si_ic(si), &trash) == -1) {
551 si_rx_room_blk(si);
552 return 0;
553 }
554
555 /* dump complete */
556 return 1;
William Lallemand9001ce82019-04-01 11:29:57 +0200557}
William Lallemand88dc7c52019-04-01 11:30:01 +0200558
559/* reload the master process */
560static int cli_parse_reload(char **args, char *payload, struct appctx *appctx, void *private)
561{
562 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
563 return 1;
564
565 mworker_reload();
566
567 return 1;
568}
569
570
William Lallemand27edc4b2019-05-07 17:49:33 +0200571static int mworker_parse_global_max_reloads(char **args, int section_type, struct proxy *curpx,
572 struct proxy *defpx, const char *file, int linenum, char **err)
573{
574
575 int err_code = 0;
576
577 if (alertif_too_many_args(1, file, linenum, args, &err_code))
578 goto out;
579
580 if (*(args[1]) == 0) {
581 memprintf(err, "%sparsing [%s:%d] : '%s' expects an integer argument.\n", *err, file, linenum, args[0]);
582 err_code |= ERR_ALERT | ERR_FATAL;
583 goto out;
584 }
585
586 max_reloads = atol(args[1]);
587 if (max_reloads < 0) {
588 memprintf(err, "%sparsing [%s:%d] '%s' : invalid value %d, must be >= 0", *err, file, linenum, args[0], max_reloads);
589 err_code |= ERR_ALERT | ERR_FATAL;
590 goto out;
591 }
592
593out:
594 return err_code;
595}
596
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200597void mworker_free_child(struct mworker_proc *child)
598{
599 if (child == NULL)
600 return;
601
602 if (child->command) {
603 int i;
604
605 for (i = 0; child->command[i]; i++) {
606 if (child->command[i]) {
607 free(child->command[i]);
608 child->command[i] = NULL;
609 }
William Lallemande8669fc2019-06-12 18:21:17 +0200610
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200611 }
612 free(child->command);
613 child->command = NULL;
614 }
615 if (child->id) {
616 free(child->id);
617 child->id = NULL;
618 }
William Lallemand1dc69632019-06-12 19:11:33 +0200619 if (child->version) {
620 free(child->version);
621 child->version = NULL;
622 }
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200623 free(child);
624}
William Lallemand27edc4b2019-05-07 17:49:33 +0200625
626static struct cfg_kw_list mworker_kws = {{ }, {
627 { CFG_GLOBAL, "mworker-max-reloads", mworker_parse_global_max_reloads },
628 { 0, NULL, NULL },
629}};
630
631INITCALL1(STG_REGISTER, cfg_register_keywords, &mworker_kws);
632
633
William Lallemand88dc7c52019-04-01 11:30:01 +0200634/* register cli keywords */
635static struct cli_kw_list cli_kws = {{ },{
636 { { "@<relative pid>", NULL }, "@<relative pid> : send a command to the <relative pid> process", NULL, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
637 { { "@!<pid>", NULL }, "@!<pid> : send a command to the <pid> process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
638 { { "@master", NULL }, "@master : send a command to the master process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
639 { { "show", "proc", NULL }, "show proc : show processes status", cli_parse_default, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
640 { { "reload", NULL }, "reload : reload haproxy", cli_parse_reload, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
641 {{},}
642}};
643
644INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);