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; |
| 38 | |
| 39 | if (!use_program) |
| 40 | return 0; |
| 41 | |
| 42 | /* find the right mworker_proc */ |
| 43 | list_for_each_entry(child, &proc_list, list) { |
| 44 | if (child->reloads == 0 && child->type == 'e') { |
| 45 | child->timestamp = now.tv_sec; |
| 46 | |
| 47 | ret = fork(); |
| 48 | if (ret < 0) { |
| 49 | ha_alert("Cannot fork program '%s'.\n", child->id); |
| 50 | exit(EXIT_FAILURE); /* there has been an error */ |
| 51 | } else if (ret > 0) { /* parent */ |
| 52 | child->pid = ret; |
| 53 | ha_notice("New program '%s' (%d) forked\n", child->id, ret); |
| 54 | continue; |
| 55 | } else if (ret == 0) { |
| 56 | /* In child */ |
| 57 | mworker_unblock_signals(); |
| 58 | mworker_cleanlisteners(); |
| 59 | mworker_cleantasks(); |
| 60 | |
| 61 | execvp(child->command[0], child->command); |
| 62 | |
| 63 | ha_alert("Cannot execute %s: %s\n", child->command[0], strerror(errno)); |
| 64 | exit(EXIT_FAILURE); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return 0; |
| 70 | |
| 71 | } |
| 72 | |
| 73 | |
| 74 | /* Configuration */ |
| 75 | |
| 76 | int cfg_parse_program(const char *file, int linenum, char **args, int kwm) |
| 77 | { |
| 78 | static struct mworker_proc *ext_child = NULL; |
| 79 | struct mworker_proc *child; |
| 80 | int err_code = 0; |
| 81 | |
| 82 | if (!strcmp(args[0], "program")) { |
| 83 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 84 | err_code |= ERR_ABORT; |
| 85 | goto error; |
| 86 | } |
| 87 | |
| 88 | if (!*args[1]) { |
| 89 | ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n", |
| 90 | file, linenum, args[0]); |
| 91 | err_code |= ERR_ALERT | ERR_ABORT; |
| 92 | goto error; |
| 93 | } |
| 94 | |
| 95 | ext_child = calloc(1, sizeof(*ext_child)); |
| 96 | if (!ext_child) { |
| 97 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 98 | err_code |= ERR_ALERT | ERR_ABORT; |
| 99 | goto error; |
| 100 | } |
| 101 | |
| 102 | ext_child->type = 'e'; /* external process */ |
| 103 | ext_child->command = NULL; |
| 104 | ext_child->path = NULL; |
| 105 | ext_child->id = NULL; |
| 106 | ext_child->pid = -1; |
| 107 | ext_child->relative_pid = -1; |
| 108 | ext_child->reloads = 0; |
| 109 | ext_child->timestamp = -1; |
| 110 | ext_child->ipc_fd[0] = -1; |
| 111 | ext_child->ipc_fd[1] = -1; |
| 112 | LIST_INIT(&ext_child->list); |
| 113 | |
| 114 | list_for_each_entry(child, &proc_list, list) { |
| 115 | if (child->reloads == 0 && child->type == 'e') { |
| 116 | if (!strcmp(args[1], child->id)) { |
| 117 | ha_alert("parsing [%s:%d]: '%s' program section already exists in the configuration.\n", file, linenum, args[1]); |
| 118 | err_code |= ERR_ALERT | ERR_ABORT; |
| 119 | goto error; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | ext_child->id = strdup(args[1]); |
| 125 | if (!ext_child->id) { |
| 126 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 127 | err_code |= ERR_ALERT | ERR_ABORT; |
| 128 | goto error; |
| 129 | } |
| 130 | |
| 131 | LIST_ADDQ(&proc_list, &ext_child->list); |
| 132 | |
| 133 | } else if (!strcmp(args[0], "command")) { |
| 134 | int arg_nb = 0; |
| 135 | int i = 0; |
| 136 | |
| 137 | if (*(args[1]) == 0) { |
| 138 | ha_alert("parsing [%s:%d]: '%s' expects a command with optional arguments separated in words.\n", file, linenum, args[0]); |
| 139 | err_code |= ERR_ALERT | ERR_FATAL; |
| 140 | goto error; |
| 141 | } |
| 142 | |
| 143 | while (*args[arg_nb+1]) |
| 144 | arg_nb++; |
| 145 | |
| 146 | ext_child->command = calloc(arg_nb+1, sizeof(*ext_child->command)); |
| 147 | |
| 148 | if (!ext_child->command) { |
| 149 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 150 | err_code |= ERR_ALERT | ERR_ABORT; |
| 151 | goto error; |
| 152 | } |
| 153 | |
| 154 | while (i < arg_nb) { |
| 155 | ext_child->command[i] = strdup(args[i+1]); |
| 156 | if (!ext_child->command[i]) { |
| 157 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 158 | err_code |= ERR_ALERT | ERR_ABORT; |
| 159 | goto error; |
| 160 | } |
| 161 | i++; |
| 162 | } |
| 163 | ext_child->command[i] = NULL; |
| 164 | |
| 165 | } else { |
| 166 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "program"); |
| 167 | err_code |= ERR_ALERT | ERR_FATAL; |
| 168 | goto error; |
| 169 | } |
| 170 | |
| 171 | use_program = 1; |
| 172 | |
| 173 | return err_code; |
| 174 | |
| 175 | error: |
| 176 | LIST_DEL(&ext_child->list); |
| 177 | if (ext_child->command) { |
| 178 | int i; |
| 179 | |
| 180 | for (i = 0; ext_child->command[i]; i++) { |
| 181 | if (ext_child->command[i]) { |
| 182 | free(ext_child->command[i]); |
| 183 | ext_child->command[i] = NULL; |
| 184 | } |
| 185 | } |
| 186 | free(ext_child->command); |
| 187 | ext_child->command = NULL; |
| 188 | } |
| 189 | if (ext_child->id) { |
| 190 | free(ext_child->id); |
| 191 | ext_child->id = NULL; |
| 192 | } |
| 193 | |
| 194 | free(ext_child); |
| 195 | ext_child = NULL; |
| 196 | |
| 197 | return err_code; |
| 198 | |
| 199 | } |
| 200 | |
| 201 | int cfg_program_postparser() |
| 202 | { |
| 203 | int err_code = 0; |
| 204 | struct mworker_proc *child; |
| 205 | |
| 206 | list_for_each_entry(child, &proc_list, list) { |
| 207 | if (child->reloads == 0 && child->type == 'e') { |
| 208 | if (child->command == NULL) { |
| 209 | ha_alert("The program section '%s' lacks a command to launch.\n", child->id); |
| 210 | err_code |= ERR_ALERT | ERR_FATAL; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | if (use_program && !(global.mode & MODE_MWORKER)) { |
| 216 | ha_alert("Can't use a 'program' section without master worker mode.\n"); |
| 217 | err_code |= ERR_ALERT | ERR_FATAL; |
| 218 | } |
| 219 | |
| 220 | return err_code; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | REGISTER_CONFIG_SECTION("program", cfg_parse_program, NULL); |
| 225 | REGISTER_CONFIG_POSTPARSER("program", cfg_program_postparser); |