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