blob: 28c6ce06d19fed502d9e209381496c9febff9597 [file] [log] [blame]
Willy Tarreau36b9e222018-11-11 15:19:52 +01001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <netdb.h>
5#include <ctype.h>
6#include <pwd.h>
7#include <grp.h>
8#include <errno.h>
9#include <sys/types.h>
10#include <sys/stat.h>
11#include <fcntl.h>
12#include <unistd.h>
13
14#include <common/cfgparse.h>
Eric Salama8eb49c52020-10-02 11:58:19 +020015#include <common/buf.h>
Willy Tarreau36b9e222018-11-11 15:19:52 +010016#include <proto/compression.h>
17
18/*
19 * parse a line in a <global> section. Returns the error code, 0 if OK, or
20 * any combination of :
21 * - ERR_ABORT: must abort ASAP
22 * - ERR_FATAL: we can continue parsing but not start the service
23 * - ERR_WARN: a warning has been emitted
24 * - ERR_ALERT: an alert has been emitted
25 * Only the two first ones can stop processing, the two others are just
26 * indicators.
27 */
28int cfg_parse_global(const char *file, int linenum, char **args, int kwm)
29{
30 int err_code = 0;
31 char *errmsg = NULL;
32
33 if (!strcmp(args[0], "global")) { /* new section */
34 /* no option, nothing special to do */
35 alertif_too_many_args(0, file, linenum, args, &err_code);
36 goto out;
37 }
38 else if (!strcmp(args[0], "daemon")) {
39 if (alertif_too_many_args(0, file, linenum, args, &err_code))
40 goto out;
41 global.mode |= MODE_DAEMON;
42 }
43 else if (!strcmp(args[0], "master-worker")) {
44 if (alertif_too_many_args(1, file, linenum, args, &err_code))
45 goto out;
46 if (*args[1]) {
47 if (!strcmp(args[1], "no-exit-on-failure")) {
48 global.tune.options |= GTUNE_NOEXIT_ONFAILURE;
49 } else {
50 ha_alert("parsing [%s:%d] : '%s' only supports 'no-exit-on-failure' option.\n", file, linenum, args[0]);
51 err_code |= ERR_ALERT | ERR_FATAL;
52 goto out;
53 }
54 }
55 global.mode |= MODE_MWORKER;
56 }
57 else if (!strcmp(args[0], "debug")) {
58 if (alertif_too_many_args(0, file, linenum, args, &err_code))
59 goto out;
60 global.mode |= MODE_DEBUG;
61 }
62 else if (!strcmp(args[0], "noepoll")) {
63 if (alertif_too_many_args(0, file, linenum, args, &err_code))
64 goto out;
65 global.tune.options &= ~GTUNE_USE_EPOLL;
66 }
67 else if (!strcmp(args[0], "nokqueue")) {
68 if (alertif_too_many_args(0, file, linenum, args, &err_code))
69 goto out;
70 global.tune.options &= ~GTUNE_USE_KQUEUE;
71 }
Emmanuel Hocdet0ba4f482019-04-08 16:53:32 +000072 else if (!strcmp(args[0], "noevports")) {
73 if (alertif_too_many_args(0, file, linenum, args, &err_code))
74 goto out;
75 global.tune.options &= ~GTUNE_USE_EVPORTS;
76 }
Willy Tarreau36b9e222018-11-11 15:19:52 +010077 else if (!strcmp(args[0], "nopoll")) {
78 if (alertif_too_many_args(0, file, linenum, args, &err_code))
79 goto out;
80 global.tune.options &= ~GTUNE_USE_POLL;
81 }
Willy Tarreaubeb859a2018-11-22 18:07:59 +010082 else if (!strcmp(args[0], "busy-polling")) { /* "no busy-polling" or "busy-polling" */
83 if (alertif_too_many_args(0, file, linenum, args, &err_code))
84 goto out;
85 if (kwm == KWM_NO)
86 global.tune.options &= ~GTUNE_BUSY_POLLING;
87 else
88 global.tune.options |= GTUNE_BUSY_POLLING;
89 }
Willy Tarreau636848a2019-04-15 19:38:50 +020090 else if (!strcmp(args[0], "set-dumpable")) { /* "no set-dumpable" or "set-dumpable" */
91 if (alertif_too_many_args(0, file, linenum, args, &err_code))
92 goto out;
93 if (kwm == KWM_NO)
94 global.tune.options &= ~GTUNE_SET_DUMPABLE;
95 else
96 global.tune.options |= GTUNE_SET_DUMPABLE;
97 }
Willy Tarreau36b9e222018-11-11 15:19:52 +010098 else if (!strcmp(args[0], "nosplice")) {
99 if (alertif_too_many_args(0, file, linenum, args, &err_code))
100 goto out;
101 global.tune.options &= ~GTUNE_USE_SPLICE;
102 }
103 else if (!strcmp(args[0], "nogetaddrinfo")) {
104 if (alertif_too_many_args(0, file, linenum, args, &err_code))
105 goto out;
106 global.tune.options &= ~GTUNE_USE_GAI;
107 }
108 else if (!strcmp(args[0], "noreuseport")) {
109 if (alertif_too_many_args(0, file, linenum, args, &err_code))
110 goto out;
111 global.tune.options &= ~GTUNE_USE_REUSEPORT;
112 }
113 else if (!strcmp(args[0], "quiet")) {
114 if (alertif_too_many_args(0, file, linenum, args, &err_code))
115 goto out;
116 global.mode |= MODE_QUIET;
117 }
118 else if (!strcmp(args[0], "tune.runqueue-depth")) {
119 if (alertif_too_many_args(1, file, linenum, args, &err_code))
120 goto out;
121 if (global.tune.runqueue_depth != 0) {
122 ha_alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
123 err_code |= ERR_ALERT;
124 goto out;
125 }
126 if (*(args[1]) == 0) {
127 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
128 err_code |= ERR_ALERT | ERR_FATAL;
129 goto out;
130 }
131 global.tune.runqueue_depth = atol(args[1]);
132
133 }
134 else if (!strcmp(args[0], "tune.maxpollevents")) {
135 if (alertif_too_many_args(1, file, linenum, args, &err_code))
136 goto out;
137 if (global.tune.maxpollevents != 0) {
138 ha_alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
139 err_code |= ERR_ALERT;
140 goto out;
141 }
142 if (*(args[1]) == 0) {
143 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
144 err_code |= ERR_ALERT | ERR_FATAL;
145 goto out;
146 }
147 global.tune.maxpollevents = atol(args[1]);
148 }
149 else if (!strcmp(args[0], "tune.maxaccept")) {
Christopher Faulet6b02ab82019-04-30 14:03:56 +0200150 long max;
151
Willy Tarreau36b9e222018-11-11 15:19:52 +0100152 if (alertif_too_many_args(1, file, linenum, args, &err_code))
153 goto out;
154 if (global.tune.maxaccept != 0) {
155 ha_alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
156 err_code |= ERR_ALERT;
157 goto out;
158 }
159 if (*(args[1]) == 0) {
160 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
161 err_code |= ERR_ALERT | ERR_FATAL;
162 goto out;
163 }
Christopher Faulet6b02ab82019-04-30 14:03:56 +0200164 max = atol(args[1]);
165 if (/*max < -1 || */max > INT_MAX) {
166 ha_alert("parsing [%s:%d] : '%s' expects -1 or an integer from 0 to INT_MAX.\n", file, linenum, args[0]);
167 err_code |= ERR_ALERT | ERR_FATAL;
168 goto out;
169 }
170 global.tune.maxaccept = max;
Willy Tarreau36b9e222018-11-11 15:19:52 +0100171 }
172 else if (!strcmp(args[0], "tune.chksize")) {
173 if (alertif_too_many_args(1, file, linenum, args, &err_code))
174 goto out;
175 if (*(args[1]) == 0) {
176 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
177 err_code |= ERR_ALERT | ERR_FATAL;
178 goto out;
179 }
180 global.tune.chksize = atol(args[1]);
181 }
182 else if (!strcmp(args[0], "tune.recv_enough")) {
183 if (alertif_too_many_args(1, file, linenum, args, &err_code))
184 goto out;
185 if (*(args[1]) == 0) {
186 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
187 err_code |= ERR_ALERT | ERR_FATAL;
188 goto out;
189 }
190 global.tune.recv_enough = atol(args[1]);
191 }
192 else if (!strcmp(args[0], "tune.buffers.limit")) {
193 if (alertif_too_many_args(1, file, linenum, args, &err_code))
194 goto out;
195 if (*(args[1]) == 0) {
196 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
197 err_code |= ERR_ALERT | ERR_FATAL;
198 goto out;
199 }
200 global.tune.buf_limit = atol(args[1]);
201 if (global.tune.buf_limit) {
202 if (global.tune.buf_limit < 3)
203 global.tune.buf_limit = 3;
204 if (global.tune.buf_limit <= global.tune.reserved_bufs)
205 global.tune.buf_limit = global.tune.reserved_bufs + 1;
206 }
207 }
208 else if (!strcmp(args[0], "tune.buffers.reserve")) {
209 if (alertif_too_many_args(1, file, linenum, args, &err_code))
210 goto out;
211 if (*(args[1]) == 0) {
212 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
213 err_code |= ERR_ALERT | ERR_FATAL;
214 goto out;
215 }
216 global.tune.reserved_bufs = atol(args[1]);
217 if (global.tune.reserved_bufs < 2)
218 global.tune.reserved_bufs = 2;
219 if (global.tune.buf_limit && global.tune.buf_limit <= global.tune.reserved_bufs)
220 global.tune.buf_limit = global.tune.reserved_bufs + 1;
221 }
222 else if (!strcmp(args[0], "tune.bufsize")) {
223 if (alertif_too_many_args(1, file, linenum, args, &err_code))
224 goto out;
225 if (*(args[1]) == 0) {
226 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
227 err_code |= ERR_ALERT | ERR_FATAL;
228 goto out;
229 }
230 global.tune.bufsize = atol(args[1]);
Willy Tarreauc77d3642018-12-12 06:19:42 +0100231 /* round it up to support a two-pointer alignment at the end */
232 global.tune.bufsize = (global.tune.bufsize + 2 * sizeof(void *) - 1) & -(2 * sizeof(void *));
Willy Tarreau36b9e222018-11-11 15:19:52 +0100233 if (global.tune.bufsize <= 0) {
234 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n", file, linenum, args[0]);
235 err_code |= ERR_ALERT | ERR_FATAL;
236 goto out;
237 }
238 }
239 else if (!strcmp(args[0], "tune.maxrewrite")) {
240 if (alertif_too_many_args(1, file, linenum, args, &err_code))
241 goto out;
242 if (*(args[1]) == 0) {
243 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
244 err_code |= ERR_ALERT | ERR_FATAL;
245 goto out;
246 }
247 global.tune.maxrewrite = atol(args[1]);
248 if (global.tune.maxrewrite < 0) {
249 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n", file, linenum, args[0]);
250 err_code |= ERR_ALERT | ERR_FATAL;
251 goto out;
252 }
253 }
254 else if (!strcmp(args[0], "tune.idletimer")) {
255 unsigned int idle;
256 const char *res;
257
258 if (alertif_too_many_args(1, file, linenum, args, &err_code))
259 goto out;
260 if (*(args[1]) == 0) {
261 ha_alert("parsing [%s:%d] : '%s' expects a timer value between 0 and 65535 ms.\n", file, linenum, args[0]);
262 err_code |= ERR_ALERT | ERR_FATAL;
263 goto out;
264 }
265
266 res = parse_time_err(args[1], &idle, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200267 if (res == PARSE_TIME_OVER) {
268 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s>, maximum value is 65535 ms.\n",
269 file, linenum, args[1], args[0]);
270 err_code |= ERR_ALERT | ERR_FATAL;
271 goto out;
272 }
273 else if (res == PARSE_TIME_UNDER) {
274 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s>, minimum non-null value is 1 ms.\n",
275 file, linenum, args[1], args[0]);
276 err_code |= ERR_ALERT | ERR_FATAL;
277 goto out;
278 }
279 else if (res) {
Willy Tarreau36b9e222018-11-11 15:19:52 +0100280 ha_alert("parsing [%s:%d]: unexpected character '%c' in argument to <%s>.\n",
Willy Tarreau9faebe32019-06-07 19:00:37 +0200281 file, linenum, *res, args[0]);
Willy Tarreau36b9e222018-11-11 15:19:52 +0100282 err_code |= ERR_ALERT | ERR_FATAL;
283 goto out;
284 }
285
286 if (idle > 65535) {
287 ha_alert("parsing [%s:%d] : '%s' expects a timer value between 0 and 65535 ms.\n", file, linenum, args[0]);
288 err_code |= ERR_ALERT | ERR_FATAL;
289 goto out;
290 }
291 global.tune.idle_timer = idle;
292 }
293 else if (!strcmp(args[0], "tune.rcvbuf.client")) {
294 if (alertif_too_many_args(1, file, linenum, args, &err_code))
295 goto out;
296 if (global.tune.client_rcvbuf != 0) {
297 ha_alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
298 err_code |= ERR_ALERT;
299 goto out;
300 }
301 if (*(args[1]) == 0) {
302 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
303 err_code |= ERR_ALERT | ERR_FATAL;
304 goto out;
305 }
306 global.tune.client_rcvbuf = atol(args[1]);
307 }
308 else if (!strcmp(args[0], "tune.rcvbuf.server")) {
309 if (alertif_too_many_args(1, file, linenum, args, &err_code))
310 goto out;
311 if (global.tune.server_rcvbuf != 0) {
312 ha_alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
313 err_code |= ERR_ALERT;
314 goto out;
315 }
316 if (*(args[1]) == 0) {
317 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
318 err_code |= ERR_ALERT | ERR_FATAL;
319 goto out;
320 }
321 global.tune.server_rcvbuf = atol(args[1]);
322 }
323 else if (!strcmp(args[0], "tune.sndbuf.client")) {
324 if (alertif_too_many_args(1, file, linenum, args, &err_code))
325 goto out;
326 if (global.tune.client_sndbuf != 0) {
327 ha_alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
328 err_code |= ERR_ALERT;
329 goto out;
330 }
331 if (*(args[1]) == 0) {
332 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
333 err_code |= ERR_ALERT | ERR_FATAL;
334 goto out;
335 }
336 global.tune.client_sndbuf = atol(args[1]);
337 }
338 else if (!strcmp(args[0], "tune.sndbuf.server")) {
339 if (alertif_too_many_args(1, file, linenum, args, &err_code))
340 goto out;
341 if (global.tune.server_sndbuf != 0) {
342 ha_alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
343 err_code |= ERR_ALERT;
344 goto out;
345 }
346 if (*(args[1]) == 0) {
347 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
348 err_code |= ERR_ALERT | ERR_FATAL;
349 goto out;
350 }
351 global.tune.server_sndbuf = atol(args[1]);
352 }
353 else if (!strcmp(args[0], "tune.pipesize")) {
354 if (alertif_too_many_args(1, file, linenum, args, &err_code))
355 goto out;
356 if (*(args[1]) == 0) {
357 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
358 err_code |= ERR_ALERT | ERR_FATAL;
359 goto out;
360 }
361 global.tune.pipesize = atol(args[1]);
362 }
363 else if (!strcmp(args[0], "tune.http.cookielen")) {
364 if (alertif_too_many_args(1, file, linenum, args, &err_code))
365 goto out;
366 if (*(args[1]) == 0) {
367 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
368 err_code |= ERR_ALERT | ERR_FATAL;
369 goto out;
370 }
371 global.tune.cookie_len = atol(args[1]) + 1;
372 }
373 else if (!strcmp(args[0], "tune.http.logurilen")) {
374 if (alertif_too_many_args(1, file, linenum, args, &err_code))
375 goto out;
376 if (*(args[1]) == 0) {
377 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
378 err_code |= ERR_ALERT | ERR_FATAL;
379 goto out;
380 }
381 global.tune.requri_len = atol(args[1]) + 1;
382 }
383 else if (!strcmp(args[0], "tune.http.maxhdr")) {
384 if (alertif_too_many_args(1, file, linenum, args, &err_code))
385 goto out;
386 if (*(args[1]) == 0) {
387 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
388 err_code |= ERR_ALERT | ERR_FATAL;
389 goto out;
390 }
391 global.tune.max_http_hdr = atoi(args[1]);
392 if (global.tune.max_http_hdr < 1 || global.tune.max_http_hdr > 32767) {
393 ha_alert("parsing [%s:%d] : '%s' expects a numeric value between 1 and 32767\n",
394 file, linenum, args[0]);
395 err_code |= ERR_ALERT | ERR_FATAL;
396 goto out;
397 }
398 }
399 else if (!strcmp(args[0], "tune.comp.maxlevel")) {
400 if (alertif_too_many_args(1, file, linenum, args, &err_code))
401 goto out;
402 if (*args[1]) {
403 global.tune.comp_maxlevel = atoi(args[1]);
404 if (global.tune.comp_maxlevel < 1 || global.tune.comp_maxlevel > 9) {
405 ha_alert("parsing [%s:%d] : '%s' expects a numeric value between 1 and 9\n",
406 file, linenum, args[0]);
407 err_code |= ERR_ALERT | ERR_FATAL;
408 goto out;
409 }
410 } else {
411 ha_alert("parsing [%s:%d] : '%s' expects a numeric value between 1 and 9\n",
412 file, linenum, args[0]);
413 err_code |= ERR_ALERT | ERR_FATAL;
414 goto out;
415 }
416 }
417 else if (!strcmp(args[0], "tune.pattern.cache-size")) {
418 if (*args[1]) {
419 global.tune.pattern_cache = atoi(args[1]);
420 if (global.tune.pattern_cache < 0) {
421 ha_alert("parsing [%s:%d] : '%s' expects a positive numeric value\n",
422 file, linenum, args[0]);
423 err_code |= ERR_ALERT | ERR_FATAL;
424 goto out;
425 }
426 } else {
427 ha_alert("parsing [%s:%d] : '%s' expects a positive numeric value\n",
428 file, linenum, args[0]);
429 err_code |= ERR_ALERT | ERR_FATAL;
430 goto out;
431 }
432 }
433 else if (!strcmp(args[0], "uid")) {
434 if (alertif_too_many_args(1, file, linenum, args, &err_code))
435 goto out;
436 if (global.uid != 0) {
437 ha_alert("parsing [%s:%d] : user/uid already specified. Continuing.\n", file, linenum);
438 err_code |= ERR_ALERT;
439 goto out;
440 }
441 if (*(args[1]) == 0) {
442 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
443 err_code |= ERR_ALERT | ERR_FATAL;
444 goto out;
445 }
446 if (strl2irc(args[1], strlen(args[1]), &global.uid) != 0) {
447 ha_warning("parsing [%s:%d] : uid: string '%s' is not a number.\n | You might want to use the 'user' parameter to use a system user name.\n", file, linenum, args[1]);
448 err_code |= ERR_WARN;
449 goto out;
450 }
451
452 }
453 else if (!strcmp(args[0], "gid")) {
454 if (alertif_too_many_args(1, file, linenum, args, &err_code))
455 goto out;
456 if (global.gid != 0) {
457 ha_alert("parsing [%s:%d] : group/gid already specified. Continuing.\n", file, linenum);
458 err_code |= ERR_ALERT;
459 goto out;
460 }
461 if (*(args[1]) == 0) {
462 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
463 err_code |= ERR_ALERT | ERR_FATAL;
464 goto out;
465 }
466 if (strl2irc(args[1], strlen(args[1]), &global.gid) != 0) {
467 ha_warning("parsing [%s:%d] : gid: string '%s' is not a number.\n | You might want to use the 'group' parameter to use a system group name.\n", file, linenum, args[1]);
468 err_code |= ERR_WARN;
469 goto out;
470 }
471 }
472 else if (!strcmp(args[0], "external-check")) {
473 if (alertif_too_many_args(0, file, linenum, args, &err_code))
474 goto out;
475 global.external_check = 1;
476 }
477 /* user/group name handling */
478 else if (!strcmp(args[0], "user")) {
479 struct passwd *ha_user;
480 if (alertif_too_many_args(1, file, linenum, args, &err_code))
481 goto out;
482 if (global.uid != 0) {
483 ha_alert("parsing [%s:%d] : user/uid already specified. Continuing.\n", file, linenum);
484 err_code |= ERR_ALERT;
485 goto out;
486 }
487 errno = 0;
488 ha_user = getpwnam(args[1]);
489 if (ha_user != NULL) {
490 global.uid = (int)ha_user->pw_uid;
491 }
492 else {
493 ha_alert("parsing [%s:%d] : cannot find user id for '%s' (%d:%s)\n", file, linenum, args[1], errno, strerror(errno));
494 err_code |= ERR_ALERT | ERR_FATAL;
495 }
496 }
497 else if (!strcmp(args[0], "group")) {
498 struct group *ha_group;
499 if (alertif_too_many_args(1, file, linenum, args, &err_code))
500 goto out;
501 if (global.gid != 0) {
502 ha_alert("parsing [%s:%d] : gid/group was already specified. Continuing.\n", file, linenum);
503 err_code |= ERR_ALERT;
504 goto out;
505 }
506 errno = 0;
507 ha_group = getgrnam(args[1]);
508 if (ha_group != NULL) {
509 global.gid = (int)ha_group->gr_gid;
510 }
511 else {
512 ha_alert("parsing [%s:%d] : cannot find group id for '%s' (%d:%s)\n", file, linenum, args[1], errno, strerror(errno));
513 err_code |= ERR_ALERT | ERR_FATAL;
514 }
515 }
516 /* end of user/group name handling*/
517 else if (!strcmp(args[0], "nbproc")) {
518 if (alertif_too_many_args(1, file, linenum, args, &err_code))
519 goto out;
520 if (*(args[1]) == 0) {
521 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
522 err_code |= ERR_ALERT | ERR_FATAL;
523 goto out;
524 }
525 global.nbproc = atol(args[1]);
Willy Tarreaua38a7172019-02-02 17:11:28 +0100526 all_proc_mask = nbits(global.nbproc);
Willy Tarreauff9c9142019-02-07 10:39:36 +0100527 if (global.nbproc < 1 || global.nbproc > MAX_PROCS) {
Willy Tarreau36b9e222018-11-11 15:19:52 +0100528 ha_alert("parsing [%s:%d] : '%s' must be between 1 and %d (was %d).\n",
Willy Tarreauff9c9142019-02-07 10:39:36 +0100529 file, linenum, args[0], MAX_PROCS, global.nbproc);
Willy Tarreau36b9e222018-11-11 15:19:52 +0100530 err_code |= ERR_ALERT | ERR_FATAL;
531 goto out;
532 }
533 }
534 else if (!strcmp(args[0], "nbthread")) {
535 if (alertif_too_many_args(1, file, linenum, args, &err_code))
536 goto out;
537 if (*(args[1]) == 0) {
538 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
539 err_code |= ERR_ALERT | ERR_FATAL;
540 goto out;
541 }
542 global.nbthread = parse_nbthread(args[1], &errmsg);
543 if (!global.nbthread) {
544 ha_alert("parsing [%s:%d] : '%s' %s.\n",
545 file, linenum, args[0], errmsg);
546 err_code |= ERR_ALERT | ERR_FATAL;
547 goto out;
548 }
549 }
550 else if (!strcmp(args[0], "maxconn")) {
551 if (alertif_too_many_args(1, file, linenum, args, &err_code))
552 goto out;
553 if (global.maxconn != 0) {
554 ha_alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
555 err_code |= ERR_ALERT;
556 goto out;
557 }
558 if (*(args[1]) == 0) {
559 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
560 err_code |= ERR_ALERT | ERR_FATAL;
561 goto out;
562 }
563 global.maxconn = atol(args[1]);
564#ifdef SYSTEM_MAXCONN
Willy Tarreauca783d42019-03-13 10:03:07 +0100565 if (global.maxconn > SYSTEM_MAXCONN && cfg_maxconn <= SYSTEM_MAXCONN) {
566 ha_alert("parsing [%s:%d] : maxconn value %d too high for this system.\nLimiting to %d. Please use '-n' to force the value.\n", file, linenum, global.maxconn, SYSTEM_MAXCONN);
567 global.maxconn = SYSTEM_MAXCONN;
Willy Tarreau36b9e222018-11-11 15:19:52 +0100568 err_code |= ERR_ALERT;
569 }
570#endif /* SYSTEM_MAXCONN */
571 }
572 else if (!strcmp(args[0], "ssl-server-verify")) {
573 if (alertif_too_many_args(1, file, linenum, args, &err_code))
574 goto out;
575 if (*(args[1]) == 0) {
576 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
577 err_code |= ERR_ALERT | ERR_FATAL;
578 goto out;
579 }
580 if (strcmp(args[1],"none") == 0)
581 global.ssl_server_verify = SSL_SERVER_VERIFY_NONE;
582 else if (strcmp(args[1],"required") == 0)
583 global.ssl_server_verify = SSL_SERVER_VERIFY_REQUIRED;
584 else {
585 ha_alert("parsing [%s:%d] : '%s' expects 'none' or 'required' as argument.\n", file, linenum, args[0]);
586 err_code |= ERR_ALERT | ERR_FATAL;
587 goto out;
588 }
589 }
590 else if (!strcmp(args[0], "maxconnrate")) {
591 if (alertif_too_many_args(1, file, linenum, args, &err_code))
592 goto out;
593 if (global.cps_lim != 0) {
594 ha_alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
595 err_code |= ERR_ALERT;
596 goto out;
597 }
598 if (*(args[1]) == 0) {
599 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
600 err_code |= ERR_ALERT | ERR_FATAL;
601 goto out;
602 }
603 global.cps_lim = atol(args[1]);
604 }
605 else if (!strcmp(args[0], "maxsessrate")) {
606 if (alertif_too_many_args(1, file, linenum, args, &err_code))
607 goto out;
608 if (global.sps_lim != 0) {
609 ha_alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
610 err_code |= ERR_ALERT;
611 goto out;
612 }
613 if (*(args[1]) == 0) {
614 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
615 err_code |= ERR_ALERT | ERR_FATAL;
616 goto out;
617 }
618 global.sps_lim = atol(args[1]);
619 }
620 else if (!strcmp(args[0], "maxsslrate")) {
621 if (alertif_too_many_args(1, file, linenum, args, &err_code))
622 goto out;
623 if (global.ssl_lim != 0) {
624 ha_alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
625 err_code |= ERR_ALERT;
626 goto out;
627 }
628 if (*(args[1]) == 0) {
629 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
630 err_code |= ERR_ALERT | ERR_FATAL;
631 goto out;
632 }
633 global.ssl_lim = atol(args[1]);
634 }
635 else if (!strcmp(args[0], "maxcomprate")) {
636 if (alertif_too_many_args(1, file, linenum, args, &err_code))
637 goto out;
638 if (*(args[1]) == 0) {
639 ha_alert("parsing [%s:%d] : '%s' expects an integer argument in kb/s.\n", file, linenum, args[0]);
640 err_code |= ERR_ALERT | ERR_FATAL;
641 goto out;
642 }
643 global.comp_rate_lim = atoi(args[1]) * 1024;
644 }
645 else if (!strcmp(args[0], "maxpipes")) {
646 if (alertif_too_many_args(1, file, linenum, args, &err_code))
647 goto out;
648 if (global.maxpipes != 0) {
649 ha_alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
650 err_code |= ERR_ALERT;
651 goto out;
652 }
653 if (*(args[1]) == 0) {
654 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
655 err_code |= ERR_ALERT | ERR_FATAL;
656 goto out;
657 }
658 global.maxpipes = atol(args[1]);
659 }
660 else if (!strcmp(args[0], "maxzlibmem")) {
661 if (alertif_too_many_args(1, file, linenum, args, &err_code))
662 goto out;
663 if (*(args[1]) == 0) {
664 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
665 err_code |= ERR_ALERT | ERR_FATAL;
666 goto out;
667 }
668 global.maxzlibmem = atol(args[1]) * 1024L * 1024L;
669 }
670 else if (!strcmp(args[0], "maxcompcpuusage")) {
671 if (alertif_too_many_args(1, file, linenum, args, &err_code))
672 goto out;
673 if (*(args[1]) == 0) {
674 ha_alert("parsing [%s:%d] : '%s' expects an integer argument between 0 and 100.\n", file, linenum, args[0]);
675 err_code |= ERR_ALERT | ERR_FATAL;
676 goto out;
677 }
678 compress_min_idle = 100 - atoi(args[1]);
679 if (compress_min_idle > 100) {
680 ha_alert("parsing [%s:%d] : '%s' expects an integer argument between 0 and 100.\n", file, linenum, args[0]);
681 err_code |= ERR_ALERT | ERR_FATAL;
682 goto out;
683 }
684 }
685
686 else if (!strcmp(args[0], "ulimit-n")) {
687 if (alertif_too_many_args(1, file, linenum, args, &err_code))
688 goto out;
689 if (global.rlimit_nofile != 0) {
690 ha_alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
691 err_code |= ERR_ALERT;
692 goto out;
693 }
694 if (*(args[1]) == 0) {
695 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]);
696 err_code |= ERR_ALERT | ERR_FATAL;
697 goto out;
698 }
699 global.rlimit_nofile = atol(args[1]);
700 }
701 else if (!strcmp(args[0], "chroot")) {
702 if (alertif_too_many_args(1, file, linenum, args, &err_code))
703 goto out;
704 if (global.chroot != NULL) {
705 ha_alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
706 err_code |= ERR_ALERT;
707 goto out;
708 }
709 if (*(args[1]) == 0) {
710 ha_alert("parsing [%s:%d] : '%s' expects a directory as an argument.\n", file, linenum, args[0]);
711 err_code |= ERR_ALERT | ERR_FATAL;
712 goto out;
713 }
714 global.chroot = strdup(args[1]);
715 }
716 else if (!strcmp(args[0], "description")) {
717 int i, len=0;
718 char *d;
719
720 if (!*args[1]) {
721 ha_alert("parsing [%s:%d]: '%s' expects a string argument.\n",
722 file, linenum, args[0]);
723 err_code |= ERR_ALERT | ERR_FATAL;
724 goto out;
725 }
726
727 for (i = 1; *args[i]; i++)
728 len += strlen(args[i]) + 1;
729
730 if (global.desc)
731 free(global.desc);
732
733 global.desc = d = calloc(1, len);
734
735 d += snprintf(d, global.desc + len - d, "%s", args[1]);
736 for (i = 2; *args[i]; i++)
737 d += snprintf(d, global.desc + len - d, " %s", args[i]);
738 }
739 else if (!strcmp(args[0], "node")) {
740 int i;
741 char c;
742
743 if (alertif_too_many_args(1, file, linenum, args, &err_code))
744 goto out;
745
746 for (i=0; args[1][i]; i++) {
747 c = args[1][i];
748 if (!isupper((unsigned char)c) && !islower((unsigned char)c) &&
749 !isdigit((unsigned char)c) && c != '_' && c != '-' && c != '.')
750 break;
751 }
752
753 if (!i || args[1][i]) {
754 ha_alert("parsing [%s:%d]: '%s' requires valid node name - non-empty string"
755 " with digits(0-9), letters(A-Z, a-z), dot(.), hyphen(-) or underscode(_).\n",
756 file, linenum, args[0]);
757 err_code |= ERR_ALERT | ERR_FATAL;
758 goto out;
759 }
760
761 if (global.node)
762 free(global.node);
763
764 global.node = strdup(args[1]);
765 }
766 else if (!strcmp(args[0], "pidfile")) {
767 if (alertif_too_many_args(1, file, linenum, args, &err_code))
768 goto out;
769 if (global.pidfile != NULL) {
770 ha_alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
771 err_code |= ERR_ALERT;
772 goto out;
773 }
774 if (*(args[1]) == 0) {
775 ha_alert("parsing [%s:%d] : '%s' expects a file name as an argument.\n", file, linenum, args[0]);
776 err_code |= ERR_ALERT | ERR_FATAL;
777 goto out;
778 }
779 global.pidfile = strdup(args[1]);
780 }
781 else if (!strcmp(args[0], "unix-bind")) {
782 int cur_arg = 1;
783 while (*(args[cur_arg])) {
784 if (!strcmp(args[cur_arg], "prefix")) {
785 if (global.unix_bind.prefix != NULL) {
786 ha_alert("parsing [%s:%d] : unix-bind '%s' already specified. Continuing.\n", file, linenum, args[cur_arg]);
787 err_code |= ERR_ALERT;
788 cur_arg += 2;
789 continue;
790 }
791
792 if (*(args[cur_arg+1]) == 0) {
793 ha_alert("parsing [%s:%d] : unix_bind '%s' expects a path as an argument.\n", file, linenum, args[cur_arg]);
794 err_code |= ERR_ALERT | ERR_FATAL;
795 goto out;
796 }
797 global.unix_bind.prefix = strdup(args[cur_arg+1]);
798 cur_arg += 2;
799 continue;
800 }
801
802 if (!strcmp(args[cur_arg], "mode")) {
803
804 global.unix_bind.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
805 cur_arg += 2;
806 continue;
807 }
808
809 if (!strcmp(args[cur_arg], "uid")) {
810
811 global.unix_bind.ux.uid = atol(args[cur_arg + 1 ]);
812 cur_arg += 2;
813 continue;
814 }
815
816 if (!strcmp(args[cur_arg], "gid")) {
817
818 global.unix_bind.ux.gid = atol(args[cur_arg + 1 ]);
819 cur_arg += 2;
820 continue;
821 }
822
823 if (!strcmp(args[cur_arg], "user")) {
824 struct passwd *user;
825
826 user = getpwnam(args[cur_arg + 1]);
827 if (!user) {
828 ha_alert("parsing [%s:%d] : '%s' : '%s' unknown user.\n",
829 file, linenum, args[0], args[cur_arg + 1 ]);
830 err_code |= ERR_ALERT | ERR_FATAL;
831 goto out;
832 }
833
834 global.unix_bind.ux.uid = user->pw_uid;
835 cur_arg += 2;
836 continue;
837 }
838
839 if (!strcmp(args[cur_arg], "group")) {
840 struct group *group;
841
842 group = getgrnam(args[cur_arg + 1]);
843 if (!group) {
844 ha_alert("parsing [%s:%d] : '%s' : '%s' unknown group.\n",
845 file, linenum, args[0], args[cur_arg + 1 ]);
846 err_code |= ERR_ALERT | ERR_FATAL;
847 goto out;
848 }
849
850 global.unix_bind.ux.gid = group->gr_gid;
851 cur_arg += 2;
852 continue;
853 }
854
855 ha_alert("parsing [%s:%d] : '%s' only supports the 'prefix', 'mode', 'uid', 'gid', 'user' and 'group' options.\n",
856 file, linenum, args[0]);
857 err_code |= ERR_ALERT | ERR_FATAL;
858 goto out;
859 }
860 }
861 else if (!strcmp(args[0], "log")) { /* "no log" or "log ..." */
862 if (!parse_logsrv(args, &global.logsrvs, (kwm == KWM_NO), &errmsg)) {
863 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
864 err_code |= ERR_ALERT | ERR_FATAL;
865 goto out;
866 }
867 }
868 else if (!strcmp(args[0], "log-send-hostname")) { /* set the hostname in syslog header */
869 char *name;
870
871 if (global.log_send_hostname != NULL) {
872 ha_alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
873 err_code |= ERR_ALERT;
874 goto out;
875 }
876
877 if (*(args[1]))
878 name = args[1];
879 else
880 name = hostname;
881
882 free(global.log_send_hostname);
883 global.log_send_hostname = strdup(name);
884 }
885 else if (!strcmp(args[0], "server-state-base")) { /* path base where HAProxy can find server state files */
886 if (global.server_state_base != NULL) {
887 ha_alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
888 err_code |= ERR_ALERT;
889 goto out;
890 }
891
892 if (!*(args[1])) {
893 ha_alert("parsing [%s:%d] : '%s' expects one argument: a directory path.\n", file, linenum, args[0]);
894 err_code |= ERR_FATAL;
895 goto out;
896 }
897
898 global.server_state_base = strdup(args[1]);
899 }
900 else if (!strcmp(args[0], "server-state-file")) { /* path to the file where HAProxy can load the server states */
901 if (global.server_state_file != NULL) {
902 ha_alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
903 err_code |= ERR_ALERT;
904 goto out;
905 }
906
907 if (!*(args[1])) {
908 ha_alert("parsing [%s:%d] : '%s' expect one argument: a file path.\n", file, linenum, args[0]);
909 err_code |= ERR_FATAL;
910 goto out;
911 }
912
913 global.server_state_file = strdup(args[1]);
914 }
915 else if (!strcmp(args[0], "log-tag")) { /* tag to report to syslog */
916 if (alertif_too_many_args(1, file, linenum, args, &err_code))
917 goto out;
918 if (*(args[1]) == 0) {
919 ha_alert("parsing [%s:%d] : '%s' expects a tag for use in syslog.\n", file, linenum, args[0]);
920 err_code |= ERR_ALERT | ERR_FATAL;
921 goto out;
922 }
923 chunk_destroy(&global.log_tag);
Eric Salama8eb49c52020-10-02 11:58:19 +0200924 chunk_initlen(&global.log_tag, strdup(args[1]), strlen(args[1]), strlen(args[1]));
925 if (b_orig(&global.log_tag) == NULL) {
926 chunk_destroy(&global.log_tag);
927 ha_alert("parsing [%s:%d]: cannot allocate memory for '%s'.\n", file, linenum, args[0]);
928 err_code |= ERR_ALERT | ERR_FATAL;
929 goto out;
930 }
Willy Tarreau36b9e222018-11-11 15:19:52 +0100931 }
932 else if (!strcmp(args[0], "spread-checks")) { /* random time between checks (0-50) */
933 if (alertif_too_many_args(1, file, linenum, args, &err_code))
934 goto out;
935 if (global.spread_checks != 0) {
936 ha_alert("parsing [%s:%d]: spread-checks already specified. Continuing.\n", file, linenum);
937 err_code |= ERR_ALERT;
938 goto out;
939 }
940 if (*(args[1]) == 0) {
941 ha_alert("parsing [%s:%d]: '%s' expects an integer argument (0..50).\n", file, linenum, args[0]);
942 err_code |= ERR_ALERT | ERR_FATAL;
943 goto out;
944 }
945 global.spread_checks = atol(args[1]);
946 if (global.spread_checks < 0 || global.spread_checks > 50) {
947 ha_alert("parsing [%s:%d]: 'spread-checks' needs a positive value in range 0..50.\n", file, linenum);
948 err_code |= ERR_ALERT | ERR_FATAL;
949 }
950 }
951 else if (!strcmp(args[0], "max-spread-checks")) { /* maximum time between first and last check */
952 const char *err;
953 unsigned int val;
954
955 if (alertif_too_many_args(1, file, linenum, args, &err_code))
956 goto out;
957 if (*(args[1]) == 0) {
958 ha_alert("parsing [%s:%d]: '%s' expects an integer argument (0..50).\n", file, linenum, args[0]);
959 err_code |= ERR_ALERT | ERR_FATAL;
960 goto out;
961 }
962
963 err = parse_time_err(args[1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200964 if (err == PARSE_TIME_OVER) {
965 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s>, maximum value is 2147483647 ms (~24.8 days).\n",
966 file, linenum, args[1], args[0]);
Willy Tarreau36b9e222018-11-11 15:19:52 +0100967 err_code |= ERR_ALERT | ERR_FATAL;
968 }
Willy Tarreau9faebe32019-06-07 19:00:37 +0200969 else if (err == PARSE_TIME_UNDER) {
970 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s>, minimum non-null value is 1 ms.\n",
971 file, linenum, args[1], args[0]);
972 err_code |= ERR_ALERT | ERR_FATAL;
973 }
974 else if (err) {
975 ha_alert("parsing [%s:%d]: unsupported character '%c' in '%s' (wants an integer delay).\n", file, linenum, *err, args[0]);
Willy Tarreau36b9e222018-11-11 15:19:52 +0100976 err_code |= ERR_ALERT | ERR_FATAL;
977 }
Willy Tarreau9faebe32019-06-07 19:00:37 +0200978 global.max_spread_checks = val;
Willy Tarreau36b9e222018-11-11 15:19:52 +0100979 }
980 else if (strcmp(args[0], "cpu-map") == 0) {
981 /* map a process list to a CPU set */
982#ifdef USE_CPU_AFFINITY
983 char *slash;
984 unsigned long proc = 0, thread = 0, cpus;
985 int i, j, n, autoinc;
986
987 if (!*args[1] || !*args[2]) {
988 ha_alert("parsing [%s:%d] : %s expects a process number "
989 " ('all', 'odd', 'even', a number from 1 to %d or a range), "
990 " followed by a list of CPU ranges with numbers from 0 to %d.\n",
Willy Tarreau5799e9c2019-03-05 18:14:03 +0100991 file, linenum, args[0], LONGBITS, LONGBITS - 1);
Willy Tarreau36b9e222018-11-11 15:19:52 +0100992 err_code |= ERR_ALERT | ERR_FATAL;
993 goto out;
994 }
995
996 if ((slash = strchr(args[1], '/')) != NULL)
997 *slash = 0;
998
Willy Tarreau5799e9c2019-03-05 18:14:03 +0100999 /* note: we silently ignore processes over MAX_PROCS and
1000 * threads over MAX_THREADS so as not to make configurations a
1001 * pain to maintain.
1002 */
1003 if (parse_process_number(args[1], &proc, LONGBITS, &autoinc, &errmsg)) {
Willy Tarreau36b9e222018-11-11 15:19:52 +01001004 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
1005 err_code |= ERR_ALERT | ERR_FATAL;
1006 goto out;
1007 }
1008
1009 if (slash) {
Willy Tarreau5799e9c2019-03-05 18:14:03 +01001010 if (parse_process_number(slash+1, &thread, LONGBITS, NULL, &errmsg)) {
Willy Tarreau36b9e222018-11-11 15:19:52 +01001011 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
1012 err_code |= ERR_ALERT | ERR_FATAL;
1013 goto out;
1014 }
1015 *slash = '/';
1016
1017 if (autoinc && atleast2(proc) && atleast2(thread)) {
1018 ha_alert("parsing [%s:%d] : %s : '%s' : unable to automatically bind "
1019 "a process range _AND_ a thread range\n",
1020 file, linenum, args[0], args[1]);
1021 err_code |= ERR_ALERT | ERR_FATAL;
1022 goto out;
1023 }
1024 }
1025
1026 if (parse_cpu_set((const char **)args+2, &cpus, &errmsg)) {
1027 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
1028 err_code |= ERR_ALERT | ERR_FATAL;
1029 goto out;
1030 }
1031
1032 if (autoinc &&
1033 my_popcountl(proc) != my_popcountl(cpus) &&
1034 my_popcountl(thread) != my_popcountl(cpus)) {
1035 ha_alert("parsing [%s:%d] : %s : PROC/THREAD range and CPU sets "
1036 "must have the same size to be automatically bound\n",
1037 file, linenum, args[0]);
1038 err_code |= ERR_ALERT | ERR_FATAL;
1039 goto out;
1040 }
1041
Willy Tarreau47b00ae2019-07-16 15:10:34 +02001042 /* we now have to deal with 3 real cases :
1043 * cpu-map P-Q => mapping for whole processes, numbers P to Q
1044 * cpu-map P-Q/1 => mapping of first thread of processes P to Q
1045 * cpu-map 1/T-U => mapping of threads T to U of process 1
1046 * Otherwise other combinations are silently ignored since nbthread
1047 * and nbproc cannot both be >1 :
1048 * cpu-map P-Q/T => mapping for thread T for processes P to Q.
1049 * Only one of T,Q may be > 1, others ignored.
1050 * cpu-map P/T-U => mapping for threads T to U of process P. Only
1051 * one of P,U may be > 1, others ignored.
1052 */
1053 if (!thread) {
1054 /* mapping for whole processes. E.g. cpu-map 1-4 0-3 */
Willy Tarreau81492c92019-05-03 09:41:23 +02001055 for (i = n = 0; i < MAX_PROCS; i++) {
1056 /* No mapping for this process */
1057 if (!(proc & (1UL << i)))
1058 continue;
1059
Willy Tarreau36b9e222018-11-11 15:19:52 +01001060 if (!autoinc)
1061 global.cpu_map.proc[i] = cpus;
1062 else {
1063 n += my_ffsl(cpus >> n);
1064 global.cpu_map.proc[i] = (1UL << (n-1));
1065 }
Willy Tarreau36b9e222018-11-11 15:19:52 +01001066 }
Willy Tarreau47b00ae2019-07-16 15:10:34 +02001067 } else {
1068 /* Mapping at the thread level. All threads are retained
1069 * for process 1, and only thread 1 is retained for other
1070 * processes.
1071 */
1072 if (thread == 0x1) {
1073 /* first thread, iterate on processes. E.g. cpu-map 1-4/1 0-3 */
1074 for (i = n = 0; i < MAX_PROCS; i++) {
1075 /* No mapping for this process */
1076 if (!(proc & (1UL << i)))
1077 continue;
1078 if (!autoinc)
1079 global.cpu_map.proc_t1[i] = cpus;
1080 else {
1081 n += my_ffsl(cpus >> n);
1082 global.cpu_map.proc_t1[i] = (1UL << (n-1));
1083 }
1084 }
1085 }
Willy Tarreau36b9e222018-11-11 15:19:52 +01001086
Willy Tarreau47b00ae2019-07-16 15:10:34 +02001087 if (proc == 0x1) {
1088 /* first process, iterate on threads. E.g. cpu-map 1/1-4 0-3 */
1089 for (j = n = 0; j < MAX_THREADS; j++) {
1090 /* No mapping for this thread */
1091 if (!(thread & (1UL << j)))
1092 continue;
Willy Tarreau36b9e222018-11-11 15:19:52 +01001093
Willy Tarreau47b00ae2019-07-16 15:10:34 +02001094 if (!autoinc)
1095 global.cpu_map.thread[j] = cpus;
1096 else {
1097 n += my_ffsl(cpus >> n);
1098 global.cpu_map.thread[j] = (1UL << (n-1));
1099 }
Willy Tarreau36b9e222018-11-11 15:19:52 +01001100 }
1101 }
1102 }
1103#else
1104 ha_alert("parsing [%s:%d] : '%s' is not enabled, please check build options for USE_CPU_AFFINITY.\n",
1105 file, linenum, args[0]);
1106 err_code |= ERR_ALERT | ERR_FATAL;
1107 goto out;
1108#endif /* ! USE_CPU_AFFINITY */
1109 }
1110 else if (strcmp(args[0], "setenv") == 0 || strcmp(args[0], "presetenv") == 0) {
1111 if (alertif_too_many_args(3, file, linenum, args, &err_code))
1112 goto out;
1113
1114 if (*(args[2]) == 0) {
1115 ha_alert("parsing [%s:%d]: '%s' expects a name and a value.\n", file, linenum, args[0]);
1116 err_code |= ERR_ALERT | ERR_FATAL;
1117 goto out;
1118 }
1119
1120 /* "setenv" overwrites, "presetenv" only sets if not yet set */
1121 if (setenv(args[1], args[2], (args[0][0] == 's')) != 0) {
1122 ha_alert("parsing [%s:%d]: '%s' failed on variable '%s' : %s.\n", file, linenum, args[0], args[1], strerror(errno));
1123 err_code |= ERR_ALERT | ERR_FATAL;
1124 goto out;
1125 }
1126 }
1127 else if (!strcmp(args[0], "unsetenv")) {
1128 int arg;
1129
1130 if (*(args[1]) == 0) {
1131 ha_alert("parsing [%s:%d]: '%s' expects at least one variable name.\n", file, linenum, args[0]);
1132 err_code |= ERR_ALERT | ERR_FATAL;
1133 goto out;
1134 }
1135
1136 for (arg = 1; *args[arg]; arg++) {
1137 if (unsetenv(args[arg]) != 0) {
1138 ha_alert("parsing [%s:%d]: '%s' failed on variable '%s' : %s.\n", file, linenum, args[0], args[arg], strerror(errno));
1139 err_code |= ERR_ALERT | ERR_FATAL;
1140 goto out;
1141 }
1142 }
1143 }
1144 else if (!strcmp(args[0], "resetenv")) {
1145 extern char **environ;
1146 char **env = environ;
1147
1148 /* args contain variable names to keep, one per argument */
1149 while (*env) {
1150 int arg;
1151
1152 /* look for current variable in among all those we want to keep */
1153 for (arg = 1; *args[arg]; arg++) {
1154 if (strncmp(*env, args[arg], strlen(args[arg])) == 0 &&
1155 (*env)[strlen(args[arg])] == '=')
1156 break;
1157 }
1158
1159 /* delete this variable */
1160 if (!*args[arg]) {
1161 char *delim = strchr(*env, '=');
1162
1163 if (!delim || delim - *env >= trash.size) {
1164 ha_alert("parsing [%s:%d]: '%s' failed to unset invalid variable '%s'.\n", file, linenum, args[0], *env);
1165 err_code |= ERR_ALERT | ERR_FATAL;
1166 goto out;
1167 }
1168
1169 memcpy(trash.area, *env, delim - *env);
1170 trash.area[delim - *env] = 0;
1171
1172 if (unsetenv(trash.area) != 0) {
1173 ha_alert("parsing [%s:%d]: '%s' failed to unset variable '%s' : %s.\n", file, linenum, args[0], *env, strerror(errno));
1174 err_code |= ERR_ALERT | ERR_FATAL;
1175 goto out;
1176 }
1177 }
1178 else
1179 env++;
1180 }
1181 }
1182 else {
1183 struct cfg_kw_list *kwl;
1184 int index;
1185 int rc;
1186
1187 list_for_each_entry(kwl, &cfg_keywords.list, list) {
1188 for (index = 0; kwl->kw[index].kw != NULL; index++) {
1189 if (kwl->kw[index].section != CFG_GLOBAL)
1190 continue;
1191 if (strcmp(kwl->kw[index].kw, args[0]) == 0) {
1192 rc = kwl->kw[index].parse(args, CFG_GLOBAL, NULL, NULL, file, linenum, &errmsg);
1193 if (rc < 0) {
1194 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
1195 err_code |= ERR_ALERT | ERR_FATAL;
1196 }
1197 else if (rc > 0) {
1198 ha_warning("parsing [%s:%d] : %s\n", file, linenum, errmsg);
1199 err_code |= ERR_WARN;
1200 goto out;
1201 }
1202 goto out;
1203 }
1204 }
1205 }
1206
1207 ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "global");
1208 err_code |= ERR_ALERT | ERR_FATAL;
1209 }
1210
1211 out:
1212 free(errmsg);
1213 return err_code;
1214}
1215