xref: /PHP-8.2/sapi/fpm/fpm/fpm_conf.c (revision 43bc53a7)
1 	/* (c) 2007,2008 Andrei Nigmatulin */
2 
3 #include "fpm_config.h"
4 
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <stddef.h>
11 #include <string.h>
12 #include <inttypes.h>
13 #ifdef HAVE_GLOB
14 # include <glob.h>
15 #endif
16 
17 #include <stdio.h>
18 #include <unistd.h>
19 
20 #include "php.h"
21 #include "zend_ini_scanner.h"
22 #include "zend_globals.h"
23 #include "zend_stream.h"
24 #include "php_syslog.h"
25 
26 #include "fpm.h"
27 #include "fpm_conf.h"
28 #include "fpm_stdio.h"
29 #include "fpm_worker_pool.h"
30 #include "fpm_cleanup.h"
31 #include "fpm_php.h"
32 #include "fpm_sockets.h"
33 #include "fpm_shm.h"
34 #include "fpm_status.h"
35 #include "fpm_log.h"
36 #include "fpm_events.h"
37 #include "fpm_unix.h"
38 #include "zlog.h"
39 #ifdef HAVE_SYSTEMD
40 #include "fpm_systemd.h"
41 #endif
42 
43 
44 #define STR2STR(a) (a ? a : "undefined")
45 #define BOOL2STR(a) (a ? "yes" : "no")
46 #define GO(field) offsetof(struct fpm_global_config_s, field)
47 #define WPO(field) offsetof(struct fpm_worker_pool_config_s, field)
48 
49 static int fpm_conf_load_ini_file(char *filename);
50 static char *fpm_conf_set_integer(zval *value, void **config, intptr_t offset);
51 #if 0 /* not used for now */
52 static char *fpm_conf_set_long(zval *value, void **config, intptr_t offset);
53 #endif
54 static char *fpm_conf_set_time(zval *value, void **config, intptr_t offset);
55 static char *fpm_conf_set_boolean(zval *value, void **config, intptr_t offset);
56 static char *fpm_conf_set_string(zval *value, void **config, intptr_t offset);
57 static char *fpm_conf_set_log_level(zval *value, void **config, intptr_t offset);
58 static char *fpm_conf_set_rlimit_core(zval *value, void **config, intptr_t offset);
59 static char *fpm_conf_set_pm(zval *value, void **config, intptr_t offset);
60 #ifdef HAVE_SYSLOG_H
61 static char *fpm_conf_set_syslog_facility(zval *value, void **config, intptr_t offset);
62 #endif
63 
64 struct fpm_global_config_s fpm_global_config = {
65 	.daemonize = 1,
66 #ifdef HAVE_SYSLOG_H
67 	.syslog_facility = -1,
68 #endif
69 	.process_max = 0,
70 	.process_priority = 64, /* 64 means unset */
71 #ifdef HAVE_SYSTEMD
72 	.systemd_watchdog = 0,
73 	.systemd_interval = -1, /* -1 means not set */
74 #endif
75 	.log_buffering = ZLOG_DEFAULT_BUFFERING,
76 	.log_limit = ZLOG_DEFAULT_LIMIT
77 };
78 static struct fpm_worker_pool_s *current_wp = NULL;
79 static int ini_recursion = 0;
80 static char *ini_filename = NULL;
81 static int ini_lineno = 0;
82 static char *ini_include = NULL;
83 
84 /*
85  * Please keep the same order as in fpm_conf.h and in php-fpm.conf.in
86  */
87 static struct ini_value_parser_s ini_fpm_global_options[] = {
88 	{ "pid",                         &fpm_conf_set_string,          GO(pid_file) },
89 	{ "error_log",                   &fpm_conf_set_string,          GO(error_log) },
90 #ifdef HAVE_SYSLOG_H
91 	{ "syslog.ident",                &fpm_conf_set_string,          GO(syslog_ident) },
92 	{ "syslog.facility",             &fpm_conf_set_syslog_facility, GO(syslog_facility) },
93 #endif
94 	{ "log_buffering",               &fpm_conf_set_boolean,         GO(log_buffering) },
95 	{ "log_level",                   &fpm_conf_set_log_level,       GO(log_level) },
96 	{ "log_limit",                   &fpm_conf_set_integer,         GO(log_limit) },
97 	{ "emergency_restart_threshold", &fpm_conf_set_integer,         GO(emergency_restart_threshold) },
98 	{ "emergency_restart_interval",  &fpm_conf_set_time,            GO(emergency_restart_interval) },
99 	{ "process_control_timeout",     &fpm_conf_set_time,            GO(process_control_timeout) },
100 	{ "process.max",                 &fpm_conf_set_integer,         GO(process_max) },
101 	{ "process.priority",            &fpm_conf_set_integer,         GO(process_priority) },
102 	{ "daemonize",                   &fpm_conf_set_boolean,         GO(daemonize) },
103 	{ "rlimit_files",                &fpm_conf_set_integer,         GO(rlimit_files) },
104 	{ "rlimit_core",                 &fpm_conf_set_rlimit_core,     GO(rlimit_core) },
105 	{ "events.mechanism",            &fpm_conf_set_string,          GO(events_mechanism) },
106 #ifdef HAVE_SYSTEMD
107 	{ "systemd_interval",            &fpm_conf_set_time,            GO(systemd_interval) },
108 #endif
109 	{ 0, 0, 0 }
110 };
111 
112 /*
113  * Please keep the same order as in fpm_conf.h and in php-fpm.conf.in
114  */
115 static struct ini_value_parser_s ini_fpm_pool_options[] = {
116 	{ "prefix",                    &fpm_conf_set_string,      WPO(prefix) },
117 	{ "user",                      &fpm_conf_set_string,      WPO(user) },
118 	{ "group",                     &fpm_conf_set_string,      WPO(group) },
119 	{ "listen",                    &fpm_conf_set_string,      WPO(listen_address) },
120 	{ "listen.backlog",            &fpm_conf_set_integer,     WPO(listen_backlog) },
121 #ifdef HAVE_FPM_ACL
122 	{ "listen.acl_users",          &fpm_conf_set_string,      WPO(listen_acl_users) },
123 	{ "listen.acl_groups",         &fpm_conf_set_string,      WPO(listen_acl_groups) },
124 #endif
125 	{ "listen.owner",              &fpm_conf_set_string,      WPO(listen_owner) },
126 	{ "listen.group",              &fpm_conf_set_string,      WPO(listen_group) },
127 	{ "listen.mode",               &fpm_conf_set_string,      WPO(listen_mode) },
128 	{ "listen.allowed_clients",    &fpm_conf_set_string,      WPO(listen_allowed_clients) },
129 #ifdef SO_SETFIB
130 	{ "listen.setfib",             &fpm_conf_set_integer,     WPO(listen_setfib) },
131 #endif
132 	{ "process.priority",          &fpm_conf_set_integer,     WPO(process_priority) },
133 	{ "process.dumpable",          &fpm_conf_set_boolean,     WPO(process_dumpable) },
134 	{ "pm",                        &fpm_conf_set_pm,          WPO(pm) },
135 	{ "pm.max_children",           &fpm_conf_set_integer,     WPO(pm_max_children) },
136 	{ "pm.start_servers",          &fpm_conf_set_integer,     WPO(pm_start_servers) },
137 	{ "pm.min_spare_servers",      &fpm_conf_set_integer,     WPO(pm_min_spare_servers) },
138 	{ "pm.max_spare_servers",      &fpm_conf_set_integer,     WPO(pm_max_spare_servers) },
139 	{ "pm.max_spawn_rate",         &fpm_conf_set_integer,     WPO(pm_max_spawn_rate) },
140 	{ "pm.process_idle_timeout",   &fpm_conf_set_time,        WPO(pm_process_idle_timeout) },
141 	{ "pm.max_requests",           &fpm_conf_set_integer,     WPO(pm_max_requests) },
142 	{ "pm.status_path",            &fpm_conf_set_string,      WPO(pm_status_path) },
143 	{ "pm.status_listen",          &fpm_conf_set_string,      WPO(pm_status_listen) },
144 	{ "ping.path",                 &fpm_conf_set_string,      WPO(ping_path) },
145 	{ "ping.response",             &fpm_conf_set_string,      WPO(ping_response) },
146 	{ "access.log",                &fpm_conf_set_string,      WPO(access_log) },
147 	{ "access.format",             &fpm_conf_set_string,      WPO(access_format) },
148 	{ "slowlog",                   &fpm_conf_set_string,      WPO(slowlog) },
149 	{ "request_slowlog_timeout",   &fpm_conf_set_time,        WPO(request_slowlog_timeout) },
150 	{ "request_slowlog_trace_depth", &fpm_conf_set_integer,     WPO(request_slowlog_trace_depth) },
151 	{ "request_terminate_timeout", &fpm_conf_set_time,        WPO(request_terminate_timeout) },
152 	{ "request_terminate_timeout_track_finished", &fpm_conf_set_boolean, WPO(request_terminate_timeout_track_finished) },
153 	{ "rlimit_files",              &fpm_conf_set_integer,     WPO(rlimit_files) },
154 	{ "rlimit_core",               &fpm_conf_set_rlimit_core, WPO(rlimit_core) },
155 	{ "chroot",                    &fpm_conf_set_string,      WPO(chroot) },
156 	{ "chdir",                     &fpm_conf_set_string,      WPO(chdir) },
157 	{ "catch_workers_output",      &fpm_conf_set_boolean,     WPO(catch_workers_output) },
158 	{ "decorate_workers_output",   &fpm_conf_set_boolean,     WPO(decorate_workers_output) },
159 	{ "clear_env",                 &fpm_conf_set_boolean,     WPO(clear_env) },
160 	{ "security.limit_extensions", &fpm_conf_set_string,      WPO(security_limit_extensions) },
161 #ifdef HAVE_APPARMOR
162 	{ "apparmor_hat",              &fpm_conf_set_string,      WPO(apparmor_hat) },
163 #endif
164 	{ 0, 0, 0 }
165 };
166 
fpm_conf_is_dir(char * path)167 static int fpm_conf_is_dir(char *path) /* {{{ */
168 {
169 	struct stat sb;
170 
171 	if (stat(path, &sb) != 0) {
172 		return 0;
173 	}
174 
175 	return (sb.st_mode & S_IFMT) == S_IFDIR;
176 }
177 /* }}} */
178 
179 /*
180  * Expands the '$pool' token in a dynamically allocated string
181  */
fpm_conf_expand_pool_name(char ** value)182 static int fpm_conf_expand_pool_name(char **value) {
183 	char *token;
184 
185 	if (!value || !*value) {
186 		return 0;
187 	}
188 
189 	while (*value && (token = strstr(*value, "$pool"))) {
190 		char *buf;
191 		char *p2 = token + strlen("$pool");
192 
193 		/* If we are not in a pool, we cannot expand this name now */
194 		if (!current_wp || !current_wp->config  || !current_wp->config->name) {
195 			return -1;
196 		}
197 
198 		/* "aaa$poolbbb" becomes "aaa\0oolbbb" */
199 		token[0] = '\0';
200 
201 		/* Build a brand new string with the expanded token */
202 		spprintf(&buf, 0, "%s%s%s", *value, current_wp->config->name, p2);
203 
204 		/* Free the previous value and save the new one */
205 		free(*value);
206 		*value = strdup(buf);
207 		efree(buf);
208 	}
209 
210 	return 0;
211 }
212 
fpm_conf_set_boolean(zval * value,void ** config,intptr_t offset)213 static char *fpm_conf_set_boolean(zval *value, void **config, intptr_t offset) /* {{{ */
214 {
215 	zend_string *val = Z_STR_P(value);
216 	/* we need to check all allowed values to correctly set value from the environment variable */
217 	bool value_y = (
218 		zend_string_equals_literal(val, "1") ||
219 		zend_string_equals_literal(val, "yes") ||
220 		zend_string_equals_literal(val, "true") ||
221 		zend_string_equals_literal(val, "on")
222 	);
223 	bool value_n = (
224 		value_y || ZSTR_LEN(val) == 0 ||
225 		zend_string_equals_literal(val, "0") ||
226 		zend_string_equals_literal(val, "no") ||
227 		zend_string_equals_literal(val, "none") ||
228 		zend_string_equals_literal(val, "false") ||
229 		zend_string_equals_literal(val, "off")
230 	);
231 
232 
233 	if (!value_y && !value_n) {
234 		return "invalid boolean value";
235 	}
236 
237 	* (int *) ((char *) *config + offset) = value_y ? 1 : 0;
238 	return NULL;
239 }
240 /* }}} */
241 
fpm_conf_set_string(zval * value,void ** config,intptr_t offset)242 static char *fpm_conf_set_string(zval *value, void **config, intptr_t offset) /* {{{ */
243 {
244 	char **config_val = (char **) ((char *) *config + offset);
245 
246 	if (!config_val) {
247 		return "internal error: NULL value";
248 	}
249 
250 	/* Check if there is a previous value to deallocate */
251 	if (*config_val) {
252 		free(*config_val);
253 	}
254 
255 	*config_val = strdup(Z_STRVAL_P(value));
256 	if (!*config_val) {
257 		return "fpm_conf_set_string(): strdup() failed";
258 	}
259 	if (fpm_conf_expand_pool_name(config_val) == -1) {
260 		return "Can't use '$pool' when the pool is not defined";
261 	}
262 
263 	return NULL;
264 }
265 /* }}} */
266 
fpm_conf_set_integer(zval * value,void ** config,intptr_t offset)267 static char *fpm_conf_set_integer(zval *value, void **config, intptr_t offset) /* {{{ */
268 {
269 	char *val = Z_STRVAL_P(value);
270 	char *p;
271 
272 	/* we don't use strtol because we don't want to allow negative values */
273 	for (p = val; *p; p++) {
274 		if (p == val && *p == '-') continue;
275 		if (*p < '0' || *p > '9') {
276 			return "is not a valid number (greater or equal than zero)";
277 		}
278 	}
279 	* (int *) ((char *) *config + offset) = atoi(val);
280 	return NULL;
281 }
282 /* }}} */
283 
284 #if 0 /* not used for now */
285 static char *fpm_conf_set_long(zval *value, void **config, intptr_t offset) /* {{{ */
286 {
287 	char *val = Z_STRVAL_P(value);
288 	char *p;
289 
290 	for (p = val; *p; p++) {
291 		if ( p == val && *p == '-' ) continue;
292 		if (*p < '0' || *p > '9') {
293 			return "is not a valid number (greater or equal than zero)";
294 		}
295 	}
296 	* (long int *) ((char *) *config + offset) = atol(val);
297 	return NULL;
298 }
299 /* }}} */
300 #endif
301 
fpm_conf_set_time(zval * value,void ** config,intptr_t offset)302 static char *fpm_conf_set_time(zval *value, void **config, intptr_t offset) /* {{{ */
303 {
304 	char *val = Z_STRVAL_P(value);
305 	int len = strlen(val);
306 	char suffix;
307 	int seconds;
308 	if (!len) {
309 		return "invalid time value";
310 	}
311 
312 	suffix = val[len-1];
313 	switch (suffix) {
314 		case 'm' :
315 			val[len-1] = '\0';
316 			seconds = 60 * atoi(val);
317 			break;
318 		case 'h' :
319 			val[len-1] = '\0';
320 			seconds = 60 * 60 * atoi(val);
321 			break;
322 		case 'd' :
323 			val[len-1] = '\0';
324 			seconds = 24 * 60 * 60 * atoi(val);
325 			break;
326 		case 's' : /* s is the default suffix */
327 			val[len-1] = '\0';
328 			suffix = '0';
329 			ZEND_FALLTHROUGH;
330 		default :
331 			if (suffix < '0' || suffix > '9') {
332 				return "unknown suffix used in time value";
333 			}
334 			seconds = atoi(val);
335 			break;
336 	}
337 
338 	* (int *) ((char *) *config + offset) = seconds;
339 	return NULL;
340 }
341 /* }}} */
342 
fpm_conf_set_log_level(zval * value,void ** config,intptr_t offset)343 static char *fpm_conf_set_log_level(zval *value, void **config, intptr_t offset) /* {{{ */
344 {
345 	zend_string *val = Z_STR_P(value);
346 	int log_level;
347 
348 	if (zend_string_equals_literal_ci(val, "debug")) {
349 		log_level = ZLOG_DEBUG;
350 	} else if (zend_string_equals_literal_ci(val, "notice")) {
351 		log_level = ZLOG_NOTICE;
352 	} else if (zend_string_equals_literal_ci(val, "warning") || zend_string_equals_literal_ci(val, "warn")) {
353 		log_level = ZLOG_WARNING;
354 	} else if (zend_string_equals_literal_ci(val, "error")) {
355 		log_level = ZLOG_ERROR;
356 	} else if (zend_string_equals_literal_ci(val, "alert")) {
357 		log_level = ZLOG_ALERT;
358 	} else {
359 		return "invalid value for 'log_level'";
360 	}
361 
362 	* (int *) ((char *) *config + offset) = log_level;
363 	return NULL;
364 }
365 /* }}} */
366 
367 #ifdef HAVE_SYSLOG_H
fpm_conf_set_syslog_facility(zval * value,void ** config,intptr_t offset)368 static char *fpm_conf_set_syslog_facility(zval *value, void **config, intptr_t offset) /* {{{ */
369 {
370 	zend_string *val = Z_STR_P(value);
371 	int *conf = (int *) ((char *) *config + offset);
372 
373 #ifdef LOG_AUTH
374 	if (zend_string_equals_literal_ci(val, "AUTH")) {
375 		*conf = LOG_AUTH;
376 		return NULL;
377 	}
378 #endif
379 
380 #ifdef LOG_AUTHPRIV
381 	if (zend_string_equals_literal_ci(val, "AUTHPRIV")) {
382 		*conf = LOG_AUTHPRIV;
383 		return NULL;
384 	}
385 #endif
386 
387 #ifdef LOG_CRON
388 	if (zend_string_equals_literal_ci(val, "CRON")) {
389 		*conf = LOG_CRON;
390 		return NULL;
391 	}
392 #endif
393 
394 #ifdef LOG_DAEMON
395 	if (zend_string_equals_literal_ci(val, "DAEMON")) {
396 		*conf = LOG_DAEMON;
397 		return NULL;
398 	}
399 #endif
400 
401 #ifdef LOG_FTP
402 	if (zend_string_equals_literal_ci(val, "FTP")) {
403 		*conf = LOG_FTP;
404 		return NULL;
405 	}
406 #endif
407 
408 #ifdef LOG_KERN
409 	if (zend_string_equals_literal_ci(val, "KERN")) {
410 		*conf = LOG_KERN;
411 		return NULL;
412 	}
413 #endif
414 
415 #ifdef LOG_LPR
416 	if (zend_string_equals_literal_ci(val, "LPR")) {
417 		*conf = LOG_LPR;
418 		return NULL;
419 	}
420 #endif
421 
422 #ifdef LOG_MAIL
423 	if (zend_string_equals_literal_ci(val, "MAIL")) {
424 		*conf = LOG_MAIL;
425 		return NULL;
426 	}
427 #endif
428 
429 #ifdef LOG_NEWS
430 	if (zend_string_equals_literal_ci(val, "NEWS")) {
431 		*conf = LOG_NEWS;
432 		return NULL;
433 	}
434 #endif
435 
436 #ifdef LOG_SYSLOG
437 	if (zend_string_equals_literal_ci(val, "SYSLOG")) {
438 		*conf = LOG_SYSLOG;
439 		return NULL;
440 	}
441 #endif
442 
443 #ifdef LOG_USER
444 	if (zend_string_equals_literal_ci(val, "USER")) {
445 		*conf = LOG_USER;
446 		return NULL;
447 	}
448 #endif
449 
450 #ifdef LOG_UUCP
451 	if (zend_string_equals_literal_ci(val, "UUCP")) {
452 		*conf = LOG_UUCP;
453 		return NULL;
454 	}
455 #endif
456 
457 #ifdef LOG_LOCAL0
458 	if (zend_string_equals_literal_ci(val, "LOCAL0")) {
459 		*conf = LOG_LOCAL0;
460 		return NULL;
461 	}
462 #endif
463 
464 #ifdef LOG_LOCAL1
465 	if (zend_string_equals_literal_ci(val, "LOCAL1")) {
466 		*conf = LOG_LOCAL1;
467 		return NULL;
468 	}
469 #endif
470 
471 #ifdef LOG_LOCAL2
472 	if (zend_string_equals_literal_ci(val, "LOCAL2")) {
473 		*conf = LOG_LOCAL2;
474 		return NULL;
475 	}
476 #endif
477 
478 #ifdef LOG_LOCAL3
479 	if (zend_string_equals_literal_ci(val, "LOCAL3")) {
480 		*conf = LOG_LOCAL3;
481 		return NULL;
482 	}
483 #endif
484 
485 #ifdef LOG_LOCAL4
486 	if (zend_string_equals_literal_ci(val, "LOCAL4")) {
487 		*conf = LOG_LOCAL4;
488 		return NULL;
489 	}
490 #endif
491 
492 #ifdef LOG_LOCAL5
493 	if (zend_string_equals_literal_ci(val, "LOCAL5")) {
494 		*conf = LOG_LOCAL5;
495 		return NULL;
496 	}
497 #endif
498 
499 #ifdef LOG_LOCAL6
500 	if (zend_string_equals_literal_ci(val, "LOCAL6")) {
501 		*conf = LOG_LOCAL6;
502 		return NULL;
503 	}
504 #endif
505 
506 #ifdef LOG_LOCAL7
507 	if (zend_string_equals_literal_ci(val, "LOCAL7")) {
508 		*conf = LOG_LOCAL7;
509 		return NULL;
510 	}
511 #endif
512 
513 	return "invalid value";
514 }
515 /* }}} */
516 #endif
517 
fpm_conf_set_rlimit_core(zval * value,void ** config,intptr_t offset)518 static char *fpm_conf_set_rlimit_core(zval *value, void **config, intptr_t offset) /* {{{ */
519 {
520 	zend_string *val = Z_STR_P(value);
521 	int *ptr = (int *) ((char *) *config + offset);
522 
523 	if (zend_string_equals_literal_ci(val, "unlimited")) {
524 		*ptr = -1;
525 	} else {
526 		int int_value;
527 		void *subconf = &int_value;
528 		char *error;
529 
530 		error = fpm_conf_set_integer(value, &subconf, 0);
531 
532 		if (error) {
533 			return error;
534 		}
535 
536 		if (int_value < 0) {
537 			return "must be greater than zero or 'unlimited'";
538 		}
539 
540 		*ptr = int_value;
541 	}
542 
543 	return NULL;
544 }
545 /* }}} */
546 
fpm_conf_set_pm(zval * value,void ** config,intptr_t offset)547 static char *fpm_conf_set_pm(zval *value, void **config, intptr_t offset) /* {{{ */
548 {
549 	zend_string *val = Z_STR_P(value);
550 	struct fpm_worker_pool_config_s  *c = *config;
551 	if (zend_string_equals_literal_ci(val, "static")) {
552 		c->pm = PM_STYLE_STATIC;
553 	} else if (zend_string_equals_literal_ci(val, "dynamic")) {
554 		c->pm = PM_STYLE_DYNAMIC;
555 	} else if (zend_string_equals_literal_ci(val, "ondemand")) {
556 		c->pm = PM_STYLE_ONDEMAND;
557 	} else {
558 		return "invalid process manager (static, dynamic or ondemand)";
559 	}
560 	return NULL;
561 }
562 /* }}} */
563 
fpm_conf_set_array(zval * key,zval * value,void ** config,int convert_to_bool)564 static char *fpm_conf_set_array(zval *key, zval *value, void **config, int convert_to_bool) /* {{{ */
565 {
566 	struct key_value_s *kv;
567 	struct key_value_s ***parent = (struct key_value_s ***) config;
568 	int b;
569 	void *subconf = &b;
570 
571 	kv = malloc(sizeof(*kv));
572 
573 	if (!kv) {
574 		return "malloc() failed";
575 	}
576 
577 	memset(kv, 0, sizeof(*kv));
578 	if (key) {
579 		kv->key = strdup(Z_STRVAL_P(key));
580 
581 		if (!kv->key) {
582 			free(kv);
583 			return "fpm_conf_set_array: strdup(key) failed";
584 		}
585 	}
586 
587 	if (convert_to_bool) {
588 		char *err = fpm_conf_set_boolean(value, &subconf, 0);
589 		if (err) {
590 			free(kv->key);
591 			free(kv);
592 			return err;
593 		}
594 		kv->value = strdup(b ? "1" : "0");
595 	} else {
596 		kv->value = strdup(Z_STRVAL_P(value));
597 		if (fpm_conf_expand_pool_name(&kv->value) == -1) {
598 			free(kv->key);
599 			free(kv);
600 			return "Can't use '$pool' when the pool is not defined";
601 		}
602 	}
603 
604 	if (!kv->value) {
605 		free(kv->key);
606 		free(kv);
607 		return "fpm_conf_set_array: strdup(value) failed";
608 	}
609 
610 	kv->next = **parent;
611 	**parent = kv;
612 	return NULL;
613 }
614 /* }}} */
615 
fpm_worker_pool_config_alloc(void)616 static void *fpm_worker_pool_config_alloc(void)
617 {
618 	struct fpm_worker_pool_s *wp;
619 
620 	wp = fpm_worker_pool_alloc();
621 
622 	if (!wp) {
623 		return 0;
624 	}
625 
626 	wp->config = malloc(sizeof(struct fpm_worker_pool_config_s));
627 
628 	if (!wp->config) {
629 		fpm_worker_pool_free(wp);
630 		return 0;
631 	}
632 
633 	memset(wp->config, 0, sizeof(struct fpm_worker_pool_config_s));
634 	wp->config->listen_backlog = FPM_BACKLOG_DEFAULT;
635 	wp->config->pm_max_spawn_rate = 32; /* 32 by default */
636 	wp->config->pm_process_idle_timeout = 10; /* 10s by default */
637 	wp->config->process_priority = 64; /* 64 means unset */
638 	wp->config->process_dumpable = 0;
639 	wp->config->clear_env = 1;
640 	wp->config->decorate_workers_output = 1;
641 #ifdef SO_SETFIB
642 	wp->config->listen_setfib = -1;
643 #endif
644 
645 	if (!fpm_worker_all_pools) {
646 		fpm_worker_all_pools = wp;
647 	} else {
648 		struct fpm_worker_pool_s *tmp = fpm_worker_all_pools;
649 		while (tmp) {
650 			if (!tmp->next) {
651 				tmp->next = wp;
652 				break;
653 			}
654 			tmp = tmp->next;
655 		}
656 	}
657 
658 	current_wp = wp;
659 	return wp->config;
660 }
661 
fpm_worker_pool_config_free(struct fpm_worker_pool_config_s * wpc)662 int fpm_worker_pool_config_free(struct fpm_worker_pool_config_s *wpc) /* {{{ */
663 {
664 	struct key_value_s *kv, *kv_next;
665 
666 	free(wpc->name);
667 	free(wpc->prefix);
668 	free(wpc->user);
669 	free(wpc->group);
670 	free(wpc->listen_address);
671 	free(wpc->listen_owner);
672 	free(wpc->listen_group);
673 	free(wpc->listen_mode);
674 	free(wpc->listen_allowed_clients);
675 	free(wpc->pm_status_path);
676 	free(wpc->ping_path);
677 	free(wpc->ping_response);
678 	free(wpc->access_log);
679 	free(wpc->access_format);
680 	free(wpc->slowlog);
681 	free(wpc->chroot);
682 	free(wpc->chdir);
683 	free(wpc->security_limit_extensions);
684 #ifdef HAVE_APPARMOR
685 	free(wpc->apparmor_hat);
686 #endif
687 
688 	for (kv = wpc->access_suppress_paths; kv; kv = kv_next) {
689 		kv_next = kv->next;
690 		free(kv->value);
691 		free(kv);
692 	}
693 	for (kv = wpc->php_values; kv; kv = kv_next) {
694 		kv_next = kv->next;
695 		free(kv->key);
696 		free(kv->value);
697 		free(kv);
698 	}
699 	for (kv = wpc->php_admin_values; kv; kv = kv_next) {
700 		kv_next = kv->next;
701 		free(kv->key);
702 		free(kv->value);
703 		free(kv);
704 	}
705 	for (kv = wpc->env; kv; kv = kv_next) {
706 		kv_next = kv->next;
707 		free(kv->key);
708 		free(kv->value);
709 		free(kv);
710 	}
711 
712 	return 0;
713 }
714 /* }}} */
715 
716 #define FPM_WPC_STR_CP_EX(_cfg, _scfg, _sf, _df) \
717 	do { \
718 		if (_scfg->_df && !(_cfg->_sf = strdup(_scfg->_df))) { \
719 			return -1; \
720 		} \
721 	} while (0)
722 #define FPM_WPC_STR_CP(_cfg, _scfg, _field) FPM_WPC_STR_CP_EX(_cfg, _scfg, _field, _field)
723 
fpm_worker_pool_shared_status_alloc(struct fpm_worker_pool_s * shared_wp)724 static int fpm_worker_pool_shared_status_alloc(struct fpm_worker_pool_s *shared_wp) { /* {{{ */
725 	struct fpm_worker_pool_config_s *config, *shared_config;
726 	config = fpm_worker_pool_config_alloc();
727 	if (!config) {
728 		return -1;
729 	}
730 	shared_config = shared_wp->config;
731 
732 	config->name = malloc(strlen(shared_config->name) + sizeof("_status"));
733 	if (!config->name) {
734 		return -1;
735 	}
736 	strcpy(config->name, shared_config->name);
737 	strcpy(config->name + strlen(shared_config->name), "_status");
738 
739 	if (!shared_config->pm_status_path) {
740 		shared_config->pm_status_path = strdup("/");
741 	}
742 
743 	FPM_WPC_STR_CP_EX(config, shared_config, listen_address, pm_status_listen);
744 #ifdef HAVE_FPM_ACL
745 	FPM_WPC_STR_CP(config, shared_config, listen_acl_groups);
746 	FPM_WPC_STR_CP(config, shared_config, listen_acl_users);
747 #endif
748 	FPM_WPC_STR_CP(config, shared_config, listen_allowed_clients);
749 	FPM_WPC_STR_CP(config, shared_config, listen_group);
750 	FPM_WPC_STR_CP(config, shared_config, listen_owner);
751 	FPM_WPC_STR_CP(config, shared_config, listen_mode);
752 	FPM_WPC_STR_CP(config, shared_config, user);
753 	FPM_WPC_STR_CP(config, shared_config, group);
754 	FPM_WPC_STR_CP(config, shared_config, pm_status_path);
755 	FPM_WPC_STR_CP(config, shared_config, ping_path);
756 	FPM_WPC_STR_CP(config, shared_config, ping_response);
757 
758 	config->pm = PM_STYLE_ONDEMAND;
759 	config->pm_max_children = 2;
760 
761 	current_wp->shared = shared_wp;
762 
763 	return 0;
764 }
765 /* }}} */
766 
fpm_evaluate_full_path(char ** path,struct fpm_worker_pool_s * wp,char * default_prefix,int expand)767 static int fpm_evaluate_full_path(char **path, struct fpm_worker_pool_s *wp, char *default_prefix, int expand) /* {{{ */
768 {
769 	char *prefix = NULL;
770 	char *full_path;
771 
772 	if (!path || !*path || **path == '/') {
773 		return 0;
774 	}
775 
776 	if (wp && wp->config) {
777 		prefix = wp->config->prefix;
778 	}
779 
780 	/* if the wp prefix is not set */
781 	if (prefix == NULL) {
782 		prefix = fpm_globals.prefix;
783 	}
784 
785 	/* if the global prefix is not set */
786 	if (prefix == NULL) {
787 		prefix = default_prefix ? default_prefix : PHP_PREFIX;
788 	}
789 
790 	if (expand) {
791 		char *tmp;
792 		tmp = strstr(*path, "$prefix");
793 		if (tmp != NULL) {
794 
795 			if (tmp != *path) {
796 				zlog(ZLOG_ERROR, "'$prefix' must be use at the beginning of the value");
797 				return -1;
798 			}
799 
800 			if (strlen(*path) > strlen("$prefix")) {
801 				tmp = strdup((*path) + strlen("$prefix"));
802 				free(*path);
803 				*path = tmp;
804 			} else {
805 				free(*path);
806 				*path = NULL;
807 			}
808 		}
809 	}
810 
811 	if (*path) {
812 		spprintf(&full_path, 0, "%s/%s", prefix, *path);
813 		free(*path);
814 		*path = strdup(full_path);
815 		efree(full_path);
816 	} else {
817 		*path = strdup(prefix);
818 	}
819 
820 	if (**path != '/' && wp != NULL && wp->config) {
821 		return fpm_evaluate_full_path(path, NULL, default_prefix, expand);
822 	}
823 	return 0;
824 }
825 /* }}} */
826 
fpm_conf_process_all_pools(void)827 static int fpm_conf_process_all_pools(void)
828 {
829 	struct fpm_worker_pool_s *wp, *wp2;
830 
831 	if (!fpm_worker_all_pools) {
832 		zlog(ZLOG_ERROR, "No pool defined. at least one pool section must be specified in config file");
833 		return -1;
834 	}
835 
836 	for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
837 
838 		/* prefix */
839 		if (wp->config->prefix && *wp->config->prefix) {
840 			fpm_evaluate_full_path(&wp->config->prefix, NULL, NULL, 0);
841 
842 			if (!fpm_conf_is_dir(wp->config->prefix)) {
843 				zlog(ZLOG_ERROR, "[pool %s] the prefix '%s' does not exist or is not a directory", wp->config->name, wp->config->prefix);
844 				return -1;
845 			}
846 		}
847 
848 		/* alert if user is not set; only if we are root and fpm is not running with --allow-to-run-as-root */
849 		if (!wp->config->user && !geteuid() && !fpm_globals.run_as_root) {
850 			zlog(ZLOG_ALERT, "[pool %s] user has not been defined", wp->config->name);
851 			return -1;
852 		}
853 
854 		/* listen */
855 		if (wp->config->listen_address && *wp->config->listen_address) {
856 			wp->listen_address_domain = fpm_sockets_domain_from_address(wp->config->listen_address);
857 
858 			if (wp->listen_address_domain == FPM_AF_UNIX && *wp->config->listen_address != '/') {
859 				fpm_evaluate_full_path(&wp->config->listen_address, wp, NULL, 0);
860 			}
861 		} else {
862 			zlog(ZLOG_ALERT, "[pool %s] no listen address have been defined!", wp->config->name);
863 			return -1;
864 		}
865 
866 		if (wp->config->process_priority != 64 && (wp->config->process_priority < -19 || wp->config->process_priority > 20)) {
867 			zlog(ZLOG_ERROR, "[pool %s] process.priority must be included into [-19,20]", wp->config->name);
868 			return -1;
869 		}
870 
871 		/* pm */
872 		if (wp->config->pm != PM_STYLE_STATIC && wp->config->pm != PM_STYLE_DYNAMIC && wp->config->pm != PM_STYLE_ONDEMAND) {
873 			zlog(ZLOG_ALERT, "[pool %s] the process manager is missing (static, dynamic or ondemand)", wp->config->name);
874 			return -1;
875 		}
876 
877 		/* pm.max_children */
878 		if (wp->config->pm_max_children < 1) {
879 			zlog(ZLOG_ALERT, "[pool %s] pm.max_children must be a positive value", wp->config->name);
880 			return -1;
881 		}
882 
883 		/* pm.start_servers, pm.min_spare_servers, pm.max_spare_servers, pm.max_spawn_rate */
884 		if (wp->config->pm == PM_STYLE_DYNAMIC) {
885 			struct fpm_worker_pool_config_s *config = wp->config;
886 
887 			if (config->pm_min_spare_servers <= 0) {
888 				zlog(ZLOG_ALERT, "[pool %s] pm.min_spare_servers(%d) must be a positive value", wp->config->name, config->pm_min_spare_servers);
889 				return -1;
890 			}
891 
892 			if (config->pm_max_spare_servers <= 0) {
893 				zlog(ZLOG_ALERT, "[pool %s] pm.max_spare_servers(%d) must be a positive value", wp->config->name, config->pm_max_spare_servers);
894 				return -1;
895 			}
896 
897 			if (config->pm_min_spare_servers > config->pm_max_children ||
898 					config->pm_max_spare_servers > config->pm_max_children) {
899 				zlog(ZLOG_ALERT, "[pool %s] pm.min_spare_servers(%d) and pm.max_spare_servers(%d) cannot be greater than pm.max_children(%d)", wp->config->name, config->pm_min_spare_servers, config->pm_max_spare_servers, config->pm_max_children);
900 				return -1;
901 			}
902 
903 			if (config->pm_max_spare_servers < config->pm_min_spare_servers) {
904 				zlog(ZLOG_ALERT, "[pool %s] pm.max_spare_servers(%d) must not be less than pm.min_spare_servers(%d)", wp->config->name, config->pm_max_spare_servers, config->pm_min_spare_servers);
905 				return -1;
906 			}
907 
908 			if (config->pm_start_servers <= 0) {
909 				config->pm_start_servers = config->pm_min_spare_servers + ((config->pm_max_spare_servers - config->pm_min_spare_servers) / 2);
910 				zlog(ZLOG_NOTICE, "[pool %s] pm.start_servers is not set. It's been set to %d.", wp->config->name, config->pm_start_servers);
911 
912 			} else if (config->pm_start_servers < config->pm_min_spare_servers || config->pm_start_servers > config->pm_max_spare_servers) {
913 				zlog(ZLOG_ALERT, "[pool %s] pm.start_servers(%d) must not be less than pm.min_spare_servers(%d) and not greater than pm.max_spare_servers(%d)", wp->config->name, config->pm_start_servers, config->pm_min_spare_servers, config->pm_max_spare_servers);
914 				return -1;
915 			}
916 
917 			if (config->pm_max_spawn_rate < 1) {
918 				zlog(ZLOG_ALERT, "[pool %s] pm.max_spawn_rate must be a positive value", wp->config->name);
919 				return -1;
920 			}
921 		} else if (wp->config->pm == PM_STYLE_ONDEMAND) {
922 			struct fpm_worker_pool_config_s *config = wp->config;
923 
924 			if (!fpm_event_support_edge_trigger()) {
925 				zlog(ZLOG_ALERT, "[pool %s] ondemand process manager can ONLY be used when events.mechanism is either epoll (Linux) or kqueue (*BSD).", wp->config->name);
926 				return -1;
927 			}
928 
929 			if (config->pm_process_idle_timeout < 1) {
930 				zlog(ZLOG_ALERT, "[pool %s] pm.process_idle_timeout(%ds) must be greater than 0s", wp->config->name, config->pm_process_idle_timeout);
931 				return -1;
932 			}
933 
934 			if (config->listen_backlog < FPM_BACKLOG_DEFAULT) {
935 				zlog(ZLOG_WARNING, "[pool %s] listen.backlog(%d) was too low for the ondemand process manager. I updated it for you to %d.", wp->config->name, config->listen_backlog, FPM_BACKLOG_DEFAULT);
936 				config->listen_backlog = FPM_BACKLOG_DEFAULT;
937 			}
938 
939 			/* certainly useless but proper */
940 			config->pm_start_servers = 0;
941 			config->pm_min_spare_servers = 0;
942 			config->pm_max_spare_servers = 0;
943 		}
944 
945 		/* status */
946 		if (wp->config->pm_status_listen && fpm_worker_pool_shared_status_alloc(wp)) {
947 			zlog(ZLOG_ERROR, "[pool %s] failed to initialize a status listener pool", wp->config->name);
948 		}
949 
950 		if (wp->config->pm_status_path && *wp->config->pm_status_path) {
951 			size_t i;
952 			char *status = wp->config->pm_status_path;
953 
954 			if (*status != '/') {
955 				zlog(ZLOG_ERROR, "[pool %s] the status path '%s' must start with a '/'", wp->config->name, status);
956 				return -1;
957 			}
958 
959 			if (!wp->config->pm_status_listen && !wp->shared && strlen(status) < 2) {
960 				zlog(ZLOG_ERROR, "[pool %s] the status path '%s' is not long enough", wp->config->name, status);
961 				return -1;
962 			}
963 
964 			for (i = 0; i < strlen(status); i++) {
965 				if (!isalnum(status[i]) && status[i] != '/' && status[i] != '-' && status[i] != '_' && status[i] != '.' && status[i] != '~') {
966 					zlog(ZLOG_ERROR, "[pool %s] the status path '%s' must contain only the following characters '[alphanum]/_-.~'", wp->config->name, status);
967 					return -1;
968 				}
969 			}
970 		}
971 
972 		/* ping */
973 		if (wp->config->ping_path && *wp->config->ping_path) {
974 			char *ping = wp->config->ping_path;
975 			size_t i;
976 
977 			if (*ping != '/') {
978 				zlog(ZLOG_ERROR, "[pool %s] the ping path '%s' must start with a '/'", wp->config->name, ping);
979 				return -1;
980 			}
981 
982 			if (strlen(ping) < 2) {
983 				zlog(ZLOG_ERROR, "[pool %s] the ping path '%s' is not long enough", wp->config->name, ping);
984 				return -1;
985 			}
986 
987 			for (i = 0; i < strlen(ping); i++) {
988 				if (!isalnum(ping[i]) && ping[i] != '/' && ping[i] != '-' && ping[i] != '_' && ping[i] != '.' && ping[i] != '~') {
989 					zlog(ZLOG_ERROR, "[pool %s] the ping path '%s' must contain only the following characters '[alphanum]/_-.~'", wp->config->name, ping);
990 					return -1;
991 				}
992 			}
993 
994 			if (!wp->config->ping_response) {
995 				wp->config->ping_response = strdup("pong");
996 			} else {
997 				if (strlen(wp->config->ping_response) < 1) {
998 					zlog(ZLOG_ERROR, "[pool %s] the ping response page '%s' is not long enough", wp->config->name, wp->config->ping_response);
999 					return -1;
1000 				}
1001 			}
1002 		} else {
1003 			if (wp->config->ping_response) {
1004 				free(wp->config->ping_response);
1005 				wp->config->ping_response = NULL;
1006 			}
1007 		}
1008 
1009 		/* access.log, access.format */
1010 		if (wp->config->access_log && *wp->config->access_log) {
1011 			fpm_evaluate_full_path(&wp->config->access_log, wp, NULL, 0);
1012 			if (!wp->config->access_format) {
1013 				wp->config->access_format = strdup("%R - %u %t \"%m %r\" %s");
1014 			}
1015 		}
1016 
1017 		if (wp->config->request_terminate_timeout) {
1018 			fpm_globals.heartbeat = fpm_globals.heartbeat ? MIN(fpm_globals.heartbeat, (wp->config->request_terminate_timeout * 1000) / 3) : (wp->config->request_terminate_timeout * 1000) / 3;
1019 		}
1020 
1021 		/* slowlog */
1022 		if (wp->config->slowlog && *wp->config->slowlog) {
1023 			fpm_evaluate_full_path(&wp->config->slowlog, wp, NULL, 0);
1024 		}
1025 
1026 		/* request_slowlog_timeout */
1027 		if (wp->config->request_slowlog_timeout) {
1028 #if HAVE_FPM_TRACE
1029 			if (! (wp->config->slowlog && *wp->config->slowlog)) {
1030 				zlog(ZLOG_ERROR, "[pool %s] 'slowlog' must be specified for use with 'request_slowlog_timeout'", wp->config->name);
1031 				return -1;
1032 			}
1033 #else
1034 			static int warned = 0;
1035 
1036 			if (!warned) {
1037 				zlog(ZLOG_WARNING, "[pool %s] 'request_slowlog_timeout' is not supported on your system",	wp->config->name);
1038 				warned = 1;
1039 			}
1040 
1041 			wp->config->request_slowlog_timeout = 0;
1042 #endif
1043 
1044 			if (wp->config->slowlog && *wp->config->slowlog) {
1045 				int fd;
1046 
1047 				fd = open(wp->config->slowlog, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
1048 
1049 				if (0 > fd) {
1050 					zlog(ZLOG_SYSERROR, "Unable to create or open slowlog(%s)", wp->config->slowlog);
1051 					return -1;
1052 				}
1053 				close(fd);
1054 			}
1055 
1056 			fpm_globals.heartbeat = fpm_globals.heartbeat ? MIN(fpm_globals.heartbeat, (wp->config->request_slowlog_timeout * 1000) / 3) : (wp->config->request_slowlog_timeout * 1000) / 3;
1057 
1058 			if (wp->config->request_terminate_timeout && wp->config->request_slowlog_timeout > wp->config->request_terminate_timeout) {
1059 				zlog(ZLOG_ERROR, "[pool %s] 'request_slowlog_timeout' (%d) can't be greater than 'request_terminate_timeout' (%d)", wp->config->name, wp->config->request_slowlog_timeout, wp->config->request_terminate_timeout);
1060 				return -1;
1061 			}
1062 		}
1063 
1064 		/* request_slowlog_trace_depth */
1065 		if (wp->config->request_slowlog_trace_depth) {
1066 #if HAVE_FPM_TRACE
1067 			if (! (wp->config->slowlog && *wp->config->slowlog)) {
1068 				zlog(ZLOG_ERROR, "[pool %s] 'slowlog' must be specified for use with 'request_slowlog_trace_depth'", wp->config->name);
1069 				return -1;
1070 			}
1071 #else
1072 			static int warned = 0;
1073 
1074 			if (!warned) {
1075 				zlog(ZLOG_WARNING, "[pool %s] 'request_slowlog_trace_depth' is not supported on your system", wp->config->name);
1076 				warned = 1;
1077 			}
1078 #endif
1079 
1080 			if (wp->config->request_slowlog_trace_depth <= 0) {
1081 				zlog(ZLOG_ERROR, "[pool %s] 'request_slowlog_trace_depth' (%d) must be a positive value", wp->config->name, wp->config->request_slowlog_trace_depth);
1082 				return -1;
1083 			}
1084 		} else {
1085 			wp->config->request_slowlog_trace_depth = 20;
1086 		}
1087 
1088 		/* chroot */
1089 		if (wp->config->chroot && *wp->config->chroot) {
1090 
1091 			fpm_evaluate_full_path(&wp->config->chroot, wp, NULL, 1);
1092 
1093 			if (*wp->config->chroot != '/') {
1094 				zlog(ZLOG_ERROR, "[pool %s] the chroot path '%s' must start with a '/'", wp->config->name, wp->config->chroot);
1095 				return -1;
1096 			}
1097 
1098 			if (!fpm_conf_is_dir(wp->config->chroot)) {
1099 				zlog(ZLOG_ERROR, "[pool %s] the chroot path '%s' does not exist or is not a directory", wp->config->name, wp->config->chroot);
1100 				return -1;
1101 			}
1102 		}
1103 
1104 		/* chdir */
1105 		if (wp->config->chdir && *wp->config->chdir) {
1106 
1107 			fpm_evaluate_full_path(&wp->config->chdir, wp, NULL, 0);
1108 
1109 			if (*wp->config->chdir != '/') {
1110 				zlog(ZLOG_ERROR, "[pool %s] the chdir path '%s' must start with a '/'", wp->config->name, wp->config->chdir);
1111 				return -1;
1112 			}
1113 
1114 			if (wp->config->chroot) {
1115 				char *buf;
1116 
1117 				spprintf(&buf, 0, "%s/%s", wp->config->chroot, wp->config->chdir);
1118 
1119 				if (!fpm_conf_is_dir(buf)) {
1120 					zlog(ZLOG_ERROR, "[pool %s] the chdir path '%s' within the chroot path '%s' ('%s') does not exist or is not a directory", wp->config->name, wp->config->chdir, wp->config->chroot, buf);
1121 					efree(buf);
1122 					return -1;
1123 				}
1124 
1125 				efree(buf);
1126 			} else {
1127 				if (!fpm_conf_is_dir(wp->config->chdir)) {
1128 					zlog(ZLOG_ERROR, "[pool %s] the chdir path '%s' does not exist or is not a directory", wp->config->name, wp->config->chdir);
1129 					return -1;
1130 				}
1131 			}
1132 		}
1133 
1134 		/* security.limit_extensions */
1135 		if (!wp->config->security_limit_extensions) {
1136 			wp->config->security_limit_extensions = strdup(".php .phar");
1137 		}
1138 
1139 		if (*wp->config->security_limit_extensions) {
1140 			int nb_ext;
1141 			char *ext;
1142 			char *security_limit_extensions;
1143 			char *limit_extensions;
1144 
1145 
1146 			/* strdup because strtok(3) alters the string it parses */
1147 			security_limit_extensions = strdup(wp->config->security_limit_extensions);
1148 			limit_extensions = security_limit_extensions;
1149 			nb_ext = 0;
1150 
1151 			/* find the number of extensions */
1152 			while (strtok(limit_extensions, " \t")) {
1153 				limit_extensions = NULL;
1154 				nb_ext++;
1155 			}
1156 			free(security_limit_extensions);
1157 
1158 			/* if something found */
1159 			if (nb_ext > 0) {
1160 
1161 				/* malloc the extension array */
1162 				wp->limit_extensions = malloc(sizeof(char *) * (nb_ext + 1));
1163 				if (!wp->limit_extensions) {
1164 					zlog(ZLOG_ERROR, "[pool %s] unable to malloc extensions array", wp->config->name);
1165 					return -1;
1166 				}
1167 
1168 				/* strdup because strtok(3) alters the string it parses */
1169 				security_limit_extensions = strdup(wp->config->security_limit_extensions);
1170 				limit_extensions = security_limit_extensions;
1171 				nb_ext = 0;
1172 
1173 				/* parse the string and save the extension in the array */
1174 				while ((ext = strtok(limit_extensions, " \t"))) {
1175 					limit_extensions = NULL;
1176 					wp->limit_extensions[nb_ext++] = strdup(ext);
1177 				}
1178 
1179 				/* end the array with NULL in order to parse it */
1180 				wp->limit_extensions[nb_ext] = NULL;
1181 				free(security_limit_extensions);
1182 			}
1183 		}
1184 
1185 		/* env[], php_value[], php_admin_values[] */
1186 		if (!wp->config->chroot) {
1187 			struct key_value_s *kv;
1188 			char *options[] = FPM_PHP_INI_TO_EXPAND;
1189 			char **p;
1190 
1191 			for (kv = wp->config->php_values; kv; kv = kv->next) {
1192 				for (p = options; *p; p++) {
1193 					if (!strcasecmp(kv->key, *p)) {
1194 						fpm_evaluate_full_path(&kv->value, wp, NULL, 0);
1195 					}
1196 				}
1197 			}
1198 			for (kv = wp->config->php_admin_values; kv; kv = kv->next) {
1199 				if (!strcasecmp(kv->key, "error_log") && !strcasecmp(kv->value, "syslog")) {
1200 					continue;
1201 				}
1202 				for (p = options; *p; p++) {
1203 					if (!strcasecmp(kv->key, *p)) {
1204 						fpm_evaluate_full_path(&kv->value, wp, NULL, 0);
1205 					}
1206 				}
1207 			}
1208 		}
1209 	}
1210 
1211 	/* ensure 2 pools do not use the same listening address */
1212 	for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
1213 		for (wp2 = fpm_worker_all_pools; wp2; wp2 = wp2->next) {
1214 			if (wp == wp2) {
1215 				continue;
1216 			}
1217 
1218 			if (wp->config->listen_address && *wp->config->listen_address && wp2->config->listen_address && *wp2->config->listen_address && !strcmp(wp->config->listen_address, wp2->config->listen_address)) {
1219 				zlog(ZLOG_ERROR, "[pool %s] unable to set listen address as it's already used in another pool '%s'", wp2->config->name, wp->config->name);
1220 				return -1;
1221 			}
1222 		}
1223 	}
1224 	return 0;
1225 }
1226 
fpm_conf_unlink_pid(void)1227 int fpm_conf_unlink_pid(void)
1228 {
1229 	if (fpm_global_config.pid_file) {
1230 		if (0 > unlink(fpm_global_config.pid_file)) {
1231 			zlog(ZLOG_SYSERROR, "Unable to remove the PID file (%s).", fpm_global_config.pid_file);
1232 			return -1;
1233 		}
1234 	}
1235 	return 0;
1236 }
1237 
fpm_conf_write_pid(void)1238 int fpm_conf_write_pid(void)
1239 {
1240 	int fd;
1241 
1242 	if (fpm_global_config.pid_file) {
1243 		char buf[64];
1244 		int len;
1245 
1246 		unlink(fpm_global_config.pid_file);
1247 		fd = creat(fpm_global_config.pid_file, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
1248 
1249 		if (fd < 0) {
1250 			zlog(ZLOG_SYSERROR, "Unable to create the PID file (%s).", fpm_global_config.pid_file);
1251 			return -1;
1252 		}
1253 
1254 		len = sprintf(buf, "%d", (int) fpm_globals.parent_pid);
1255 
1256 		if (len != write(fd, buf, len)) {
1257 			zlog(ZLOG_SYSERROR, "Unable to write to the PID file.");
1258 			close(fd);
1259 			return -1;
1260 		}
1261 		close(fd);
1262 	}
1263 	return 0;
1264 }
1265 
fpm_conf_post_process(int force_daemon)1266 static int fpm_conf_post_process(int force_daemon) /* {{{ */
1267 {
1268 	struct fpm_worker_pool_s *wp;
1269 
1270 	if (fpm_global_config.pid_file) {
1271 		fpm_evaluate_full_path(&fpm_global_config.pid_file, NULL, PHP_LOCALSTATEDIR, 0);
1272 	}
1273 
1274 	if (force_daemon >= 0) {
1275 		/* forced from command line options */
1276 		fpm_global_config.daemonize = force_daemon;
1277 	}
1278 
1279 	fpm_globals.log_level = fpm_global_config.log_level;
1280 	zlog_set_level(fpm_globals.log_level);
1281 	if (fpm_global_config.log_limit < ZLOG_MIN_LIMIT) {
1282 		zlog(ZLOG_ERROR, "log_limit must be greater than %d", ZLOG_MIN_LIMIT);
1283 		return -1;
1284 	}
1285 	zlog_set_limit(fpm_global_config.log_limit);
1286 	zlog_set_buffering(fpm_global_config.log_buffering);
1287 
1288 	if (fpm_global_config.process_max < 0) {
1289 		zlog(ZLOG_ERROR, "process_max can't be negative");
1290 		return -1;
1291 	}
1292 
1293 	if (fpm_global_config.process_priority != 64 && (fpm_global_config.process_priority < -19 || fpm_global_config.process_priority > 20)) {
1294 		zlog(ZLOG_ERROR, "process.priority must be included into [-19,20]");
1295 		return -1;
1296 	}
1297 
1298 	if (!fpm_global_config.error_log) {
1299 		fpm_global_config.error_log = strdup("log/php-fpm.log");
1300 	}
1301 
1302 #ifdef HAVE_SYSTEMD
1303 	if (0 > fpm_systemd_conf()) {
1304 		return -1;
1305 	}
1306 #endif
1307 
1308 #ifdef HAVE_SYSLOG_H
1309 	if (!fpm_global_config.syslog_ident) {
1310 		fpm_global_config.syslog_ident = strdup("php-fpm");
1311 	}
1312 
1313 	if (fpm_global_config.syslog_facility < 0) {
1314 		fpm_global_config.syslog_facility = LOG_DAEMON;
1315 	}
1316 
1317 	if (strcasecmp(fpm_global_config.error_log, "syslog") != 0)
1318 #endif
1319 	{
1320 		fpm_evaluate_full_path(&fpm_global_config.error_log, NULL, PHP_LOCALSTATEDIR, 0);
1321 	}
1322 
1323 	if (!fpm_global_config.daemonize && 0 > fpm_stdio_save_original_stderr()) {
1324 		return -1;
1325 	}
1326 
1327 	if (0 > fpm_stdio_open_error_log(0)) {
1328 		return -1;
1329 	}
1330 
1331 	if (0 > fpm_event_pre_init(fpm_global_config.events_mechanism)) {
1332 		return -1;
1333 	}
1334 
1335 	if (0 > fpm_conf_process_all_pools()) {
1336 		return -1;
1337 	}
1338 
1339 	if (0 > fpm_log_open(0)) {
1340 		return -1;
1341 	}
1342 
1343 	for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
1344 		if (!wp->config->access_log || !*wp->config->access_log) {
1345 			continue;
1346 		}
1347 		if (0 > fpm_log_write(wp->config->access_format)) {
1348 			zlog(ZLOG_ERROR, "[pool %s] wrong format for access.format '%s'", wp->config->name, wp->config->access_format);
1349 			return -1;
1350 		}
1351 	}
1352 
1353 	return 0;
1354 }
1355 /* }}} */
1356 
fpm_conf_cleanup(int which,void * arg)1357 static void fpm_conf_cleanup(int which, void *arg) /* {{{ */
1358 {
1359 	free(fpm_global_config.pid_file);
1360 	free(fpm_global_config.error_log);
1361 	free(fpm_global_config.events_mechanism);
1362 	fpm_global_config.pid_file = 0;
1363 	fpm_global_config.error_log = 0;
1364 	fpm_global_config.log_limit = ZLOG_DEFAULT_LIMIT;
1365 #ifdef HAVE_SYSLOG_H
1366 	free(fpm_global_config.syslog_ident);
1367 	fpm_global_config.syslog_ident = 0;
1368 #endif
1369 	free(fpm_globals.config);
1370 }
1371 /* }}} */
1372 
fpm_conf_ini_parser_include(char * inc,void * arg)1373 static void fpm_conf_ini_parser_include(char *inc, void *arg) /* {{{ */
1374 {
1375 	char *filename;
1376 	int *error = (int *)arg;
1377 #ifdef HAVE_GLOB
1378 	glob_t g;
1379 #endif
1380 	size_t i;
1381 
1382 	if (!inc || !arg) return;
1383 	if (*error) return; /* We got already an error. Switch to the end. */
1384 	spprintf(&filename, 0, "%s", ini_filename);
1385 
1386 #ifdef HAVE_GLOB
1387 	{
1388 		g.gl_offs = 0;
1389 		if ((i = glob(inc, GLOB_ERR | GLOB_MARK, NULL, &g)) != 0) {
1390 #ifdef GLOB_NOMATCH
1391 			if (i == GLOB_NOMATCH) {
1392 				zlog(ZLOG_WARNING, "Nothing matches the include pattern '%s' from %s at line %d.", inc, filename, ini_lineno);
1393 				efree(filename);
1394 				return;
1395 			}
1396 #endif /* GLOB_NOMATCH */
1397 			zlog(ZLOG_ERROR, "Unable to globalize '%s' (ret=%zd) from %s at line %d.", inc, i, filename, ini_lineno);
1398 			*error = 1;
1399 			efree(filename);
1400 			return;
1401 		}
1402 
1403 		for (i = 0; i < g.gl_pathc; i++) {
1404 			int len = strlen(g.gl_pathv[i]);
1405 			if (len < 1) continue;
1406 			if (g.gl_pathv[i][len - 1] == '/') continue; /* don't parse directories */
1407 			if (0 > fpm_conf_load_ini_file(g.gl_pathv[i])) {
1408 				zlog(ZLOG_ERROR, "Unable to include %s from %s at line %d", g.gl_pathv[i], filename, ini_lineno);
1409 				*error = 1;
1410 				efree(filename);
1411 				return;
1412 			}
1413 		}
1414 		globfree(&g);
1415 	}
1416 #else /* HAVE_GLOB */
1417 	if (0 > fpm_conf_load_ini_file(inc)) {
1418 		zlog(ZLOG_ERROR, "Unable to include %s from %s at line %d", inc, filename, ini_lineno);
1419 		*error = 1;
1420 		efree(filename);
1421 		return;
1422 	}
1423 #endif /* HAVE_GLOB */
1424 
1425 	efree(filename);
1426 }
1427 /* }}} */
1428 
fpm_conf_ini_parser_section(zval * section,void * arg)1429 static void fpm_conf_ini_parser_section(zval *section, void *arg) /* {{{ */
1430 {
1431 	struct fpm_worker_pool_s *wp;
1432 	struct fpm_worker_pool_config_s *config;
1433 	int *error = (int *)arg;
1434 
1435 	/* switch to global conf */
1436 	if (zend_string_equals_literal_ci(Z_STR_P(section), "global")) {
1437 		current_wp = NULL;
1438 		return;
1439 	}
1440 
1441 	for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
1442 		if (!wp->config) continue;
1443 		if (!wp->config->name) continue;
1444 		if (!strcasecmp(wp->config->name, Z_STRVAL_P(section))) {
1445 			/* Found a wp with the same name. Bring it back */
1446 			current_wp = wp;
1447 			return;
1448 		}
1449 	}
1450 
1451 	/* it's a new pool */
1452 	config = (struct fpm_worker_pool_config_s *)fpm_worker_pool_config_alloc();
1453 	if (!current_wp || !config) {
1454 		zlog(ZLOG_ERROR, "[%s:%d] Unable to alloc a new WorkerPool for worker '%s'", ini_filename, ini_lineno, Z_STRVAL_P(section));
1455 		*error = 1;
1456 		return;
1457 	}
1458 	config->name = strdup(Z_STRVAL_P(section));
1459 	if (!config->name) {
1460 		zlog(ZLOG_ERROR, "[%s:%d] Unable to alloc memory for configuration name for worker '%s'", ini_filename, ini_lineno, Z_STRVAL_P(section));
1461 		*error = 1;
1462 		return;
1463 	}
1464 }
1465 /* }}} */
1466 
fpm_conf_ini_parser_entry(zval * name,zval * value,void * arg)1467 static void fpm_conf_ini_parser_entry(zval *name, zval *value, void *arg) /* {{{ */
1468 {
1469 	struct ini_value_parser_s *parser;
1470 	void *config = NULL;
1471 
1472 	int *error = (int *)arg;
1473 	if (!value) {
1474 		zlog(ZLOG_ERROR, "[%s:%d] value is NULL for a ZEND_INI_PARSER_ENTRY", ini_filename, ini_lineno);
1475 		*error = 1;
1476 		return;
1477 	}
1478 
1479 	if (zend_string_equals_literal(Z_STR_P(name), "include")) {
1480 		if (ini_include) {
1481 			zlog(ZLOG_ERROR, "[%s:%d] two includes at the same time !", ini_filename, ini_lineno);
1482 			*error = 1;
1483 			return;
1484 		}
1485 		ini_include = strdup(Z_STRVAL_P(value));
1486 		return;
1487 	}
1488 
1489 	if (!current_wp) { /* we are in the global section */
1490 		parser = ini_fpm_global_options;
1491 		config = &fpm_global_config;
1492 	} else {
1493 		parser = ini_fpm_pool_options;
1494 		config = current_wp->config;
1495 	}
1496 
1497 	for (; parser->name; parser++) {
1498 		if (!strcasecmp(parser->name, Z_STRVAL_P(name))) {
1499 			char *ret;
1500 			if (!parser->parser) {
1501 				zlog(ZLOG_ERROR, "[%s:%d] the parser for entry '%s' is not defined", ini_filename, ini_lineno, parser->name);
1502 				*error = 1;
1503 				return;
1504 			}
1505 
1506 			ret = parser->parser(value, &config, parser->offset);
1507 			if (ret) {
1508 				zlog(ZLOG_ERROR, "[%s:%d] unable to parse value for entry '%s': %s", ini_filename, ini_lineno, parser->name, ret);
1509 				*error = 1;
1510 				return;
1511 			}
1512 
1513 			/* all is good ! */
1514 			return;
1515 		}
1516 	}
1517 
1518 	/* nothing has been found if we got here */
1519 	zlog(ZLOG_ERROR, "[%s:%d] unknown entry '%s'", ini_filename, ini_lineno, Z_STRVAL_P(name));
1520 	*error = 1;
1521 }
1522 /* }}} */
1523 
fpm_conf_ini_parser_array(zval * name,zval * key,zval * value,void * arg)1524 static void fpm_conf_ini_parser_array(zval *name, zval *key, zval *value, void *arg) /* {{{ */
1525 {
1526 	int *error = (int *)arg;
1527 	char *err = NULL;
1528 	void *config;
1529 
1530 	if (zend_string_equals_literal(Z_STR_P(name), "access.suppress_path")) {
1531 		if (!(*Z_STRVAL_P(key) == '\0')) {
1532 			zlog(ZLOG_ERROR, "[%s:%d] Keys provided to field 'access.suppress_path' are ignored", ini_filename, ini_lineno);
1533 			*error = 1;
1534 		}
1535 		if (!(*Z_STRVAL_P(value)) || (*Z_STRVAL_P(value) != '/')) {
1536 			zlog(ZLOG_ERROR, "[%s:%d] Values provided to field 'access.suppress_path' must begin with '/'", ini_filename, ini_lineno);
1537 			*error = 1;
1538 		}
1539 		if (*error) {
1540 			return;
1541 		}
1542 	} else if (!*Z_STRVAL_P(key)) {
1543 		zlog(ZLOG_ERROR, "[%s:%d] You must provide a key for field '%s'", ini_filename, ini_lineno, Z_STRVAL_P(name));
1544 		*error = 1;
1545 		return;
1546 	}
1547 
1548 	if (!current_wp || !current_wp->config) {
1549 		zlog(ZLOG_ERROR, "[%s:%d] Array are not allowed in the global section", ini_filename, ini_lineno);
1550 		*error = 1;
1551 		return;
1552 	}
1553 
1554 	if (zend_string_equals_literal(Z_STR_P(name), "env")) {
1555 		if (!*Z_STRVAL_P(value)) {
1556 			zlog(ZLOG_ERROR, "[%s:%d] empty value", ini_filename, ini_lineno);
1557 			*error = 1;
1558 			return;
1559 		}
1560 		config = (char *)current_wp->config + WPO(env);
1561 		err = fpm_conf_set_array(key, value, &config, 0);
1562 
1563 	} else if (zend_string_equals_literal(Z_STR_P(name), "php_value")) {
1564 		config = (char *)current_wp->config + WPO(php_values);
1565 		err = fpm_conf_set_array(key, value, &config, 0);
1566 
1567 	} else if (zend_string_equals_literal(Z_STR_P(name), "php_admin_value")) {
1568 		config = (char *)current_wp->config + WPO(php_admin_values);
1569 		err = fpm_conf_set_array(key, value, &config, 0);
1570 
1571 	} else if (zend_string_equals_literal(Z_STR_P(name), "php_flag")) {
1572 		config = (char *)current_wp->config + WPO(php_values);
1573 		err = fpm_conf_set_array(key, value, &config, 1);
1574 
1575 	} else if (zend_string_equals_literal(Z_STR_P(name), "php_admin_flag")) {
1576 		config = (char *)current_wp->config + WPO(php_admin_values);
1577 		err = fpm_conf_set_array(key, value, &config, 1);
1578 
1579 	} else if (zend_string_equals_literal(Z_STR_P(name), "access.suppress_path")) {
1580 		config = (char *)current_wp->config + WPO(access_suppress_paths);
1581 		err = fpm_conf_set_array(NULL, value, &config, 0);
1582 
1583 	} else {
1584 		zlog(ZLOG_ERROR, "[%s:%d] unknown directive '%s'", ini_filename, ini_lineno, Z_STRVAL_P(name));
1585 		*error = 1;
1586 		return;
1587 	}
1588 
1589 	if (err) {
1590 		zlog(ZLOG_ERROR, "[%s:%d] error while parsing '%s[%s]' : %s", ini_filename, ini_lineno, Z_STRVAL_P(name), Z_STRVAL_P(key), err);
1591 		*error = 1;
1592 		return;
1593 	}
1594 }
1595 /* }}} */
1596 
fpm_conf_ini_parser(zval * arg1,zval * arg2,zval * arg3,int callback_type,void * arg)1597 static void fpm_conf_ini_parser(zval *arg1, zval *arg2, zval *arg3, int callback_type, void *arg) /* {{{ */
1598 {
1599 	int *error;
1600 
1601 	if (!arg1 || !arg) return;
1602 	error = (int *)arg;
1603 	if (*error) return; /* We got already an error. Switch to the end. */
1604 
1605 	switch(callback_type) {
1606 		case ZEND_INI_PARSER_ENTRY:
1607 			fpm_conf_ini_parser_entry(arg1, arg2, error);
1608 			break;
1609 		case ZEND_INI_PARSER_SECTION:
1610 			fpm_conf_ini_parser_section(arg1, error);
1611 			break;
1612 		case ZEND_INI_PARSER_POP_ENTRY:
1613 			fpm_conf_ini_parser_array(arg1, arg3, arg2, error);
1614 			break;
1615 		default:
1616 			zlog(ZLOG_ERROR, "[%s:%d] Unknown INI syntax", ini_filename, ini_lineno);
1617 			*error = 1;
1618 			break;
1619 	}
1620 }
1621 /* }}} */
1622 
fpm_conf_load_ini_file(char * filename)1623 int fpm_conf_load_ini_file(char *filename) /* {{{ */
1624 {
1625 	int error = 0;
1626 	char *buf = NULL, *newbuf = NULL;
1627 	int bufsize = 0;
1628 	int fd, n;
1629 	int nb_read = 1;
1630 	char c = '*';
1631 
1632 	int ret = 1;
1633 
1634 	if (!filename || !filename[0]) {
1635 		zlog(ZLOG_ERROR, "configuration filename is empty");
1636 		return -1;
1637 	}
1638 
1639 	fd = open(filename, O_RDONLY, 0);
1640 	if (fd < 0) {
1641 		zlog(ZLOG_SYSERROR, "failed to open configuration file '%s'", filename);
1642 		return -1;
1643 	}
1644 
1645 	if (ini_recursion++ > 4) {
1646 		zlog(ZLOG_ERROR, "failed to include more than 5 files recursively");
1647 		close(fd);
1648 		return -1;
1649 	}
1650 
1651 	ini_lineno = 0;
1652 	while (nb_read > 0) {
1653 		int tmp;
1654 		ini_lineno++;
1655 		ini_filename = filename;
1656 		for (n = 0; (nb_read = read(fd, &c, sizeof(char))) == sizeof(char) && c != '\n'; n++) {
1657 			if (n == bufsize) {
1658 				bufsize += 1024;
1659 				newbuf = (char*) realloc(buf, sizeof(char) * (bufsize + 2));
1660 				if (newbuf == NULL) {
1661 					ini_recursion--;
1662 					close(fd);
1663 					free(buf);
1664 					return -1;
1665 				}
1666 				buf = newbuf;
1667 			}
1668 
1669 			buf[n] = c;
1670 		}
1671 		if (n == 0) {
1672 			continue;
1673 		}
1674 		/* always append newline and null terminate */
1675 		buf[n++] = '\n';
1676 		buf[n] = '\0';
1677 		tmp = zend_parse_ini_string(buf, 1, ZEND_INI_SCANNER_NORMAL, (zend_ini_parser_cb_t)fpm_conf_ini_parser, &error);
1678 		ini_filename = filename;
1679 		if (error || tmp == FAILURE) {
1680 			if (ini_include) {
1681 				free(ini_include);
1682 				ini_include = NULL;
1683 			}
1684 			ini_recursion--;
1685 			close(fd);
1686 			free(buf);
1687 			return -1;
1688 		}
1689 		if (ini_include) {
1690 			char *tmp = ini_include;
1691 			ini_include = NULL;
1692 			fpm_evaluate_full_path(&tmp, NULL, NULL, 0);
1693 			fpm_conf_ini_parser_include(tmp, &error);
1694 			if (error) {
1695 				free(tmp);
1696 				ini_recursion--;
1697 				close(fd);
1698 				free(buf);
1699 				return -1;
1700 			}
1701 			free(tmp);
1702 		}
1703 	}
1704 	free(buf);
1705 
1706 	ini_recursion--;
1707 	close(fd);
1708 	return ret;
1709 }
1710 /* }}} */
1711 
fpm_conf_dump(void)1712 static void fpm_conf_dump(void)
1713 {
1714 	struct fpm_worker_pool_s *wp;
1715 
1716 	/*
1717 	 * Please keep the same order as in fpm_conf.h and in php-fpm.conf.in
1718 	 */
1719 	zlog(ZLOG_NOTICE, "[global]");
1720 	zlog(ZLOG_NOTICE, "\tpid = %s",                         STR2STR(fpm_global_config.pid_file));
1721 	zlog(ZLOG_NOTICE, "\terror_log = %s",                   STR2STR(fpm_global_config.error_log));
1722 #ifdef HAVE_SYSLOG_H
1723 	zlog(ZLOG_NOTICE, "\tsyslog.ident = %s",                STR2STR(fpm_global_config.syslog_ident));
1724 	zlog(ZLOG_NOTICE, "\tsyslog.facility = %d",             fpm_global_config.syslog_facility); /* FIXME: convert to string */
1725 #endif
1726 	zlog(ZLOG_NOTICE, "\tlog_buffering = %s",               BOOL2STR(fpm_global_config.log_buffering));
1727 	zlog(ZLOG_NOTICE, "\tlog_level = %s",                   zlog_get_level_name(fpm_globals.log_level));
1728 	zlog(ZLOG_NOTICE, "\tlog_limit = %d",                   fpm_global_config.log_limit);
1729 	zlog(ZLOG_NOTICE, "\temergency_restart_interval = %ds", fpm_global_config.emergency_restart_interval);
1730 	zlog(ZLOG_NOTICE, "\temergency_restart_threshold = %d", fpm_global_config.emergency_restart_threshold);
1731 	zlog(ZLOG_NOTICE, "\tprocess_control_timeout = %ds",    fpm_global_config.process_control_timeout);
1732 	zlog(ZLOG_NOTICE, "\tprocess.max = %d",                 fpm_global_config.process_max);
1733 	if (fpm_global_config.process_priority == 64) {
1734 		zlog(ZLOG_NOTICE, "\tprocess.priority = undefined");
1735 	} else {
1736 		zlog(ZLOG_NOTICE, "\tprocess.priority = %d", fpm_global_config.process_priority);
1737 	}
1738 	zlog(ZLOG_NOTICE, "\tdaemonize = %s",                   BOOL2STR(fpm_global_config.daemonize));
1739 	zlog(ZLOG_NOTICE, "\trlimit_files = %d",                fpm_global_config.rlimit_files);
1740 	zlog(ZLOG_NOTICE, "\trlimit_core = %d",                 fpm_global_config.rlimit_core);
1741 	zlog(ZLOG_NOTICE, "\tevents.mechanism = %s",            fpm_event_mechanism_name());
1742 #ifdef HAVE_SYSTEMD
1743 	zlog(ZLOG_NOTICE, "\tsystemd_interval = %ds",           fpm_global_config.systemd_interval/1000);
1744 #endif
1745 	zlog(ZLOG_NOTICE, " ");
1746 
1747 	for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
1748 		struct key_value_s *kv;
1749 
1750 		if (!wp->config || wp->shared) {
1751 			continue;
1752 		}
1753 
1754 		zlog(ZLOG_NOTICE, "[%s]",                              STR2STR(wp->config->name));
1755 		zlog(ZLOG_NOTICE, "\tprefix = %s",                     STR2STR(wp->config->prefix));
1756 		zlog(ZLOG_NOTICE, "\tuser = %s",                       STR2STR(wp->config->user));
1757 		zlog(ZLOG_NOTICE, "\tgroup = %s",                      STR2STR(wp->config->group));
1758 		zlog(ZLOG_NOTICE, "\tlisten = %s",                     STR2STR(wp->config->listen_address));
1759 		zlog(ZLOG_NOTICE, "\tlisten.backlog = %d",             wp->config->listen_backlog);
1760 #ifdef HAVE_FPM_ACL
1761 		zlog(ZLOG_NOTICE, "\tlisten.acl_users = %s",           STR2STR(wp->config->listen_acl_users));
1762 		zlog(ZLOG_NOTICE, "\tlisten.acl_groups = %s",          STR2STR(wp->config->listen_acl_groups));
1763 #endif
1764 		zlog(ZLOG_NOTICE, "\tlisten.owner = %s",               STR2STR(wp->config->listen_owner));
1765 		zlog(ZLOG_NOTICE, "\tlisten.group = %s",               STR2STR(wp->config->listen_group));
1766 		zlog(ZLOG_NOTICE, "\tlisten.mode = %s",                STR2STR(wp->config->listen_mode));
1767 		zlog(ZLOG_NOTICE, "\tlisten.allowed_clients = %s",     STR2STR(wp->config->listen_allowed_clients));
1768 #ifdef SO_SETFIB
1769 		zlog(ZLOG_NOTICE, "\tlisten.setfib = %d",              wp->config->listen_setfib);
1770 #endif
1771 		if (wp->config->process_priority == 64) {
1772 			zlog(ZLOG_NOTICE, "\tprocess.priority = undefined");
1773 		} else {
1774 			zlog(ZLOG_NOTICE, "\tprocess.priority = %d", wp->config->process_priority);
1775 		}
1776 		zlog(ZLOG_NOTICE, "\tprocess.dumpable = %s",           BOOL2STR(wp->config->process_dumpable));
1777 		zlog(ZLOG_NOTICE, "\tpm = %s",                         PM2STR(wp->config->pm));
1778 		zlog(ZLOG_NOTICE, "\tpm.max_children = %d",            wp->config->pm_max_children);
1779 		zlog(ZLOG_NOTICE, "\tpm.start_servers = %d",           wp->config->pm_start_servers);
1780 		zlog(ZLOG_NOTICE, "\tpm.min_spare_servers = %d",       wp->config->pm_min_spare_servers);
1781 		zlog(ZLOG_NOTICE, "\tpm.max_spare_servers = %d",       wp->config->pm_max_spare_servers);
1782 		zlog(ZLOG_NOTICE, "\tpm.max_spawn_rate = %d",          wp->config->pm_max_spawn_rate);
1783 		zlog(ZLOG_NOTICE, "\tpm.process_idle_timeout = %d",    wp->config->pm_process_idle_timeout);
1784 		zlog(ZLOG_NOTICE, "\tpm.max_requests = %d",            wp->config->pm_max_requests);
1785 		zlog(ZLOG_NOTICE, "\tpm.status_path = %s",             STR2STR(wp->config->pm_status_path));
1786 		zlog(ZLOG_NOTICE, "\tpm.status_listen = %s",           STR2STR(wp->config->pm_status_listen));
1787 		zlog(ZLOG_NOTICE, "\tping.path = %s",                  STR2STR(wp->config->ping_path));
1788 		zlog(ZLOG_NOTICE, "\tping.response = %s",              STR2STR(wp->config->ping_response));
1789 		zlog(ZLOG_NOTICE, "\taccess.log = %s",                 STR2STR(wp->config->access_log));
1790 		zlog(ZLOG_NOTICE, "\taccess.format = %s",              STR2STR(wp->config->access_format));
1791 		for (kv = wp->config->access_suppress_paths; kv; kv = kv->next) {
1792 			zlog(ZLOG_NOTICE, "\taccess.suppress_path[] = %s", kv->value);
1793 		}
1794 		zlog(ZLOG_NOTICE, "\tslowlog = %s",                    STR2STR(wp->config->slowlog));
1795 		zlog(ZLOG_NOTICE, "\trequest_slowlog_timeout = %ds",   wp->config->request_slowlog_timeout);
1796 		zlog(ZLOG_NOTICE, "\trequest_slowlog_trace_depth = %d", wp->config->request_slowlog_trace_depth);
1797 		zlog(ZLOG_NOTICE, "\trequest_terminate_timeout = %ds", wp->config->request_terminate_timeout);
1798 		zlog(ZLOG_NOTICE, "\trequest_terminate_timeout_track_finished = %s", BOOL2STR(wp->config->request_terminate_timeout_track_finished));
1799 		zlog(ZLOG_NOTICE, "\trlimit_files = %d",               wp->config->rlimit_files);
1800 		zlog(ZLOG_NOTICE, "\trlimit_core = %d",                wp->config->rlimit_core);
1801 		zlog(ZLOG_NOTICE, "\tchroot = %s",                     STR2STR(wp->config->chroot));
1802 		zlog(ZLOG_NOTICE, "\tchdir = %s",                      STR2STR(wp->config->chdir));
1803 		zlog(ZLOG_NOTICE, "\tcatch_workers_output = %s",       BOOL2STR(wp->config->catch_workers_output));
1804 		zlog(ZLOG_NOTICE, "\tdecorate_workers_output = %s",    BOOL2STR(wp->config->decorate_workers_output));
1805 		zlog(ZLOG_NOTICE, "\tclear_env = %s",                  BOOL2STR(wp->config->clear_env));
1806 		zlog(ZLOG_NOTICE, "\tsecurity.limit_extensions = %s",  wp->config->security_limit_extensions);
1807 
1808 		for (kv = wp->config->env; kv; kv = kv->next) {
1809 			zlog(ZLOG_NOTICE, "\tenv[%s] = %s", kv->key, kv->value);
1810 		}
1811 
1812 		for (kv = wp->config->php_values; kv; kv = kv->next) {
1813 			zlog(ZLOG_NOTICE, "\tphp_value[%s] = %s", kv->key, kv->value);
1814 		}
1815 
1816 		for (kv = wp->config->php_admin_values; kv; kv = kv->next) {
1817 			zlog(ZLOG_NOTICE, "\tphp_admin_value[%s] = %s", kv->key, kv->value);
1818 		}
1819 		zlog(ZLOG_NOTICE, " ");
1820 	}
1821 }
1822 
fpm_conf_init_main(int test_conf,int force_daemon)1823 int fpm_conf_init_main(int test_conf, int force_daemon) /* {{{ */
1824 {
1825 	int ret;
1826 
1827 	if (fpm_globals.prefix && *fpm_globals.prefix) {
1828 		if (!fpm_conf_is_dir(fpm_globals.prefix)) {
1829 			zlog(ZLOG_ERROR, "the global prefix '%s' does not exist or is not a directory", fpm_globals.prefix);
1830 			return -1;
1831 		}
1832 	}
1833 
1834 	if (fpm_globals.pid && *fpm_globals.pid) {
1835 		fpm_global_config.pid_file = strdup(fpm_globals.pid);
1836 	}
1837 
1838 	if (fpm_globals.config == NULL) {
1839 		char *tmp;
1840 
1841 		if (fpm_globals.prefix == NULL) {
1842 			spprintf(&tmp, 0, "%s/php-fpm.conf", PHP_SYSCONFDIR);
1843 		} else {
1844 			spprintf(&tmp, 0, "%s/etc/php-fpm.conf", fpm_globals.prefix);
1845 		}
1846 
1847 		if (!tmp) {
1848 			zlog(ZLOG_SYSERROR, "spprintf() failed (tmp for fpm_globals.config)");
1849 			return -1;
1850 		}
1851 
1852 		fpm_globals.config = strdup(tmp);
1853 		efree(tmp);
1854 
1855 		if (!fpm_globals.config) {
1856 			zlog(ZLOG_SYSERROR, "spprintf() failed (fpm_globals.config)");
1857 			return -1;
1858 		}
1859 	}
1860 
1861 	ret = fpm_conf_load_ini_file(fpm_globals.config);
1862 
1863 	if (0 > ret) {
1864 		zlog(ZLOG_ERROR, "failed to load configuration file '%s'", fpm_globals.config);
1865 		return -1;
1866 	}
1867 
1868 	if (0 > fpm_conf_post_process(force_daemon)) {
1869 		zlog(ZLOG_ERROR, "failed to post process the configuration");
1870 		return -1;
1871 	}
1872 
1873 	if (test_conf) {
1874 		for (struct fpm_worker_pool_s *wp = fpm_worker_all_pools; wp; wp = wp->next) {
1875 			if (!fpm_unix_test_config(wp)) {
1876 				return -1;
1877 			}
1878 		}
1879 
1880 		if (test_conf > 1) {
1881 			fpm_conf_dump();
1882 		}
1883 		zlog(ZLOG_NOTICE, "configuration file %s test is successful\n", fpm_globals.config);
1884 		fpm_globals.test_successful = 1;
1885 		return -1;
1886 	}
1887 
1888 	if (0 > fpm_cleanup_add(FPM_CLEANUP_ALL, fpm_conf_cleanup, 0)) {
1889 		return -1;
1890 	}
1891 
1892 	return 0;
1893 }
1894 /* }}} */
1895