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