xref: /PHP-8.2/ext/pcntl/pcntl.c (revision 1bdb0fdd)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Author: Jason Greene <jason@inetgurus.net>                           |
14    +----------------------------------------------------------------------+
15  */
16 
17 #define PCNTL_DEBUG 0
18 
19 #if PCNTL_DEBUG
20 #define DEBUG_OUT printf("DEBUG: ");printf
21 #define IF_DEBUG(z) z
22 #else
23 #define IF_DEBUG(z)
24 #endif
25 
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 
30 #include "php.h"
31 #include "php_ini.h"
32 #include "ext/standard/info.h"
33 #include "php_pcntl.h"
34 #include "php_signal.h"
35 #include "php_ticks.h"
36 #include "zend_fibers.h"
37 
38 #if defined(HAVE_GETPRIORITY) || defined(HAVE_SETPRIORITY) || defined(HAVE_WAIT3)
39 #include <sys/wait.h>
40 #include <sys/time.h>
41 #include <sys/resource.h>
42 #endif
43 
44 #include <errno.h>
45 #ifdef HAVE_UNSHARE
46 #include <sched.h>
47 #endif
48 
49 #ifdef HAVE_FORKX
50 #include <sys/fork.h>
51 #endif
52 
53 #ifndef NSIG
54 # define NSIG 32
55 #endif
56 
57 #define LONG_CONST(c) (zend_long) c
58 
59 #include "pcntl_arginfo.h"
60 
61 #include "Zend/zend_max_execution_timer.h"
62 
63 ZEND_DECLARE_MODULE_GLOBALS(pcntl)
64 static PHP_GINIT_FUNCTION(pcntl);
65 
66 zend_module_entry pcntl_module_entry = {
67 	STANDARD_MODULE_HEADER,
68 	"pcntl",
69 	ext_functions,
70 	PHP_MINIT(pcntl),
71 	PHP_MSHUTDOWN(pcntl),
72 	PHP_RINIT(pcntl),
73 	PHP_RSHUTDOWN(pcntl),
74 	PHP_MINFO(pcntl),
75 	PHP_PCNTL_VERSION,
76 	PHP_MODULE_GLOBALS(pcntl),
77 	PHP_GINIT(pcntl),
78 	NULL,
79 	NULL,
80 	STANDARD_MODULE_PROPERTIES_EX
81 };
82 
83 #ifdef COMPILE_DL_PCNTL
84 #ifdef ZTS
85 ZEND_TSRMLS_CACHE_DEFINE()
86 #endif
87 ZEND_GET_MODULE(pcntl)
88 #endif
89 
90 static void (*orig_interrupt_function)(zend_execute_data *execute_data);
91 
92 #ifdef HAVE_STRUCT_SIGINFO_T
93 static void pcntl_signal_handler(int, siginfo_t*, void*);
94 static void pcntl_siginfo_to_zval(int, siginfo_t*, zval*);
95 #else
96 static void pcntl_signal_handler(int);
97 #endif
98 static void pcntl_signal_dispatch(void);
99 static void pcntl_signal_dispatch_tick_function(int dummy_int, void *dummy_pointer);
100 static void pcntl_interrupt_function(zend_execute_data *execute_data);
101 
PHP_GINIT_FUNCTION(pcntl)102 static PHP_GINIT_FUNCTION(pcntl)
103 {
104 #if defined(COMPILE_DL_PCNTL) && defined(ZTS)
105 	ZEND_TSRMLS_CACHE_UPDATE();
106 #endif
107 	memset(pcntl_globals, 0, sizeof(*pcntl_globals));
108 }
109 
PHP_RINIT_FUNCTION(pcntl)110 PHP_RINIT_FUNCTION(pcntl)
111 {
112 	php_add_tick_function(pcntl_signal_dispatch_tick_function, NULL);
113 	zend_hash_init(&PCNTL_G(php_signal_table), 16, NULL, ZVAL_PTR_DTOR, 0);
114 	PCNTL_G(head) = PCNTL_G(tail) = PCNTL_G(spares) = NULL;
115 	PCNTL_G(async_signals) = 0;
116 	PCNTL_G(last_error) = 0;
117 	PCNTL_G(num_signals) = NSIG;
118 #ifdef SIGRTMAX
119 	/* At least FreeBSD reports an incorrecrt NSIG that does not include realtime signals.
120 	 * As SIGRTMAX may be a dynamic value, adjust the value in INIT. */
121 	if (NSIG < SIGRTMAX + 1) {
122 		PCNTL_G(num_signals) = SIGRTMAX + 1;
123 	}
124 #endif
125 	return SUCCESS;
126 }
127 
PHP_MINIT_FUNCTION(pcntl)128 PHP_MINIT_FUNCTION(pcntl)
129 {
130 	register_pcntl_symbols(module_number);
131 	orig_interrupt_function = zend_interrupt_function;
132 	zend_interrupt_function = pcntl_interrupt_function;
133 
134 	return SUCCESS;
135 }
136 
PHP_MSHUTDOWN_FUNCTION(pcntl)137 PHP_MSHUTDOWN_FUNCTION(pcntl)
138 {
139 	return SUCCESS;
140 }
141 
PHP_RSHUTDOWN_FUNCTION(pcntl)142 PHP_RSHUTDOWN_FUNCTION(pcntl)
143 {
144 	struct php_pcntl_pending_signal *sig;
145 	zend_long signo;
146 	zval *handle;
147 
148 	/* Reset all signals to their default disposition */
149 	ZEND_HASH_FOREACH_NUM_KEY_VAL(&PCNTL_G(php_signal_table), signo, handle) {
150 		if (Z_TYPE_P(handle) != IS_LONG || Z_LVAL_P(handle) != (zend_long)SIG_DFL) {
151 			php_signal(signo, (Sigfunc *)(zend_long)SIG_DFL, 0);
152 		}
153 	} ZEND_HASH_FOREACH_END();
154 
155 	zend_hash_destroy(&PCNTL_G(php_signal_table));
156 
157 	while (PCNTL_G(head)) {
158 		sig = PCNTL_G(head);
159 		PCNTL_G(head) = sig->next;
160 		efree(sig);
161 	}
162 	while (PCNTL_G(spares)) {
163 		sig = PCNTL_G(spares);
164 		PCNTL_G(spares) = sig->next;
165 		efree(sig);
166 	}
167 
168 	return SUCCESS;
169 }
170 
PHP_MINFO_FUNCTION(pcntl)171 PHP_MINFO_FUNCTION(pcntl)
172 {
173 	php_info_print_table_start();
174 	php_info_print_table_row(2, "pcntl support", "enabled");
175 	php_info_print_table_end();
176 }
177 
178 /* {{{ Forks the currently running process following the same behavior as the UNIX fork() system call*/
PHP_FUNCTION(pcntl_fork)179 PHP_FUNCTION(pcntl_fork)
180 {
181 	pid_t id;
182 
183 	ZEND_PARSE_PARAMETERS_NONE();
184 
185 	id = fork();
186 	if (id == -1) {
187 		PCNTL_G(last_error) = errno;
188 		php_error_docref(NULL, E_WARNING, "Error %d", errno);
189 	} else if (id == 0) {
190 		zend_max_execution_timer_init();
191 	}
192 
193 	RETURN_LONG((zend_long) id);
194 }
195 /* }}} */
196 
197 /* {{{ Set an alarm clock for delivery of a signal*/
PHP_FUNCTION(pcntl_alarm)198 PHP_FUNCTION(pcntl_alarm)
199 {
200 	zend_long seconds;
201 
202 	ZEND_PARSE_PARAMETERS_START(1, 1)
203 		Z_PARAM_LONG(seconds);
204 	ZEND_PARSE_PARAMETERS_END();
205 
206 	RETURN_LONG((zend_long) alarm(seconds));
207 }
208 /* }}} */
209 
210 #define PHP_RUSAGE_PARA(from, to, field) \
211 	add_assoc_long(to, #field, from.field)
212 #ifndef _OSD_POSIX
213 	#define PHP_RUSAGE_SPECIAL(from, to) \
214 		PHP_RUSAGE_PARA(from, to, ru_oublock); \
215 		PHP_RUSAGE_PARA(from, to, ru_inblock); \
216 		PHP_RUSAGE_PARA(from, to, ru_msgsnd); \
217 		PHP_RUSAGE_PARA(from, to, ru_msgrcv); \
218 		PHP_RUSAGE_PARA(from, to, ru_maxrss); \
219 		PHP_RUSAGE_PARA(from, to, ru_ixrss); \
220 		PHP_RUSAGE_PARA(from, to, ru_idrss); \
221 		PHP_RUSAGE_PARA(from, to, ru_minflt); \
222 		PHP_RUSAGE_PARA(from, to, ru_majflt); \
223 		PHP_RUSAGE_PARA(from, to, ru_nsignals); \
224 		PHP_RUSAGE_PARA(from, to, ru_nvcsw); \
225 		PHP_RUSAGE_PARA(from, to, ru_nivcsw); \
226 		PHP_RUSAGE_PARA(from, to, ru_nswap);
227 #else /*_OSD_POSIX*/
228 	#define PHP_RUSAGE_SPECIAL(from, to)
229 #endif
230 
231 #define PHP_RUSAGE_COMMON(from ,to) \
232 	PHP_RUSAGE_PARA(from, to, ru_utime.tv_usec); \
233 	PHP_RUSAGE_PARA(from, to, ru_utime.tv_sec); \
234 	PHP_RUSAGE_PARA(from, to, ru_stime.tv_usec); \
235 	PHP_RUSAGE_PARA(from, to, ru_stime.tv_sec);
236 
237 #define PHP_RUSAGE_TO_ARRAY(from, to) \
238 	if (to) { \
239 		PHP_RUSAGE_SPECIAL(from, to) \
240 		PHP_RUSAGE_COMMON(from, to); \
241 	}
242 
243 /* {{{ Waits on or returns the status of a forked child as defined by the waitpid() system call */
PHP_FUNCTION(pcntl_waitpid)244 PHP_FUNCTION(pcntl_waitpid)
245 {
246 	zend_long pid, options = 0;
247 	zval *z_status = NULL, *z_rusage = NULL;
248 	int status;
249 	pid_t child_id;
250 #ifdef HAVE_WAIT4
251 	struct rusage rusage;
252 #endif
253 
254 	ZEND_PARSE_PARAMETERS_START(2, 4)
255 		Z_PARAM_LONG(pid)
256 		Z_PARAM_ZVAL(z_status)
257 		Z_PARAM_OPTIONAL
258 		Z_PARAM_LONG(options)
259 		Z_PARAM_ZVAL(z_rusage)
260 	ZEND_PARSE_PARAMETERS_END();
261 
262 	status = zval_get_long(z_status);
263 
264 #ifdef HAVE_WAIT4
265 	if (z_rusage) {
266 		z_rusage = zend_try_array_init(z_rusage);
267 		if (!z_rusage) {
268 			RETURN_THROWS();
269 		}
270 
271 		memset(&rusage, 0, sizeof(struct rusage));
272 		child_id = wait4((pid_t) pid, &status, options, &rusage);
273 	} else {
274 		child_id = waitpid((pid_t) pid, &status, options);
275 	}
276 #else
277 	child_id = waitpid((pid_t) pid, &status, options);
278 #endif
279 
280 	if (child_id < 0) {
281 		PCNTL_G(last_error) = errno;
282 	}
283 
284 #ifdef HAVE_WAIT4
285 	if (child_id > 0) {
286 		PHP_RUSAGE_TO_ARRAY(rusage, z_rusage);
287 	}
288 #endif
289 
290 	ZEND_TRY_ASSIGN_REF_LONG(z_status, status);
291 
292 	RETURN_LONG((zend_long) child_id);
293 }
294 /* }}} */
295 
296 /* {{{ Waits on or returns the status of a forked child as defined by the waitpid() system call */
PHP_FUNCTION(pcntl_wait)297 PHP_FUNCTION(pcntl_wait)
298 {
299 	zend_long options = 0;
300 	zval *z_status = NULL, *z_rusage = NULL;
301 	int status;
302 	pid_t child_id;
303 #ifdef HAVE_WAIT3
304 	struct rusage rusage;
305 #endif
306 
307 	ZEND_PARSE_PARAMETERS_START(1, 3)
308 		Z_PARAM_ZVAL(z_status)
309 		Z_PARAM_OPTIONAL
310 		Z_PARAM_LONG(options)
311 		Z_PARAM_ZVAL(z_rusage)
312 	ZEND_PARSE_PARAMETERS_END();
313 
314 	status = zval_get_long(z_status);
315 #ifdef HAVE_WAIT3
316 	if (z_rusage) {
317 		z_rusage = zend_try_array_init(z_rusage);
318 		if (!z_rusage) {
319 			RETURN_THROWS();
320 		}
321 
322 		memset(&rusage, 0, sizeof(struct rusage));
323 		child_id = wait3(&status, options, &rusage);
324 	} else if (options) {
325 		child_id = wait3(&status, options, NULL);
326 	} else {
327 		child_id = wait(&status);
328 	}
329 #else
330 	child_id = wait(&status);
331 #endif
332 	if (child_id < 0) {
333 		PCNTL_G(last_error) = errno;
334 	}
335 
336 #ifdef HAVE_WAIT3
337 	if (child_id > 0) {
338 		PHP_RUSAGE_TO_ARRAY(rusage, z_rusage);
339 	}
340 #endif
341 
342 	ZEND_TRY_ASSIGN_REF_LONG(z_status, status);
343 
344 	RETURN_LONG((zend_long) child_id);
345 }
346 /* }}} */
347 
348 #undef PHP_RUSAGE_PARA
349 #undef PHP_RUSAGE_SPECIAL
350 #undef PHP_RUSAGE_COMMON
351 #undef PHP_RUSAGE_TO_ARRAY
352 
353 /* {{{ Returns true if the child status code represents a successful exit */
PHP_FUNCTION(pcntl_wifexited)354 PHP_FUNCTION(pcntl_wifexited)
355 {
356 	zend_long status_word;
357 
358 	ZEND_PARSE_PARAMETERS_START(1, 1)
359 		Z_PARAM_LONG(status_word)
360 	ZEND_PARSE_PARAMETERS_END();
361 
362 #ifdef WIFEXITED
363 	int int_status_word = (int) status_word;
364 	if (WIFEXITED(int_status_word)) {
365 		RETURN_TRUE;
366 	}
367 #endif
368 
369 	RETURN_FALSE;
370 }
371 /* }}} */
372 
373 /* {{{ Returns true if the child status code represents a stopped process (WUNTRACED must have been used with waitpid) */
PHP_FUNCTION(pcntl_wifstopped)374 PHP_FUNCTION(pcntl_wifstopped)
375 {
376 	zend_long status_word;
377 
378 	ZEND_PARSE_PARAMETERS_START(1, 1)
379 		Z_PARAM_LONG(status_word)
380 	ZEND_PARSE_PARAMETERS_END();
381 
382 #ifdef WIFSTOPPED
383 	int int_status_word = (int) status_word;
384 	if (WIFSTOPPED(int_status_word)) {
385 		RETURN_TRUE;
386 	}
387 #endif
388 
389 	RETURN_FALSE;
390 }
391 /* }}} */
392 
393 /* {{{ Returns true if the child status code represents a process that was terminated due to a signal */
PHP_FUNCTION(pcntl_wifsignaled)394 PHP_FUNCTION(pcntl_wifsignaled)
395 {
396 	zend_long status_word;
397 
398 	ZEND_PARSE_PARAMETERS_START(1, 1)
399 		Z_PARAM_LONG(status_word)
400 	ZEND_PARSE_PARAMETERS_END();
401 
402 #ifdef WIFSIGNALED
403 	int int_status_word = (int) status_word;
404 	if (WIFSIGNALED(int_status_word)) {
405 		RETURN_TRUE;
406 	}
407 #endif
408 
409 	RETURN_FALSE;
410 }
411 /* }}} */
412 
413 /* {{{ Returns true if the child status code represents a process that was resumed due to a SIGCONT signal */
PHP_FUNCTION(pcntl_wifcontinued)414 PHP_FUNCTION(pcntl_wifcontinued)
415 {
416 	zend_long status_word;
417 
418 	ZEND_PARSE_PARAMETERS_START(1, 1)
419 		Z_PARAM_LONG(status_word)
420 	ZEND_PARSE_PARAMETERS_END();
421 
422 #ifdef HAVE_WCONTINUED
423 	int int_status_word = (int) status_word;
424 	if (WIFCONTINUED(int_status_word)) {
425 		RETURN_TRUE;
426 	}
427 #endif
428 	RETURN_FALSE;
429 }
430 /* }}} */
431 
432 
433 /* {{{ Returns the status code of a child's exit */
PHP_FUNCTION(pcntl_wexitstatus)434 PHP_FUNCTION(pcntl_wexitstatus)
435 {
436 	zend_long status_word;
437 
438 	ZEND_PARSE_PARAMETERS_START(1, 1)
439 		Z_PARAM_LONG(status_word)
440 	ZEND_PARSE_PARAMETERS_END();
441 
442 #ifdef WEXITSTATUS
443 	int int_status_word = (int) status_word;
444 	RETURN_LONG(WEXITSTATUS(int_status_word));
445 #else
446 	RETURN_FALSE;
447 #endif
448 }
449 /* }}} */
450 
451 /* {{{ Returns the number of the signal that terminated the process who's status code is passed  */
PHP_FUNCTION(pcntl_wtermsig)452 PHP_FUNCTION(pcntl_wtermsig)
453 {
454 	zend_long status_word;
455 
456 	ZEND_PARSE_PARAMETERS_START(1, 1)
457 		Z_PARAM_LONG(status_word)
458 	ZEND_PARSE_PARAMETERS_END();
459 
460 #ifdef WTERMSIG
461 	int int_status_word = (int) status_word;
462 	RETURN_LONG(WTERMSIG(int_status_word));
463 #else
464 	RETURN_FALSE;
465 #endif
466 }
467 /* }}} */
468 
469 /* {{{ Returns the number of the signal that caused the process to stop who's status code is passed */
PHP_FUNCTION(pcntl_wstopsig)470 PHP_FUNCTION(pcntl_wstopsig)
471 {
472 	zend_long status_word;
473 
474 	ZEND_PARSE_PARAMETERS_START(1, 1)
475 		Z_PARAM_LONG(status_word)
476 	ZEND_PARSE_PARAMETERS_END();
477 
478 #ifdef WSTOPSIG
479 	int int_status_word = (int) status_word;
480 	RETURN_LONG(WSTOPSIG(int_status_word));
481 #else
482 	RETURN_FALSE;
483 #endif
484 }
485 /* }}} */
486 
487 /* {{{ Executes specified program in current process space as defined by exec(2) */
PHP_FUNCTION(pcntl_exec)488 PHP_FUNCTION(pcntl_exec)
489 {
490 	zval *args = NULL, *envs = NULL;
491 	zval *element;
492 	HashTable *args_hash, *envs_hash;
493 	int argc = 0, argi = 0;
494 	int envc = 0, envi = 0;
495 	char **argv = NULL, **envp = NULL;
496 	char **current_arg, **pair;
497 	size_t pair_length;
498 	zend_string *key;
499 	char *path;
500 	size_t path_len;
501 	zend_ulong key_num;
502 
503 	ZEND_PARSE_PARAMETERS_START(1, 3)
504 		Z_PARAM_PATH(path, path_len)
505 		Z_PARAM_OPTIONAL
506 		Z_PARAM_ARRAY(args)
507 		Z_PARAM_ARRAY(envs)
508 	ZEND_PARSE_PARAMETERS_END();
509 
510 	if (ZEND_NUM_ARGS() > 1) {
511 		/* Build argument list */
512 		SEPARATE_ARRAY(args);
513 		args_hash = Z_ARRVAL_P(args);
514 		argc = zend_hash_num_elements(args_hash);
515 
516 		argv = safe_emalloc((argc + 2), sizeof(char *), 0);
517 		*argv = path;
518 		current_arg = argv+1;
519 		ZEND_HASH_FOREACH_VAL(args_hash, element) {
520 			if (argi >= argc) break;
521 			if (!try_convert_to_string(element)) {
522 				efree(argv);
523 				RETURN_THROWS();
524 			}
525 
526 			*current_arg = Z_STRVAL_P(element);
527 			argi++;
528 			current_arg++;
529 		} ZEND_HASH_FOREACH_END();
530 		*current_arg = NULL;
531 	} else {
532 		argv = emalloc(2 * sizeof(char *));
533 		argv[0] = path;
534 		argv[1] = NULL;
535 	}
536 
537 	if ( ZEND_NUM_ARGS() == 3 ) {
538 		/* Build environment pair list */
539 		SEPARATE_ARRAY(envs);
540 		envs_hash = Z_ARRVAL_P(envs);
541 		envc = zend_hash_num_elements(envs_hash);
542 
543 		pair = envp = safe_emalloc((envc + 1), sizeof(char *), 0);
544 		ZEND_HASH_FOREACH_KEY_VAL(envs_hash, key_num, key, element) {
545 			if (envi >= envc) break;
546 			if (!key) {
547 				key = zend_long_to_str(key_num);
548 			} else {
549 				zend_string_addref(key);
550 			}
551 
552 			if (!try_convert_to_string(element)) {
553 				zend_string_release(key);
554 				efree(argv);
555 				efree(envp);
556 				RETURN_THROWS();
557 			}
558 
559 			/* Length of element + equal sign + length of key + null */
560 			ZEND_ASSERT(Z_STRLEN_P(element) < SIZE_MAX && ZSTR_LEN(key) < SIZE_MAX);
561 			*pair = safe_emalloc(Z_STRLEN_P(element) + 1, sizeof(char), ZSTR_LEN(key) + 1);
562 			pair_length = Z_STRLEN_P(element) + ZSTR_LEN(key) + 2;
563 			strlcpy(*pair, ZSTR_VAL(key), ZSTR_LEN(key) + 1);
564 			strlcat(*pair, "=", pair_length);
565 			strlcat(*pair, Z_STRVAL_P(element), pair_length);
566 
567 			/* Cleanup */
568 			zend_string_release_ex(key, 0);
569 			envi++;
570 			pair++;
571 		} ZEND_HASH_FOREACH_END();
572 		*(pair) = NULL;
573 
574 		if (execve(path, argv, envp) == -1) {
575 			PCNTL_G(last_error) = errno;
576 			php_error_docref(NULL, E_WARNING, "Error has occurred: (errno %d) %s", errno, strerror(errno));
577 		}
578 
579 		/* Cleanup */
580 		for (pair = envp; *pair != NULL; pair++) efree(*pair);
581 		efree(envp);
582 	} else {
583 
584 		if (execv(path, argv) == -1) {
585 			PCNTL_G(last_error) = errno;
586 			php_error_docref(NULL, E_WARNING, "Error has occurred: (errno %d) %s", errno, strerror(errno));
587 		}
588 	}
589 
590 	efree(argv);
591 
592 	RETURN_FALSE;
593 }
594 /* }}} */
595 
596 /* {{{ Assigns a system signal handler to a PHP function */
PHP_FUNCTION(pcntl_signal)597 PHP_FUNCTION(pcntl_signal)
598 {
599 	zval *handle;
600 	zend_long signo;
601 	bool restart_syscalls = 1;
602 	bool restart_syscalls_is_null = 1;
603 
604 	ZEND_PARSE_PARAMETERS_START(2, 3)
605 		Z_PARAM_LONG(signo)
606 		Z_PARAM_ZVAL(handle)
607 		Z_PARAM_OPTIONAL
608 		Z_PARAM_BOOL_OR_NULL(restart_syscalls, restart_syscalls_is_null)
609 	ZEND_PARSE_PARAMETERS_END();
610 
611 	if (signo < 1) {
612 		zend_argument_value_error(1, "must be greater than or equal to 1");
613 		RETURN_THROWS();
614 	}
615 
616 	if (signo >= PCNTL_G(num_signals)) {
617 		zend_argument_value_error(1, "must be less than %d", PCNTL_G(num_signals));
618 		RETURN_THROWS();
619 	}
620 
621 	if (!PCNTL_G(spares)) {
622 		/* since calling malloc() from within a signal handler is not portable,
623 		 * pre-allocate a few records for recording signals */
624 		int i;
625 		for (i = 0; i < PCNTL_G(num_signals); i++) {
626 			struct php_pcntl_pending_signal *psig;
627 
628 			psig = emalloc(sizeof(*psig));
629 			psig->next = PCNTL_G(spares);
630 			PCNTL_G(spares) = psig;
631 		}
632 	}
633 
634 	/* If restart_syscalls was not explicitly specified and the signal is SIGALRM, then default
635 	 * restart_syscalls to false. PHP used to enforce that restart_syscalls is false for SIGALRM,
636 	 * so we keep this differing default to reduce the degree of BC breakage. */
637 	if (restart_syscalls_is_null && signo == SIGALRM) {
638 		restart_syscalls = 0;
639 	}
640 
641 	/* Special long value case for SIG_DFL and SIG_IGN */
642 	if (Z_TYPE_P(handle) == IS_LONG) {
643 		if (Z_LVAL_P(handle) != (zend_long) SIG_DFL && Z_LVAL_P(handle) != (zend_long) SIG_IGN) {
644 			zend_argument_value_error(2, "must be either SIG_DFL or SIG_IGN when an integer value is given");
645 			RETURN_THROWS();
646 		}
647 		if (php_signal(signo, (Sigfunc *) Z_LVAL_P(handle), (int) restart_syscalls) == (void *)SIG_ERR) {
648 			PCNTL_G(last_error) = errno;
649 			php_error_docref(NULL, E_WARNING, "Error assigning signal");
650 			RETURN_FALSE;
651 		}
652 		zend_hash_index_update(&PCNTL_G(php_signal_table), signo, handle);
653 		RETURN_TRUE;
654 	}
655 
656 	if (!zend_is_callable_ex(handle, NULL, 0, NULL, NULL, NULL)) {
657 		PCNTL_G(last_error) = EINVAL;
658 
659 		zend_argument_type_error(2, "must be of type callable|int, %s given", zend_zval_value_name(handle));
660 		RETURN_THROWS();
661 	}
662 
663 	/* Add the function name to our signal table */
664 	handle = zend_hash_index_update(&PCNTL_G(php_signal_table), signo, handle);
665 	Z_TRY_ADDREF_P(handle);
666 
667 	if (php_signal4(signo, pcntl_signal_handler, (int) restart_syscalls, 1) == (void *)SIG_ERR) {
668 		PCNTL_G(last_error) = errno;
669 		php_error_docref(NULL, E_WARNING, "Error assigning signal");
670 		RETURN_FALSE;
671 	}
672 	RETURN_TRUE;
673 }
674 /* }}} */
675 
676 /* {{{ Gets signal handler */
PHP_FUNCTION(pcntl_signal_get_handler)677 PHP_FUNCTION(pcntl_signal_get_handler)
678 {
679 	zval *prev_handle;
680 	zend_long signo;
681 
682 	ZEND_PARSE_PARAMETERS_START(1, 1)
683 		Z_PARAM_LONG(signo)
684 	ZEND_PARSE_PARAMETERS_END();
685 
686 	if (signo < 1 || signo > 32) {
687 		zend_argument_value_error(1, "must be between 1 and 32");
688 		RETURN_THROWS();
689 	}
690 
691 	if ((prev_handle = zend_hash_index_find(&PCNTL_G(php_signal_table), signo)) != NULL) {
692 		RETURN_COPY(prev_handle);
693 	} else {
694 		RETURN_LONG((zend_long)SIG_DFL);
695 	}
696 }
697 
698 /* {{{ Dispatch signals to signal handlers */
PHP_FUNCTION(pcntl_signal_dispatch)699 PHP_FUNCTION(pcntl_signal_dispatch)
700 {
701 	ZEND_PARSE_PARAMETERS_NONE();
702 
703 	pcntl_signal_dispatch();
704 	RETURN_TRUE;
705 }
706 /* }}} */
707 
708 /* Common helper function for these 3 wrapper functions */
709 #if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT) || defined(HAVE_SIGPROCMASK)
php_pcntl_set_user_signal_infos(HashTable * const user_signals,sigset_t * const set,size_t arg_num,bool allow_empty_signal_array)710 static bool php_pcntl_set_user_signal_infos(
711 	/* const */ HashTable *const user_signals,
712 	sigset_t *const set,
713 	size_t arg_num,
714 	bool allow_empty_signal_array
715 ) {
716 	if (!allow_empty_signal_array && zend_hash_num_elements(user_signals) == 0) {
717 		zend_argument_value_error(arg_num, "cannot be empty");
718 		return false;
719 	}
720 
721 	errno = 0;
722 	if (sigemptyset(set) != 0) {
723 		PCNTL_G(last_error) = errno;
724 		php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
725 		return false;
726 	}
727 
728 	zval *user_signal_no;
729 	ZEND_HASH_FOREACH_VAL(user_signals, user_signal_no) {
730 		bool failed = true;
731 		zend_long tmp = zval_try_get_long(user_signal_no, &failed);
732 
733 		if (failed) {
734 			zend_argument_type_error(arg_num, "signals must be of type int, %s given", zend_zval_value_name(user_signal_no));
735 			return false;
736 		}
737 		/* Signals are positive integers */
738 		if (tmp < 1 || tmp >= PCNTL_G(num_signals)) {
739 			/* PCNTL_G(num_signals) stores +1 from the last valid signal */
740 			zend_argument_value_error(arg_num, "signals must be between 1 and %d", PCNTL_G(num_signals)-1);
741 			return false;
742 		}
743 
744 		int signal_no = (int) tmp;
745 		errno = 0;
746 		if (sigaddset(set, signal_no) != 0) {
747 			PCNTL_G(last_error) = errno;
748 			php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
749 			return false;
750 		}
751 	} ZEND_HASH_FOREACH_END();
752 	return true;
753 }
754 #endif
755 
756 #ifdef HAVE_SIGPROCMASK
757 /* {{{ Examine and change blocked signals */
PHP_FUNCTION(pcntl_sigprocmask)758 PHP_FUNCTION(pcntl_sigprocmask)
759 {
760 	zend_long how;
761 	HashTable *user_set;
762 	/* Optional by-ref out-param array of old signals */
763 	zval *user_old_set = NULL;
764 
765 	ZEND_PARSE_PARAMETERS_START(2, 3)
766 		Z_PARAM_LONG(how)
767 		Z_PARAM_ARRAY_HT(user_set)
768 		Z_PARAM_OPTIONAL
769 		Z_PARAM_ZVAL(user_old_set)
770 	ZEND_PARSE_PARAMETERS_END();
771 
772 	if (how != SIG_BLOCK && how != SIG_UNBLOCK && how != SIG_SETMASK) {
773 		zend_argument_value_error(1, "must be one of SIG_BLOCK, SIG_UNBLOCK, or SIG_SETMASK");
774 		RETURN_THROWS();
775 	}
776 
777 	errno = 0;
778 	sigset_t old_set;
779 	if (sigemptyset(&old_set) != 0) {
780 		PCNTL_G(last_error) = errno;
781 		php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
782 		RETURN_FALSE;
783 	}
784 
785 	sigset_t set;
786 	bool status = php_pcntl_set_user_signal_infos(user_set, &set, 2, /* allow_empty_signal_array */ how == SIG_SETMASK);
787 	/* Some error occurred */
788 	if (!status) {
789 		RETURN_FALSE;
790 	}
791 
792 	if (sigprocmask(how, &set, &old_set) != 0) {
793 		PCNTL_G(last_error) = errno;
794 		php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
795 		RETURN_FALSE;
796 	}
797 
798 	if (user_old_set != NULL) {
799 		user_old_set = zend_try_array_init(user_old_set);
800 		if (!user_old_set) {
801 			RETURN_THROWS();
802 		}
803 
804 		for (int signal_no = 1; signal_no < PCNTL_G(num_signals); ++signal_no) {
805 			if (sigismember(&old_set, signal_no) != 1) {
806 				continue;
807 			}
808 			add_next_index_long(user_old_set, signal_no);
809 		}
810 	}
811 
812 	RETURN_TRUE;
813 }
814 /* }}} */
815 #endif
816 
817 #ifdef HAVE_STRUCT_SIGINFO_T
818 # ifdef HAVE_SIGWAITINFO
819 
820 /* {{{ Synchronously wait for queued signals */
PHP_FUNCTION(pcntl_sigwaitinfo)821 PHP_FUNCTION(pcntl_sigwaitinfo)
822 {
823 	HashTable *user_set;
824 	/* Optional by-ref array of ints */
825 	zval *user_siginfo = NULL;
826 
827 	ZEND_PARSE_PARAMETERS_START(1, 2)
828 		Z_PARAM_ARRAY_HT(user_set)
829 		Z_PARAM_OPTIONAL
830 		Z_PARAM_ZVAL(user_siginfo)
831 	ZEND_PARSE_PARAMETERS_END();
832 
833 	sigset_t set;
834 	bool status = php_pcntl_set_user_signal_infos(user_set, &set, 1, /* allow_empty_signal_array */ false);
835 	/* Some error occurred */
836 	if (!status) {
837 		RETURN_FALSE;
838 	}
839 
840 	errno = 0;
841 	siginfo_t siginfo;
842 	int signal_no = sigwaitinfo(&set, &siginfo);
843 	/* sigwaitinfo() never sets errno to EAGAIN according to POSIX */
844 	if (signal_no == -1) {
845 		PCNTL_G(last_error) = errno;
846 		php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
847 		RETURN_FALSE;
848 	}
849 
850 	/* sigwaitinfo can return 0 on success on some platforms, e.g. NetBSD */
851 	if (!signal_no && siginfo.si_signo) {
852 		signal_no = siginfo.si_signo;
853 	}
854 
855 	pcntl_siginfo_to_zval(signal_no, &siginfo, user_siginfo);
856 
857 	RETURN_LONG(signal_no);
858 }
859 /* }}} */
860 # endif
861 # ifdef HAVE_SIGTIMEDWAIT
862 /* {{{ Wait for queued signals */
PHP_FUNCTION(pcntl_sigtimedwait)863 PHP_FUNCTION(pcntl_sigtimedwait)
864 {
865 	HashTable *user_set;
866 	/* Optional by-ref array of ints */
867 	zval *user_siginfo = NULL;
868 	zend_long tv_sec = 0;
869 	zend_long tv_nsec = 0;
870 
871 	ZEND_PARSE_PARAMETERS_START(1, 4)
872 		Z_PARAM_ARRAY_HT(user_set)
873 		Z_PARAM_OPTIONAL
874 		Z_PARAM_ZVAL(user_siginfo)
875 		Z_PARAM_LONG(tv_sec)
876 		Z_PARAM_LONG(tv_nsec)
877 	ZEND_PARSE_PARAMETERS_END();
878 
879 	sigset_t set;
880 	bool status = php_pcntl_set_user_signal_infos(user_set, &set, 1, /* allow_empty_signal_array */ false);
881 	/* Some error occurred */
882 	if (!status) {
883 		RETURN_FALSE;
884 	}
885 	if (tv_sec < 0) {
886 		zend_argument_value_error(3, "must be greater than or equal to 0");
887 		RETURN_THROWS();
888 	}
889 	/* Nanosecond between 0 and 1e9 */
890 	if (tv_nsec < 0 || tv_nsec >= 1000000000) {
891 		zend_argument_value_error(4, "must be between 0 and 1e9");
892 		RETURN_THROWS();
893 	}
894 	if (UNEXPECTED(tv_sec == 0 && tv_nsec == 0)) {
895 		zend_value_error("pcntl_sigtimedwait(): At least one of argument #3 ($seconds) or argument #4 ($nanoseconds) must be greater than 0");
896 		RETURN_THROWS();
897 	}
898 
899 	errno = 0;
900 	siginfo_t siginfo;
901 	struct timespec timeout;
902 	timeout.tv_sec  = (time_t) tv_sec;
903 	timeout.tv_nsec = tv_nsec;
904 	int signal_no = sigtimedwait(&set, &siginfo, &timeout);
905 	if (signal_no == -1) {
906 		if (errno != EAGAIN) {
907 			PCNTL_G(last_error) = errno;
908 			php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
909 		}
910 		RETURN_FALSE;
911 	}
912 
913 	/* sigtimedwait can return 0 on success on some platforms, e.g. NetBSD */
914 	if (!signal_no && siginfo.si_signo) {
915 		signal_no = siginfo.si_signo;
916 	}
917 
918 	pcntl_siginfo_to_zval(signal_no, &siginfo, user_siginfo);
919 
920 	RETURN_LONG(signal_no);
921 }
922 /* }}} */
923 # endif
924 
pcntl_siginfo_to_zval(int signo,siginfo_t * siginfo,zval * user_siginfo)925 static void pcntl_siginfo_to_zval(int signo, siginfo_t *siginfo, zval *user_siginfo) /* {{{ */
926 {
927 	if (signo > 0 && user_siginfo) {
928 		user_siginfo = zend_try_array_init(user_siginfo);
929 		if (!user_siginfo) {
930 			return;
931 		}
932 
933 		add_assoc_long_ex(user_siginfo, "signo", sizeof("signo")-1, siginfo->si_signo);
934 		add_assoc_long_ex(user_siginfo, "errno", sizeof("errno")-1, siginfo->si_errno);
935 		add_assoc_long_ex(user_siginfo, "code",  sizeof("code")-1,  siginfo->si_code);
936 		switch(signo) {
937 #ifdef SIGCHLD
938 			case SIGCHLD:
939 				add_assoc_long_ex(user_siginfo,   "status", sizeof("status")-1, siginfo->si_status);
940 # ifdef si_utime
941 				add_assoc_double_ex(user_siginfo, "utime",  sizeof("utime")-1,  siginfo->si_utime);
942 # endif
943 # ifdef si_stime
944 				add_assoc_double_ex(user_siginfo, "stime",  sizeof("stime")-1,  siginfo->si_stime);
945 # endif
946 				add_assoc_long_ex(user_siginfo,   "pid",    sizeof("pid")-1,    siginfo->si_pid);
947 				add_assoc_long_ex(user_siginfo,   "uid",    sizeof("uid")-1,    siginfo->si_uid);
948 				break;
949 			case SIGUSR1:
950 			case SIGUSR2:
951 				add_assoc_long_ex(user_siginfo,   "pid",    sizeof("pid")-1,    siginfo->si_pid);
952 				add_assoc_long_ex(user_siginfo,   "uid",    sizeof("uid")-1,    siginfo->si_uid);
953 				break;
954 #endif
955 			case SIGILL:
956 			case SIGFPE:
957 			case SIGSEGV:
958 			case SIGBUS:
959 				add_assoc_double_ex(user_siginfo, "addr", sizeof("addr")-1, (zend_long)siginfo->si_addr);
960 				break;
961 #if defined(SIGPOLL) && !defined(__CYGWIN__)
962 			case SIGPOLL:
963 				add_assoc_long_ex(user_siginfo, "band", sizeof("band")-1, siginfo->si_band);
964 # ifdef si_fd
965 				add_assoc_long_ex(user_siginfo, "fd",   sizeof("fd")-1,   siginfo->si_fd);
966 # endif
967 				break;
968 #endif
969 		}
970 #if defined(SIGRTMIN) && defined(SIGRTMAX)
971 		if (SIGRTMIN <= signo && signo <= SIGRTMAX) {
972 			add_assoc_long_ex(user_siginfo, "pid", sizeof("pid")-1, siginfo->si_pid);
973 			add_assoc_long_ex(user_siginfo, "uid", sizeof("uid")-1, siginfo->si_uid);
974 		}
975 #endif
976 	}
977 }
978 /* }}} */
979 #endif
980 
981 #ifdef HAVE_GETPRIORITY
982 /* {{{ Get the priority of any process */
PHP_FUNCTION(pcntl_getpriority)983 PHP_FUNCTION(pcntl_getpriority)
984 {
985 	zend_long who = PRIO_PROCESS;
986 	zend_long pid;
987 	bool pid_is_null = 1;
988 	int pri;
989 
990 	ZEND_PARSE_PARAMETERS_START(0, 2)
991 		Z_PARAM_OPTIONAL
992 		Z_PARAM_LONG_OR_NULL(pid, pid_is_null)
993 		Z_PARAM_LONG(who)
994 	ZEND_PARSE_PARAMETERS_END();
995 
996 	/* needs to be cleared, since any returned value is valid */
997 	errno = 0;
998 
999 	pid = pid_is_null ? getpid() : pid;
1000 	pri = getpriority(who, pid);
1001 
1002 	if (errno) {
1003 		PCNTL_G(last_error) = errno;
1004 		switch (errno) {
1005 			case ESRCH:
1006 				php_error_docref(NULL, E_WARNING, "Error %d: No process was located using the given parameters", errno);
1007 				break;
1008 			case EINVAL:
1009 #ifdef PRIO_DARWIN_BG
1010 				if (who != PRIO_PGRP && who != PRIO_USER && who != PRIO_PROCESS && who != PRIO_DARWIN_THREAD) {
1011 					zend_argument_value_error(2, "must be one of PRIO_PGRP, PRIO_USER, PRIO_PROCESS or PRIO_DARWIN_THREAD");
1012 					RETURN_THROWS();
1013 				} else if (who == PRIO_DARWIN_THREAD && pid != 0) {
1014 					zend_argument_value_error(1, "must be 0 (zero) if PRIO_DARWIN_THREAD is provided as second parameter");
1015 					RETURN_THROWS();
1016 				} else {
1017 					zend_argument_value_error(1, "is not a valid process, process group, or user ID");
1018 					RETURN_THROWS();
1019 				}
1020 #else
1021 				zend_argument_value_error(2, "must be one of PRIO_PGRP, PRIO_USER, or PRIO_PROCESS");
1022 				RETURN_THROWS();
1023 #endif
1024 
1025 			default:
1026 				php_error_docref(NULL, E_WARNING, "Unknown error %d has occurred", errno);
1027 				break;
1028 		}
1029 		RETURN_FALSE;
1030 	}
1031 
1032 	RETURN_LONG(pri);
1033 }
1034 /* }}} */
1035 #endif
1036 
1037 #ifdef HAVE_SETPRIORITY
1038 /* {{{ Change the priority of any process */
PHP_FUNCTION(pcntl_setpriority)1039 PHP_FUNCTION(pcntl_setpriority)
1040 {
1041 	zend_long who = PRIO_PROCESS;
1042 	zend_long pid;
1043 	bool pid_is_null = 1;
1044 	zend_long pri;
1045 
1046 	ZEND_PARSE_PARAMETERS_START(1, 3)
1047 		Z_PARAM_LONG(pri)
1048 		Z_PARAM_OPTIONAL
1049 		Z_PARAM_LONG_OR_NULL(pid, pid_is_null)
1050 		Z_PARAM_LONG(who)
1051 	ZEND_PARSE_PARAMETERS_END();
1052 
1053 	pid = pid_is_null ? getpid() : pid;
1054 
1055 	if (setpriority(who, pid, pri)) {
1056 		PCNTL_G(last_error) = errno;
1057 		switch (errno) {
1058 			case ESRCH:
1059 				php_error_docref(NULL, E_WARNING, "Error %d: No process was located using the given parameters", errno);
1060 				break;
1061 			case EINVAL:
1062 #ifdef PRIO_DARWIN_BG
1063 				if (who != PRIO_PGRP && who != PRIO_USER && who != PRIO_PROCESS && who != PRIO_DARWIN_THREAD) {
1064 					zend_argument_value_error(3, "must be one of PRIO_PGRP, PRIO_USER, PRIO_PROCESS or PRIO_DARWIN_THREAD");
1065 					RETURN_THROWS();
1066 				} else if (who == PRIO_DARWIN_THREAD && pid != 0) {
1067 					zend_argument_value_error(2, "must be 0 (zero) if PRIO_DARWIN_THREAD is provided as second parameter");
1068 					RETURN_THROWS();
1069 				} else if (who == PRIO_DARWIN_THREAD && pid == 0 && (pri != 0 && pri != PRIO_DARWIN_BG)) {
1070 					zend_argument_value_error(1, "must be either 0 (zero) or PRIO_DARWIN_BG, for mode PRIO_DARWIN_THREAD");
1071 					RETURN_THROWS();
1072 				} else {
1073 					zend_argument_value_error(2, "is not a valid process, process group, or user ID");
1074 					RETURN_THROWS();
1075 				}
1076 #else
1077 				zend_argument_value_error(3, "must be one of PRIO_PGRP, PRIO_USER, or PRIO_PROCESS");
1078 				RETURN_THROWS();
1079 #endif
1080 			case EPERM:
1081 				php_error_docref(NULL, E_WARNING, "Error %d: A process was located, but neither its effective nor real user ID matched the effective user ID of the caller", errno);
1082 				break;
1083 			case EACCES:
1084 				php_error_docref(NULL, E_WARNING, "Error %d: Only a super user may attempt to increase the process priority", errno);
1085 				break;
1086 			default:
1087 				php_error_docref(NULL, E_WARNING, "Unknown error %d has occurred", errno);
1088 				break;
1089 		}
1090 		RETURN_FALSE;
1091 	}
1092 
1093 	RETURN_TRUE;
1094 }
1095 /* }}} */
1096 #endif
1097 
1098 /* {{{ Retrieve the error number set by the last pcntl function which failed. */
PHP_FUNCTION(pcntl_get_last_error)1099 PHP_FUNCTION(pcntl_get_last_error)
1100 {
1101 	ZEND_PARSE_PARAMETERS_NONE();
1102 
1103 	RETURN_LONG(PCNTL_G(last_error));
1104 }
1105 /* }}} */
1106 
1107 /* {{{ Retrieve the system error message associated with the given errno. */
PHP_FUNCTION(pcntl_strerror)1108 PHP_FUNCTION(pcntl_strerror)
1109 {
1110 	zend_long error;
1111 
1112 	ZEND_PARSE_PARAMETERS_START(1, 1)
1113 		Z_PARAM_LONG(error)
1114 	ZEND_PARSE_PARAMETERS_END();
1115 
1116 	RETURN_STRING(strerror(error));
1117 }
1118 /* }}} */
1119 
1120 /* Our custom signal handler that calls the appropriate php_function */
1121 #ifdef HAVE_STRUCT_SIGINFO_T
pcntl_signal_handler(int signo,siginfo_t * siginfo,void * context)1122 static void pcntl_signal_handler(int signo, siginfo_t *siginfo, void *context)
1123 #else
1124 static void pcntl_signal_handler(int signo)
1125 #endif
1126 {
1127 	struct php_pcntl_pending_signal *psig = PCNTL_G(spares);
1128 	if (!psig) {
1129 		/* oops, too many signals for us to track, so we'll forget about this one */
1130 		return;
1131 	}
1132 	PCNTL_G(spares) = psig->next;
1133 
1134 	psig->signo = signo;
1135 	psig->next = NULL;
1136 
1137 #ifdef HAVE_STRUCT_SIGINFO_T
1138 	psig->siginfo = *siginfo;
1139 #endif
1140 
1141 	/* the head check is important, as the tick handler cannot atomically clear both
1142 	 * the head and tail */
1143 	if (PCNTL_G(head) && PCNTL_G(tail)) {
1144 		PCNTL_G(tail)->next = psig;
1145 	} else {
1146 		PCNTL_G(head) = psig;
1147 	}
1148 	PCNTL_G(tail) = psig;
1149 	PCNTL_G(pending_signals) = 1;
1150 	if (PCNTL_G(async_signals)) {
1151 		zend_atomic_bool_store_ex(&EG(vm_interrupt), true);
1152 	}
1153 }
1154 
pcntl_signal_dispatch(void)1155 void pcntl_signal_dispatch(void)
1156 {
1157 	zval params[2], *handle, retval;
1158 	struct php_pcntl_pending_signal *queue, *next;
1159 	sigset_t mask;
1160 	sigset_t old_mask;
1161 
1162 	if(!PCNTL_G(pending_signals)) {
1163 		return;
1164 	}
1165 
1166 	/* Mask all signals */
1167 	sigfillset(&mask);
1168 	sigprocmask(SIG_BLOCK, &mask, &old_mask);
1169 
1170 	/* Bail if the queue is empty or if we are already playing the queue */
1171 	if (!PCNTL_G(head) || PCNTL_G(processing_signal_queue)) {
1172 		sigprocmask(SIG_SETMASK, &old_mask, NULL);
1173 		return;
1174 	}
1175 
1176 	/* Prevent switching fibers when handling signals */
1177 	zend_fiber_switch_block();
1178 
1179 	/* Prevent reentrant handler calls */
1180 	PCNTL_G(processing_signal_queue) = 1;
1181 
1182 	queue = PCNTL_G(head);
1183 	PCNTL_G(head) = NULL; /* simple stores are atomic */
1184 
1185 	/* Allocate */
1186 	while (queue) {
1187 		if ((handle = zend_hash_index_find(&PCNTL_G(php_signal_table), queue->signo)) != NULL) {
1188 			if (Z_TYPE_P(handle) != IS_LONG) {
1189 				ZVAL_NULL(&retval);
1190 				ZVAL_LONG(&params[0], queue->signo);
1191 #ifdef HAVE_STRUCT_SIGINFO_T
1192 				array_init(&params[1]);
1193 				pcntl_siginfo_to_zval(queue->signo, &queue->siginfo, &params[1]);
1194 #else
1195 				ZVAL_NULL(&params[1]);
1196 #endif
1197 
1198 				/* Call php signal handler - Note that we do not report errors, and we ignore the return value */
1199 				/* FIXME: this is probably broken when multiple signals are handled in this while loop (retval) */
1200 				call_user_function(NULL, NULL, handle, &retval, 2, params);
1201 				zval_ptr_dtor(&retval);
1202 #ifdef HAVE_STRUCT_SIGINFO_T
1203 				zval_ptr_dtor(&params[1]);
1204 #endif
1205 			}
1206 		}
1207 
1208 		next = queue->next;
1209 		queue->next = PCNTL_G(spares);
1210 		PCNTL_G(spares) = queue;
1211 		queue = next;
1212 	}
1213 
1214 	PCNTL_G(pending_signals) = 0;
1215 
1216 	/* Re-enable queue */
1217 	PCNTL_G(processing_signal_queue) = 0;
1218 
1219 	/* Re-enable fiber switching */
1220 	zend_fiber_switch_unblock();
1221 
1222 	/* return signal mask to previous state */
1223 	sigprocmask(SIG_SETMASK, &old_mask, NULL);
1224 }
1225 
pcntl_signal_dispatch_tick_function(int dummy_int,void * dummy_pointer)1226 static void pcntl_signal_dispatch_tick_function(int dummy_int, void *dummy_pointer)
1227 {
1228 	return pcntl_signal_dispatch();
1229 }
1230 
1231 /* {{{ Enable/disable asynchronous signal handling and return the old setting. */
PHP_FUNCTION(pcntl_async_signals)1232 PHP_FUNCTION(pcntl_async_signals)
1233 {
1234 	bool on, on_is_null = 1;
1235 
1236 	ZEND_PARSE_PARAMETERS_START(0, 1)
1237 		Z_PARAM_OPTIONAL
1238 		Z_PARAM_BOOL_OR_NULL(on, on_is_null)
1239 	ZEND_PARSE_PARAMETERS_END();
1240 
1241 	if (on_is_null) {
1242 		RETURN_BOOL(PCNTL_G(async_signals));
1243 	}
1244 
1245 	RETVAL_BOOL(PCNTL_G(async_signals));
1246 	PCNTL_G(async_signals) = on;
1247 }
1248 /* }}} */
1249 
1250 #ifdef HAVE_UNSHARE
1251 /* {{{ disassociate parts of the process execution context */
PHP_FUNCTION(pcntl_unshare)1252 PHP_FUNCTION(pcntl_unshare)
1253 {
1254 	zend_long flags;
1255 
1256 	ZEND_PARSE_PARAMETERS_START(1, 1)
1257 		Z_PARAM_LONG(flags)
1258 	ZEND_PARSE_PARAMETERS_END();
1259 
1260 	if (unshare(flags) == -1) {
1261 		PCNTL_G(last_error) = errno;
1262 		switch (errno) {
1263 #ifdef EINVAL
1264 			case EINVAL:
1265 				zend_argument_value_error(1, "must be a combination of CLONE_* flags");
1266 				RETURN_THROWS();
1267 				break;
1268 #endif
1269 #ifdef ENOMEM
1270 			case ENOMEM:
1271 				php_error_docref(NULL, E_WARNING, "Error %d: Insufficient memory for unshare", errno);
1272 				break;
1273 #endif
1274 #ifdef EPERM
1275 			case EPERM:
1276 				php_error_docref(NULL, E_WARNING, "Error %d: No privilege to use these flags", errno);
1277 				break;
1278 #endif
1279 #ifdef ENOSPC
1280 			case ENOSPC:
1281 				php_error_docref(NULL, E_WARNING, "Error %d: Reached the maximum nesting limit for one of the specified namespaces", errno);
1282 				break;
1283 #endif
1284 #ifdef EUSERS
1285 			case EUSERS:
1286 				php_error_docref(NULL, E_WARNING, "Error %d: Reached the maximum nesting limit for the user namespace", errno);
1287 				break;
1288 #endif
1289 			default:
1290 				php_error_docref(NULL, E_WARNING, "Unknown error %d has occurred", errno);
1291 				break;
1292 		}
1293 		RETURN_FALSE;
1294 	}
1295 
1296 	RETURN_TRUE;
1297 }
1298 /* }}} */
1299 #endif
1300 
1301 #ifdef HAVE_RFORK
1302 /* {{{ proto bool pcntl_rfork(int flags [, int signal])
1303    More control over the process creation is given over fork/vfork. */
PHP_FUNCTION(pcntl_rfork)1304 PHP_FUNCTION(pcntl_rfork)
1305 {
1306 	zend_long flags;
1307 	zend_long csignal = 0;
1308 	pid_t pid;
1309 
1310 	ZEND_PARSE_PARAMETERS_START(1, 2)
1311 		Z_PARAM_LONG(flags)
1312 		Z_PARAM_OPTIONAL
1313 		Z_PARAM_LONG(csignal)
1314 	ZEND_PARSE_PARAMETERS_END();
1315 
1316 	/* This is a flag to use with great caution in general, preferably not within PHP */
1317 	if ((flags & RFMEM) != 0) {
1318 		zend_argument_value_error(1, "must not include RFMEM value, not allowed within this context");
1319 		RETURN_THROWS();
1320 	}
1321 
1322 	if ((flags & RFSIGSHARE) != 0) {
1323 		zend_argument_value_error(1, "must not include RFSIGSHARE value, not allowed within this context");
1324 		RETURN_THROWS();
1325 	}
1326 
1327 	if ((flags & (RFFDG | RFCFDG)) == (RFFDG | RFCFDG)) {
1328 		zend_argument_value_error(1, "must not include both RFFDG and RFCFDG, because these flags are mutually exclusive");
1329 		RETURN_THROWS();
1330 	}
1331 
1332 	/* A new pid is required */
1333 	if (!(flags & (RFPROC))) {
1334 		flags |= RFPROC;
1335 	}
1336 
1337 #ifdef RFTSIGZMB
1338 	if ((flags & RFTSIGZMB) != 0) {
1339 		flags |= RFTSIGFLAGS(csignal);
1340 	}
1341 #endif
1342 
1343 	pid = rfork(flags);
1344 
1345 	if (pid == -1) {
1346 		PCNTL_G(last_error) = errno;
1347 		switch (errno) {
1348 			case EAGAIN:
1349 			php_error_docref(NULL, E_WARNING, "Maximum process creations limit reached\n");
1350 		break;
1351 
1352 		default:
1353 			php_error_docref(NULL, E_WARNING, "Error %d", errno);
1354 		}
1355 	}
1356 
1357 	RETURN_LONG((zend_long) pid);
1358 }
1359 #endif
1360 /* }}} */
1361 
1362 #ifdef HAVE_FORKX
1363 /* {{{ proto bool pcntl_forkx(int flags)
1364    More elaborated version of fork with the following settings.
1365    FORK_WAITPID: forbid the parent process to wait for multiple pid but one only
1366    FORK_NOSIGCHLD: SIGCHLD signal ignored when the child terminates */
PHP_FUNCTION(pcntl_forkx)1367 PHP_FUNCTION(pcntl_forkx)
1368 {
1369 	zend_long flags;
1370 	pid_t pid;
1371 
1372 	ZEND_PARSE_PARAMETERS_START(1, 1)
1373 		Z_PARAM_LONG(flags)
1374 	ZEND_PARSE_PARAMETERS_END();
1375 
1376 	if (flags < FORK_NOSIGCHLD || flags > FORK_WAITPID) {
1377 		zend_argument_value_error(1, "must be FORK_NOSIGCHLD or FORK_WAITPID");
1378 		RETURN_THROWS();
1379 	}
1380 
1381 	pid = forkx(flags);
1382 
1383 	if (pid == -1) {
1384 		PCNTL_G(last_error) = errno;
1385 		switch (errno) {
1386 			case EAGAIN:
1387 			php_error_docref(NULL, E_WARNING, "Maximum process creations limit reached\n");
1388 		break;
1389 			case EPERM:
1390 			php_error_docref(NULL, E_WARNING, "Calling process not having the proper privileges\n");
1391 		break;
1392 			case ENOMEM:
1393 			php_error_docref(NULL, E_WARNING, "No swap space left\n");
1394 		break;
1395 		default:
1396 			php_error_docref(NULL, E_WARNING, "Error %d", errno);
1397 		}
1398 	}
1399 
1400 	RETURN_LONG((zend_long) pid);
1401 }
1402 #endif
1403 /* }}} */
1404 
pcntl_interrupt_function(zend_execute_data * execute_data)1405 static void pcntl_interrupt_function(zend_execute_data *execute_data)
1406 {
1407 	pcntl_signal_dispatch();
1408 	if (orig_interrupt_function) {
1409 		orig_interrupt_function(execute_data);
1410 	}
1411 }
1412