blob: 2bc3b776f14646b3ccb78f40e1ed32f4d4ca2f8d [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 Tarreau745e98c2021-05-08 13:58:19 +020041#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020042#include <haproxy/version.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +020043
William Lallemand48dfbbd2019-04-01 11:29:53 +020044
William Lallemande25473c2019-04-01 11:29:56 +020045static int exitcode = -1;
William Lallemand27edc4b2019-05-07 17:49:33 +020046static int max_reloads = -1; /* number max of reloads a worker can have until they are killed */
Willy Tarreau15f9ac32021-05-08 12:30:50 +020047struct mworker_proc *proc_self = NULL; /* process structure of current process */
William Lallemande25473c2019-04-01 11:29:56 +020048
William Lallemande25473c2019-04-01 11:29:56 +020049/* ----- children processes handling ----- */
William Lallemand48dfbbd2019-04-01 11:29:53 +020050
William Lallemand48dfbbd2019-04-01 11:29:53 +020051/*
William Lallemande25473c2019-04-01 11:29:56 +020052 * Send signal to every known children.
53 */
54
55static void mworker_kill(int sig)
56{
William Lallemand3f128872019-04-01 11:29:59 +020057 struct mworker_proc *child;
William Lallemande25473c2019-04-01 11:29:56 +020058
William Lallemand3f128872019-04-01 11:29:59 +020059 list_for_each_entry(child, &proc_list, list) {
60 /* 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 +020061 if ((child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG)) && (child->pid > 0))
William Lallemand3f128872019-04-01 11:29:59 +020062 kill(child->pid, sig);
William Lallemande25473c2019-04-01 11:29:56 +020063 }
64}
65
William Lallemand27edc4b2019-05-07 17:49:33 +020066void mworker_kill_max_reloads(int sig)
67{
68 struct mworker_proc *child;
69
70 list_for_each_entry(child, &proc_list, list) {
71 if (max_reloads != -1 && (child->options & PROC_O_TYPE_WORKER) &&
72 (child->pid > 0) && (child->reloads > max_reloads))
73 kill(child->pid, sig);
74 }
75}
William Lallemande25473c2019-04-01 11:29:56 +020076
77/* return 1 if a pid is a current child otherwise 0 */
William Lallemand3f128872019-04-01 11:29:59 +020078int mworker_current_child(int pid)
William Lallemande25473c2019-04-01 11:29:56 +020079{
William Lallemand3f128872019-04-01 11:29:59 +020080 struct mworker_proc *child;
William Lallemande25473c2019-04-01 11:29:56 +020081
William Lallemand3f128872019-04-01 11:29:59 +020082 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +020083 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 +020084 return 1;
85 }
86 return 0;
87}
88
William Lallemand3f128872019-04-01 11:29:59 +020089/*
90 * Return the number of new and old children (including workers and external
91 * processes)
92 */
93int mworker_child_nb()
94{
95 struct mworker_proc *child;
96 int ret = 0;
97
98 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +020099 if (child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG))
William Lallemand3f128872019-04-01 11:29:59 +0200100 ret++;
101 }
102
103 return ret;
104}
105
106
William Lallemande25473c2019-04-01 11:29:56 +0200107/*
William Lallemand48dfbbd2019-04-01 11:29:53 +0200108 * serialize the proc list and put it in the environment
109 */
110void mworker_proc_list_to_env()
111{
112 char *msg = NULL;
113 struct mworker_proc *child;
114
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 Lallemand48dfbbd2019-04-01 11:29:53 +0200125 if (child->pid > -1)
William Lallemand68836742021-11-10 10:49:06 +0100126 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 +0200127 }
128 if (msg)
129 setenv("HAPROXY_PROCESSES", msg, 1);
130}
131
William Lallemand56be0e02022-01-28 21:11:41 +0100132struct mworker_proc *mworker_proc_new()
133{
134 struct mworker_proc *child;
135
136 child = calloc(1, sizeof(*child));
137 if (!child)
138 return NULL;
139
140 child->failedreloads = 0;
141 child->reloads = 0;
142 child->pid = -1;
143 child->ipc_fd[0] = -1;
144 child->ipc_fd[1] = -1;
145 child->timestamp = -1;
146
147 return child;
148}
149
150
William Lallemand48dfbbd2019-04-01 11:29:53 +0200151/*
152 * unserialize the proc list from the environment
153 */
Remi Tricot-Le Breton1f4fa902021-05-19 10:45:12 +0200154int mworker_env_to_proc_list()
William Lallemand48dfbbd2019-04-01 11:29:53 +0200155{
156 char *msg, *token = NULL, *s1;
William Lallemand90034bb2021-11-10 11:26:14 +0100157 struct mworker_proc *child;
158 int minreloads = INT_MAX; /* minimum number of reloads to chose which processes are "current" ones */
William Lallemand48dfbbd2019-04-01 11:29:53 +0200159
160 msg = getenv("HAPROXY_PROCESSES");
161 if (!msg)
Remi Tricot-Le Breton1f4fa902021-05-19 10:45:12 +0200162 return 0;
William Lallemand48dfbbd2019-04-01 11:29:53 +0200163
164 while ((token = strtok_r(msg, "|", &s1))) {
William Lallemand48dfbbd2019-04-01 11:29:53 +0200165 char *subtoken = NULL;
166 char *s2;
167
168 msg = NULL;
169
William Lallemand56be0e02022-01-28 21:11:41 +0100170 child = mworker_proc_new();
Remi Tricot-Le Breton1f4fa902021-05-19 10:45:12 +0200171 if (!child) {
172 ha_alert("Out of memory while trying to allocate a worker process structure.");
173 return -1;
174 }
William Lallemand48dfbbd2019-04-01 11:29:53 +0200175
176 while ((subtoken = strtok_r(token, ";", &s2))) {
177
178 token = NULL;
179
180 if (strncmp(subtoken, "type=", 5) == 0) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200181 char type;
182
183 type = *(subtoken+5);
184 if (type == 'm') { /* we are in the master, assign it */
William Lallemand48dfbbd2019-04-01 11:29:53 +0200185 proc_self = child;
William Lallemand8f7069a2019-04-12 16:09:23 +0200186 child->options |= PROC_O_TYPE_MASTER;
187 } else if (type == 'e') {
188 child->options |= PROC_O_TYPE_PROG;
189 } else if (type == 'w') {
190 child->options |= PROC_O_TYPE_WORKER;
191 }
192
William Lallemand48dfbbd2019-04-01 11:29:53 +0200193 } else if (strncmp(subtoken, "fd=", 3) == 0) {
194 child->ipc_fd[0] = atoi(subtoken+3);
195 } else if (strncmp(subtoken, "pid=", 4) == 0) {
196 child->pid = atoi(subtoken+4);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200197 } else if (strncmp(subtoken, "reloads=", 8) == 0) {
William Lallemandad221f42021-11-09 18:43:59 +0100198 /* we only increment the number of asked reload */
199 child->reloads = atoi(subtoken+8);
William Lallemand90034bb2021-11-10 11:26:14 +0100200
201 if (child->reloads < minreloads)
202 minreloads = child->reloads;
William Lallemand68836742021-11-10 10:49:06 +0100203 } else if (strncmp(subtoken, "failedreloads=", 14) == 0) {
204 child->failedreloads = atoi(subtoken+14);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200205 } else if (strncmp(subtoken, "timestamp=", 10) == 0) {
206 child->timestamp = atoi(subtoken+10);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200207 } else if (strncmp(subtoken, "id=", 3) == 0) {
208 child->id = strdup(subtoken+3);
William Lallemand1dc69632019-06-12 19:11:33 +0200209 } else if (strncmp(subtoken, "version=", 8) == 0) {
210 child->version = strdup(subtoken+8);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200211 }
212 }
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200213 if (child->pid) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200214 LIST_APPEND(&proc_list, &child->list);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200215 } else {
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200216 mworker_free_child(child);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200217 }
William Lallemand48dfbbd2019-04-01 11:29:53 +0200218 }
219
William Lallemand90034bb2021-11-10 11:26:14 +0100220 /* set the leaving processes once we know which number of reloads are the current processes */
221
222 list_for_each_entry(child, &proc_list, list) {
223 if (child->reloads > minreloads)
224 child->options |= PROC_O_LEAVING;
225 }
226
William Lallemand48dfbbd2019-04-01 11:29:53 +0200227 unsetenv("HAPROXY_PROCESSES");
Remi Tricot-Le Breton1f4fa902021-05-19 10:45:12 +0200228
229 return 0;
William Lallemand48dfbbd2019-04-01 11:29:53 +0200230}
William Lallemand3cd95d22019-04-01 11:29:54 +0200231
232/* Signal blocking and unblocking */
233
234void mworker_block_signals()
235{
236 sigset_t set;
237
238 sigemptyset(&set);
239 sigaddset(&set, SIGUSR1);
240 sigaddset(&set, SIGUSR2);
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100241 sigaddset(&set, SIGTTIN);
242 sigaddset(&set, SIGTTOU);
William Lallemand3cd95d22019-04-01 11:29:54 +0200243 sigaddset(&set, SIGHUP);
244 sigaddset(&set, SIGCHLD);
245 ha_sigmask(SIG_SETMASK, &set, NULL);
246}
247
248void mworker_unblock_signals()
249{
250 haproxy_unblock_signals();
251}
William Lallemand3fa724d2019-04-01 11:29:55 +0200252
William Lallemande25473c2019-04-01 11:29:56 +0200253/* ----- mworker signal handlers ----- */
254
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100255/* broadcast the configured signal to the workers */
256void mworker_broadcast_signal(struct sig_handler *sh)
257{
258 mworker_kill(sh->arg);
259}
260
William Lallemande25473c2019-04-01 11:29:56 +0200261/*
262 * When called, this function reexec haproxy with -sf followed by current
263 * children PIDs and possibly old children PIDs if they didn't leave yet.
264 */
265void mworker_catch_sighup(struct sig_handler *sh)
266{
267 mworker_reload();
268}
269
270void mworker_catch_sigterm(struct sig_handler *sh)
271{
272 int sig = sh->arg;
273
274#if defined(USE_SYSTEMD)
275 if (global.tune.options & GTUNE_USE_SYSTEMD) {
276 sd_notify(0, "STOPPING=1");
277 }
278#endif
279 ha_warning("Exiting Master process...\n");
280 mworker_kill(sig);
281}
282
283/*
284 * Wait for every children to exit
285 */
286
287void mworker_catch_sigchld(struct sig_handler *sh)
288{
289 int exitpid = -1;
290 int status = 0;
William Lallemande25473c2019-04-01 11:29:56 +0200291 int childfound;
292
293restart_wait:
294
295 childfound = 0;
296
297 exitpid = waitpid(-1, &status, WNOHANG);
298 if (exitpid > 0) {
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200299 struct mworker_proc *child, *it;
300
William Lallemande25473c2019-04-01 11:29:56 +0200301 if (WIFEXITED(status))
302 status = WEXITSTATUS(status);
303 else if (WIFSIGNALED(status))
304 status = 128 + WTERMSIG(status);
305 else if (WIFSTOPPED(status))
306 status = 128 + WSTOPSIG(status);
307 else
308 status = 255;
309
William Lallemand3f128872019-04-01 11:29:59 +0200310 /* delete the child from the process list */
William Lallemande25473c2019-04-01 11:29:56 +0200311 list_for_each_entry_safe(child, it, &proc_list, list) {
312 if (child->pid != exitpid)
313 continue;
314
Willy Tarreau2b718102021-04-21 07:32:39 +0200315 LIST_DELETE(&child->list);
William Lallemande25473c2019-04-01 11:29:56 +0200316 close(child->ipc_fd[0]);
317 childfound = 1;
318 break;
319 }
320
William Lallemand3f128872019-04-01 11:29:59 +0200321 if (!childfound) {
322 /* 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 +0200323 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 +0200324 } else {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200325 /* check if exited child is a current child */
William Lallemand45286112019-04-12 16:09:21 +0200326 if (!(child->options & PROC_O_LEAVING)) {
William Lallemanda655ba42020-05-06 17:27:03 +0200327 if (child->options & PROC_O_TYPE_WORKER) {
328 if (status < 128)
William Lallemand5d71a6b2021-11-09 15:25:31 +0100329 ha_warning("Current worker (%d) exited with code %d (%s)\n", exitpid, status, "Exit");
William Lallemanda655ba42020-05-06 17:27:03 +0200330 else
William Lallemand5d71a6b2021-11-09 15:25:31 +0100331 ha_alert("Current worker (%d) exited with code %d (%s)\n", exitpid, status, strsignal(status - 128));
William Lallemanda655ba42020-05-06 17:27:03 +0200332 }
William Lallemand8f7069a2019-04-12 16:09:23 +0200333 else if (child->options & PROC_O_TYPE_PROG)
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200334 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 +0200335
William Lallemande25473c2019-04-01 11:29:56 +0200336 if (status != 0 && status != 130 && status != 143
337 && !(global.tune.options & GTUNE_NOEXIT_ONFAILURE)) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200338 ha_alert("exit-on-failure: killing every processes with SIGTERM\n");
William Lallemande25473c2019-04-01 11:29:56 +0200339 mworker_kill(SIGTERM);
340 }
William Lallemand74f0ec32019-04-16 17:42:44 +0200341 /* 0 & SIGTERM (143) are normal, but we should report SIGINT (130) and other signals */
342 if (exitcode < 0 && status != 0 && status != 143)
343 exitcode = status;
William Lallemande25473c2019-04-01 11:29:56 +0200344 } else {
William Lallemand8f7069a2019-04-12 16:09:23 +0200345 if (child->options & PROC_O_TYPE_WORKER) {
William Lallemand5d71a6b2021-11-09 15:25:31 +0100346 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 +0200347 delete_oldpid(exitpid);
William Lallemand8f7069a2019-04-12 16:09:23 +0200348 } else if (child->options & PROC_O_TYPE_PROG) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200349 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 +0200350 }
William Lallemande25473c2019-04-01 11:29:56 +0200351 }
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200352 mworker_free_child(child);
353 child = NULL;
William Lallemande25473c2019-04-01 11:29:56 +0200354 }
355
356 /* do it again to check if it was the last worker */
357 goto restart_wait;
358 }
359 /* Better rely on the system than on a list of process to check if it was the last one */
360 else if (exitpid == -1 && errno == ECHILD) {
William Lallemand4cf4b332019-04-16 17:42:43 +0200361 ha_warning("All workers exited. Exiting... (%d)\n", (exitcode > 0) ? exitcode : EXIT_SUCCESS);
William Lallemande25473c2019-04-01 11:29:56 +0200362 atexit_flag = 0;
363 if (exitcode > 0)
William Lallemand4cf4b332019-04-16 17:42:43 +0200364 exit(exitcode); /* parent must leave using the status code that provoked the exit */
365 exit(EXIT_SUCCESS);
William Lallemande25473c2019-04-01 11:29:56 +0200366 }
367
368}
369
William Lallemand3fa724d2019-04-01 11:29:55 +0200370/* ----- IPC FD (sockpair) related ----- */
371
372/* This wrapper is called from the workers. It is registered instead of the
373 * normal listener_accept() so the worker can exit() when it detects that the
374 * master closed the IPC FD. If it's not a close, we just call the regular
Willy Tarreaua74cb382020-10-15 21:29:49 +0200375 * listener_accept() function.
376 */
William Lallemand3fa724d2019-04-01 11:29:55 +0200377void mworker_accept_wrapper(int fd)
378{
379 char c;
380 int ret;
381
382 while (1) {
383 ret = recv(fd, &c, 1, MSG_PEEK);
384 if (ret == -1) {
385 if (errno == EINTR)
386 continue;
387 if (errno == EAGAIN) {
388 fd_cant_recv(fd);
389 return;
390 }
391 break;
392 } else if (ret > 0) {
Willy Tarreaua74cb382020-10-15 21:29:49 +0200393 struct listener *l = fdtab[fd].owner;
394
395 if (l)
396 listener_accept(l);
William Lallemand3fa724d2019-04-01 11:29:55 +0200397 return;
398 } else if (ret == 0) {
399 /* At this step the master is down before
400 * this worker perform a 'normal' exit.
401 * So we want to exit with an error but
402 * other threads could currently process
403 * some stuff so we can't perform a clean
404 * deinit().
405 */
406 exit(EXIT_FAILURE);
407 }
408 }
409 return;
410}
411
412/*
Willy Tarreau619a95f2019-05-20 11:12:15 +0200413 * This function registers the accept wrapper for the sockpair of the master
414 * worker. It's only handled by worker thread #0. Other threads and master do
415 * nothing here. It always returns 1 (success).
William Lallemand3fa724d2019-04-01 11:29:55 +0200416 */
Willy Tarreau619a95f2019-05-20 11:12:15 +0200417static int mworker_pipe_register_per_thread()
William Lallemand3fa724d2019-04-01 11:29:55 +0200418{
Willy Tarreau619a95f2019-05-20 11:12:15 +0200419 if (!(global.mode & MODE_MWORKER) || master)
420 return 1;
421
422 if (tid != 0)
423 return 1;
William Lallemand3fa724d2019-04-01 11:29:55 +0200424
425 fcntl(proc_self->ipc_fd[1], F_SETFL, O_NONBLOCK);
426 /* In multi-tread, we need only one thread to process
427 * events on the pipe with master
428 */
Willy Tarreau619a95f2019-05-20 11:12:15 +0200429 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 +0200430 fd_want_recv(proc_self->ipc_fd[1]);
Willy Tarreau619a95f2019-05-20 11:12:15 +0200431 return 1;
William Lallemand3fa724d2019-04-01 11:29:55 +0200432}
William Lallemand9001ce82019-04-01 11:29:57 +0200433
Willy Tarreau619a95f2019-05-20 11:12:15 +0200434REGISTER_PER_THREAD_INIT(mworker_pipe_register_per_thread);
435
William Lallemand9001ce82019-04-01 11:29:57 +0200436/* ----- proxies ----- */
437/*
438 * Upon a reload, the master worker needs to close all listeners FDs but the mworker_pipe
439 * fd, and the FD provided by fd@
440 */
441void mworker_cleanlisteners()
442{
443 struct listener *l, *l_next;
444 struct proxy *curproxy;
445 struct peers *curpeers;
446
447 /* we might have to unbind some peers sections from some processes */
448 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
449 if (!curpeers->peers_fe)
450 continue;
451
452 stop_proxy(curpeers->peers_fe);
453 /* disable this peer section so that it kills itself */
454 signal_unregister_handler(curpeers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200455 task_destroy(curpeers->sync_task);
William Lallemand9001ce82019-04-01 11:29:57 +0200456 curpeers->sync_task = NULL;
Olivier Houchard3f795f72019-04-17 22:51:06 +0200457 task_destroy(curpeers->peers_fe->task);
William Lallemand9001ce82019-04-01 11:29:57 +0200458 curpeers->peers_fe->task = NULL;
459 curpeers->peers_fe = NULL;
460 }
461
462 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
463 int listen_in_master = 0;
464
465 list_for_each_entry_safe(l, l_next, &curproxy->conf.listeners, by_fe) {
466 /* remove the listener, but not those we need in the master... */
Willy Tarreau18c20d22020-10-09 16:11:46 +0200467 if (!(l->rx.flags & RX_F_MWORKER)) {
Willy Tarreau75c98d12020-10-09 15:55:23 +0200468 unbind_listener(l);
William Lallemand9001ce82019-04-01 11:29:57 +0200469 delete_listener(l);
470 } else {
471 listen_in_master = 1;
472 }
473 }
474 /* if the proxy shouldn't be in the master, we stop it */
475 if (!listen_in_master)
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200476 curproxy->flags |= PR_FL_DISABLED;
William Lallemand9001ce82019-04-01 11:29:57 +0200477 }
William Lallemand88dc7c52019-04-01 11:30:01 +0200478}
479
William Lallemand55a921c2022-01-28 21:17:30 +0100480/* Upon a configuration loading error some mworker_proc and FDs/server were
481 * assigned but the worker was never forked, we must close the FDs and
482 * remove the server
483 */
484void mworker_cleanup_proc()
485{
486 struct mworker_proc *child, *it;
487
488 list_for_each_entry_safe(child, it, &proc_list, list) {
489
490 if (child->pid == -1) {
491 /* Close the socketpair master side. We don't need to
492 * close the worker side, because it's stored in the
493 * GLOBAL cli listener which was supposed to be in the
494 * worker and which will be closed in
495 * mworker_cleanlisteners()
496 */
497 if (child->ipc_fd[0] > -1)
498 close(child->ipc_fd[0]);
499 if (child->srv) {
500 /* only exists if we created a master CLI listener */
501 srv_drop(child->srv);
502 }
503 LIST_DELETE(&child->list);
504 mworker_free_child(child);
505 }
506 }
507}
508
509
William Lallemand88dc7c52019-04-01 11:30:01 +0200510/* Displays workers and processes */
511static int cli_io_handler_show_proc(struct appctx *appctx)
512{
Christopher Faulet908628c2022-03-25 16:43:49 +0100513 struct conn_stream *cs = appctx->owner;
William Lallemand88dc7c52019-04-01 11:30:01 +0200514 struct mworker_proc *child;
515 int old = 0;
516 int up = now.tv_sec - proc_self->timestamp;
William Lallemande8669fc2019-06-12 18:21:17 +0200517 char *uptime = NULL;
William Lallemand68836742021-11-10 10:49:06 +0100518 char *reloadtxt = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200519
Christopher Faulet908628c2022-03-25 16:43:49 +0100520 if (unlikely(cs_ic(cs)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
William Lallemand88dc7c52019-04-01 11:30:01 +0200521 return 1;
522
523 chunk_reset(&trash);
524
William Lallemand68836742021-11-10 10:49:06 +0100525 memprintf(&reloadtxt, "%d [failed: %d]", proc_self->reloads, proc_self->failedreloads);
William Lallemand5d71a6b2021-11-09 15:25:31 +0100526 chunk_printf(&trash, "#%-14s %-15s %-15s %-15s %-15s\n", "<PID>", "<type>", "<reloads>", "<uptime>", "<version>");
William Lallemande8669fc2019-06-12 18:21:17 +0200527 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand68836742021-11-10 10:49:06 +0100528 chunk_appendf(&trash, "%-15u %-15s %-15s %-15s %-15s\n", (unsigned int)getpid(), "master", reloadtxt, uptime, haproxy_version);
529 ha_free(&reloadtxt);
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100530 ha_free(&uptime);
William Lallemand88dc7c52019-04-01 11:30:01 +0200531
532 /* displays current processes */
533
534 chunk_appendf(&trash, "# workers\n");
535 list_for_each_entry(child, &proc_list, list) {
536 up = now.tv_sec - child->timestamp;
537
William Lallemand8f7069a2019-04-12 16:09:23 +0200538 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200539 continue;
540
William Lallemand45286112019-04-12 16:09:21 +0200541 if (child->options & PROC_O_LEAVING) {
William Lallemand88dc7c52019-04-01 11:30:01 +0200542 old++;
543 continue;
544 }
William Lallemande8669fc2019-06-12 18:21:17 +0200545 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand5d71a6b2021-11-09 15:25:31 +0100546 chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, "worker", child->reloads, uptime, child->version);
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100547 ha_free(&uptime);
William Lallemand88dc7c52019-04-01 11:30:01 +0200548 }
549
550 /* displays old processes */
551
552 if (old) {
553 char *msg = NULL;
554
555 chunk_appendf(&trash, "# old workers\n");
556 list_for_each_entry(child, &proc_list, list) {
557 up = now.tv_sec - child->timestamp;
558
William Lallemand8f7069a2019-04-12 16:09:23 +0200559 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200560 continue;
561
William Lallemand45286112019-04-12 16:09:21 +0200562 if (child->options & PROC_O_LEAVING) {
William Lallemande8669fc2019-06-12 18:21:17 +0200563 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand5d71a6b2021-11-09 15:25:31 +0100564 chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, "worker", child->reloads, uptime, child->version);
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100565 ha_free(&uptime);
William Lallemand88dc7c52019-04-01 11:30:01 +0200566 }
567 }
568 free(msg);
569 }
570
William Lallemandad53d6d2019-04-01 11:30:03 +0200571 /* displays external process */
572 chunk_appendf(&trash, "# programs\n");
573 old = 0;
574 list_for_each_entry(child, &proc_list, list) {
575 up = now.tv_sec - child->timestamp;
576
William Lallemand8f7069a2019-04-12 16:09:23 +0200577 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200578 continue;
579
William Lallemand45286112019-04-12 16:09:21 +0200580 if (child->options & PROC_O_LEAVING) {
William Lallemandad53d6d2019-04-01 11:30:03 +0200581 old++;
582 continue;
583 }
William Lallemande8669fc2019-06-12 18:21:17 +0200584 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand5d71a6b2021-11-09 15:25:31 +0100585 chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, child->id, child->reloads, uptime, "-");
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100586 ha_free(&uptime);
William Lallemandad53d6d2019-04-01 11:30:03 +0200587 }
588
589 if (old) {
590 chunk_appendf(&trash, "# old programs\n");
591 list_for_each_entry(child, &proc_list, list) {
592 up = now.tv_sec - child->timestamp;
593
William Lallemand8f7069a2019-04-12 16:09:23 +0200594 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200595 continue;
596
William Lallemand45286112019-04-12 16:09:21 +0200597 if (child->options & PROC_O_LEAVING) {
William Lallemande8669fc2019-06-12 18:21:17 +0200598 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand5d71a6b2021-11-09 15:25:31 +0100599 chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, child->id, child->reloads, uptime, "-");
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100600 ha_free(&uptime);
William Lallemandad53d6d2019-04-01 11:30:03 +0200601 }
602 }
603 }
604
605
606
Christopher Faulet908628c2022-03-25 16:43:49 +0100607 if (ci_putchk(cs_ic(cs), &trash) == -1) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200608 cs_rx_room_blk(cs);
William Lallemand88dc7c52019-04-01 11:30:01 +0200609 return 0;
610 }
611
612 /* dump complete */
613 return 1;
William Lallemand9001ce82019-04-01 11:29:57 +0200614}
William Lallemand88dc7c52019-04-01 11:30:01 +0200615
616/* reload the master process */
617static int cli_parse_reload(char **args, char *payload, struct appctx *appctx, void *private)
618{
619 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
620 return 1;
621
622 mworker_reload();
623
624 return 1;
625}
626
627
William Lallemand27edc4b2019-05-07 17:49:33 +0200628static int mworker_parse_global_max_reloads(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +0100629 const struct proxy *defpx, const char *file, int linenum, char **err)
William Lallemand27edc4b2019-05-07 17:49:33 +0200630{
631
632 int err_code = 0;
633
634 if (alertif_too_many_args(1, file, linenum, args, &err_code))
635 goto out;
636
637 if (*(args[1]) == 0) {
638 memprintf(err, "%sparsing [%s:%d] : '%s' expects an integer argument.\n", *err, file, linenum, args[0]);
639 err_code |= ERR_ALERT | ERR_FATAL;
640 goto out;
641 }
642
643 max_reloads = atol(args[1]);
644 if (max_reloads < 0) {
645 memprintf(err, "%sparsing [%s:%d] '%s' : invalid value %d, must be >= 0", *err, file, linenum, args[0], max_reloads);
646 err_code |= ERR_ALERT | ERR_FATAL;
647 goto out;
648 }
649
650out:
651 return err_code;
652}
653
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200654void mworker_free_child(struct mworker_proc *child)
655{
William Lallemand08cb9452022-01-27 15:33:40 +0100656 int i;
657
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200658 if (child == NULL)
659 return;
660
William Lallemand08cb9452022-01-27 15:33:40 +0100661 for (i = 0; child->command && child->command[i]; i++)
662 ha_free(&child->command[i]);
William Lallemande8669fc2019-06-12 18:21:17 +0200663
William Lallemand08cb9452022-01-27 15:33:40 +0100664 ha_free(&child->command);
665 ha_free(&child->id);
666 ha_free(&child->version);
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200667 free(child);
668}
William Lallemand27edc4b2019-05-07 17:49:33 +0200669
670static struct cfg_kw_list mworker_kws = {{ }, {
671 { CFG_GLOBAL, "mworker-max-reloads", mworker_parse_global_max_reloads },
672 { 0, NULL, NULL },
673}};
674
675INITCALL1(STG_REGISTER, cfg_register_keywords, &mworker_kws);
676
677
William Lallemand88dc7c52019-04-01 11:30:01 +0200678/* register cli keywords */
679static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau23c740e2021-05-09 22:49:44 +0200680 { { "@<relative pid>", NULL }, "@<relative pid> : send a command to the <relative pid> process", NULL, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
681 { { "@!<pid>", NULL }, "@!<pid> : send a command to the <pid> process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
682 { { "@master", NULL }, "@master : send a command to the master process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
683 { { "show", "proc", NULL }, "show proc : show processes status", cli_parse_default, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
684 { { "reload", NULL }, "reload : reload haproxy", cli_parse_reload, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
William Lallemand88dc7c52019-04-01 11:30:01 +0200685 {{},}
686}};
687
688INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);