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