blob: b1115cc6e5627918730186717e522003035e3904 [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>
William Lallemandec1f8a62022-10-13 17:49:54 +020040#include <haproxy/ring.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020041#include <haproxy/sc_strm.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +020042#include <haproxy/signal.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020043#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020044#include <haproxy/stream.h>
Willy Tarreau745e98c2021-05-08 13:58:19 +020045#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020046#include <haproxy/version.h>
William Lallemand48dfbbd2019-04-01 11:29:53 +020047
William Lallemand48dfbbd2019-04-01 11:29:53 +020048
William Lallemande25473c2019-04-01 11:29:56 +020049static int exitcode = -1;
William Lallemand27edc4b2019-05-07 17:49:33 +020050static int max_reloads = -1; /* number max of reloads a worker can have until they are killed */
Willy Tarreau15f9ac32021-05-08 12:30:50 +020051struct mworker_proc *proc_self = NULL; /* process structure of current process */
William Lallemande25473c2019-04-01 11:29:56 +020052
William Lallemande25473c2019-04-01 11:29:56 +020053/* ----- children processes handling ----- */
William Lallemand48dfbbd2019-04-01 11:29:53 +020054
William Lallemand48dfbbd2019-04-01 11:29:53 +020055/*
William Lallemande25473c2019-04-01 11:29:56 +020056 * Send signal to every known children.
57 */
58
59static void mworker_kill(int sig)
60{
William Lallemand3f128872019-04-01 11:29:59 +020061 struct mworker_proc *child;
William Lallemande25473c2019-04-01 11:29:56 +020062
William Lallemand3f128872019-04-01 11:29:59 +020063 list_for_each_entry(child, &proc_list, list) {
64 /* 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 +020065 if ((child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG)) && (child->pid > 0))
William Lallemand3f128872019-04-01 11:29:59 +020066 kill(child->pid, sig);
William Lallemande25473c2019-04-01 11:29:56 +020067 }
68}
69
William Lallemand27edc4b2019-05-07 17:49:33 +020070void mworker_kill_max_reloads(int sig)
71{
72 struct mworker_proc *child;
73
74 list_for_each_entry(child, &proc_list, list) {
75 if (max_reloads != -1 && (child->options & PROC_O_TYPE_WORKER) &&
76 (child->pid > 0) && (child->reloads > max_reloads))
77 kill(child->pid, sig);
78 }
79}
William Lallemande25473c2019-04-01 11:29:56 +020080
81/* return 1 if a pid is a current child otherwise 0 */
William Lallemand3f128872019-04-01 11:29:59 +020082int mworker_current_child(int pid)
William Lallemande25473c2019-04-01 11:29:56 +020083{
William Lallemand3f128872019-04-01 11:29:59 +020084 struct mworker_proc *child;
William Lallemande25473c2019-04-01 11:29:56 +020085
William Lallemand3f128872019-04-01 11:29:59 +020086 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +020087 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 +020088 return 1;
89 }
90 return 0;
91}
92
William Lallemand3f128872019-04-01 11:29:59 +020093/*
94 * Return the number of new and old children (including workers and external
95 * processes)
96 */
97int mworker_child_nb()
98{
99 struct mworker_proc *child;
100 int ret = 0;
101
102 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200103 if (child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG))
William Lallemand3f128872019-04-01 11:29:59 +0200104 ret++;
105 }
106
107 return ret;
108}
109
110
William Lallemande25473c2019-04-01 11:29:56 +0200111/*
William Lallemand48dfbbd2019-04-01 11:29:53 +0200112 * serialize the proc list and put it in the environment
113 */
114void mworker_proc_list_to_env()
115{
116 char *msg = NULL;
117 struct mworker_proc *child;
William Lallemand14b98ef2022-07-27 11:57:12 +0200118 int minreloads = INT_MAX; /* minimum number of reloads to chose which processes are "current" ones */
William Lallemand48dfbbd2019-04-01 11:29:53 +0200119
120 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200121 char type = '?';
122
123 if (child->options & PROC_O_TYPE_MASTER)
124 type = 'm';
125 else if (child->options & PROC_O_TYPE_PROG)
126 type = 'e';
127 else if (child->options &= PROC_O_TYPE_WORKER)
128 type = 'w';
129
William Lallemand14b98ef2022-07-27 11:57:12 +0200130 if (child->reloads < minreloads)
131 minreloads = child->reloads;
132
William Lallemand48dfbbd2019-04-01 11:29:53 +0200133 if (child->pid > -1)
William Lallemandec059c22022-09-22 17:26:23 +0200134 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 +0200135 }
136 if (msg)
137 setenv("HAPROXY_PROCESSES", msg, 1);
William Lallemand14b98ef2022-07-27 11:57:12 +0200138
139 list_for_each_entry(child, &proc_list, list) {
140 if (child->reloads > minreloads && !(child->options & PROC_O_TYPE_MASTER)) {
141 child->options |= PROC_O_LEAVING;
142 }
143 }
144
145
William Lallemand48dfbbd2019-04-01 11:29:53 +0200146}
147
William Lallemand56be0e02022-01-28 21:11:41 +0100148struct mworker_proc *mworker_proc_new()
149{
150 struct mworker_proc *child;
151
152 child = calloc(1, sizeof(*child));
153 if (!child)
154 return NULL;
155
156 child->failedreloads = 0;
157 child->reloads = 0;
158 child->pid = -1;
159 child->ipc_fd[0] = -1;
160 child->ipc_fd[1] = -1;
161 child->timestamp = -1;
162
163 return child;
164}
165
166
William Lallemand48dfbbd2019-04-01 11:29:53 +0200167/*
168 * unserialize the proc list from the environment
William Lallemandd27f4572023-02-21 12:44:56 +0100169 * Return < 0 upon error.
William Lallemand48dfbbd2019-04-01 11:29:53 +0200170 */
Remi Tricot-Le Breton1f4fa902021-05-19 10:45:12 +0200171int mworker_env_to_proc_list()
William Lallemand48dfbbd2019-04-01 11:29:53 +0200172{
William Lallemandd27f4572023-02-21 12:44:56 +0100173 char *env, *msg, *omsg = NULL, *token = NULL, *s1;
William Lallemand90034bb2021-11-10 11:26:14 +0100174 struct mworker_proc *child;
175 int minreloads = INT_MAX; /* minimum number of reloads to chose which processes are "current" ones */
William Lallemandd27f4572023-02-21 12:44:56 +0100176 int err = 0;
William Lallemand48dfbbd2019-04-01 11:29:53 +0200177
William Lallemandd27f4572023-02-21 12:44:56 +0100178 env = getenv("HAPROXY_PROCESSES");
179 if (!env)
William Lallemande16d3202023-02-21 13:17:24 +0100180 goto no_env;
William Lallemand48dfbbd2019-04-01 11:29:53 +0200181
William Lallemandd27f4572023-02-21 12:44:56 +0100182 omsg = msg = strdup(env);
183 if (!msg) {
184 ha_alert("Out of memory while trying to allocate a worker process structure.");
185 err = -1;
186 goto out;
187 }
188
William Lallemand48dfbbd2019-04-01 11:29:53 +0200189 while ((token = strtok_r(msg, "|", &s1))) {
William Lallemand48dfbbd2019-04-01 11:29:53 +0200190 char *subtoken = NULL;
191 char *s2;
192
193 msg = NULL;
194
William Lallemand56be0e02022-01-28 21:11:41 +0100195 child = mworker_proc_new();
Remi Tricot-Le Breton1f4fa902021-05-19 10:45:12 +0200196 if (!child) {
William Lallemandd27f4572023-02-21 12:44:56 +0100197 ha_alert("out of memory while trying to allocate a worker process structure.");
198 err = -1;
199 goto out;
Remi Tricot-Le Breton1f4fa902021-05-19 10:45:12 +0200200 }
William Lallemand48dfbbd2019-04-01 11:29:53 +0200201
202 while ((subtoken = strtok_r(token, ";", &s2))) {
203
204 token = NULL;
205
206 if (strncmp(subtoken, "type=", 5) == 0) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200207 char type;
208
209 type = *(subtoken+5);
210 if (type == 'm') { /* we are in the master, assign it */
William Lallemand48dfbbd2019-04-01 11:29:53 +0200211 proc_self = child;
William Lallemand8f7069a2019-04-12 16:09:23 +0200212 child->options |= PROC_O_TYPE_MASTER;
213 } else if (type == 'e') {
214 child->options |= PROC_O_TYPE_PROG;
215 } else if (type == 'w') {
216 child->options |= PROC_O_TYPE_WORKER;
217 }
218
William Lallemand48dfbbd2019-04-01 11:29:53 +0200219 } else if (strncmp(subtoken, "fd=", 3) == 0) {
220 child->ipc_fd[0] = atoi(subtoken+3);
William Lallemandec059c22022-09-22 17:26:23 +0200221 } else if (strncmp(subtoken, "cfd=", 4) == 0) {
222 child->ipc_fd[1] = atoi(subtoken+4);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200223 } else if (strncmp(subtoken, "pid=", 4) == 0) {
224 child->pid = atoi(subtoken+4);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200225 } else if (strncmp(subtoken, "reloads=", 8) == 0) {
William Lallemandad221f42021-11-09 18:43:59 +0100226 /* we only increment the number of asked reload */
227 child->reloads = atoi(subtoken+8);
William Lallemand90034bb2021-11-10 11:26:14 +0100228
229 if (child->reloads < minreloads)
230 minreloads = child->reloads;
William Lallemand68836742021-11-10 10:49:06 +0100231 } else if (strncmp(subtoken, "failedreloads=", 14) == 0) {
232 child->failedreloads = atoi(subtoken+14);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200233 } else if (strncmp(subtoken, "timestamp=", 10) == 0) {
234 child->timestamp = atoi(subtoken+10);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200235 } else if (strncmp(subtoken, "id=", 3) == 0) {
236 child->id = strdup(subtoken+3);
William Lallemand1dc69632019-06-12 19:11:33 +0200237 } else if (strncmp(subtoken, "version=", 8) == 0) {
238 child->version = strdup(subtoken+8);
William Lallemand48dfbbd2019-04-01 11:29:53 +0200239 }
240 }
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200241 if (child->pid) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200242 LIST_APPEND(&proc_list, &child->list);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200243 } else {
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200244 mworker_free_child(child);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200245 }
William Lallemand48dfbbd2019-04-01 11:29:53 +0200246 }
247
William Lallemand90034bb2021-11-10 11:26:14 +0100248 /* set the leaving processes once we know which number of reloads are the current processes */
249
250 list_for_each_entry(child, &proc_list, list) {
251 if (child->reloads > minreloads)
252 child->options |= PROC_O_LEAVING;
253 }
254
William Lallemand48dfbbd2019-04-01 11:29:53 +0200255 unsetenv("HAPROXY_PROCESSES");
Remi Tricot-Le Breton1f4fa902021-05-19 10:45:12 +0200256
William Lallemande16d3202023-02-21 13:17:24 +0100257no_env:
258
259 if (!proc_self) {
260
261 proc_self = mworker_proc_new();
262 if (!proc_self) {
263 ha_alert("Cannot allocate process structures.\n");
264 err = -1;
265 goto out;
266 }
267 proc_self->options |= PROC_O_TYPE_MASTER;
268 proc_self->pid = pid;
269 proc_self->timestamp = 0; /* we don't know the startime anymore */
270
271 LIST_APPEND(&proc_list, &proc_self->list);
272 ha_warning("The master internals are corrupted or it was started with a too old version (< 1.9). Please restart the master process.\n");
273 }
274
William Lallemandd27f4572023-02-21 12:44:56 +0100275out:
276 free(omsg);
277 return err;
William Lallemand48dfbbd2019-04-01 11:29:53 +0200278}
William Lallemand3cd95d22019-04-01 11:29:54 +0200279
280/* Signal blocking and unblocking */
281
282void mworker_block_signals()
283{
284 sigset_t set;
285
286 sigemptyset(&set);
287 sigaddset(&set, SIGUSR1);
288 sigaddset(&set, SIGUSR2);
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100289 sigaddset(&set, SIGTTIN);
290 sigaddset(&set, SIGTTOU);
William Lallemand3cd95d22019-04-01 11:29:54 +0200291 sigaddset(&set, SIGHUP);
292 sigaddset(&set, SIGCHLD);
293 ha_sigmask(SIG_SETMASK, &set, NULL);
294}
295
296void mworker_unblock_signals()
297{
298 haproxy_unblock_signals();
299}
William Lallemand3fa724d2019-04-01 11:29:55 +0200300
William Lallemande25473c2019-04-01 11:29:56 +0200301/* ----- mworker signal handlers ----- */
302
Willy Tarreaud26c9f92019-12-11 14:24:07 +0100303/* broadcast the configured signal to the workers */
304void mworker_broadcast_signal(struct sig_handler *sh)
305{
306 mworker_kill(sh->arg);
307}
308
William Lallemande25473c2019-04-01 11:29:56 +0200309/*
310 * When called, this function reexec haproxy with -sf followed by current
311 * children PIDs and possibly old children PIDs if they didn't leave yet.
312 */
313void mworker_catch_sighup(struct sig_handler *sh)
314{
315 mworker_reload();
316}
317
318void mworker_catch_sigterm(struct sig_handler *sh)
319{
320 int sig = sh->arg;
321
322#if defined(USE_SYSTEMD)
323 if (global.tune.options & GTUNE_USE_SYSTEMD) {
324 sd_notify(0, "STOPPING=1");
325 }
326#endif
327 ha_warning("Exiting Master process...\n");
328 mworker_kill(sig);
329}
330
331/*
332 * Wait for every children to exit
333 */
334
335void mworker_catch_sigchld(struct sig_handler *sh)
336{
337 int exitpid = -1;
338 int status = 0;
William Lallemande25473c2019-04-01 11:29:56 +0200339 int childfound;
340
341restart_wait:
342
343 childfound = 0;
344
345 exitpid = waitpid(-1, &status, WNOHANG);
346 if (exitpid > 0) {
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200347 struct mworker_proc *child, *it;
348
William Lallemande25473c2019-04-01 11:29:56 +0200349 if (WIFEXITED(status))
350 status = WEXITSTATUS(status);
351 else if (WIFSIGNALED(status))
352 status = 128 + WTERMSIG(status);
353 else if (WIFSTOPPED(status))
354 status = 128 + WSTOPSIG(status);
355 else
356 status = 255;
357
William Lallemand3f128872019-04-01 11:29:59 +0200358 /* delete the child from the process list */
William Lallemande25473c2019-04-01 11:29:56 +0200359 list_for_each_entry_safe(child, it, &proc_list, list) {
360 if (child->pid != exitpid)
361 continue;
362
Willy Tarreau2b718102021-04-21 07:32:39 +0200363 LIST_DELETE(&child->list);
William Lallemande25473c2019-04-01 11:29:56 +0200364 close(child->ipc_fd[0]);
365 childfound = 1;
366 break;
367 }
368
William Lallemand3f128872019-04-01 11:29:59 +0200369 if (!childfound) {
370 /* 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 +0200371 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 +0200372 } else {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200373 /* check if exited child is a current child */
William Lallemand45286112019-04-12 16:09:21 +0200374 if (!(child->options & PROC_O_LEAVING)) {
William Lallemanda655ba42020-05-06 17:27:03 +0200375 if (child->options & PROC_O_TYPE_WORKER) {
376 if (status < 128)
William Lallemand5d71a6b2021-11-09 15:25:31 +0100377 ha_warning("Current worker (%d) exited with code %d (%s)\n", exitpid, status, "Exit");
William Lallemanda655ba42020-05-06 17:27:03 +0200378 else
William Lallemand5d71a6b2021-11-09 15:25:31 +0100379 ha_alert("Current worker (%d) exited with code %d (%s)\n", exitpid, status, strsignal(status - 128));
William Lallemanda655ba42020-05-06 17:27:03 +0200380 }
William Lallemand8f7069a2019-04-12 16:09:23 +0200381 else if (child->options & PROC_O_TYPE_PROG)
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200382 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 +0200383
William Lallemande25473c2019-04-01 11:29:56 +0200384 if (status != 0 && status != 130 && status != 143
385 && !(global.tune.options & GTUNE_NOEXIT_ONFAILURE)) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200386 ha_alert("exit-on-failure: killing every processes with SIGTERM\n");
William Lallemande25473c2019-04-01 11:29:56 +0200387 mworker_kill(SIGTERM);
388 }
William Lallemand74f0ec32019-04-16 17:42:44 +0200389 /* 0 & SIGTERM (143) are normal, but we should report SIGINT (130) and other signals */
390 if (exitcode < 0 && status != 0 && status != 143)
391 exitcode = status;
William Lallemande25473c2019-04-01 11:29:56 +0200392 } else {
William Lallemand8f7069a2019-04-12 16:09:23 +0200393 if (child->options & PROC_O_TYPE_WORKER) {
William Lallemand5d71a6b2021-11-09 15:25:31 +0100394 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 +0200395 delete_oldpid(exitpid);
William Lallemand8f7069a2019-04-12 16:09:23 +0200396 } else if (child->options & PROC_O_TYPE_PROG) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200397 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 +0200398 }
William Lallemande25473c2019-04-01 11:29:56 +0200399 }
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200400 mworker_free_child(child);
401 child = NULL;
William Lallemande25473c2019-04-01 11:29:56 +0200402 }
403
404 /* do it again to check if it was the last worker */
405 goto restart_wait;
406 }
407 /* Better rely on the system than on a list of process to check if it was the last one */
408 else if (exitpid == -1 && errno == ECHILD) {
William Lallemand4cf4b332019-04-16 17:42:43 +0200409 ha_warning("All workers exited. Exiting... (%d)\n", (exitcode > 0) ? exitcode : EXIT_SUCCESS);
William Lallemande25473c2019-04-01 11:29:56 +0200410 atexit_flag = 0;
411 if (exitcode > 0)
William Lallemand4cf4b332019-04-16 17:42:43 +0200412 exit(exitcode); /* parent must leave using the status code that provoked the exit */
413 exit(EXIT_SUCCESS);
William Lallemande25473c2019-04-01 11:29:56 +0200414 }
415
416}
417
William Lallemand3fa724d2019-04-01 11:29:55 +0200418/* ----- IPC FD (sockpair) related ----- */
419
420/* This wrapper is called from the workers. It is registered instead of the
421 * normal listener_accept() so the worker can exit() when it detects that the
422 * master closed the IPC FD. If it's not a close, we just call the regular
Willy Tarreaua74cb382020-10-15 21:29:49 +0200423 * listener_accept() function.
424 */
William Lallemand3fa724d2019-04-01 11:29:55 +0200425void mworker_accept_wrapper(int fd)
426{
427 char c;
428 int ret;
429
430 while (1) {
431 ret = recv(fd, &c, 1, MSG_PEEK);
432 if (ret == -1) {
433 if (errno == EINTR)
434 continue;
Willy Tarreauacef5e22022-04-25 20:32:15 +0200435 if (errno == EAGAIN || errno == EWOULDBLOCK) {
William Lallemand3fa724d2019-04-01 11:29:55 +0200436 fd_cant_recv(fd);
437 return;
438 }
439 break;
440 } else if (ret > 0) {
Willy Tarreaua74cb382020-10-15 21:29:49 +0200441 struct listener *l = fdtab[fd].owner;
442
443 if (l)
444 listener_accept(l);
William Lallemand3fa724d2019-04-01 11:29:55 +0200445 return;
446 } else if (ret == 0) {
447 /* At this step the master is down before
448 * this worker perform a 'normal' exit.
449 * So we want to exit with an error but
450 * other threads could currently process
451 * some stuff so we can't perform a clean
452 * deinit().
453 */
454 exit(EXIT_FAILURE);
455 }
456 }
457 return;
458}
459
460/*
Willy Tarreau619a95f2019-05-20 11:12:15 +0200461 * This function registers the accept wrapper for the sockpair of the master
462 * worker. It's only handled by worker thread #0. Other threads and master do
463 * nothing here. It always returns 1 (success).
William Lallemand3fa724d2019-04-01 11:29:55 +0200464 */
William Lallemand2ee490f2022-07-05 09:04:03 +0200465static int mworker_sockpair_register_per_thread()
William Lallemand3fa724d2019-04-01 11:29:55 +0200466{
Willy Tarreau619a95f2019-05-20 11:12:15 +0200467 if (!(global.mode & MODE_MWORKER) || master)
468 return 1;
469
470 if (tid != 0)
471 return 1;
William Lallemand3fa724d2019-04-01 11:29:55 +0200472
William Lallemandcc5b9fa2023-02-21 13:41:24 +0100473 if (proc_self->ipc_fd[1] < 0) /* proc_self was incomplete and we can't find the socketpair */
474 return 1;
475
Willy Tarreau38247432022-04-26 10:24:14 +0200476 fd_set_nonblock(proc_self->ipc_fd[1]);
William Lallemand2ee490f2022-07-05 09:04:03 +0200477 /* register the wrapper to handle read 0 when the master exits */
William Lallemand34aae2f2022-07-05 00:55:09 +0200478 fdtab[proc_self->ipc_fd[1]].iocb = mworker_accept_wrapper;
William Lallemand3fa724d2019-04-01 11:29:55 +0200479 fd_want_recv(proc_self->ipc_fd[1]);
Willy Tarreau619a95f2019-05-20 11:12:15 +0200480 return 1;
William Lallemand3fa724d2019-04-01 11:29:55 +0200481}
William Lallemand9001ce82019-04-01 11:29:57 +0200482
William Lallemand2ee490f2022-07-05 09:04:03 +0200483REGISTER_PER_THREAD_INIT(mworker_sockpair_register_per_thread);
Willy Tarreau619a95f2019-05-20 11:12:15 +0200484
William Lallemand9001ce82019-04-01 11:29:57 +0200485/* ----- proxies ----- */
486/*
487 * Upon a reload, the master worker needs to close all listeners FDs but the mworker_pipe
488 * fd, and the FD provided by fd@
489 */
490void mworker_cleanlisteners()
491{
492 struct listener *l, *l_next;
493 struct proxy *curproxy;
494 struct peers *curpeers;
495
Aurelien DARRAGON1412d312022-11-23 19:56:35 +0100496 /* peers proxies cleanup */
William Lallemand9001ce82019-04-01 11:29:57 +0200497 for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) {
498 if (!curpeers->peers_fe)
499 continue;
500
501 stop_proxy(curpeers->peers_fe);
502 /* disable this peer section so that it kills itself */
William Lallemand035058e2022-12-07 15:21:24 +0100503 if (curpeers->sighandler)
504 signal_unregister_handler(curpeers->sighandler);
Tim Duesterhusfe83f582023-04-22 17:47:34 +0200505 task_destroy(curpeers->sync_task);
William Lallemand9001ce82019-04-01 11:29:57 +0200506 curpeers->sync_task = NULL;
William Lallemand9001ce82019-04-01 11:29:57 +0200507 curpeers->peers_fe = NULL;
508 }
509
Aurelien DARRAGON1412d312022-11-23 19:56:35 +0100510 /* main proxies cleanup */
William Lallemand9001ce82019-04-01 11:29:57 +0200511 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
512 int listen_in_master = 0;
513
514 list_for_each_entry_safe(l, l_next, &curproxy->conf.listeners, by_fe) {
515 /* remove the listener, but not those we need in the master... */
Willy Tarreau18c20d22020-10-09 16:11:46 +0200516 if (!(l->rx.flags & RX_F_MWORKER)) {
Willy Tarreau75c98d12020-10-09 15:55:23 +0200517 unbind_listener(l);
William Lallemand9001ce82019-04-01 11:29:57 +0200518 delete_listener(l);
519 } else {
520 listen_in_master = 1;
521 }
522 }
523 /* if the proxy shouldn't be in the master, we stop it */
524 if (!listen_in_master)
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200525 curproxy->flags |= PR_FL_DISABLED;
William Lallemand9001ce82019-04-01 11:29:57 +0200526 }
William Lallemand88dc7c52019-04-01 11:30:01 +0200527}
528
William Lallemand55a921c2022-01-28 21:17:30 +0100529/* Upon a configuration loading error some mworker_proc and FDs/server were
530 * assigned but the worker was never forked, we must close the FDs and
531 * remove the server
532 */
533void mworker_cleanup_proc()
534{
535 struct mworker_proc *child, *it;
536
537 list_for_each_entry_safe(child, it, &proc_list, list) {
538
539 if (child->pid == -1) {
540 /* Close the socketpair master side. We don't need to
541 * close the worker side, because it's stored in the
542 * GLOBAL cli listener which was supposed to be in the
543 * worker and which will be closed in
544 * mworker_cleanlisteners()
545 */
546 if (child->ipc_fd[0] > -1)
547 close(child->ipc_fd[0]);
548 if (child->srv) {
549 /* only exists if we created a master CLI listener */
550 srv_drop(child->srv);
551 }
552 LIST_DELETE(&child->list);
553 mworker_free_child(child);
554 }
555 }
556}
557
558
William Lallemand88dc7c52019-04-01 11:30:01 +0200559/* Displays workers and processes */
560static int cli_io_handler_show_proc(struct appctx *appctx)
561{
Willy Tarreauc12b3212022-05-27 11:08:15 +0200562 struct stconn *sc = appctx_sc(appctx);
William Lallemand88dc7c52019-04-01 11:30:01 +0200563 struct mworker_proc *child;
564 int old = 0;
William Lallemand5a7f83a2023-02-17 16:23:52 +0100565 int up = date.tv_sec - proc_self->timestamp;
William Lallemande8669fc2019-06-12 18:21:17 +0200566 char *uptime = NULL;
William Lallemand68836742021-11-10 10:49:06 +0100567 char *reloadtxt = NULL;
William Lallemand88dc7c52019-04-01 11:30:01 +0200568
Christopher Faulet87633c32023-04-03 18:32:50 +0200569 /* FIXME: Don't watch the other side !*/
Christopher Faulet208c7122023-04-13 16:16:15 +0200570 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE))
William Lallemand88dc7c52019-04-01 11:30:01 +0200571 return 1;
572
William Lallemand5a7f83a2023-02-17 16:23:52 +0100573 if (up < 0) /* must never be negative because of clock drift */
574 up = 0;
575
William Lallemand88dc7c52019-04-01 11:30:01 +0200576 chunk_reset(&trash);
577
William Lallemand68836742021-11-10 10:49:06 +0100578 memprintf(&reloadtxt, "%d [failed: %d]", proc_self->reloads, proc_self->failedreloads);
William Lallemand5d71a6b2021-11-09 15:25:31 +0100579 chunk_printf(&trash, "#%-14s %-15s %-15s %-15s %-15s\n", "<PID>", "<type>", "<reloads>", "<uptime>", "<version>");
William Lallemande8669fc2019-06-12 18:21:17 +0200580 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand68836742021-11-10 10:49:06 +0100581 chunk_appendf(&trash, "%-15u %-15s %-15s %-15s %-15s\n", (unsigned int)getpid(), "master", reloadtxt, uptime, haproxy_version);
582 ha_free(&reloadtxt);
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100583 ha_free(&uptime);
William Lallemand88dc7c52019-04-01 11:30:01 +0200584
585 /* displays current processes */
586
587 chunk_appendf(&trash, "# workers\n");
588 list_for_each_entry(child, &proc_list, list) {
William Lallemand5a7f83a2023-02-17 16:23:52 +0100589 up = date.tv_sec - child->timestamp;
590 if (up < 0) /* must never be negative because of clock drift */
591 up = 0;
William Lallemand88dc7c52019-04-01 11:30:01 +0200592
William Lallemand8f7069a2019-04-12 16:09:23 +0200593 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200594 continue;
595
William Lallemand45286112019-04-12 16:09:21 +0200596 if (child->options & PROC_O_LEAVING) {
William Lallemand88dc7c52019-04-01 11:30:01 +0200597 old++;
598 continue;
599 }
William Lallemande8669fc2019-06-12 18:21:17 +0200600 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand5d71a6b2021-11-09 15:25:31 +0100601 chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, "worker", child->reloads, uptime, child->version);
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100602 ha_free(&uptime);
William Lallemand88dc7c52019-04-01 11:30:01 +0200603 }
604
605 /* displays old processes */
606
607 if (old) {
608 char *msg = NULL;
609
610 chunk_appendf(&trash, "# old workers\n");
611 list_for_each_entry(child, &proc_list, list) {
William Lallemand5a7f83a2023-02-17 16:23:52 +0100612 up = date.tv_sec - child->timestamp;
613 if (up <= 0) /* must never be negative because of clock drift */
614 up = 0;
William Lallemand88dc7c52019-04-01 11:30:01 +0200615
William Lallemand8f7069a2019-04-12 16:09:23 +0200616 if (!(child->options & PROC_O_TYPE_WORKER))
William Lallemand88dc7c52019-04-01 11:30:01 +0200617 continue;
618
William Lallemand45286112019-04-12 16:09:21 +0200619 if (child->options & PROC_O_LEAVING) {
William Lallemande8669fc2019-06-12 18:21:17 +0200620 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand5d71a6b2021-11-09 15:25:31 +0100621 chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, "worker", child->reloads, uptime, child->version);
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100622 ha_free(&uptime);
William Lallemand88dc7c52019-04-01 11:30:01 +0200623 }
624 }
625 free(msg);
626 }
627
William Lallemandad53d6d2019-04-01 11:30:03 +0200628 /* displays external process */
629 chunk_appendf(&trash, "# programs\n");
630 old = 0;
631 list_for_each_entry(child, &proc_list, list) {
William Lallemand5a7f83a2023-02-17 16:23:52 +0100632 up = date.tv_sec - child->timestamp;
633 if (up < 0) /* must never be negative because of clock drift */
634 up = 0;
William Lallemandad53d6d2019-04-01 11:30:03 +0200635
William Lallemand8f7069a2019-04-12 16:09:23 +0200636 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200637 continue;
638
William Lallemand45286112019-04-12 16:09:21 +0200639 if (child->options & PROC_O_LEAVING) {
William Lallemandad53d6d2019-04-01 11:30:03 +0200640 old++;
641 continue;
642 }
William Lallemande8669fc2019-06-12 18:21:17 +0200643 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand5d71a6b2021-11-09 15:25:31 +0100644 chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, child->id, child->reloads, uptime, "-");
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100645 ha_free(&uptime);
William Lallemandad53d6d2019-04-01 11:30:03 +0200646 }
647
648 if (old) {
649 chunk_appendf(&trash, "# old programs\n");
650 list_for_each_entry(child, &proc_list, list) {
William Lallemand5a7f83a2023-02-17 16:23:52 +0100651 up = date.tv_sec - child->timestamp;
652 if (up < 0) /* must never be negative because of clock drift */
653 up = 0;
William Lallemandad53d6d2019-04-01 11:30:03 +0200654
William Lallemand8f7069a2019-04-12 16:09:23 +0200655 if (!(child->options & PROC_O_TYPE_PROG))
William Lallemandad53d6d2019-04-01 11:30:03 +0200656 continue;
657
William Lallemand45286112019-04-12 16:09:21 +0200658 if (child->options & PROC_O_LEAVING) {
William Lallemande8669fc2019-06-12 18:21:17 +0200659 memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
William Lallemand5d71a6b2021-11-09 15:25:31 +0100660 chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, child->id, child->reloads, uptime, "-");
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100661 ha_free(&uptime);
William Lallemandad53d6d2019-04-01 11:30:03 +0200662 }
663 }
664 }
665
666
667
Willy Tarreaud0a06d52022-05-18 15:07:19 +0200668 if (applet_putchk(appctx, &trash) == -1)
William Lallemand88dc7c52019-04-01 11:30:01 +0200669 return 0;
William Lallemand88dc7c52019-04-01 11:30:01 +0200670
671 /* dump complete */
672 return 1;
William Lallemand9001ce82019-04-01 11:29:57 +0200673}
William Lallemand88dc7c52019-04-01 11:30:01 +0200674
675/* reload the master process */
676static int cli_parse_reload(char **args, char *payload, struct appctx *appctx, void *private)
677{
William Lallemandec059c22022-09-22 17:26:23 +0200678 struct stconn *scb = NULL;
679 struct stream *strm = NULL;
680 struct connection *conn = NULL;
681 int fd = -1;
682
William Lallemand88dc7c52019-04-01 11:30:01 +0200683 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
684 return 1;
685
William Lallemandec059c22022-09-22 17:26:23 +0200686 /* This ask for a synchronous reload, which means we will keep this FD
687 instead of closing it. */
688
689 scb = appctx_sc(appctx);
690 if (scb)
691 strm = sc_strm(scb);
692 if (strm && strm->scf)
693 conn = sc_conn(strm->scf);
694 if (conn)
695 fd = conn_fd(conn);
696
697 /* Send the FD of the current session to the "cli_reload" FD, which won't be polled */
698 if (fd != -1 && send_fd_uxst(proc_self->ipc_fd[0], fd) == 0) {
William Lallemand479cb3e2022-09-23 10:21:32 +0200699 fd_delete(fd); /* avoid the leak of the FD after sending it via the socketpair */
William Lallemandec059c22022-09-22 17:26:23 +0200700 }
William Lallemand88dc7c52019-04-01 11:30:01 +0200701 mworker_reload();
702
703 return 1;
704}
705
William Lallemandec1f8a62022-10-13 17:49:54 +0200706/* Displays if the current reload failed or succeed.
707 * If the startup-logs is available, dump it. */
William Lallemand2f67dd92022-10-21 14:00:05 +0200708static int cli_io_handler_show_loadstatus(struct appctx *appctx)
William Lallemand68192b22022-09-24 15:44:42 +0200709{
710 char *env;
William Lallemandec1f8a62022-10-13 17:49:54 +0200711 struct stconn *sc = appctx_sc(appctx);
William Lallemand68192b22022-09-24 15:44:42 +0200712
713 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
714 return 1;
715
Christopher Faulet87633c32023-04-03 18:32:50 +0200716 /* FIXME: Don't watch the other side !*/
Christopher Faulet208c7122023-04-13 16:16:15 +0200717 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE))
William Lallemandec1f8a62022-10-13 17:49:54 +0200718 return 1;
719
William Lallemand68192b22022-09-24 15:44:42 +0200720 env = getenv("HAPROXY_LOAD_SUCCESS");
721 if (!env)
722 return 1;
723
724 if (strcmp(env, "0") == 0) {
William Lallemandec1f8a62022-10-13 17:49:54 +0200725 chunk_printf(&trash, "Success=0\n");
William Lallemand68192b22022-09-24 15:44:42 +0200726 } else if (strcmp(env, "1") == 0) {
William Lallemandec1f8a62022-10-13 17:49:54 +0200727 chunk_printf(&trash, "Success=1\n");
William Lallemand68192b22022-09-24 15:44:42 +0200728 }
William Lallemand1344ebd2022-10-21 14:03:29 +0200729#ifdef USE_SHM_OPEN
William Lallemandec1f8a62022-10-13 17:49:54 +0200730 if (startup_logs && b_data(&startup_logs->buf) > 1)
731 chunk_appendf(&trash, "--\n");
William Lallemand68192b22022-09-24 15:44:42 +0200732
William Lallemandec1f8a62022-10-13 17:49:54 +0200733 if (applet_putchk(appctx, &trash) == -1)
734 return 0;
William Lallemand68192b22022-09-24 15:44:42 +0200735
William Lallemandec1f8a62022-10-13 17:49:54 +0200736 if (startup_logs) {
737 appctx->io_handler = NULL;
738 ring_attach_cli(startup_logs, appctx, 0);
739 return 0;
740 }
William Lallemand1344ebd2022-10-21 14:03:29 +0200741#else
742 if (applet_putchk(appctx, &trash) == -1)
743 return 0;
744#endif
William Lallemandec1f8a62022-10-13 17:49:54 +0200745 return 1;
746}
William Lallemand88dc7c52019-04-01 11:30:01 +0200747
William Lallemand27edc4b2019-05-07 17:49:33 +0200748static int mworker_parse_global_max_reloads(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +0100749 const struct proxy *defpx, const char *file, int linenum, char **err)
William Lallemand27edc4b2019-05-07 17:49:33 +0200750{
751
752 int err_code = 0;
753
754 if (alertif_too_many_args(1, file, linenum, args, &err_code))
755 goto out;
756
757 if (*(args[1]) == 0) {
758 memprintf(err, "%sparsing [%s:%d] : '%s' expects an integer argument.\n", *err, file, linenum, args[0]);
759 err_code |= ERR_ALERT | ERR_FATAL;
760 goto out;
761 }
762
763 max_reloads = atol(args[1]);
764 if (max_reloads < 0) {
765 memprintf(err, "%sparsing [%s:%d] '%s' : invalid value %d, must be >= 0", *err, file, linenum, args[0], max_reloads);
766 err_code |= ERR_ALERT | ERR_FATAL;
767 goto out;
768 }
769
770out:
771 return err_code;
772}
773
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200774void mworker_free_child(struct mworker_proc *child)
775{
William Lallemand08cb9452022-01-27 15:33:40 +0100776 int i;
777
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200778 if (child == NULL)
779 return;
780
William Lallemand08cb9452022-01-27 15:33:40 +0100781 for (i = 0; child->command && child->command[i]; i++)
782 ha_free(&child->command[i]);
William Lallemande8669fc2019-06-12 18:21:17 +0200783
William Lallemand08cb9452022-01-27 15:33:40 +0100784 ha_free(&child->command);
785 ha_free(&child->id);
786 ha_free(&child->version);
Tim Duesterhus9b7a9762019-05-16 20:23:22 +0200787 free(child);
788}
William Lallemand27edc4b2019-05-07 17:49:33 +0200789
790static struct cfg_kw_list mworker_kws = {{ }, {
791 { CFG_GLOBAL, "mworker-max-reloads", mworker_parse_global_max_reloads },
792 { 0, NULL, NULL },
793}};
794
795INITCALL1(STG_REGISTER, cfg_register_keywords, &mworker_kws);
796
797
William Lallemand88dc7c52019-04-01 11:30:01 +0200798/* register cli keywords */
799static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau23c740e2021-05-09 22:49:44 +0200800 { { "@<relative pid>", NULL }, "@<relative pid> : send a command to the <relative pid> process", NULL, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
801 { { "@!<pid>", NULL }, "@!<pid> : send a command to the <pid> process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
802 { { "@master", NULL }, "@master : send a command to the master process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
803 { { "show", "proc", NULL }, "show proc : show processes status", cli_parse_default, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY},
804 { { "reload", NULL }, "reload : reload haproxy", cli_parse_reload, NULL, NULL, NULL, ACCESS_MASTER_ONLY},
William Lallemand2f67dd92022-10-21 14:00:05 +0200805 { { "_loadstatus", NULL }, NULL, cli_parse_default, cli_io_handler_show_loadstatus, NULL, NULL, ACCESS_MASTER_ONLY},
William Lallemand88dc7c52019-04-01 11:30:01 +0200806 {{},}
807}};
808
809INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);