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