blob: e613a4e1640376f6af5c55c7693cf02b167b42e3 [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 Lallemandec059c22022-09-22 17:26:23 +020016#include <fcntl.h>
William Lallemand3fa724d2019-04-01 11:29:55 +020017#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 Lallemandec059c22022-09-22 17:26:23 +020021#include <unistd.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +020022
Willy Tarreaub2551052020-06-09 09:07:15 +020023#if defined(USE_SYSTEMD)
24#include <systemd/sd-daemon.h>
25#endif
26
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020027#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020028#include <haproxy/cfgparse.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020029#include <haproxy/cli.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020030#include <haproxy/errors.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020031#include <haproxy/fd.h>
32#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020033#include <haproxy/list.h>
William Lallemand68192b22022-09-24 15:44:42 +020034#include <haproxy/log.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>
William Lallemandec059c22022-09-22 17:26:23 +020038#include <haproxy/proto_sockpair.h>
Willy Tarreau7c668572021-05-08 20:21:31 +020039#include <haproxy/proxy.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020040#include <haproxy/sc_strm.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +020041#include <haproxy/signal.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020042#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020043#include <haproxy/stream.h>
Willy Tarreau745e98c2021-05-08 13:58:19 +020044#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020045#include <haproxy/version.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +020046
William Lallemand48dfbbd2019-04-01 11:29:53 +020047
William Lallemande25473c2019-04-01 11:29:56 +020048static int exitcode = -1;
William Lallemand27edc4b2019-05-07 17:49:33 +020049static int max_reloads = -1; /* number max of reloads a worker can have until they are killed */
Willy Tarreau15f9ac32021-05-08 12:30:50 +020050struct mworker_proc *proc_self = NULL; /* process structure of current process */
William Lallemande25473c2019-04-01 11:29:56 +020051
William Lallemande25473c2019-04-01 11:29:56 +020052/* ----- children processes handling ----- */
William Lallemand48dfbbd2019-04-01 11:29:53 +020053
William Lallemand48dfbbd2019-04-01 11:29:53 +020054/*
William Lallemande25473c2019-04-01 11:29:56 +020055 * Send signal to every known children.
56 */
57
58static void mworker_kill(int sig)
59{
William Lallemand3f128872019-04-01 11:29:59 +020060 struct mworker_proc *child;
William Lallemande25473c2019-04-01 11:29:56 +020061
William Lallemand3f128872019-04-01 11:29:59 +020062 list_for_each_entry(child, &proc_list, list) {
63 /* 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 +020064 if ((child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG)) && (child->pid > 0))
William Lallemand3f128872019-04-01 11:29:59 +020065 kill(child->pid, sig);
William Lallemande25473c2019-04-01 11:29:56 +020066 }
67}
68
William Lallemand27edc4b2019-05-07 17:49:33 +020069void mworker_kill_max_reloads(int sig)
70{
71 struct mworker_proc *child;
72
73 list_for_each_entry(child, &proc_list, list) {
74 if (max_reloads != -1 && (child->options & PROC_O_TYPE_WORKER) &&
75 (child->pid > 0) && (child->reloads > max_reloads))
76 kill(child->pid, sig);
77 }
78}
William Lallemande25473c2019-04-01 11:29:56 +020079
80/* return 1 if a pid is a current child otherwise 0 */
William Lallemand3f128872019-04-01 11:29:59 +020081int mworker_current_child(int pid)
William Lallemande25473c2019-04-01 11:29:56 +020082{
William Lallemand3f128872019-04-01 11:29:59 +020083 struct mworker_proc *child;
William Lallemande25473c2019-04-01 11:29:56 +020084
William Lallemand3f128872019-04-01 11:29:59 +020085 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +020086 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 +020087 return 1;
88 }
89 return 0;
90}
91
William Lallemand3f128872019-04-01 11:29:59 +020092/*
93 * Return the number of new and old children (including workers and external
94 * processes)
95 */
96int mworker_child_nb()
97{
98 struct mworker_proc *child;
99 int ret = 0;
100
101 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200102 if (child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG))
William Lallemand3f128872019-04-01 11:29:59 +0200103 ret++;
104 }
105
106 return ret;
107}
108
109
William Lallemande25473c2019-04-01 11:29:56 +0200110/*
William Lallemand48dfbbd2019-04-01 11:29:53 +0200111 * serialize the proc list and put it in the environment
112 */
113void mworker_proc_list_to_env()
114{
115 char *msg = NULL;
116 struct mworker_proc *child;
William Lallemand14b98ef2022-07-27 11:57:12 +0200117 int minreloads = INT_MAX; /* minimum number of reloads to chose which processes are "current" ones */
William Lallemand48dfbbd2019-04-01 11:29:53 +0200118
119 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200120 char type = '?';
121
122 if (child->options & PROC_O_TYPE_MASTER)
123 type = 'm';
124 else if (child->options & PROC_O_TYPE_PROG)
125 type = 'e';
126 else if (child->options &= PROC_O_TYPE_WORKER)
127 type = 'w';
128
William Lallemand14b98ef2022-07-27 11:57:12 +0200129 if (child->reloads < minreloads)
130 minreloads = child->reloads;
131
William Lallemand48dfbbd2019-04-01 11:29:53 +0200132 if (child->pid > -1)
William Lallemandec059c22022-09-22 17:26:23 +0200133 memprintf(&msg, "%s|type=%c;fd=%d;cfd=%d;pid=%d;reloads=%d;failedreloads=%d;timestamp=%d;id=%s;version=%s", msg ? msg : "", type, child->ipc_fd[0], child->ipc_fd[1], child->pid, child->reloads, child->failedreloads, child->timestamp, child->id ? child->id : "", child->version);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200134 }
135 if (msg)
136 setenv("HAPROXY_PROCESSES", msg, 1);
William Lallemand14b98ef2022-07-27 11:57:12 +0200137
138 list_for_each_entry(child, &proc_list, list) {
139 if (child->reloads > minreloads && !(child->options & PROC_O_TYPE_MASTER)) {
140 child->options |= PROC_O_LEAVING;
141 }
142 }
143
144
William Lallemand48dfbbd2019-04-01 11:29:53 +0200145}
146
William Lallemand56be0e02022-01-28 21:11:41 +0100147struct mworker_proc *mworker_proc_new()
148{
149 struct mworker_proc *child;
150
151 child = calloc(1, sizeof(*child));
152 if (!child)
153 return NULL;
154
155 child->failedreloads = 0;
156 child->reloads = 0;
157 child->pid = -1;
158 child->ipc_fd[0] = -1;
159 child->ipc_fd[1] = -1;
160 child->timestamp = -1;
161
162 return child;
163}
164
165
William Lallemand48dfbbd2019-04-01 11:29:53 +0200166/*
167 * unserialize the proc list from the environment
168 */
Remi Tricot-Le Breton1f4fa902021-05-19 10:45:12 +0200169int mworker_env_to_proc_list()
William Lallemand48dfbbd2019-04-01 11:29:53 +0200170{
171 char *msg, *token = NULL, *s1;
William Lallemand90034bb2021-11-10 11:26:14 +0100172 struct mworker_proc *child;
173 int minreloads = INT_MAX; /* minimum number of reloads to chose which processes are "current" ones */
William Lallemand48dfbbd2019-04-01 11:29:53 +0200174
175 msg = getenv("HAPROXY_PROCESSES");
176 if (!msg)
Remi Tricot-Le Breton1f4fa902021-05-19 10:45:12 +0200177 return 0;
William Lallemand48dfbbd2019-04-01 11:29:53 +0200178
179 while ((token = strtok_r(msg, "|", &s1))) {
William Lallemand48dfbbd2019-04-01 11:29:53 +0200180 char *subtoken = NULL;
181 char *s2;
182
183 msg = NULL;
184
William Lallemand56be0e02022-01-28 21:11:41 +0100185 child = mworker_proc_new();
Remi Tricot-Le Breton1f4fa902021-05-19 10:45:12 +0200186 if (!child) {
187 ha_alert("Out of memory while trying to allocate a worker process structure.");
188 return -1;
189 }
William Lallemand48dfbbd2019-04-01 11:29:53 +0200190
191 while ((subtoken = strtok_r(token, ";", &s2))) {
192
193 token = NULL;
194
195 if (strncmp(subtoken, "type=", 5) == 0) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200196 char type;
197
198 type = *(subtoken+5);
199 if (type == 'm') { /* we are in the master, assign it */
William Lallemand48dfbbd2019-04-01 11:29:53 +0200200 proc_self = child;
William Lallemand8f7069a2019-04-12 16:09:23 +0200201 child->options |= PROC_O_TYPE_MASTER;
202 } else if (type == 'e') {
203 child->options |= PROC_O_TYPE_PROG;
204 } else if (type == 'w') {
205 child->options |= PROC_O_TYPE_WORKER;
206 }
207
William Lallemand48dfbbd2019-04-01 11:29:53 +0200208 } else if (strncmp(subtoken, "fd=", 3) == 0) {
209 child->ipc_fd[0] = atoi(subtoken+3);
William Lallemandec059c22022-09-22 17:26:23 +0200210 } else if (strncmp(subtoken, "cfd=", 4) == 0) {
211 child->ipc_fd[1] = atoi(subtoken+4);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200212 } else if (strncmp(subtoken, "pid=", 4) == 0) {
213 child->pid = atoi(subtoken+4);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200214 } else if (strncmp(subtoken, "reloads=", 8) == 0) {
William Lallemandad221f42021-11-09 18:43:59 +0100215 /* we only increment the number of asked reload */
216 child->reloads = atoi(subtoken+8);
William Lallemand90034bb2021-11-10 11:26:14 +0100217
218 if (child->reloads < minreloads)
219 minreloads = child->reloads;
William Lallemand68836742021-11-10 10:49:06 +0100220 } else if (strncmp(subtoken, "failedreloads=", 14) == 0) {
221 child->failedreloads = atoi(subtoken+14);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200222 } else if (strncmp(subtoken, "timestamp=", 10) == 0) {
223 child->timestamp = atoi(subtoken+10);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200224 } else if (strncmp(subtoken, "id=", 3) == 0) {
225 child->id = strdup(subtoken+3);
William Lallemand1dc69632019-06-12 19:11:33 +0200226 } else if (strncmp(subtoken, "version=", 8) == 0) {
227 child->version = strdup(subtoken+8);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200228 }
229 }
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200230 if (child->pid) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200231 LIST_APPEND(&proc_list, &child->list);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200232 } else {
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200233 mworker_free_child(child);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200234 }
William Lallemand48dfbbd2019-04-01 11:29:53 +0200235 }
236
William Lallemand90034bb2021-11-10 11:26:14 +0100237 /* set the leaving processes once we know which number of reloads are the current processes */
238
239 list_for_each_entry(child, &proc_list, list) {
240 if (child->reloads > minreloads)
241 child->options |= PROC_O_LEAVING;
242 }
243
William Lallemand48dfbbd2019-04-01 11:29:53 +0200244 unsetenv("HAPROXY_PROCESSES");
Remi Tricot-Le Breton1f4fa902021-05-19 10:45:12 +0200245
246 return 0;
William Lallemand48dfbbd2019-04-01 11:29:53 +0200247}
William Lallemand3cd95d22019-04-01 11:29:54 +0200248
249/* Signal blocking and unblocking */
250
251void mworker_block_signals()
252{
253 sigset_t set;
254
255 sigemptyset(&set);
256 sigaddset(&set, SIGUSR1);
257 sigaddset(&set, SIGUSR2);
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100258 sigaddset(&set, SIGTTIN);
259 sigaddset(&set, SIGTTOU);
William Lallemand3cd95d22019-04-01 11:29:54 +0200260 sigaddset(&set, SIGHUP);
261 sigaddset(&set, SIGCHLD);
262 ha_sigmask(SIG_SETMASK, &set, NULL);
263}
264
265void mworker_unblock_signals()
266{
267 haproxy_unblock_signals();
268}
William Lallemand3fa724d2019-04-01 11:29:55 +0200269
William Lallemande25473c2019-04-01 11:29:56 +0200270/* ----- mworker signal handlers ----- */
271
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100272/* broadcast the configured signal to the workers */
273void mworker_broadcast_signal(struct sig_handler *sh)
274{
275 mworker_kill(sh->arg);
276}
277
William Lallemande25473c2019-04-01 11:29:56 +0200278/*
279 * When called, this function reexec haproxy with -sf followed by current
280 * children PIDs and possibly old children PIDs if they didn't leave yet.
281 */
282void mworker_catch_sighup(struct sig_handler *sh)
283{
284 mworker_reload();
285}
286
287void mworker_catch_sigterm(struct sig_handler *sh)
288{
289 int sig = sh->arg;
290
291#if defined(USE_SYSTEMD)
292 if (global.tune.options & GTUNE_USE_SYSTEMD) {
293 sd_notify(0, "STOPPING=1");
294 }
295#endif
296 ha_warning("Exiting Master process...\n");
297 mworker_kill(sig);
298}
299
300/*
301 * Wait for every children to exit
302 */
303
304void mworker_catch_sigchld(struct sig_handler *sh)
305{
306 int exitpid = -1;
307 int status = 0;
William Lallemande25473c2019-04-01 11:29:56 +0200308 int childfound;
309
310restart_wait:
311
312 childfound = 0;
313
314 exitpid = waitpid(-1, &status, WNOHANG);
315 if (exitpid > 0) {
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200316 struct mworker_proc *child, *it;
317
William Lallemande25473c2019-04-01 11:29:56 +0200318 if (WIFEXITED(status))
319 status = WEXITSTATUS(status);
320 else if (WIFSIGNALED(status))
321 status = 128 + WTERMSIG(status);
322 else if (WIFSTOPPED(status))
323 status = 128 + WSTOPSIG(status);
324 else
325 status = 255;
326
William Lallemand3f128872019-04-01 11:29:59 +0200327 /* delete the child from the process list */
William Lallemande25473c2019-04-01 11:29:56 +0200328 list_for_each_entry_safe(child, it, &proc_list, list) {
329 if (child->pid != exitpid)
330 continue;
331
Willy Tarreau2b718102021-04-21 07:32:39 +0200332 LIST_DELETE(&child->list);
William Lallemande25473c2019-04-01 11:29:56 +0200333 close(child->ipc_fd[0]);
334 childfound = 1;
335 break;
336 }
337
William Lallemand3f128872019-04-01 11:29:59 +0200338 if (!childfound) {
339 /* 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 +0200340 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 +0200341 } else {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200342 /* check if exited child is a current child */
William Lallemand45286112019-04-12 16:09:21 +0200343 if (!(child->options & PROC_O_LEAVING)) {
William Lallemanda655ba42020-05-06 17:27:03 +0200344 if (child->options & PROC_O_TYPE_WORKER) {
345 if (status < 128)
William Lallemand5d71a6b2021-11-09 15:25:31 +0100346 ha_warning("Current worker (%d) exited with code %d (%s)\n", exitpid, status, "Exit");
William Lallemanda655ba42020-05-06 17:27:03 +0200347 else
William Lallemand5d71a6b2021-11-09 15:25:31 +0100348 ha_alert("Current worker (%d) exited with code %d (%s)\n", exitpid, status, strsignal(status - 128));
William Lallemanda655ba42020-05-06 17:27:03 +0200349 }
William Lallemand8f7069a2019-04-12 16:09:23 +0200350 else if (child->options & PROC_O_TYPE_PROG)
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200351 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 +0200352
William Lallemande25473c2019-04-01 11:29:56 +0200353 if (status != 0 && status != 130 && status != 143
354 && !(global.tune.options & GTUNE_NOEXIT_ONFAILURE)) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200355 ha_alert("exit-on-failure: killing every processes with SIGTERM\n");
William Lallemande25473c2019-04-01 11:29:56 +0200356 mworker_kill(SIGTERM);
357 }
William Lallemand74f0ec32019-04-16 17:42:44 +0200358 /* 0 & SIGTERM (143) are normal, but we should report SIGINT (130) and other signals */
359 if (exitcode < 0 && status != 0 && status != 143)
360 exitcode = status;
William Lallemande25473c2019-04-01 11:29:56 +0200361 } else {
William Lallemand8f7069a2019-04-12 16:09:23 +0200362 if (child->options & PROC_O_TYPE_WORKER) {
William Lallemand5d71a6b2021-11-09 15:25:31 +0100363 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 +0200364 delete_oldpid(exitpid);
William Lallemand8f7069a2019-04-12 16:09:23 +0200365 } else if (child->options & PROC_O_TYPE_PROG) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200366 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 +0200367 }
William Lallemande25473c2019-04-01 11:29:56 +0200368 }
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200369 mworker_free_child(child);
370 child = NULL;
William Lallemande25473c2019-04-01 11:29:56 +0200371 }
372
373 /* do it again to check if it was the last worker */
374 goto restart_wait;
375 }
376 /* Better rely on the system than on a list of process to check if it was the last one */
377 else if (exitpid == -1 && errno == ECHILD) {
William Lallemand4cf4b332019-04-16 17:42:43 +0200378 ha_warning("All workers exited. Exiting... (%d)\n", (exitcode > 0) ? exitcode : EXIT_SUCCESS);
William Lallemande25473c2019-04-01 11:29:56 +0200379 atexit_flag = 0;
380 if (exitcode > 0)
William Lallemand4cf4b332019-04-16 17:42:43 +0200381 exit(exitcode); /* parent must leave using the status code that provoked the exit */
382 exit(EXIT_SUCCESS);
William Lallemande25473c2019-04-01 11:29:56 +0200383 }
384
385}
386
William Lallemand3fa724d2019-04-01 11:29:55 +0200387/* ----- IPC FD (sockpair) related ----- */
388
389/* This wrapper is called from the workers. It is registered instead of the
390 * normal listener_accept() so the worker can exit() when it detects that the
391 * master closed the IPC FD. If it's not a close, we just call the regular
Willy Tarreaua74cb382020-10-15 21:29:49 +0200392 * listener_accept() function.
393 */
William Lallemand3fa724d2019-04-01 11:29:55 +0200394void mworker_accept_wrapper(int fd)
395{
396 char c;
397 int ret;
398
399 while (1) {
400 ret = recv(fd, &c, 1, MSG_PEEK);
401 if (ret == -1) {
402 if (errno == EINTR)
403 continue;
Willy Tarreauacef5e22022-04-25 20:32:15 +0200404 if (errno == EAGAIN || errno == EWOULDBLOCK) {
William Lallemand3fa724d2019-04-01 11:29:55 +0200405 fd_cant_recv(fd);
406 return;
407 }
408 break;
409 } else if (ret > 0) {
Willy Tarreaua74cb382020-10-15 21:29:49 +0200410 struct listener *l = fdtab[fd].owner;
411
412 if (l)
413 listener_accept(l);
William Lallemand3fa724d2019-04-01 11:29:55 +0200414 return;
415 } else if (ret == 0) {
416 /* At this step the master is down before
417 * this worker perform a 'normal' exit.
418 * So we want to exit with an error but
419 * other threads could currently process
420 * some stuff so we can't perform a clean
421 * deinit().
422 */
423 exit(EXIT_FAILURE);
424 }
425 }
426 return;
427}
428
429/*
Willy Tarreau619a95f2019-05-20 11:12:15 +0200430 * This function registers the accept wrapper for the sockpair of the master
431 * worker. It's only handled by worker thread #0. Other threads and master do
432 * nothing here. It always returns 1 (success).
William Lallemand3fa724d2019-04-01 11:29:55 +0200433 */
William Lallemand2ee490f2022-07-05 09:04:03 +0200434static int mworker_sockpair_register_per_thread()
William Lallemand3fa724d2019-04-01 11:29:55 +0200435{
Willy Tarreau619a95f2019-05-20 11:12:15 +0200436 if (!(global.mode & MODE_MWORKER) || master)
437 return 1;
438
439 if (tid != 0)
440 return 1;
William Lallemand3fa724d2019-04-01 11:29:55 +0200441
Willy Tarreau38247432022-04-26 10:24:14 +0200442 fd_set_nonblock(proc_self->ipc_fd[1]);
William Lallemand2ee490f2022-07-05 09:04:03 +0200443 /* register the wrapper to handle read 0 when the master exits */
William Lallemand34aae2f2022-07-05 00:55:09 +0200444 fdtab[proc_self->ipc_fd[1]].iocb = mworker_accept_wrapper;
William Lallemand3fa724d2019-04-01 11:29:55 +0200445 fd_want_recv(proc_self->ipc_fd[1]);
Willy Tarreau619a95f2019-05-20 11:12:15 +0200446 return 1;
William Lallemand3fa724d2019-04-01 11:29:55 +0200447}
William Lallemand9001ce82019-04-01 11:29:57 +0200448
William Lallemand2ee490f2022-07-05 09:04:03 +0200449REGISTER_PER_THREAD_INIT(mworker_sockpair_register_per_thread);
Willy Tarreau619a95f2019-05-20 11:12:15 +0200450
William Lallemand9001ce82019-04-01 11:29:57 +0200451/* ----- proxies ----- */
452/*
453 * Upon a reload, the master worker needs to close all listeners FDs but the mworker_pipe
454 * fd, and the FD provided by fd@
455 */
456void mworker_cleanlisteners()
457{
458 struct listener *l, *l_next;
459 struct proxy *curproxy;
460 struct peers *curpeers;
461
462 /* we might have to unbind some peers sections from some processes */
463 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
464 if (!curpeers->peers_fe)
465 continue;
466
467 stop_proxy(curpeers->peers_fe);
468 /* disable this peer section so that it kills itself */
469 signal_unregister_handler(curpeers->sighandler);
Olivier Houchard3f795f72019-04-17 22:51:06 +0200470 task_destroy(curpeers->sync_task);
William Lallemand9001ce82019-04-01 11:29:57 +0200471 curpeers->sync_task = NULL;
Olivier Houchard3f795f72019-04-17 22:51:06 +0200472 task_destroy(curpeers->peers_fe->task);
William Lallemand9001ce82019-04-01 11:29:57 +0200473 curpeers->peers_fe->task = NULL;
474 curpeers->peers_fe = NULL;
475 }
476
477 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
478 int listen_in_master = 0;
479
480 list_for_each_entry_safe(l, l_next, &curproxy->conf.listeners, by_fe) {
481 /* remove the listener, but not those we need in the master... */
Willy Tarreau18c20d22020-10-09 16:11:46 +0200482 if (!(l->rx.flags & RX_F_MWORKER)) {
Willy Tarreau75c98d12020-10-09 15:55:23 +0200483 unbind_listener(l);
William Lallemand9001ce82019-04-01 11:29:57 +0200484 delete_listener(l);
485 } else {
486 listen_in_master = 1;
487 }
488 }
489 /* if the proxy shouldn't be in the master, we stop it */
490 if (!listen_in_master)
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200491 curproxy->flags |= PR_FL_DISABLED;
William Lallemand9001ce82019-04-01 11:29:57 +0200492 }
William Lallemand88dc7c52019-04-01 11:30:01 +0200493}
494
William Lallemand55a921c2022-01-28 21:17:30 +0100495/* Upon a configuration loading error some mworker_proc and FDs/server were
496 * assigned but the worker was never forked, we must close the FDs and
497 * remove the server
498 */
499void mworker_cleanup_proc()
500{
501 struct mworker_proc *child, *it;
502
503 list_for_each_entry_safe(child, it, &proc_list, list) {
504
505 if (child->pid == -1) {
506 /* Close the socketpair master side. We don't need to
507 * close the worker side, because it's stored in the
508 * GLOBAL cli listener which was supposed to be in the
509 * worker and which will be closed in
510 * mworker_cleanlisteners()
511 */
512 if (child->ipc_fd[0] > -1)
513 close(child->ipc_fd[0]);
514 if (child->srv) {
515 /* only exists if we created a master CLI listener */
516 srv_drop(child->srv);
517 }
518 LIST_DELETE(&child->list);
519 mworker_free_child(child);
520 }
521 }
522}
523
524
William Lallemand88dc7c52019-04-01 11:30:01 +0200525/* Displays workers and processes */
526static int cli_io_handler_show_proc(struct appctx *appctx)
527{
Willy Tarreauc12b3212022-05-27 11:08:15 +0200528 struct stconn *sc = appctx_sc(appctx);
William Lallemand88dc7c52019-04-01 11:30:01 +0200529 struct mworker_proc *child;
530 int old = 0;
531 int up = now.tv_sec - proc_self->timestamp;
William Lallemande8669fc2019-06-12 18:21:17 +0200532 char *uptime = NULL;
William Lallemand68836742021-11-10 10:49:06 +0100533 char *reloadtxt = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200534
Willy Tarreau475e4632022-05-27 10:26:46 +0200535 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
William Lallemand88dc7c52019-04-01 11:30:01 +0200536 return 1;
537
538 chunk_reset(&trash);
539
William Lallemand68836742021-11-10 10:49:06 +0100540 memprintf(&reloadtxt, "%d [failed: %d]", proc_self->reloads, proc_self->failedreloads);
William Lallemand5d71a6b2021-11-09 15:25:31 +0100541 chunk_printf(&trash, "#%-14s %-15s %-15s %-15s %-15s\n", "<PID>", "<type>", "<reloads>", "<uptime>", "<version>");
William Lallemande8669fc2019-06-12 18:21:17 +0200542 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand68836742021-11-10 10:49:06 +0100543 chunk_appendf(&trash, "%-15u %-15s %-15s %-15s %-15s\n", (unsigned int)getpid(), "master", reloadtxt, uptime, haproxy_version);
544 ha_free(&reloadtxt);
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100545 ha_free(&uptime);
William Lallemand88dc7c52019-04-01 11:30:01 +0200546
547 /* displays current processes */
548
549 chunk_appendf(&trash, "# workers\n");
550 list_for_each_entry(child, &proc_list, list) {
551 up = now.tv_sec - child->timestamp;
552
William Lallemand8f7069a2019-04-12 16:09:23 +0200553 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200554 continue;
555
William Lallemand45286112019-04-12 16:09:21 +0200556 if (child->options & PROC_O_LEAVING) {
William Lallemand88dc7c52019-04-01 11:30:01 +0200557 old++;
558 continue;
559 }
William Lallemande8669fc2019-06-12 18:21:17 +0200560 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand5d71a6b2021-11-09 15:25:31 +0100561 chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, "worker", child->reloads, uptime, child->version);
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100562 ha_free(&uptime);
William Lallemand88dc7c52019-04-01 11:30:01 +0200563 }
564
565 /* displays old processes */
566
567 if (old) {
568 char *msg = NULL;
569
570 chunk_appendf(&trash, "# old workers\n");
571 list_for_each_entry(child, &proc_list, list) {
572 up = now.tv_sec - child->timestamp;
573
William Lallemand8f7069a2019-04-12 16:09:23 +0200574 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200575 continue;
576
William Lallemand45286112019-04-12 16:09:21 +0200577 if (child->options & PROC_O_LEAVING) {
William Lallemande8669fc2019-06-12 18:21:17 +0200578 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand5d71a6b2021-11-09 15:25:31 +0100579 chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, "worker", child->reloads, uptime, child->version);
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100580 ha_free(&uptime);
William Lallemand88dc7c52019-04-01 11:30:01 +0200581 }
582 }
583 free(msg);
584 }
585
William Lallemandad53d6d2019-04-01 11:30:03 +0200586 /* displays external process */
587 chunk_appendf(&trash, "# programs\n");
588 old = 0;
589 list_for_each_entry(child, &proc_list, list) {
590 up = now.tv_sec - child->timestamp;
591
William Lallemand8f7069a2019-04-12 16:09:23 +0200592 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200593 continue;
594
William Lallemand45286112019-04-12 16:09:21 +0200595 if (child->options & PROC_O_LEAVING) {
William Lallemandad53d6d2019-04-01 11:30:03 +0200596 old++;
597 continue;
598 }
William Lallemande8669fc2019-06-12 18:21:17 +0200599 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand5d71a6b2021-11-09 15:25:31 +0100600 chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, child->id, child->reloads, uptime, "-");
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100601 ha_free(&uptime);
William Lallemandad53d6d2019-04-01 11:30:03 +0200602 }
603
604 if (old) {
605 chunk_appendf(&trash, "# old programs\n");
606 list_for_each_entry(child, &proc_list, list) {
607 up = now.tv_sec - child->timestamp;
608
William Lallemand8f7069a2019-04-12 16:09:23 +0200609 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200610 continue;
611
William Lallemand45286112019-04-12 16:09:21 +0200612 if (child->options & PROC_O_LEAVING) {
William Lallemande8669fc2019-06-12 18:21:17 +0200613 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand5d71a6b2021-11-09 15:25:31 +0100614 chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, child->id, child->reloads, uptime, "-");
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100615 ha_free(&uptime);
William Lallemandad53d6d2019-04-01 11:30:03 +0200616 }
617 }
618 }
619
620
621
Willy Tarreaud0a06d52022-05-18 15:07:19 +0200622 if (applet_putchk(appctx, &trash) == -1)
William Lallemand88dc7c52019-04-01 11:30:01 +0200623 return 0;
William Lallemand88dc7c52019-04-01 11:30:01 +0200624
625 /* dump complete */
626 return 1;
William Lallemand9001ce82019-04-01 11:29:57 +0200627}
William Lallemand88dc7c52019-04-01 11:30:01 +0200628
629/* reload the master process */
630static int cli_parse_reload(char **args, char *payload, struct appctx *appctx, void *private)
631{
William Lallemandec059c22022-09-22 17:26:23 +0200632 struct stconn *scb = NULL;
633 struct stream *strm = NULL;
634 struct connection *conn = NULL;
635 int fd = -1;
636
William Lallemand88dc7c52019-04-01 11:30:01 +0200637 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
638 return 1;
639
William Lallemandec059c22022-09-22 17:26:23 +0200640 /* This ask for a synchronous reload, which means we will keep this FD
641 instead of closing it. */
642
643 scb = appctx_sc(appctx);
644 if (scb)
645 strm = sc_strm(scb);
646 if (strm && strm->scf)
647 conn = sc_conn(strm->scf);
648 if (conn)
649 fd = conn_fd(conn);
650
651 /* Send the FD of the current session to the "cli_reload" FD, which won't be polled */
652 if (fd != -1 && send_fd_uxst(proc_self->ipc_fd[0], fd) == 0) {
William Lallemand479cb3e2022-09-23 10:21:32 +0200653 fd_delete(fd); /* avoid the leak of the FD after sending it via the socketpair */
William Lallemandec059c22022-09-22 17:26:23 +0200654 }
William Lallemand88dc7c52019-04-01 11:30:01 +0200655 mworker_reload();
656
657 return 1;
658}
659
William Lallemand68192b22022-09-24 15:44:42 +0200660/* Displays if the current reload failed or succeed */
661static int cli_parse_status(char **args, char *payload, struct appctx *appctx, void *private)
662{
663 char *env;
664
665 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
666 return 1;
667
668 env = getenv("HAPROXY_LOAD_SUCCESS");
669 if (!env)
670 return 1;
671
672 if (strcmp(env, "0") == 0) {
673 return cli_msg(appctx, LOG_INFO, "Loading failure!\n");
674 } else if (strcmp(env, "1") == 0) {
675 return cli_msg(appctx, LOG_INFO, "Loading success.\n");
676 }
677
678 return 1;
679}
680
681
William Lallemand88dc7c52019-04-01 11:30:01 +0200682
William Lallemand27edc4b2019-05-07 17:49:33 +0200683static int mworker_parse_global_max_reloads(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +0100684 const struct proxy *defpx, const char *file, int linenum, char **err)
William Lallemand27edc4b2019-05-07 17:49:33 +0200685{
686
687 int err_code = 0;
688
689 if (alertif_too_many_args(1, file, linenum, args, &err_code))
690 goto out;
691
692 if (*(args[1]) == 0) {
693 memprintf(err, "%sparsing [%s:%d] : '%s' expects an integer argument.\n", *err, file, linenum, args[0]);
694 err_code |= ERR_ALERT | ERR_FATAL;
695 goto out;
696 }
697
698 max_reloads = atol(args[1]);
699 if (max_reloads < 0) {
700 memprintf(err, "%sparsing [%s:%d] '%s' : invalid value %d, must be >= 0", *err, file, linenum, args[0], max_reloads);
701 err_code |= ERR_ALERT | ERR_FATAL;
702 goto out;
703 }
704
705out:
706 return err_code;
707}
708
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200709void mworker_free_child(struct mworker_proc *child)
710{
William Lallemand08cb9452022-01-27 15:33:40 +0100711 int i;
712
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200713 if (child == NULL)
714 return;
715
William Lallemand08cb9452022-01-27 15:33:40 +0100716 for (i = 0; child->command && child->command[i]; i++)
717 ha_free(&child->command[i]);
William Lallemande8669fc2019-06-12 18:21:17 +0200718
William Lallemand08cb9452022-01-27 15:33:40 +0100719 ha_free(&child->command);
720 ha_free(&child->id);
721 ha_free(&child->version);
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200722 free(child);
723}
William Lallemand27edc4b2019-05-07 17:49:33 +0200724
725static struct cfg_kw_list mworker_kws = {{ }, {
726 { CFG_GLOBAL, "mworker-max-reloads", mworker_parse_global_max_reloads },
727 { 0, NULL, NULL },
728}};
729
730INITCALL1(STG_REGISTER, cfg_register_keywords, &mworker_kws);
731
732
William Lallemand88dc7c52019-04-01 11:30:01 +0200733/* register cli keywords */
734static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau23c740e2021-05-09 22:49:44 +0200735 { { "@<relative pid>", NULL }, "@<relative pid> : send a command to the <relative pid> process", NULL, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
736 { { "@!<pid>", NULL }, "@!<pid> : send a command to the <pid> process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
737 { { "@master", NULL }, "@master : send a command to the master process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
738 { { "show", "proc", NULL }, "show proc : show processes status", cli_parse_default, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
739 { { "reload", NULL }, "reload : reload haproxy", cli_parse_reload, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
William Lallemand68192b22022-09-24 15:44:42 +0200740 { { "_loadstatus", NULL }, NULL, cli_parse_status, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
William Lallemand88dc7c52019-04-01 11:30:01 +0200741 {{},}
742}};
743
744INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);