blob: 6365a59cf055c86ac7cad72685cf919964270c7a [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>
William Lallemand3fa724d2019-04-01 11:29:55 +020016#include <signal.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +020017#include <stdlib.h>
18#include <string.h>
William Lallemande25473c2019-04-01 11:29:56 +020019#include <sys/wait.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +020020
Willy Tarreaub2551052020-06-09 09:07:15 +020021#if defined(USE_SYSTEMD)
22#include <systemd/sd-daemon.h>
23#endif
24
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020025#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020026#include <haproxy/cfgparse.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020027#include <haproxy/cli.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020028#include <haproxy/errors.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020029#include <haproxy/fd.h>
30#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020031#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020032#include <haproxy/listener.h>
Willy Tarreaub5abe5b2020-06-04 14:07:37 +020033#include <haproxy/mworker.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +020034#include <haproxy/peers.h>
Willy Tarreau7c668572021-05-08 20:21:31 +020035#include <haproxy/proxy.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020036#include <haproxy/sc_strm.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +020037#include <haproxy/signal.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020038#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020039#include <haproxy/stream.h>
Willy Tarreau745e98c2021-05-08 13:58:19 +020040#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020041#include <haproxy/version.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +020042
William Lallemand48dfbbd2019-04-01 11:29:53 +020043
William Lallemande25473c2019-04-01 11:29:56 +020044static int exitcode = -1;
William Lallemand27edc4b2019-05-07 17:49:33 +020045static int max_reloads = -1; /* number max of reloads a worker can have until they are killed */
Willy Tarreau15f9ac32021-05-08 12:30:50 +020046struct mworker_proc *proc_self = NULL; /* process structure of current process */
William Lallemande25473c2019-04-01 11:29:56 +020047
William Lallemande25473c2019-04-01 11:29:56 +020048/* ----- children processes handling ----- */
William Lallemand48dfbbd2019-04-01 11:29:53 +020049
William Lallemand48dfbbd2019-04-01 11:29:53 +020050/*
William Lallemande25473c2019-04-01 11:29:56 +020051 * Send signal to every known children.
52 */
53
54static void mworker_kill(int sig)
55{
William Lallemand3f128872019-04-01 11:29:59 +020056 struct mworker_proc *child;
William Lallemande25473c2019-04-01 11:29:56 +020057
William Lallemand3f128872019-04-01 11:29:59 +020058 list_for_each_entry(child, &proc_list, list) {
59 /* careful there, we must be sure that the pid > 0, we don't want to emit a kill -1 */
William Lallemand32b69012019-04-16 17:42:42 +020060 if ((child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG)) && (child->pid > 0))
William Lallemand3f128872019-04-01 11:29:59 +020061 kill(child->pid, sig);
William Lallemande25473c2019-04-01 11:29:56 +020062 }
63}
64
William Lallemand27edc4b2019-05-07 17:49:33 +020065void mworker_kill_max_reloads(int sig)
66{
67 struct mworker_proc *child;
68
69 list_for_each_entry(child, &proc_list, list) {
70 if (max_reloads != -1 && (child->options & PROC_O_TYPE_WORKER) &&
71 (child->pid > 0) && (child->reloads > max_reloads))
72 kill(child->pid, sig);
73 }
74}
William Lallemande25473c2019-04-01 11:29:56 +020075
76/* return 1 if a pid is a current child otherwise 0 */
William Lallemand3f128872019-04-01 11:29:59 +020077int mworker_current_child(int pid)
William Lallemande25473c2019-04-01 11:29:56 +020078{
William Lallemand3f128872019-04-01 11:29:59 +020079 struct mworker_proc *child;
William Lallemande25473c2019-04-01 11:29:56 +020080
William Lallemand3f128872019-04-01 11:29:59 +020081 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +020082 if ((child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG)) && (!(child->options & PROC_O_LEAVING)) && (child->pid == pid))
William Lallemande25473c2019-04-01 11:29:56 +020083 return 1;
84 }
85 return 0;
86}
87
William Lallemand3f128872019-04-01 11:29:59 +020088/*
89 * Return the number of new and old children (including workers and external
90 * processes)
91 */
92int mworker_child_nb()
93{
94 struct mworker_proc *child;
95 int ret = 0;
96
97 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +020098 if (child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG))
William Lallemand3f128872019-04-01 11:29:59 +020099 ret++;
100 }
101
102 return ret;
103}
104
105
William Lallemande25473c2019-04-01 11:29:56 +0200106/*
William Lallemand48dfbbd2019-04-01 11:29:53 +0200107 * serialize the proc list and put it in the environment
108 */
109void mworker_proc_list_to_env()
110{
111 char *msg = NULL;
112 struct mworker_proc *child;
William Lallemand14b98ef2022-07-27 11:57:12 +0200113 int minreloads = INT_MAX; /* minimum number of reloads to chose which processes are "current" ones */
William Lallemand48dfbbd2019-04-01 11:29:53 +0200114
115 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200116 char type = '?';
117
118 if (child->options & PROC_O_TYPE_MASTER)
119 type = 'm';
120 else if (child->options & PROC_O_TYPE_PROG)
121 type = 'e';
122 else if (child->options &= PROC_O_TYPE_WORKER)
123 type = 'w';
124
William Lallemand14b98ef2022-07-27 11:57:12 +0200125 if (child->reloads < minreloads)
126 minreloads = child->reloads;
127
William Lallemand48dfbbd2019-04-01 11:29:53 +0200128 if (child->pid > -1)
William Lallemand68836742021-11-10 10:49:06 +0100129 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 +0200130 }
131 if (msg)
132 setenv("HAPROXY_PROCESSES", msg, 1);
William Lallemand14b98ef2022-07-27 11:57:12 +0200133
134 list_for_each_entry(child, &proc_list, list) {
135 if (child->reloads > minreloads && !(child->options & PROC_O_TYPE_MASTER)) {
136 child->options |= PROC_O_LEAVING;
137 }
138 }
139
140
William Lallemand48dfbbd2019-04-01 11:29:53 +0200141}
142
William Lallemand56be0e02022-01-28 21:11:41 +0100143struct mworker_proc *mworker_proc_new()
144{
145 struct mworker_proc *child;
146
147 child = calloc(1, sizeof(*child));
148 if (!child)
149 return NULL;
150
151 child->failedreloads = 0;
152 child->reloads = 0;
153 child->pid = -1;
154 child->ipc_fd[0] = -1;
155 child->ipc_fd[1] = -1;
156 child->timestamp = -1;
157
158 return child;
159}
160
161
William Lallemand48dfbbd2019-04-01 11:29:53 +0200162/*
163 * unserialize the proc list from the environment
164 */
Remi Tricot-Le Breton1f4fa902021-05-19 10:45:12 +0200165int mworker_env_to_proc_list()
William Lallemand48dfbbd2019-04-01 11:29:53 +0200166{
167 char *msg, *token = NULL, *s1;
William Lallemand90034bb2021-11-10 11:26:14 +0100168 struct mworker_proc *child;
169 int minreloads = INT_MAX; /* minimum number of reloads to chose which processes are "current" ones */
William Lallemand48dfbbd2019-04-01 11:29:53 +0200170
171 msg = getenv("HAPROXY_PROCESSES");
172 if (!msg)
Remi Tricot-Le Breton1f4fa902021-05-19 10:45:12 +0200173 return 0;
William Lallemand48dfbbd2019-04-01 11:29:53 +0200174
175 while ((token = strtok_r(msg, "|", &s1))) {
William Lallemand48dfbbd2019-04-01 11:29:53 +0200176 char *subtoken = NULL;
177 char *s2;
178
179 msg = NULL;
180
William Lallemand56be0e02022-01-28 21:11:41 +0100181 child = mworker_proc_new();
Remi Tricot-Le Breton1f4fa902021-05-19 10:45:12 +0200182 if (!child) {
183 ha_alert("Out of memory while trying to allocate a worker process structure.");
184 return -1;
185 }
William Lallemand48dfbbd2019-04-01 11:29:53 +0200186
187 while ((subtoken = strtok_r(token, ";", &s2))) {
188
189 token = NULL;
190
191 if (strncmp(subtoken, "type=", 5) == 0) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200192 char type;
193
194 type = *(subtoken+5);
195 if (type == 'm') { /* we are in the master, assign it */
William Lallemand48dfbbd2019-04-01 11:29:53 +0200196 proc_self = child;
William Lallemand8f7069a2019-04-12 16:09:23 +0200197 child->options |= PROC_O_TYPE_MASTER;
198 } else if (type == 'e') {
199 child->options |= PROC_O_TYPE_PROG;
200 } else if (type == 'w') {
201 child->options |= PROC_O_TYPE_WORKER;
202 }
203
William Lallemand48dfbbd2019-04-01 11:29:53 +0200204 } else if (strncmp(subtoken, "fd=", 3) == 0) {
205 child->ipc_fd[0] = atoi(subtoken+3);
206 } else if (strncmp(subtoken, "pid=", 4) == 0) {
207 child->pid = atoi(subtoken+4);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200208 } else if (strncmp(subtoken, "reloads=", 8) == 0) {
William Lallemandad221f42021-11-09 18:43:59 +0100209 /* we only increment the number of asked reload */
210 child->reloads = atoi(subtoken+8);
William Lallemand90034bb2021-11-10 11:26:14 +0100211
212 if (child->reloads < minreloads)
213 minreloads = child->reloads;
William Lallemand68836742021-11-10 10:49:06 +0100214 } else if (strncmp(subtoken, "failedreloads=", 14) == 0) {
215 child->failedreloads = atoi(subtoken+14);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200216 } else if (strncmp(subtoken, "timestamp=", 10) == 0) {
217 child->timestamp = atoi(subtoken+10);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200218 } else if (strncmp(subtoken, "id=", 3) == 0) {
219 child->id = strdup(subtoken+3);
William Lallemand1dc69632019-06-12 19:11:33 +0200220 } else if (strncmp(subtoken, "version=", 8) == 0) {
221 child->version = strdup(subtoken+8);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200222 }
223 }
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200224 if (child->pid) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200225 LIST_APPEND(&proc_list, &child->list);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200226 } else {
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200227 mworker_free_child(child);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200228 }
William Lallemand48dfbbd2019-04-01 11:29:53 +0200229 }
230
William Lallemand90034bb2021-11-10 11:26:14 +0100231 /* set the leaving processes once we know which number of reloads are the current processes */
232
233 list_for_each_entry(child, &proc_list, list) {
234 if (child->reloads > minreloads)
235 child->options |= PROC_O_LEAVING;
236 }
237
William Lallemand48dfbbd2019-04-01 11:29:53 +0200238 unsetenv("HAPROXY_PROCESSES");
Remi Tricot-Le Breton1f4fa902021-05-19 10:45:12 +0200239
240 return 0;
William Lallemand48dfbbd2019-04-01 11:29:53 +0200241}
William Lallemand3cd95d22019-04-01 11:29:54 +0200242
243/* Signal blocking and unblocking */
244
245void mworker_block_signals()
246{
247 sigset_t set;
248
249 sigemptyset(&set);
250 sigaddset(&set, SIGUSR1);
251 sigaddset(&set, SIGUSR2);
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100252 sigaddset(&set, SIGTTIN);
253 sigaddset(&set, SIGTTOU);
William Lallemand3cd95d22019-04-01 11:29:54 +0200254 sigaddset(&set, SIGHUP);
255 sigaddset(&set, SIGCHLD);
256 ha_sigmask(SIG_SETMASK, &set, NULL);
257}
258
259void mworker_unblock_signals()
260{
261 haproxy_unblock_signals();
262}
William Lallemand3fa724d2019-04-01 11:29:55 +0200263
William Lallemande25473c2019-04-01 11:29:56 +0200264/* ----- mworker signal handlers ----- */
265
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100266/* broadcast the configured signal to the workers */
267void mworker_broadcast_signal(struct sig_handler *sh)
268{
269 mworker_kill(sh->arg);
270}
271
William Lallemande25473c2019-04-01 11:29:56 +0200272/*
273 * When called, this function reexec haproxy with -sf followed by current
274 * children PIDs and possibly old children PIDs if they didn't leave yet.
275 */
276void mworker_catch_sighup(struct sig_handler *sh)
277{
278 mworker_reload();
279}
280
281void mworker_catch_sigterm(struct sig_handler *sh)
282{
283 int sig = sh->arg;
284
285#if defined(USE_SYSTEMD)
286 if (global.tune.options & GTUNE_USE_SYSTEMD) {
287 sd_notify(0, "STOPPING=1");
288 }
289#endif
290 ha_warning("Exiting Master process...\n");
291 mworker_kill(sig);
292}
293
294/*
295 * Wait for every children to exit
296 */
297
298void mworker_catch_sigchld(struct sig_handler *sh)
299{
300 int exitpid = -1;
301 int status = 0;
William Lallemande25473c2019-04-01 11:29:56 +0200302 int childfound;
303
304restart_wait:
305
306 childfound = 0;
307
308 exitpid = waitpid(-1, &status, WNOHANG);
309 if (exitpid > 0) {
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200310 struct mworker_proc *child, *it;
311
William Lallemande25473c2019-04-01 11:29:56 +0200312 if (WIFEXITED(status))
313 status = WEXITSTATUS(status);
314 else if (WIFSIGNALED(status))
315 status = 128 + WTERMSIG(status);
316 else if (WIFSTOPPED(status))
317 status = 128 + WSTOPSIG(status);
318 else
319 status = 255;
320
William Lallemand3f128872019-04-01 11:29:59 +0200321 /* delete the child from the process list */
William Lallemande25473c2019-04-01 11:29:56 +0200322 list_for_each_entry_safe(child, it, &proc_list, list) {
323 if (child->pid != exitpid)
324 continue;
325
Willy Tarreau2b718102021-04-21 07:32:39 +0200326 LIST_DELETE(&child->list);
William Lallemande25473c2019-04-01 11:29:56 +0200327 close(child->ipc_fd[0]);
328 childfound = 1;
329 break;
330 }
331
William Lallemand3f128872019-04-01 11:29:59 +0200332 if (!childfound) {
333 /* 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 +0200334 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 +0200335 } else {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200336 /* check if exited child is a current child */
William Lallemand45286112019-04-12 16:09:21 +0200337 if (!(child->options & PROC_O_LEAVING)) {
William Lallemanda655ba42020-05-06 17:27:03 +0200338 if (child->options & PROC_O_TYPE_WORKER) {
339 if (status < 128)
William Lallemand5d71a6b2021-11-09 15:25:31 +0100340 ha_warning("Current worker (%d) exited with code %d (%s)\n", exitpid, status, "Exit");
William Lallemanda655ba42020-05-06 17:27:03 +0200341 else
William Lallemand5d71a6b2021-11-09 15:25:31 +0100342 ha_alert("Current worker (%d) exited with code %d (%s)\n", exitpid, status, strsignal(status - 128));
William Lallemanda655ba42020-05-06 17:27:03 +0200343 }
William Lallemand8f7069a2019-04-12 16:09:23 +0200344 else if (child->options & PROC_O_TYPE_PROG)
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200345 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 +0200346
William Lallemande25473c2019-04-01 11:29:56 +0200347 if (status != 0 && status != 130 && status != 143
348 && !(global.tune.options & GTUNE_NOEXIT_ONFAILURE)) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200349 ha_alert("exit-on-failure: killing every processes with SIGTERM\n");
William Lallemande25473c2019-04-01 11:29:56 +0200350 mworker_kill(SIGTERM);
351 }
William Lallemand74f0ec32019-04-16 17:42:44 +0200352 /* 0 & SIGTERM (143) are normal, but we should report SIGINT (130) and other signals */
353 if (exitcode < 0 && status != 0 && status != 143)
354 exitcode = status;
William Lallemande25473c2019-04-01 11:29:56 +0200355 } else {
William Lallemand8f7069a2019-04-12 16:09:23 +0200356 if (child->options & PROC_O_TYPE_WORKER) {
William Lallemand5d71a6b2021-11-09 15:25:31 +0100357 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 +0200358 delete_oldpid(exitpid);
William Lallemand8f7069a2019-04-12 16:09:23 +0200359 } else if (child->options & PROC_O_TYPE_PROG) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200360 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 +0200361 }
William Lallemande25473c2019-04-01 11:29:56 +0200362 }
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200363 mworker_free_child(child);
364 child = NULL;
William Lallemande25473c2019-04-01 11:29:56 +0200365 }
366
367 /* do it again to check if it was the last worker */
368 goto restart_wait;
369 }
370 /* Better rely on the system than on a list of process to check if it was the last one */
371 else if (exitpid == -1 && errno == ECHILD) {
William Lallemand4cf4b332019-04-16 17:42:43 +0200372 ha_warning("All workers exited. Exiting... (%d)\n", (exitcode > 0) ? exitcode : EXIT_SUCCESS);
William Lallemande25473c2019-04-01 11:29:56 +0200373 atexit_flag = 0;
374 if (exitcode > 0)
William Lallemand4cf4b332019-04-16 17:42:43 +0200375 exit(exitcode); /* parent must leave using the status code that provoked the exit */
376 exit(EXIT_SUCCESS);
William Lallemande25473c2019-04-01 11:29:56 +0200377 }
378
379}
380
William Lallemand3fa724d2019-04-01 11:29:55 +0200381/* ----- IPC FD (sockpair) related ----- */
382
383/* This wrapper is called from the workers. It is registered instead of the
384 * normal listener_accept() so the worker can exit() when it detects that the
385 * master closed the IPC FD. If it's not a close, we just call the regular
Willy Tarreaua74cb382020-10-15 21:29:49 +0200386 * listener_accept() function.
387 */
William Lallemand3fa724d2019-04-01 11:29:55 +0200388void mworker_accept_wrapper(int fd)
389{
390 char c;
391 int ret;
392
393 while (1) {
394 ret = recv(fd, &c, 1, MSG_PEEK);
395 if (ret == -1) {
396 if (errno == EINTR)
397 continue;
Willy Tarreauacef5e22022-04-25 20:32:15 +0200398 if (errno == EAGAIN || errno == EWOULDBLOCK) {
William Lallemand3fa724d2019-04-01 11:29:55 +0200399 fd_cant_recv(fd);
400 return;
401 }
402 break;
403 } else if (ret > 0) {
Willy Tarreaua74cb382020-10-15 21:29:49 +0200404 struct listener *l = fdtab[fd].owner;
405
406 if (l)
407 listener_accept(l);
William Lallemand3fa724d2019-04-01 11:29:55 +0200408 return;
409 } else if (ret == 0) {
410 /* At this step the master is down before
411 * this worker perform a 'normal' exit.
412 * So we want to exit with an error but
413 * other threads could currently process
414 * some stuff so we can't perform a clean
415 * deinit().
416 */
417 exit(EXIT_FAILURE);
418 }
419 }
420 return;
421}
422
423/*
Willy Tarreau619a95f2019-05-20 11:12:15 +0200424 * This function registers the accept wrapper for the sockpair of the master
425 * worker. It's only handled by worker thread #0. Other threads and master do
426 * nothing here. It always returns 1 (success).
William Lallemand3fa724d2019-04-01 11:29:55 +0200427 */
William Lallemand2ee490f2022-07-05 09:04:03 +0200428static int mworker_sockpair_register_per_thread()
William Lallemand3fa724d2019-04-01 11:29:55 +0200429{
Willy Tarreau619a95f2019-05-20 11:12:15 +0200430 if (!(global.mode & MODE_MWORKER) || master)
431 return 1;
432
433 if (tid != 0)
434 return 1;
William Lallemand3fa724d2019-04-01 11:29:55 +0200435
Willy Tarreau38247432022-04-26 10:24:14 +0200436 fd_set_nonblock(proc_self->ipc_fd[1]);
William Lallemand2ee490f2022-07-05 09:04:03 +0200437 /* register the wrapper to handle read 0 when the master exits */
William Lallemand34aae2f2022-07-05 00:55:09 +0200438 fdtab[proc_self->ipc_fd[1]].iocb = mworker_accept_wrapper;
William Lallemand3fa724d2019-04-01 11:29:55 +0200439 fd_want_recv(proc_self->ipc_fd[1]);
Willy Tarreau619a95f2019-05-20 11:12:15 +0200440 return 1;
William Lallemand3fa724d2019-04-01 11:29:55 +0200441}
William Lallemand9001ce82019-04-01 11:29:57 +0200442
William Lallemand2ee490f2022-07-05 09:04:03 +0200443REGISTER_PER_THREAD_INIT(mworker_sockpair_register_per_thread);
Willy Tarreau619a95f2019-05-20 11:12:15 +0200444
William Lallemand9001ce82019-04-01 11:29:57 +0200445/* ----- proxies ----- */
446/*
447 * Upon a reload, the master worker needs to close all listeners FDs but the mworker_pipe
448 * fd, and the FD provided by fd@
449 */
450void mworker_cleanlisteners()
451{
452 struct listener *l, *l_next;
453 struct proxy *curproxy;
454 struct peers *curpeers;
455
456 /* we might have to unbind some peers sections from some processes */
457 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
458 if (!curpeers->peers_fe)
459 continue;
460
461 stop_proxy(curpeers->peers_fe);
462 /* disable this peer section so that it kills itself */
463 signal_unregister_handler(curpeers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200464 task_destroy(curpeers->sync_task);
William Lallemand9001ce82019-04-01 11:29:57 +0200465 curpeers->sync_task = NULL;
Olivier Houchard3f795f72019-04-17 22:51:06 +0200466 task_destroy(curpeers->peers_fe->task);
William Lallemand9001ce82019-04-01 11:29:57 +0200467 curpeers->peers_fe->task = NULL;
468 curpeers->peers_fe = NULL;
469 }
470
471 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
472 int listen_in_master = 0;
473
474 list_for_each_entry_safe(l, l_next, &curproxy->conf.listeners, by_fe) {
475 /* remove the listener, but not those we need in the master... */
Willy Tarreau18c20d22020-10-09 16:11:46 +0200476 if (!(l->rx.flags & RX_F_MWORKER)) {
Willy Tarreau75c98d12020-10-09 15:55:23 +0200477 unbind_listener(l);
William Lallemand9001ce82019-04-01 11:29:57 +0200478 delete_listener(l);
479 } else {
480 listen_in_master = 1;
481 }
482 }
483 /* if the proxy shouldn't be in the master, we stop it */
484 if (!listen_in_master)
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200485 curproxy->flags |= PR_FL_DISABLED;
William Lallemand9001ce82019-04-01 11:29:57 +0200486 }
William Lallemand88dc7c52019-04-01 11:30:01 +0200487}
488
William Lallemand55a921c2022-01-28 21:17:30 +0100489/* Upon a configuration loading error some mworker_proc and FDs/server were
490 * assigned but the worker was never forked, we must close the FDs and
491 * remove the server
492 */
493void mworker_cleanup_proc()
494{
495 struct mworker_proc *child, *it;
496
497 list_for_each_entry_safe(child, it, &proc_list, list) {
498
499 if (child->pid == -1) {
500 /* Close the socketpair master side. We don't need to
501 * close the worker side, because it's stored in the
502 * GLOBAL cli listener which was supposed to be in the
503 * worker and which will be closed in
504 * mworker_cleanlisteners()
505 */
506 if (child->ipc_fd[0] > -1)
507 close(child->ipc_fd[0]);
508 if (child->srv) {
509 /* only exists if we created a master CLI listener */
510 srv_drop(child->srv);
511 }
512 LIST_DELETE(&child->list);
513 mworker_free_child(child);
514 }
515 }
516}
517
518
William Lallemand88dc7c52019-04-01 11:30:01 +0200519/* Displays workers and processes */
520static int cli_io_handler_show_proc(struct appctx *appctx)
521{
Willy Tarreauc12b3212022-05-27 11:08:15 +0200522 struct stconn *sc = appctx_sc(appctx);
William Lallemand88dc7c52019-04-01 11:30:01 +0200523 struct mworker_proc *child;
524 int old = 0;
525 int up = now.tv_sec - proc_self->timestamp;
William Lallemande8669fc2019-06-12 18:21:17 +0200526 char *uptime = NULL;
William Lallemand68836742021-11-10 10:49:06 +0100527 char *reloadtxt = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200528
Willy Tarreau475e4632022-05-27 10:26:46 +0200529 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
William Lallemand88dc7c52019-04-01 11:30:01 +0200530 return 1;
531
532 chunk_reset(&trash);
533
William Lallemand68836742021-11-10 10:49:06 +0100534 memprintf(&reloadtxt, "%d [failed: %d]", proc_self->reloads, proc_self->failedreloads);
William Lallemand5d71a6b2021-11-09 15:25:31 +0100535 chunk_printf(&trash, "#%-14s %-15s %-15s %-15s %-15s\n", "<PID>", "<type>", "<reloads>", "<uptime>", "<version>");
William Lallemande8669fc2019-06-12 18:21:17 +0200536 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand68836742021-11-10 10:49:06 +0100537 chunk_appendf(&trash, "%-15u %-15s %-15s %-15s %-15s\n", (unsigned int)getpid(), "master", reloadtxt, uptime, haproxy_version);
538 ha_free(&reloadtxt);
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100539 ha_free(&uptime);
William Lallemand88dc7c52019-04-01 11:30:01 +0200540
541 /* displays current processes */
542
543 chunk_appendf(&trash, "# workers\n");
544 list_for_each_entry(child, &proc_list, list) {
545 up = now.tv_sec - child->timestamp;
546
William Lallemand8f7069a2019-04-12 16:09:23 +0200547 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200548 continue;
549
William Lallemand45286112019-04-12 16:09:21 +0200550 if (child->options & PROC_O_LEAVING) {
William Lallemand88dc7c52019-04-01 11:30:01 +0200551 old++;
552 continue;
553 }
William Lallemande8669fc2019-06-12 18:21:17 +0200554 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand5d71a6b2021-11-09 15:25:31 +0100555 chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, "worker", child->reloads, uptime, child->version);
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100556 ha_free(&uptime);
William Lallemand88dc7c52019-04-01 11:30:01 +0200557 }
558
559 /* displays old processes */
560
561 if (old) {
562 char *msg = NULL;
563
564 chunk_appendf(&trash, "# old workers\n");
565 list_for_each_entry(child, &proc_list, list) {
566 up = now.tv_sec - child->timestamp;
567
William Lallemand8f7069a2019-04-12 16:09:23 +0200568 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200569 continue;
570
William Lallemand45286112019-04-12 16:09:21 +0200571 if (child->options & PROC_O_LEAVING) {
William Lallemande8669fc2019-06-12 18:21:17 +0200572 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand5d71a6b2021-11-09 15:25:31 +0100573 chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, "worker", child->reloads, uptime, child->version);
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100574 ha_free(&uptime);
William Lallemand88dc7c52019-04-01 11:30:01 +0200575 }
576 }
577 free(msg);
578 }
579
William Lallemandad53d6d2019-04-01 11:30:03 +0200580 /* displays external process */
581 chunk_appendf(&trash, "# programs\n");
582 old = 0;
583 list_for_each_entry(child, &proc_list, list) {
584 up = now.tv_sec - child->timestamp;
585
William Lallemand8f7069a2019-04-12 16:09:23 +0200586 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200587 continue;
588
William Lallemand45286112019-04-12 16:09:21 +0200589 if (child->options & PROC_O_LEAVING) {
William Lallemandad53d6d2019-04-01 11:30:03 +0200590 old++;
591 continue;
592 }
William Lallemande8669fc2019-06-12 18:21:17 +0200593 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand5d71a6b2021-11-09 15:25:31 +0100594 chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, child->id, child->reloads, uptime, "-");
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100595 ha_free(&uptime);
William Lallemandad53d6d2019-04-01 11:30:03 +0200596 }
597
598 if (old) {
599 chunk_appendf(&trash, "# old programs\n");
600 list_for_each_entry(child, &proc_list, list) {
601 up = now.tv_sec - child->timestamp;
602
William Lallemand8f7069a2019-04-12 16:09:23 +0200603 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200604 continue;
605
William Lallemand45286112019-04-12 16:09:21 +0200606 if (child->options & PROC_O_LEAVING) {
William Lallemande8669fc2019-06-12 18:21:17 +0200607 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand5d71a6b2021-11-09 15:25:31 +0100608 chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, child->id, child->reloads, uptime, "-");
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100609 ha_free(&uptime);
William Lallemandad53d6d2019-04-01 11:30:03 +0200610 }
611 }
612 }
613
614
615
Willy Tarreaud0a06d52022-05-18 15:07:19 +0200616 if (applet_putchk(appctx, &trash) == -1)
William Lallemand88dc7c52019-04-01 11:30:01 +0200617 return 0;
William Lallemand88dc7c52019-04-01 11:30:01 +0200618
619 /* dump complete */
620 return 1;
William Lallemand9001ce82019-04-01 11:29:57 +0200621}
William Lallemand88dc7c52019-04-01 11:30:01 +0200622
623/* reload the master process */
624static int cli_parse_reload(char **args, char *payload, struct appctx *appctx, void *private)
625{
626 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
627 return 1;
628
629 mworker_reload();
630
631 return 1;
632}
633
634
William Lallemand27edc4b2019-05-07 17:49:33 +0200635static int mworker_parse_global_max_reloads(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +0100636 const struct proxy *defpx, const char *file, int linenum, char **err)
William Lallemand27edc4b2019-05-07 17:49:33 +0200637{
638
639 int err_code = 0;
640
641 if (alertif_too_many_args(1, file, linenum, args, &err_code))
642 goto out;
643
644 if (*(args[1]) == 0) {
645 memprintf(err, "%sparsing [%s:%d] : '%s' expects an integer argument.\n", *err, file, linenum, args[0]);
646 err_code |= ERR_ALERT | ERR_FATAL;
647 goto out;
648 }
649
650 max_reloads = atol(args[1]);
651 if (max_reloads < 0) {
652 memprintf(err, "%sparsing [%s:%d] '%s' : invalid value %d, must be >= 0", *err, file, linenum, args[0], max_reloads);
653 err_code |= ERR_ALERT | ERR_FATAL;
654 goto out;
655 }
656
657out:
658 return err_code;
659}
660
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200661void mworker_free_child(struct mworker_proc *child)
662{
William Lallemand08cb9452022-01-27 15:33:40 +0100663 int i;
664
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200665 if (child == NULL)
666 return;
667
William Lallemand08cb9452022-01-27 15:33:40 +0100668 for (i = 0; child->command && child->command[i]; i++)
669 ha_free(&child->command[i]);
William Lallemande8669fc2019-06-12 18:21:17 +0200670
William Lallemand08cb9452022-01-27 15:33:40 +0100671 ha_free(&child->command);
672 ha_free(&child->id);
673 ha_free(&child->version);
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200674 free(child);
675}
William Lallemand27edc4b2019-05-07 17:49:33 +0200676
677static struct cfg_kw_list mworker_kws = {{ }, {
678 { CFG_GLOBAL, "mworker-max-reloads", mworker_parse_global_max_reloads },
679 { 0, NULL, NULL },
680}};
681
682INITCALL1(STG_REGISTER, cfg_register_keywords, &mworker_kws);
683
684
William Lallemand88dc7c52019-04-01 11:30:01 +0200685/* register cli keywords */
686static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau23c740e2021-05-09 22:49:44 +0200687 { { "@<relative pid>", NULL }, "@<relative pid> : send a command to the <relative pid> process", NULL, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
688 { { "@!<pid>", NULL }, "@!<pid> : send a command to the <pid> process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
689 { { "@master", NULL }, "@master : send a command to the master process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
690 { { "show", "proc", NULL }, "show proc : show processes status", cli_parse_default, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
691 { { "reload", NULL }, "reload : reload haproxy", cli_parse_reload, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
William Lallemand88dc7c52019-04-01 11:30:01 +0200692 {{},}
693}};
694
695INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);