William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 1 | /* |
| 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 Jacquin | 25439de | 2021-01-21 01:31:46 +0000 | [diff] [blame] | 13 | #define _GNU_SOURCE |
| 14 | |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 15 | #include <errno.h> |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 16 | #include <signal.h> |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 19 | #include <sys/wait.h> |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 20 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 21 | #if defined(USE_SYSTEMD) |
| 22 | #include <systemd/sd-daemon.h> |
| 23 | #endif |
| 24 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 25 | #include <haproxy/api.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 26 | #include <haproxy/cfgparse.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 27 | #include <haproxy/cli.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 28 | #include <haproxy/errors.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 29 | #include <haproxy/fd.h> |
| 30 | #include <haproxy/global.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 31 | #include <haproxy/list.h> |
Willy Tarreau | 213e990 | 2020-06-04 14:58:24 +0200 | [diff] [blame] | 32 | #include <haproxy/listener.h> |
Willy Tarreau | b5abe5b | 2020-06-04 14:07:37 +0200 | [diff] [blame] | 33 | #include <haproxy/mworker.h> |
Willy Tarreau | 3c2a7c2 | 2020-06-04 18:38:21 +0200 | [diff] [blame] | 34 | #include <haproxy/peers.h> |
Willy Tarreau | 7c66857 | 2021-05-08 20:21:31 +0200 | [diff] [blame] | 35 | #include <haproxy/proxy.h> |
Willy Tarreau | 5edca2f | 2022-05-27 09:25:10 +0200 | [diff] [blame] | 36 | #include <haproxy/sc_strm.h> |
Willy Tarreau | 3727a8a | 2020-06-04 17:37:26 +0200 | [diff] [blame] | 37 | #include <haproxy/signal.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 38 | #include <haproxy/stconn.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 39 | #include <haproxy/stream.h> |
Willy Tarreau | 745e98c | 2021-05-08 13:58:19 +0200 | [diff] [blame] | 40 | #include <haproxy/tools.h> |
Willy Tarreau | d678805 | 2020-05-27 15:59:00 +0200 | [diff] [blame] | 41 | #include <haproxy/version.h> |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 42 | |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 43 | |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 44 | static int exitcode = -1; |
William Lallemand | 27edc4b | 2019-05-07 17:49:33 +0200 | [diff] [blame] | 45 | static int max_reloads = -1; /* number max of reloads a worker can have until they are killed */ |
Willy Tarreau | 15f9ac3 | 2021-05-08 12:30:50 +0200 | [diff] [blame] | 46 | struct mworker_proc *proc_self = NULL; /* process structure of current process */ |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 47 | |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 48 | /* ----- children processes handling ----- */ |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 49 | |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 50 | /* |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 51 | * Send signal to every known children. |
| 52 | */ |
| 53 | |
| 54 | static void mworker_kill(int sig) |
| 55 | { |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 56 | struct mworker_proc *child; |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 57 | |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 58 | 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 Lallemand | 32b6901 | 2019-04-16 17:42:42 +0200 | [diff] [blame] | 60 | if ((child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG)) && (child->pid > 0)) |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 61 | kill(child->pid, sig); |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
William Lallemand | 27edc4b | 2019-05-07 17:49:33 +0200 | [diff] [blame] | 65 | void 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 Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 75 | |
| 76 | /* return 1 if a pid is a current child otherwise 0 */ |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 77 | int mworker_current_child(int pid) |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 78 | { |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 79 | struct mworker_proc *child; |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 80 | |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 81 | list_for_each_entry(child, &proc_list, list) { |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 82 | if ((child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG)) && (!(child->options & PROC_O_LEAVING)) && (child->pid == pid)) |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 83 | return 1; |
| 84 | } |
| 85 | return 0; |
| 86 | } |
| 87 | |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 88 | /* |
| 89 | * Return the number of new and old children (including workers and external |
| 90 | * processes) |
| 91 | */ |
| 92 | int mworker_child_nb() |
| 93 | { |
| 94 | struct mworker_proc *child; |
| 95 | int ret = 0; |
| 96 | |
| 97 | list_for_each_entry(child, &proc_list, list) { |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 98 | if (child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG)) |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 99 | ret++; |
| 100 | } |
| 101 | |
| 102 | return ret; |
| 103 | } |
| 104 | |
| 105 | |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 106 | /* |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 107 | * serialize the proc list and put it in the environment |
| 108 | */ |
| 109 | void 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 Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 115 | 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 Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 124 | if (child->pid > -1) |
William Lallemand | 6883674 | 2021-11-10 10:49:06 +0100 | [diff] [blame] | 125 | memprintf(&msg, "%s|type=%c;fd=%d;pid=%d;reloads=%d;failedreloads=%d;timestamp=%d;id=%s;version=%s", msg ? msg : "", type, child->ipc_fd[0], child->pid, child->reloads, child->failedreloads, child->timestamp, child->id ? child->id : "", child->version); |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 126 | } |
| 127 | if (msg) |
| 128 | setenv("HAPROXY_PROCESSES", msg, 1); |
| 129 | } |
| 130 | |
William Lallemand | 56be0e0 | 2022-01-28 21:11:41 +0100 | [diff] [blame] | 131 | struct mworker_proc *mworker_proc_new() |
| 132 | { |
| 133 | struct mworker_proc *child; |
| 134 | |
| 135 | child = calloc(1, sizeof(*child)); |
| 136 | if (!child) |
| 137 | return NULL; |
| 138 | |
| 139 | child->failedreloads = 0; |
| 140 | child->reloads = 0; |
| 141 | child->pid = -1; |
| 142 | child->ipc_fd[0] = -1; |
| 143 | child->ipc_fd[1] = -1; |
| 144 | child->timestamp = -1; |
| 145 | |
| 146 | return child; |
| 147 | } |
| 148 | |
| 149 | |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 150 | /* |
| 151 | * unserialize the proc list from the environment |
| 152 | */ |
Remi Tricot-Le Breton | 1f4fa90 | 2021-05-19 10:45:12 +0200 | [diff] [blame] | 153 | int mworker_env_to_proc_list() |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 154 | { |
| 155 | char *msg, *token = NULL, *s1; |
William Lallemand | 90034bb | 2021-11-10 11:26:14 +0100 | [diff] [blame] | 156 | struct mworker_proc *child; |
| 157 | int minreloads = INT_MAX; /* minimum number of reloads to chose which processes are "current" ones */ |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 158 | |
| 159 | msg = getenv("HAPROXY_PROCESSES"); |
| 160 | if (!msg) |
Remi Tricot-Le Breton | 1f4fa90 | 2021-05-19 10:45:12 +0200 | [diff] [blame] | 161 | return 0; |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 162 | |
| 163 | while ((token = strtok_r(msg, "|", &s1))) { |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 164 | char *subtoken = NULL; |
| 165 | char *s2; |
| 166 | |
| 167 | msg = NULL; |
| 168 | |
William Lallemand | 56be0e0 | 2022-01-28 21:11:41 +0100 | [diff] [blame] | 169 | child = mworker_proc_new(); |
Remi Tricot-Le Breton | 1f4fa90 | 2021-05-19 10:45:12 +0200 | [diff] [blame] | 170 | if (!child) { |
| 171 | ha_alert("Out of memory while trying to allocate a worker process structure."); |
| 172 | return -1; |
| 173 | } |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 174 | |
| 175 | while ((subtoken = strtok_r(token, ";", &s2))) { |
| 176 | |
| 177 | token = NULL; |
| 178 | |
| 179 | if (strncmp(subtoken, "type=", 5) == 0) { |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 180 | char type; |
| 181 | |
| 182 | type = *(subtoken+5); |
| 183 | if (type == 'm') { /* we are in the master, assign it */ |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 184 | proc_self = child; |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 185 | child->options |= PROC_O_TYPE_MASTER; |
| 186 | } else if (type == 'e') { |
| 187 | child->options |= PROC_O_TYPE_PROG; |
| 188 | } else if (type == 'w') { |
| 189 | child->options |= PROC_O_TYPE_WORKER; |
| 190 | } |
| 191 | |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 192 | } else if (strncmp(subtoken, "fd=", 3) == 0) { |
| 193 | child->ipc_fd[0] = atoi(subtoken+3); |
| 194 | } else if (strncmp(subtoken, "pid=", 4) == 0) { |
| 195 | child->pid = atoi(subtoken+4); |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 196 | } else if (strncmp(subtoken, "reloads=", 8) == 0) { |
William Lallemand | ad221f4 | 2021-11-09 18:43:59 +0100 | [diff] [blame] | 197 | /* we only increment the number of asked reload */ |
| 198 | child->reloads = atoi(subtoken+8); |
William Lallemand | 90034bb | 2021-11-10 11:26:14 +0100 | [diff] [blame] | 199 | |
| 200 | if (child->reloads < minreloads) |
| 201 | minreloads = child->reloads; |
William Lallemand | 6883674 | 2021-11-10 10:49:06 +0100 | [diff] [blame] | 202 | } else if (strncmp(subtoken, "failedreloads=", 14) == 0) { |
| 203 | child->failedreloads = atoi(subtoken+14); |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 204 | } else if (strncmp(subtoken, "timestamp=", 10) == 0) { |
| 205 | child->timestamp = atoi(subtoken+10); |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 206 | } else if (strncmp(subtoken, "id=", 3) == 0) { |
| 207 | child->id = strdup(subtoken+3); |
William Lallemand | 1dc6963 | 2019-06-12 19:11:33 +0200 | [diff] [blame] | 208 | } else if (strncmp(subtoken, "version=", 8) == 0) { |
| 209 | child->version = strdup(subtoken+8); |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 210 | } |
| 211 | } |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 212 | if (child->pid) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 213 | LIST_APPEND(&proc_list, &child->list); |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 214 | } else { |
Tim Duesterhus | 9b7a976 | 2019-05-16 20:23:22 +0200 | [diff] [blame] | 215 | mworker_free_child(child); |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 216 | } |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 217 | } |
| 218 | |
William Lallemand | 90034bb | 2021-11-10 11:26:14 +0100 | [diff] [blame] | 219 | /* set the leaving processes once we know which number of reloads are the current processes */ |
| 220 | |
| 221 | list_for_each_entry(child, &proc_list, list) { |
| 222 | if (child->reloads > minreloads) |
| 223 | child->options |= PROC_O_LEAVING; |
| 224 | } |
| 225 | |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 226 | unsetenv("HAPROXY_PROCESSES"); |
Remi Tricot-Le Breton | 1f4fa90 | 2021-05-19 10:45:12 +0200 | [diff] [blame] | 227 | |
| 228 | return 0; |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 229 | } |
William Lallemand | 3cd95d2 | 2019-04-01 11:29:54 +0200 | [diff] [blame] | 230 | |
| 231 | /* Signal blocking and unblocking */ |
| 232 | |
| 233 | void mworker_block_signals() |
| 234 | { |
| 235 | sigset_t set; |
| 236 | |
| 237 | sigemptyset(&set); |
| 238 | sigaddset(&set, SIGUSR1); |
| 239 | sigaddset(&set, SIGUSR2); |
Willy Tarreau | d26c9f9 | 2019-12-11 14:24:07 +0100 | [diff] [blame] | 240 | sigaddset(&set, SIGTTIN); |
| 241 | sigaddset(&set, SIGTTOU); |
William Lallemand | 3cd95d2 | 2019-04-01 11:29:54 +0200 | [diff] [blame] | 242 | sigaddset(&set, SIGHUP); |
| 243 | sigaddset(&set, SIGCHLD); |
| 244 | ha_sigmask(SIG_SETMASK, &set, NULL); |
| 245 | } |
| 246 | |
| 247 | void mworker_unblock_signals() |
| 248 | { |
| 249 | haproxy_unblock_signals(); |
| 250 | } |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 251 | |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 252 | /* ----- mworker signal handlers ----- */ |
| 253 | |
Willy Tarreau | d26c9f9 | 2019-12-11 14:24:07 +0100 | [diff] [blame] | 254 | /* broadcast the configured signal to the workers */ |
| 255 | void mworker_broadcast_signal(struct sig_handler *sh) |
| 256 | { |
| 257 | mworker_kill(sh->arg); |
| 258 | } |
| 259 | |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 260 | /* |
| 261 | * When called, this function reexec haproxy with -sf followed by current |
| 262 | * children PIDs and possibly old children PIDs if they didn't leave yet. |
| 263 | */ |
| 264 | void mworker_catch_sighup(struct sig_handler *sh) |
| 265 | { |
| 266 | mworker_reload(); |
| 267 | } |
| 268 | |
| 269 | void mworker_catch_sigterm(struct sig_handler *sh) |
| 270 | { |
| 271 | int sig = sh->arg; |
| 272 | |
| 273 | #if defined(USE_SYSTEMD) |
| 274 | if (global.tune.options & GTUNE_USE_SYSTEMD) { |
| 275 | sd_notify(0, "STOPPING=1"); |
| 276 | } |
| 277 | #endif |
| 278 | ha_warning("Exiting Master process...\n"); |
| 279 | mworker_kill(sig); |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * Wait for every children to exit |
| 284 | */ |
| 285 | |
| 286 | void mworker_catch_sigchld(struct sig_handler *sh) |
| 287 | { |
| 288 | int exitpid = -1; |
| 289 | int status = 0; |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 290 | int childfound; |
| 291 | |
| 292 | restart_wait: |
| 293 | |
| 294 | childfound = 0; |
| 295 | |
| 296 | exitpid = waitpid(-1, &status, WNOHANG); |
| 297 | if (exitpid > 0) { |
Tim Duesterhus | 9b7a976 | 2019-05-16 20:23:22 +0200 | [diff] [blame] | 298 | struct mworker_proc *child, *it; |
| 299 | |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 300 | if (WIFEXITED(status)) |
| 301 | status = WEXITSTATUS(status); |
| 302 | else if (WIFSIGNALED(status)) |
| 303 | status = 128 + WTERMSIG(status); |
| 304 | else if (WIFSTOPPED(status)) |
| 305 | status = 128 + WSTOPSIG(status); |
| 306 | else |
| 307 | status = 255; |
| 308 | |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 309 | /* delete the child from the process list */ |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 310 | list_for_each_entry_safe(child, it, &proc_list, list) { |
| 311 | if (child->pid != exitpid) |
| 312 | continue; |
| 313 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 314 | LIST_DELETE(&child->list); |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 315 | close(child->ipc_fd[0]); |
| 316 | childfound = 1; |
| 317 | break; |
| 318 | } |
| 319 | |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 320 | if (!childfound) { |
| 321 | /* We didn't find the PID in the list, that shouldn't happen but we can emit a warning */ |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 322 | ha_warning("Process %d exited with code %d (%s)\n", exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit"); |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 323 | } else { |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 324 | /* check if exited child is a current child */ |
William Lallemand | 4528611 | 2019-04-12 16:09:21 +0200 | [diff] [blame] | 325 | if (!(child->options & PROC_O_LEAVING)) { |
William Lallemand | a655ba4 | 2020-05-06 17:27:03 +0200 | [diff] [blame] | 326 | if (child->options & PROC_O_TYPE_WORKER) { |
| 327 | if (status < 128) |
William Lallemand | 5d71a6b | 2021-11-09 15:25:31 +0100 | [diff] [blame] | 328 | ha_warning("Current worker (%d) exited with code %d (%s)\n", exitpid, status, "Exit"); |
William Lallemand | a655ba4 | 2020-05-06 17:27:03 +0200 | [diff] [blame] | 329 | else |
William Lallemand | 5d71a6b | 2021-11-09 15:25:31 +0100 | [diff] [blame] | 330 | ha_alert("Current worker (%d) exited with code %d (%s)\n", exitpid, status, strsignal(status - 128)); |
William Lallemand | a655ba4 | 2020-05-06 17:27:03 +0200 | [diff] [blame] | 331 | } |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 332 | else if (child->options & PROC_O_TYPE_PROG) |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 333 | ha_alert("Current program '%s' (%d) exited with code %d (%s)\n", child->id, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit"); |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 334 | |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 335 | if (status != 0 && status != 130 && status != 143 |
| 336 | && !(global.tune.options & GTUNE_NOEXIT_ONFAILURE)) { |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 337 | ha_alert("exit-on-failure: killing every processes with SIGTERM\n"); |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 338 | mworker_kill(SIGTERM); |
| 339 | } |
William Lallemand | 74f0ec3 | 2019-04-16 17:42:44 +0200 | [diff] [blame] | 340 | /* 0 & SIGTERM (143) are normal, but we should report SIGINT (130) and other signals */ |
| 341 | if (exitcode < 0 && status != 0 && status != 143) |
| 342 | exitcode = status; |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 343 | } else { |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 344 | if (child->options & PROC_O_TYPE_WORKER) { |
William Lallemand | 5d71a6b | 2021-11-09 15:25:31 +0100 | [diff] [blame] | 345 | ha_warning("Former worker (%d) exited with code %d (%s)\n", exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit"); |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 346 | delete_oldpid(exitpid); |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 347 | } else if (child->options & PROC_O_TYPE_PROG) { |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 348 | ha_warning("Former program '%s' (%d) exited with code %d (%s)\n", child->id, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit"); |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 349 | } |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 350 | } |
Tim Duesterhus | 9b7a976 | 2019-05-16 20:23:22 +0200 | [diff] [blame] | 351 | mworker_free_child(child); |
| 352 | child = NULL; |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /* do it again to check if it was the last worker */ |
| 356 | goto restart_wait; |
| 357 | } |
| 358 | /* Better rely on the system than on a list of process to check if it was the last one */ |
| 359 | else if (exitpid == -1 && errno == ECHILD) { |
William Lallemand | 4cf4b33 | 2019-04-16 17:42:43 +0200 | [diff] [blame] | 360 | ha_warning("All workers exited. Exiting... (%d)\n", (exitcode > 0) ? exitcode : EXIT_SUCCESS); |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 361 | atexit_flag = 0; |
| 362 | if (exitcode > 0) |
William Lallemand | 4cf4b33 | 2019-04-16 17:42:43 +0200 | [diff] [blame] | 363 | exit(exitcode); /* parent must leave using the status code that provoked the exit */ |
| 364 | exit(EXIT_SUCCESS); |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | } |
| 368 | |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 369 | /* ----- IPC FD (sockpair) related ----- */ |
| 370 | |
| 371 | /* This wrapper is called from the workers. It is registered instead of the |
| 372 | * normal listener_accept() so the worker can exit() when it detects that the |
| 373 | * master closed the IPC FD. If it's not a close, we just call the regular |
Willy Tarreau | a74cb38 | 2020-10-15 21:29:49 +0200 | [diff] [blame] | 374 | * listener_accept() function. |
| 375 | */ |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 376 | void mworker_accept_wrapper(int fd) |
| 377 | { |
| 378 | char c; |
| 379 | int ret; |
| 380 | |
| 381 | while (1) { |
| 382 | ret = recv(fd, &c, 1, MSG_PEEK); |
| 383 | if (ret == -1) { |
| 384 | if (errno == EINTR) |
| 385 | continue; |
Willy Tarreau | acef5e2 | 2022-04-25 20:32:15 +0200 | [diff] [blame] | 386 | if (errno == EAGAIN || errno == EWOULDBLOCK) { |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 387 | fd_cant_recv(fd); |
| 388 | return; |
| 389 | } |
| 390 | break; |
| 391 | } else if (ret > 0) { |
Willy Tarreau | a74cb38 | 2020-10-15 21:29:49 +0200 | [diff] [blame] | 392 | struct listener *l = fdtab[fd].owner; |
| 393 | |
| 394 | if (l) |
| 395 | listener_accept(l); |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 396 | return; |
| 397 | } else if (ret == 0) { |
| 398 | /* At this step the master is down before |
| 399 | * this worker perform a 'normal' exit. |
| 400 | * So we want to exit with an error but |
| 401 | * other threads could currently process |
| 402 | * some stuff so we can't perform a clean |
| 403 | * deinit(). |
| 404 | */ |
| 405 | exit(EXIT_FAILURE); |
| 406 | } |
| 407 | } |
| 408 | return; |
| 409 | } |
| 410 | |
| 411 | /* |
Willy Tarreau | 619a95f | 2019-05-20 11:12:15 +0200 | [diff] [blame] | 412 | * This function registers the accept wrapper for the sockpair of the master |
| 413 | * worker. It's only handled by worker thread #0. Other threads and master do |
| 414 | * nothing here. It always returns 1 (success). |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 415 | */ |
Willy Tarreau | 619a95f | 2019-05-20 11:12:15 +0200 | [diff] [blame] | 416 | static int mworker_pipe_register_per_thread() |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 417 | { |
Willy Tarreau | 619a95f | 2019-05-20 11:12:15 +0200 | [diff] [blame] | 418 | if (!(global.mode & MODE_MWORKER) || master) |
| 419 | return 1; |
| 420 | |
| 421 | if (tid != 0) |
| 422 | return 1; |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 423 | |
Willy Tarreau | 3824743 | 2022-04-26 10:24:14 +0200 | [diff] [blame] | 424 | fd_set_nonblock(proc_self->ipc_fd[1]); |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 425 | /* In multi-tread, we need only one thread to process |
| 426 | * events on the pipe with master |
| 427 | */ |
Willy Tarreau | 619a95f | 2019-05-20 11:12:15 +0200 | [diff] [blame] | 428 | fd_insert(proc_self->ipc_fd[1], fdtab[proc_self->ipc_fd[1]].owner, mworker_accept_wrapper, tid_bit); |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 429 | fd_want_recv(proc_self->ipc_fd[1]); |
Willy Tarreau | 619a95f | 2019-05-20 11:12:15 +0200 | [diff] [blame] | 430 | return 1; |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 431 | } |
William Lallemand | 9001ce8 | 2019-04-01 11:29:57 +0200 | [diff] [blame] | 432 | |
Willy Tarreau | 619a95f | 2019-05-20 11:12:15 +0200 | [diff] [blame] | 433 | REGISTER_PER_THREAD_INIT(mworker_pipe_register_per_thread); |
| 434 | |
William Lallemand | 9001ce8 | 2019-04-01 11:29:57 +0200 | [diff] [blame] | 435 | /* ----- proxies ----- */ |
| 436 | /* |
| 437 | * Upon a reload, the master worker needs to close all listeners FDs but the mworker_pipe |
| 438 | * fd, and the FD provided by fd@ |
| 439 | */ |
| 440 | void mworker_cleanlisteners() |
| 441 | { |
| 442 | struct listener *l, *l_next; |
| 443 | struct proxy *curproxy; |
| 444 | struct peers *curpeers; |
| 445 | |
| 446 | /* we might have to unbind some peers sections from some processes */ |
| 447 | for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) { |
| 448 | if (!curpeers->peers_fe) |
| 449 | continue; |
| 450 | |
| 451 | stop_proxy(curpeers->peers_fe); |
| 452 | /* disable this peer section so that it kills itself */ |
| 453 | signal_unregister_handler(curpeers->sighandler); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 454 | task_destroy(curpeers->sync_task); |
William Lallemand | 9001ce8 | 2019-04-01 11:29:57 +0200 | [diff] [blame] | 455 | curpeers->sync_task = NULL; |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 456 | task_destroy(curpeers->peers_fe->task); |
William Lallemand | 9001ce8 | 2019-04-01 11:29:57 +0200 | [diff] [blame] | 457 | curpeers->peers_fe->task = NULL; |
| 458 | curpeers->peers_fe = NULL; |
| 459 | } |
| 460 | |
| 461 | for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) { |
| 462 | int listen_in_master = 0; |
| 463 | |
| 464 | list_for_each_entry_safe(l, l_next, &curproxy->conf.listeners, by_fe) { |
| 465 | /* remove the listener, but not those we need in the master... */ |
Willy Tarreau | 18c20d2 | 2020-10-09 16:11:46 +0200 | [diff] [blame] | 466 | if (!(l->rx.flags & RX_F_MWORKER)) { |
Willy Tarreau | 75c98d1 | 2020-10-09 15:55:23 +0200 | [diff] [blame] | 467 | unbind_listener(l); |
William Lallemand | 9001ce8 | 2019-04-01 11:29:57 +0200 | [diff] [blame] | 468 | delete_listener(l); |
| 469 | } else { |
| 470 | listen_in_master = 1; |
| 471 | } |
| 472 | } |
| 473 | /* if the proxy shouldn't be in the master, we stop it */ |
| 474 | if (!listen_in_master) |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 475 | curproxy->flags |= PR_FL_DISABLED; |
William Lallemand | 9001ce8 | 2019-04-01 11:29:57 +0200 | [diff] [blame] | 476 | } |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 477 | } |
| 478 | |
William Lallemand | 55a921c | 2022-01-28 21:17:30 +0100 | [diff] [blame] | 479 | /* Upon a configuration loading error some mworker_proc and FDs/server were |
| 480 | * assigned but the worker was never forked, we must close the FDs and |
| 481 | * remove the server |
| 482 | */ |
| 483 | void mworker_cleanup_proc() |
| 484 | { |
| 485 | struct mworker_proc *child, *it; |
| 486 | |
| 487 | list_for_each_entry_safe(child, it, &proc_list, list) { |
| 488 | |
| 489 | if (child->pid == -1) { |
| 490 | /* Close the socketpair master side. We don't need to |
| 491 | * close the worker side, because it's stored in the |
| 492 | * GLOBAL cli listener which was supposed to be in the |
| 493 | * worker and which will be closed in |
| 494 | * mworker_cleanlisteners() |
| 495 | */ |
| 496 | if (child->ipc_fd[0] > -1) |
| 497 | close(child->ipc_fd[0]); |
| 498 | if (child->srv) { |
| 499 | /* only exists if we created a master CLI listener */ |
| 500 | srv_drop(child->srv); |
| 501 | } |
| 502 | LIST_DELETE(&child->list); |
| 503 | mworker_free_child(child); |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 509 | /* Displays workers and processes */ |
| 510 | static int cli_io_handler_show_proc(struct appctx *appctx) |
| 511 | { |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 512 | struct stconn *sc = appctx_sc(appctx); |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 513 | struct mworker_proc *child; |
| 514 | int old = 0; |
| 515 | int up = now.tv_sec - proc_self->timestamp; |
William Lallemand | e8669fc | 2019-06-12 18:21:17 +0200 | [diff] [blame] | 516 | char *uptime = NULL; |
William Lallemand | 6883674 | 2021-11-10 10:49:06 +0100 | [diff] [blame] | 517 | char *reloadtxt = NULL; |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 518 | |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 519 | if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 520 | return 1; |
| 521 | |
| 522 | chunk_reset(&trash); |
| 523 | |
William Lallemand | 6883674 | 2021-11-10 10:49:06 +0100 | [diff] [blame] | 524 | memprintf(&reloadtxt, "%d [failed: %d]", proc_self->reloads, proc_self->failedreloads); |
William Lallemand | 5d71a6b | 2021-11-09 15:25:31 +0100 | [diff] [blame] | 525 | chunk_printf(&trash, "#%-14s %-15s %-15s %-15s %-15s\n", "<PID>", "<type>", "<reloads>", "<uptime>", "<version>"); |
William Lallemand | e8669fc | 2019-06-12 18:21:17 +0200 | [diff] [blame] | 526 | memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60)); |
William Lallemand | 6883674 | 2021-11-10 10:49:06 +0100 | [diff] [blame] | 527 | chunk_appendf(&trash, "%-15u %-15s %-15s %-15s %-15s\n", (unsigned int)getpid(), "master", reloadtxt, uptime, haproxy_version); |
| 528 | ha_free(&reloadtxt); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 529 | ha_free(&uptime); |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 530 | |
| 531 | /* displays current processes */ |
| 532 | |
| 533 | chunk_appendf(&trash, "# workers\n"); |
| 534 | list_for_each_entry(child, &proc_list, list) { |
| 535 | up = now.tv_sec - child->timestamp; |
| 536 | |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 537 | if (!(child->options & PROC_O_TYPE_WORKER)) |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 538 | continue; |
| 539 | |
William Lallemand | 4528611 | 2019-04-12 16:09:21 +0200 | [diff] [blame] | 540 | if (child->options & PROC_O_LEAVING) { |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 541 | old++; |
| 542 | continue; |
| 543 | } |
William Lallemand | e8669fc | 2019-06-12 18:21:17 +0200 | [diff] [blame] | 544 | memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60)); |
William Lallemand | 5d71a6b | 2021-11-09 15:25:31 +0100 | [diff] [blame] | 545 | chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, "worker", child->reloads, uptime, child->version); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 546 | ha_free(&uptime); |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | /* displays old processes */ |
| 550 | |
| 551 | if (old) { |
| 552 | char *msg = NULL; |
| 553 | |
| 554 | chunk_appendf(&trash, "# old workers\n"); |
| 555 | list_for_each_entry(child, &proc_list, list) { |
| 556 | up = now.tv_sec - child->timestamp; |
| 557 | |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 558 | if (!(child->options & PROC_O_TYPE_WORKER)) |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 559 | continue; |
| 560 | |
William Lallemand | 4528611 | 2019-04-12 16:09:21 +0200 | [diff] [blame] | 561 | if (child->options & PROC_O_LEAVING) { |
William Lallemand | e8669fc | 2019-06-12 18:21:17 +0200 | [diff] [blame] | 562 | memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60)); |
William Lallemand | 5d71a6b | 2021-11-09 15:25:31 +0100 | [diff] [blame] | 563 | chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, "worker", child->reloads, uptime, child->version); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 564 | ha_free(&uptime); |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | free(msg); |
| 568 | } |
| 569 | |
William Lallemand | ad53d6d | 2019-04-01 11:30:03 +0200 | [diff] [blame] | 570 | /* displays external process */ |
| 571 | chunk_appendf(&trash, "# programs\n"); |
| 572 | old = 0; |
| 573 | list_for_each_entry(child, &proc_list, list) { |
| 574 | up = now.tv_sec - child->timestamp; |
| 575 | |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 576 | if (!(child->options & PROC_O_TYPE_PROG)) |
William Lallemand | ad53d6d | 2019-04-01 11:30:03 +0200 | [diff] [blame] | 577 | continue; |
| 578 | |
William Lallemand | 4528611 | 2019-04-12 16:09:21 +0200 | [diff] [blame] | 579 | if (child->options & PROC_O_LEAVING) { |
William Lallemand | ad53d6d | 2019-04-01 11:30:03 +0200 | [diff] [blame] | 580 | old++; |
| 581 | continue; |
| 582 | } |
William Lallemand | e8669fc | 2019-06-12 18:21:17 +0200 | [diff] [blame] | 583 | memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60)); |
William Lallemand | 5d71a6b | 2021-11-09 15:25:31 +0100 | [diff] [blame] | 584 | chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, child->id, child->reloads, uptime, "-"); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 585 | ha_free(&uptime); |
William Lallemand | ad53d6d | 2019-04-01 11:30:03 +0200 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | if (old) { |
| 589 | chunk_appendf(&trash, "# old programs\n"); |
| 590 | list_for_each_entry(child, &proc_list, list) { |
| 591 | up = now.tv_sec - child->timestamp; |
| 592 | |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 593 | if (!(child->options & PROC_O_TYPE_PROG)) |
William Lallemand | ad53d6d | 2019-04-01 11:30:03 +0200 | [diff] [blame] | 594 | continue; |
| 595 | |
William Lallemand | 4528611 | 2019-04-12 16:09:21 +0200 | [diff] [blame] | 596 | if (child->options & PROC_O_LEAVING) { |
William Lallemand | e8669fc | 2019-06-12 18:21:17 +0200 | [diff] [blame] | 597 | memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60)); |
William Lallemand | 5d71a6b | 2021-11-09 15:25:31 +0100 | [diff] [blame] | 598 | chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, child->id, child->reloads, uptime, "-"); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 599 | ha_free(&uptime); |
William Lallemand | ad53d6d | 2019-04-01 11:30:03 +0200 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | |
| 605 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 606 | if (applet_putchk(appctx, &trash) == -1) |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 607 | return 0; |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 608 | |
| 609 | /* dump complete */ |
| 610 | return 1; |
William Lallemand | 9001ce8 | 2019-04-01 11:29:57 +0200 | [diff] [blame] | 611 | } |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 612 | |
| 613 | /* reload the master process */ |
| 614 | static int cli_parse_reload(char **args, char *payload, struct appctx *appctx, void *private) |
| 615 | { |
| 616 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 617 | return 1; |
| 618 | |
| 619 | mworker_reload(); |
| 620 | |
| 621 | return 1; |
| 622 | } |
| 623 | |
| 624 | |
William Lallemand | 27edc4b | 2019-05-07 17:49:33 +0200 | [diff] [blame] | 625 | static int mworker_parse_global_max_reloads(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 626 | const struct proxy *defpx, const char *file, int linenum, char **err) |
William Lallemand | 27edc4b | 2019-05-07 17:49:33 +0200 | [diff] [blame] | 627 | { |
| 628 | |
| 629 | int err_code = 0; |
| 630 | |
| 631 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 632 | goto out; |
| 633 | |
| 634 | if (*(args[1]) == 0) { |
| 635 | memprintf(err, "%sparsing [%s:%d] : '%s' expects an integer argument.\n", *err, file, linenum, args[0]); |
| 636 | err_code |= ERR_ALERT | ERR_FATAL; |
| 637 | goto out; |
| 638 | } |
| 639 | |
| 640 | max_reloads = atol(args[1]); |
| 641 | if (max_reloads < 0) { |
| 642 | memprintf(err, "%sparsing [%s:%d] '%s' : invalid value %d, must be >= 0", *err, file, linenum, args[0], max_reloads); |
| 643 | err_code |= ERR_ALERT | ERR_FATAL; |
| 644 | goto out; |
| 645 | } |
| 646 | |
| 647 | out: |
| 648 | return err_code; |
| 649 | } |
| 650 | |
Tim Duesterhus | 9b7a976 | 2019-05-16 20:23:22 +0200 | [diff] [blame] | 651 | void mworker_free_child(struct mworker_proc *child) |
| 652 | { |
William Lallemand | 08cb945 | 2022-01-27 15:33:40 +0100 | [diff] [blame] | 653 | int i; |
| 654 | |
Tim Duesterhus | 9b7a976 | 2019-05-16 20:23:22 +0200 | [diff] [blame] | 655 | if (child == NULL) |
| 656 | return; |
| 657 | |
William Lallemand | 08cb945 | 2022-01-27 15:33:40 +0100 | [diff] [blame] | 658 | for (i = 0; child->command && child->command[i]; i++) |
| 659 | ha_free(&child->command[i]); |
William Lallemand | e8669fc | 2019-06-12 18:21:17 +0200 | [diff] [blame] | 660 | |
William Lallemand | 08cb945 | 2022-01-27 15:33:40 +0100 | [diff] [blame] | 661 | ha_free(&child->command); |
| 662 | ha_free(&child->id); |
| 663 | ha_free(&child->version); |
Tim Duesterhus | 9b7a976 | 2019-05-16 20:23:22 +0200 | [diff] [blame] | 664 | free(child); |
| 665 | } |
William Lallemand | 27edc4b | 2019-05-07 17:49:33 +0200 | [diff] [blame] | 666 | |
| 667 | static struct cfg_kw_list mworker_kws = {{ }, { |
| 668 | { CFG_GLOBAL, "mworker-max-reloads", mworker_parse_global_max_reloads }, |
| 669 | { 0, NULL, NULL }, |
| 670 | }}; |
| 671 | |
| 672 | INITCALL1(STG_REGISTER, cfg_register_keywords, &mworker_kws); |
| 673 | |
| 674 | |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 675 | /* register cli keywords */ |
| 676 | static struct cli_kw_list cli_kws = {{ },{ |
Willy Tarreau | 23c740e | 2021-05-09 22:49:44 +0200 | [diff] [blame] | 677 | { { "@<relative pid>", NULL }, "@<relative pid> : send a command to the <relative pid> process", NULL, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY}, |
| 678 | { { "@!<pid>", NULL }, "@!<pid> : send a command to the <pid> process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY}, |
| 679 | { { "@master", NULL }, "@master : send a command to the master process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY}, |
| 680 | { { "show", "proc", NULL }, "show proc : show processes status", cli_parse_default, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY}, |
| 681 | { { "reload", NULL }, "reload : reload haproxy", cli_parse_reload, NULL, NULL, NULL, ACCESS_MASTER_ONLY}, |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 682 | {{},} |
| 683 | }}; |
| 684 | |
| 685 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |