blob: d45b35795236a81d646113ec896bb5542c7077f6 [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 Tarreaub2551052020-06-09 09:07:15 +020020#if defined(USE_SYSTEMD)
21#include <systemd/sd-daemon.h>
22#endif
23
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020024#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020025#include <haproxy/cfgparse.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020026#include <haproxy/cli.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020027#include <haproxy/errors.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020028#include <haproxy/fd.h>
29#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020030#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020031#include <haproxy/listener.h>
Willy Tarreaub5abe5b2020-06-04 14:07:37 +020032#include <haproxy/mworker.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +020033#include <haproxy/peers.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020034#include <haproxy/proxy-t.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +020035#include <haproxy/signal.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020036#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020037#include <haproxy/stream_interface.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020038#include <haproxy/version.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +020039
William Lallemand48dfbbd2019-04-01 11:29:53 +020040
William Lallemande25473c2019-04-01 11:29:56 +020041static int exitcode = -1;
William Lallemand27edc4b2019-05-07 17:49:33 +020042static int max_reloads = -1; /* number max of reloads a worker can have until they are killed */
William Lallemande25473c2019-04-01 11:29:56 +020043
William Lallemande25473c2019-04-01 11:29:56 +020044/* ----- children processes handling ----- */
William Lallemand48dfbbd2019-04-01 11:29:53 +020045
William Lallemand48dfbbd2019-04-01 11:29:53 +020046/*
William Lallemande25473c2019-04-01 11:29:56 +020047 * Send signal to every known children.
48 */
49
50static void mworker_kill(int sig)
51{
William Lallemand3f128872019-04-01 11:29:59 +020052 struct mworker_proc *child;
William Lallemande25473c2019-04-01 11:29:56 +020053
William Lallemand3f128872019-04-01 11:29:59 +020054 list_for_each_entry(child, &proc_list, list) {
55 /* 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 +020056 if ((child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG)) && (child->pid > 0))
William Lallemand3f128872019-04-01 11:29:59 +020057 kill(child->pid, sig);
William Lallemande25473c2019-04-01 11:29:56 +020058 }
59}
60
William Lallemand27edc4b2019-05-07 17:49:33 +020061void mworker_kill_max_reloads(int sig)
62{
63 struct mworker_proc *child;
64
65 list_for_each_entry(child, &proc_list, list) {
66 if (max_reloads != -1 && (child->options & PROC_O_TYPE_WORKER) &&
67 (child->pid > 0) && (child->reloads > max_reloads))
68 kill(child->pid, sig);
69 }
70}
William Lallemande25473c2019-04-01 11:29:56 +020071
72/* return 1 if a pid is a current child otherwise 0 */
William Lallemand3f128872019-04-01 11:29:59 +020073int mworker_current_child(int pid)
William Lallemande25473c2019-04-01 11:29:56 +020074{
William Lallemand3f128872019-04-01 11:29:59 +020075 struct mworker_proc *child;
William Lallemande25473c2019-04-01 11:29:56 +020076
William Lallemand3f128872019-04-01 11:29:59 +020077 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +020078 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 +020079 return 1;
80 }
81 return 0;
82}
83
William Lallemand3f128872019-04-01 11:29:59 +020084/*
85 * Return the number of new and old children (including workers and external
86 * processes)
87 */
88int mworker_child_nb()
89{
90 struct mworker_proc *child;
91 int ret = 0;
92
93 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +020094 if (child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG))
William Lallemand3f128872019-04-01 11:29:59 +020095 ret++;
96 }
97
98 return ret;
99}
100
101
William Lallemande25473c2019-04-01 11:29:56 +0200102/*
William Lallemand48dfbbd2019-04-01 11:29:53 +0200103 * serialize the proc list and put it in the environment
104 */
105void mworker_proc_list_to_env()
106{
107 char *msg = NULL;
108 struct mworker_proc *child;
109
110 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200111 char type = '?';
112
113 if (child->options & PROC_O_TYPE_MASTER)
114 type = 'm';
115 else if (child->options & PROC_O_TYPE_PROG)
116 type = 'e';
117 else if (child->options &= PROC_O_TYPE_WORKER)
118 type = 'w';
119
William Lallemand48dfbbd2019-04-01 11:29:53 +0200120 if (child->pid > -1)
William Lallemand1dc69632019-06-12 19:11:33 +0200121 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 +0200122 }
123 if (msg)
124 setenv("HAPROXY_PROCESSES", msg, 1);
125}
126
127/*
128 * unserialize the proc list from the environment
129 */
130void mworker_env_to_proc_list()
131{
132 char *msg, *token = NULL, *s1;
133
134 msg = getenv("HAPROXY_PROCESSES");
135 if (!msg)
136 return;
137
138 while ((token = strtok_r(msg, "|", &s1))) {
139 struct mworker_proc *child;
140 char *subtoken = NULL;
141 char *s2;
142
143 msg = NULL;
144
145 child = calloc(1, sizeof(*child));
146
147 while ((subtoken = strtok_r(token, ";", &s2))) {
148
149 token = NULL;
150
151 if (strncmp(subtoken, "type=", 5) == 0) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200152 char type;
153
154 type = *(subtoken+5);
155 if (type == 'm') { /* we are in the master, assign it */
William Lallemand48dfbbd2019-04-01 11:29:53 +0200156 proc_self = child;
William Lallemand8f7069a2019-04-12 16:09:23 +0200157 child->options |= PROC_O_TYPE_MASTER;
158 } else if (type == 'e') {
159 child->options |= PROC_O_TYPE_PROG;
160 } else if (type == 'w') {
161 child->options |= PROC_O_TYPE_WORKER;
162 }
163
William Lallemand48dfbbd2019-04-01 11:29:53 +0200164 } else if (strncmp(subtoken, "fd=", 3) == 0) {
165 child->ipc_fd[0] = atoi(subtoken+3);
166 } else if (strncmp(subtoken, "pid=", 4) == 0) {
167 child->pid = atoi(subtoken+4);
168 } else if (strncmp(subtoken, "rpid=", 5) == 0) {
169 child->relative_pid = atoi(subtoken+5);
170 } else if (strncmp(subtoken, "reloads=", 8) == 0) {
171 /* we reloaded this process once more */
172 child->reloads = atoi(subtoken+8) + 1;
173 } else if (strncmp(subtoken, "timestamp=", 10) == 0) {
174 child->timestamp = atoi(subtoken+10);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200175 } else if (strncmp(subtoken, "id=", 3) == 0) {
176 child->id = strdup(subtoken+3);
William Lallemand1dc69632019-06-12 19:11:33 +0200177 } else if (strncmp(subtoken, "version=", 8) == 0) {
178 child->version = strdup(subtoken+8);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200179 }
180 }
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200181 if (child->pid) {
William Lallemand920fc8b2019-05-14 11:15:18 +0200182 /* this is a process inherited from a reload that should be leaving */
183 child->options |= PROC_O_LEAVING;
184
William Lallemand48dfbbd2019-04-01 11:29:53 +0200185 LIST_ADDQ(&proc_list, &child->list);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200186 } else {
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200187 mworker_free_child(child);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200188 }
William Lallemand48dfbbd2019-04-01 11:29:53 +0200189 }
190
191 unsetenv("HAPROXY_PROCESSES");
192}
William Lallemand3cd95d22019-04-01 11:29:54 +0200193
194/* Signal blocking and unblocking */
195
196void mworker_block_signals()
197{
198 sigset_t set;
199
200 sigemptyset(&set);
201 sigaddset(&set, SIGUSR1);
202 sigaddset(&set, SIGUSR2);
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100203 sigaddset(&set, SIGTTIN);
204 sigaddset(&set, SIGTTOU);
William Lallemand3cd95d22019-04-01 11:29:54 +0200205 sigaddset(&set, SIGHUP);
206 sigaddset(&set, SIGCHLD);
207 ha_sigmask(SIG_SETMASK, &set, NULL);
208}
209
210void mworker_unblock_signals()
211{
212 haproxy_unblock_signals();
213}
William Lallemand3fa724d2019-04-01 11:29:55 +0200214
William Lallemande25473c2019-04-01 11:29:56 +0200215/* ----- mworker signal handlers ----- */
216
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100217/* broadcast the configured signal to the workers */
218void mworker_broadcast_signal(struct sig_handler *sh)
219{
220 mworker_kill(sh->arg);
221}
222
William Lallemande25473c2019-04-01 11:29:56 +0200223/*
224 * When called, this function reexec haproxy with -sf followed by current
225 * children PIDs and possibly old children PIDs if they didn't leave yet.
226 */
227void mworker_catch_sighup(struct sig_handler *sh)
228{
229 mworker_reload();
230}
231
232void mworker_catch_sigterm(struct sig_handler *sh)
233{
234 int sig = sh->arg;
235
236#if defined(USE_SYSTEMD)
237 if (global.tune.options & GTUNE_USE_SYSTEMD) {
238 sd_notify(0, "STOPPING=1");
239 }
240#endif
241 ha_warning("Exiting Master process...\n");
242 mworker_kill(sig);
243}
244
245/*
246 * Wait for every children to exit
247 */
248
249void mworker_catch_sigchld(struct sig_handler *sh)
250{
251 int exitpid = -1;
252 int status = 0;
William Lallemande25473c2019-04-01 11:29:56 +0200253 int childfound;
254
255restart_wait:
256
257 childfound = 0;
258
259 exitpid = waitpid(-1, &status, WNOHANG);
260 if (exitpid > 0) {
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200261 struct mworker_proc *child, *it;
262
William Lallemande25473c2019-04-01 11:29:56 +0200263 if (WIFEXITED(status))
264 status = WEXITSTATUS(status);
265 else if (WIFSIGNALED(status))
266 status = 128 + WTERMSIG(status);
267 else if (WIFSTOPPED(status))
268 status = 128 + WSTOPSIG(status);
269 else
270 status = 255;
271
William Lallemand3f128872019-04-01 11:29:59 +0200272 /* delete the child from the process list */
William Lallemande25473c2019-04-01 11:29:56 +0200273 list_for_each_entry_safe(child, it, &proc_list, list) {
274 if (child->pid != exitpid)
275 continue;
276
277 LIST_DEL(&child->list);
278 close(child->ipc_fd[0]);
279 childfound = 1;
280 break;
281 }
282
William Lallemand3f128872019-04-01 11:29:59 +0200283 if (!childfound) {
284 /* 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 +0200285 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 +0200286 } else {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200287 /* check if exited child is a current child */
William Lallemand45286112019-04-12 16:09:21 +0200288 if (!(child->options & PROC_O_LEAVING)) {
William Lallemanda655ba42020-05-06 17:27:03 +0200289 if (child->options & PROC_O_TYPE_WORKER) {
290 if (status < 128)
291 ha_warning("Current worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, "Exit");
292 else
293 ha_alert("Current worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, strsignal(status - 128));
294 }
William Lallemand8f7069a2019-04-12 16:09:23 +0200295 else if (child->options & PROC_O_TYPE_PROG)
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200296 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 +0200297
William Lallemande25473c2019-04-01 11:29:56 +0200298 if (status != 0 && status != 130 && status != 143
299 && !(global.tune.options & GTUNE_NOEXIT_ONFAILURE)) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200300 ha_alert("exit-on-failure: killing every processes with SIGTERM\n");
William Lallemande25473c2019-04-01 11:29:56 +0200301 mworker_kill(SIGTERM);
302 }
William Lallemand74f0ec32019-04-16 17:42:44 +0200303 /* 0 & SIGTERM (143) are normal, but we should report SIGINT (130) and other signals */
304 if (exitcode < 0 && status != 0 && status != 143)
305 exitcode = status;
William Lallemande25473c2019-04-01 11:29:56 +0200306 } else {
William Lallemand8f7069a2019-04-12 16:09:23 +0200307 if (child->options & PROC_O_TYPE_WORKER) {
William Lallemand3f128872019-04-01 11:29:59 +0200308 ha_warning("Former worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
309 delete_oldpid(exitpid);
William Lallemand8f7069a2019-04-12 16:09:23 +0200310 } else if (child->options & PROC_O_TYPE_PROG) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200311 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 +0200312 }
William Lallemande25473c2019-04-01 11:29:56 +0200313 }
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200314 mworker_free_child(child);
315 child = NULL;
William Lallemande25473c2019-04-01 11:29:56 +0200316 }
317
318 /* do it again to check if it was the last worker */
319 goto restart_wait;
320 }
321 /* Better rely on the system than on a list of process to check if it was the last one */
322 else if (exitpid == -1 && errno == ECHILD) {
William Lallemand4cf4b332019-04-16 17:42:43 +0200323 ha_warning("All workers exited. Exiting... (%d)\n", (exitcode > 0) ? exitcode : EXIT_SUCCESS);
William Lallemande25473c2019-04-01 11:29:56 +0200324 atexit_flag = 0;
325 if (exitcode > 0)
William Lallemand4cf4b332019-04-16 17:42:43 +0200326 exit(exitcode); /* parent must leave using the status code that provoked the exit */
327 exit(EXIT_SUCCESS);
William Lallemande25473c2019-04-01 11:29:56 +0200328 }
329
330}
331
William Lallemand3fa724d2019-04-01 11:29:55 +0200332/* ----- IPC FD (sockpair) related ----- */
333
334/* This wrapper is called from the workers. It is registered instead of the
335 * normal listener_accept() so the worker can exit() when it detects that the
336 * master closed the IPC FD. If it's not a close, we just call the regular
337 * listener_accept() function */
338void mworker_accept_wrapper(int fd)
339{
340 char c;
341 int ret;
342
343 while (1) {
344 ret = recv(fd, &c, 1, MSG_PEEK);
345 if (ret == -1) {
346 if (errno == EINTR)
347 continue;
348 if (errno == EAGAIN) {
349 fd_cant_recv(fd);
350 return;
351 }
352 break;
353 } else if (ret > 0) {
354 listener_accept(fd);
355 return;
356 } else if (ret == 0) {
357 /* At this step the master is down before
358 * this worker perform a 'normal' exit.
359 * So we want to exit with an error but
360 * other threads could currently process
361 * some stuff so we can't perform a clean
362 * deinit().
363 */
364 exit(EXIT_FAILURE);
365 }
366 }
367 return;
368}
369
370/*
Willy Tarreau619a95f2019-05-20 11:12:15 +0200371 * This function registers the accept wrapper for the sockpair of the master
372 * worker. It's only handled by worker thread #0. Other threads and master do
373 * nothing here. It always returns 1 (success).
William Lallemand3fa724d2019-04-01 11:29:55 +0200374 */
Willy Tarreau619a95f2019-05-20 11:12:15 +0200375static int mworker_pipe_register_per_thread()
William Lallemand3fa724d2019-04-01 11:29:55 +0200376{
Willy Tarreau619a95f2019-05-20 11:12:15 +0200377 if (!(global.mode & MODE_MWORKER) || master)
378 return 1;
379
380 if (tid != 0)
381 return 1;
William Lallemand3fa724d2019-04-01 11:29:55 +0200382
383 fcntl(proc_self->ipc_fd[1], F_SETFL, O_NONBLOCK);
384 /* In multi-tread, we need only one thread to process
385 * events on the pipe with master
386 */
Willy Tarreau619a95f2019-05-20 11:12:15 +0200387 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 +0200388 fd_want_recv(proc_self->ipc_fd[1]);
Willy Tarreau619a95f2019-05-20 11:12:15 +0200389 return 1;
William Lallemand3fa724d2019-04-01 11:29:55 +0200390}
William Lallemand9001ce82019-04-01 11:29:57 +0200391
Willy Tarreau619a95f2019-05-20 11:12:15 +0200392REGISTER_PER_THREAD_INIT(mworker_pipe_register_per_thread);
393
William Lallemand9001ce82019-04-01 11:29:57 +0200394/* ----- proxies ----- */
395/*
396 * Upon a reload, the master worker needs to close all listeners FDs but the mworker_pipe
397 * fd, and the FD provided by fd@
398 */
399void mworker_cleanlisteners()
400{
401 struct listener *l, *l_next;
402 struct proxy *curproxy;
403 struct peers *curpeers;
404
405 /* we might have to unbind some peers sections from some processes */
406 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
407 if (!curpeers->peers_fe)
408 continue;
409
410 stop_proxy(curpeers->peers_fe);
411 /* disable this peer section so that it kills itself */
412 signal_unregister_handler(curpeers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200413 task_destroy(curpeers->sync_task);
William Lallemand9001ce82019-04-01 11:29:57 +0200414 curpeers->sync_task = NULL;
Olivier Houchard3f795f72019-04-17 22:51:06 +0200415 task_destroy(curpeers->peers_fe->task);
William Lallemand9001ce82019-04-01 11:29:57 +0200416 curpeers->peers_fe->task = NULL;
417 curpeers->peers_fe = NULL;
418 }
419
420 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
421 int listen_in_master = 0;
422
423 list_for_each_entry_safe(l, l_next, &curproxy->conf.listeners, by_fe) {
424 /* remove the listener, but not those we need in the master... */
Willy Tarreau18c20d22020-10-09 16:11:46 +0200425 if (!(l->rx.flags & RX_F_MWORKER)) {
Willy Tarreau75c98d12020-10-09 15:55:23 +0200426 unbind_listener(l);
William Lallemand9001ce82019-04-01 11:29:57 +0200427 delete_listener(l);
428 } else {
429 listen_in_master = 1;
430 }
431 }
432 /* if the proxy shouldn't be in the master, we stop it */
433 if (!listen_in_master)
Willy Tarreauc3914d42020-09-24 08:39:22 +0200434 curproxy->disabled = 1;
William Lallemand9001ce82019-04-01 11:29:57 +0200435 }
William Lallemand88dc7c52019-04-01 11:30:01 +0200436}
437
438/* Displays workers and processes */
439static int cli_io_handler_show_proc(struct appctx *appctx)
440{
441 struct stream_interface *si = appctx->owner;
442 struct mworker_proc *child;
443 int old = 0;
444 int up = now.tv_sec - proc_self->timestamp;
William Lallemande8669fc2019-06-12 18:21:17 +0200445 char *uptime = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200446
447 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
448 return 1;
449
450 chunk_reset(&trash);
451
William Lallemand1dc69632019-06-12 19:11:33 +0200452 chunk_printf(&trash, "#%-14s %-15s %-15s %-15s %-15s %-15s\n", "<PID>", "<type>", "<relative PID>", "<reloads>", "<uptime>", "<version>");
William Lallemande8669fc2019-06-12 18:21:17 +0200453 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
Willy Tarreau76a80c72019-06-22 07:41:38 +0200454 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 +0200455 free(uptime);
456 uptime = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200457
458 /* displays current processes */
459
460 chunk_appendf(&trash, "# workers\n");
461 list_for_each_entry(child, &proc_list, list) {
462 up = now.tv_sec - child->timestamp;
463
William Lallemand8f7069a2019-04-12 16:09:23 +0200464 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200465 continue;
466
William Lallemand45286112019-04-12 16:09:21 +0200467 if (child->options & PROC_O_LEAVING) {
William Lallemand88dc7c52019-04-01 11:30:01 +0200468 old++;
469 continue;
470 }
William Lallemande8669fc2019-06-12 18:21:17 +0200471 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200472 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 +0200473 free(uptime);
474 uptime = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200475 }
476
477 /* displays old processes */
478
479 if (old) {
480 char *msg = NULL;
481
482 chunk_appendf(&trash, "# old workers\n");
483 list_for_each_entry(child, &proc_list, list) {
484 up = now.tv_sec - child->timestamp;
485
William Lallemand8f7069a2019-04-12 16:09:23 +0200486 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200487 continue;
488
William Lallemand45286112019-04-12 16:09:21 +0200489 if (child->options & PROC_O_LEAVING) {
William Lallemand88dc7c52019-04-01 11:30:01 +0200490 memprintf(&msg, "[was: %u]", child->relative_pid);
William Lallemande8669fc2019-06-12 18:21:17 +0200491 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200492 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 +0200493 free(uptime);
494 uptime = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200495 }
496 }
497 free(msg);
498 }
499
William Lallemandad53d6d2019-04-01 11:30:03 +0200500 /* displays external process */
501 chunk_appendf(&trash, "# programs\n");
502 old = 0;
503 list_for_each_entry(child, &proc_list, list) {
504 up = now.tv_sec - child->timestamp;
505
William Lallemand8f7069a2019-04-12 16:09:23 +0200506 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200507 continue;
508
William Lallemand45286112019-04-12 16:09:21 +0200509 if (child->options & PROC_O_LEAVING) {
William Lallemandad53d6d2019-04-01 11:30:03 +0200510 old++;
511 continue;
512 }
William Lallemande8669fc2019-06-12 18:21:17 +0200513 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200514 chunk_appendf(&trash, "%-15u %-15s %-15s %-15d %-15s %-15s\n", child->pid, child->id, "-", child->reloads, uptime, "-");
William Lallemande8669fc2019-06-12 18:21:17 +0200515 free(uptime);
516 uptime = NULL;
William Lallemandad53d6d2019-04-01 11:30:03 +0200517 }
518
519 if (old) {
520 chunk_appendf(&trash, "# old programs\n");
521 list_for_each_entry(child, &proc_list, list) {
522 up = now.tv_sec - child->timestamp;
523
William Lallemand8f7069a2019-04-12 16:09:23 +0200524 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200525 continue;
526
William Lallemand45286112019-04-12 16:09:21 +0200527 if (child->options & PROC_O_LEAVING) {
William Lallemande8669fc2019-06-12 18:21:17 +0200528 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200529 chunk_appendf(&trash, "%-15u %-15s %-15s %-15d %-15s %-15s\n", child->pid, child->id, "-", child->reloads, uptime, "-");
William Lallemande8669fc2019-06-12 18:21:17 +0200530 free(uptime);
531 uptime = NULL;
William Lallemandad53d6d2019-04-01 11:30:03 +0200532 }
533 }
534 }
535
536
537
William Lallemand88dc7c52019-04-01 11:30:01 +0200538 if (ci_putchk(si_ic(si), &trash) == -1) {
539 si_rx_room_blk(si);
540 return 0;
541 }
542
543 /* dump complete */
544 return 1;
William Lallemand9001ce82019-04-01 11:29:57 +0200545}
William Lallemand88dc7c52019-04-01 11:30:01 +0200546
547/* reload the master process */
548static int cli_parse_reload(char **args, char *payload, struct appctx *appctx, void *private)
549{
550 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
551 return 1;
552
553 mworker_reload();
554
555 return 1;
556}
557
558
William Lallemand27edc4b2019-05-07 17:49:33 +0200559static int mworker_parse_global_max_reloads(char **args, int section_type, struct proxy *curpx,
560 struct proxy *defpx, const char *file, int linenum, char **err)
561{
562
563 int err_code = 0;
564
565 if (alertif_too_many_args(1, file, linenum, args, &err_code))
566 goto out;
567
568 if (*(args[1]) == 0) {
569 memprintf(err, "%sparsing [%s:%d] : '%s' expects an integer argument.\n", *err, file, linenum, args[0]);
570 err_code |= ERR_ALERT | ERR_FATAL;
571 goto out;
572 }
573
574 max_reloads = atol(args[1]);
575 if (max_reloads < 0) {
576 memprintf(err, "%sparsing [%s:%d] '%s' : invalid value %d, must be >= 0", *err, file, linenum, args[0], max_reloads);
577 err_code |= ERR_ALERT | ERR_FATAL;
578 goto out;
579 }
580
581out:
582 return err_code;
583}
584
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200585void mworker_free_child(struct mworker_proc *child)
586{
587 if (child == NULL)
588 return;
589
590 if (child->command) {
591 int i;
592
593 for (i = 0; child->command[i]; i++) {
594 if (child->command[i]) {
595 free(child->command[i]);
596 child->command[i] = NULL;
597 }
William Lallemande8669fc2019-06-12 18:21:17 +0200598
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200599 }
600 free(child->command);
601 child->command = NULL;
602 }
603 if (child->id) {
604 free(child->id);
605 child->id = NULL;
606 }
William Lallemand1dc69632019-06-12 19:11:33 +0200607 if (child->version) {
608 free(child->version);
609 child->version = NULL;
610 }
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200611 free(child);
612}
William Lallemand27edc4b2019-05-07 17:49:33 +0200613
614static struct cfg_kw_list mworker_kws = {{ }, {
615 { CFG_GLOBAL, "mworker-max-reloads", mworker_parse_global_max_reloads },
616 { 0, NULL, NULL },
617}};
618
619INITCALL1(STG_REGISTER, cfg_register_keywords, &mworker_kws);
620
621
William Lallemand88dc7c52019-04-01 11:30:01 +0200622/* register cli keywords */
623static struct cli_kw_list cli_kws = {{ },{
624 { { "@<relative pid>", NULL }, "@<relative pid> : send a command to the <relative pid> process", NULL, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
625 { { "@!<pid>", NULL }, "@!<pid> : send a command to the <pid> process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
626 { { "@master", NULL }, "@master : send a command to the master process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
627 { { "show", "proc", NULL }, "show proc : show processes status", cli_parse_default, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
628 { { "reload", NULL }, "reload : reload haproxy", cli_parse_reload, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
629 {{},}
630}};
631
632INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);