blob: 5ecd3df36b72db9450532404333554c0128774a3 [file] [log] [blame]
William Lallemand9a1ee7a2019-04-01 11:30:02 +02001/*
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 Heberle97236962019-07-12 11:50:26 +080018#include <pwd.h>
William Lallemand9a1ee7a2019-04-01 11:30:02 +020019#include <stdio.h>
20#include <string.h>
21#include <unistd.h>
22
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020023#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020024#include <haproxy/cfgparse.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020025#include <haproxy/errors.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020026#include <haproxy/global.h>
Willy Tarreaub5abe5b2020-06-04 14:07:37 +020027#include <haproxy/mworker.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020028#include <haproxy/task.h>
Willy Tarreau410e2592021-10-06 19:31:06 +020029#include <haproxy/time.h>
William Lallemand9a1ee7a2019-04-01 11:30:02 +020030
William Lallemand9a1ee7a2019-04-01 11:30:02 +020031
32static int use_program = 0; /* do we use the program section ? */
33
34/*
35 * Launch every programs
36 */
37int mworker_ext_launch_all()
38{
39 int ret;
40 struct mworker_proc *child;
William Lallemandbd3de3e2019-04-12 16:09:22 +020041 struct mworker_proc *tmp;
42 int reexec = 0;
William Lallemand9a1ee7a2019-04-01 11:30:02 +020043
44 if (!use_program)
45 return 0;
46
William Lallemandbd3de3e2019-04-12 16:09:22 +020047 reexec = getenv("HAPROXY_MWORKER_REEXEC") ? 1 : 0;
48
William Lallemand9a1ee7a2019-04-01 11:30:02 +020049 /* find the right mworker_proc */
William Lallemandbd3de3e2019-04-12 16:09:22 +020050 list_for_each_entry_safe(child, tmp, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +020051 if (child->reloads == 0 && (child->options & PROC_O_TYPE_PROG)) {
William Lallemandbd3de3e2019-04-12 16:09:22 +020052
53 if (reexec && (!(child->options & PROC_O_START_RELOAD))) {
54 struct mworker_proc *old_child;
55
56 /*
57 * This is a reload and we don't want to fork a
58 * new program so have to remove the entry in
59 * the list.
60 *
61 * But before that, we need to mark the
62 * previous program as not leaving, if we find one.
63 */
64
65 list_for_each_entry(old_child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +020066 if (!(old_child->options & PROC_O_TYPE_PROG) || (!(old_child->options & PROC_O_LEAVING)))
William Lallemandbd3de3e2019-04-12 16:09:22 +020067 continue;
68
Tim Duesterhuse5ff1412021-01-02 22:31:53 +010069 if (strcmp(old_child->id, child->id) == 0)
William Lallemandbd3de3e2019-04-12 16:09:22 +020070 old_child->options &= ~PROC_O_LEAVING;
71 }
72
73
Willy Tarreau2b718102021-04-21 07:32:39 +020074 LIST_DELETE(&child->list);
Tim Duesterhus9b7a9762019-05-16 20:23:22 +020075 mworker_free_child(child);
William Lallemandbd3de3e2019-04-12 16:09:22 +020076 child = NULL;
77
78 continue;
79 }
80
William Lallemand9a1ee7a2019-04-01 11:30:02 +020081 child->timestamp = now.tv_sec;
82
83 ret = fork();
84 if (ret < 0) {
85 ha_alert("Cannot fork program '%s'.\n", child->id);
86 exit(EXIT_FAILURE); /* there has been an error */
87 } else if (ret > 0) { /* parent */
88 child->pid = ret;
89 ha_notice("New program '%s' (%d) forked\n", child->id, ret);
90 continue;
91 } else if (ret == 0) {
92 /* In child */
93 mworker_unblock_signals();
94 mworker_cleanlisteners();
95 mworker_cleantasks();
96
Andrew Heberle97236962019-07-12 11:50:26 +080097 /* setgid / setuid */
98 if (child->gid != -1) {
99 if (getgroups(0, NULL) > 0 && setgroups(0, NULL) == -1)
100 ha_warning("[%s.main()] Failed to drop supplementary groups. Using 'gid'/'group'"
101 " without 'uid'/'user' is generally useless.\n", child->command[0]);
102
103 if (setgid(child->gid) == -1) {
104 ha_alert("[%s.main()] Cannot set gid %d.\n", child->command[0], child->gid);
105 exit(1);
106 }
107 }
108
109 if (child->uid != -1 && setuid(child->uid) == -1) {
110 ha_alert("[%s.main()] Cannot set uid %d.\n", child->command[0], child->gid);
111 exit(1);
112 }
113
Willy Tarreau3c032f22021-07-21 10:17:02 +0200114 /* This one must not be exported, it's internal! */
115 unsetenv("HAPROXY_MWORKER_REEXEC");
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200116 execvp(child->command[0], child->command);
117
118 ha_alert("Cannot execute %s: %s\n", child->command[0], strerror(errno));
119 exit(EXIT_FAILURE);
120 }
121 }
122 }
123
124 return 0;
125
126}
127
128
129/* Configuration */
130
131int cfg_parse_program(const char *file, int linenum, char **args, int kwm)
132{
133 static struct mworker_proc *ext_child = NULL;
134 struct mworker_proc *child;
135 int err_code = 0;
136
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100137 if (strcmp(args[0], "program") == 0) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200138 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
139 err_code |= ERR_ABORT;
140 goto error;
141 }
142
143 if (!*args[1]) {
144 ha_alert("parsing [%s:%d] : '%s' expects an <id> argument\n",
145 file, linenum, args[0]);
146 err_code |= ERR_ALERT | ERR_ABORT;
147 goto error;
148 }
149
150 ext_child = calloc(1, sizeof(*ext_child));
151 if (!ext_child) {
152 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
153 err_code |= ERR_ALERT | ERR_ABORT;
154 goto error;
155 }
156
William Lallemand8f7069a2019-04-12 16:09:23 +0200157 ext_child->options |= PROC_O_TYPE_PROG; /* external process */
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200158 ext_child->command = NULL;
159 ext_child->path = NULL;
160 ext_child->id = NULL;
161 ext_child->pid = -1;
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200162 ext_child->reloads = 0;
163 ext_child->timestamp = -1;
164 ext_child->ipc_fd[0] = -1;
165 ext_child->ipc_fd[1] = -1;
William Lallemandbd3de3e2019-04-12 16:09:22 +0200166 ext_child->options |= PROC_O_START_RELOAD; /* restart the programs by default */
Andrew Heberle97236962019-07-12 11:50:26 +0800167 ext_child->uid = -1;
168 ext_child->gid = -1;
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200169 LIST_INIT(&ext_child->list);
170
171 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200172 if (child->reloads == 0 && (child->options & PROC_O_TYPE_PROG)) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100173 if (strcmp(args[1], child->id) == 0) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200174 ha_alert("parsing [%s:%d]: '%s' program section already exists in the configuration.\n", file, linenum, args[1]);
175 err_code |= ERR_ALERT | ERR_ABORT;
176 goto error;
177 }
178 }
179 }
180
181 ext_child->id = strdup(args[1]);
182 if (!ext_child->id) {
183 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
184 err_code |= ERR_ALERT | ERR_ABORT;
185 goto error;
186 }
187
Willy Tarreau2b718102021-04-21 07:32:39 +0200188 LIST_APPEND(&proc_list, &ext_child->list);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200189
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100190 } else if (strcmp(args[0], "command") == 0) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200191 int arg_nb = 0;
192 int i = 0;
193
194 if (*(args[1]) == 0) {
195 ha_alert("parsing [%s:%d]: '%s' expects a command with optional arguments separated in words.\n", file, linenum, args[0]);
196 err_code |= ERR_ALERT | ERR_FATAL;
197 goto error;
198 }
199
200 while (*args[arg_nb+1])
201 arg_nb++;
202
203 ext_child->command = calloc(arg_nb+1, sizeof(*ext_child->command));
204
205 if (!ext_child->command) {
206 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
207 err_code |= ERR_ALERT | ERR_ABORT;
208 goto error;
209 }
210
211 while (i < arg_nb) {
212 ext_child->command[i] = strdup(args[i+1]);
213 if (!ext_child->command[i]) {
214 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
215 err_code |= ERR_ALERT | ERR_ABORT;
216 goto error;
217 }
218 i++;
219 }
220 ext_child->command[i] = NULL;
221
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100222 } else if (strcmp(args[0], "option") == 0) {
William Lallemandbd3de3e2019-04-12 16:09:22 +0200223
224 if (*(args[1]) == '\0') {
225 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
226 file, linenum, args[0]);
227 err_code |= ERR_ALERT | ERR_FATAL;
228 goto error;
229 }
230
231 if (strcmp(args[1], "start-on-reload") == 0) {
232 if (alertif_too_many_args_idx(0, 1, file, linenum, args, &err_code))
233 goto error;
234 if (kwm == KWM_STD)
235 ext_child->options |= PROC_O_START_RELOAD;
236 else if (kwm == KWM_NO)
237 ext_child->options &= ~PROC_O_START_RELOAD;
238 goto out;
239
240 } else {
241 ha_alert("parsing [%s:%d] : unknown option '%s'.\n", file, linenum, args[1]);
242 err_code |= ERR_ALERT | ERR_FATAL;
243 goto error;
244 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100245 } else if (strcmp(args[0], "user") == 0) {
Andrew Heberle97236962019-07-12 11:50:26 +0800246 struct passwd *ext_child_user;
247 if (*(args[1]) == '\0') {
248 ha_alert("parsing [%s:%d]: '%s' expects a user name.\n",
249 file, linenum, args[0]);
250 err_code |= ERR_ALERT | ERR_FATAL;
251 goto error;
252 }
253
254 if (alertif_too_many_args(1, file, linenum, args, &err_code))
255 goto error;
256
257 if (ext_child->uid != -1) {
258 ha_alert("parsing [%s:%d] : user/uid already specified. Continuing.\n", file, linenum);
259 err_code |= ERR_ALERT;
260 goto out;
261 }
262
263 ext_child_user = getpwnam(args[1]);
264 if (ext_child_user != NULL) {
265 ext_child->uid = (int)ext_child_user->pw_uid;
266 } else {
267 ha_alert("parsing [%s:%d] : cannot find user id for '%s' (%d:%s)\n", file, linenum, args[1], errno, strerror(errno));
268 err_code |= ERR_ALERT | ERR_FATAL;
269 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100270 } else if (strcmp(args[0], "group") == 0) {
Andrew Heberle97236962019-07-12 11:50:26 +0800271 struct group *ext_child_group;
272 if (*(args[1]) == '\0') {
273 ha_alert("parsing [%s:%d]: '%s' expects a group name.\n",
274 file, linenum, args[0]);
275 err_code |= ERR_ALERT | ERR_FATAL;
276 goto error;
277 }
278
279 if (alertif_too_many_args(1, file, linenum, args, &err_code))
280 goto error;
281
282 if (ext_child->gid != -1) {
283 ha_alert("parsing [%s:%d] : group/gid already specified. Continuing.\n", file, linenum);
284 err_code |= ERR_ALERT;
285 goto out;
286 }
287
288 ext_child_group = getgrnam(args[1]);
289 if (ext_child_group != NULL) {
290 ext_child->gid = (int)ext_child_group->gr_gid;
291 } else {
292 ha_alert("parsing [%s:%d] : cannot find group id for '%s' (%d:%s)\n", file, linenum, args[1], errno, strerror(errno));
293 err_code |= ERR_ALERT | ERR_FATAL;
294 }
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200295 } else {
296 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "program");
297 err_code |= ERR_ALERT | ERR_FATAL;
298 goto error;
299 }
300
301 use_program = 1;
302
303 return err_code;
304
305error:
Tim Duesterhus2c9e2742019-06-23 22:10:12 +0200306 if (ext_child) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200307 LIST_DELETE(&ext_child->list);
Tim Duesterhus2c9e2742019-06-23 22:10:12 +0200308 if (ext_child->command) {
309 int i;
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200310
Tim Duesterhus2c9e2742019-06-23 22:10:12 +0200311 for (i = 0; ext_child->command[i]; i++) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100312 ha_free(&ext_child->command[i]);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200313 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100314 ha_free(&ext_child->command);
Tim Duesterhus2c9e2742019-06-23 22:10:12 +0200315 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100316 ha_free(&ext_child->id);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200317 }
318
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100319 ha_free(&ext_child);
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200320
William Lallemandbd3de3e2019-04-12 16:09:22 +0200321out:
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200322 return err_code;
323
324}
325
326int cfg_program_postparser()
327{
328 int err_code = 0;
329 struct mworker_proc *child;
330
331 list_for_each_entry(child, &proc_list, list) {
William Lallemand8f7069a2019-04-12 16:09:23 +0200332 if (child->reloads == 0 && (child->options & PROC_O_TYPE_PROG)) {
William Lallemand9a1ee7a2019-04-01 11:30:02 +0200333 if (child->command == NULL) {
334 ha_alert("The program section '%s' lacks a command to launch.\n", child->id);
335 err_code |= ERR_ALERT | ERR_FATAL;
336 }
337 }
338 }
339
340 if (use_program && !(global.mode & MODE_MWORKER)) {
341 ha_alert("Can't use a 'program' section without master worker mode.\n");
342 err_code |= ERR_ALERT | ERR_FATAL;
343 }
344
345 return err_code;
346}
347
348
349REGISTER_CONFIG_SECTION("program", cfg_parse_program, NULL);
350REGISTER_CONFIG_POSTPARSER("program", cfg_program_postparser);