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); |
| 207 | sigaddset(&set, SIGHUP); |
| 208 | sigaddset(&set, SIGCHLD); |
| 209 | ha_sigmask(SIG_SETMASK, &set, NULL); |
| 210 | } |
| 211 | |
| 212 | void mworker_unblock_signals() |
| 213 | { |
| 214 | haproxy_unblock_signals(); |
| 215 | } |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 216 | |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 217 | /* ----- mworker signal handlers ----- */ |
| 218 | |
| 219 | /* |
| 220 | * When called, this function reexec haproxy with -sf followed by current |
| 221 | * children PIDs and possibly old children PIDs if they didn't leave yet. |
| 222 | */ |
| 223 | void mworker_catch_sighup(struct sig_handler *sh) |
| 224 | { |
| 225 | mworker_reload(); |
| 226 | } |
| 227 | |
| 228 | void mworker_catch_sigterm(struct sig_handler *sh) |
| 229 | { |
| 230 | int sig = sh->arg; |
| 231 | |
| 232 | #if defined(USE_SYSTEMD) |
| 233 | if (global.tune.options & GTUNE_USE_SYSTEMD) { |
| 234 | sd_notify(0, "STOPPING=1"); |
| 235 | } |
| 236 | #endif |
| 237 | ha_warning("Exiting Master process...\n"); |
| 238 | mworker_kill(sig); |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Wait for every children to exit |
| 243 | */ |
| 244 | |
| 245 | void mworker_catch_sigchld(struct sig_handler *sh) |
| 246 | { |
| 247 | int exitpid = -1; |
| 248 | int status = 0; |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 249 | int childfound; |
| 250 | |
| 251 | restart_wait: |
| 252 | |
| 253 | childfound = 0; |
| 254 | |
| 255 | exitpid = waitpid(-1, &status, WNOHANG); |
| 256 | if (exitpid > 0) { |
Tim Duesterhus | 9b7a976 | 2019-05-16 20:23:22 +0200 | [diff] [blame] | 257 | struct mworker_proc *child, *it; |
| 258 | |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 259 | if (WIFEXITED(status)) |
| 260 | status = WEXITSTATUS(status); |
| 261 | else if (WIFSIGNALED(status)) |
| 262 | status = 128 + WTERMSIG(status); |
| 263 | else if (WIFSTOPPED(status)) |
| 264 | status = 128 + WSTOPSIG(status); |
| 265 | else |
| 266 | status = 255; |
| 267 | |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 268 | /* delete the child from the process list */ |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 269 | list_for_each_entry_safe(child, it, &proc_list, list) { |
| 270 | if (child->pid != exitpid) |
| 271 | continue; |
| 272 | |
| 273 | LIST_DEL(&child->list); |
| 274 | close(child->ipc_fd[0]); |
| 275 | childfound = 1; |
| 276 | break; |
| 277 | } |
| 278 | |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 279 | if (!childfound) { |
| 280 | /* 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] | 281 | 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] | 282 | } else { |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 283 | /* check if exited child is a current child */ |
William Lallemand | 4528611 | 2019-04-12 16:09:21 +0200 | [diff] [blame] | 284 | if (!(child->options & PROC_O_LEAVING)) { |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 285 | if (child->options & PROC_O_TYPE_WORKER) |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 286 | 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] | 287 | else if (child->options & PROC_O_TYPE_PROG) |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 288 | 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] | 289 | |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 290 | if (status != 0 && status != 130 && status != 143 |
| 291 | && !(global.tune.options & GTUNE_NOEXIT_ONFAILURE)) { |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 292 | ha_alert("exit-on-failure: killing every processes with SIGTERM\n"); |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 293 | mworker_kill(SIGTERM); |
| 294 | } |
William Lallemand | 74f0ec3 | 2019-04-16 17:42:44 +0200 | [diff] [blame] | 295 | /* 0 & SIGTERM (143) are normal, but we should report SIGINT (130) and other signals */ |
| 296 | if (exitcode < 0 && status != 0 && status != 143) |
| 297 | exitcode = status; |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 298 | } else { |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 299 | if (child->options & PROC_O_TYPE_WORKER) { |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 300 | ha_warning("Former worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit"); |
| 301 | delete_oldpid(exitpid); |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 302 | } else if (child->options & PROC_O_TYPE_PROG) { |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 303 | 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] | 304 | } |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 305 | } |
Tim Duesterhus | 9b7a976 | 2019-05-16 20:23:22 +0200 | [diff] [blame] | 306 | mworker_free_child(child); |
| 307 | child = NULL; |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /* do it again to check if it was the last worker */ |
| 311 | goto restart_wait; |
| 312 | } |
| 313 | /* Better rely on the system than on a list of process to check if it was the last one */ |
| 314 | else if (exitpid == -1 && errno == ECHILD) { |
William Lallemand | 4cf4b33 | 2019-04-16 17:42:43 +0200 | [diff] [blame] | 315 | 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] | 316 | atexit_flag = 0; |
| 317 | if (exitcode > 0) |
William Lallemand | 4cf4b33 | 2019-04-16 17:42:43 +0200 | [diff] [blame] | 318 | exit(exitcode); /* parent must leave using the status code that provoked the exit */ |
| 319 | exit(EXIT_SUCCESS); |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | } |
| 323 | |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 324 | /* ----- IPC FD (sockpair) related ----- */ |
| 325 | |
| 326 | /* This wrapper is called from the workers. It is registered instead of the |
| 327 | * normal listener_accept() so the worker can exit() when it detects that the |
| 328 | * master closed the IPC FD. If it's not a close, we just call the regular |
| 329 | * listener_accept() function */ |
| 330 | void mworker_accept_wrapper(int fd) |
| 331 | { |
| 332 | char c; |
| 333 | int ret; |
| 334 | |
| 335 | while (1) { |
| 336 | ret = recv(fd, &c, 1, MSG_PEEK); |
| 337 | if (ret == -1) { |
| 338 | if (errno == EINTR) |
| 339 | continue; |
| 340 | if (errno == EAGAIN) { |
| 341 | fd_cant_recv(fd); |
| 342 | return; |
| 343 | } |
| 344 | break; |
| 345 | } else if (ret > 0) { |
| 346 | listener_accept(fd); |
| 347 | return; |
| 348 | } else if (ret == 0) { |
| 349 | /* At this step the master is down before |
| 350 | * this worker perform a 'normal' exit. |
| 351 | * So we want to exit with an error but |
| 352 | * other threads could currently process |
| 353 | * some stuff so we can't perform a clean |
| 354 | * deinit(). |
| 355 | */ |
| 356 | exit(EXIT_FAILURE); |
| 357 | } |
| 358 | } |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | /* |
Willy Tarreau | 619a95f | 2019-05-20 11:12:15 +0200 | [diff] [blame] | 363 | * This function registers the accept wrapper for the sockpair of the master |
| 364 | * worker. It's only handled by worker thread #0. Other threads and master do |
| 365 | * nothing here. It always returns 1 (success). |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 366 | */ |
Willy Tarreau | 619a95f | 2019-05-20 11:12:15 +0200 | [diff] [blame] | 367 | static int mworker_pipe_register_per_thread() |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 368 | { |
Willy Tarreau | 619a95f | 2019-05-20 11:12:15 +0200 | [diff] [blame] | 369 | if (!(global.mode & MODE_MWORKER) || master) |
| 370 | return 1; |
| 371 | |
| 372 | if (tid != 0) |
| 373 | return 1; |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 374 | |
| 375 | fcntl(proc_self->ipc_fd[1], F_SETFL, O_NONBLOCK); |
| 376 | /* In multi-tread, we need only one thread to process |
| 377 | * events on the pipe with master |
| 378 | */ |
Willy Tarreau | 619a95f | 2019-05-20 11:12:15 +0200 | [diff] [blame] | 379 | 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] | 380 | fd_want_recv(proc_self->ipc_fd[1]); |
Willy Tarreau | 619a95f | 2019-05-20 11:12:15 +0200 | [diff] [blame] | 381 | return 1; |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 382 | } |
William Lallemand | 9001ce8 | 2019-04-01 11:29:57 +0200 | [diff] [blame] | 383 | |
Willy Tarreau | 619a95f | 2019-05-20 11:12:15 +0200 | [diff] [blame] | 384 | REGISTER_PER_THREAD_INIT(mworker_pipe_register_per_thread); |
| 385 | |
William Lallemand | 9001ce8 | 2019-04-01 11:29:57 +0200 | [diff] [blame] | 386 | /* ----- proxies ----- */ |
| 387 | /* |
| 388 | * Upon a reload, the master worker needs to close all listeners FDs but the mworker_pipe |
| 389 | * fd, and the FD provided by fd@ |
| 390 | */ |
| 391 | void mworker_cleanlisteners() |
| 392 | { |
| 393 | struct listener *l, *l_next; |
| 394 | struct proxy *curproxy; |
| 395 | struct peers *curpeers; |
| 396 | |
| 397 | /* we might have to unbind some peers sections from some processes */ |
| 398 | for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) { |
| 399 | if (!curpeers->peers_fe) |
| 400 | continue; |
| 401 | |
| 402 | stop_proxy(curpeers->peers_fe); |
| 403 | /* disable this peer section so that it kills itself */ |
| 404 | signal_unregister_handler(curpeers->sighandler); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 405 | task_destroy(curpeers->sync_task); |
William Lallemand | 9001ce8 | 2019-04-01 11:29:57 +0200 | [diff] [blame] | 406 | curpeers->sync_task = NULL; |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 407 | task_destroy(curpeers->peers_fe->task); |
William Lallemand | 9001ce8 | 2019-04-01 11:29:57 +0200 | [diff] [blame] | 408 | curpeers->peers_fe->task = NULL; |
| 409 | curpeers->peers_fe = NULL; |
| 410 | } |
| 411 | |
| 412 | for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) { |
| 413 | int listen_in_master = 0; |
| 414 | |
| 415 | list_for_each_entry_safe(l, l_next, &curproxy->conf.listeners, by_fe) { |
| 416 | /* remove the listener, but not those we need in the master... */ |
| 417 | if (!(l->options & LI_O_MWORKER)) { |
| 418 | /* unbind the listener but does not close if |
| 419 | the FD is inherited with fd@ from the parent |
| 420 | process */ |
| 421 | if (l->options & LI_O_INHERITED) |
| 422 | unbind_listener_no_close(l); |
| 423 | else |
| 424 | unbind_listener(l); |
| 425 | delete_listener(l); |
| 426 | } else { |
| 427 | listen_in_master = 1; |
| 428 | } |
| 429 | } |
| 430 | /* if the proxy shouldn't be in the master, we stop it */ |
| 431 | if (!listen_in_master) |
| 432 | curproxy->state = PR_STSTOPPED; |
| 433 | } |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /* Displays workers and processes */ |
| 437 | static int cli_io_handler_show_proc(struct appctx *appctx) |
| 438 | { |
| 439 | struct stream_interface *si = appctx->owner; |
| 440 | struct mworker_proc *child; |
| 441 | int old = 0; |
| 442 | int up = now.tv_sec - proc_self->timestamp; |
William Lallemand | e8669fc | 2019-06-12 18:21:17 +0200 | [diff] [blame] | 443 | char *uptime = NULL; |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 444 | |
| 445 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 446 | return 1; |
| 447 | |
| 448 | chunk_reset(&trash); |
| 449 | |
William Lallemand | 1dc6963 | 2019-06-12 19:11:33 +0200 | [diff] [blame] | 450 | 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] | 451 | 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] | 452 | 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] | 453 | free(uptime); |
| 454 | uptime = NULL; |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 455 | |
| 456 | /* displays current processes */ |
| 457 | |
| 458 | chunk_appendf(&trash, "# workers\n"); |
| 459 | list_for_each_entry(child, &proc_list, list) { |
| 460 | up = now.tv_sec - child->timestamp; |
| 461 | |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 462 | if (!(child->options & PROC_O_TYPE_WORKER)) |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 463 | continue; |
| 464 | |
William Lallemand | 4528611 | 2019-04-12 16:09:21 +0200 | [diff] [blame] | 465 | if (child->options & PROC_O_LEAVING) { |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 466 | old++; |
| 467 | continue; |
| 468 | } |
William Lallemand | e8669fc | 2019-06-12 18:21:17 +0200 | [diff] [blame] | 469 | 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] | 470 | 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] | 471 | free(uptime); |
| 472 | uptime = NULL; |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /* displays old processes */ |
| 476 | |
| 477 | if (old) { |
| 478 | char *msg = NULL; |
| 479 | |
| 480 | chunk_appendf(&trash, "# old workers\n"); |
| 481 | list_for_each_entry(child, &proc_list, list) { |
| 482 | up = now.tv_sec - child->timestamp; |
| 483 | |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 484 | if (!(child->options & PROC_O_TYPE_WORKER)) |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 485 | continue; |
| 486 | |
William Lallemand | 4528611 | 2019-04-12 16:09:21 +0200 | [diff] [blame] | 487 | if (child->options & PROC_O_LEAVING) { |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 488 | memprintf(&msg, "[was: %u]", child->relative_pid); |
William Lallemand | e8669fc | 2019-06-12 18:21:17 +0200 | [diff] [blame] | 489 | 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] | 490 | 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] | 491 | free(uptime); |
| 492 | uptime = NULL; |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | free(msg); |
| 496 | } |
| 497 | |
William Lallemand | ad53d6d | 2019-04-01 11:30:03 +0200 | [diff] [blame] | 498 | /* displays external process */ |
| 499 | chunk_appendf(&trash, "# programs\n"); |
| 500 | old = 0; |
| 501 | list_for_each_entry(child, &proc_list, list) { |
| 502 | up = now.tv_sec - child->timestamp; |
| 503 | |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 504 | if (!(child->options & PROC_O_TYPE_PROG)) |
William Lallemand | ad53d6d | 2019-04-01 11:30:03 +0200 | [diff] [blame] | 505 | continue; |
| 506 | |
William Lallemand | 4528611 | 2019-04-12 16:09:21 +0200 | [diff] [blame] | 507 | if (child->options & PROC_O_LEAVING) { |
William Lallemand | ad53d6d | 2019-04-01 11:30:03 +0200 | [diff] [blame] | 508 | old++; |
| 509 | continue; |
| 510 | } |
William Lallemand | e8669fc | 2019-06-12 18:21:17 +0200 | [diff] [blame] | 511 | 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] | 512 | 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] | 513 | free(uptime); |
| 514 | uptime = NULL; |
William Lallemand | ad53d6d | 2019-04-01 11:30:03 +0200 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | if (old) { |
| 518 | chunk_appendf(&trash, "# old programs\n"); |
| 519 | list_for_each_entry(child, &proc_list, list) { |
| 520 | up = now.tv_sec - child->timestamp; |
| 521 | |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 522 | if (!(child->options & PROC_O_TYPE_PROG)) |
William Lallemand | ad53d6d | 2019-04-01 11:30:03 +0200 | [diff] [blame] | 523 | continue; |
| 524 | |
William Lallemand | 4528611 | 2019-04-12 16:09:21 +0200 | [diff] [blame] | 525 | if (child->options & PROC_O_LEAVING) { |
William Lallemand | e8669fc | 2019-06-12 18:21:17 +0200 | [diff] [blame] | 526 | memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60)); |
William Lallemand | 1dc6963 | 2019-06-12 19:11:33 +0200 | [diff] [blame] | 527 | 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] | 528 | free(uptime); |
| 529 | uptime = NULL; |
William Lallemand | ad53d6d | 2019-04-01 11:30:03 +0200 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | |
| 535 | |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 536 | if (ci_putchk(si_ic(si), &trash) == -1) { |
| 537 | si_rx_room_blk(si); |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | /* dump complete */ |
| 542 | return 1; |
William Lallemand | 9001ce8 | 2019-04-01 11:29:57 +0200 | [diff] [blame] | 543 | } |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 544 | |
| 545 | /* reload the master process */ |
| 546 | static int cli_parse_reload(char **args, char *payload, struct appctx *appctx, void *private) |
| 547 | { |
| 548 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 549 | return 1; |
| 550 | |
| 551 | mworker_reload(); |
| 552 | |
| 553 | return 1; |
| 554 | } |
| 555 | |
| 556 | |
William Lallemand | 27edc4b | 2019-05-07 17:49:33 +0200 | [diff] [blame] | 557 | static int mworker_parse_global_max_reloads(char **args, int section_type, struct proxy *curpx, |
| 558 | struct proxy *defpx, const char *file, int linenum, char **err) |
| 559 | { |
| 560 | |
| 561 | int err_code = 0; |
| 562 | |
| 563 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 564 | goto out; |
| 565 | |
| 566 | if (*(args[1]) == 0) { |
| 567 | memprintf(err, "%sparsing [%s:%d] : '%s' expects an integer argument.\n", *err, file, linenum, args[0]); |
| 568 | err_code |= ERR_ALERT | ERR_FATAL; |
| 569 | goto out; |
| 570 | } |
| 571 | |
| 572 | max_reloads = atol(args[1]); |
| 573 | if (max_reloads < 0) { |
| 574 | memprintf(err, "%sparsing [%s:%d] '%s' : invalid value %d, must be >= 0", *err, file, linenum, args[0], max_reloads); |
| 575 | err_code |= ERR_ALERT | ERR_FATAL; |
| 576 | goto out; |
| 577 | } |
| 578 | |
| 579 | out: |
| 580 | return err_code; |
| 581 | } |
| 582 | |
Tim Duesterhus | 9b7a976 | 2019-05-16 20:23:22 +0200 | [diff] [blame] | 583 | void mworker_free_child(struct mworker_proc *child) |
| 584 | { |
| 585 | if (child == NULL) |
| 586 | return; |
| 587 | |
| 588 | if (child->command) { |
| 589 | int i; |
| 590 | |
| 591 | for (i = 0; child->command[i]; i++) { |
| 592 | if (child->command[i]) { |
| 593 | free(child->command[i]); |
| 594 | child->command[i] = NULL; |
| 595 | } |
William Lallemand | e8669fc | 2019-06-12 18:21:17 +0200 | [diff] [blame] | 596 | |
Tim Duesterhus | 9b7a976 | 2019-05-16 20:23:22 +0200 | [diff] [blame] | 597 | } |
| 598 | free(child->command); |
| 599 | child->command = NULL; |
| 600 | } |
| 601 | if (child->id) { |
| 602 | free(child->id); |
| 603 | child->id = NULL; |
| 604 | } |
William Lallemand | 1dc6963 | 2019-06-12 19:11:33 +0200 | [diff] [blame] | 605 | if (child->version) { |
| 606 | free(child->version); |
| 607 | child->version = NULL; |
| 608 | } |
Tim Duesterhus | 9b7a976 | 2019-05-16 20:23:22 +0200 | [diff] [blame] | 609 | free(child); |
| 610 | } |
William Lallemand | 27edc4b | 2019-05-07 17:49:33 +0200 | [diff] [blame] | 611 | |
| 612 | static struct cfg_kw_list mworker_kws = {{ }, { |
| 613 | { CFG_GLOBAL, "mworker-max-reloads", mworker_parse_global_max_reloads }, |
| 614 | { 0, NULL, NULL }, |
| 615 | }}; |
| 616 | |
| 617 | INITCALL1(STG_REGISTER, cfg_register_keywords, &mworker_kws); |
| 618 | |
| 619 | |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 620 | /* register cli keywords */ |
| 621 | static struct cli_kw_list cli_kws = {{ },{ |
| 622 | { { "@<relative pid>", NULL }, "@<relative pid> : send a command to the <relative pid> process", NULL, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY}, |
| 623 | { { "@!<pid>", NULL }, "@!<pid> : send a command to the <pid> process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY}, |
| 624 | { { "@master", NULL }, "@master : send a command to the master process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY}, |
| 625 | { { "show", "proc", NULL }, "show proc : show processes status", cli_parse_default, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY}, |
| 626 | { { "reload", NULL }, "reload : reload haproxy", cli_parse_reload, NULL, NULL, NULL, ACCESS_MASTER_ONLY}, |
| 627 | {{},} |
| 628 | }}; |
| 629 | |
| 630 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |