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