William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Master Worker |
| 3 | * |
| 4 | * Copyright HAProxy Technologies 2019 - William Lallemand <wlallemand@haproxy.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Bertrand Jacquin | 25439de | 2021-01-21 01:31:46 +0000 | [diff] [blame] | 13 | #define _GNU_SOURCE |
| 14 | |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 15 | #include <errno.h> |
William Lallemand | ec059c2 | 2022-09-22 17:26:23 +0200 | [diff] [blame] | 16 | #include <fcntl.h> |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 17 | #include <signal.h> |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 20 | #include <sys/wait.h> |
William Lallemand | ec059c2 | 2022-09-22 17:26:23 +0200 | [diff] [blame] | 21 | #include <unistd.h> |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 22 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 23 | #if defined(USE_SYSTEMD) |
| 24 | #include <systemd/sd-daemon.h> |
| 25 | #endif |
| 26 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 27 | #include <haproxy/api.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 28 | #include <haproxy/cfgparse.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 29 | #include <haproxy/cli.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 30 | #include <haproxy/errors.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 31 | #include <haproxy/fd.h> |
| 32 | #include <haproxy/global.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 33 | #include <haproxy/list.h> |
William Lallemand | 68192b2 | 2022-09-24 15:44:42 +0200 | [diff] [blame] | 34 | #include <haproxy/log.h> |
Willy Tarreau | 213e990 | 2020-06-04 14:58:24 +0200 | [diff] [blame] | 35 | #include <haproxy/listener.h> |
Willy Tarreau | b5abe5b | 2020-06-04 14:07:37 +0200 | [diff] [blame] | 36 | #include <haproxy/mworker.h> |
Willy Tarreau | 3c2a7c2 | 2020-06-04 18:38:21 +0200 | [diff] [blame] | 37 | #include <haproxy/peers.h> |
William Lallemand | ec059c2 | 2022-09-22 17:26:23 +0200 | [diff] [blame] | 38 | #include <haproxy/proto_sockpair.h> |
Willy Tarreau | 7c66857 | 2021-05-08 20:21:31 +0200 | [diff] [blame] | 39 | #include <haproxy/proxy.h> |
William Lallemand | ec1f8a6 | 2022-10-13 17:49:54 +0200 | [diff] [blame] | 40 | #include <haproxy/ring.h> |
Willy Tarreau | 5edca2f | 2022-05-27 09:25:10 +0200 | [diff] [blame] | 41 | #include <haproxy/sc_strm.h> |
Willy Tarreau | 3727a8a | 2020-06-04 17:37:26 +0200 | [diff] [blame] | 42 | #include <haproxy/signal.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 43 | #include <haproxy/stconn.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 44 | #include <haproxy/stream.h> |
Willy Tarreau | 745e98c | 2021-05-08 13:58:19 +0200 | [diff] [blame] | 45 | #include <haproxy/tools.h> |
Willy Tarreau | d678805 | 2020-05-27 15:59:00 +0200 | [diff] [blame] | 46 | #include <haproxy/version.h> |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 47 | |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 48 | |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 49 | static int exitcode = -1; |
William Lallemand | 27edc4b | 2019-05-07 17:49:33 +0200 | [diff] [blame] | 50 | static int max_reloads = -1; /* number max of reloads a worker can have until they are killed */ |
Willy Tarreau | 15f9ac3 | 2021-05-08 12:30:50 +0200 | [diff] [blame] | 51 | struct mworker_proc *proc_self = NULL; /* process structure of current process */ |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 52 | |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 53 | /* ----- children processes handling ----- */ |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 54 | |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 55 | /* |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 56 | * Send signal to every known children. |
| 57 | */ |
| 58 | |
| 59 | static void mworker_kill(int sig) |
| 60 | { |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 61 | struct mworker_proc *child; |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 62 | |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 63 | list_for_each_entry(child, &proc_list, list) { |
| 64 | /* 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] | 65 | 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] | 66 | kill(child->pid, sig); |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
William Lallemand | 27edc4b | 2019-05-07 17:49:33 +0200 | [diff] [blame] | 70 | void mworker_kill_max_reloads(int sig) |
| 71 | { |
| 72 | struct mworker_proc *child; |
| 73 | |
| 74 | list_for_each_entry(child, &proc_list, list) { |
| 75 | if (max_reloads != -1 && (child->options & PROC_O_TYPE_WORKER) && |
| 76 | (child->pid > 0) && (child->reloads > max_reloads)) |
| 77 | kill(child->pid, sig); |
| 78 | } |
| 79 | } |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 80 | |
| 81 | /* return 1 if a pid is a current child otherwise 0 */ |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 82 | int mworker_current_child(int pid) |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 83 | { |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 84 | struct mworker_proc *child; |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 85 | |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 86 | list_for_each_entry(child, &proc_list, list) { |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 87 | 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] | 88 | return 1; |
| 89 | } |
| 90 | return 0; |
| 91 | } |
| 92 | |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 93 | /* |
| 94 | * Return the number of new and old children (including workers and external |
| 95 | * processes) |
| 96 | */ |
| 97 | int mworker_child_nb() |
| 98 | { |
| 99 | struct mworker_proc *child; |
| 100 | int ret = 0; |
| 101 | |
| 102 | list_for_each_entry(child, &proc_list, list) { |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 103 | if (child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG)) |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 104 | ret++; |
| 105 | } |
| 106 | |
| 107 | return ret; |
| 108 | } |
| 109 | |
| 110 | |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 111 | /* |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 112 | * serialize the proc list and put it in the environment |
| 113 | */ |
| 114 | void mworker_proc_list_to_env() |
| 115 | { |
| 116 | char *msg = NULL; |
| 117 | struct mworker_proc *child; |
William Lallemand | 14b98ef | 2022-07-27 11:57:12 +0200 | [diff] [blame] | 118 | int minreloads = INT_MAX; /* minimum number of reloads to chose which processes are "current" ones */ |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 119 | |
| 120 | list_for_each_entry(child, &proc_list, list) { |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 121 | char type = '?'; |
| 122 | |
| 123 | if (child->options & PROC_O_TYPE_MASTER) |
| 124 | type = 'm'; |
| 125 | else if (child->options & PROC_O_TYPE_PROG) |
| 126 | type = 'e'; |
| 127 | else if (child->options &= PROC_O_TYPE_WORKER) |
| 128 | type = 'w'; |
| 129 | |
William Lallemand | 14b98ef | 2022-07-27 11:57:12 +0200 | [diff] [blame] | 130 | if (child->reloads < minreloads) |
| 131 | minreloads = child->reloads; |
| 132 | |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 133 | if (child->pid > -1) |
William Lallemand | ec059c2 | 2022-09-22 17:26:23 +0200 | [diff] [blame] | 134 | memprintf(&msg, "%s|type=%c;fd=%d;cfd=%d;pid=%d;reloads=%d;failedreloads=%d;timestamp=%d;id=%s;version=%s", msg ? msg : "", type, child->ipc_fd[0], child->ipc_fd[1], child->pid, child->reloads, child->failedreloads, child->timestamp, child->id ? child->id : "", child->version); |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 135 | } |
| 136 | if (msg) |
| 137 | setenv("HAPROXY_PROCESSES", msg, 1); |
William Lallemand | 14b98ef | 2022-07-27 11:57:12 +0200 | [diff] [blame] | 138 | |
| 139 | list_for_each_entry(child, &proc_list, list) { |
| 140 | if (child->reloads > minreloads && !(child->options & PROC_O_TYPE_MASTER)) { |
| 141 | child->options |= PROC_O_LEAVING; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 146 | } |
| 147 | |
William Lallemand | 56be0e0 | 2022-01-28 21:11:41 +0100 | [diff] [blame] | 148 | struct mworker_proc *mworker_proc_new() |
| 149 | { |
| 150 | struct mworker_proc *child; |
| 151 | |
| 152 | child = calloc(1, sizeof(*child)); |
| 153 | if (!child) |
| 154 | return NULL; |
| 155 | |
| 156 | child->failedreloads = 0; |
| 157 | child->reloads = 0; |
| 158 | child->pid = -1; |
| 159 | child->ipc_fd[0] = -1; |
| 160 | child->ipc_fd[1] = -1; |
| 161 | child->timestamp = -1; |
| 162 | |
| 163 | return child; |
| 164 | } |
| 165 | |
| 166 | |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 167 | /* |
| 168 | * unserialize the proc list from the environment |
William Lallemand | d27f457 | 2023-02-21 12:44:56 +0100 | [diff] [blame] | 169 | * Return < 0 upon error. |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 170 | */ |
Remi Tricot-Le Breton | 1f4fa90 | 2021-05-19 10:45:12 +0200 | [diff] [blame] | 171 | int mworker_env_to_proc_list() |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 172 | { |
William Lallemand | d27f457 | 2023-02-21 12:44:56 +0100 | [diff] [blame] | 173 | char *env, *msg, *omsg = NULL, *token = NULL, *s1; |
William Lallemand | 90034bb | 2021-11-10 11:26:14 +0100 | [diff] [blame] | 174 | struct mworker_proc *child; |
| 175 | int minreloads = INT_MAX; /* minimum number of reloads to chose which processes are "current" ones */ |
William Lallemand | d27f457 | 2023-02-21 12:44:56 +0100 | [diff] [blame] | 176 | int err = 0; |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 177 | |
William Lallemand | d27f457 | 2023-02-21 12:44:56 +0100 | [diff] [blame] | 178 | env = getenv("HAPROXY_PROCESSES"); |
| 179 | if (!env) |
William Lallemand | e16d320 | 2023-02-21 13:17:24 +0100 | [diff] [blame] | 180 | goto no_env; |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 181 | |
William Lallemand | d27f457 | 2023-02-21 12:44:56 +0100 | [diff] [blame] | 182 | omsg = msg = strdup(env); |
| 183 | if (!msg) { |
| 184 | ha_alert("Out of memory while trying to allocate a worker process structure."); |
| 185 | err = -1; |
| 186 | goto out; |
| 187 | } |
| 188 | |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 189 | while ((token = strtok_r(msg, "|", &s1))) { |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 190 | char *subtoken = NULL; |
| 191 | char *s2; |
| 192 | |
| 193 | msg = NULL; |
| 194 | |
William Lallemand | 56be0e0 | 2022-01-28 21:11:41 +0100 | [diff] [blame] | 195 | child = mworker_proc_new(); |
Remi Tricot-Le Breton | 1f4fa90 | 2021-05-19 10:45:12 +0200 | [diff] [blame] | 196 | if (!child) { |
William Lallemand | d27f457 | 2023-02-21 12:44:56 +0100 | [diff] [blame] | 197 | ha_alert("out of memory while trying to allocate a worker process structure."); |
| 198 | err = -1; |
| 199 | goto out; |
Remi Tricot-Le Breton | 1f4fa90 | 2021-05-19 10:45:12 +0200 | [diff] [blame] | 200 | } |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 201 | |
| 202 | while ((subtoken = strtok_r(token, ";", &s2))) { |
| 203 | |
| 204 | token = NULL; |
| 205 | |
| 206 | if (strncmp(subtoken, "type=", 5) == 0) { |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 207 | char type; |
| 208 | |
| 209 | type = *(subtoken+5); |
| 210 | if (type == 'm') { /* we are in the master, assign it */ |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 211 | proc_self = child; |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 212 | child->options |= PROC_O_TYPE_MASTER; |
| 213 | } else if (type == 'e') { |
| 214 | child->options |= PROC_O_TYPE_PROG; |
| 215 | } else if (type == 'w') { |
| 216 | child->options |= PROC_O_TYPE_WORKER; |
| 217 | } |
| 218 | |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 219 | } else if (strncmp(subtoken, "fd=", 3) == 0) { |
| 220 | child->ipc_fd[0] = atoi(subtoken+3); |
William Lallemand | 67bddbf | 2023-06-19 17:12:58 +0200 | [diff] [blame] | 221 | if (child->ipc_fd[0] > -1) |
| 222 | global.maxsock++; |
William Lallemand | ec059c2 | 2022-09-22 17:26:23 +0200 | [diff] [blame] | 223 | } else if (strncmp(subtoken, "cfd=", 4) == 0) { |
| 224 | child->ipc_fd[1] = atoi(subtoken+4); |
William Lallemand | 67bddbf | 2023-06-19 17:12:58 +0200 | [diff] [blame] | 225 | if (child->ipc_fd[1] > -1) |
| 226 | global.maxsock++; |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 227 | } else if (strncmp(subtoken, "pid=", 4) == 0) { |
| 228 | child->pid = atoi(subtoken+4); |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 229 | } else if (strncmp(subtoken, "reloads=", 8) == 0) { |
William Lallemand | ad221f4 | 2021-11-09 18:43:59 +0100 | [diff] [blame] | 230 | /* we only increment the number of asked reload */ |
| 231 | child->reloads = atoi(subtoken+8); |
William Lallemand | 90034bb | 2021-11-10 11:26:14 +0100 | [diff] [blame] | 232 | |
| 233 | if (child->reloads < minreloads) |
| 234 | minreloads = child->reloads; |
William Lallemand | 6883674 | 2021-11-10 10:49:06 +0100 | [diff] [blame] | 235 | } else if (strncmp(subtoken, "failedreloads=", 14) == 0) { |
| 236 | child->failedreloads = atoi(subtoken+14); |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 237 | } else if (strncmp(subtoken, "timestamp=", 10) == 0) { |
| 238 | child->timestamp = atoi(subtoken+10); |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 239 | } else if (strncmp(subtoken, "id=", 3) == 0) { |
| 240 | child->id = strdup(subtoken+3); |
William Lallemand | 1dc6963 | 2019-06-12 19:11:33 +0200 | [diff] [blame] | 241 | } else if (strncmp(subtoken, "version=", 8) == 0) { |
| 242 | child->version = strdup(subtoken+8); |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 243 | } |
| 244 | } |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 245 | if (child->pid) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 246 | LIST_APPEND(&proc_list, &child->list); |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 247 | } else { |
Tim Duesterhus | 9b7a976 | 2019-05-16 20:23:22 +0200 | [diff] [blame] | 248 | mworker_free_child(child); |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 249 | } |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 250 | } |
| 251 | |
William Lallemand | 90034bb | 2021-11-10 11:26:14 +0100 | [diff] [blame] | 252 | /* set the leaving processes once we know which number of reloads are the current processes */ |
| 253 | |
| 254 | list_for_each_entry(child, &proc_list, list) { |
| 255 | if (child->reloads > minreloads) |
| 256 | child->options |= PROC_O_LEAVING; |
| 257 | } |
| 258 | |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 259 | unsetenv("HAPROXY_PROCESSES"); |
Remi Tricot-Le Breton | 1f4fa90 | 2021-05-19 10:45:12 +0200 | [diff] [blame] | 260 | |
William Lallemand | e16d320 | 2023-02-21 13:17:24 +0100 | [diff] [blame] | 261 | no_env: |
| 262 | |
| 263 | if (!proc_self) { |
| 264 | |
| 265 | proc_self = mworker_proc_new(); |
| 266 | if (!proc_self) { |
| 267 | ha_alert("Cannot allocate process structures.\n"); |
| 268 | err = -1; |
| 269 | goto out; |
| 270 | } |
| 271 | proc_self->options |= PROC_O_TYPE_MASTER; |
| 272 | proc_self->pid = pid; |
| 273 | proc_self->timestamp = 0; /* we don't know the startime anymore */ |
| 274 | |
| 275 | LIST_APPEND(&proc_list, &proc_self->list); |
| 276 | ha_warning("The master internals are corrupted or it was started with a too old version (< 1.9). Please restart the master process.\n"); |
| 277 | } |
| 278 | |
William Lallemand | d27f457 | 2023-02-21 12:44:56 +0100 | [diff] [blame] | 279 | out: |
| 280 | free(omsg); |
| 281 | return err; |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 282 | } |
William Lallemand | 3cd95d2 | 2019-04-01 11:29:54 +0200 | [diff] [blame] | 283 | |
| 284 | /* Signal blocking and unblocking */ |
| 285 | |
| 286 | void mworker_block_signals() |
| 287 | { |
| 288 | sigset_t set; |
| 289 | |
| 290 | sigemptyset(&set); |
| 291 | sigaddset(&set, SIGUSR1); |
| 292 | sigaddset(&set, SIGUSR2); |
Willy Tarreau | d26c9f9 | 2019-12-11 14:24:07 +0100 | [diff] [blame] | 293 | sigaddset(&set, SIGTTIN); |
| 294 | sigaddset(&set, SIGTTOU); |
William Lallemand | 3cd95d2 | 2019-04-01 11:29:54 +0200 | [diff] [blame] | 295 | sigaddset(&set, SIGHUP); |
| 296 | sigaddset(&set, SIGCHLD); |
| 297 | ha_sigmask(SIG_SETMASK, &set, NULL); |
| 298 | } |
| 299 | |
| 300 | void mworker_unblock_signals() |
| 301 | { |
| 302 | haproxy_unblock_signals(); |
| 303 | } |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 304 | |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 305 | /* ----- mworker signal handlers ----- */ |
| 306 | |
Willy Tarreau | d26c9f9 | 2019-12-11 14:24:07 +0100 | [diff] [blame] | 307 | /* broadcast the configured signal to the workers */ |
| 308 | void mworker_broadcast_signal(struct sig_handler *sh) |
| 309 | { |
| 310 | mworker_kill(sh->arg); |
| 311 | } |
| 312 | |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 313 | /* |
| 314 | * When called, this function reexec haproxy with -sf followed by current |
| 315 | * children PIDs and possibly old children PIDs if they didn't leave yet. |
| 316 | */ |
| 317 | void mworker_catch_sighup(struct sig_handler *sh) |
| 318 | { |
| 319 | mworker_reload(); |
| 320 | } |
| 321 | |
| 322 | void mworker_catch_sigterm(struct sig_handler *sh) |
| 323 | { |
| 324 | int sig = sh->arg; |
| 325 | |
| 326 | #if defined(USE_SYSTEMD) |
| 327 | if (global.tune.options & GTUNE_USE_SYSTEMD) { |
| 328 | sd_notify(0, "STOPPING=1"); |
| 329 | } |
| 330 | #endif |
| 331 | ha_warning("Exiting Master process...\n"); |
| 332 | mworker_kill(sig); |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * Wait for every children to exit |
| 337 | */ |
| 338 | |
| 339 | void mworker_catch_sigchld(struct sig_handler *sh) |
| 340 | { |
| 341 | int exitpid = -1; |
| 342 | int status = 0; |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 343 | int childfound; |
| 344 | |
| 345 | restart_wait: |
| 346 | |
| 347 | childfound = 0; |
| 348 | |
| 349 | exitpid = waitpid(-1, &status, WNOHANG); |
| 350 | if (exitpid > 0) { |
Tim Duesterhus | 9b7a976 | 2019-05-16 20:23:22 +0200 | [diff] [blame] | 351 | struct mworker_proc *child, *it; |
| 352 | |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 353 | if (WIFEXITED(status)) |
| 354 | status = WEXITSTATUS(status); |
| 355 | else if (WIFSIGNALED(status)) |
| 356 | status = 128 + WTERMSIG(status); |
| 357 | else if (WIFSTOPPED(status)) |
| 358 | status = 128 + WSTOPSIG(status); |
| 359 | else |
| 360 | status = 255; |
| 361 | |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 362 | /* delete the child from the process list */ |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 363 | list_for_each_entry_safe(child, it, &proc_list, list) { |
| 364 | if (child->pid != exitpid) |
| 365 | continue; |
| 366 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 367 | LIST_DELETE(&child->list); |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 368 | close(child->ipc_fd[0]); |
| 369 | childfound = 1; |
| 370 | break; |
| 371 | } |
| 372 | |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 373 | if (!childfound) { |
| 374 | /* 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] | 375 | 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] | 376 | } else { |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 377 | /* check if exited child is a current child */ |
William Lallemand | 4528611 | 2019-04-12 16:09:21 +0200 | [diff] [blame] | 378 | if (!(child->options & PROC_O_LEAVING)) { |
William Lallemand | a655ba4 | 2020-05-06 17:27:03 +0200 | [diff] [blame] | 379 | if (child->options & PROC_O_TYPE_WORKER) { |
| 380 | if (status < 128) |
William Lallemand | 5d71a6b | 2021-11-09 15:25:31 +0100 | [diff] [blame] | 381 | ha_warning("Current worker (%d) exited with code %d (%s)\n", exitpid, status, "Exit"); |
William Lallemand | a655ba4 | 2020-05-06 17:27:03 +0200 | [diff] [blame] | 382 | else |
William Lallemand | 5d71a6b | 2021-11-09 15:25:31 +0100 | [diff] [blame] | 383 | ha_alert("Current worker (%d) exited with code %d (%s)\n", exitpid, status, strsignal(status - 128)); |
William Lallemand | a655ba4 | 2020-05-06 17:27:03 +0200 | [diff] [blame] | 384 | } |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 385 | else if (child->options & PROC_O_TYPE_PROG) |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 386 | 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] | 387 | |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 388 | if (status != 0 && status != 130 && status != 143 |
| 389 | && !(global.tune.options & GTUNE_NOEXIT_ONFAILURE)) { |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 390 | ha_alert("exit-on-failure: killing every processes with SIGTERM\n"); |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 391 | mworker_kill(SIGTERM); |
| 392 | } |
William Lallemand | 74f0ec3 | 2019-04-16 17:42:44 +0200 | [diff] [blame] | 393 | /* 0 & SIGTERM (143) are normal, but we should report SIGINT (130) and other signals */ |
| 394 | if (exitcode < 0 && status != 0 && status != 143) |
| 395 | exitcode = status; |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 396 | } else { |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 397 | if (child->options & PROC_O_TYPE_WORKER) { |
William Lallemand | 5d71a6b | 2021-11-09 15:25:31 +0100 | [diff] [blame] | 398 | ha_warning("Former worker (%d) exited with code %d (%s)\n", exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit"); |
William Lallemand | 3f12887 | 2019-04-01 11:29:59 +0200 | [diff] [blame] | 399 | delete_oldpid(exitpid); |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 400 | } else if (child->options & PROC_O_TYPE_PROG) { |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 401 | 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] | 402 | } |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 403 | } |
Tim Duesterhus | 9b7a976 | 2019-05-16 20:23:22 +0200 | [diff] [blame] | 404 | mworker_free_child(child); |
| 405 | child = NULL; |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /* do it again to check if it was the last worker */ |
| 409 | goto restart_wait; |
| 410 | } |
| 411 | /* Better rely on the system than on a list of process to check if it was the last one */ |
| 412 | else if (exitpid == -1 && errno == ECHILD) { |
William Lallemand | 4cf4b33 | 2019-04-16 17:42:43 +0200 | [diff] [blame] | 413 | 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] | 414 | atexit_flag = 0; |
| 415 | if (exitcode > 0) |
William Lallemand | 4cf4b33 | 2019-04-16 17:42:43 +0200 | [diff] [blame] | 416 | exit(exitcode); /* parent must leave using the status code that provoked the exit */ |
| 417 | exit(EXIT_SUCCESS); |
William Lallemand | e25473c | 2019-04-01 11:29:56 +0200 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | } |
| 421 | |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 422 | /* ----- IPC FD (sockpair) related ----- */ |
| 423 | |
| 424 | /* This wrapper is called from the workers. It is registered instead of the |
| 425 | * normal listener_accept() so the worker can exit() when it detects that the |
| 426 | * master closed the IPC FD. If it's not a close, we just call the regular |
Willy Tarreau | a74cb38 | 2020-10-15 21:29:49 +0200 | [diff] [blame] | 427 | * listener_accept() function. |
| 428 | */ |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 429 | void mworker_accept_wrapper(int fd) |
| 430 | { |
| 431 | char c; |
| 432 | int ret; |
| 433 | |
| 434 | while (1) { |
| 435 | ret = recv(fd, &c, 1, MSG_PEEK); |
| 436 | if (ret == -1) { |
| 437 | if (errno == EINTR) |
| 438 | continue; |
Willy Tarreau | acef5e2 | 2022-04-25 20:32:15 +0200 | [diff] [blame] | 439 | if (errno == EAGAIN || errno == EWOULDBLOCK) { |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 440 | fd_cant_recv(fd); |
| 441 | return; |
| 442 | } |
| 443 | break; |
| 444 | } else if (ret > 0) { |
Willy Tarreau | a74cb38 | 2020-10-15 21:29:49 +0200 | [diff] [blame] | 445 | struct listener *l = fdtab[fd].owner; |
| 446 | |
| 447 | if (l) |
| 448 | listener_accept(l); |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 449 | return; |
| 450 | } else if (ret == 0) { |
| 451 | /* At this step the master is down before |
| 452 | * this worker perform a 'normal' exit. |
| 453 | * So we want to exit with an error but |
| 454 | * other threads could currently process |
| 455 | * some stuff so we can't perform a clean |
| 456 | * deinit(). |
| 457 | */ |
| 458 | exit(EXIT_FAILURE); |
| 459 | } |
| 460 | } |
| 461 | return; |
| 462 | } |
| 463 | |
| 464 | /* |
Willy Tarreau | 619a95f | 2019-05-20 11:12:15 +0200 | [diff] [blame] | 465 | * This function registers the accept wrapper for the sockpair of the master |
| 466 | * worker. It's only handled by worker thread #0. Other threads and master do |
| 467 | * nothing here. It always returns 1 (success). |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 468 | */ |
William Lallemand | 2ee490f | 2022-07-05 09:04:03 +0200 | [diff] [blame] | 469 | static int mworker_sockpair_register_per_thread() |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 470 | { |
Willy Tarreau | 619a95f | 2019-05-20 11:12:15 +0200 | [diff] [blame] | 471 | if (!(global.mode & MODE_MWORKER) || master) |
| 472 | return 1; |
| 473 | |
| 474 | if (tid != 0) |
| 475 | return 1; |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 476 | |
William Lallemand | cc5b9fa | 2023-02-21 13:41:24 +0100 | [diff] [blame] | 477 | if (proc_self->ipc_fd[1] < 0) /* proc_self was incomplete and we can't find the socketpair */ |
| 478 | return 1; |
| 479 | |
Willy Tarreau | 3824743 | 2022-04-26 10:24:14 +0200 | [diff] [blame] | 480 | fd_set_nonblock(proc_self->ipc_fd[1]); |
William Lallemand | 2ee490f | 2022-07-05 09:04:03 +0200 | [diff] [blame] | 481 | /* register the wrapper to handle read 0 when the master exits */ |
William Lallemand | 34aae2f | 2022-07-05 00:55:09 +0200 | [diff] [blame] | 482 | fdtab[proc_self->ipc_fd[1]].iocb = mworker_accept_wrapper; |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 483 | fd_want_recv(proc_self->ipc_fd[1]); |
Willy Tarreau | 619a95f | 2019-05-20 11:12:15 +0200 | [diff] [blame] | 484 | return 1; |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame] | 485 | } |
William Lallemand | 9001ce8 | 2019-04-01 11:29:57 +0200 | [diff] [blame] | 486 | |
William Lallemand | 2ee490f | 2022-07-05 09:04:03 +0200 | [diff] [blame] | 487 | REGISTER_PER_THREAD_INIT(mworker_sockpair_register_per_thread); |
Willy Tarreau | 619a95f | 2019-05-20 11:12:15 +0200 | [diff] [blame] | 488 | |
William Lallemand | 9001ce8 | 2019-04-01 11:29:57 +0200 | [diff] [blame] | 489 | /* ----- proxies ----- */ |
| 490 | /* |
| 491 | * Upon a reload, the master worker needs to close all listeners FDs but the mworker_pipe |
| 492 | * fd, and the FD provided by fd@ |
| 493 | */ |
| 494 | void mworker_cleanlisteners() |
| 495 | { |
| 496 | struct listener *l, *l_next; |
| 497 | struct proxy *curproxy; |
| 498 | struct peers *curpeers; |
| 499 | |
Aurelien DARRAGON | 1412d31 | 2022-11-23 19:56:35 +0100 | [diff] [blame] | 500 | /* peers proxies cleanup */ |
William Lallemand | 9001ce8 | 2019-04-01 11:29:57 +0200 | [diff] [blame] | 501 | for (curpeers = cfg_peers; curpeers; curpeers = curpeers->next) { |
| 502 | if (!curpeers->peers_fe) |
| 503 | continue; |
| 504 | |
| 505 | stop_proxy(curpeers->peers_fe); |
| 506 | /* disable this peer section so that it kills itself */ |
William Lallemand | 035058e | 2022-12-07 15:21:24 +0100 | [diff] [blame] | 507 | if (curpeers->sighandler) |
| 508 | signal_unregister_handler(curpeers->sighandler); |
Tim Duesterhus | fe83f58 | 2023-04-22 17:47:34 +0200 | [diff] [blame] | 509 | task_destroy(curpeers->sync_task); |
William Lallemand | 9001ce8 | 2019-04-01 11:29:57 +0200 | [diff] [blame] | 510 | curpeers->sync_task = NULL; |
William Lallemand | 9001ce8 | 2019-04-01 11:29:57 +0200 | [diff] [blame] | 511 | curpeers->peers_fe = NULL; |
| 512 | } |
| 513 | |
Aurelien DARRAGON | 1412d31 | 2022-11-23 19:56:35 +0100 | [diff] [blame] | 514 | /* main proxies cleanup */ |
William Lallemand | 9001ce8 | 2019-04-01 11:29:57 +0200 | [diff] [blame] | 515 | for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) { |
| 516 | int listen_in_master = 0; |
| 517 | |
| 518 | list_for_each_entry_safe(l, l_next, &curproxy->conf.listeners, by_fe) { |
| 519 | /* remove the listener, but not those we need in the master... */ |
Willy Tarreau | 18c20d2 | 2020-10-09 16:11:46 +0200 | [diff] [blame] | 520 | if (!(l->rx.flags & RX_F_MWORKER)) { |
Willy Tarreau | 75c98d1 | 2020-10-09 15:55:23 +0200 | [diff] [blame] | 521 | unbind_listener(l); |
William Lallemand | 9001ce8 | 2019-04-01 11:29:57 +0200 | [diff] [blame] | 522 | delete_listener(l); |
| 523 | } else { |
| 524 | listen_in_master = 1; |
| 525 | } |
| 526 | } |
| 527 | /* if the proxy shouldn't be in the master, we stop it */ |
| 528 | if (!listen_in_master) |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 529 | curproxy->flags |= PR_FL_DISABLED; |
William Lallemand | 9001ce8 | 2019-04-01 11:29:57 +0200 | [diff] [blame] | 530 | } |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 531 | } |
| 532 | |
William Lallemand | 55a921c | 2022-01-28 21:17:30 +0100 | [diff] [blame] | 533 | /* Upon a configuration loading error some mworker_proc and FDs/server were |
| 534 | * assigned but the worker was never forked, we must close the FDs and |
| 535 | * remove the server |
| 536 | */ |
| 537 | void mworker_cleanup_proc() |
| 538 | { |
| 539 | struct mworker_proc *child, *it; |
| 540 | |
| 541 | list_for_each_entry_safe(child, it, &proc_list, list) { |
| 542 | |
| 543 | if (child->pid == -1) { |
William Lallemand | 8ee9a50 | 2023-06-21 09:44:18 +0200 | [diff] [blame] | 544 | /* Close the socketpairs. */ |
William Lallemand | 55a921c | 2022-01-28 21:17:30 +0100 | [diff] [blame] | 545 | if (child->ipc_fd[0] > -1) |
| 546 | close(child->ipc_fd[0]); |
William Lallemand | 8ee9a50 | 2023-06-21 09:44:18 +0200 | [diff] [blame] | 547 | if (child->ipc_fd[1] > -1) |
| 548 | close(child->ipc_fd[1]); |
William Lallemand | 55a921c | 2022-01-28 21:17:30 +0100 | [diff] [blame] | 549 | if (child->srv) { |
| 550 | /* only exists if we created a master CLI listener */ |
| 551 | srv_drop(child->srv); |
| 552 | } |
| 553 | LIST_DELETE(&child->list); |
| 554 | mworker_free_child(child); |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 560 | /* Displays workers and processes */ |
| 561 | static int cli_io_handler_show_proc(struct appctx *appctx) |
| 562 | { |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 563 | struct stconn *sc = appctx_sc(appctx); |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 564 | struct mworker_proc *child; |
| 565 | int old = 0; |
William Lallemand | 5a7f83a | 2023-02-17 16:23:52 +0100 | [diff] [blame] | 566 | int up = date.tv_sec - proc_self->timestamp; |
William Lallemand | e8669fc | 2019-06-12 18:21:17 +0200 | [diff] [blame] | 567 | char *uptime = NULL; |
William Lallemand | 6883674 | 2021-11-10 10:49:06 +0100 | [diff] [blame] | 568 | char *reloadtxt = NULL; |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 569 | |
Christopher Faulet | 87633c3 | 2023-04-03 18:32:50 +0200 | [diff] [blame] | 570 | /* FIXME: Don't watch the other side !*/ |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 571 | if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE)) |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 572 | return 1; |
| 573 | |
William Lallemand | 5a7f83a | 2023-02-17 16:23:52 +0100 | [diff] [blame] | 574 | if (up < 0) /* must never be negative because of clock drift */ |
| 575 | up = 0; |
| 576 | |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 577 | chunk_reset(&trash); |
| 578 | |
William Lallemand | 6883674 | 2021-11-10 10:49:06 +0100 | [diff] [blame] | 579 | memprintf(&reloadtxt, "%d [failed: %d]", proc_self->reloads, proc_self->failedreloads); |
William Lallemand | 5d71a6b | 2021-11-09 15:25:31 +0100 | [diff] [blame] | 580 | chunk_printf(&trash, "#%-14s %-15s %-15s %-15s %-15s\n", "<PID>", "<type>", "<reloads>", "<uptime>", "<version>"); |
William Lallemand | e8669fc | 2019-06-12 18:21:17 +0200 | [diff] [blame] | 581 | memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60)); |
William Lallemand | 6883674 | 2021-11-10 10:49:06 +0100 | [diff] [blame] | 582 | chunk_appendf(&trash, "%-15u %-15s %-15s %-15s %-15s\n", (unsigned int)getpid(), "master", reloadtxt, uptime, haproxy_version); |
| 583 | ha_free(&reloadtxt); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 584 | ha_free(&uptime); |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 585 | |
| 586 | /* displays current processes */ |
| 587 | |
| 588 | chunk_appendf(&trash, "# workers\n"); |
| 589 | list_for_each_entry(child, &proc_list, list) { |
William Lallemand | 5a7f83a | 2023-02-17 16:23:52 +0100 | [diff] [blame] | 590 | up = date.tv_sec - child->timestamp; |
| 591 | if (up < 0) /* must never be negative because of clock drift */ |
| 592 | up = 0; |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 593 | |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 594 | if (!(child->options & PROC_O_TYPE_WORKER)) |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 595 | continue; |
| 596 | |
William Lallemand | 4528611 | 2019-04-12 16:09:21 +0200 | [diff] [blame] | 597 | if (child->options & PROC_O_LEAVING) { |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 598 | old++; |
| 599 | continue; |
| 600 | } |
William Lallemand | e8669fc | 2019-06-12 18:21:17 +0200 | [diff] [blame] | 601 | memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60)); |
William Lallemand | 5d71a6b | 2021-11-09 15:25:31 +0100 | [diff] [blame] | 602 | chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, "worker", child->reloads, uptime, child->version); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 603 | ha_free(&uptime); |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | /* displays old processes */ |
| 607 | |
| 608 | if (old) { |
| 609 | char *msg = NULL; |
| 610 | |
| 611 | chunk_appendf(&trash, "# old workers\n"); |
| 612 | list_for_each_entry(child, &proc_list, list) { |
William Lallemand | 5a7f83a | 2023-02-17 16:23:52 +0100 | [diff] [blame] | 613 | up = date.tv_sec - child->timestamp; |
| 614 | if (up <= 0) /* must never be negative because of clock drift */ |
| 615 | up = 0; |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 616 | |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 617 | if (!(child->options & PROC_O_TYPE_WORKER)) |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 618 | continue; |
| 619 | |
William Lallemand | 4528611 | 2019-04-12 16:09:21 +0200 | [diff] [blame] | 620 | if (child->options & PROC_O_LEAVING) { |
William Lallemand | e8669fc | 2019-06-12 18:21:17 +0200 | [diff] [blame] | 621 | memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60)); |
William Lallemand | 5d71a6b | 2021-11-09 15:25:31 +0100 | [diff] [blame] | 622 | chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, "worker", child->reloads, uptime, child->version); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 623 | ha_free(&uptime); |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | free(msg); |
| 627 | } |
| 628 | |
William Lallemand | ad53d6d | 2019-04-01 11:30:03 +0200 | [diff] [blame] | 629 | /* displays external process */ |
| 630 | chunk_appendf(&trash, "# programs\n"); |
| 631 | old = 0; |
| 632 | list_for_each_entry(child, &proc_list, list) { |
William Lallemand | 5a7f83a | 2023-02-17 16:23:52 +0100 | [diff] [blame] | 633 | up = date.tv_sec - child->timestamp; |
| 634 | if (up < 0) /* must never be negative because of clock drift */ |
| 635 | up = 0; |
William Lallemand | ad53d6d | 2019-04-01 11:30:03 +0200 | [diff] [blame] | 636 | |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 637 | if (!(child->options & PROC_O_TYPE_PROG)) |
William Lallemand | ad53d6d | 2019-04-01 11:30:03 +0200 | [diff] [blame] | 638 | continue; |
| 639 | |
William Lallemand | 4528611 | 2019-04-12 16:09:21 +0200 | [diff] [blame] | 640 | if (child->options & PROC_O_LEAVING) { |
William Lallemand | ad53d6d | 2019-04-01 11:30:03 +0200 | [diff] [blame] | 641 | old++; |
| 642 | continue; |
| 643 | } |
William Lallemand | e8669fc | 2019-06-12 18:21:17 +0200 | [diff] [blame] | 644 | memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60)); |
William Lallemand | 5d71a6b | 2021-11-09 15:25:31 +0100 | [diff] [blame] | 645 | chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, child->id, child->reloads, uptime, "-"); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 646 | ha_free(&uptime); |
William Lallemand | ad53d6d | 2019-04-01 11:30:03 +0200 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | if (old) { |
| 650 | chunk_appendf(&trash, "# old programs\n"); |
| 651 | list_for_each_entry(child, &proc_list, list) { |
William Lallemand | 5a7f83a | 2023-02-17 16:23:52 +0100 | [diff] [blame] | 652 | up = date.tv_sec - child->timestamp; |
| 653 | if (up < 0) /* must never be negative because of clock drift */ |
| 654 | up = 0; |
William Lallemand | ad53d6d | 2019-04-01 11:30:03 +0200 | [diff] [blame] | 655 | |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 656 | if (!(child->options & PROC_O_TYPE_PROG)) |
William Lallemand | ad53d6d | 2019-04-01 11:30:03 +0200 | [diff] [blame] | 657 | continue; |
| 658 | |
William Lallemand | 4528611 | 2019-04-12 16:09:21 +0200 | [diff] [blame] | 659 | if (child->options & PROC_O_LEAVING) { |
William Lallemand | e8669fc | 2019-06-12 18:21:17 +0200 | [diff] [blame] | 660 | memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60)); |
William Lallemand | 5d71a6b | 2021-11-09 15:25:31 +0100 | [diff] [blame] | 661 | chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s\n", child->pid, child->id, child->reloads, uptime, "-"); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 662 | ha_free(&uptime); |
William Lallemand | ad53d6d | 2019-04-01 11:30:03 +0200 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | |
| 668 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 669 | if (applet_putchk(appctx, &trash) == -1) |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 670 | return 0; |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 671 | |
| 672 | /* dump complete */ |
| 673 | return 1; |
William Lallemand | 9001ce8 | 2019-04-01 11:29:57 +0200 | [diff] [blame] | 674 | } |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 675 | |
| 676 | /* reload the master process */ |
| 677 | static int cli_parse_reload(char **args, char *payload, struct appctx *appctx, void *private) |
| 678 | { |
William Lallemand | ec059c2 | 2022-09-22 17:26:23 +0200 | [diff] [blame] | 679 | struct stconn *scb = NULL; |
| 680 | struct stream *strm = NULL; |
| 681 | struct connection *conn = NULL; |
| 682 | int fd = -1; |
| 683 | |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 684 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 685 | return 1; |
| 686 | |
William Lallemand | ec059c2 | 2022-09-22 17:26:23 +0200 | [diff] [blame] | 687 | /* This ask for a synchronous reload, which means we will keep this FD |
| 688 | instead of closing it. */ |
| 689 | |
| 690 | scb = appctx_sc(appctx); |
| 691 | if (scb) |
| 692 | strm = sc_strm(scb); |
| 693 | if (strm && strm->scf) |
| 694 | conn = sc_conn(strm->scf); |
| 695 | if (conn) |
| 696 | fd = conn_fd(conn); |
| 697 | |
| 698 | /* Send the FD of the current session to the "cli_reload" FD, which won't be polled */ |
| 699 | if (fd != -1 && send_fd_uxst(proc_self->ipc_fd[0], fd) == 0) { |
William Lallemand | 479cb3e | 2022-09-23 10:21:32 +0200 | [diff] [blame] | 700 | fd_delete(fd); /* avoid the leak of the FD after sending it via the socketpair */ |
William Lallemand | ec059c2 | 2022-09-22 17:26:23 +0200 | [diff] [blame] | 701 | } |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 702 | mworker_reload(); |
| 703 | |
| 704 | return 1; |
| 705 | } |
| 706 | |
William Lallemand | ec1f8a6 | 2022-10-13 17:49:54 +0200 | [diff] [blame] | 707 | /* Displays if the current reload failed or succeed. |
| 708 | * If the startup-logs is available, dump it. */ |
William Lallemand | 2f67dd9 | 2022-10-21 14:00:05 +0200 | [diff] [blame] | 709 | static int cli_io_handler_show_loadstatus(struct appctx *appctx) |
William Lallemand | 68192b2 | 2022-09-24 15:44:42 +0200 | [diff] [blame] | 710 | { |
| 711 | char *env; |
William Lallemand | ec1f8a6 | 2022-10-13 17:49:54 +0200 | [diff] [blame] | 712 | struct stconn *sc = appctx_sc(appctx); |
William Lallemand | 68192b2 | 2022-09-24 15:44:42 +0200 | [diff] [blame] | 713 | |
| 714 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 715 | return 1; |
| 716 | |
Christopher Faulet | 87633c3 | 2023-04-03 18:32:50 +0200 | [diff] [blame] | 717 | /* FIXME: Don't watch the other side !*/ |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 718 | if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE)) |
William Lallemand | ec1f8a6 | 2022-10-13 17:49:54 +0200 | [diff] [blame] | 719 | return 1; |
| 720 | |
William Lallemand | 68192b2 | 2022-09-24 15:44:42 +0200 | [diff] [blame] | 721 | env = getenv("HAPROXY_LOAD_SUCCESS"); |
| 722 | if (!env) |
| 723 | return 1; |
| 724 | |
| 725 | if (strcmp(env, "0") == 0) { |
William Lallemand | ec1f8a6 | 2022-10-13 17:49:54 +0200 | [diff] [blame] | 726 | chunk_printf(&trash, "Success=0\n"); |
William Lallemand | 68192b2 | 2022-09-24 15:44:42 +0200 | [diff] [blame] | 727 | } else if (strcmp(env, "1") == 0) { |
William Lallemand | ec1f8a6 | 2022-10-13 17:49:54 +0200 | [diff] [blame] | 728 | chunk_printf(&trash, "Success=1\n"); |
William Lallemand | 68192b2 | 2022-09-24 15:44:42 +0200 | [diff] [blame] | 729 | } |
William Lallemand | 1344ebd | 2022-10-21 14:03:29 +0200 | [diff] [blame] | 730 | #ifdef USE_SHM_OPEN |
William Lallemand | ec1f8a6 | 2022-10-13 17:49:54 +0200 | [diff] [blame] | 731 | if (startup_logs && b_data(&startup_logs->buf) > 1) |
| 732 | chunk_appendf(&trash, "--\n"); |
William Lallemand | 68192b2 | 2022-09-24 15:44:42 +0200 | [diff] [blame] | 733 | |
William Lallemand | ec1f8a6 | 2022-10-13 17:49:54 +0200 | [diff] [blame] | 734 | if (applet_putchk(appctx, &trash) == -1) |
| 735 | return 0; |
William Lallemand | 68192b2 | 2022-09-24 15:44:42 +0200 | [diff] [blame] | 736 | |
William Lallemand | ec1f8a6 | 2022-10-13 17:49:54 +0200 | [diff] [blame] | 737 | if (startup_logs) { |
| 738 | appctx->io_handler = NULL; |
| 739 | ring_attach_cli(startup_logs, appctx, 0); |
| 740 | return 0; |
| 741 | } |
William Lallemand | 1344ebd | 2022-10-21 14:03:29 +0200 | [diff] [blame] | 742 | #else |
| 743 | if (applet_putchk(appctx, &trash) == -1) |
| 744 | return 0; |
| 745 | #endif |
William Lallemand | ec1f8a6 | 2022-10-13 17:49:54 +0200 | [diff] [blame] | 746 | return 1; |
| 747 | } |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 748 | |
William Lallemand | 27edc4b | 2019-05-07 17:49:33 +0200 | [diff] [blame] | 749 | static int mworker_parse_global_max_reloads(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 750 | const struct proxy *defpx, const char *file, int linenum, char **err) |
William Lallemand | 27edc4b | 2019-05-07 17:49:33 +0200 | [diff] [blame] | 751 | { |
| 752 | |
| 753 | int err_code = 0; |
| 754 | |
| 755 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 756 | goto out; |
| 757 | |
| 758 | if (*(args[1]) == 0) { |
| 759 | memprintf(err, "%sparsing [%s:%d] : '%s' expects an integer argument.\n", *err, file, linenum, args[0]); |
| 760 | err_code |= ERR_ALERT | ERR_FATAL; |
| 761 | goto out; |
| 762 | } |
| 763 | |
| 764 | max_reloads = atol(args[1]); |
| 765 | if (max_reloads < 0) { |
| 766 | memprintf(err, "%sparsing [%s:%d] '%s' : invalid value %d, must be >= 0", *err, file, linenum, args[0], max_reloads); |
| 767 | err_code |= ERR_ALERT | ERR_FATAL; |
| 768 | goto out; |
| 769 | } |
| 770 | |
| 771 | out: |
| 772 | return err_code; |
| 773 | } |
| 774 | |
Tim Duesterhus | 9b7a976 | 2019-05-16 20:23:22 +0200 | [diff] [blame] | 775 | void mworker_free_child(struct mworker_proc *child) |
| 776 | { |
William Lallemand | 08cb945 | 2022-01-27 15:33:40 +0100 | [diff] [blame] | 777 | int i; |
| 778 | |
Tim Duesterhus | 9b7a976 | 2019-05-16 20:23:22 +0200 | [diff] [blame] | 779 | if (child == NULL) |
| 780 | return; |
| 781 | |
William Lallemand | 08cb945 | 2022-01-27 15:33:40 +0100 | [diff] [blame] | 782 | for (i = 0; child->command && child->command[i]; i++) |
| 783 | ha_free(&child->command[i]); |
William Lallemand | e8669fc | 2019-06-12 18:21:17 +0200 | [diff] [blame] | 784 | |
William Lallemand | 08cb945 | 2022-01-27 15:33:40 +0100 | [diff] [blame] | 785 | ha_free(&child->command); |
| 786 | ha_free(&child->id); |
| 787 | ha_free(&child->version); |
Tim Duesterhus | 9b7a976 | 2019-05-16 20:23:22 +0200 | [diff] [blame] | 788 | free(child); |
| 789 | } |
William Lallemand | 27edc4b | 2019-05-07 17:49:33 +0200 | [diff] [blame] | 790 | |
| 791 | static struct cfg_kw_list mworker_kws = {{ }, { |
| 792 | { CFG_GLOBAL, "mworker-max-reloads", mworker_parse_global_max_reloads }, |
| 793 | { 0, NULL, NULL }, |
| 794 | }}; |
| 795 | |
| 796 | INITCALL1(STG_REGISTER, cfg_register_keywords, &mworker_kws); |
| 797 | |
| 798 | |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 799 | /* register cli keywords */ |
| 800 | static struct cli_kw_list cli_kws = {{ },{ |
Willy Tarreau | 23c740e | 2021-05-09 22:49:44 +0200 | [diff] [blame] | 801 | { { "@<relative pid>", NULL }, "@<relative pid> : send a command to the <relative pid> process", NULL, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY}, |
| 802 | { { "@!<pid>", NULL }, "@!<pid> : send a command to the <pid> process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY}, |
| 803 | { { "@master", NULL }, "@master : send a command to the master process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY}, |
| 804 | { { "show", "proc", NULL }, "show proc : show processes status", cli_parse_default, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY}, |
| 805 | { { "reload", NULL }, "reload : reload haproxy", cli_parse_reload, NULL, NULL, NULL, ACCESS_MASTER_ONLY}, |
William Lallemand | 2f67dd9 | 2022-10-21 14:00:05 +0200 | [diff] [blame] | 806 | { { "_loadstatus", NULL }, NULL, cli_parse_default, cli_io_handler_show_loadstatus, NULL, NULL, ACCESS_MASTER_ONLY}, |
William Lallemand | 88dc7c5 | 2019-04-01 11:30:01 +0200 | [diff] [blame] | 807 | {{},} |
| 808 | }}; |
| 809 | |
| 810 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |