xref: /PHP-8.3/ext/pcntl/pcntl.c (revision f7be15db)
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 	char *error = NULL;
604 
605 	ZEND_PARSE_PARAMETERS_START(2, 3)
606 		Z_PARAM_LONG(signo)
607 		Z_PARAM_ZVAL(handle)
608 		Z_PARAM_OPTIONAL
609 		Z_PARAM_BOOL_OR_NULL(restart_syscalls, restart_syscalls_is_null)
610 	ZEND_PARSE_PARAMETERS_END();
611 
612 	if (signo < 1) {
613 		zend_argument_value_error(1, "must be greater than or equal to 1");
614 		RETURN_THROWS();
615 	}
616 
617 	if (signo >= PCNTL_G(num_signals)) {
618 		zend_argument_value_error(1, "must be less than %d", PCNTL_G(num_signals));
619 		RETURN_THROWS();
620 	}
621 
622 	if (!PCNTL_G(spares)) {
623 		/* since calling malloc() from within a signal handler is not portable,
624 		 * pre-allocate a few records for recording signals */
625 		int i;
626 		for (i = 0; i < PCNTL_G(num_signals); i++) {
627 			struct php_pcntl_pending_signal *psig;
628 
629 			psig = emalloc(sizeof(*psig));
630 			psig->next = PCNTL_G(spares);
631 			PCNTL_G(spares) = psig;
632 		}
633 	}
634 
635 	/* If restart_syscalls was not explicitly specified and the signal is SIGALRM, then default
636 	 * restart_syscalls to false. PHP used to enforce that restart_syscalls is false for SIGALRM,
637 	 * so we keep this differing default to reduce the degree of BC breakage. */
638 	if (restart_syscalls_is_null && signo == SIGALRM) {
639 		restart_syscalls = 0;
640 	}
641 
642 	/* Special long value case for SIG_DFL and SIG_IGN */
643 	if (Z_TYPE_P(handle) == IS_LONG) {
644 		if (Z_LVAL_P(handle) != (zend_long) SIG_DFL && Z_LVAL_P(handle) != (zend_long) SIG_IGN) {
645 			zend_argument_value_error(2, "must be either SIG_DFL or SIG_IGN when an integer value is given");
646 			RETURN_THROWS();
647 		}
648 		if (php_signal(signo, (Sigfunc *) Z_LVAL_P(handle), (int) restart_syscalls) == (void *)SIG_ERR) {
649 			PCNTL_G(last_error) = errno;
650 			php_error_docref(NULL, E_WARNING, "Error assigning signal");
651 			RETURN_FALSE;
652 		}
653 		zend_hash_index_update(&PCNTL_G(php_signal_table), signo, handle);
654 		RETURN_TRUE;
655 	}
656 
657 	if (!zend_is_callable_ex(handle, NULL, 0, NULL, NULL, &error)) {
658 		zend_string *func_name = zend_get_callable_name(handle);
659 		PCNTL_G(last_error) = EINVAL;
660 
661 		zend_argument_type_error(2, "must be of type callable|int, %s given", zend_zval_value_name(handle));
662 		zend_string_release_ex(func_name, 0);
663 		efree(error);
664 		RETURN_THROWS();
665 	}
666 	ZEND_ASSERT(!error);
667 
668 	/* Add the function name to our signal table */
669 	handle = zend_hash_index_update(&PCNTL_G(php_signal_table), signo, handle);
670 	Z_TRY_ADDREF_P(handle);
671 
672 	if (php_signal4(signo, pcntl_signal_handler, (int) restart_syscalls, 1) == (void *)SIG_ERR) {
673 		PCNTL_G(last_error) = errno;
674 		php_error_docref(NULL, E_WARNING, "Error assigning signal");
675 		RETURN_FALSE;
676 	}
677 	RETURN_TRUE;
678 }
679 /* }}} */
680 
681 /* {{{ Gets signal handler */
PHP_FUNCTION(pcntl_signal_get_handler)682 PHP_FUNCTION(pcntl_signal_get_handler)
683 {
684 	zval *prev_handle;
685 	zend_long signo;
686 
687 	ZEND_PARSE_PARAMETERS_START(1, 1)
688 		Z_PARAM_LONG(signo)
689 	ZEND_PARSE_PARAMETERS_END();
690 
691 	if (signo < 1 || signo > 32) {
692 		zend_argument_value_error(1, "must be between 1 and 32");
693 		RETURN_THROWS();
694 	}
695 
696 	if ((prev_handle = zend_hash_index_find(&PCNTL_G(php_signal_table), signo)) != NULL) {
697 		RETURN_COPY(prev_handle);
698 	} else {
699 		RETURN_LONG((zend_long)SIG_DFL);
700 	}
701 }
702 
703 /* {{{ Dispatch signals to signal handlers */
PHP_FUNCTION(pcntl_signal_dispatch)704 PHP_FUNCTION(pcntl_signal_dispatch)
705 {
706 	ZEND_PARSE_PARAMETERS_NONE();
707 
708 	pcntl_signal_dispatch();
709 	RETURN_TRUE;
710 }
711 /* }}} */
712 
713 #ifdef HAVE_SIGPROCMASK
714 /* {{{ Examine and change blocked signals */
PHP_FUNCTION(pcntl_sigprocmask)715 PHP_FUNCTION(pcntl_sigprocmask)
716 {
717 	zend_long          how, signo;
718 	zval         *user_set, *user_oldset = NULL, *user_signo;
719 	sigset_t      set, oldset;
720 
721 	ZEND_PARSE_PARAMETERS_START(2, 3)
722 		Z_PARAM_LONG(how)
723 		Z_PARAM_ARRAY(user_set)
724 		Z_PARAM_OPTIONAL
725 		Z_PARAM_ZVAL(user_oldset)
726 	ZEND_PARSE_PARAMETERS_END();
727 
728 	if (sigemptyset(&set) != 0 || sigemptyset(&oldset) != 0) {
729 		PCNTL_G(last_error) = errno;
730 		php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
731 		RETURN_FALSE;
732 	}
733 
734 	ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(user_set), user_signo) {
735 		signo = zval_get_long(user_signo);
736 		if (sigaddset(&set, signo) != 0) {
737 			PCNTL_G(last_error) = errno;
738 			php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
739 			RETURN_FALSE;
740 		}
741 	} ZEND_HASH_FOREACH_END();
742 
743 	if (sigprocmask(how, &set, &oldset) != 0) {
744 		PCNTL_G(last_error) = errno;
745 		php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
746 		RETURN_FALSE;
747 	}
748 
749 	if (user_oldset != NULL) {
750 		user_oldset = zend_try_array_init(user_oldset);
751 		if (!user_oldset) {
752 			RETURN_THROWS();
753 		}
754 
755 		for (signo = 1; signo < PCNTL_G(num_signals); ++signo) {
756 			if (sigismember(&oldset, signo) != 1) {
757 				continue;
758 			}
759 			add_next_index_long(user_oldset, signo);
760 		}
761 	}
762 
763 	RETURN_TRUE;
764 }
765 /* }}} */
766 #endif
767 
768 #ifdef HAVE_STRUCT_SIGINFO_T
769 # if defined(HAVE_SIGWAITINFO) && defined(HAVE_SIGTIMEDWAIT)
pcntl_sigwaitinfo(INTERNAL_FUNCTION_PARAMETERS,int timedwait)770 static void pcntl_sigwaitinfo(INTERNAL_FUNCTION_PARAMETERS, int timedwait) /* {{{ */
771 {
772 	zval            *user_set, *user_signo, *user_siginfo = NULL;
773 	zend_long             tv_sec = 0, tv_nsec = 0;
774 	sigset_t         set;
775 	int              signo;
776 	siginfo_t        siginfo;
777 	struct timespec  timeout;
778 
779 	if (timedwait) {
780 		ZEND_PARSE_PARAMETERS_START(1, 4)
781 			Z_PARAM_ARRAY(user_set)
782 			Z_PARAM_OPTIONAL
783 			Z_PARAM_ZVAL(user_siginfo)
784 			Z_PARAM_LONG(tv_sec)
785 			Z_PARAM_LONG(tv_nsec)
786 		ZEND_PARSE_PARAMETERS_END();
787 	} else {
788 		ZEND_PARSE_PARAMETERS_START(1, 2)
789 			Z_PARAM_ARRAY(user_set)
790 			Z_PARAM_OPTIONAL
791 			Z_PARAM_ZVAL(user_siginfo)
792 		ZEND_PARSE_PARAMETERS_END();
793 	}
794 
795 	if (sigemptyset(&set) != 0) {
796 		PCNTL_G(last_error) = errno;
797 		php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
798 		RETURN_FALSE;
799 	}
800 
801 	ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(user_set), user_signo) {
802 		signo = zval_get_long(user_signo);
803 		if (sigaddset(&set, signo) != 0) {
804 			PCNTL_G(last_error) = errno;
805 			php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
806 			RETURN_FALSE;
807 		}
808 	} ZEND_HASH_FOREACH_END();
809 
810 	if (timedwait) {
811 		timeout.tv_sec  = (time_t) tv_sec;
812 		timeout.tv_nsec = tv_nsec;
813 		signo = sigtimedwait(&set, &siginfo, &timeout);
814 	} else {
815 		signo = sigwaitinfo(&set, &siginfo);
816 	}
817 	if (signo == -1 && errno != EAGAIN) {
818 		PCNTL_G(last_error) = errno;
819 		php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
820 	}
821 
822 	/*
823 	 * sigtimedwait and sigwaitinfo can return 0 on success on some
824 	 * platforms, e.g. NetBSD
825 	 */
826 	if (!signo && siginfo.si_signo) {
827 		signo = siginfo.si_signo;
828 	}
829 	pcntl_siginfo_to_zval(signo, &siginfo, user_siginfo);
830 	RETURN_LONG(signo);
831 }
832 /* }}} */
833 
834 /* {{{ Synchronously wait for queued signals */
PHP_FUNCTION(pcntl_sigwaitinfo)835 PHP_FUNCTION(pcntl_sigwaitinfo)
836 {
837 	pcntl_sigwaitinfo(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
838 }
839 /* }}} */
840 
841 /* {{{ Wait for queued signals */
PHP_FUNCTION(pcntl_sigtimedwait)842 PHP_FUNCTION(pcntl_sigtimedwait)
843 {
844 	pcntl_sigwaitinfo(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
845 }
846 /* }}} */
847 # endif
848 
pcntl_siginfo_to_zval(int signo,siginfo_t * siginfo,zval * user_siginfo)849 static void pcntl_siginfo_to_zval(int signo, siginfo_t *siginfo, zval *user_siginfo) /* {{{ */
850 {
851 	if (signo > 0 && user_siginfo) {
852 		user_siginfo = zend_try_array_init(user_siginfo);
853 		if (!user_siginfo) {
854 			return;
855 		}
856 
857 		add_assoc_long_ex(user_siginfo, "signo", sizeof("signo")-1, siginfo->si_signo);
858 		add_assoc_long_ex(user_siginfo, "errno", sizeof("errno")-1, siginfo->si_errno);
859 		add_assoc_long_ex(user_siginfo, "code",  sizeof("code")-1,  siginfo->si_code);
860 		switch(signo) {
861 #ifdef SIGCHLD
862 			case SIGCHLD:
863 				add_assoc_long_ex(user_siginfo,   "status", sizeof("status")-1, siginfo->si_status);
864 # ifdef si_utime
865 				add_assoc_double_ex(user_siginfo, "utime",  sizeof("utime")-1,  siginfo->si_utime);
866 # endif
867 # ifdef si_stime
868 				add_assoc_double_ex(user_siginfo, "stime",  sizeof("stime")-1,  siginfo->si_stime);
869 # endif
870 				add_assoc_long_ex(user_siginfo,   "pid",    sizeof("pid")-1,    siginfo->si_pid);
871 				add_assoc_long_ex(user_siginfo,   "uid",    sizeof("uid")-1,    siginfo->si_uid);
872 				break;
873 			case SIGUSR1:
874 			case SIGUSR2:
875 				add_assoc_long_ex(user_siginfo,   "pid",    sizeof("pid")-1,    siginfo->si_pid);
876 				add_assoc_long_ex(user_siginfo,   "uid",    sizeof("uid")-1,    siginfo->si_uid);
877 				break;
878 #endif
879 			case SIGILL:
880 			case SIGFPE:
881 			case SIGSEGV:
882 			case SIGBUS:
883 				add_assoc_double_ex(user_siginfo, "addr", sizeof("addr")-1, (zend_long)siginfo->si_addr);
884 				break;
885 #if defined(SIGPOLL) && !defined(__CYGWIN__)
886 			case SIGPOLL:
887 				add_assoc_long_ex(user_siginfo, "band", sizeof("band")-1, siginfo->si_band);
888 # ifdef si_fd
889 				add_assoc_long_ex(user_siginfo, "fd",   sizeof("fd")-1,   siginfo->si_fd);
890 # endif
891 				break;
892 #endif
893 		}
894 #if defined(SIGRTMIN) && defined(SIGRTMAX)
895 		if (SIGRTMIN <= signo && signo <= SIGRTMAX) {
896 			add_assoc_long_ex(user_siginfo, "pid", sizeof("pid")-1, siginfo->si_pid);
897 			add_assoc_long_ex(user_siginfo, "uid", sizeof("uid")-1, siginfo->si_uid);
898 		}
899 #endif
900 	}
901 }
902 /* }}} */
903 #endif
904 
905 #ifdef HAVE_GETPRIORITY
906 /* {{{ Get the priority of any process */
PHP_FUNCTION(pcntl_getpriority)907 PHP_FUNCTION(pcntl_getpriority)
908 {
909 	zend_long who = PRIO_PROCESS;
910 	zend_long pid;
911 	bool pid_is_null = 1;
912 	int pri;
913 
914 	ZEND_PARSE_PARAMETERS_START(0, 2)
915 		Z_PARAM_OPTIONAL
916 		Z_PARAM_LONG_OR_NULL(pid, pid_is_null)
917 		Z_PARAM_LONG(who)
918 	ZEND_PARSE_PARAMETERS_END();
919 
920 	/* needs to be cleared, since any returned value is valid */
921 	errno = 0;
922 
923 	pid = pid_is_null ? getpid() : pid;
924 	pri = getpriority(who, pid);
925 
926 	if (errno) {
927 		PCNTL_G(last_error) = errno;
928 		switch (errno) {
929 			case ESRCH:
930 				php_error_docref(NULL, E_WARNING, "Error %d: No process was located using the given parameters", errno);
931 				break;
932 			case EINVAL:
933 #ifdef PRIO_DARWIN_BG
934 				if (who != PRIO_PGRP && who != PRIO_USER && who != PRIO_PROCESS && who != PRIO_DARWIN_THREAD) {
935 					zend_argument_value_error(2, "must be one of PRIO_PGRP, PRIO_USER, PRIO_PROCESS or PRIO_DARWIN_THREAD");
936 					RETURN_THROWS();
937 				} else if (who == PRIO_DARWIN_THREAD && pid != 0) {
938 					zend_argument_value_error(1, "must be 0 (zero) if PRIO_DARWIN_THREAD is provided as second parameter");
939 					RETURN_THROWS();
940 				} else {
941 					zend_argument_value_error(1, "is not a valid process, process group, or user ID");
942 					RETURN_THROWS();
943 				}
944 #else
945 				zend_argument_value_error(2, "must be one of PRIO_PGRP, PRIO_USER, or PRIO_PROCESS");
946 				RETURN_THROWS();
947 #endif
948 
949 			default:
950 				php_error_docref(NULL, E_WARNING, "Unknown error %d has occurred", errno);
951 				break;
952 		}
953 		RETURN_FALSE;
954 	}
955 
956 	RETURN_LONG(pri);
957 }
958 /* }}} */
959 #endif
960 
961 #ifdef HAVE_SETPRIORITY
962 /* {{{ Change the priority of any process */
PHP_FUNCTION(pcntl_setpriority)963 PHP_FUNCTION(pcntl_setpriority)
964 {
965 	zend_long who = PRIO_PROCESS;
966 	zend_long pid;
967 	bool pid_is_null = 1;
968 	zend_long pri;
969 
970 	ZEND_PARSE_PARAMETERS_START(1, 3)
971 		Z_PARAM_LONG(pri)
972 		Z_PARAM_OPTIONAL
973 		Z_PARAM_LONG_OR_NULL(pid, pid_is_null)
974 		Z_PARAM_LONG(who)
975 	ZEND_PARSE_PARAMETERS_END();
976 
977 	pid = pid_is_null ? getpid() : pid;
978 
979 	if (setpriority(who, pid, pri)) {
980 		PCNTL_G(last_error) = errno;
981 		switch (errno) {
982 			case ESRCH:
983 				php_error_docref(NULL, E_WARNING, "Error %d: No process was located using the given parameters", errno);
984 				break;
985 			case EINVAL:
986 #ifdef PRIO_DARWIN_BG
987 				if (who != PRIO_PGRP && who != PRIO_USER && who != PRIO_PROCESS && who != PRIO_DARWIN_THREAD) {
988 					zend_argument_value_error(3, "must be one of PRIO_PGRP, PRIO_USER, PRIO_PROCESS or PRIO_DARWIN_THREAD");
989 					RETURN_THROWS();
990 				} else if (who == PRIO_DARWIN_THREAD && pid != 0) {
991 					zend_argument_value_error(2, "must be 0 (zero) if PRIO_DARWIN_THREAD is provided as second parameter");
992 					RETURN_THROWS();
993 				} else if (who == PRIO_DARWIN_THREAD && pid == 0 && (pri != 0 && pri != PRIO_DARWIN_BG)) {
994 					zend_argument_value_error(1, "must be either 0 (zero) or PRIO_DARWIN_BG, for mode PRIO_DARWIN_THREAD");
995 					RETURN_THROWS();
996 				} else {
997 					zend_argument_value_error(2, "is not a valid process, process group, or user ID");
998 					RETURN_THROWS();
999 				}
1000 #else
1001 				zend_argument_value_error(3, "must be one of PRIO_PGRP, PRIO_USER, or PRIO_PROCESS");
1002 				RETURN_THROWS();
1003 #endif
1004 			case EPERM:
1005 				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);
1006 				break;
1007 			case EACCES:
1008 				php_error_docref(NULL, E_WARNING, "Error %d: Only a super user may attempt to increase the process priority", errno);
1009 				break;
1010 			default:
1011 				php_error_docref(NULL, E_WARNING, "Unknown error %d has occurred", errno);
1012 				break;
1013 		}
1014 		RETURN_FALSE;
1015 	}
1016 
1017 	RETURN_TRUE;
1018 }
1019 /* }}} */
1020 #endif
1021 
1022 /* {{{ Retrieve the error number set by the last pcntl function which failed. */
PHP_FUNCTION(pcntl_get_last_error)1023 PHP_FUNCTION(pcntl_get_last_error)
1024 {
1025 	ZEND_PARSE_PARAMETERS_NONE();
1026 
1027 	RETURN_LONG(PCNTL_G(last_error));
1028 }
1029 /* }}} */
1030 
1031 /* {{{ Retrieve the system error message associated with the given errno. */
PHP_FUNCTION(pcntl_strerror)1032 PHP_FUNCTION(pcntl_strerror)
1033 {
1034 	zend_long error;
1035 
1036 	ZEND_PARSE_PARAMETERS_START(1, 1)
1037 		Z_PARAM_LONG(error)
1038 	ZEND_PARSE_PARAMETERS_END();
1039 
1040 	RETURN_STRING(strerror(error));
1041 }
1042 /* }}} */
1043 
1044 /* Our custom signal handler that calls the appropriate php_function */
1045 #ifdef HAVE_STRUCT_SIGINFO_T
pcntl_signal_handler(int signo,siginfo_t * siginfo,void * context)1046 static void pcntl_signal_handler(int signo, siginfo_t *siginfo, void *context)
1047 #else
1048 static void pcntl_signal_handler(int signo)
1049 #endif
1050 {
1051 	struct php_pcntl_pending_signal *psig = PCNTL_G(spares);
1052 	if (!psig) {
1053 		/* oops, too many signals for us to track, so we'll forget about this one */
1054 		return;
1055 	}
1056 	PCNTL_G(spares) = psig->next;
1057 
1058 	psig->signo = signo;
1059 	psig->next = NULL;
1060 
1061 #ifdef HAVE_STRUCT_SIGINFO_T
1062 	psig->siginfo = *siginfo;
1063 #endif
1064 
1065 	/* the head check is important, as the tick handler cannot atomically clear both
1066 	 * the head and tail */
1067 	if (PCNTL_G(head) && PCNTL_G(tail)) {
1068 		PCNTL_G(tail)->next = psig;
1069 	} else {
1070 		PCNTL_G(head) = psig;
1071 	}
1072 	PCNTL_G(tail) = psig;
1073 	PCNTL_G(pending_signals) = 1;
1074 	if (PCNTL_G(async_signals)) {
1075 		zend_atomic_bool_store_ex(&EG(vm_interrupt), true);
1076 	}
1077 }
1078 
pcntl_signal_dispatch(void)1079 void pcntl_signal_dispatch(void)
1080 {
1081 	zval params[2], *handle, retval;
1082 	struct php_pcntl_pending_signal *queue, *next;
1083 	sigset_t mask;
1084 	sigset_t old_mask;
1085 
1086 	if(!PCNTL_G(pending_signals)) {
1087 		return;
1088 	}
1089 
1090 	/* Mask all signals */
1091 	sigfillset(&mask);
1092 	sigprocmask(SIG_BLOCK, &mask, &old_mask);
1093 
1094 	/* Bail if the queue is empty or if we are already playing the queue */
1095 	if (!PCNTL_G(head) || PCNTL_G(processing_signal_queue)) {
1096 		sigprocmask(SIG_SETMASK, &old_mask, NULL);
1097 		return;
1098 	}
1099 
1100 	/* Prevent switching fibers when handling signals */
1101 	zend_fiber_switch_block();
1102 
1103 	/* Prevent reentrant handler calls */
1104 	PCNTL_G(processing_signal_queue) = 1;
1105 
1106 	queue = PCNTL_G(head);
1107 	PCNTL_G(head) = NULL; /* simple stores are atomic */
1108 
1109 	/* Allocate */
1110 	while (queue) {
1111 		if ((handle = zend_hash_index_find(&PCNTL_G(php_signal_table), queue->signo)) != NULL) {
1112 			if (Z_TYPE_P(handle) != IS_LONG) {
1113 				ZVAL_NULL(&retval);
1114 				ZVAL_LONG(&params[0], queue->signo);
1115 #ifdef HAVE_STRUCT_SIGINFO_T
1116 				array_init(&params[1]);
1117 				pcntl_siginfo_to_zval(queue->signo, &queue->siginfo, &params[1]);
1118 #else
1119 				ZVAL_NULL(&params[1]);
1120 #endif
1121 
1122 				/* Call php signal handler - Note that we do not report errors, and we ignore the return value */
1123 				/* FIXME: this is probably broken when multiple signals are handled in this while loop (retval) */
1124 				call_user_function(NULL, NULL, handle, &retval, 2, params);
1125 				zval_ptr_dtor(&retval);
1126 #ifdef HAVE_STRUCT_SIGINFO_T
1127 				zval_ptr_dtor(&params[1]);
1128 #endif
1129 			}
1130 		}
1131 
1132 		next = queue->next;
1133 		queue->next = PCNTL_G(spares);
1134 		PCNTL_G(spares) = queue;
1135 		queue = next;
1136 	}
1137 
1138 	PCNTL_G(pending_signals) = 0;
1139 
1140 	/* Re-enable queue */
1141 	PCNTL_G(processing_signal_queue) = 0;
1142 
1143 	/* Re-enable fiber switching */
1144 	zend_fiber_switch_unblock();
1145 
1146 	/* return signal mask to previous state */
1147 	sigprocmask(SIG_SETMASK, &old_mask, NULL);
1148 }
1149 
pcntl_signal_dispatch_tick_function(int dummy_int,void * dummy_pointer)1150 static void pcntl_signal_dispatch_tick_function(int dummy_int, void *dummy_pointer)
1151 {
1152 	return pcntl_signal_dispatch();
1153 }
1154 
1155 /* {{{ Enable/disable asynchronous signal handling and return the old setting. */
PHP_FUNCTION(pcntl_async_signals)1156 PHP_FUNCTION(pcntl_async_signals)
1157 {
1158 	bool on, on_is_null = 1;
1159 
1160 	ZEND_PARSE_PARAMETERS_START(0, 1)
1161 		Z_PARAM_OPTIONAL
1162 		Z_PARAM_BOOL_OR_NULL(on, on_is_null)
1163 	ZEND_PARSE_PARAMETERS_END();
1164 
1165 	if (on_is_null) {
1166 		RETURN_BOOL(PCNTL_G(async_signals));
1167 	}
1168 
1169 	RETVAL_BOOL(PCNTL_G(async_signals));
1170 	PCNTL_G(async_signals) = on;
1171 }
1172 /* }}} */
1173 
1174 #ifdef HAVE_UNSHARE
1175 /* {{{ disassociate parts of the process execution context */
PHP_FUNCTION(pcntl_unshare)1176 PHP_FUNCTION(pcntl_unshare)
1177 {
1178 	zend_long flags;
1179 
1180 	ZEND_PARSE_PARAMETERS_START(1, 1)
1181 		Z_PARAM_LONG(flags)
1182 	ZEND_PARSE_PARAMETERS_END();
1183 
1184 	if (unshare(flags) == -1) {
1185 		PCNTL_G(last_error) = errno;
1186 		switch (errno) {
1187 #ifdef EINVAL
1188 			case EINVAL:
1189 				zend_argument_value_error(1, "must be a combination of CLONE_* flags");
1190 				RETURN_THROWS();
1191 				break;
1192 #endif
1193 #ifdef ENOMEM
1194 			case ENOMEM:
1195 				php_error_docref(NULL, E_WARNING, "Error %d: Insufficient memory for unshare", errno);
1196 				break;
1197 #endif
1198 #ifdef EPERM
1199 			case EPERM:
1200 				php_error_docref(NULL, E_WARNING, "Error %d: No privilege to use these flags", errno);
1201 				break;
1202 #endif
1203 #ifdef ENOSPC
1204 			case ENOSPC:
1205 				php_error_docref(NULL, E_WARNING, "Error %d: Reached the maximum nesting limit for one of the specified namespaces", errno);
1206 				break;
1207 #endif
1208 #ifdef EUSERS
1209 			case EUSERS:
1210 				php_error_docref(NULL, E_WARNING, "Error %d: Reached the maximum nesting limit for the user namespace", errno);
1211 				break;
1212 #endif
1213 			default:
1214 				php_error_docref(NULL, E_WARNING, "Unknown error %d has occurred", errno);
1215 				break;
1216 		}
1217 		RETURN_FALSE;
1218 	}
1219 
1220 	RETURN_TRUE;
1221 }
1222 /* }}} */
1223 #endif
1224 
1225 #ifdef HAVE_RFORK
1226 /* {{{ proto bool pcntl_rfork(int flags [, int signal])
1227    More control over the process creation is given over fork/vfork. */
PHP_FUNCTION(pcntl_rfork)1228 PHP_FUNCTION(pcntl_rfork)
1229 {
1230 	zend_long flags;
1231 	zend_long csignal = 0;
1232 	pid_t pid;
1233 
1234 	ZEND_PARSE_PARAMETERS_START(1, 2)
1235 		Z_PARAM_LONG(flags)
1236 		Z_PARAM_OPTIONAL
1237 		Z_PARAM_LONG(csignal)
1238 	ZEND_PARSE_PARAMETERS_END();
1239 
1240 	/* This is a flag to use with great caution in general, preferably not within PHP */
1241 	if ((flags & RFMEM) != 0) {
1242 		zend_argument_value_error(1, "must not include RFMEM value, not allowed within this context");
1243 		RETURN_THROWS();
1244 	}
1245 
1246 	if ((flags & RFSIGSHARE) != 0) {
1247 		zend_argument_value_error(1, "must not include RFSIGSHARE value, not allowed within this context");
1248 		RETURN_THROWS();
1249 	}
1250 
1251 	if ((flags & (RFFDG | RFCFDG)) == (RFFDG | RFCFDG)) {
1252 		zend_argument_value_error(1, "must not include both RFFDG and RFCFDG, because these flags are mutually exclusive");
1253 		RETURN_THROWS();
1254 	}
1255 
1256 	/* A new pid is required */
1257 	if (!(flags & (RFPROC))) {
1258 		flags |= RFPROC;
1259 	}
1260 
1261 #ifdef RFTSIGZMB
1262 	if ((flags & RFTSIGZMB) != 0) {
1263 		flags |= RFTSIGFLAGS(csignal);
1264 	}
1265 #endif
1266 
1267 	pid = rfork(flags);
1268 
1269 	if (pid == -1) {
1270 		PCNTL_G(last_error) = errno;
1271 		switch (errno) {
1272 			case EAGAIN:
1273 			php_error_docref(NULL, E_WARNING, "Maximum process creations limit reached\n");
1274 		break;
1275 
1276 		default:
1277 			php_error_docref(NULL, E_WARNING, "Error %d", errno);
1278 		}
1279 	}
1280 
1281 	RETURN_LONG((zend_long) pid);
1282 }
1283 #endif
1284 /* }}} */
1285 
1286 #ifdef HAVE_FORKX
1287 /* {{{ proto bool pcntl_forkx(int flags)
1288    More elaborated version of fork with the following settings.
1289    FORK_WAITPID: forbid the parent process to wait for multiple pid but one only
1290    FORK_NOSIGCHLD: SIGCHLD signal ignored when the child terminates */
PHP_FUNCTION(pcntl_forkx)1291 PHP_FUNCTION(pcntl_forkx)
1292 {
1293 	zend_long flags;
1294 	pid_t pid;
1295 
1296 	ZEND_PARSE_PARAMETERS_START(1, 1)
1297 		Z_PARAM_LONG(flags)
1298 	ZEND_PARSE_PARAMETERS_END();
1299 
1300 	if (flags < FORK_NOSIGCHLD || flags > FORK_WAITPID) {
1301 		zend_argument_value_error(1, "must be FORK_NOSIGCHLD or FORK_WAITPID");
1302 		RETURN_THROWS();
1303 	}
1304 
1305 	pid = forkx(flags);
1306 
1307 	if (pid == -1) {
1308 		PCNTL_G(last_error) = errno;
1309 		switch (errno) {
1310 			case EAGAIN:
1311 			php_error_docref(NULL, E_WARNING, "Maximum process creations limit reached\n");
1312 		break;
1313 			case EPERM:
1314 			php_error_docref(NULL, E_WARNING, "Calling process not having the proper privileges\n");
1315 		break;
1316 			case ENOMEM:
1317 			php_error_docref(NULL, E_WARNING, "No swap space left\n");
1318 		break;
1319 		default:
1320 			php_error_docref(NULL, E_WARNING, "Error %d", errno);
1321 		}
1322 	}
1323 
1324 	RETURN_LONG((zend_long) pid);
1325 }
1326 #endif
1327 /* }}} */
1328 
pcntl_interrupt_function(zend_execute_data * execute_data)1329 static void pcntl_interrupt_function(zend_execute_data *execute_data)
1330 {
1331 	pcntl_signal_dispatch();
1332 	if (orig_interrupt_function) {
1333 		orig_interrupt_function(execute_data);
1334 	}
1335 }
1336