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