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