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