xref: /php-src/ext/pcntl/pcntl.c (revision 2fa3e809)
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 #if defined(HAVE_UNSHARE) || defined(HAVE_SCHED_SETAFFINITY) || defined(HAVE_SCHED_GETCPU)
46 #include <sched.h>
47 #if defined(__FreeBSD__)
48 #include <sys/types.h>
49 #include <sys/cpuset.h>
50 typedef cpuset_t cpu_set_t;
51 #endif
52  #define PCNTL_CPUSET(mask) &mask
53  #define PCNTL_CPUSET_SIZE(mask) sizeof(mask)
54  #define PCNTL_CPU_ISSET(i, mask) CPU_ISSET(i, &mask)
55  #define PCNTL_CPU_SET(i, mask) CPU_SET(i, &mask)
56  #define PCNTL_CPU_ZERO(mask) CPU_ZERO(&mask)
57  #define PCNTL_CPU_DESTROY(mask) ((void)0)
58 #elif defined(__NetBSD__)
59 #include <sys/syscall.h>
60 #include <sched.h>
61 typedef cpuset_t *cpu_set_t;
62  #define sched_getaffinity(p, c, m) syscall(SYS__sched_getaffinity, p, 0, c, m)
63  #define sched_setaffinity(p, c, m) syscall(SYS__sched_setaffinity, p, 0, c, m)
64  #define PCNTL_CPUSET(mask) mask
65  #define PCNTL_CPUSET_SIZE(mask) cpuset_size(mask)
66  #define PCNTL_CPU_ISSET(i, mask) cpuset_isset((cpuid_t)i, mask)
67  #define PCNTL_CPU_SET(i, mask) cpuset_set((cpuid_t)i, mask)
68  #define PCNTL_CPU_ZERO(mask)										\
69 	do {												\
70 		mask = cpuset_create(); 								\
71 		if (UNEXPECTED(!mask)) {								\
72 			php_error_docref(NULL, E_WARNING, "cpuset_create: Insufficient memory");	\
73 			RETURN_FALSE;   								\
74 		}											\
75 		cpuset_zero(mask);									\
76 	} while(0)
77  #define PCNTL_CPU_DESTROY(mask) cpuset_destroy(mask)
78  #define HAVE_SCHED_SETAFFINITY 1
79 #elif defined(HAVE_PSET_BIND)
80 #include <sys/pset.h>
81 typedef psetid_t cpu_set_t;
82  #define sched_getaffinity(p, c, m) pset_bind(PS_QUERY, P_PID, (p <= 0 ? getpid() : p), &m)
83  #define sched_setaffinity(p, c, m) pset_bind(m, P_PID, (p <= 0 ? getpid() : p), NULL)
84  #define PCNTL_CPUSET(mask) mask
85  #define PCNTL_CPU_ISSET(i, mask) (pset_assign(PS_QUERY, (processorid_t)i, &query) == 0 && query == mask)
86  #define PCNTL_CPU_SET(i, mask) pset_assign(mask, (processorid_t)i, NULL)
87  #define PCNTL_CPU_ZERO(mask) 														\
88 	psetid_t query;																	\
89 	do {																			\
90 		if (UNEXPECTED(pset_create(&mask) != 0)) {									\
91 			php_error_docref(NULL, E_WARNING, "pset_create: %s", strerror(errno));	\
92 			RETURN_FALSE;															\
93 		}																			\
94 	 } while (0)
95  #define PCNTL_CPU_DESTROY(mask) 													\
96 	do {																			\
97 		if (UNEXPECTED(mask != PS_NONE && pset_destroy(mask) != 0)) {				\
98 			php_error_docref(NULL, E_WARNING, "pset_destroy: %s", strerror(errno));	\
99 		}																			\
100 	} while (0)
101  #define HAVE_SCHED_SETAFFINITY 1
102 #endif
103 
104 #if defined(HAVE_GETCPUID)
105 #include <sys/processor.h>
106 #define sched_getcpu getcpuid
107 #define HAVE_SCHED_GETCPU 1
108 #endif
109 
110 #if defined(HAVE_PTHREAD_SET_QOS_CLASS_SELF_NP)
111 #include <pthread/qos.h>
112 static zend_class_entry *QosClass_ce;
113 #endif
114 
115 #ifdef HAVE_PIDFD_OPEN
116 #include <sys/syscall.h>
117 #endif
118 
119 #ifdef HAVE_FORKX
120 #include <sys/fork.h>
121 #endif
122 
123 #ifndef NSIG
124 # define NSIG 32
125 #endif
126 
127 #define LONG_CONST(c) (zend_long) c
128 
129 #include "Zend/zend_enum.h"
130 #include "Zend/zend_max_execution_timer.h"
131 
132 #include "pcntl_arginfo.h"
133 
134 ZEND_DECLARE_MODULE_GLOBALS(pcntl)
135 static PHP_GINIT_FUNCTION(pcntl);
136 
137 zend_module_entry pcntl_module_entry = {
138 	STANDARD_MODULE_HEADER,
139 	"pcntl",
140 	ext_functions,
141 	PHP_MINIT(pcntl),
142 	PHP_MSHUTDOWN(pcntl),
143 	PHP_RINIT(pcntl),
144 	PHP_RSHUTDOWN(pcntl),
145 	PHP_MINFO(pcntl),
146 	PHP_PCNTL_VERSION,
147 	PHP_MODULE_GLOBALS(pcntl),
148 	PHP_GINIT(pcntl),
149 	NULL,
150 	NULL,
151 	STANDARD_MODULE_PROPERTIES_EX
152 };
153 
154 #ifdef COMPILE_DL_PCNTL
155 #ifdef ZTS
156 ZEND_TSRMLS_CACHE_DEFINE()
157 #endif
158 ZEND_GET_MODULE(pcntl)
159 #endif
160 
161 static void (*orig_interrupt_function)(zend_execute_data *execute_data);
162 
163 #ifdef HAVE_STRUCT_SIGINFO_T
164 static void pcntl_signal_handler(int, siginfo_t*, void*);
165 static void pcntl_siginfo_to_zval(int, siginfo_t*, zval*);
166 #else
167 static void pcntl_signal_handler(int);
168 #endif
169 static void pcntl_signal_dispatch(void);
170 static void pcntl_signal_dispatch_tick_function(int dummy_int, void *dummy_pointer);
171 static void pcntl_interrupt_function(zend_execute_data *execute_data);
172 
PHP_GINIT_FUNCTION(pcntl)173 static PHP_GINIT_FUNCTION(pcntl)
174 {
175 #if defined(COMPILE_DL_PCNTL) && defined(ZTS)
176 	ZEND_TSRMLS_CACHE_UPDATE();
177 #endif
178 	memset(pcntl_globals, 0, sizeof(*pcntl_globals));
179 }
180 
PHP_RINIT_FUNCTION(pcntl)181 PHP_RINIT_FUNCTION(pcntl)
182 {
183 	php_add_tick_function(pcntl_signal_dispatch_tick_function, NULL);
184 	zend_hash_init(&PCNTL_G(php_signal_table), 16, NULL, ZVAL_PTR_DTOR, 0);
185 	PCNTL_G(head) = PCNTL_G(tail) = PCNTL_G(spares) = NULL;
186 	PCNTL_G(async_signals) = 0;
187 	PCNTL_G(last_error) = 0;
188 	PCNTL_G(num_signals) = NSIG;
189 #ifdef SIGRTMAX
190 	/* At least FreeBSD reports an incorrecrt NSIG that does not include realtime signals.
191 	 * As SIGRTMAX may be a dynamic value, adjust the value in INIT. */
192 	if (NSIG < SIGRTMAX + 1) {
193 		PCNTL_G(num_signals) = SIGRTMAX + 1;
194 	}
195 #endif
196 	return SUCCESS;
197 }
198 
PHP_MINIT_FUNCTION(pcntl)199 PHP_MINIT_FUNCTION(pcntl)
200 {
201 #if defined(HAVE_PTHREAD_SET_QOS_CLASS_SELF_NP)
202 	QosClass_ce = register_class_QosClass();
203 #endif
204 	register_pcntl_symbols(module_number);
205 	orig_interrupt_function = zend_interrupt_function;
206 	zend_interrupt_function = pcntl_interrupt_function;
207 
208 	return SUCCESS;
209 }
210 
PHP_MSHUTDOWN_FUNCTION(pcntl)211 PHP_MSHUTDOWN_FUNCTION(pcntl)
212 {
213 	return SUCCESS;
214 }
215 
PHP_RSHUTDOWN_FUNCTION(pcntl)216 PHP_RSHUTDOWN_FUNCTION(pcntl)
217 {
218 	struct php_pcntl_pending_signal *sig;
219 	zend_long signo;
220 	zval *handle;
221 
222 	/* Reset all signals to their default disposition */
223 	ZEND_HASH_FOREACH_NUM_KEY_VAL(&PCNTL_G(php_signal_table), signo, handle) {
224 		if (Z_TYPE_P(handle) != IS_LONG || Z_LVAL_P(handle) != (zend_long)SIG_DFL) {
225 			php_signal(signo, (Sigfunc *)(zend_long)SIG_DFL, 0);
226 		}
227 	} ZEND_HASH_FOREACH_END();
228 
229 	zend_hash_destroy(&PCNTL_G(php_signal_table));
230 
231 	while (PCNTL_G(head)) {
232 		sig = PCNTL_G(head);
233 		PCNTL_G(head) = sig->next;
234 		efree(sig);
235 	}
236 	while (PCNTL_G(spares)) {
237 		sig = PCNTL_G(spares);
238 		PCNTL_G(spares) = sig->next;
239 		efree(sig);
240 	}
241 
242 	return SUCCESS;
243 }
244 
PHP_MINFO_FUNCTION(pcntl)245 PHP_MINFO_FUNCTION(pcntl)
246 {
247 	php_info_print_table_start();
248 	php_info_print_table_row(2, "pcntl support", "enabled");
249 	php_info_print_table_end();
250 }
251 
252 /* {{{ Forks the currently running process following the same behavior as the UNIX fork() system call*/
PHP_FUNCTION(pcntl_fork)253 PHP_FUNCTION(pcntl_fork)
254 {
255 	pid_t id;
256 
257 	ZEND_PARSE_PARAMETERS_NONE();
258 
259 	id = fork();
260 	if (id == -1) {
261 		PCNTL_G(last_error) = errno;
262 		switch (errno) {
263 			case EAGAIN:
264 				php_error_docref(NULL, E_WARNING, "Error %d: Reached the maximum limit of number of processes", errno);
265 				break;
266 			case ENOMEM:
267 				php_error_docref(NULL, E_WARNING, "Error %d: Insufficient memory", errno);
268 				break;
269 			// unlikely, especially nowadays.
270 			case ENOSYS:
271 				php_error_docref(NULL, E_WARNING, "Error %d: Unimplemented", errno);
272 				break;
273 			// QNX is the only platform returning it so far and is a different case from EAGAIN.
274 			// Retries can be handled with sleep eventually.
275 			case EBADF:
276 				php_error_docref(NULL, E_WARNING, "Error %d: File descriptor concurrency issue", errno);
277 				break;
278 			default:
279 				php_error_docref(NULL, E_WARNING, "Error %d", errno);
280 
281 		}
282 	} else if (id == 0) {
283 		zend_max_execution_timer_init();
284 	}
285 
286 	RETURN_LONG((zend_long) id);
287 }
288 /* }}} */
289 
290 /* {{{ Set an alarm clock for delivery of a signal*/
PHP_FUNCTION(pcntl_alarm)291 PHP_FUNCTION(pcntl_alarm)
292 {
293 	zend_long seconds;
294 
295 	ZEND_PARSE_PARAMETERS_START(1, 1)
296 		Z_PARAM_LONG(seconds);
297 	ZEND_PARSE_PARAMETERS_END();
298 
299 	RETURN_LONG((zend_long) alarm(seconds));
300 }
301 /* }}} */
302 
303 #define PHP_RUSAGE_PARA(from, to, field) \
304 	add_assoc_long(to, #field, from.field)
305 #ifndef _OSD_POSIX
306 	#define PHP_RUSAGE_SPECIAL(from, to) \
307 		PHP_RUSAGE_PARA(from, to, ru_oublock); \
308 		PHP_RUSAGE_PARA(from, to, ru_inblock); \
309 		PHP_RUSAGE_PARA(from, to, ru_msgsnd); \
310 		PHP_RUSAGE_PARA(from, to, ru_msgrcv); \
311 		PHP_RUSAGE_PARA(from, to, ru_maxrss); \
312 		PHP_RUSAGE_PARA(from, to, ru_ixrss); \
313 		PHP_RUSAGE_PARA(from, to, ru_idrss); \
314 		PHP_RUSAGE_PARA(from, to, ru_minflt); \
315 		PHP_RUSAGE_PARA(from, to, ru_majflt); \
316 		PHP_RUSAGE_PARA(from, to, ru_nsignals); \
317 		PHP_RUSAGE_PARA(from, to, ru_nvcsw); \
318 		PHP_RUSAGE_PARA(from, to, ru_nivcsw); \
319 		PHP_RUSAGE_PARA(from, to, ru_nswap);
320 #else /*_OSD_POSIX*/
321 	#define PHP_RUSAGE_SPECIAL(from, to)
322 #endif
323 
324 #define PHP_RUSAGE_COMMON(from ,to) \
325 	PHP_RUSAGE_PARA(from, to, ru_utime.tv_usec); \
326 	PHP_RUSAGE_PARA(from, to, ru_utime.tv_sec); \
327 	PHP_RUSAGE_PARA(from, to, ru_stime.tv_usec); \
328 	PHP_RUSAGE_PARA(from, to, ru_stime.tv_sec);
329 
330 #define PHP_RUSAGE_TO_ARRAY(from, to) \
331 	if (to) { \
332 		PHP_RUSAGE_SPECIAL(from, to) \
333 		PHP_RUSAGE_COMMON(from, to); \
334 	}
335 
336 /* {{{ Waits on or returns the status of a forked child as defined by the waitpid() system call */
PHP_FUNCTION(pcntl_waitpid)337 PHP_FUNCTION(pcntl_waitpid)
338 {
339 	zend_long pid, options = 0;
340 	zval *z_status = NULL, *z_rusage = NULL;
341 	int status;
342 	pid_t child_id;
343 #ifdef HAVE_WAIT4
344 	struct rusage rusage;
345 #endif
346 
347 	ZEND_PARSE_PARAMETERS_START(2, 4)
348 		Z_PARAM_LONG(pid)
349 		Z_PARAM_ZVAL(z_status)
350 		Z_PARAM_OPTIONAL
351 		Z_PARAM_LONG(options)
352 		Z_PARAM_ZVAL(z_rusage)
353 	ZEND_PARSE_PARAMETERS_END();
354 
355 	status = zval_get_long(z_status);
356 
357 #ifdef HAVE_WAIT4
358 	if (z_rusage) {
359 		z_rusage = zend_try_array_init(z_rusage);
360 		if (!z_rusage) {
361 			RETURN_THROWS();
362 		}
363 
364 		memset(&rusage, 0, sizeof(struct rusage));
365 		child_id = wait4((pid_t) pid, &status, options, &rusage);
366 	} else {
367 		child_id = waitpid((pid_t) pid, &status, options);
368 	}
369 #else
370 	child_id = waitpid((pid_t) pid, &status, options);
371 #endif
372 
373 	if (child_id < 0) {
374 		PCNTL_G(last_error) = errno;
375 	}
376 
377 #ifdef HAVE_WAIT4
378 	if (child_id > 0) {
379 		PHP_RUSAGE_TO_ARRAY(rusage, z_rusage);
380 	}
381 #endif
382 
383 	ZEND_TRY_ASSIGN_REF_LONG(z_status, status);
384 
385 	RETURN_LONG((zend_long) child_id);
386 }
387 /* }}} */
388 
389 /* {{{ Waits on or returns the status of a forked child as defined by the waitpid() system call */
PHP_FUNCTION(pcntl_wait)390 PHP_FUNCTION(pcntl_wait)
391 {
392 	zend_long options = 0;
393 	zval *z_status = NULL, *z_rusage = NULL;
394 	int status;
395 	pid_t child_id;
396 #ifdef HAVE_WAIT3
397 	struct rusage rusage;
398 #endif
399 
400 	ZEND_PARSE_PARAMETERS_START(1, 3)
401 		Z_PARAM_ZVAL(z_status)
402 		Z_PARAM_OPTIONAL
403 		Z_PARAM_LONG(options)
404 		Z_PARAM_ZVAL(z_rusage)
405 	ZEND_PARSE_PARAMETERS_END();
406 
407 	status = zval_get_long(z_status);
408 #ifdef HAVE_WAIT3
409 	if (z_rusage) {
410 		z_rusage = zend_try_array_init(z_rusage);
411 		if (!z_rusage) {
412 			RETURN_THROWS();
413 		}
414 
415 		memset(&rusage, 0, sizeof(struct rusage));
416 		child_id = wait3(&status, options, &rusage);
417 	} else if (options) {
418 		child_id = wait3(&status, options, NULL);
419 	} else {
420 		child_id = wait(&status);
421 	}
422 #else
423 	child_id = wait(&status);
424 #endif
425 	if (child_id < 0) {
426 		PCNTL_G(last_error) = errno;
427 	}
428 
429 #ifdef HAVE_WAIT3
430 	if (child_id > 0) {
431 		PHP_RUSAGE_TO_ARRAY(rusage, z_rusage);
432 	}
433 #endif
434 
435 	ZEND_TRY_ASSIGN_REF_LONG(z_status, status);
436 
437 	RETURN_LONG((zend_long) child_id);
438 }
439 /* }}} */
440 
441 #undef PHP_RUSAGE_PARA
442 #undef PHP_RUSAGE_SPECIAL
443 #undef PHP_RUSAGE_COMMON
444 #undef PHP_RUSAGE_TO_ARRAY
445 
446 /* {{{ Returns true if the child status code represents a successful exit */
PHP_FUNCTION(pcntl_wifexited)447 PHP_FUNCTION(pcntl_wifexited)
448 {
449 	zend_long status_word;
450 
451 	ZEND_PARSE_PARAMETERS_START(1, 1)
452 		Z_PARAM_LONG(status_word)
453 	ZEND_PARSE_PARAMETERS_END();
454 
455 #ifdef WIFEXITED
456 	int int_status_word = (int) status_word;
457 	if (WIFEXITED(int_status_word)) {
458 		RETURN_TRUE;
459 	}
460 #endif
461 
462 	RETURN_FALSE;
463 }
464 /* }}} */
465 
466 /* {{{ Returns true if the child status code represents a stopped process (WUNTRACED must have been used with waitpid) */
PHP_FUNCTION(pcntl_wifstopped)467 PHP_FUNCTION(pcntl_wifstopped)
468 {
469 	zend_long status_word;
470 
471 	ZEND_PARSE_PARAMETERS_START(1, 1)
472 		Z_PARAM_LONG(status_word)
473 	ZEND_PARSE_PARAMETERS_END();
474 
475 #ifdef WIFSTOPPED
476 	int int_status_word = (int) status_word;
477 	if (WIFSTOPPED(int_status_word)) {
478 		RETURN_TRUE;
479 	}
480 #endif
481 
482 	RETURN_FALSE;
483 }
484 /* }}} */
485 
486 /* {{{ Returns true if the child status code represents a process that was terminated due to a signal */
PHP_FUNCTION(pcntl_wifsignaled)487 PHP_FUNCTION(pcntl_wifsignaled)
488 {
489 	zend_long status_word;
490 
491 	ZEND_PARSE_PARAMETERS_START(1, 1)
492 		Z_PARAM_LONG(status_word)
493 	ZEND_PARSE_PARAMETERS_END();
494 
495 #ifdef WIFSIGNALED
496 	int int_status_word = (int) status_word;
497 	if (WIFSIGNALED(int_status_word)) {
498 		RETURN_TRUE;
499 	}
500 #endif
501 
502 	RETURN_FALSE;
503 }
504 /* }}} */
505 
506 /* {{{ Returns true if the child status code represents a process that was resumed due to a SIGCONT signal */
PHP_FUNCTION(pcntl_wifcontinued)507 PHP_FUNCTION(pcntl_wifcontinued)
508 {
509 	zend_long status_word;
510 
511 	ZEND_PARSE_PARAMETERS_START(1, 1)
512 		Z_PARAM_LONG(status_word)
513 	ZEND_PARSE_PARAMETERS_END();
514 
515 #ifdef HAVE_WCONTINUED
516 	int int_status_word = (int) status_word;
517 	if (WIFCONTINUED(int_status_word)) {
518 		RETURN_TRUE;
519 	}
520 #endif
521 	RETURN_FALSE;
522 }
523 /* }}} */
524 
525 
526 /* {{{ Returns the status code of a child's exit */
PHP_FUNCTION(pcntl_wexitstatus)527 PHP_FUNCTION(pcntl_wexitstatus)
528 {
529 	zend_long status_word;
530 
531 	ZEND_PARSE_PARAMETERS_START(1, 1)
532 		Z_PARAM_LONG(status_word)
533 	ZEND_PARSE_PARAMETERS_END();
534 
535 #ifdef WEXITSTATUS
536 	int int_status_word = (int) status_word;
537 	RETURN_LONG(WEXITSTATUS(int_status_word));
538 #else
539 	RETURN_FALSE;
540 #endif
541 }
542 /* }}} */
543 
544 /* {{{ Returns the number of the signal that terminated the process who's status code is passed  */
PHP_FUNCTION(pcntl_wtermsig)545 PHP_FUNCTION(pcntl_wtermsig)
546 {
547 	zend_long status_word;
548 
549 	ZEND_PARSE_PARAMETERS_START(1, 1)
550 		Z_PARAM_LONG(status_word)
551 	ZEND_PARSE_PARAMETERS_END();
552 
553 #ifdef WTERMSIG
554 	int int_status_word = (int) status_word;
555 	RETURN_LONG(WTERMSIG(int_status_word));
556 #else
557 	RETURN_FALSE;
558 #endif
559 }
560 /* }}} */
561 
562 /* {{{ Returns the number of the signal that caused the process to stop who's status code is passed */
PHP_FUNCTION(pcntl_wstopsig)563 PHP_FUNCTION(pcntl_wstopsig)
564 {
565 	zend_long status_word;
566 
567 	ZEND_PARSE_PARAMETERS_START(1, 1)
568 		Z_PARAM_LONG(status_word)
569 	ZEND_PARSE_PARAMETERS_END();
570 
571 #ifdef WSTOPSIG
572 	int int_status_word = (int) status_word;
573 	RETURN_LONG(WSTOPSIG(int_status_word));
574 #else
575 	RETURN_FALSE;
576 #endif
577 }
578 /* }}} */
579 
580 /* {{{ Executes specified program in current process space as defined by exec(2) */
PHP_FUNCTION(pcntl_exec)581 PHP_FUNCTION(pcntl_exec)
582 {
583 	zval *args = NULL, *envs = NULL;
584 	zval *element;
585 	HashTable *args_hash, *envs_hash;
586 	int argc = 0, argi = 0;
587 	int envc = 0, envi = 0;
588 	char **argv = NULL, **envp = NULL;
589 	char **current_arg, **pair;
590 	size_t pair_length;
591 	zend_string *key;
592 	char *path;
593 	size_t path_len;
594 	zend_ulong key_num;
595 
596 	ZEND_PARSE_PARAMETERS_START(1, 3)
597 		Z_PARAM_PATH(path, path_len)
598 		Z_PARAM_OPTIONAL
599 		Z_PARAM_ARRAY(args)
600 		Z_PARAM_ARRAY(envs)
601 	ZEND_PARSE_PARAMETERS_END();
602 
603 	if (ZEND_NUM_ARGS() > 1) {
604 		/* Build argument list */
605 		SEPARATE_ARRAY(args);
606 		args_hash = Z_ARRVAL_P(args);
607 		argc = zend_hash_num_elements(args_hash);
608 
609 		argv = safe_emalloc((argc + 2), sizeof(char *), 0);
610 		*argv = path;
611 		current_arg = argv+1;
612 		ZEND_HASH_FOREACH_VAL(args_hash, element) {
613 			if (argi >= argc) break;
614 			if (!try_convert_to_string(element)) {
615 				efree(argv);
616 				RETURN_THROWS();
617 			}
618 
619 			*current_arg = Z_STRVAL_P(element);
620 			argi++;
621 			current_arg++;
622 		} ZEND_HASH_FOREACH_END();
623 		*current_arg = NULL;
624 	} else {
625 		argv = emalloc(2 * sizeof(char *));
626 		argv[0] = path;
627 		argv[1] = NULL;
628 	}
629 
630 	if ( ZEND_NUM_ARGS() == 3 ) {
631 		/* Build environment pair list */
632 		SEPARATE_ARRAY(envs);
633 		envs_hash = Z_ARRVAL_P(envs);
634 		envc = zend_hash_num_elements(envs_hash);
635 
636 		pair = envp = safe_emalloc((envc + 1), sizeof(char *), 0);
637 		ZEND_HASH_FOREACH_KEY_VAL(envs_hash, key_num, key, element) {
638 			if (envi >= envc) break;
639 			if (!key) {
640 				key = zend_long_to_str(key_num);
641 			} else {
642 				zend_string_addref(key);
643 			}
644 
645 			if (!try_convert_to_string(element)) {
646 				zend_string_release(key);
647 				efree(argv);
648 				efree(envp);
649 				RETURN_THROWS();
650 			}
651 
652 			/* Length of element + equal sign + length of key + null */
653 			ZEND_ASSERT(Z_STRLEN_P(element) < SIZE_MAX && ZSTR_LEN(key) < SIZE_MAX);
654 			*pair = safe_emalloc(Z_STRLEN_P(element) + 1, sizeof(char), ZSTR_LEN(key) + 1);
655 			pair_length = Z_STRLEN_P(element) + ZSTR_LEN(key) + 2;
656 			strlcpy(*pair, ZSTR_VAL(key), ZSTR_LEN(key) + 1);
657 			strlcat(*pair, "=", pair_length);
658 			strlcat(*pair, Z_STRVAL_P(element), pair_length);
659 
660 			/* Cleanup */
661 			zend_string_release_ex(key, 0);
662 			envi++;
663 			pair++;
664 		} ZEND_HASH_FOREACH_END();
665 		*(pair) = NULL;
666 
667 		if (execve(path, argv, envp) == -1) {
668 			PCNTL_G(last_error) = errno;
669 			php_error_docref(NULL, E_WARNING, "Error has occurred: (errno %d) %s", errno, strerror(errno));
670 		}
671 
672 		/* Cleanup */
673 		for (pair = envp; *pair != NULL; pair++) efree(*pair);
674 		efree(envp);
675 	} else {
676 
677 		if (execv(path, argv) == -1) {
678 			PCNTL_G(last_error) = errno;
679 			php_error_docref(NULL, E_WARNING, "Error has occurred: (errno %d) %s", errno, strerror(errno));
680 		}
681 	}
682 
683 	efree(argv);
684 
685 	RETURN_FALSE;
686 }
687 /* }}} */
688 
689 /* {{{ Assigns a system signal handler to a PHP function */
PHP_FUNCTION(pcntl_signal)690 PHP_FUNCTION(pcntl_signal)
691 {
692 	zval *handle;
693 	zend_long signo;
694 	bool restart_syscalls = 1;
695 	bool restart_syscalls_is_null = 1;
696 
697 	ZEND_PARSE_PARAMETERS_START(2, 3)
698 		Z_PARAM_LONG(signo)
699 		Z_PARAM_ZVAL(handle)
700 		Z_PARAM_OPTIONAL
701 		Z_PARAM_BOOL_OR_NULL(restart_syscalls, restart_syscalls_is_null)
702 	ZEND_PARSE_PARAMETERS_END();
703 
704 	if (signo < 1) {
705 		zend_argument_value_error(1, "must be greater than or equal to 1");
706 		RETURN_THROWS();
707 	}
708 
709 	if (signo >= PCNTL_G(num_signals)) {
710 		zend_argument_value_error(1, "must be less than %d", PCNTL_G(num_signals));
711 		RETURN_THROWS();
712 	}
713 
714 	if (!PCNTL_G(spares)) {
715 		/* since calling malloc() from within a signal handler is not portable,
716 		 * pre-allocate a few records for recording signals */
717 		for (unsigned int i = 0; i < PCNTL_G(num_signals); i++) {
718 			struct php_pcntl_pending_signal *psig;
719 
720 			psig = emalloc(sizeof(*psig));
721 			psig->next = PCNTL_G(spares);
722 			PCNTL_G(spares) = psig;
723 		}
724 	}
725 
726 	/* If restart_syscalls was not explicitly specified and the signal is SIGALRM, then default
727 	 * restart_syscalls to false. PHP used to enforce that restart_syscalls is false for SIGALRM,
728 	 * so we keep this differing default to reduce the degree of BC breakage. */
729 	if (restart_syscalls_is_null && signo == SIGALRM) {
730 		restart_syscalls = 0;
731 	}
732 
733 	/* Special long value case for SIG_DFL and SIG_IGN */
734 	if (Z_TYPE_P(handle) == IS_LONG) {
735 		if (Z_LVAL_P(handle) != (zend_long) SIG_DFL && Z_LVAL_P(handle) != (zend_long) SIG_IGN) {
736 			zend_argument_value_error(2, "must be either SIG_DFL or SIG_IGN when an integer value is given");
737 			RETURN_THROWS();
738 		}
739 		if (php_signal(signo, (Sigfunc *) Z_LVAL_P(handle), (int) restart_syscalls) == (void *)SIG_ERR) {
740 			PCNTL_G(last_error) = errno;
741 			php_error_docref(NULL, E_WARNING, "Error assigning signal");
742 			RETURN_FALSE;
743 		}
744 		zend_hash_index_update(&PCNTL_G(php_signal_table), signo, handle);
745 		RETURN_TRUE;
746 	}
747 
748 	if (!zend_is_callable_ex(handle, NULL, 0, NULL, NULL, NULL)) {
749 		PCNTL_G(last_error) = EINVAL;
750 
751 		zend_argument_type_error(2, "must be of type callable|int, %s given", zend_zval_value_name(handle));
752 		RETURN_THROWS();
753 	}
754 
755 	/* Add the function name to our signal table */
756 	handle = zend_hash_index_update(&PCNTL_G(php_signal_table), signo, handle);
757 	Z_TRY_ADDREF_P(handle);
758 
759 	if (php_signal4(signo, pcntl_signal_handler, (int) restart_syscalls, 1) == (void *)SIG_ERR) {
760 		PCNTL_G(last_error) = errno;
761 		php_error_docref(NULL, E_WARNING, "Error assigning signal");
762 		RETURN_FALSE;
763 	}
764 	RETURN_TRUE;
765 }
766 /* }}} */
767 
768 /* {{{ Gets signal handler */
PHP_FUNCTION(pcntl_signal_get_handler)769 PHP_FUNCTION(pcntl_signal_get_handler)
770 {
771 	zval *prev_handle;
772 	zend_long signo;
773 
774 	ZEND_PARSE_PARAMETERS_START(1, 1)
775 		Z_PARAM_LONG(signo)
776 	ZEND_PARSE_PARAMETERS_END();
777 
778 	// note: max signal on mac is SIGUSR2 (31), no real time signals.
779 	int sigmax = NSIG - 1;
780 #if defined(SIGRTMAX)
781 	// oddily enough, NSIG on freebsd reports only 32 whereas SIGRTMIN starts at 65.
782 	if (sigmax < SIGRTMAX) {
783 		sigmax = SIGRTMAX;
784 	}
785 #endif
786 
787 	if (signo < 1 || signo > sigmax) {
788 		zend_argument_value_error(1, "must be between 1 and %d", sigmax);
789 		RETURN_THROWS();
790 	}
791 
792 	if ((prev_handle = zend_hash_index_find(&PCNTL_G(php_signal_table), signo)) != NULL) {
793 		RETURN_COPY(prev_handle);
794 	} else {
795 		RETURN_LONG((zend_long)SIG_DFL);
796 	}
797 }
798 
799 /* {{{ Dispatch signals to signal handlers */
PHP_FUNCTION(pcntl_signal_dispatch)800 PHP_FUNCTION(pcntl_signal_dispatch)
801 {
802 	ZEND_PARSE_PARAMETERS_NONE();
803 
804 	pcntl_signal_dispatch();
805 	RETURN_TRUE;
806 }
807 /* }}} */
808 
809 /* Common helper function for these 3 wrapper functions */
810 #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)811 static bool php_pcntl_set_user_signal_infos(
812 	/* const */ HashTable *const user_signals,
813 	sigset_t *const set,
814 	size_t arg_num,
815 	bool allow_empty_signal_array
816 ) {
817 	if (!allow_empty_signal_array && zend_hash_num_elements(user_signals) == 0) {
818 		zend_argument_value_error(arg_num, "cannot be empty");
819 		return false;
820 	}
821 
822 	errno = 0;
823 	if (sigemptyset(set) != 0) {
824 		PCNTL_G(last_error) = errno;
825 		php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
826 		return false;
827 	}
828 
829 	zval *user_signal_no;
830 	ZEND_HASH_FOREACH_VAL(user_signals, user_signal_no) {
831 		bool failed = true;
832 		zend_long tmp = zval_try_get_long(user_signal_no, &failed);
833 
834 		if (failed) {
835 			zend_argument_type_error(arg_num, "signals must be of type int, %s given", zend_zval_value_name(user_signal_no));
836 			return false;
837 		}
838 		/* Signals are positive integers */
839 		if (tmp < 1 || tmp >= PCNTL_G(num_signals)) {
840 			/* PCNTL_G(num_signals) stores +1 from the last valid signal */
841 			zend_argument_value_error(arg_num, "signals must be between 1 and %d", PCNTL_G(num_signals)-1);
842 			return false;
843 		}
844 
845 		int signal_no = (int) tmp;
846 		errno = 0;
847 		if (sigaddset(set, signal_no) != 0) {
848 			PCNTL_G(last_error) = errno;
849 			php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
850 			return false;
851 		}
852 	} ZEND_HASH_FOREACH_END();
853 	return true;
854 }
855 #endif
856 
857 #ifdef HAVE_SIGPROCMASK
858 /* {{{ Examine and change blocked signals */
PHP_FUNCTION(pcntl_sigprocmask)859 PHP_FUNCTION(pcntl_sigprocmask)
860 {
861 	zend_long how;
862 	HashTable *user_set;
863 	/* Optional by-ref out-param array of old signals */
864 	zval *user_old_set = NULL;
865 
866 	ZEND_PARSE_PARAMETERS_START(2, 3)
867 		Z_PARAM_LONG(how)
868 		Z_PARAM_ARRAY_HT(user_set)
869 		Z_PARAM_OPTIONAL
870 		Z_PARAM_ZVAL(user_old_set)
871 	ZEND_PARSE_PARAMETERS_END();
872 
873 	if (how != SIG_BLOCK && how != SIG_UNBLOCK && how != SIG_SETMASK) {
874 		zend_argument_value_error(1, "must be one of SIG_BLOCK, SIG_UNBLOCK, or SIG_SETMASK");
875 		RETURN_THROWS();
876 	}
877 
878 	errno = 0;
879 	sigset_t old_set;
880 	if (sigemptyset(&old_set) != 0) {
881 		PCNTL_G(last_error) = errno;
882 		php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
883 		RETURN_FALSE;
884 	}
885 
886 	sigset_t set;
887 	bool status = php_pcntl_set_user_signal_infos(user_set, &set, 2, /* allow_empty_signal_array */ how == SIG_SETMASK);
888 	/* Some error occurred */
889 	if (!status) {
890 		RETURN_FALSE;
891 	}
892 
893 	if (sigprocmask(how, &set, &old_set) != 0) {
894 		PCNTL_G(last_error) = errno;
895 		php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
896 		RETURN_FALSE;
897 	}
898 
899 	if (user_old_set != NULL) {
900 		user_old_set = zend_try_array_init(user_old_set);
901 		if (!user_old_set) {
902 			RETURN_THROWS();
903 		}
904 
905 		for (unsigned int signal_no = 1; signal_no < PCNTL_G(num_signals); ++signal_no) {
906 			if (sigismember(&old_set, signal_no) != 1) {
907 				continue;
908 			}
909 			add_next_index_long(user_old_set, signal_no);
910 		}
911 	}
912 
913 	RETURN_TRUE;
914 }
915 /* }}} */
916 #endif
917 
918 #ifdef HAVE_STRUCT_SIGINFO_T
919 # ifdef HAVE_SIGWAITINFO
920 
921 /* {{{ Synchronously wait for queued signals */
PHP_FUNCTION(pcntl_sigwaitinfo)922 PHP_FUNCTION(pcntl_sigwaitinfo)
923 {
924 	HashTable *user_set;
925 	/* Optional by-ref array of ints */
926 	zval *user_siginfo = NULL;
927 
928 	ZEND_PARSE_PARAMETERS_START(1, 2)
929 		Z_PARAM_ARRAY_HT(user_set)
930 		Z_PARAM_OPTIONAL
931 		Z_PARAM_ZVAL(user_siginfo)
932 	ZEND_PARSE_PARAMETERS_END();
933 
934 	sigset_t set;
935 	bool status = php_pcntl_set_user_signal_infos(user_set, &set, 1, /* allow_empty_signal_array */ false);
936 	/* Some error occurred */
937 	if (!status) {
938 		RETURN_FALSE;
939 	}
940 
941 	errno = 0;
942 	siginfo_t siginfo;
943 	int signal_no = sigwaitinfo(&set, &siginfo);
944 	/* sigwaitinfo() never sets errno to EAGAIN according to POSIX */
945 	if (signal_no == -1) {
946 		PCNTL_G(last_error) = errno;
947 		php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
948 		RETURN_FALSE;
949 	}
950 
951 	/* sigwaitinfo can return 0 on success on some platforms, e.g. NetBSD */
952 	if (!signal_no && siginfo.si_signo) {
953 		signal_no = siginfo.si_signo;
954 	}
955 
956 	pcntl_siginfo_to_zval(signal_no, &siginfo, user_siginfo);
957 
958 	RETURN_LONG(signal_no);
959 }
960 /* }}} */
961 # endif
962 # ifdef HAVE_SIGTIMEDWAIT
963 /* {{{ Wait for queued signals */
PHP_FUNCTION(pcntl_sigtimedwait)964 PHP_FUNCTION(pcntl_sigtimedwait)
965 {
966 	HashTable *user_set;
967 	/* Optional by-ref array of ints */
968 	zval *user_siginfo = NULL;
969 	zend_long tv_sec = 0;
970 	zend_long tv_nsec = 0;
971 
972 	ZEND_PARSE_PARAMETERS_START(1, 4)
973 		Z_PARAM_ARRAY_HT(user_set)
974 		Z_PARAM_OPTIONAL
975 		Z_PARAM_ZVAL(user_siginfo)
976 		Z_PARAM_LONG(tv_sec)
977 		Z_PARAM_LONG(tv_nsec)
978 	ZEND_PARSE_PARAMETERS_END();
979 
980 	sigset_t set;
981 	bool status = php_pcntl_set_user_signal_infos(user_set, &set, 1, /* allow_empty_signal_array */ false);
982 	/* Some error occurred */
983 	if (!status) {
984 		RETURN_FALSE;
985 	}
986 	if (tv_sec < 0) {
987 		zend_argument_value_error(3, "must be greater than or equal to 0");
988 		RETURN_THROWS();
989 	}
990 	/* Nanosecond between 0 and 1e9 */
991 	if (tv_nsec < 0 || tv_nsec >= 1000000000) {
992 		zend_argument_value_error(4, "must be between 0 and 1e9");
993 		RETURN_THROWS();
994 	}
995 	if (UNEXPECTED(tv_sec == 0 && tv_nsec == 0)) {
996 		zend_value_error("pcntl_sigtimedwait(): At least one of argument #3 ($seconds) or argument #4 ($nanoseconds) must be greater than 0");
997 		RETURN_THROWS();
998 	}
999 
1000 	errno = 0;
1001 	siginfo_t siginfo;
1002 	struct timespec timeout;
1003 	timeout.tv_sec  = (time_t) tv_sec;
1004 	timeout.tv_nsec = tv_nsec;
1005 	int signal_no = sigtimedwait(&set, &siginfo, &timeout);
1006 	if (signal_no == -1) {
1007 		if (errno != EAGAIN) {
1008 			PCNTL_G(last_error) = errno;
1009 			php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
1010 		}
1011 		RETURN_FALSE;
1012 	}
1013 
1014 	/* sigtimedwait can return 0 on success on some platforms, e.g. NetBSD */
1015 	if (!signal_no && siginfo.si_signo) {
1016 		signal_no = siginfo.si_signo;
1017 	}
1018 
1019 	pcntl_siginfo_to_zval(signal_no, &siginfo, user_siginfo);
1020 
1021 	RETURN_LONG(signal_no);
1022 }
1023 /* }}} */
1024 # endif
1025 
pcntl_siginfo_to_zval(int signo,siginfo_t * siginfo,zval * user_siginfo)1026 static void pcntl_siginfo_to_zval(int signo, siginfo_t *siginfo, zval *user_siginfo) /* {{{ */
1027 {
1028 	if (signo > 0 && user_siginfo) {
1029 		user_siginfo = zend_try_array_init(user_siginfo);
1030 		if (!user_siginfo) {
1031 			return;
1032 		}
1033 
1034 		add_assoc_long_ex(user_siginfo, "signo", sizeof("signo")-1, siginfo->si_signo);
1035 		add_assoc_long_ex(user_siginfo, "errno", sizeof("errno")-1, siginfo->si_errno);
1036 		add_assoc_long_ex(user_siginfo, "code",  sizeof("code")-1,  siginfo->si_code);
1037 		switch(signo) {
1038 #ifdef SIGCHLD
1039 			case SIGCHLD:
1040 				add_assoc_long_ex(user_siginfo,   "status", sizeof("status")-1, siginfo->si_status);
1041 # ifdef si_utime
1042 				add_assoc_double_ex(user_siginfo, "utime",  sizeof("utime")-1,  siginfo->si_utime);
1043 # endif
1044 # ifdef si_stime
1045 				add_assoc_double_ex(user_siginfo, "stime",  sizeof("stime")-1,  siginfo->si_stime);
1046 # endif
1047 				add_assoc_long_ex(user_siginfo,   "pid",    sizeof("pid")-1,    siginfo->si_pid);
1048 				add_assoc_long_ex(user_siginfo,   "uid",    sizeof("uid")-1,    siginfo->si_uid);
1049 				break;
1050 			case SIGUSR1:
1051 			case SIGUSR2:
1052 				add_assoc_long_ex(user_siginfo,   "pid",    sizeof("pid")-1,    siginfo->si_pid);
1053 				add_assoc_long_ex(user_siginfo,   "uid",    sizeof("uid")-1,    siginfo->si_uid);
1054 				break;
1055 #endif
1056 			case SIGILL:
1057 			case SIGFPE:
1058 			case SIGSEGV:
1059 			case SIGBUS:
1060 				add_assoc_double_ex(user_siginfo, "addr", sizeof("addr")-1, (zend_long)siginfo->si_addr);
1061 				break;
1062 #if defined(SIGPOLL) && !defined(__CYGWIN__)
1063 			case SIGPOLL:
1064 				add_assoc_long_ex(user_siginfo, "band", sizeof("band")-1, siginfo->si_band);
1065 # ifdef si_fd
1066 				add_assoc_long_ex(user_siginfo, "fd",   sizeof("fd")-1,   siginfo->si_fd);
1067 # endif
1068 				break;
1069 #endif
1070 
1071 #ifdef SIGTRAP
1072 			case SIGTRAP:
1073 # if defined(si_syscall) && defined(__FreeBSD__)
1074 				if (siginfo->si_code == TRAP_CAP) {
1075 					add_assoc_long_ex(user_siginfo, "syscall", sizeof("syscall")-1, (zend_long)siginfo->si_syscall);
1076 				} else {
1077 					add_assoc_long_ex(user_siginfo, "trapno", sizeof("trapno")-1, (zend_long)siginfo->si_trapno);
1078 				}
1079 
1080 # endif
1081 				break;
1082 
1083 #endif
1084 		}
1085 #if defined(SIGRTMIN) && defined(SIGRTMAX)
1086 		if (SIGRTMIN <= signo && signo <= SIGRTMAX) {
1087 			add_assoc_long_ex(user_siginfo, "pid", sizeof("pid")-1, siginfo->si_pid);
1088 			add_assoc_long_ex(user_siginfo, "uid", sizeof("uid")-1, siginfo->si_uid);
1089 		}
1090 #endif
1091 	}
1092 }
1093 /* }}} */
1094 #endif
1095 
1096 #ifdef HAVE_GETPRIORITY
1097 /* {{{ Get the priority of any process */
PHP_FUNCTION(pcntl_getpriority)1098 PHP_FUNCTION(pcntl_getpriority)
1099 {
1100 	zend_long who = PRIO_PROCESS;
1101 	zend_long pid;
1102 	bool pid_is_null = 1;
1103 	int pri;
1104 
1105 	ZEND_PARSE_PARAMETERS_START(0, 2)
1106 		Z_PARAM_OPTIONAL
1107 		Z_PARAM_LONG_OR_NULL(pid, pid_is_null)
1108 		Z_PARAM_LONG(who)
1109 	ZEND_PARSE_PARAMETERS_END();
1110 
1111 	/* needs to be cleared, since any returned value is valid */
1112 	errno = 0;
1113 
1114 	pid = pid_is_null ? 0 : pid;
1115 	pri = getpriority(who, pid);
1116 
1117 	if (errno) {
1118 		PCNTL_G(last_error) = errno;
1119 		switch (errno) {
1120 			case ESRCH:
1121 				php_error_docref(NULL, E_WARNING, "Error %d: No process was located using the given parameters", errno);
1122 				break;
1123 			case EINVAL:
1124 #ifdef PRIO_DARWIN_BG
1125 				if (who != PRIO_PGRP && who != PRIO_USER && who != PRIO_PROCESS && who != PRIO_DARWIN_THREAD) {
1126 					zend_argument_value_error(2, "must be one of PRIO_PGRP, PRIO_USER, PRIO_PROCESS or PRIO_DARWIN_THREAD");
1127 					RETURN_THROWS();
1128 				} else if (who == PRIO_DARWIN_THREAD && pid != 0) {
1129 					zend_argument_value_error(1, "must be 0 (zero) if PRIO_DARWIN_THREAD is provided as second parameter");
1130 					RETURN_THROWS();
1131 				} else {
1132 					zend_argument_value_error(1, "is not a valid process, process group, or user ID");
1133 					RETURN_THROWS();
1134 				}
1135 #else
1136 				zend_argument_value_error(2, "must be one of PRIO_PGRP, PRIO_USER, or PRIO_PROCESS");
1137 				RETURN_THROWS();
1138 #endif
1139 
1140 			default:
1141 				php_error_docref(NULL, E_WARNING, "Unknown error %d has occurred", errno);
1142 				break;
1143 		}
1144 		RETURN_FALSE;
1145 	}
1146 
1147 	RETURN_LONG(pri);
1148 }
1149 /* }}} */
1150 #endif
1151 
1152 #ifdef HAVE_SETPRIORITY
1153 /* {{{ Change the priority of any process */
PHP_FUNCTION(pcntl_setpriority)1154 PHP_FUNCTION(pcntl_setpriority)
1155 {
1156 	zend_long who = PRIO_PROCESS;
1157 	zend_long pid;
1158 	bool pid_is_null = 1;
1159 	zend_long pri;
1160 
1161 	ZEND_PARSE_PARAMETERS_START(1, 3)
1162 		Z_PARAM_LONG(pri)
1163 		Z_PARAM_OPTIONAL
1164 		Z_PARAM_LONG_OR_NULL(pid, pid_is_null)
1165 		Z_PARAM_LONG(who)
1166 	ZEND_PARSE_PARAMETERS_END();
1167 
1168 	pid = pid_is_null ? 0 : pid;
1169 
1170 	if (setpriority(who, pid, pri)) {
1171 		PCNTL_G(last_error) = errno;
1172 		switch (errno) {
1173 			case ESRCH:
1174 				php_error_docref(NULL, E_WARNING, "Error %d: No process was located using the given parameters", errno);
1175 				break;
1176 			case EINVAL:
1177 #ifdef PRIO_DARWIN_BG
1178 				if (who != PRIO_PGRP && who != PRIO_USER && who != PRIO_PROCESS && who != PRIO_DARWIN_THREAD) {
1179 					zend_argument_value_error(3, "must be one of PRIO_PGRP, PRIO_USER, PRIO_PROCESS or PRIO_DARWIN_THREAD");
1180 					RETURN_THROWS();
1181 				} else if (who == PRIO_DARWIN_THREAD && pid != 0) {
1182 					zend_argument_value_error(2, "must be 0 (zero) if PRIO_DARWIN_THREAD is provided as second parameter");
1183 					RETURN_THROWS();
1184 				} else if (who == PRIO_DARWIN_THREAD && pid == 0 && (pri != 0 && pri != PRIO_DARWIN_BG)) {
1185 					zend_argument_value_error(1, "must be either 0 (zero) or PRIO_DARWIN_BG, for mode PRIO_DARWIN_THREAD");
1186 					RETURN_THROWS();
1187 				} else {
1188 					zend_argument_value_error(2, "is not a valid process, process group, or user ID");
1189 					RETURN_THROWS();
1190 				}
1191 #else
1192 				zend_argument_value_error(3, "must be one of PRIO_PGRP, PRIO_USER, or PRIO_PROCESS");
1193 				RETURN_THROWS();
1194 #endif
1195 			case EPERM:
1196 				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);
1197 				break;
1198 			case EACCES:
1199 				php_error_docref(NULL, E_WARNING, "Error %d: Only a super user may attempt to increase the process priority", errno);
1200 				break;
1201 			default:
1202 				php_error_docref(NULL, E_WARNING, "Unknown error %d has occurred", errno);
1203 				break;
1204 		}
1205 		RETURN_FALSE;
1206 	}
1207 
1208 	RETURN_TRUE;
1209 }
1210 /* }}} */
1211 #endif
1212 
1213 /* {{{ Retrieve the error number set by the last pcntl function which failed. */
PHP_FUNCTION(pcntl_get_last_error)1214 PHP_FUNCTION(pcntl_get_last_error)
1215 {
1216 	ZEND_PARSE_PARAMETERS_NONE();
1217 
1218 	RETURN_LONG(PCNTL_G(last_error));
1219 }
1220 /* }}} */
1221 
1222 /* {{{ Retrieve the system error message associated with the given errno. */
PHP_FUNCTION(pcntl_strerror)1223 PHP_FUNCTION(pcntl_strerror)
1224 {
1225 	zend_long error;
1226 
1227 	ZEND_PARSE_PARAMETERS_START(1, 1)
1228 		Z_PARAM_LONG(error)
1229 	ZEND_PARSE_PARAMETERS_END();
1230 
1231 	RETURN_STRING(strerror(error));
1232 }
1233 /* }}} */
1234 
1235 /* Our custom signal handler that calls the appropriate php_function */
1236 #ifdef HAVE_STRUCT_SIGINFO_T
pcntl_signal_handler(int signo,siginfo_t * siginfo,void * context)1237 static void pcntl_signal_handler(int signo, siginfo_t *siginfo, void *context)
1238 #else
1239 static void pcntl_signal_handler(int signo)
1240 #endif
1241 {
1242 	struct php_pcntl_pending_signal *psig = PCNTL_G(spares);
1243 	if (!psig) {
1244 		/* oops, too many signals for us to track, so we'll forget about this one */
1245 		return;
1246 	}
1247 	PCNTL_G(spares) = psig->next;
1248 
1249 	psig->signo = signo;
1250 	psig->next = NULL;
1251 
1252 #ifdef HAVE_STRUCT_SIGINFO_T
1253 	psig->siginfo = *siginfo;
1254 #endif
1255 
1256 	/* the head check is important, as the tick handler cannot atomically clear both
1257 	 * the head and tail */
1258 	if (PCNTL_G(head) && PCNTL_G(tail)) {
1259 		PCNTL_G(tail)->next = psig;
1260 	} else {
1261 		PCNTL_G(head) = psig;
1262 	}
1263 	PCNTL_G(tail) = psig;
1264 	PCNTL_G(pending_signals) = 1;
1265 	if (PCNTL_G(async_signals)) {
1266 		zend_atomic_bool_store_ex(&EG(vm_interrupt), true);
1267 	}
1268 }
1269 
pcntl_signal_dispatch(void)1270 void pcntl_signal_dispatch(void)
1271 {
1272 	zval params[2], *handle, retval;
1273 	struct php_pcntl_pending_signal *queue, *next;
1274 	sigset_t mask;
1275 	sigset_t old_mask;
1276 
1277 	if(!PCNTL_G(pending_signals)) {
1278 		return;
1279 	}
1280 
1281 	/* Mask all signals */
1282 	sigfillset(&mask);
1283 	sigprocmask(SIG_BLOCK, &mask, &old_mask);
1284 
1285 	/* Bail if the queue is empty or if we are already playing the queue */
1286 	if (!PCNTL_G(head) || PCNTL_G(processing_signal_queue)) {
1287 		sigprocmask(SIG_SETMASK, &old_mask, NULL);
1288 		return;
1289 	}
1290 
1291 	/* Prevent switching fibers when handling signals */
1292 	zend_fiber_switch_block();
1293 
1294 	/* Prevent reentrant handler calls */
1295 	PCNTL_G(processing_signal_queue) = 1;
1296 
1297 	queue = PCNTL_G(head);
1298 	PCNTL_G(head) = NULL; /* simple stores are atomic */
1299 
1300 	/* Allocate */
1301 	while (queue) {
1302 		if ((handle = zend_hash_index_find(&PCNTL_G(php_signal_table), queue->signo)) != NULL) {
1303 			if (Z_TYPE_P(handle) != IS_LONG) {
1304 				ZVAL_NULL(&retval);
1305 				ZVAL_LONG(&params[0], queue->signo);
1306 #ifdef HAVE_STRUCT_SIGINFO_T
1307 				array_init(&params[1]);
1308 				pcntl_siginfo_to_zval(queue->signo, &queue->siginfo, &params[1]);
1309 #else
1310 				ZVAL_NULL(&params[1]);
1311 #endif
1312 
1313 				/* Call php signal handler - Note that we do not report errors, and we ignore the return value */
1314 				/* FIXME: this is probably broken when multiple signals are handled in this while loop (retval) */
1315 				call_user_function(NULL, NULL, handle, &retval, 2, params);
1316 				zval_ptr_dtor(&retval);
1317 #ifdef HAVE_STRUCT_SIGINFO_T
1318 				zval_ptr_dtor(&params[1]);
1319 #endif
1320 			}
1321 		}
1322 
1323 		next = queue->next;
1324 		queue->next = PCNTL_G(spares);
1325 		PCNTL_G(spares) = queue;
1326 		queue = next;
1327 	}
1328 
1329 	PCNTL_G(pending_signals) = 0;
1330 
1331 	/* Re-enable queue */
1332 	PCNTL_G(processing_signal_queue) = 0;
1333 
1334 	/* Re-enable fiber switching */
1335 	zend_fiber_switch_unblock();
1336 
1337 	/* return signal mask to previous state */
1338 	sigprocmask(SIG_SETMASK, &old_mask, NULL);
1339 }
1340 
pcntl_signal_dispatch_tick_function(int dummy_int,void * dummy_pointer)1341 static void pcntl_signal_dispatch_tick_function(int dummy_int, void *dummy_pointer)
1342 {
1343 	return pcntl_signal_dispatch();
1344 }
1345 
1346 /* {{{ Enable/disable asynchronous signal handling and return the old setting. */
PHP_FUNCTION(pcntl_async_signals)1347 PHP_FUNCTION(pcntl_async_signals)
1348 {
1349 	bool on, on_is_null = 1;
1350 
1351 	ZEND_PARSE_PARAMETERS_START(0, 1)
1352 		Z_PARAM_OPTIONAL
1353 		Z_PARAM_BOOL_OR_NULL(on, on_is_null)
1354 	ZEND_PARSE_PARAMETERS_END();
1355 
1356 	if (on_is_null) {
1357 		RETURN_BOOL(PCNTL_G(async_signals));
1358 	}
1359 
1360 	RETVAL_BOOL(PCNTL_G(async_signals));
1361 	PCNTL_G(async_signals) = on;
1362 }
1363 /* }}} */
1364 
1365 #ifdef HAVE_UNSHARE
1366 /* {{{ disassociate parts of the process execution context */
PHP_FUNCTION(pcntl_unshare)1367 PHP_FUNCTION(pcntl_unshare)
1368 {
1369 	zend_long flags;
1370 
1371 	ZEND_PARSE_PARAMETERS_START(1, 1)
1372 		Z_PARAM_LONG(flags)
1373 	ZEND_PARSE_PARAMETERS_END();
1374 
1375 	if (unshare(flags) == -1) {
1376 		PCNTL_G(last_error) = errno;
1377 		switch (errno) {
1378 #ifdef EINVAL
1379 			case EINVAL:
1380 				zend_argument_value_error(1, "must be a combination of CLONE_* flags, or at least one flag is unsupported by the kernel");
1381 				RETURN_THROWS();
1382 				break;
1383 #endif
1384 #ifdef ENOMEM
1385 			case ENOMEM:
1386 				php_error_docref(NULL, E_WARNING, "Error %d: Insufficient memory for unshare", errno);
1387 				break;
1388 #endif
1389 #ifdef EPERM
1390 			case EPERM:
1391 				php_error_docref(NULL, E_WARNING, "Error %d: No privilege to use these flags", errno);
1392 				break;
1393 #endif
1394 #ifdef ENOSPC
1395 			case ENOSPC:
1396 				php_error_docref(NULL, E_WARNING, "Error %d: Reached the maximum nesting limit for one of the specified namespaces", errno);
1397 				break;
1398 #endif
1399 #ifdef EUSERS
1400 			case EUSERS:
1401 				php_error_docref(NULL, E_WARNING, "Error %d: Reached the maximum nesting limit for the user namespace", errno);
1402 				break;
1403 #endif
1404 			default:
1405 				php_error_docref(NULL, E_WARNING, "Unknown error %d has occurred", errno);
1406 				break;
1407 		}
1408 		RETURN_FALSE;
1409 	}
1410 
1411 	RETURN_TRUE;
1412 }
1413 /* }}} */
1414 #endif
1415 
1416 #ifdef HAVE_RFORK
1417 /* {{{ proto bool pcntl_rfork(int flags [, int signal])
1418    More control over the process creation is given over fork/vfork. */
PHP_FUNCTION(pcntl_rfork)1419 PHP_FUNCTION(pcntl_rfork)
1420 {
1421 	zend_long flags;
1422 	zend_long csignal = 0;
1423 	pid_t pid;
1424 
1425 	ZEND_PARSE_PARAMETERS_START(1, 2)
1426 		Z_PARAM_LONG(flags)
1427 		Z_PARAM_OPTIONAL
1428 		Z_PARAM_LONG(csignal)
1429 	ZEND_PARSE_PARAMETERS_END();
1430 
1431 	/* This is a flag to use with great caution in general, preferably not within PHP */
1432 	if ((flags & RFMEM) != 0) {
1433 		zend_argument_value_error(1, "must not include RFMEM value, not allowed within this context");
1434 		RETURN_THROWS();
1435 	}
1436 
1437 	if ((flags & RFSIGSHARE) != 0) {
1438 		zend_argument_value_error(1, "must not include RFSIGSHARE value, not allowed within this context");
1439 		RETURN_THROWS();
1440 	}
1441 
1442 	if ((flags & (RFFDG | RFCFDG)) == (RFFDG | RFCFDG)) {
1443 		zend_argument_value_error(1, "must not include both RFFDG and RFCFDG, because these flags are mutually exclusive");
1444 		RETURN_THROWS();
1445 	}
1446 
1447 	/* A new pid is required */
1448 	if (!(flags & (RFPROC))) {
1449 		flags |= RFPROC;
1450 	}
1451 
1452 #ifdef RFTSIGZMB
1453 	if ((flags & RFTSIGZMB) != 0) {
1454 		flags |= RFTSIGFLAGS(csignal);
1455 	}
1456 #endif
1457 
1458 	pid = rfork(flags);
1459 
1460 	if (pid == -1) {
1461 		PCNTL_G(last_error) = errno;
1462 		switch (errno) {
1463 			case EAGAIN:
1464 			php_error_docref(NULL, E_WARNING, "Maximum process creations limit reached\n");
1465 		break;
1466 
1467 		default:
1468 			php_error_docref(NULL, E_WARNING, "Error %d", errno);
1469 		}
1470 	}
1471 
1472 	RETURN_LONG((zend_long) pid);
1473 }
1474 #endif
1475 /* }}} */
1476 
1477 #ifdef HAVE_FORKX
1478 /* {{{ proto bool pcntl_forkx(int flags)
1479    More elaborated version of fork with the following settings.
1480    FORK_WAITPID: forbid the parent process to wait for multiple pid but one only
1481    FORK_NOSIGCHLD: SIGCHLD signal ignored when the child terminates */
PHP_FUNCTION(pcntl_forkx)1482 PHP_FUNCTION(pcntl_forkx)
1483 {
1484 	zend_long flags;
1485 	pid_t pid;
1486 
1487 	ZEND_PARSE_PARAMETERS_START(1, 1)
1488 		Z_PARAM_LONG(flags)
1489 	ZEND_PARSE_PARAMETERS_END();
1490 
1491 	if (flags < FORK_NOSIGCHLD || flags > FORK_WAITPID) {
1492 		zend_argument_value_error(1, "must be FORK_NOSIGCHLD or FORK_WAITPID");
1493 		RETURN_THROWS();
1494 	}
1495 
1496 	pid = forkx(flags);
1497 
1498 	if (pid == -1) {
1499 		PCNTL_G(last_error) = errno;
1500 		switch (errno) {
1501 			case EAGAIN:
1502 			php_error_docref(NULL, E_WARNING, "Maximum process creations limit reached\n");
1503 		break;
1504 			case EPERM:
1505 			php_error_docref(NULL, E_WARNING, "Calling process not having the proper privileges\n");
1506 		break;
1507 			case ENOMEM:
1508 			php_error_docref(NULL, E_WARNING, "No swap space left\n");
1509 		break;
1510 		default:
1511 			php_error_docref(NULL, E_WARNING, "Error %d", errno);
1512 		}
1513 	}
1514 
1515 	RETURN_LONG((zend_long) pid);
1516 }
1517 #endif
1518 /* }}} */
1519 
1520 #ifdef HAVE_PIDFD_OPEN
1521 // The `pidfd_open` syscall is available since 5.3
1522 // and `setns` since 3.0.
PHP_FUNCTION(pcntl_setns)1523 PHP_FUNCTION(pcntl_setns)
1524 {
1525 	zend_long pid, nstype = CLONE_NEWNET;
1526 	bool pid_is_null = 1;
1527 	int fd, ret;
1528 
1529 	ZEND_PARSE_PARAMETERS_START(0, 2)
1530 		Z_PARAM_OPTIONAL
1531 		Z_PARAM_LONG_OR_NULL(pid, pid_is_null)
1532 		Z_PARAM_LONG(nstype)
1533 	ZEND_PARSE_PARAMETERS_END();
1534 
1535 	pid = pid_is_null ? getpid() : pid;
1536 	fd = syscall(SYS_pidfd_open, pid, 0);
1537 	if (errno) {
1538 		PCNTL_G(last_error) = errno;
1539 		switch (errno) {
1540 			case EINVAL:
1541 			case ESRCH:
1542 				zend_argument_value_error(1, "is not a valid process (" ZEND_LONG_FMT ")", pid);
1543 				RETURN_THROWS();
1544 
1545 			case ENFILE:
1546 				php_error_docref(NULL, E_WARNING, "Error %d: File descriptors per-process limit reached", errno);
1547 				break;
1548 
1549 			case ENODEV:
1550 				php_error_docref(NULL, E_WARNING, "Error %d: Anonymous inode fs unsupported", errno);
1551 				break;
1552 
1553 			case ENOMEM:
1554 				php_error_docref(NULL, E_WARNING, "Error %d: Insufficient memory for pidfd_open", errno);
1555 				break;
1556 
1557 			default:
1558 			        php_error_docref(NULL, E_WARNING, "Error %d", errno);
1559 		}
1560 		RETURN_FALSE;
1561 	}
1562 	ret = setns(fd, (int)nstype);
1563 	close(fd);
1564 
1565 	if (ret == -1) {
1566 		PCNTL_G(last_error) = errno;
1567 		switch (errno) {
1568 			case ESRCH:
1569 				zend_argument_value_error(1, "process no longer available (" ZEND_LONG_FMT ")", pid);
1570 				RETURN_THROWS();
1571 
1572 			case EINVAL:
1573 				zend_argument_value_error(2, "is an invalid nstype (%d)", nstype);
1574 				RETURN_THROWS();
1575 
1576 			case EPERM:
1577 				php_error_docref(NULL, E_WARNING, "Error %d: No required capability for this process", errno);
1578 				break;
1579 
1580 			default:
1581 			        php_error_docref(NULL, E_WARNING, "Error %d", errno);
1582 		}
1583 		RETURN_FALSE;
1584 	} else {
1585 		RETURN_TRUE;
1586 	}
1587 }
1588 #endif
1589 
1590 #ifdef HAVE_SCHED_SETAFFINITY
PHP_FUNCTION(pcntl_getcpuaffinity)1591 PHP_FUNCTION(pcntl_getcpuaffinity)
1592 {
1593 	zend_long pid;
1594 	bool pid_is_null = 1;
1595 	cpu_set_t mask;
1596 
1597 	ZEND_PARSE_PARAMETERS_START(0, 1)
1598 		Z_PARAM_OPTIONAL
1599 		Z_PARAM_LONG_OR_NULL(pid, pid_is_null)
1600 	ZEND_PARSE_PARAMETERS_END();
1601 
1602 	// 0 == getpid in this context, we're just saving a syscall
1603 	pid = pid_is_null ? 0 : pid;
1604 
1605 	PCNTL_CPU_ZERO(mask);
1606 
1607 	if (sched_getaffinity(pid, PCNTL_CPUSET_SIZE(mask), PCNTL_CPUSET(mask)) != 0) {
1608 		PCNTL_CPU_DESTROY(mask);
1609 		PCNTL_G(last_error) = errno;
1610 		switch (errno) {
1611 			case ESRCH:
1612 				zend_argument_value_error(1, "invalid process (" ZEND_LONG_FMT ")", pid);
1613 				RETURN_THROWS();
1614 			case EPERM:
1615 				php_error_docref(NULL, E_WARNING, "Calling process not having the proper privileges");
1616 				break;
1617 			case EINVAL:
1618 				zend_value_error("invalid cpu affinity mask size");
1619 				RETURN_THROWS();
1620 			default:
1621 				php_error_docref(NULL, E_WARNING, "Error %d", errno);
1622 		}
1623 
1624 		RETURN_FALSE;
1625 	}
1626 
1627 	zend_ulong maxcpus = (zend_ulong)sysconf(_SC_NPROCESSORS_CONF);
1628 	array_init(return_value);
1629 
1630 	for (zend_ulong i = 0; i < maxcpus; i ++) {
1631 		if (PCNTL_CPU_ISSET(i, mask)) {
1632 			add_next_index_long(return_value, i);
1633 		}
1634 	}
1635 	PCNTL_CPU_DESTROY(mask);
1636 }
1637 
PHP_FUNCTION(pcntl_setcpuaffinity)1638 PHP_FUNCTION(pcntl_setcpuaffinity)
1639 {
1640 	zend_long pid;
1641 	bool pid_is_null = 1;
1642 	cpu_set_t mask;
1643 	zval *hmask = NULL, *ncpu;
1644 
1645 	ZEND_PARSE_PARAMETERS_START(0, 2)
1646 		Z_PARAM_OPTIONAL
1647 		Z_PARAM_LONG_OR_NULL(pid, pid_is_null)
1648 		Z_PARAM_ARRAY(hmask)
1649 	ZEND_PARSE_PARAMETERS_END();
1650 
1651 	if (!hmask || zend_hash_num_elements(Z_ARRVAL_P(hmask)) == 0) {
1652 		zend_argument_value_error(2, "must not be empty");
1653 		RETURN_THROWS();
1654 	}
1655 
1656 	// 0 == getpid in this context, we're just saving a syscall
1657 	pid = pid_is_null ? 0 : pid;
1658 	zend_long maxcpus = sysconf(_SC_NPROCESSORS_CONF);
1659 	PCNTL_CPU_ZERO(mask);
1660 
1661 	ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(hmask), ncpu) {
1662 		ZVAL_DEREF(ncpu);
1663 		zend_long cpu;
1664 		if (Z_TYPE_P(ncpu) != IS_LONG) {
1665 			if (Z_TYPE_P(ncpu) == IS_STRING) {
1666 				zend_ulong tmp;
1667 				if (!ZEND_HANDLE_NUMERIC(Z_STR_P(ncpu), tmp)) {
1668 					zend_argument_value_error(2, "cpu id invalid value (%s)", ZSTR_VAL(Z_STR_P(ncpu)));
1669 					PCNTL_CPU_DESTROY(mask);
1670 					RETURN_THROWS();
1671 				}
1672 
1673 				cpu = (zend_long)tmp;
1674 			} else {
1675 				zend_string *wcpu = zval_get_string_func(ncpu);
1676 				zend_argument_value_error(2, "cpu id invalid type (%s)", ZSTR_VAL(wcpu));
1677 				zend_string_release(wcpu);
1678 				PCNTL_CPU_DESTROY(mask);
1679 				RETURN_THROWS();
1680 			}
1681 		} else {
1682 			cpu = Z_LVAL_P(ncpu);
1683 		}
1684 
1685 		if (cpu < 0 || cpu >= maxcpus) {
1686 			zend_argument_value_error(2, "cpu id must be between 0 and " ZEND_ULONG_FMT " (" ZEND_LONG_FMT ")", maxcpus, cpu);
1687 			RETURN_THROWS();
1688 		}
1689 
1690 		if (!PCNTL_CPU_ISSET(cpu, mask)) {
1691 			PCNTL_CPU_SET(cpu, mask);
1692 		}
1693 	} ZEND_HASH_FOREACH_END();
1694 
1695 	if (sched_setaffinity(pid, PCNTL_CPUSET_SIZE(mask), PCNTL_CPUSET(mask)) != 0) {
1696 		PCNTL_CPU_DESTROY(mask);
1697 		PCNTL_G(last_error) = errno;
1698 		switch (errno) {
1699 			case ESRCH:
1700 				zend_argument_value_error(1, "invalid process (" ZEND_LONG_FMT ")", pid);
1701 				RETURN_THROWS();
1702 			case EPERM:
1703 				php_error_docref(NULL, E_WARNING, "Calling process not having the proper privileges");
1704 				break;
1705 			case EINVAL:
1706 				zend_argument_value_error(2, "invalid cpu affinity mask size or unmapped cpu id(s)");
1707 				RETURN_THROWS();
1708 			default:
1709 				php_error_docref(NULL, E_WARNING, "Error %d", errno);
1710 		}
1711 		RETURN_FALSE;
1712 	} else {
1713 		PCNTL_CPU_DESTROY(mask);
1714 		RETURN_TRUE;
1715 	}
1716 }
1717 #endif
1718 
1719 #if defined(HAVE_SCHED_GETCPU)
PHP_FUNCTION(pcntl_getcpu)1720 PHP_FUNCTION(pcntl_getcpu)
1721 {
1722 	ZEND_PARSE_PARAMETERS_NONE();
1723 
1724 	RETURN_LONG(sched_getcpu());
1725 }
1726 #endif
1727 
1728 #if defined(HAVE_PTHREAD_SET_QOS_CLASS_SELF_NP)
qos_zval_to_lval(const zval * qos_obj)1729 static qos_class_t qos_zval_to_lval(const zval *qos_obj)
1730 {
1731 	qos_class_t qos_class = QOS_CLASS_DEFAULT;
1732 	zend_string *entry = Z_STR_P(zend_enum_fetch_case_name(Z_OBJ_P(qos_obj)));
1733 
1734 	if (zend_string_equals_literal(entry, "UserInteractive")) {
1735 		qos_class = QOS_CLASS_USER_INTERACTIVE;
1736 	} else if (zend_string_equals_literal(entry, "UserInitiated")) {
1737 		qos_class = QOS_CLASS_USER_INITIATED;
1738 	} else if (zend_string_equals_literal(entry, "Utility")) {
1739 		qos_class = QOS_CLASS_UTILITY;
1740 	} else if (zend_string_equals_literal(entry, "Background")) {
1741 		qos_class = QOS_CLASS_BACKGROUND;
1742 	}
1743 
1744 	return qos_class;
1745 }
1746 
qos_lval_to_zval(qos_class_t qos_class)1747 static zend_object *qos_lval_to_zval(qos_class_t qos_class)
1748 {
1749 	const char *entryname;
1750 	switch (qos_class)
1751 	{
1752 	case QOS_CLASS_USER_INTERACTIVE:
1753 		entryname = "UserInteractive";
1754 		break;
1755 	case QOS_CLASS_USER_INITIATED:
1756 		entryname = "UserInitiated";
1757 		break;
1758 	case QOS_CLASS_UTILITY:
1759 		entryname = "Utility";
1760 		break;
1761 	case QOS_CLASS_BACKGROUND:
1762 		entryname = "Background";
1763 		break;
1764 	case QOS_CLASS_DEFAULT:
1765 	default:
1766 		entryname = "Default";
1767 		break;
1768 	}
1769 
1770 	return zend_enum_get_case_cstr(QosClass_ce, entryname);
1771 }
1772 
PHP_FUNCTION(pcntl_getqos_class)1773 PHP_FUNCTION(pcntl_getqos_class)
1774 {
1775 	qos_class_t qos_class;
1776 
1777 	ZEND_PARSE_PARAMETERS_NONE();
1778 
1779 	if (UNEXPECTED(pthread_get_qos_class_np(pthread_self(), &qos_class, NULL) != 0))
1780 	{
1781 		// unlikely unless an external tool set the QOS class with a wrong value
1782 		PCNTL_G(last_error) = errno;
1783 		zend_throw_error(NULL, "invalid QOS class %u", qos_class);
1784 		RETURN_THROWS();
1785 	}
1786 
1787 	RETURN_OBJ_COPY(qos_lval_to_zval(qos_class));
1788 }
1789 
PHP_FUNCTION(pcntl_setqos_class)1790 PHP_FUNCTION(pcntl_setqos_class)
1791 {
1792 	zval *qos_obj;
1793 
1794 	ZEND_PARSE_PARAMETERS_START(1, 1)
1795 		Z_PARAM_OBJECT_OF_CLASS(qos_obj, QosClass_ce)
1796 	ZEND_PARSE_PARAMETERS_END();
1797 
1798 	qos_class_t qos_class = qos_zval_to_lval(qos_obj);
1799 
1800 	if (UNEXPECTED(pthread_set_qos_class_self_np((qos_class_t)qos_class, 0) != 0))
1801 	{
1802 		// unlikely, unless it is a new os issue, as we draw from the specified enum values
1803 		PCNTL_G(last_error) = errno;
1804 		zend_throw_error(NULL, "pcntl_setqos_class failed");
1805 		RETURN_THROWS();
1806 	}
1807 }
1808 #endif
1809 
pcntl_interrupt_function(zend_execute_data * execute_data)1810 static void pcntl_interrupt_function(zend_execute_data *execute_data)
1811 {
1812 	pcntl_signal_dispatch();
1813 	if (orig_interrupt_function) {
1814 		orig_interrupt_function(execute_data);
1815 	}
1816 }
1817