William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Master Worker - program |
| 3 | * |
| 4 | * Copyright HAProxy Technologies - 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 | |
| 13 | #define _GNU_SOURCE |
| 14 | |
| 15 | #include <sys/types.h> |
| 16 | #include <errno.h> |
| 17 | #include <grp.h> |
Andrew Heberle | 9723696 | 2019-07-12 11:50:26 +0800 | [diff] [blame] | 18 | #include <pwd.h> |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <string.h> |
| 21 | #include <unistd.h> |
| 22 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 23 | #include <haproxy/api.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 24 | #include <haproxy/cfgparse.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 25 | #include <haproxy/errors.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 26 | #include <haproxy/global.h> |
Willy Tarreau | b5abe5b | 2020-06-04 14:07:37 +0200 | [diff] [blame] | 27 | #include <haproxy/mworker.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 28 | #include <haproxy/task.h> |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 29 | |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 30 | |
| 31 | static int use_program = 0; /* do we use the program section ? */ |
| 32 | |
| 33 | /* |
| 34 | * Launch every programs |
| 35 | */ |
| 36 | int mworker_ext_launch_all() |
| 37 | { |
| 38 | int ret; |
| 39 | struct mworker_proc *child; |
William Lallemand | bd3de3e | 2019-04-12 16:09:22 +0200 | [diff] [blame] | 40 | struct mworker_proc *tmp; |
| 41 | int reexec = 0; |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 42 | |
| 43 | if (!use_program) |
| 44 | return 0; |
| 45 | |
William Lallemand | bd3de3e | 2019-04-12 16:09:22 +0200 | [diff] [blame] | 46 | reexec = getenv("HAPROXY_MWORKER_REEXEC") ? 1 : 0; |
| 47 | |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 48 | /* find the right mworker_proc */ |
William Lallemand | bd3de3e | 2019-04-12 16:09:22 +0200 | [diff] [blame] | 49 | list_for_each_entry_safe(child, tmp, &proc_list, list) { |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 50 | if (child->reloads == 0 && (child->options & PROC_O_TYPE_PROG)) { |
William Lallemand | bd3de3e | 2019-04-12 16:09:22 +0200 | [diff] [blame] | 51 | |
| 52 | if (reexec && (!(child->options & PROC_O_START_RELOAD))) { |
| 53 | struct mworker_proc *old_child; |
| 54 | |
| 55 | /* |
| 56 | * This is a reload and we don't want to fork a |
| 57 | * new program so have to remove the entry in |
| 58 | * the list. |
| 59 | * |
| 60 | * But before that, we need to mark the |
| 61 | * previous program as not leaving, if we find one. |
| 62 | */ |
| 63 | |
| 64 | list_for_each_entry(old_child, &proc_list, list) { |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 65 | if (!(old_child->options & PROC_O_TYPE_PROG) || (!(old_child->options & PROC_O_LEAVING))) |
William Lallemand | bd3de3e | 2019-04-12 16:09:22 +0200 | [diff] [blame] | 66 | continue; |
| 67 | |
| 68 | if (!strcmp(old_child->id, child->id)) |
| 69 | old_child->options &= ~PROC_O_LEAVING; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | LIST_DEL(&child->list); |
Tim Duesterhus | 9b7a976 | 2019-05-16 20:23:22 +0200 | [diff] [blame] | 74 | mworker_free_child(child); |
William Lallemand | bd3de3e | 2019-04-12 16:09:22 +0200 | [diff] [blame] | 75 | child = NULL; |
| 76 | |
| 77 | continue; |
| 78 | } |
| 79 | |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 80 | child->timestamp = now.tv_sec; |
| 81 | |
| 82 | ret = fork(); |
| 83 | if (ret < 0) { |
| 84 | ha_alert("Cannot fork program '%s'.\n", child->id); |
| 85 | exit(EXIT_FAILURE); /* there has been an error */ |
| 86 | } else if (ret > 0) { /* parent */ |
| 87 | child->pid = ret; |
| 88 | ha_notice("New program '%s' (%d) forked\n", child->id, ret); |
| 89 | continue; |
| 90 | } else if (ret == 0) { |
| 91 | /* In child */ |
| 92 | mworker_unblock_signals(); |
| 93 | mworker_cleanlisteners(); |
| 94 | mworker_cleantasks(); |
| 95 | |
Andrew Heberle | 9723696 | 2019-07-12 11:50:26 +0800 | [diff] [blame] | 96 | /* setgid / setuid */ |
| 97 | if (child->gid != -1) { |
| 98 | if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1) |
| 99 | ha_warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'" |
| 100 | " without 'uid'/'user' is generally useless.\n", child->command[0]); |
| 101 | |
| 102 | if (setgid(child->gid) == -1) { |
| 103 | ha_alert("[%s.main()] Cannot set gid %d.\n", child->command[0], child->gid); |
| 104 | exit(1); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | if (child->uid != -1 && setuid(child->uid) == -1) { |
| 109 | ha_alert("[%s.main()] Cannot set uid %d.\n", child->command[0], child->gid); |
| 110 | exit(1); |
| 111 | } |
| 112 | |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 113 | execvp(child->command[0], child->command); |
| 114 | |
| 115 | ha_alert("Cannot execute %s: %s\n", child->command[0], strerror(errno)); |
| 116 | exit(EXIT_FAILURE); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return 0; |
| 122 | |
| 123 | } |
| 124 | |
| 125 | |
| 126 | /* Configuration */ |
| 127 | |
| 128 | int cfg_parse_program(const char *file, int linenum, char **args, int kwm) |
| 129 | { |
| 130 | static struct mworker_proc *ext_child = NULL; |
| 131 | struct mworker_proc *child; |
| 132 | int err_code = 0; |
| 133 | |
| 134 | if (!strcmp(args[0], "program")) { |
| 135 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 136 | err_code |= ERR_ABORT; |
| 137 | goto error; |
| 138 | } |
| 139 | |
| 140 | if (!*args[1]) { |
| 141 | ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n", |
| 142 | file, linenum, args[0]); |
| 143 | err_code |= ERR_ALERT | ERR_ABORT; |
| 144 | goto error; |
| 145 | } |
| 146 | |
| 147 | ext_child = calloc(1, sizeof(*ext_child)); |
| 148 | if (!ext_child) { |
| 149 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 150 | err_code |= ERR_ALERT | ERR_ABORT; |
| 151 | goto error; |
| 152 | } |
| 153 | |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 154 | ext_child->options |= PROC_O_TYPE_PROG; /* external process */ |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 155 | ext_child->command = NULL; |
| 156 | ext_child->path = NULL; |
| 157 | ext_child->id = NULL; |
| 158 | ext_child->pid = -1; |
| 159 | ext_child->relative_pid = -1; |
| 160 | ext_child->reloads = 0; |
| 161 | ext_child->timestamp = -1; |
| 162 | ext_child->ipc_fd[0] = -1; |
| 163 | ext_child->ipc_fd[1] = -1; |
William Lallemand | bd3de3e | 2019-04-12 16:09:22 +0200 | [diff] [blame] | 164 | ext_child->options |= PROC_O_START_RELOAD; /* restart the programs by default */ |
Andrew Heberle | 9723696 | 2019-07-12 11:50:26 +0800 | [diff] [blame] | 165 | ext_child->uid = -1; |
| 166 | ext_child->gid = -1; |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 167 | LIST_INIT(&ext_child->list); |
| 168 | |
| 169 | list_for_each_entry(child, &proc_list, list) { |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 170 | if (child->reloads == 0 && (child->options & PROC_O_TYPE_PROG)) { |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 171 | if (!strcmp(args[1], child->id)) { |
| 172 | ha_alert("parsing [%s:%d]: '%s' program section already exists in the configuration.\n", file, linenum, args[1]); |
| 173 | err_code |= ERR_ALERT | ERR_ABORT; |
| 174 | goto error; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | ext_child->id = strdup(args[1]); |
| 180 | if (!ext_child->id) { |
| 181 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 182 | err_code |= ERR_ALERT | ERR_ABORT; |
| 183 | goto error; |
| 184 | } |
| 185 | |
| 186 | LIST_ADDQ(&proc_list, &ext_child->list); |
| 187 | |
| 188 | } else if (!strcmp(args[0], "command")) { |
| 189 | int arg_nb = 0; |
| 190 | int i = 0; |
| 191 | |
| 192 | if (*(args[1]) == 0) { |
| 193 | ha_alert("parsing [%s:%d]: '%s' expects a command with optional arguments separated in words.\n", file, linenum, args[0]); |
| 194 | err_code |= ERR_ALERT | ERR_FATAL; |
| 195 | goto error; |
| 196 | } |
| 197 | |
| 198 | while (*args[arg_nb+1]) |
| 199 | arg_nb++; |
| 200 | |
| 201 | ext_child->command = calloc(arg_nb+1, sizeof(*ext_child->command)); |
| 202 | |
| 203 | if (!ext_child->command) { |
| 204 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 205 | err_code |= ERR_ALERT | ERR_ABORT; |
| 206 | goto error; |
| 207 | } |
| 208 | |
| 209 | while (i < arg_nb) { |
| 210 | ext_child->command[i] = strdup(args[i+1]); |
| 211 | if (!ext_child->command[i]) { |
| 212 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 213 | err_code |= ERR_ALERT | ERR_ABORT; |
| 214 | goto error; |
| 215 | } |
| 216 | i++; |
| 217 | } |
| 218 | ext_child->command[i] = NULL; |
| 219 | |
William Lallemand | bd3de3e | 2019-04-12 16:09:22 +0200 | [diff] [blame] | 220 | } else if (!strcmp(args[0], "option")) { |
| 221 | |
| 222 | if (*(args[1]) == '\0') { |
| 223 | ha_alert("parsing [%s:%d]: '%s' expects an option name.\n", |
| 224 | file, linenum, args[0]); |
| 225 | err_code |= ERR_ALERT | ERR_FATAL; |
| 226 | goto error; |
| 227 | } |
| 228 | |
| 229 | if (strcmp(args[1], "start-on-reload") == 0) { |
| 230 | if (alertif_too_many_args_idx(0, 1, file, linenum, args, &err_code)) |
| 231 | goto error; |
| 232 | if (kwm == KWM_STD) |
| 233 | ext_child->options |= PROC_O_START_RELOAD; |
| 234 | else if (kwm == KWM_NO) |
| 235 | ext_child->options &= ~PROC_O_START_RELOAD; |
| 236 | goto out; |
| 237 | |
| 238 | } else { |
| 239 | ha_alert("parsing [%s:%d] : unknown option '%s'.\n", file, linenum, args[1]); |
| 240 | err_code |= ERR_ALERT | ERR_FATAL; |
| 241 | goto error; |
| 242 | } |
Andrew Heberle | 9723696 | 2019-07-12 11:50:26 +0800 | [diff] [blame] | 243 | } else if (!strcmp(args[0], "user")) { |
| 244 | struct passwd *ext_child_user; |
| 245 | if (*(args[1]) == '\0') { |
| 246 | ha_alert("parsing [%s:%d]: '%s' expects a user name.\n", |
| 247 | file, linenum, args[0]); |
| 248 | err_code |= ERR_ALERT | ERR_FATAL; |
| 249 | goto error; |
| 250 | } |
| 251 | |
| 252 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 253 | goto error; |
| 254 | |
| 255 | if (ext_child->uid != -1) { |
| 256 | ha_alert("parsing [%s:%d] : user/uid already specified. Continuing.\n", file, linenum); |
| 257 | err_code |= ERR_ALERT; |
| 258 | goto out; |
| 259 | } |
| 260 | |
| 261 | ext_child_user = getpwnam(args[1]); |
| 262 | if (ext_child_user != NULL) { |
| 263 | ext_child->uid = (int)ext_child_user->pw_uid; |
| 264 | } else { |
| 265 | ha_alert("parsing [%s:%d] : cannot find user id for '%s' (%d:%s)\n", file, linenum, args[1], errno, strerror(errno)); |
| 266 | err_code |= ERR_ALERT | ERR_FATAL; |
| 267 | } |
| 268 | } else if (!strcmp(args[0], "group")) { |
| 269 | struct group *ext_child_group; |
| 270 | if (*(args[1]) == '\0') { |
| 271 | ha_alert("parsing [%s:%d]: '%s' expects a group name.\n", |
| 272 | file, linenum, args[0]); |
| 273 | err_code |= ERR_ALERT | ERR_FATAL; |
| 274 | goto error; |
| 275 | } |
| 276 | |
| 277 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 278 | goto error; |
| 279 | |
| 280 | if (ext_child->gid != -1) { |
| 281 | ha_alert("parsing [%s:%d] : group/gid already specified. Continuing.\n", file, linenum); |
| 282 | err_code |= ERR_ALERT; |
| 283 | goto out; |
| 284 | } |
| 285 | |
| 286 | ext_child_group = getgrnam(args[1]); |
| 287 | if (ext_child_group != NULL) { |
| 288 | ext_child->gid = (int)ext_child_group->gr_gid; |
| 289 | } else { |
| 290 | ha_alert("parsing [%s:%d] : cannot find group id for '%s' (%d:%s)\n", file, linenum, args[1], errno, strerror(errno)); |
| 291 | err_code |= ERR_ALERT | ERR_FATAL; |
| 292 | } |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 293 | } else { |
| 294 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "program"); |
| 295 | err_code |= ERR_ALERT | ERR_FATAL; |
| 296 | goto error; |
| 297 | } |
| 298 | |
| 299 | use_program = 1; |
| 300 | |
| 301 | return err_code; |
| 302 | |
| 303 | error: |
Tim Duesterhus | 2c9e274 | 2019-06-23 22:10:12 +0200 | [diff] [blame] | 304 | if (ext_child) { |
| 305 | LIST_DEL(&ext_child->list); |
| 306 | if (ext_child->command) { |
| 307 | int i; |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 308 | |
Tim Duesterhus | 2c9e274 | 2019-06-23 22:10:12 +0200 | [diff] [blame] | 309 | for (i = 0; ext_child->command[i]; i++) { |
| 310 | if (ext_child->command[i]) { |
| 311 | free(ext_child->command[i]); |
| 312 | ext_child->command[i] = NULL; |
| 313 | } |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 314 | } |
Tim Duesterhus | 2c9e274 | 2019-06-23 22:10:12 +0200 | [diff] [blame] | 315 | free(ext_child->command); |
| 316 | ext_child->command = NULL; |
| 317 | } |
| 318 | if (ext_child->id) { |
| 319 | free(ext_child->id); |
| 320 | ext_child->id = NULL; |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 321 | } |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | free(ext_child); |
| 325 | ext_child = NULL; |
| 326 | |
William Lallemand | bd3de3e | 2019-04-12 16:09:22 +0200 | [diff] [blame] | 327 | out: |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 328 | return err_code; |
| 329 | |
| 330 | } |
| 331 | |
| 332 | int cfg_program_postparser() |
| 333 | { |
| 334 | int err_code = 0; |
| 335 | struct mworker_proc *child; |
| 336 | |
| 337 | list_for_each_entry(child, &proc_list, list) { |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 338 | if (child->reloads == 0 && (child->options & PROC_O_TYPE_PROG)) { |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 339 | if (child->command == NULL) { |
| 340 | ha_alert("The program section '%s' lacks a command to launch.\n", child->id); |
| 341 | err_code |= ERR_ALERT | ERR_FATAL; |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | if (use_program && !(global.mode & MODE_MWORKER)) { |
| 347 | ha_alert("Can't use a 'program' section without master worker mode.\n"); |
| 348 | err_code |= ERR_ALERT | ERR_FATAL; |
| 349 | } |
| 350 | |
| 351 | return err_code; |
| 352 | } |
| 353 | |
| 354 | |
| 355 | REGISTER_CONFIG_SECTION("program", cfg_parse_program, NULL); |
| 356 | REGISTER_CONFIG_POSTPARSER("program", cfg_program_postparser); |