xref: /php-src/ext/pcntl/pcntl.c (revision fe7f699c)
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 		int i;
718 		for (i = 0; i < PCNTL_G(num_signals); i++) {
719 			struct php_pcntl_pending_signal *psig;
720 
721 			psig = emalloc(sizeof(*psig));
722 			psig->next = PCNTL_G(spares);
723 			PCNTL_G(spares) = psig;
724 		}
725 	}
726 
727 	/* If restart_syscalls was not explicitly specified and the signal is SIGALRM, then default
728 	 * restart_syscalls to false. PHP used to enforce that restart_syscalls is false for SIGALRM,
729 	 * so we keep this differing default to reduce the degree of BC breakage. */
730 	if (restart_syscalls_is_null && signo == SIGALRM) {
731 		restart_syscalls = 0;
732 	}
733 
734 	/* Special long value case for SIG_DFL and SIG_IGN */
735 	if (Z_TYPE_P(handle) == IS_LONG) {
736 		if (Z_LVAL_P(handle) != (zend_long) SIG_DFL && Z_LVAL_P(handle) != (zend_long) SIG_IGN) {
737 			zend_argument_value_error(2, "must be either SIG_DFL or SIG_IGN when an integer value is given");
738 			RETURN_THROWS();
739 		}
740 		if (php_signal(signo, (Sigfunc *) Z_LVAL_P(handle), (int) restart_syscalls) == (void *)SIG_ERR) {
741 			PCNTL_G(last_error) = errno;
742 			php_error_docref(NULL, E_WARNING, "Error assigning signal");
743 			RETURN_FALSE;
744 		}
745 		zend_hash_index_update(&PCNTL_G(php_signal_table), signo, handle);
746 		RETURN_TRUE;
747 	}
748 
749 	if (!zend_is_callable_ex(handle, NULL, 0, NULL, NULL, NULL)) {
750 		PCNTL_G(last_error) = EINVAL;
751 
752 		zend_argument_type_error(2, "must be of type callable|int, %s given", zend_zval_value_name(handle));
753 		RETURN_THROWS();
754 	}
755 
756 	/* Add the function name to our signal table */
757 	handle = zend_hash_index_update(&PCNTL_G(php_signal_table), signo, handle);
758 	Z_TRY_ADDREF_P(handle);
759 
760 	if (php_signal4(signo, pcntl_signal_handler, (int) restart_syscalls, 1) == (void *)SIG_ERR) {
761 		PCNTL_G(last_error) = errno;
762 		php_error_docref(NULL, E_WARNING, "Error assigning signal");
763 		RETURN_FALSE;
764 	}
765 	RETURN_TRUE;
766 }
767 /* }}} */
768 
769 /* {{{ Gets signal handler */
PHP_FUNCTION(pcntl_signal_get_handler)770 PHP_FUNCTION(pcntl_signal_get_handler)
771 {
772 	zval *prev_handle;
773 	zend_long signo;
774 
775 	ZEND_PARSE_PARAMETERS_START(1, 1)
776 		Z_PARAM_LONG(signo)
777 	ZEND_PARSE_PARAMETERS_END();
778 
779 	// note: max signal on mac is SIGUSR2 (31), no real time signals.
780 	int sigmax = NSIG - 1;
781 #if defined(SIGRTMAX)
782 	// oddily enough, NSIG on freebsd reports only 32 whereas SIGRTMIN starts at 65.
783 	if (sigmax < SIGRTMAX) {
784 		sigmax = SIGRTMAX;
785 	}
786 #endif
787 
788 	if (signo < 1 || signo > sigmax) {
789 		zend_argument_value_error(1, "must be between 1 and %d", sigmax);
790 		RETURN_THROWS();
791 	}
792 
793 	if ((prev_handle = zend_hash_index_find(&PCNTL_G(php_signal_table), signo)) != NULL) {
794 		RETURN_COPY(prev_handle);
795 	} else {
796 		RETURN_LONG((zend_long)SIG_DFL);
797 	}
798 }
799 
800 /* {{{ Dispatch signals to signal handlers */
PHP_FUNCTION(pcntl_signal_dispatch)801 PHP_FUNCTION(pcntl_signal_dispatch)
802 {
803 	ZEND_PARSE_PARAMETERS_NONE();
804 
805 	pcntl_signal_dispatch();
806 	RETURN_TRUE;
807 }
808 /* }}} */
809 
810 /* Common helper function for these 3 wrapper functions */
811 #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)812 static bool php_pcntl_set_user_signal_infos(
813 	/* const */ HashTable *const user_signals,
814 	sigset_t *const set,
815 	size_t arg_num,
816 	bool allow_empty_signal_array
817 ) {
818 	if (!allow_empty_signal_array && zend_hash_num_elements(user_signals) == 0) {
819 		zend_argument_value_error(arg_num, "cannot be empty");
820 		return false;
821 	}
822 
823 	errno = 0;
824 	if (sigemptyset(set) != 0) {
825 		PCNTL_G(last_error) = errno;
826 		php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
827 		return false;
828 	}
829 
830 	zval *user_signal_no;
831 	ZEND_HASH_FOREACH_VAL(user_signals, user_signal_no) {
832 		bool failed = true;
833 		zend_long tmp = zval_try_get_long(user_signal_no, &failed);
834 
835 		if (failed) {
836 			zend_argument_type_error(arg_num, "signals must be of type int, %s given", zend_zval_value_name(user_signal_no));
837 			return false;
838 		}
839 		/* Signals are positive integers */
840 		if (tmp < 1 || tmp >= PCNTL_G(num_signals)) {
841 			/* PCNTL_G(num_signals) stores +1 from the last valid signal */
842 			zend_argument_value_error(arg_num, "signals must be between 1 and %d", PCNTL_G(num_signals)-1);
843 			return false;
844 		}
845 
846 		int signal_no = (int) tmp;
847 		errno = 0;
848 		if (sigaddset(set, signal_no) != 0) {
849 			PCNTL_G(last_error) = errno;
850 			php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
851 			return false;
852 		}
853 	} ZEND_HASH_FOREACH_END();
854 	return true;
855 }
856 #endif
857 
858 #ifdef HAVE_SIGPROCMASK
859 /* {{{ Examine and change blocked signals */
PHP_FUNCTION(pcntl_sigprocmask)860 PHP_FUNCTION(pcntl_sigprocmask)
861 {
862 	zend_long how;
863 	HashTable *user_set;
864 	/* Optional by-ref out-param array of old signals */
865 	zval *user_old_set = NULL;
866 
867 	ZEND_PARSE_PARAMETERS_START(2, 3)
868 		Z_PARAM_LONG(how)
869 		Z_PARAM_ARRAY_HT(user_set)
870 		Z_PARAM_OPTIONAL
871 		Z_PARAM_ZVAL(user_old_set)
872 	ZEND_PARSE_PARAMETERS_END();
873 
874 	if (how != SIG_BLOCK && how != SIG_UNBLOCK && how != SIG_SETMASK) {
875 		zend_argument_value_error(1, "must be one of SIG_BLOCK, SIG_UNBLOCK, or SIG_SETMASK");
876 		RETURN_THROWS();
877 	}
878 
879 	errno = 0;
880 	sigset_t old_set;
881 	if (sigemptyset(&old_set) != 0) {
882 		PCNTL_G(last_error) = errno;
883 		php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
884 		RETURN_FALSE;
885 	}
886 
887 	sigset_t set;
888 	bool status = php_pcntl_set_user_signal_infos(user_set, &set, 2, /* allow_empty_signal_array */ how == SIG_SETMASK);
889 	/* Some error occurred */
890 	if (!status) {
891 		RETURN_FALSE;
892 	}
893 
894 	if (sigprocmask(how, &set, &old_set) != 0) {
895 		PCNTL_G(last_error) = errno;
896 		php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
897 		RETURN_FALSE;
898 	}
899 
900 	if (user_old_set != NULL) {
901 		user_old_set = zend_try_array_init(user_old_set);
902 		if (!user_old_set) {
903 			RETURN_THROWS();
904 		}
905 
906 		for (int signal_no = 1; signal_no < PCNTL_G(num_signals); ++signal_no) {
907 			if (sigismember(&old_set, signal_no) != 1) {
908 				continue;
909 			}
910 			add_next_index_long(user_old_set, signal_no);
911 		}
912 	}
913 
914 	RETURN_TRUE;
915 }
916 /* }}} */
917 #endif
918 
919 #ifdef HAVE_STRUCT_SIGINFO_T
920 # ifdef HAVE_SIGWAITINFO
921 
922 /* {{{ Synchronously wait for queued signals */
PHP_FUNCTION(pcntl_sigwaitinfo)923 PHP_FUNCTION(pcntl_sigwaitinfo)
924 {
925 	HashTable *user_set;
926 	/* Optional by-ref array of ints */
927 	zval *user_siginfo = NULL;
928 
929 	ZEND_PARSE_PARAMETERS_START(1, 2)
930 		Z_PARAM_ARRAY_HT(user_set)
931 		Z_PARAM_OPTIONAL
932 		Z_PARAM_ZVAL(user_siginfo)
933 	ZEND_PARSE_PARAMETERS_END();
934 
935 	sigset_t set;
936 	bool status = php_pcntl_set_user_signal_infos(user_set, &set, 1, /* allow_empty_signal_array */ false);
937 	/* Some error occurred */
938 	if (!status) {
939 		RETURN_FALSE;
940 	}
941 
942 	errno = 0;
943 	siginfo_t siginfo;
944 	int signal_no = sigwaitinfo(&set, &siginfo);
945 	/* sigwaitinfo() never sets errno to EAGAIN according to POSIX */
946 	if (signal_no == -1) {
947 		PCNTL_G(last_error) = errno;
948 		php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
949 		RETURN_FALSE;
950 	}
951 
952 	/* sigwaitinfo can return 0 on success on some platforms, e.g. NetBSD */
953 	if (!signal_no && siginfo.si_signo) {
954 		signal_no = siginfo.si_signo;
955 	}
956 
957 	pcntl_siginfo_to_zval(signal_no, &siginfo, user_siginfo);
958 
959 	RETURN_LONG(signal_no);
960 }
961 /* }}} */
962 # endif
963 # ifdef HAVE_SIGTIMEDWAIT
964 /* {{{ Wait for queued signals */
PHP_FUNCTION(pcntl_sigtimedwait)965 PHP_FUNCTION(pcntl_sigtimedwait)
966 {
967 	HashTable *user_set;
968 	/* Optional by-ref array of ints */
969 	zval *user_siginfo = NULL;
970 	zend_long tv_sec = 0;
971 	zend_long tv_nsec = 0;
972 
973 	ZEND_PARSE_PARAMETERS_START(1, 4)
974 		Z_PARAM_ARRAY_HT(user_set)
975 		Z_PARAM_OPTIONAL
976 		Z_PARAM_ZVAL(user_siginfo)
977 		Z_PARAM_LONG(tv_sec)
978 		Z_PARAM_LONG(tv_nsec)
979 	ZEND_PARSE_PARAMETERS_END();
980 
981 	sigset_t set;
982 	bool status = php_pcntl_set_user_signal_infos(user_set, &set, 1, /* allow_empty_signal_array */ false);
983 	/* Some error occurred */
984 	if (!status) {
985 		RETURN_FALSE;
986 	}
987 	if (tv_sec < 0) {
988 		zend_argument_value_error(3, "must be greater than or equal to 0");
989 		RETURN_THROWS();
990 	}
991 	/* Nanosecond between 0 and 1e9 */
992 	if (tv_nsec < 0 || tv_nsec >= 1000000000) {
993 		zend_argument_value_error(4, "must be between 0 and 1e9");
994 		RETURN_THROWS();
995 	}
996 	if (UNEXPECTED(tv_sec == 0 && tv_nsec == 0)) {
997 		zend_value_error("pcntl_sigtimedwait(): At least one of argument #3 ($seconds) or argument #4 ($nanoseconds) must be greater than 0");
998 		RETURN_THROWS();
999 	}
1000 
1001 	errno = 0;
1002 	siginfo_t siginfo;
1003 	struct timespec timeout;
1004 	timeout.tv_sec  = (time_t) tv_sec;
1005 	timeout.tv_nsec = tv_nsec;
1006 	int signal_no = sigtimedwait(&set, &siginfo, &timeout);
1007 	if (signal_no == -1) {
1008 		if (errno != EAGAIN) {
1009 			PCNTL_G(last_error) = errno;
1010 			php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
1011 		}
1012 		RETURN_FALSE;
1013 	}
1014 
1015 	/* sigtimedwait can return 0 on success on some platforms, e.g. NetBSD */
1016 	if (!signal_no && siginfo.si_signo) {
1017 		signal_no = siginfo.si_signo;
1018 	}
1019 
1020 	pcntl_siginfo_to_zval(signal_no, &siginfo, user_siginfo);
1021 
1022 	RETURN_LONG(signal_no);
1023 }
1024 /* }}} */
1025 # endif
1026 
pcntl_siginfo_to_zval(int signo,siginfo_t * siginfo,zval * user_siginfo)1027 static void pcntl_siginfo_to_zval(int signo, siginfo_t *siginfo, zval *user_siginfo) /* {{{ */
1028 {
1029 	if (signo > 0 && user_siginfo) {
1030 		user_siginfo = zend_try_array_init(user_siginfo);
1031 		if (!user_siginfo) {
1032 			return;
1033 		}
1034 
1035 		add_assoc_long_ex(user_siginfo, "signo", sizeof("signo")-1, siginfo->si_signo);
1036 		add_assoc_long_ex(user_siginfo, "errno", sizeof("errno")-1, siginfo->si_errno);
1037 		add_assoc_long_ex(user_siginfo, "code",  sizeof("code")-1,  siginfo->si_code);
1038 		switch(signo) {
1039 #ifdef SIGCHLD
1040 			case SIGCHLD:
1041 				add_assoc_long_ex(user_siginfo,   "status", sizeof("status")-1, siginfo->si_status);
1042 # ifdef si_utime
1043 				add_assoc_double_ex(user_siginfo, "utime",  sizeof("utime")-1,  siginfo->si_utime);
1044 # endif
1045 # ifdef si_stime
1046 				add_assoc_double_ex(user_siginfo, "stime",  sizeof("stime")-1,  siginfo->si_stime);
1047 # endif
1048 				add_assoc_long_ex(user_siginfo,   "pid",    sizeof("pid")-1,    siginfo->si_pid);
1049 				add_assoc_long_ex(user_siginfo,   "uid",    sizeof("uid")-1,    siginfo->si_uid);
1050 				break;
1051 			case SIGUSR1:
1052 			case SIGUSR2:
1053 				add_assoc_long_ex(user_siginfo,   "pid",    sizeof("pid")-1,    siginfo->si_pid);
1054 				add_assoc_long_ex(user_siginfo,   "uid",    sizeof("uid")-1,    siginfo->si_uid);
1055 				break;
1056 #endif
1057 			case SIGILL:
1058 			case SIGFPE:
1059 			case SIGSEGV:
1060 			case SIGBUS:
1061 				add_assoc_double_ex(user_siginfo, "addr", sizeof("addr")-1, (zend_long)siginfo->si_addr);
1062 				break;
1063 #if defined(SIGPOLL) && !defined(__CYGWIN__)
1064 			case SIGPOLL:
1065 				add_assoc_long_ex(user_siginfo, "band", sizeof("band")-1, siginfo->si_band);
1066 # ifdef si_fd
1067 				add_assoc_long_ex(user_siginfo, "fd",   sizeof("fd")-1,   siginfo->si_fd);
1068 # endif
1069 				break;
1070 #endif
1071 
1072 #ifdef SIGTRAP
1073 			case SIGTRAP:
1074 # if defined(si_syscall) && defined(__FreeBSD__)
1075 				if (siginfo->si_code == TRAP_CAP) {
1076 					add_assoc_long_ex(user_siginfo, "syscall", sizeof("syscall")-1, (zend_long)siginfo->si_syscall);
1077 				} else {
1078 					add_assoc_long_ex(user_siginfo, "trapno", sizeof("trapno")-1, (zend_long)siginfo->si_trapno);
1079 				}
1080 
1081 # endif
1082 				break;
1083 
1084 #endif
1085 		}
1086 #if defined(SIGRTMIN) && defined(SIGRTMAX)
1087 		if (SIGRTMIN <= signo && signo <= SIGRTMAX) {
1088 			add_assoc_long_ex(user_siginfo, "pid", sizeof("pid")-1, siginfo->si_pid);
1089 			add_assoc_long_ex(user_siginfo, "uid", sizeof("uid")-1, siginfo->si_uid);
1090 		}
1091 #endif
1092 	}
1093 }
1094 /* }}} */
1095 #endif
1096 
1097 #ifdef HAVE_GETPRIORITY
1098 /* {{{ Get the priority of any process */
PHP_FUNCTION(pcntl_getpriority)1099 PHP_FUNCTION(pcntl_getpriority)
1100 {
1101 	zend_long who = PRIO_PROCESS;
1102 	zend_long pid;
1103 	bool pid_is_null = 1;
1104 	int pri;
1105 
1106 	ZEND_PARSE_PARAMETERS_START(0, 2)
1107 		Z_PARAM_OPTIONAL
1108 		Z_PARAM_LONG_OR_NULL(pid, pid_is_null)
1109 		Z_PARAM_LONG(who)
1110 	ZEND_PARSE_PARAMETERS_END();
1111 
1112 	/* needs to be cleared, since any returned value is valid */
1113 	errno = 0;
1114 
1115 	pid = pid_is_null ? 0 : pid;
1116 	pri = getpriority(who, pid);
1117 
1118 	if (errno) {
1119 		PCNTL_G(last_error) = errno;
1120 		switch (errno) {
1121 			case ESRCH:
1122 				php_error_docref(NULL, E_WARNING, "Error %d: No process was located using the given parameters", errno);
1123 				break;
1124 			case EINVAL:
1125 #ifdef PRIO_DARWIN_BG
1126 				if (who != PRIO_PGRP && who != PRIO_USER && who != PRIO_PROCESS && who != PRIO_DARWIN_THREAD) {
1127 					zend_argument_value_error(2, "must be one of PRIO_PGRP, PRIO_USER, PRIO_PROCESS or PRIO_DARWIN_THREAD");
1128 					RETURN_THROWS();
1129 				} else if (who == PRIO_DARWIN_THREAD && pid != 0) {
1130 					zend_argument_value_error(1, "must be 0 (zero) if PRIO_DARWIN_THREAD is provided as second parameter");
1131 					RETURN_THROWS();
1132 				} else {
1133 					zend_argument_value_error(1, "is not a valid process, process group, or user ID");
1134 					RETURN_THROWS();
1135 				}
1136 #else
1137 				zend_argument_value_error(2, "must be one of PRIO_PGRP, PRIO_USER, or PRIO_PROCESS");
1138 				RETURN_THROWS();
1139 #endif
1140 
1141 			default:
1142 				php_error_docref(NULL, E_WARNING, "Unknown error %d has occurred", errno);
1143 				break;
1144 		}
1145 		RETURN_FALSE;
1146 	}
1147 
1148 	RETURN_LONG(pri);
1149 }
1150 /* }}} */
1151 #endif
1152 
1153 #ifdef HAVE_SETPRIORITY
1154 /* {{{ Change the priority of any process */
PHP_FUNCTION(pcntl_setpriority)1155 PHP_FUNCTION(pcntl_setpriority)
1156 {
1157 	zend_long who = PRIO_PROCESS;
1158 	zend_long pid;
1159 	bool pid_is_null = 1;
1160 	zend_long pri;
1161 
1162 	ZEND_PARSE_PARAMETERS_START(1, 3)
1163 		Z_PARAM_LONG(pri)
1164 		Z_PARAM_OPTIONAL
1165 		Z_PARAM_LONG_OR_NULL(pid, pid_is_null)
1166 		Z_PARAM_LONG(who)
1167 	ZEND_PARSE_PARAMETERS_END();
1168 
1169 	pid = pid_is_null ? 0 : pid;
1170 
1171 	if (setpriority(who, pid, pri)) {
1172 		PCNTL_G(last_error) = errno;
1173 		switch (errno) {
1174 			case ESRCH:
1175 				php_error_docref(NULL, E_WARNING, "Error %d: No process was located using the given parameters", errno);
1176 				break;
1177 			case EINVAL:
1178 #ifdef PRIO_DARWIN_BG
1179 				if (who != PRIO_PGRP && who != PRIO_USER && who != PRIO_PROCESS && who != PRIO_DARWIN_THREAD) {
1180 					zend_argument_value_error(3, "must be one of PRIO_PGRP, PRIO_USER, PRIO_PROCESS or PRIO_DARWIN_THREAD");
1181 					RETURN_THROWS();
1182 				} else if (who == PRIO_DARWIN_THREAD && pid != 0) {
1183 					zend_argument_value_error(2, "must be 0 (zero) if PRIO_DARWIN_THREAD is provided as second parameter");
1184 					RETURN_THROWS();
1185 				} else if (who == PRIO_DARWIN_THREAD && pid == 0 && (pri != 0 && pri != PRIO_DARWIN_BG)) {
1186 					zend_argument_value_error(1, "must be either 0 (zero) or PRIO_DARWIN_BG, for mode PRIO_DARWIN_THREAD");
1187 					RETURN_THROWS();
1188 				} else {
1189 					zend_argument_value_error(2, "is not a valid process, process group, or user ID");
1190 					RETURN_THROWS();
1191 				}
1192 #else
1193 				zend_argument_value_error(3, "must be one of PRIO_PGRP, PRIO_USER, or PRIO_PROCESS");
1194 				RETURN_THROWS();
1195 #endif
1196 			case EPERM:
1197 				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);
1198 				break;
1199 			case EACCES:
1200 				php_error_docref(NULL, E_WARNING, "Error %d: Only a super user may attempt to increase the process priority", errno);
1201 				break;
1202 			default:
1203 				php_error_docref(NULL, E_WARNING, "Unknown error %d has occurred", errno);
1204 				break;
1205 		}
1206 		RETURN_FALSE;
1207 	}
1208 
1209 	RETURN_TRUE;
1210 }
1211 /* }}} */
1212 #endif
1213 
1214 /* {{{ Retrieve the error number set by the last pcntl function which failed. */
PHP_FUNCTION(pcntl_get_last_error)1215 PHP_FUNCTION(pcntl_get_last_error)
1216 {
1217 	ZEND_PARSE_PARAMETERS_NONE();
1218 
1219 	RETURN_LONG(PCNTL_G(last_error));
1220 }
1221 /* }}} */
1222 
1223 /* {{{ Retrieve the system error message associated with the given errno. */
PHP_FUNCTION(pcntl_strerror)1224 PHP_FUNCTION(pcntl_strerror)
1225 {
1226 	zend_long error;
1227 
1228 	ZEND_PARSE_PARAMETERS_START(1, 1)
1229 		Z_PARAM_LONG(error)
1230 	ZEND_PARSE_PARAMETERS_END();
1231 
1232 	RETURN_STRING(strerror(error));
1233 }
1234 /* }}} */
1235 
1236 /* Our custom signal handler that calls the appropriate php_function */
1237 #ifdef HAVE_STRUCT_SIGINFO_T
pcntl_signal_handler(int signo,siginfo_t * siginfo,void * context)1238 static void pcntl_signal_handler(int signo, siginfo_t *siginfo, void *context)
1239 #else
1240 static void pcntl_signal_handler(int signo)
1241 #endif
1242 {
1243 	struct php_pcntl_pending_signal *psig = PCNTL_G(spares);
1244 	if (!psig) {
1245 		/* oops, too many signals for us to track, so we'll forget about this one */
1246 		return;
1247 	}
1248 	PCNTL_G(spares) = psig->next;
1249 
1250 	psig->signo = signo;
1251 	psig->next = NULL;
1252 
1253 #ifdef HAVE_STRUCT_SIGINFO_T
1254 	psig->siginfo = *siginfo;
1255 #endif
1256 
1257 	/* the head check is important, as the tick handler cannot atomically clear both
1258 	 * the head and tail */
1259 	if (PCNTL_G(head) && PCNTL_G(tail)) {
1260 		PCNTL_G(tail)->next = psig;
1261 	} else {
1262 		PCNTL_G(head) = psig;
1263 	}
1264 	PCNTL_G(tail) = psig;
1265 	PCNTL_G(pending_signals) = 1;
1266 	if (PCNTL_G(async_signals)) {
1267 		zend_atomic_bool_store_ex(&EG(vm_interrupt), true);
1268 	}
1269 }
1270 
pcntl_signal_dispatch(void)1271 void pcntl_signal_dispatch(void)
1272 {
1273 	zval params[2], *handle, retval;
1274 	struct php_pcntl_pending_signal *queue, *next;
1275 	sigset_t mask;
1276 	sigset_t old_mask;
1277 
1278 	if(!PCNTL_G(pending_signals)) {
1279 		return;
1280 	}
1281 
1282 	/* Mask all signals */
1283 	sigfillset(&mask);
1284 	sigprocmask(SIG_BLOCK, &mask, &old_mask);
1285 
1286 	/* Bail if the queue is empty or if we are already playing the queue */
1287 	if (!PCNTL_G(head) || PCNTL_G(processing_signal_queue)) {
1288 		sigprocmask(SIG_SETMASK, &old_mask, NULL);
1289 		return;
1290 	}
1291 
1292 	/* Prevent switching fibers when handling signals */
1293 	zend_fiber_switch_block();
1294 
1295 	/* Prevent reentrant handler calls */
1296 	PCNTL_G(processing_signal_queue) = 1;
1297 
1298 	queue = PCNTL_G(head);
1299 	PCNTL_G(head) = NULL; /* simple stores are atomic */
1300 
1301 	/* Allocate */
1302 	while (queue) {
1303 		if ((handle = zend_hash_index_find(&PCNTL_G(php_signal_table), queue->signo)) != NULL) {
1304 			if (Z_TYPE_P(handle) != IS_LONG) {
1305 				ZVAL_NULL(&retval);
1306 				ZVAL_LONG(&params[0], queue->signo);
1307 #ifdef HAVE_STRUCT_SIGINFO_T
1308 				array_init(&params[1]);
1309 				pcntl_siginfo_to_zval(queue->signo, &queue->siginfo, &params[1]);
1310 #else
1311 				ZVAL_NULL(&params[1]);
1312 #endif
1313 
1314 				/* Call php signal handler - Note that we do not report errors, and we ignore the return value */
1315 				/* FIXME: this is probably broken when multiple signals are handled in this while loop (retval) */
1316 				call_user_function(NULL, NULL, handle, &retval, 2, params);
1317 				zval_ptr_dtor(&retval);
1318 #ifdef HAVE_STRUCT_SIGINFO_T
1319 				zval_ptr_dtor(&params[1]);
1320 #endif
1321 			}
1322 		}
1323 
1324 		next = queue->next;
1325 		queue->next = PCNTL_G(spares);
1326 		PCNTL_G(spares) = queue;
1327 		queue = next;
1328 	}
1329 
1330 	PCNTL_G(pending_signals) = 0;
1331 
1332 	/* Re-enable queue */
1333 	PCNTL_G(processing_signal_queue) = 0;
1334 
1335 	/* Re-enable fiber switching */
1336 	zend_fiber_switch_unblock();
1337 
1338 	/* return signal mask to previous state */
1339 	sigprocmask(SIG_SETMASK, &old_mask, NULL);
1340 }
1341 
pcntl_signal_dispatch_tick_function(int dummy_int,void * dummy_pointer)1342 static void pcntl_signal_dispatch_tick_function(int dummy_int, void *dummy_pointer)
1343 {
1344 	return pcntl_signal_dispatch();
1345 }
1346 
1347 /* {{{ Enable/disable asynchronous signal handling and return the old setting. */
PHP_FUNCTION(pcntl_async_signals)1348 PHP_FUNCTION(pcntl_async_signals)
1349 {
1350 	bool on, on_is_null = 1;
1351 
1352 	ZEND_PARSE_PARAMETERS_START(0, 1)
1353 		Z_PARAM_OPTIONAL
1354 		Z_PARAM_BOOL_OR_NULL(on, on_is_null)
1355 	ZEND_PARSE_PARAMETERS_END();
1356 
1357 	if (on_is_null) {
1358 		RETURN_BOOL(PCNTL_G(async_signals));
1359 	}
1360 
1361 	RETVAL_BOOL(PCNTL_G(async_signals));
1362 	PCNTL_G(async_signals) = on;
1363 }
1364 /* }}} */
1365 
1366 #ifdef HAVE_UNSHARE
1367 /* {{{ disassociate parts of the process execution context */
PHP_FUNCTION(pcntl_unshare)1368 PHP_FUNCTION(pcntl_unshare)
1369 {
1370 	zend_long flags;
1371 
1372 	ZEND_PARSE_PARAMETERS_START(1, 1)
1373 		Z_PARAM_LONG(flags)
1374 	ZEND_PARSE_PARAMETERS_END();
1375 
1376 	if (unshare(flags) == -1) {
1377 		PCNTL_G(last_error) = errno;
1378 		switch (errno) {
1379 #ifdef EINVAL
1380 			case EINVAL:
1381 				zend_argument_value_error(1, "must be a combination of CLONE_* flags, or at least one flag is unsupported by the kernel");
1382 				RETURN_THROWS();
1383 				break;
1384 #endif
1385 #ifdef ENOMEM
1386 			case ENOMEM:
1387 				php_error_docref(NULL, E_WARNING, "Error %d: Insufficient memory for unshare", errno);
1388 				break;
1389 #endif
1390 #ifdef EPERM
1391 			case EPERM:
1392 				php_error_docref(NULL, E_WARNING, "Error %d: No privilege to use these flags", errno);
1393 				break;
1394 #endif
1395 #ifdef ENOSPC
1396 			case ENOSPC:
1397 				php_error_docref(NULL, E_WARNING, "Error %d: Reached the maximum nesting limit for one of the specified namespaces", errno);
1398 				break;
1399 #endif
1400 #ifdef EUSERS
1401 			case EUSERS:
1402 				php_error_docref(NULL, E_WARNING, "Error %d: Reached the maximum nesting limit for the user namespace", errno);
1403 				break;
1404 #endif
1405 			default:
1406 				php_error_docref(NULL, E_WARNING, "Unknown error %d has occurred", errno);
1407 				break;
1408 		}
1409 		RETURN_FALSE;
1410 	}
1411 
1412 	RETURN_TRUE;
1413 }
1414 /* }}} */
1415 #endif
1416 
1417 #ifdef HAVE_RFORK
1418 /* {{{ proto bool pcntl_rfork(int flags [, int signal])
1419    More control over the process creation is given over fork/vfork. */
PHP_FUNCTION(pcntl_rfork)1420 PHP_FUNCTION(pcntl_rfork)
1421 {
1422 	zend_long flags;
1423 	zend_long csignal = 0;
1424 	pid_t pid;
1425 
1426 	ZEND_PARSE_PARAMETERS_START(1, 2)
1427 		Z_PARAM_LONG(flags)
1428 		Z_PARAM_OPTIONAL
1429 		Z_PARAM_LONG(csignal)
1430 	ZEND_PARSE_PARAMETERS_END();
1431 
1432 	/* This is a flag to use with great caution in general, preferably not within PHP */
1433 	if ((flags & RFMEM) != 0) {
1434 		zend_argument_value_error(1, "must not include RFMEM value, not allowed within this context");
1435 		RETURN_THROWS();
1436 	}
1437 
1438 	if ((flags & RFSIGSHARE) != 0) {
1439 		zend_argument_value_error(1, "must not include RFSIGSHARE value, not allowed within this context");
1440 		RETURN_THROWS();
1441 	}
1442 
1443 	if ((flags & (RFFDG | RFCFDG)) == (RFFDG | RFCFDG)) {
1444 		zend_argument_value_error(1, "must not include both RFFDG and RFCFDG, because these flags are mutually exclusive");
1445 		RETURN_THROWS();
1446 	}
1447 
1448 	/* A new pid is required */
1449 	if (!(flags & (RFPROC))) {
1450 		flags |= RFPROC;
1451 	}
1452 
1453 #ifdef RFTSIGZMB
1454 	if ((flags & RFTSIGZMB) != 0) {
1455 		flags |= RFTSIGFLAGS(csignal);
1456 	}
1457 #endif
1458 
1459 	pid = rfork(flags);
1460 
1461 	if (pid == -1) {
1462 		PCNTL_G(last_error) = errno;
1463 		switch (errno) {
1464 			case EAGAIN:
1465 			php_error_docref(NULL, E_WARNING, "Maximum process creations limit reached\n");
1466 		break;
1467 
1468 		default:
1469 			php_error_docref(NULL, E_WARNING, "Error %d", errno);
1470 		}
1471 	}
1472 
1473 	RETURN_LONG((zend_long) pid);
1474 }
1475 #endif
1476 /* }}} */
1477 
1478 #ifdef HAVE_FORKX
1479 /* {{{ proto bool pcntl_forkx(int flags)
1480    More elaborated version of fork with the following settings.
1481    FORK_WAITPID: forbid the parent process to wait for multiple pid but one only
1482    FORK_NOSIGCHLD: SIGCHLD signal ignored when the child terminates */
PHP_FUNCTION(pcntl_forkx)1483 PHP_FUNCTION(pcntl_forkx)
1484 {
1485 	zend_long flags;
1486 	pid_t pid;
1487 
1488 	ZEND_PARSE_PARAMETERS_START(1, 1)
1489 		Z_PARAM_LONG(flags)
1490 	ZEND_PARSE_PARAMETERS_END();
1491 
1492 	if (flags < FORK_NOSIGCHLD || flags > FORK_WAITPID) {
1493 		zend_argument_value_error(1, "must be FORK_NOSIGCHLD or FORK_WAITPID");
1494 		RETURN_THROWS();
1495 	}
1496 
1497 	pid = forkx(flags);
1498 
1499 	if (pid == -1) {
1500 		PCNTL_G(last_error) = errno;
1501 		switch (errno) {
1502 			case EAGAIN:
1503 			php_error_docref(NULL, E_WARNING, "Maximum process creations limit reached\n");
1504 		break;
1505 			case EPERM:
1506 			php_error_docref(NULL, E_WARNING, "Calling process not having the proper privileges\n");
1507 		break;
1508 			case ENOMEM:
1509 			php_error_docref(NULL, E_WARNING, "No swap space left\n");
1510 		break;
1511 		default:
1512 			php_error_docref(NULL, E_WARNING, "Error %d", errno);
1513 		}
1514 	}
1515 
1516 	RETURN_LONG((zend_long) pid);
1517 }
1518 #endif
1519 /* }}} */
1520 
1521 #ifdef HAVE_PIDFD_OPEN
1522 // The `pidfd_open` syscall is available since 5.3
1523 // and `setns` since 3.0.
PHP_FUNCTION(pcntl_setns)1524 PHP_FUNCTION(pcntl_setns)
1525 {
1526 	zend_long pid, nstype = CLONE_NEWNET;
1527 	bool pid_is_null = 1;
1528 	int fd, ret;
1529 
1530 	ZEND_PARSE_PARAMETERS_START(0, 2)
1531 		Z_PARAM_OPTIONAL
1532 		Z_PARAM_LONG_OR_NULL(pid, pid_is_null)
1533 		Z_PARAM_LONG(nstype)
1534 	ZEND_PARSE_PARAMETERS_END();
1535 
1536 	pid = pid_is_null ? getpid() : pid;
1537 	fd = syscall(SYS_pidfd_open, pid, 0);
1538 	if (errno) {
1539 		PCNTL_G(last_error) = errno;
1540 		switch (errno) {
1541 			case EINVAL:
1542 			case ESRCH:
1543 				zend_argument_value_error(1, "is not a valid process (" ZEND_LONG_FMT ")", pid);
1544 				RETURN_THROWS();
1545 
1546 			case ENFILE:
1547 				php_error_docref(NULL, E_WARNING, "Error %d: File descriptors per-process limit reached", errno);
1548 				break;
1549 
1550 			case ENODEV:
1551 				php_error_docref(NULL, E_WARNING, "Error %d: Anonymous inode fs unsupported", errno);
1552 				break;
1553 
1554 			case ENOMEM:
1555 				php_error_docref(NULL, E_WARNING, "Error %d: Insufficient memory for pidfd_open", errno);
1556 				break;
1557 
1558 			default:
1559 			        php_error_docref(NULL, E_WARNING, "Error %d", errno);
1560 		}
1561 		RETURN_FALSE;
1562 	}
1563 	ret = setns(fd, (int)nstype);
1564 	close(fd);
1565 
1566 	if (ret == -1) {
1567 		PCNTL_G(last_error) = errno;
1568 		switch (errno) {
1569 			case ESRCH:
1570 				zend_argument_value_error(1, "process no longer available (" ZEND_LONG_FMT ")", pid);
1571 				RETURN_THROWS();
1572 
1573 			case EINVAL:
1574 				zend_argument_value_error(2, "is an invalid nstype (%d)", nstype);
1575 				RETURN_THROWS();
1576 
1577 			case EPERM:
1578 				php_error_docref(NULL, E_WARNING, "Error %d: No required capability for this process", errno);
1579 				break;
1580 
1581 			default:
1582 			        php_error_docref(NULL, E_WARNING, "Error %d", errno);
1583 		}
1584 		RETURN_FALSE;
1585 	} else {
1586 		RETURN_TRUE;
1587 	}
1588 }
1589 #endif
1590 
1591 #ifdef HAVE_SCHED_SETAFFINITY
PHP_FUNCTION(pcntl_getcpuaffinity)1592 PHP_FUNCTION(pcntl_getcpuaffinity)
1593 {
1594 	zend_long pid;
1595 	bool pid_is_null = 1;
1596 	cpu_set_t mask;
1597 
1598 	ZEND_PARSE_PARAMETERS_START(0, 1)
1599 		Z_PARAM_OPTIONAL
1600 		Z_PARAM_LONG_OR_NULL(pid, pid_is_null)
1601 	ZEND_PARSE_PARAMETERS_END();
1602 
1603 	// 0 == getpid in this context, we're just saving a syscall
1604 	pid = pid_is_null ? 0 : pid;
1605 
1606 	PCNTL_CPU_ZERO(mask);
1607 
1608 	if (sched_getaffinity(pid, PCNTL_CPUSET_SIZE(mask), PCNTL_CPUSET(mask)) != 0) {
1609 		PCNTL_CPU_DESTROY(mask);
1610 		PCNTL_G(last_error) = errno;
1611 		switch (errno) {
1612 			case ESRCH:
1613 				zend_argument_value_error(1, "invalid process (" ZEND_LONG_FMT ")", pid);
1614 				RETURN_THROWS();
1615 			case EPERM:
1616 				php_error_docref(NULL, E_WARNING, "Calling process not having the proper privileges");
1617 				break;
1618 			case EINVAL:
1619 				zend_value_error("invalid cpu affinity mask size");
1620 				RETURN_THROWS();
1621 			default:
1622 				php_error_docref(NULL, E_WARNING, "Error %d", errno);
1623 		}
1624 
1625 		RETURN_FALSE;
1626 	}
1627 
1628 	zend_ulong maxcpus = (zend_ulong)sysconf(_SC_NPROCESSORS_CONF);
1629 	array_init(return_value);
1630 
1631 	for (zend_ulong i = 0; i < maxcpus; i ++) {
1632 		if (PCNTL_CPU_ISSET(i, mask)) {
1633 			add_next_index_long(return_value, i);
1634 		}
1635 	}
1636 	PCNTL_CPU_DESTROY(mask);
1637 }
1638 
PHP_FUNCTION(pcntl_setcpuaffinity)1639 PHP_FUNCTION(pcntl_setcpuaffinity)
1640 {
1641 	zend_long pid;
1642 	bool pid_is_null = 1;
1643 	cpu_set_t mask;
1644 	zval *hmask = NULL, *ncpu;
1645 
1646 	ZEND_PARSE_PARAMETERS_START(0, 2)
1647 		Z_PARAM_OPTIONAL
1648 		Z_PARAM_LONG_OR_NULL(pid, pid_is_null)
1649 		Z_PARAM_ARRAY(hmask)
1650 	ZEND_PARSE_PARAMETERS_END();
1651 
1652 	if (!hmask || zend_hash_num_elements(Z_ARRVAL_P(hmask)) == 0) {
1653 		zend_argument_value_error(2, "must not be empty");
1654 		RETURN_THROWS();
1655 	}
1656 
1657 	// 0 == getpid in this context, we're just saving a syscall
1658 	pid = pid_is_null ? 0 : pid;
1659 	zend_ulong maxcpus = (zend_ulong)sysconf(_SC_NPROCESSORS_CONF);
1660 	PCNTL_CPU_ZERO(mask);
1661 
1662 	ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(hmask), ncpu) {
1663 		ZVAL_DEREF(ncpu);
1664 		zend_long cpu;
1665 		if (Z_TYPE_P(ncpu) != IS_LONG) {
1666 			if (Z_TYPE_P(ncpu) == IS_STRING) {
1667 				zend_ulong tmp;
1668 				if (!ZEND_HANDLE_NUMERIC(Z_STR_P(ncpu), tmp)) {
1669 					zend_argument_value_error(2, "cpu id invalid value (%s)", ZSTR_VAL(Z_STR_P(ncpu)));
1670 					PCNTL_CPU_DESTROY(mask);
1671 					RETURN_THROWS();
1672 				}
1673 
1674 				cpu = (zend_long)tmp;
1675 			} else {
1676 				zend_string *wcpu = zval_get_string_func(ncpu);
1677 				zend_argument_value_error(2, "cpu id invalid type (%s)", ZSTR_VAL(wcpu));
1678 				zend_string_release(wcpu);
1679 				PCNTL_CPU_DESTROY(mask);
1680 				RETURN_THROWS();
1681 			}
1682 		} else {
1683 			cpu = Z_LVAL_P(ncpu);
1684 		}
1685 
1686 		if (cpu < 0 || cpu >= maxcpus) {
1687 			zend_argument_value_error(2, "cpu id must be between 0 and " ZEND_ULONG_FMT " (" ZEND_LONG_FMT ")", maxcpus, cpu);
1688 			RETURN_THROWS();
1689 		}
1690 
1691 		if (!PCNTL_CPU_ISSET(cpu, mask)) {
1692 			PCNTL_CPU_SET(cpu, mask);
1693 		}
1694 	} ZEND_HASH_FOREACH_END();
1695 
1696 	if (sched_setaffinity(pid, PCNTL_CPUSET_SIZE(mask), PCNTL_CPUSET(mask)) != 0) {
1697 		PCNTL_CPU_DESTROY(mask);
1698 		PCNTL_G(last_error) = errno;
1699 		switch (errno) {
1700 			case ESRCH:
1701 				zend_argument_value_error(1, "invalid process (" ZEND_LONG_FMT ")", pid);
1702 				RETURN_THROWS();
1703 			case EPERM:
1704 				php_error_docref(NULL, E_WARNING, "Calling process not having the proper privileges");
1705 				break;
1706 			case EINVAL:
1707 				zend_argument_value_error(2, "invalid cpu affinity mask size or unmapped cpu id(s)");
1708 				RETURN_THROWS();
1709 			default:
1710 				php_error_docref(NULL, E_WARNING, "Error %d", errno);
1711 		}
1712 		RETURN_FALSE;
1713 	} else {
1714 		PCNTL_CPU_DESTROY(mask);
1715 		RETURN_TRUE;
1716 	}
1717 }
1718 #endif
1719 
1720 #if defined(HAVE_SCHED_GETCPU)
PHP_FUNCTION(pcntl_getcpu)1721 PHP_FUNCTION(pcntl_getcpu)
1722 {
1723 	ZEND_PARSE_PARAMETERS_NONE();
1724 
1725 	RETURN_LONG(sched_getcpu());
1726 }
1727 #endif
1728 
1729 #if defined(HAVE_PTHREAD_SET_QOS_CLASS_SELF_NP)
qos_zval_to_lval(const zval * qos_obj)1730 static qos_class_t qos_zval_to_lval(const zval *qos_obj)
1731 {
1732 	qos_class_t qos_class = QOS_CLASS_DEFAULT;
1733 	zend_string *entry = Z_STR_P(zend_enum_fetch_case_name(Z_OBJ_P(qos_obj)));
1734 
1735 	if (zend_string_equals_literal(entry, "UserInteractive")) {
1736 		qos_class = QOS_CLASS_USER_INTERACTIVE;
1737 	} else if (zend_string_equals_literal(entry, "UserInitiated")) {
1738 		qos_class = QOS_CLASS_USER_INITIATED;
1739 	} else if (zend_string_equals_literal(entry, "Utility")) {
1740 		qos_class = QOS_CLASS_UTILITY;
1741 	} else if (zend_string_equals_literal(entry, "Background")) {
1742 		qos_class = QOS_CLASS_BACKGROUND;
1743 	}
1744 
1745 	return qos_class;
1746 }
1747 
qos_lval_to_zval(qos_class_t qos_class)1748 static zend_object *qos_lval_to_zval(qos_class_t qos_class)
1749 {
1750 	const char *entryname;
1751 	switch (qos_class)
1752 	{
1753 	case QOS_CLASS_USER_INTERACTIVE:
1754 		entryname = "UserInteractive";
1755 		break;
1756 	case QOS_CLASS_USER_INITIATED:
1757 		entryname = "UserInitiated";
1758 		break;
1759 	case QOS_CLASS_UTILITY:
1760 		entryname = "Utility";
1761 		break;
1762 	case QOS_CLASS_BACKGROUND:
1763 		entryname = "Background";
1764 		break;
1765 	case QOS_CLASS_DEFAULT:
1766 	default:
1767 		entryname = "Default";
1768 		break;
1769 	}
1770 
1771 	return zend_enum_get_case_cstr(QosClass_ce, entryname);
1772 }
1773 
PHP_FUNCTION(pcntl_getqos_class)1774 PHP_FUNCTION(pcntl_getqos_class)
1775 {
1776 	qos_class_t qos_class;
1777 
1778 	ZEND_PARSE_PARAMETERS_NONE();
1779 
1780 	if (UNEXPECTED(pthread_get_qos_class_np(pthread_self(), &qos_class, NULL) != 0))
1781 	{
1782 		// unlikely unless an external tool set the QOS class with a wrong value
1783 		PCNTL_G(last_error) = errno;
1784 		zend_throw_error(NULL, "invalid QOS class %u", qos_class);
1785 		RETURN_THROWS();
1786 	}
1787 
1788 	RETURN_OBJ_COPY(qos_lval_to_zval(qos_class));
1789 }
1790 
PHP_FUNCTION(pcntl_setqos_class)1791 PHP_FUNCTION(pcntl_setqos_class)
1792 {
1793 	zval *qos_obj;
1794 
1795 	ZEND_PARSE_PARAMETERS_START(1, 1)
1796 		Z_PARAM_OBJECT_OF_CLASS(qos_obj, QosClass_ce)
1797 	ZEND_PARSE_PARAMETERS_END();
1798 
1799 	qos_class_t qos_class = qos_zval_to_lval(qos_obj);
1800 
1801 	if (UNEXPECTED(pthread_set_qos_class_self_np((qos_class_t)qos_class, 0) != 0))
1802 	{
1803 		// unlikely, unless it is a new os issue, as we draw from the specified enum values
1804 		PCNTL_G(last_error) = errno;
1805 		zend_throw_error(NULL, "pcntl_setqos_class failed");
1806 		RETURN_THROWS();
1807 	}
1808 }
1809 #endif
1810 
pcntl_interrupt_function(zend_execute_data * execute_data)1811 static void pcntl_interrupt_function(zend_execute_data *execute_data)
1812 {
1813 	pcntl_signal_dispatch();
1814 	if (orig_interrupt_function) {
1815 		orig_interrupt_function(execute_data);
1816 	}
1817 }
1818