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> |
| 18 | #include <stdio.h> |
| 19 | #include <string.h> |
| 20 | #include <unistd.h> |
| 21 | |
| 22 | #include <common/cfgparse.h> |
| 23 | #include <common/errors.h> |
| 24 | #include <common/initcall.h> |
| 25 | |
| 26 | #include <proto/log.h> |
| 27 | #include <proto/mworker.h> |
| 28 | |
| 29 | static int use_program = 0; /* do we use the program section ? */ |
| 30 | |
| 31 | /* |
| 32 | * Launch every programs |
| 33 | */ |
| 34 | int mworker_ext_launch_all() |
| 35 | { |
| 36 | int ret; |
| 37 | struct mworker_proc *child; |
William Lallemand | bd3de3e | 2019-04-12 16:09:22 +0200 | [diff] [blame] | 38 | struct mworker_proc *tmp; |
| 39 | int reexec = 0; |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 40 | |
| 41 | if (!use_program) |
| 42 | return 0; |
| 43 | |
William Lallemand | bd3de3e | 2019-04-12 16:09:22 +0200 | [diff] [blame] | 44 | reexec = getenv("HAPROXY_MWORKER_REEXEC") ? 1 : 0; |
| 45 | |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 46 | /* find the right mworker_proc */ |
William Lallemand | bd3de3e | 2019-04-12 16:09:22 +0200 | [diff] [blame] | 47 | list_for_each_entry_safe(child, tmp, &proc_list, list) { |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 48 | if (child->reloads == 0 && (child->options & PROC_O_TYPE_PROG)) { |
William Lallemand | bd3de3e | 2019-04-12 16:09:22 +0200 | [diff] [blame] | 49 | |
| 50 | if (reexec && (!(child->options & PROC_O_START_RELOAD))) { |
| 51 | struct mworker_proc *old_child; |
| 52 | |
| 53 | /* |
| 54 | * This is a reload and we don't want to fork a |
| 55 | * new program so have to remove the entry in |
| 56 | * the list. |
| 57 | * |
| 58 | * But before that, we need to mark the |
| 59 | * previous program as not leaving, if we find one. |
| 60 | */ |
| 61 | |
| 62 | list_for_each_entry(old_child, &proc_list, list) { |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 63 | 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] | 64 | continue; |
| 65 | |
| 66 | if (!strcmp(old_child->id, child->id)) |
| 67 | old_child->options &= ~PROC_O_LEAVING; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | LIST_DEL(&child->list); |
Tim Duesterhus | 9b7a976 | 2019-05-16 20:23:22 +0200 | [diff] [blame] | 72 | mworker_free_child(child); |
William Lallemand | bd3de3e | 2019-04-12 16:09:22 +0200 | [diff] [blame] | 73 | child = NULL; |
| 74 | |
| 75 | continue; |
| 76 | } |
| 77 | |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 78 | child->timestamp = now.tv_sec; |
| 79 | |
| 80 | ret = fork(); |
| 81 | if (ret < 0) { |
| 82 | ha_alert("Cannot fork program '%s'.\n", child->id); |
| 83 | exit(EXIT_FAILURE); /* there has been an error */ |
| 84 | } else if (ret > 0) { /* parent */ |
| 85 | child->pid = ret; |
| 86 | ha_notice("New program '%s' (%d) forked\n", child->id, ret); |
| 87 | continue; |
| 88 | } else if (ret == 0) { |
| 89 | /* In child */ |
| 90 | mworker_unblock_signals(); |
| 91 | mworker_cleanlisteners(); |
| 92 | mworker_cleantasks(); |
| 93 | |
| 94 | execvp(child->command[0], child->command); |
| 95 | |
| 96 | ha_alert("Cannot execute %s: %s\n", child->command[0], strerror(errno)); |
| 97 | exit(EXIT_FAILURE); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return 0; |
| 103 | |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /* Configuration */ |
| 108 | |
| 109 | int cfg_parse_program(const char *file, int linenum, char **args, int kwm) |
| 110 | { |
| 111 | static struct mworker_proc *ext_child = NULL; |
| 112 | struct mworker_proc *child; |
| 113 | int err_code = 0; |
| 114 | |
| 115 | if (!strcmp(args[0], "program")) { |
| 116 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 117 | err_code |= ERR_ABORT; |
| 118 | goto error; |
| 119 | } |
| 120 | |
| 121 | if (!*args[1]) { |
| 122 | ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n", |
| 123 | file, linenum, args[0]); |
| 124 | err_code |= ERR_ALERT | ERR_ABORT; |
| 125 | goto error; |
| 126 | } |
| 127 | |
| 128 | ext_child = calloc(1, sizeof(*ext_child)); |
| 129 | if (!ext_child) { |
| 130 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 131 | err_code |= ERR_ALERT | ERR_ABORT; |
| 132 | goto error; |
| 133 | } |
| 134 | |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 135 | ext_child->options |= PROC_O_TYPE_PROG; /* external process */ |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 136 | ext_child->command = NULL; |
| 137 | ext_child->path = NULL; |
| 138 | ext_child->id = NULL; |
| 139 | ext_child->pid = -1; |
| 140 | ext_child->relative_pid = -1; |
| 141 | ext_child->reloads = 0; |
| 142 | ext_child->timestamp = -1; |
| 143 | ext_child->ipc_fd[0] = -1; |
| 144 | ext_child->ipc_fd[1] = -1; |
William Lallemand | bd3de3e | 2019-04-12 16:09:22 +0200 | [diff] [blame] | 145 | ext_child->options |= PROC_O_START_RELOAD; /* restart the programs by default */ |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 146 | LIST_INIT(&ext_child->list); |
| 147 | |
| 148 | list_for_each_entry(child, &proc_list, list) { |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 149 | if (child->reloads == 0 && (child->options & PROC_O_TYPE_PROG)) { |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 150 | if (!strcmp(args[1], child->id)) { |
| 151 | ha_alert("parsing [%s:%d]: '%s' program section already exists in the configuration.\n", file, linenum, args[1]); |
| 152 | err_code |= ERR_ALERT | ERR_ABORT; |
| 153 | goto error; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | ext_child->id = strdup(args[1]); |
| 159 | if (!ext_child->id) { |
| 160 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 161 | err_code |= ERR_ALERT | ERR_ABORT; |
| 162 | goto error; |
| 163 | } |
| 164 | |
| 165 | LIST_ADDQ(&proc_list, &ext_child->list); |
| 166 | |
| 167 | } else if (!strcmp(args[0], "command")) { |
| 168 | int arg_nb = 0; |
| 169 | int i = 0; |
| 170 | |
| 171 | if (*(args[1]) == 0) { |
| 172 | ha_alert("parsing [%s:%d]: '%s' expects a command with optional arguments separated in words.\n", file, linenum, args[0]); |
| 173 | err_code |= ERR_ALERT | ERR_FATAL; |
| 174 | goto error; |
| 175 | } |
| 176 | |
| 177 | while (*args[arg_nb+1]) |
| 178 | arg_nb++; |
| 179 | |
| 180 | ext_child->command = calloc(arg_nb+1, sizeof(*ext_child->command)); |
| 181 | |
| 182 | if (!ext_child->command) { |
| 183 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 184 | err_code |= ERR_ALERT | ERR_ABORT; |
| 185 | goto error; |
| 186 | } |
| 187 | |
| 188 | while (i < arg_nb) { |
| 189 | ext_child->command[i] = strdup(args[i+1]); |
| 190 | if (!ext_child->command[i]) { |
| 191 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 192 | err_code |= ERR_ALERT | ERR_ABORT; |
| 193 | goto error; |
| 194 | } |
| 195 | i++; |
| 196 | } |
| 197 | ext_child->command[i] = NULL; |
| 198 | |
William Lallemand | bd3de3e | 2019-04-12 16:09:22 +0200 | [diff] [blame] | 199 | } else if (!strcmp(args[0], "option")) { |
| 200 | |
| 201 | if (*(args[1]) == '\0') { |
| 202 | ha_alert("parsing [%s:%d]: '%s' expects an option name.\n", |
| 203 | file, linenum, args[0]); |
| 204 | err_code |= ERR_ALERT | ERR_FATAL; |
| 205 | goto error; |
| 206 | } |
| 207 | |
| 208 | if (strcmp(args[1], "start-on-reload") == 0) { |
| 209 | if (alertif_too_many_args_idx(0, 1, file, linenum, args, &err_code)) |
| 210 | goto error; |
| 211 | if (kwm == KWM_STD) |
| 212 | ext_child->options |= PROC_O_START_RELOAD; |
| 213 | else if (kwm == KWM_NO) |
| 214 | ext_child->options &= ~PROC_O_START_RELOAD; |
| 215 | goto out; |
| 216 | |
| 217 | } else { |
| 218 | ha_alert("parsing [%s:%d] : unknown option '%s'.\n", file, linenum, args[1]); |
| 219 | err_code |= ERR_ALERT | ERR_FATAL; |
| 220 | goto error; |
| 221 | } |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 222 | } else { |
| 223 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "program"); |
| 224 | err_code |= ERR_ALERT | ERR_FATAL; |
| 225 | goto error; |
| 226 | } |
| 227 | |
| 228 | use_program = 1; |
| 229 | |
| 230 | return err_code; |
| 231 | |
| 232 | error: |
Tim Duesterhus | 380ac33 | 2019-06-23 22:10:12 +0200 | [diff] [blame] | 233 | if (ext_child) { |
| 234 | LIST_DEL(&ext_child->list); |
| 235 | if (ext_child->command) { |
| 236 | int i; |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 237 | |
Tim Duesterhus | 380ac33 | 2019-06-23 22:10:12 +0200 | [diff] [blame] | 238 | for (i = 0; ext_child->command[i]; i++) { |
| 239 | if (ext_child->command[i]) { |
| 240 | free(ext_child->command[i]); |
| 241 | ext_child->command[i] = NULL; |
| 242 | } |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 243 | } |
Tim Duesterhus | 380ac33 | 2019-06-23 22:10:12 +0200 | [diff] [blame] | 244 | free(ext_child->command); |
| 245 | ext_child->command = NULL; |
| 246 | } |
| 247 | if (ext_child->id) { |
| 248 | free(ext_child->id); |
| 249 | ext_child->id = NULL; |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 250 | } |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | free(ext_child); |
| 254 | ext_child = NULL; |
| 255 | |
William Lallemand | bd3de3e | 2019-04-12 16:09:22 +0200 | [diff] [blame] | 256 | out: |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 257 | return err_code; |
| 258 | |
| 259 | } |
| 260 | |
| 261 | int cfg_program_postparser() |
| 262 | { |
| 263 | int err_code = 0; |
| 264 | struct mworker_proc *child; |
| 265 | |
| 266 | list_for_each_entry(child, &proc_list, list) { |
William Lallemand | 8f7069a | 2019-04-12 16:09:23 +0200 | [diff] [blame] | 267 | if (child->reloads == 0 && (child->options & PROC_O_TYPE_PROG)) { |
William Lallemand | 9a1ee7a | 2019-04-01 11:30:02 +0200 | [diff] [blame] | 268 | if (child->command == NULL) { |
| 269 | ha_alert("The program section '%s' lacks a command to launch.\n", child->id); |
| 270 | err_code |= ERR_ALERT | ERR_FATAL; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if (use_program && !(global.mode & MODE_MWORKER)) { |
| 276 | ha_alert("Can't use a 'program' section without master worker mode.\n"); |
| 277 | err_code |= ERR_ALERT | ERR_FATAL; |
| 278 | } |
| 279 | |
| 280 | return err_code; |
| 281 | } |
| 282 | |
| 283 | |
| 284 | REGISTER_CONFIG_SECTION("program", cfg_parse_program, NULL); |
| 285 | REGISTER_CONFIG_POSTPARSER("program", cfg_program_postparser); |