blob: eb4a2be2818e55039ae5525d33f70919949d5738 [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>
Christopher Faulet908628c2022-03-25 16:43:49 +010029#include <haproxy/conn_stream.h>
30#include <haproxy/cs_utils.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020031#include <haproxy/errors.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020032#include <haproxy/fd.h>
33#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020034#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020035#include <haproxy/listener.h>
Willy Tarreaub5abe5b2020-06-04 14:07:37 +020036#include <haproxy/mworker.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +020037#include <haproxy/peers.h>
Willy Tarreau7c668572021-05-08 20:21:31 +020038#include <haproxy/proxy.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +020039#include <haproxy/signal.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020040#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020041#include <haproxy/stream_interface.h>
Willy Tarreau745e98c2021-05-08 13:58:19 +020042#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020043#include <haproxy/version.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +020044
William Lallemand48dfbbd2019-04-01 11:29:53 +020045
William Lallemande25473c2019-04-01 11:29:56 +020046static int exitcode = -1;
William Lallemand27edc4b2019-05-07 17:49:33 +020047static int max_reloads = -1; /* number max of reloads a worker can have until they are killed */
Willy Tarreau15f9ac32021-05-08 12:30:50 +020048struct mworker_proc *proc_self = NULL; /* process structure of current process */
William Lallemande25473c2019-04-01 11:29:56 +020049
William Lallemande25473c2019-04-01 11:29:56 +020050/* ----- children processes handling ----- */
William Lallemand48dfbbd2019-04-01 11:29:53 +020051
William Lallemand48dfbbd2019-04-01 11:29:53 +020052/*
William Lallemande25473c2019-04-01 11:29:56 +020053 * Send signal to every known children.
54 */
55
56static void mworker_kill(int sig)
57{
William Lallemand3f128872019-04-01 11:29:59 +020058 struct mworker_proc *child;
William Lallemande25473c2019-04-01 11:29:56 +020059
William Lallemand3f128872019-04-01 11:29:59 +020060 list_for_each_entry(child, &proc_list, list) {
61 /* 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 +020062 if ((child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG)) && (child->pid > 0))
William Lallemand3f128872019-04-01 11:29:59 +020063 kill(child->pid, sig);
William Lallemande25473c2019-04-01 11:29:56 +020064 }
65}
66
William Lallemand27edc4b2019-05-07 17:49:33 +020067void mworker_kill_max_reloads(int sig)
68{
69 struct mworker_proc *child;
70
71 list_for_each_entry(child, &proc_list, list) {
72 if (max_reloads != -1 && (child->options & PROC_O_TYPE_WORKER) &&
73 (child->pid > 0) && (child->reloads > max_reloads))
74 kill(child->pid, sig);
75 }
76}
William Lallemande25473c2019-04-01 11:29:56 +020077
78/* return 1 if a pid is a current child otherwise 0 */
William Lallemand3f128872019-04-01 11:29:59 +020079int mworker_current_child(int pid)
William Lallemande25473c2019-04-01 11:29:56 +020080{
William Lallemand3f128872019-04-01 11:29:59 +020081 struct mworker_proc *child;
William Lallemande25473c2019-04-01 11:29:56 +020082
William Lallemand3f128872019-04-01 11:29:59 +020083 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +020084 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 +020085 return 1;
86 }
87 return 0;
88}
89
William Lallemand3f128872019-04-01 11:29:59 +020090/*
91 * Return the number of new and old children (including workers and external
92 * processes)
93 */
94int mworker_child_nb()
95{
96 struct mworker_proc *child;
97 int ret = 0;
98
99 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200100 if (child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG))
William Lallemand3f128872019-04-01 11:29:59 +0200101 ret++;
102 }
103
104 return ret;
105}
106
107
William Lallemande25473c2019-04-01 11:29:56 +0200108/*
William Lallemand48dfbbd2019-04-01 11:29:53 +0200109 * serialize the proc list and put it in the environment
110 */
111void mworker_proc_list_to_env()
112{
113 char *msg = NULL;
114 struct mworker_proc *child;
115
116 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200117 char type = '?';
118
119 if (child->options & PROC_O_TYPE_MASTER)
120 type = 'm';
121 else if (child->options & PROC_O_TYPE_PROG)
122 type = 'e';
123 else if (child->options &= PROC_O_TYPE_WORKER)
124 type = 'w';
125
William Lallemand48dfbbd2019-04-01 11:29:53 +0200126 if (child->pid > -1)
William Lallemand68836742021-11-10 10:49:06 +0100127 memprintf(&msg, "%s|type=%c;fd=%d;pid=%d;reloads=%d;failedreloads=%d;timestamp=%d;id=%s;version=%s", msg ? msg : "", type, child->ipc_fd[0], child->pid, child->reloads, child->failedreloads, child->timestamp, child->id ? child->id : "", child->version);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200128 }
129 if (msg)
130 setenv("HAPROXY_PROCESSES", msg, 1);
131}
132
William Lallemand56be0e02022-01-28 21:11:41 +0100133struct mworker_proc *mworker_proc_new()
134{
135 struct mworker_proc *child;
136
137 child = calloc(1, sizeof(*child));
138 if (!child)
139 return NULL;
140
141 child->failedreloads = 0;
142 child->reloads = 0;
143 child->pid = -1;
144 child->ipc_fd[0] = -1;
145 child->ipc_fd[1] = -1;
146 child->timestamp = -1;
147
148 return child;
149}
150
151
William Lallemand48dfbbd2019-04-01 11:29:53 +0200152/*
153 * unserialize the proc list from the environment
154 */
Remi Tricot-Le Breton1f4fa902021-05-19 10:45:12 +0200155int mworker_env_to_proc_list()
William Lallemand48dfbbd2019-04-01 11:29:53 +0200156{
157 char *msg, *token = NULL, *s1;
William Lallemand90034bb2021-11-10 11:26:14 +0100158 struct mworker_proc *child;
159 int minreloads = INT_MAX; /* minimum number of reloads to chose which processes are "current" ones */
William Lallemand48dfbbd2019-04-01 11:29:53 +0200160
161 msg = getenv("HAPROXY_PROCESSES");
162 if (!msg)
Remi Tricot-Le Breton1f4fa902021-05-19 10:45:12 +0200163 return 0;
William Lallemand48dfbbd2019-04-01 11:29:53 +0200164
165 while ((token = strtok_r(msg, "|", &s1))) {
William Lallemand48dfbbd2019-04-01 11:29:53 +0200166 char *subtoken = NULL;
167 char *s2;
168
169 msg = NULL;
170
William Lallemand56be0e02022-01-28 21:11:41 +0100171 child = mworker_proc_new();
Remi Tricot-Le Breton1f4fa902021-05-19 10:45:12 +0200172 if (!child) {
173 ha_alert("Out of memory while trying to allocate a worker process structure.");
174 return -1;
175 }
William Lallemand48dfbbd2019-04-01 11:29:53 +0200176
177 while ((subtoken = strtok_r(token, ";", &s2))) {
178
179 token = NULL;
180
181 if (strncmp(subtoken, "type=", 5) == 0) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200182 char type;
183
184 type = *(subtoken+5);
185 if (type == 'm') { /* we are in the master, assign it */
William Lallemand48dfbbd2019-04-01 11:29:53 +0200186 proc_self = child;
William Lallemand8f7069a2019-04-12 16:09:23 +0200187 child->options |= PROC_O_TYPE_MASTER;
188 } else if (type == 'e') {
189 child->options |= PROC_O_TYPE_PROG;
190 } else if (type == 'w') {
191 child->options |= PROC_O_TYPE_WORKER;
192 }
193
William Lallemand48dfbbd2019-04-01 11:29:53 +0200194 } else if (strncmp(subtoken, "fd=", 3) == 0) {
195 child->ipc_fd[0] = atoi(subtoken+3);
196 } else if (strncmp(subtoken, "pid=", 4) == 0) {
197 child->pid = atoi(subtoken+4);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200198 } else if (strncmp(subtoken, "reloads=", 8) == 0) {
William Lallemandad221f42021-11-09 18:43:59 +0100199 /* we only increment the number of asked reload */
200 child->reloads = atoi(subtoken+8);
William Lallemand90034bb2021-11-10 11:26:14 +0100201
202 if (child->reloads < minreloads)
203 minreloads = child->reloads;
William Lallemand68836742021-11-10 10:49:06 +0100204 } else if (strncmp(subtoken, "failedreloads=", 14) == 0) {
205 child->failedreloads = atoi(subtoken+14);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200206 } else if (strncmp(subtoken, "timestamp=", 10) == 0) {
207 child->timestamp = atoi(subtoken+10);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200208 } else if (strncmp(subtoken, "id=", 3) == 0) {
209 child->id = strdup(subtoken+3);
William Lallemand1dc69632019-06-12 19:11:33 +0200210 } else if (strncmp(subtoken, "version=", 8) == 0) {
211 child->version = strdup(subtoken+8);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200212 }
213 }
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200214 if (child->pid) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200215 LIST_APPEND(&proc_list, &child->list);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200216 } else {
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200217 mworker_free_child(child);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200218 }
William Lallemand48dfbbd2019-04-01 11:29:53 +0200219 }
220
William Lallemand90034bb2021-11-10 11:26:14 +0100221 /* set the leaving processes once we know which number of reloads are the current processes */
222
223 list_for_each_entry(child, &proc_list, list) {
224 if (child->reloads > minreloads)
225 child->options |= PROC_O_LEAVING;
226 }
227
William Lallemand48dfbbd2019-04-01 11:29:53 +0200228 unsetenv("HAPROXY_PROCESSES");
Remi Tricot-Le Breton1f4fa902021-05-19 10:45:12 +0200229
230 return 0;
William Lallemand48dfbbd2019-04-01 11:29:53 +0200231}
William Lallemand3cd95d22019-04-01 11:29:54 +0200232
233/* Signal blocking and unblocking */
234
235void mworker_block_signals()
236{
237 sigset_t set;
238
239 sigemptyset(&set);
240 sigaddset(&set, SIGUSR1);
241 sigaddset(&set, SIGUSR2);
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100242 sigaddset(&set, SIGTTIN);
243 sigaddset(&set, SIGTTOU);
William Lallemand3cd95d22019-04-01 11:29:54 +0200244 sigaddset(&set, SIGHUP);
245 sigaddset(&set, SIGCHLD);
246 ha_sigmask(SIG_SETMASK, &set, NULL);
247}
248
249void mworker_unblock_signals()
250{
251 haproxy_unblock_signals();
252}
William Lallemand3fa724d2019-04-01 11:29:55 +0200253
William Lallemande25473c2019-04-01 11:29:56 +0200254/* ----- mworker signal handlers ----- */
255
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100256/* broadcast the configured signal to the workers */
257void mworker_broadcast_signal(struct sig_handler *sh)
258{
259 mworker_kill(sh->arg);
260}
261
William Lallemande25473c2019-04-01 11:29:56 +0200262/*
263 * When called, this function reexec haproxy with -sf followed by current
264 * children PIDs and possibly old children PIDs if they didn't leave yet.
265 */
266void mworker_catch_sighup(struct sig_handler *sh)
267{
268 mworker_reload();
269}
270
271void mworker_catch_sigterm(struct sig_handler *sh)
272{
273 int sig = sh->arg;
274
275#if defined(USE_SYSTEMD)
276 if (global.tune.options & GTUNE_USE_SYSTEMD) {
277 sd_notify(0, "STOPPING=1");
278 }
279#endif
280 ha_warning("Exiting Master process...\n");
281 mworker_kill(sig);
282}
283
284/*
285 * Wait for every children to exit
286 */
287
288void mworker_catch_sigchld(struct sig_handler *sh)
289{
290 int exitpid = -1;
291 int status = 0;
William Lallemande25473c2019-04-01 11:29:56 +0200292 int childfound;
293
294restart_wait:
295
296 childfound = 0;
297
298 exitpid = waitpid(-1, &status, WNOHANG);
299 if (exitpid > 0) {
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200300 struct mworker_proc *child, *it;
301
William Lallemande25473c2019-04-01 11:29:56 +0200302 if (WIFEXITED(status))
303 status = WEXITSTATUS(status);
304 else if (WIFSIGNALED(status))
305 status = 128 + WTERMSIG(status);
306 else if (WIFSTOPPED(status))
307 status = 128 + WSTOPSIG(status);
308 else
309 status = 255;
310
William Lallemand3f128872019-04-01 11:29:59 +0200311 /* delete the child from the process list */
William Lallemande25473c2019-04-01 11:29:56 +0200312 list_for_each_entry_safe(child, it, &proc_list, list) {
313 if (child->pid != exitpid)
314 continue;
315
Willy Tarreau2b718102021-04-21 07:32:39 +0200316 LIST_DELETE(&child->list);
William Lallemande25473c2019-04-01 11:29:56 +0200317 close(child->ipc_fd[0]);
318 childfound = 1;
319 break;
320 }
321
William Lallemand3f128872019-04-01 11:29:59 +0200322 if (!childfound) {
323 /* 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 +0200324 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 +0200325 } else {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200326 /* check if exited child is a current child */
William Lallemand45286112019-04-12 16:09:21 +0200327 if (!(child->options & PROC_O_LEAVING)) {
William Lallemanda655ba42020-05-06 17:27:03 +0200328 if (child->options & PROC_O_TYPE_WORKER) {
329 if (status < 128)
William Lallemand5d71a6b2021-11-09 15:25:31 +0100330 ha_warning("Current worker (%d) exited with code %d (%s)\n", exitpid, status, "Exit");
William Lallemanda655ba42020-05-06 17:27:03 +0200331 else
William Lallemand5d71a6b2021-11-09 15:25:31 +0100332 ha_alert("Current worker (%d) exited with code %d (%s)\n", exitpid, status, strsignal(status - 128));
William Lallemanda655ba42020-05-06 17:27:03 +0200333 }
William Lallemand8f7069a2019-04-12 16:09:23 +0200334 else if (child->options & PROC_O_TYPE_PROG)
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200335 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 +0200336
William Lallemande25473c2019-04-01 11:29:56 +0200337 if (status != 0 && status != 130 && status != 143
338 && !(global.tune.options & GTUNE_NOEXIT_ONFAILURE)) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200339 ha_alert("exit-on-failure: killing every processes with SIGTERM\n");
William Lallemande25473c2019-04-01 11:29:56 +0200340 mworker_kill(SIGTERM);
341 }
William Lallemand74f0ec32019-04-16 17:42:44 +0200342 /* 0 & SIGTERM (143) are normal, but we should report SIGINT (130) and other signals */
343 if (exitcode < 0 && status != 0 && status != 143)
344 exitcode = status;
William Lallemande25473c2019-04-01 11:29:56 +0200345 } else {
William Lallemand8f7069a2019-04-12 16:09:23 +0200346 if (child->options & PROC_O_TYPE_WORKER) {
William Lallemand5d71a6b2021-11-09 15:25:31 +0100347 ha_warning("Former worker (%d) exited with code %d (%s)\n", exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
William Lallemand3f128872019-04-01 11:29:59 +0200348 delete_oldpid(exitpid);
William Lallemand8f7069a2019-04-12 16:09:23 +0200349 } else if (child->options & PROC_O_TYPE_PROG) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200350 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 +0200351 }
William Lallemande25473c2019-04-01 11:29:56 +0200352 }
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200353 mworker_free_child(child);
354 child = NULL;
William Lallemande25473c2019-04-01 11:29:56 +0200355 }
356
357 /* do it again to check if it was the last worker */
358 goto restart_wait;
359 }
360 /* Better rely on the system than on a list of process to check if it was the last one */
361 else if (exitpid == -1 && errno == ECHILD) {
William Lallemand4cf4b332019-04-16 17:42:43 +0200362 ha_warning("All workers exited. Exiting... (%d)\n", (exitcode > 0) ? exitcode : EXIT_SUCCESS);
William Lallemande25473c2019-04-01 11:29:56 +0200363 atexit_flag = 0;
364 if (exitcode > 0)
William Lallemand4cf4b332019-04-16 17:42:43 +0200365 exit(exitcode); /* parent must leave using the status code that provoked the exit */
366 exit(EXIT_SUCCESS);
William Lallemande25473c2019-04-01 11:29:56 +0200367 }
368
369}
370
William Lallemand3fa724d2019-04-01 11:29:55 +0200371/* ----- IPC FD (sockpair) related ----- */
372
373/* This wrapper is called from the workers. It is registered instead of the
374 * normal listener_accept() so the worker can exit() when it detects that the
375 * master closed the IPC FD. If it's not a close, we just call the regular
Willy Tarreaua74cb382020-10-15 21:29:49 +0200376 * listener_accept() function.
377 */
William Lallemand3fa724d2019-04-01 11:29:55 +0200378void mworker_accept_wrapper(int fd)
379{
380 char c;
381 int ret;
382
383 while (1) {
384 ret = recv(fd, &c, 1, MSG_PEEK);
385 if (ret == -1) {
386 if (errno == EINTR)
387 continue;
388 if (errno == EAGAIN) {
389 fd_cant_recv(fd);
390 return;
391 }
392 break;
393 } else if (ret > 0) {
Willy Tarreaua74cb382020-10-15 21:29:49 +0200394 struct listener *l = fdtab[fd].owner;
395
396 if (l)
397 listener_accept(l);
William Lallemand3fa724d2019-04-01 11:29:55 +0200398 return;
399 } else if (ret == 0) {
400 /* At this step the master is down before
401 * this worker perform a 'normal' exit.
402 * So we want to exit with an error but
403 * other threads could currently process
404 * some stuff so we can't perform a clean
405 * deinit().
406 */
407 exit(EXIT_FAILURE);
408 }
409 }
410 return;
411}
412
413/*
Willy Tarreau619a95f2019-05-20 11:12:15 +0200414 * This function registers the accept wrapper for the sockpair of the master
415 * worker. It's only handled by worker thread #0. Other threads and master do
416 * nothing here. It always returns 1 (success).
William Lallemand3fa724d2019-04-01 11:29:55 +0200417 */
Willy Tarreau619a95f2019-05-20 11:12:15 +0200418static int mworker_pipe_register_per_thread()
William Lallemand3fa724d2019-04-01 11:29:55 +0200419{
Willy Tarreau619a95f2019-05-20 11:12:15 +0200420 if (!(global.mode & MODE_MWORKER) || master)
421 return 1;
422
423 if (tid != 0)
424 return 1;
William Lallemand3fa724d2019-04-01 11:29:55 +0200425
426 fcntl(proc_self->ipc_fd[1], F_SETFL, O_NONBLOCK);
427 /* In multi-tread, we need only one thread to process
428 * events on the pipe with master
429 */
Willy Tarreau619a95f2019-05-20 11:12:15 +0200430 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 +0200431 fd_want_recv(proc_self->ipc_fd[1]);
Willy Tarreau619a95f2019-05-20 11:12:15 +0200432 return 1;
William Lallemand3fa724d2019-04-01 11:29:55 +0200433}
William Lallemand9001ce82019-04-01 11:29:57 +0200434
Willy Tarreau619a95f2019-05-20 11:12:15 +0200435REGISTER_PER_THREAD_INIT(mworker_pipe_register_per_thread);
436
William Lallemand9001ce82019-04-01 11:29:57 +0200437/* ----- proxies ----- */
438/*
439 * Upon a reload, the master worker needs to close all listeners FDs but the mworker_pipe
440 * fd, and the FD provided by fd@
441 */
442void mworker_cleanlisteners()
443{
444 struct listener *l, *l_next;
445 struct proxy *curproxy;
446 struct peers *curpeers;
447
448 /* we might have to unbind some peers sections from some processes */
449 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
450 if (!curpeers->peers_fe)
451 continue;
452
453 stop_proxy(curpeers->peers_fe);
454 /* disable this peer section so that it kills itself */
455 signal_unregister_handler(curpeers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200456 task_destroy(curpeers->sync_task);
William Lallemand9001ce82019-04-01 11:29:57 +0200457 curpeers->sync_task = NULL;
Olivier Houchard3f795f72019-04-17 22:51:06 +0200458 task_destroy(curpeers->peers_fe->task);
William Lallemand9001ce82019-04-01 11:29:57 +0200459 curpeers->peers_fe->task = NULL;
460 curpeers->peers_fe = NULL;
461 }
462
463 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
464 int listen_in_master = 0;
465
466 list_for_each_entry_safe(l, l_next, &curproxy->conf.listeners, by_fe) {
467 /* remove the listener, but not those we need in the master... */
Willy Tarreau18c20d22020-10-09 16:11:46 +0200468 if (!(l->rx.flags & RX_F_MWORKER)) {
Willy Tarreau75c98d12020-10-09 15:55:23 +0200469 unbind_listener(l);
William Lallemand9001ce82019-04-01 11:29:57 +0200470 delete_listener(l);
471 } else {
472 listen_in_master = 1;
473 }
474 }
475 /* if the proxy shouldn't be in the master, we stop it */
476 if (!listen_in_master)
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200477 curproxy->flags |= PR_FL_DISABLED;
William Lallemand9001ce82019-04-01 11:29:57 +0200478 }
William Lallemand88dc7c52019-04-01 11:30:01 +0200479}
480
William Lallemand55a921c2022-01-28 21:17:30 +0100481/* Upon a configuration loading error some mworker_proc and FDs/server were
482 * assigned but the worker was never forked, we must close the FDs and
483 * remove the server
484 */
485void mworker_cleanup_proc()
486{
487 struct mworker_proc *child, *it;
488
489 list_for_each_entry_safe(child, it, &proc_list, list) {
490
491 if (child->pid == -1) {
492 /* Close the socketpair master side. We don't need to
493 * close the worker side, because it's stored in the
494 * GLOBAL cli listener which was supposed to be in the
495 * worker and which will be closed in
496 * mworker_cleanlisteners()
497 */
498 if (child->ipc_fd[0] > -1)
499 close(child->ipc_fd[0]);
500 if (child->srv) {
501 /* only exists if we created a master CLI listener */
502 srv_drop(child->srv);
503 }
504 LIST_DELETE(&child->list);
505 mworker_free_child(child);
506 }
507 }
508}
509
510
William Lallemand88dc7c52019-04-01 11:30:01 +0200511/* Displays workers and processes */
512static int cli_io_handler_show_proc(struct appctx *appctx)
513{
Christopher Faulet908628c2022-03-25 16:43:49 +0100514 struct conn_stream *cs = appctx->owner;
William Lallemand88dc7c52019-04-01 11:30:01 +0200515 struct mworker_proc *child;
516 int old = 0;
517 int up = now.tv_sec - proc_self->timestamp;
William Lallemande8669fc2019-06-12 18:21:17 +0200518 char *uptime = NULL;
William Lallemand68836742021-11-10 10:49:06 +0100519 char *reloadtxt = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200520
Christopher Faulet908628c2022-03-25 16:43:49 +0100521 if (unlikely(cs_ic(cs)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
William Lallemand88dc7c52019-04-01 11:30:01 +0200522 return 1;
523
524 chunk_reset(&trash);
525
William Lallemand68836742021-11-10 10:49:06 +0100526 memprintf(&reloadtxt, "%d [failed: %d]", proc_self->reloads, proc_self->failedreloads);
William Lallemand5d71a6b2021-11-09 15:25:31 +0100527 chunk_printf(&trash, "#%-14s %-15s %-15s %-15s %-15s\n", "<PID>", "<type>", "<reloads>", "<uptime>", "<version>");
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 Lallemand68836742021-11-10 10:49:06 +0100529 chunk_appendf(&trash, "%-15u %-15s %-15s %-15s %-15s\n", (unsigned int)getpid(), "master", reloadtxt, uptime, haproxy_version);
530 ha_free(&reloadtxt);
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100531 ha_free(&uptime);
William Lallemand88dc7c52019-04-01 11:30:01 +0200532
533 /* displays current processes */
534
535 chunk_appendf(&trash, "# workers\n");
536 list_for_each_entry(child, &proc_list, list) {
537 up = now.tv_sec - child->timestamp;
538
William Lallemand8f7069a2019-04-12 16:09:23 +0200539 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200540 continue;
541
William Lallemand45286112019-04-12 16:09:21 +0200542 if (child->options & PROC_O_LEAVING) {
William Lallemand88dc7c52019-04-01 11:30:01 +0200543 old++;
544 continue;
545 }
William Lallemande8669fc2019-06-12 18:21:17 +0200546 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand5d71a6b2021-11-09 15:25:31 +0100547 chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, "worker", child->reloads, uptime, child->version);
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100548 ha_free(&uptime);
William Lallemand88dc7c52019-04-01 11:30:01 +0200549 }
550
551 /* displays old processes */
552
553 if (old) {
554 char *msg = NULL;
555
556 chunk_appendf(&trash, "# old workers\n");
557 list_for_each_entry(child, &proc_list, list) {
558 up = now.tv_sec - child->timestamp;
559
William Lallemand8f7069a2019-04-12 16:09:23 +0200560 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200561 continue;
562
William Lallemand45286112019-04-12 16:09:21 +0200563 if (child->options & PROC_O_LEAVING) {
William Lallemande8669fc2019-06-12 18:21:17 +0200564 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand5d71a6b2021-11-09 15:25:31 +0100565 chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, "worker", child->reloads, uptime, child->version);
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100566 ha_free(&uptime);
William Lallemand88dc7c52019-04-01 11:30:01 +0200567 }
568 }
569 free(msg);
570 }
571
William Lallemandad53d6d2019-04-01 11:30:03 +0200572 /* displays external process */
573 chunk_appendf(&trash, "# programs\n");
574 old = 0;
575 list_for_each_entry(child, &proc_list, list) {
576 up = now.tv_sec - child->timestamp;
577
William Lallemand8f7069a2019-04-12 16:09:23 +0200578 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200579 continue;
580
William Lallemand45286112019-04-12 16:09:21 +0200581 if (child->options & PROC_O_LEAVING) {
William Lallemandad53d6d2019-04-01 11:30:03 +0200582 old++;
583 continue;
584 }
William Lallemande8669fc2019-06-12 18:21:17 +0200585 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand5d71a6b2021-11-09 15:25:31 +0100586 chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, child->id, child->reloads, uptime, "-");
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100587 ha_free(&uptime);
William Lallemandad53d6d2019-04-01 11:30:03 +0200588 }
589
590 if (old) {
591 chunk_appendf(&trash, "# old programs\n");
592 list_for_each_entry(child, &proc_list, list) {
593 up = now.tv_sec - child->timestamp;
594
William Lallemand8f7069a2019-04-12 16:09:23 +0200595 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200596 continue;
597
William Lallemand45286112019-04-12 16:09:21 +0200598 if (child->options & PROC_O_LEAVING) {
William Lallemande8669fc2019-06-12 18:21:17 +0200599 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand5d71a6b2021-11-09 15:25:31 +0100600 chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, child->id, child->reloads, uptime, "-");
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100601 ha_free(&uptime);
William Lallemandad53d6d2019-04-01 11:30:03 +0200602 }
603 }
604 }
605
606
607
Christopher Faulet908628c2022-03-25 16:43:49 +0100608 if (ci_putchk(cs_ic(cs), &trash) == -1) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200609 cs_rx_room_blk(cs);
William Lallemand88dc7c52019-04-01 11:30:01 +0200610 return 0;
611 }
612
613 /* dump complete */
614 return 1;
William Lallemand9001ce82019-04-01 11:29:57 +0200615}
William Lallemand88dc7c52019-04-01 11:30:01 +0200616
617/* reload the master process */
618static int cli_parse_reload(char **args, char *payload, struct appctx *appctx, void *private)
619{
620 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
621 return 1;
622
623 mworker_reload();
624
625 return 1;
626}
627
628
William Lallemand27edc4b2019-05-07 17:49:33 +0200629static int mworker_parse_global_max_reloads(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +0100630 const struct proxy *defpx, const char *file, int linenum, char **err)
William Lallemand27edc4b2019-05-07 17:49:33 +0200631{
632
633 int err_code = 0;
634
635 if (alertif_too_many_args(1, file, linenum, args, &err_code))
636 goto out;
637
638 if (*(args[1]) == 0) {
639 memprintf(err, "%sparsing [%s:%d] : '%s' expects an integer argument.\n", *err, file, linenum, args[0]);
640 err_code |= ERR_ALERT | ERR_FATAL;
641 goto out;
642 }
643
644 max_reloads = atol(args[1]);
645 if (max_reloads < 0) {
646 memprintf(err, "%sparsing [%s:%d] '%s' : invalid value %d, must be >= 0", *err, file, linenum, args[0], max_reloads);
647 err_code |= ERR_ALERT | ERR_FATAL;
648 goto out;
649 }
650
651out:
652 return err_code;
653}
654
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200655void mworker_free_child(struct mworker_proc *child)
656{
William Lallemand08cb9452022-01-27 15:33:40 +0100657 int i;
658
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200659 if (child == NULL)
660 return;
661
William Lallemand08cb9452022-01-27 15:33:40 +0100662 for (i = 0; child->command && child->command[i]; i++)
663 ha_free(&child->command[i]);
William Lallemande8669fc2019-06-12 18:21:17 +0200664
William Lallemand08cb9452022-01-27 15:33:40 +0100665 ha_free(&child->command);
666 ha_free(&child->id);
667 ha_free(&child->version);
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200668 free(child);
669}
William Lallemand27edc4b2019-05-07 17:49:33 +0200670
671static struct cfg_kw_list mworker_kws = {{ }, {
672 { CFG_GLOBAL, "mworker-max-reloads", mworker_parse_global_max_reloads },
673 { 0, NULL, NULL },
674}};
675
676INITCALL1(STG_REGISTER, cfg_register_keywords, &mworker_kws);
677
678
William Lallemand88dc7c52019-04-01 11:30:01 +0200679/* register cli keywords */
680static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau23c740e2021-05-09 22:49:44 +0200681 { { "@<relative pid>", NULL }, "@<relative pid> : send a command to the <relative pid> process", NULL, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
682 { { "@!<pid>", NULL }, "@!<pid> : send a command to the <pid> process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
683 { { "@master", NULL }, "@master : send a command to the master process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
684 { { "show", "proc", NULL }, "show proc : show processes status", cli_parse_default, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
685 { { "reload", NULL }, "reload : reload haproxy", cli_parse_reload, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
William Lallemand88dc7c52019-04-01 11:30:01 +0200686 {{},}
687}};
688
689INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);