blob: 6415fd4a5df128095a800ba4b2679f10a74ddafb [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>
Willy Tarreau36979d92020-06-05 17:27:29 +020029#include <haproxy/errors.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020030#include <haproxy/fd.h>
31#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020032#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020033#include <haproxy/listener.h>
Willy Tarreaub5abe5b2020-06-04 14:07:37 +020034#include <haproxy/mworker.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +020035#include <haproxy/peers.h>
Willy Tarreau7c668572021-05-08 20:21:31 +020036#include <haproxy/proxy.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +020037#include <haproxy/signal.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020038#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020039#include <haproxy/stream_interface.h>
Willy 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;
113
114 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200115 char type = '?';
116
117 if (child->options & PROC_O_TYPE_MASTER)
118 type = 'm';
119 else if (child->options & PROC_O_TYPE_PROG)
120 type = 'e';
121 else if (child->options &= PROC_O_TYPE_WORKER)
122 type = 'w';
123
William Lallemand48dfbbd2019-04-01 11:29:53 +0200124 if (child->pid > -1)
William Lallemand1dc69632019-06-12 19:11:33 +0200125 memprintf(&msg, "%s|type=%c;fd=%d;pid=%d;rpid=%d;reloads=%d;timestamp=%d;id=%s;version=%s", msg ? msg : "", type, child->ipc_fd[0], child->pid, child->relative_pid, child->reloads, child->timestamp, child->id ? child->id : "", child->version);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200126 }
127 if (msg)
128 setenv("HAPROXY_PROCESSES", msg, 1);
129}
130
131/*
132 * unserialize the proc list from the environment
William Lallemandf1ff1b62023-02-21 12:44:56 +0100133 * Return < 0 upon error.
William Lallemand48dfbbd2019-04-01 11:29:53 +0200134 */
Remi Tricot-Le Bretond90aa342021-05-19 10:45:12 +0200135int mworker_env_to_proc_list()
William Lallemand48dfbbd2019-04-01 11:29:53 +0200136{
William Lallemandf1ff1b62023-02-21 12:44:56 +0100137 char *env, *msg, *omsg = NULL, *token = NULL, *s1;
138 int err = 0;
William Lallemand48dfbbd2019-04-01 11:29:53 +0200139
William Lallemandf1ff1b62023-02-21 12:44:56 +0100140 env = getenv("HAPROXY_PROCESSES");
141 if (!env)
Remi Tricot-Le Bretond90aa342021-05-19 10:45:12 +0200142 return 0;
William Lallemand48dfbbd2019-04-01 11:29:53 +0200143
William Lallemandf1ff1b62023-02-21 12:44:56 +0100144 omsg = msg = strdup(env);
145 if (!msg) {
146 ha_alert("Out of memory while trying to allocate a worker process structure.");
147 err = -1;
148 goto out;
149 }
150
William Lallemand48dfbbd2019-04-01 11:29:53 +0200151 while ((token = strtok_r(msg, "|", &s1))) {
152 struct mworker_proc *child;
153 char *subtoken = NULL;
154 char *s2;
155
156 msg = NULL;
157
158 child = calloc(1, sizeof(*child));
Remi Tricot-Le Bretond90aa342021-05-19 10:45:12 +0200159 if (!child) {
William Lallemandf1ff1b62023-02-21 12:44:56 +0100160 ha_alert("out of memory while trying to allocate a worker process structure.");
161 err = -1;
162 goto out;
Remi Tricot-Le Bretond90aa342021-05-19 10:45:12 +0200163 }
William Lallemand48dfbbd2019-04-01 11:29:53 +0200164
165 while ((subtoken = strtok_r(token, ";", &s2))) {
166
167 token = NULL;
168
169 if (strncmp(subtoken, "type=", 5) == 0) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200170 char type;
171
172 type = *(subtoken+5);
173 if (type == 'm') { /* we are in the master, assign it */
William Lallemand48dfbbd2019-04-01 11:29:53 +0200174 proc_self = child;
William Lallemand8f7069a2019-04-12 16:09:23 +0200175 child->options |= PROC_O_TYPE_MASTER;
176 } else if (type == 'e') {
177 child->options |= PROC_O_TYPE_PROG;
178 } else if (type == 'w') {
179 child->options |= PROC_O_TYPE_WORKER;
180 }
181
William Lallemand48dfbbd2019-04-01 11:29:53 +0200182 } else if (strncmp(subtoken, "fd=", 3) == 0) {
183 child->ipc_fd[0] = atoi(subtoken+3);
William Lallemand9490f9d2023-06-19 17:12:58 +0200184 if (child->ipc_fd[0] > -1)
185 global.maxsock++;
William Lallemand48dfbbd2019-04-01 11:29:53 +0200186 } else if (strncmp(subtoken, "pid=", 4) == 0) {
187 child->pid = atoi(subtoken+4);
188 } else if (strncmp(subtoken, "rpid=", 5) == 0) {
189 child->relative_pid = atoi(subtoken+5);
190 } else if (strncmp(subtoken, "reloads=", 8) == 0) {
191 /* we reloaded this process once more */
192 child->reloads = atoi(subtoken+8) + 1;
193 } else if (strncmp(subtoken, "timestamp=", 10) == 0) {
194 child->timestamp = atoi(subtoken+10);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200195 } else if (strncmp(subtoken, "id=", 3) == 0) {
196 child->id = strdup(subtoken+3);
William Lallemand1dc69632019-06-12 19:11:33 +0200197 } else if (strncmp(subtoken, "version=", 8) == 0) {
198 child->version = strdup(subtoken+8);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200199 }
200 }
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200201 if (child->pid) {
William Lallemand920fc8b2019-05-14 11:15:18 +0200202 /* this is a process inherited from a reload that should be leaving */
203 child->options |= PROC_O_LEAVING;
204
Willy Tarreau2b718102021-04-21 07:32:39 +0200205 LIST_APPEND(&proc_list, &child->list);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200206 } else {
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200207 mworker_free_child(child);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200208 }
William Lallemand48dfbbd2019-04-01 11:29:53 +0200209 }
210
211 unsetenv("HAPROXY_PROCESSES");
Remi Tricot-Le Bretond90aa342021-05-19 10:45:12 +0200212
William Lallemandf1ff1b62023-02-21 12:44:56 +0100213out:
214 free(omsg);
215 return err;
William Lallemand48dfbbd2019-04-01 11:29:53 +0200216}
William Lallemand3cd95d22019-04-01 11:29:54 +0200217
218/* Signal blocking and unblocking */
219
220void mworker_block_signals()
221{
222 sigset_t set;
223
224 sigemptyset(&set);
225 sigaddset(&set, SIGUSR1);
226 sigaddset(&set, SIGUSR2);
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100227 sigaddset(&set, SIGTTIN);
228 sigaddset(&set, SIGTTOU);
William Lallemand3cd95d22019-04-01 11:29:54 +0200229 sigaddset(&set, SIGHUP);
230 sigaddset(&set, SIGCHLD);
231 ha_sigmask(SIG_SETMASK, &set, NULL);
232}
233
234void mworker_unblock_signals()
235{
236 haproxy_unblock_signals();
237}
William Lallemand3fa724d2019-04-01 11:29:55 +0200238
William Lallemande25473c2019-04-01 11:29:56 +0200239/* ----- mworker signal handlers ----- */
240
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100241/* broadcast the configured signal to the workers */
242void mworker_broadcast_signal(struct sig_handler *sh)
243{
244 mworker_kill(sh->arg);
245}
246
William Lallemande25473c2019-04-01 11:29:56 +0200247/*
248 * When called, this function reexec haproxy with -sf followed by current
249 * children PIDs and possibly old children PIDs if they didn't leave yet.
250 */
251void mworker_catch_sighup(struct sig_handler *sh)
252{
253 mworker_reload();
254}
255
256void mworker_catch_sigterm(struct sig_handler *sh)
257{
258 int sig = sh->arg;
259
260#if defined(USE_SYSTEMD)
261 if (global.tune.options & GTUNE_USE_SYSTEMD) {
262 sd_notify(0, "STOPPING=1");
263 }
264#endif
265 ha_warning("Exiting Master process...\n");
266 mworker_kill(sig);
267}
268
269/*
270 * Wait for every children to exit
271 */
272
273void mworker_catch_sigchld(struct sig_handler *sh)
274{
275 int exitpid = -1;
276 int status = 0;
William Lallemande25473c2019-04-01 11:29:56 +0200277 int childfound;
278
279restart_wait:
280
281 childfound = 0;
282
283 exitpid = waitpid(-1, &status, WNOHANG);
284 if (exitpid > 0) {
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200285 struct mworker_proc *child, *it;
286
William Lallemande25473c2019-04-01 11:29:56 +0200287 if (WIFEXITED(status))
288 status = WEXITSTATUS(status);
289 else if (WIFSIGNALED(status))
290 status = 128 + WTERMSIG(status);
291 else if (WIFSTOPPED(status))
292 status = 128 + WSTOPSIG(status);
293 else
294 status = 255;
295
William Lallemand3f128872019-04-01 11:29:59 +0200296 /* delete the child from the process list */
William Lallemande25473c2019-04-01 11:29:56 +0200297 list_for_each_entry_safe(child, it, &proc_list, list) {
298 if (child->pid != exitpid)
299 continue;
300
Willy Tarreau2b718102021-04-21 07:32:39 +0200301 LIST_DELETE(&child->list);
William Lallemande25473c2019-04-01 11:29:56 +0200302 close(child->ipc_fd[0]);
303 childfound = 1;
304 break;
305 }
306
William Lallemand3f128872019-04-01 11:29:59 +0200307 if (!childfound) {
308 /* 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 +0200309 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 +0200310 } else {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200311 /* check if exited child is a current child */
William Lallemand45286112019-04-12 16:09:21 +0200312 if (!(child->options & PROC_O_LEAVING)) {
William Lallemanda655ba42020-05-06 17:27:03 +0200313 if (child->options & PROC_O_TYPE_WORKER) {
314 if (status < 128)
315 ha_warning("Current worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, "Exit");
316 else
317 ha_alert("Current worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, strsignal(status - 128));
318 }
William Lallemand8f7069a2019-04-12 16:09:23 +0200319 else if (child->options & PROC_O_TYPE_PROG)
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200320 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 +0200321
William Lallemande25473c2019-04-01 11:29:56 +0200322 if (status != 0 && status != 130 && status != 143
323 && !(global.tune.options & GTUNE_NOEXIT_ONFAILURE)) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200324 ha_alert("exit-on-failure: killing every processes with SIGTERM\n");
William Lallemande25473c2019-04-01 11:29:56 +0200325 mworker_kill(SIGTERM);
326 }
William Lallemand74f0ec32019-04-16 17:42:44 +0200327 /* 0 & SIGTERM (143) are normal, but we should report SIGINT (130) and other signals */
328 if (exitcode < 0 && status != 0 && status != 143)
329 exitcode = status;
William Lallemande25473c2019-04-01 11:29:56 +0200330 } else {
William Lallemand8f7069a2019-04-12 16:09:23 +0200331 if (child->options & PROC_O_TYPE_WORKER) {
William Lallemand3f128872019-04-01 11:29:59 +0200332 ha_warning("Former worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
333 delete_oldpid(exitpid);
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_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 +0200336 }
William Lallemande25473c2019-04-01 11:29:56 +0200337 }
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200338 mworker_free_child(child);
339 child = NULL;
William Lallemande25473c2019-04-01 11:29:56 +0200340 }
341
342 /* do it again to check if it was the last worker */
343 goto restart_wait;
344 }
345 /* Better rely on the system than on a list of process to check if it was the last one */
346 else if (exitpid == -1 && errno == ECHILD) {
William Lallemand4cf4b332019-04-16 17:42:43 +0200347 ha_warning("All workers exited. Exiting... (%d)\n", (exitcode > 0) ? exitcode : EXIT_SUCCESS);
William Lallemande25473c2019-04-01 11:29:56 +0200348 atexit_flag = 0;
349 if (exitcode > 0)
William Lallemand4cf4b332019-04-16 17:42:43 +0200350 exit(exitcode); /* parent must leave using the status code that provoked the exit */
351 exit(EXIT_SUCCESS);
William Lallemande25473c2019-04-01 11:29:56 +0200352 }
353
354}
355
William Lallemand3fa724d2019-04-01 11:29:55 +0200356/* ----- IPC FD (sockpair) related ----- */
357
358/* This wrapper is called from the workers. It is registered instead of the
359 * normal listener_accept() so the worker can exit() when it detects that the
360 * master closed the IPC FD. If it's not a close, we just call the regular
Willy Tarreaua74cb382020-10-15 21:29:49 +0200361 * listener_accept() function.
362 */
William Lallemand3fa724d2019-04-01 11:29:55 +0200363void mworker_accept_wrapper(int fd)
364{
365 char c;
366 int ret;
367
368 while (1) {
369 ret = recv(fd, &c, 1, MSG_PEEK);
370 if (ret == -1) {
371 if (errno == EINTR)
372 continue;
373 if (errno == EAGAIN) {
374 fd_cant_recv(fd);
375 return;
376 }
377 break;
378 } else if (ret > 0) {
Willy Tarreaua74cb382020-10-15 21:29:49 +0200379 struct listener *l = fdtab[fd].owner;
380
381 if (l)
382 listener_accept(l);
William Lallemand3fa724d2019-04-01 11:29:55 +0200383 return;
384 } else if (ret == 0) {
385 /* At this step the master is down before
386 * this worker perform a 'normal' exit.
387 * So we want to exit with an error but
388 * other threads could currently process
389 * some stuff so we can't perform a clean
390 * deinit().
391 */
392 exit(EXIT_FAILURE);
393 }
394 }
395 return;
396}
397
398/*
Willy Tarreau619a95f2019-05-20 11:12:15 +0200399 * This function registers the accept wrapper for the sockpair of the master
400 * worker. It's only handled by worker thread #0. Other threads and master do
401 * nothing here. It always returns 1 (success).
William Lallemand3fa724d2019-04-01 11:29:55 +0200402 */
Willy Tarreau619a95f2019-05-20 11:12:15 +0200403static int mworker_pipe_register_per_thread()
William Lallemand3fa724d2019-04-01 11:29:55 +0200404{
Willy Tarreau619a95f2019-05-20 11:12:15 +0200405 if (!(global.mode & MODE_MWORKER) || master)
406 return 1;
407
408 if (tid != 0)
409 return 1;
William Lallemand3fa724d2019-04-01 11:29:55 +0200410
William Lallemandfa367862023-02-21 13:41:24 +0100411 if (proc_self->ipc_fd[1] < 0) /* proc_self was incomplete and we can't find the socketpair */
412 return 1;
413
William Lallemand3fa724d2019-04-01 11:29:55 +0200414 fcntl(proc_self->ipc_fd[1], F_SETFL, O_NONBLOCK);
415 /* In multi-tread, we need only one thread to process
416 * events on the pipe with master
417 */
William Lallemandf9a43a42022-07-05 00:55:09 +0200418 fdtab[proc_self->ipc_fd[1]].iocb = mworker_accept_wrapper;
William Lallemand3fa724d2019-04-01 11:29:55 +0200419 fd_want_recv(proc_self->ipc_fd[1]);
Willy Tarreau619a95f2019-05-20 11:12:15 +0200420 return 1;
William Lallemand3fa724d2019-04-01 11:29:55 +0200421}
William Lallemand9001ce82019-04-01 11:29:57 +0200422
Willy Tarreau619a95f2019-05-20 11:12:15 +0200423REGISTER_PER_THREAD_INIT(mworker_pipe_register_per_thread);
424
William Lallemand9001ce82019-04-01 11:29:57 +0200425/* ----- proxies ----- */
426/*
427 * Upon a reload, the master worker needs to close all listeners FDs but the mworker_pipe
428 * fd, and the FD provided by fd@
429 */
430void mworker_cleanlisteners()
431{
432 struct listener *l, *l_next;
433 struct proxy *curproxy;
434 struct peers *curpeers;
435
436 /* we might have to unbind some peers sections from some processes */
437 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
438 if (!curpeers->peers_fe)
439 continue;
440
441 stop_proxy(curpeers->peers_fe);
442 /* disable this peer section so that it kills itself */
William Lallemand21377a12022-12-07 15:21:24 +0100443 if (curpeers->sighandler)
444 signal_unregister_handler(curpeers->sighandler);
445 if (curpeers->sync_task)
446 task_destroy(curpeers->sync_task);
William Lallemand9001ce82019-04-01 11:29:57 +0200447 curpeers->sync_task = NULL;
Olivier Houchard3f795f72019-04-17 22:51:06 +0200448 task_destroy(curpeers->peers_fe->task);
William Lallemand9001ce82019-04-01 11:29:57 +0200449 curpeers->peers_fe->task = NULL;
450 curpeers->peers_fe = NULL;
451 }
452
453 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
454 int listen_in_master = 0;
455
456 list_for_each_entry_safe(l, l_next, &curproxy->conf.listeners, by_fe) {
457 /* remove the listener, but not those we need in the master... */
Willy Tarreau18c20d22020-10-09 16:11:46 +0200458 if (!(l->rx.flags & RX_F_MWORKER)) {
Willy Tarreau75c98d12020-10-09 15:55:23 +0200459 unbind_listener(l);
William Lallemand9001ce82019-04-01 11:29:57 +0200460 delete_listener(l);
461 } else {
462 listen_in_master = 1;
463 }
464 }
465 /* if the proxy shouldn't be in the master, we stop it */
466 if (!listen_in_master)
Willy Tarreauc3914d42020-09-24 08:39:22 +0200467 curproxy->disabled = 1;
William Lallemand9001ce82019-04-01 11:29:57 +0200468 }
William Lallemand88dc7c52019-04-01 11:30:01 +0200469}
470
William Lallemand030571a2022-01-28 21:17:30 +0100471/* Upon a configuration loading error some mworker_proc and FDs/server were
472 * assigned but the worker was never forked, we must close the FDs and
473 * remove the server
474 */
475void mworker_cleanup_proc()
476{
477 struct mworker_proc *child, *it;
478
479 list_for_each_entry_safe(child, it, &proc_list, list) {
480
481 if (child->pid == -1) {
482 /* Close the socketpair master side. We don't need to
483 * close the worker side, because it's stored in the
484 * GLOBAL cli listener which was supposed to be in the
485 * worker and which will be closed in
486 * mworker_cleanlisteners()
487 */
488 if (child->ipc_fd[0] > -1)
489 close(child->ipc_fd[0]);
490 if (child->srv) {
491 /* only exists if we created a master CLI listener */
492 free_server(child->srv);
493 }
494 LIST_DELETE(&child->list);
495 mworker_free_child(child);
496 }
497 }
498}
499
500
William Lallemand88dc7c52019-04-01 11:30:01 +0200501/* Displays workers and processes */
502static int cli_io_handler_show_proc(struct appctx *appctx)
503{
504 struct stream_interface *si = appctx->owner;
505 struct mworker_proc *child;
506 int old = 0;
William Lallemand512df052023-02-17 16:23:52 +0100507 int up = date.tv_sec - proc_self->timestamp;
William Lallemande8669fc2019-06-12 18:21:17 +0200508 char *uptime = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200509
510 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
511 return 1;
512
William Lallemand512df052023-02-17 16:23:52 +0100513 if (up < 0) /* must never be negative because of clock drift */
514 up = 0;
515
William Lallemand88dc7c52019-04-01 11:30:01 +0200516 chunk_reset(&trash);
517
William Lallemand1dc69632019-06-12 19:11:33 +0200518 chunk_printf(&trash, "#%-14s %-15s %-15s %-15s %-15s %-15s\n", "<PID>", "<type>", "<relative PID>", "<reloads>", "<uptime>", "<version>");
William Lallemande8669fc2019-06-12 18:21:17 +0200519 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
Willy Tarreau76a80c72019-06-22 07:41:38 +0200520 chunk_appendf(&trash, "%-15u %-15s %-15u %-15d %-15s %-15s\n", (unsigned int)getpid(), "master", 0, proc_self->reloads, uptime, haproxy_version);
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100521 ha_free(&uptime);
William Lallemand88dc7c52019-04-01 11:30:01 +0200522
523 /* displays current processes */
524
525 chunk_appendf(&trash, "# workers\n");
526 list_for_each_entry(child, &proc_list, list) {
William Lallemand512df052023-02-17 16:23:52 +0100527 up = date.tv_sec - child->timestamp;
528 if (up < 0) /* must never be negative because of clock drift */
529 up = 0;
William Lallemand88dc7c52019-04-01 11:30:01 +0200530
William Lallemand8f7069a2019-04-12 16:09:23 +0200531 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200532 continue;
533
William Lallemand45286112019-04-12 16:09:21 +0200534 if (child->options & PROC_O_LEAVING) {
William Lallemand88dc7c52019-04-01 11:30:01 +0200535 old++;
536 continue;
537 }
William Lallemande8669fc2019-06-12 18:21:17 +0200538 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200539 chunk_appendf(&trash, "%-15u %-15s %-15u %-15d %-15s %-15s\n", child->pid, "worker", child->relative_pid, child->reloads, uptime, child->version);
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100540 ha_free(&uptime);
William Lallemand88dc7c52019-04-01 11:30:01 +0200541 }
542
543 /* displays old processes */
544
545 if (old) {
546 char *msg = NULL;
547
548 chunk_appendf(&trash, "# old workers\n");
549 list_for_each_entry(child, &proc_list, list) {
William Lallemand512df052023-02-17 16:23:52 +0100550 up = date.tv_sec - child->timestamp;
551 if (up <= 0) /* must never be negative because of clock drift */
552 up = 0;
William Lallemand88dc7c52019-04-01 11:30:01 +0200553
William Lallemand8f7069a2019-04-12 16:09:23 +0200554 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200555 continue;
556
William Lallemand45286112019-04-12 16:09:21 +0200557 if (child->options & PROC_O_LEAVING) {
William Lallemand88dc7c52019-04-01 11:30:01 +0200558 memprintf(&msg, "[was: %u]", child->relative_pid);
William Lallemande8669fc2019-06-12 18:21:17 +0200559 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200560 chunk_appendf(&trash, "%-15u %-15s %-15s %-15d %-15s %-15s\n", child->pid, "worker", msg, child->reloads, uptime, child->version);
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100561 ha_free(&uptime);
William Lallemand88dc7c52019-04-01 11:30:01 +0200562 }
563 }
564 free(msg);
565 }
566
William Lallemandad53d6d2019-04-01 11:30:03 +0200567 /* displays external process */
568 chunk_appendf(&trash, "# programs\n");
569 old = 0;
570 list_for_each_entry(child, &proc_list, list) {
William Lallemand512df052023-02-17 16:23:52 +0100571 up = date.tv_sec - child->timestamp;
572 if (up < 0) /* must never be negative because of clock drift */
573 up = 0;
William Lallemandad53d6d2019-04-01 11:30:03 +0200574
William Lallemand8f7069a2019-04-12 16:09:23 +0200575 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200576 continue;
577
William Lallemand45286112019-04-12 16:09:21 +0200578 if (child->options & PROC_O_LEAVING) {
William Lallemandad53d6d2019-04-01 11:30:03 +0200579 old++;
580 continue;
581 }
William Lallemande8669fc2019-06-12 18:21:17 +0200582 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand1dc69632019-06-12 19:11:33 +0200583 chunk_appendf(&trash, "%-15u %-15s %-15s %-15d %-15s %-15s\n", child->pid, child->id, "-", child->reloads, uptime, "-");
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100584 ha_free(&uptime);
William Lallemandad53d6d2019-04-01 11:30:03 +0200585 }
586
587 if (old) {
588 chunk_appendf(&trash, "# old programs\n");
589 list_for_each_entry(child, &proc_list, list) {
William Lallemand512df052023-02-17 16:23:52 +0100590 up = date.tv_sec - child->timestamp;
591 if (up < 0) /* must never be negative because of clock drift */
592 up = 0;
William Lallemandad53d6d2019-04-01 11:30:03 +0200593
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 Lallemand1dc69632019-06-12 19:11:33 +0200599 chunk_appendf(&trash, "%-15u %-15s %-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
William Lallemand88dc7c52019-04-01 11:30:01 +0200607 if (ci_putchk(si_ic(si), &trash) == -1) {
608 si_rx_room_blk(si);
609 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{
656 if (child == NULL)
657 return;
658
659 if (child->command) {
660 int i;
661
662 for (i = 0; child->command[i]; i++) {
663 if (child->command[i]) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100664 ha_free(&child->command[i]);
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200665 }
William Lallemande8669fc2019-06-12 18:21:17 +0200666
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200667 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100668 ha_free(&child->command);
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200669 }
670 if (child->id) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100671 ha_free(&child->id);
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200672 }
William Lallemand1dc69632019-06-12 19:11:33 +0200673 if (child->version) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100674 ha_free(&child->version);
William Lallemand1dc69632019-06-12 19:11:33 +0200675 }
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200676 free(child);
677}
William Lallemand27edc4b2019-05-07 17:49:33 +0200678
679static struct cfg_kw_list mworker_kws = {{ }, {
680 { CFG_GLOBAL, "mworker-max-reloads", mworker_parse_global_max_reloads },
681 { 0, NULL, NULL },
682}};
683
684INITCALL1(STG_REGISTER, cfg_register_keywords, &mworker_kws);
685
686
William Lallemand88dc7c52019-04-01 11:30:01 +0200687/* register cli keywords */
688static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau23c740e2021-05-09 22:49:44 +0200689 { { "@<relative pid>", NULL }, "@<relative pid> : send a command to the <relative pid> process", NULL, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
690 { { "@!<pid>", NULL }, "@!<pid> : send a command to the <pid> process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
691 { { "@master", NULL }, "@master : send a command to the master process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
692 { { "show", "proc", NULL }, "show proc : show processes status", cli_parse_default, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
693 { { "reload", NULL }, "reload : reload haproxy", cli_parse_reload, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
William Lallemand88dc7c52019-04-01 11:30:01 +0200694 {{},}
695}};
696
697INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);