php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
pcntl.c
Go to the documentation of this file.
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>
64typedef 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>
75typedef 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>
95typedef 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"
144
145#include "pcntl_arginfo.h"
146static zend_class_entry *QosClass_ce;
147
149static PHP_GINIT_FUNCTION(pcntl);
150
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),
161 PHP_MODULE_GLOBALS(pcntl),
162 PHP_GINIT(pcntl),
163 NULL,
164 NULL,
166};
167
168#ifdef COMPILE_DL_PCNTL
169#ifdef ZTS
171#endif
172ZEND_GET_MODULE(pcntl)
173#endif
174
175static void (*orig_interrupt_function)(zend_execute_data *execute_data);
176
177#ifdef HAVE_STRUCT_SIGINFO_T
178static void pcntl_signal_handler(int, siginfo_t*, void*);
179static void pcntl_siginfo_to_zval(int, siginfo_t*, zval*);
180#else
181static void pcntl_signal_handler(int);
182#endif
183static void pcntl_signal_dispatch(void);
184static void pcntl_signal_dispatch_tick_function(int dummy_int, void *dummy_pointer);
185static void pcntl_interrupt_function(zend_execute_data *execute_data);
186
187static PHP_GINIT_FUNCTION(pcntl)
188{
189#if defined(COMPILE_DL_PCNTL) && defined(ZTS)
191#endif
192 memset(pcntl_globals, 0, sizeof(*pcntl_globals));
193}
194
196{
197 php_add_tick_function(pcntl_signal_dispatch_tick_function, NULL);
201 PCNTL_G(last_error) = 0;
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) {
208 }
209#endif
210 return SUCCESS;
211}
212
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
224{
225 return SUCCESS;
226}
227
229{
230 struct php_pcntl_pending_signal *sig;
232 zval *handle;
233
234 /* Reset all signals to their default disposition */
238 }
240
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
258{
260 php_info_print_table_row(2, "pcntl support", "enabled");
262}
263
264/* {{{ Forks the currently running process following the same behavior as the UNIX fork() system call*/
266{
267 pid_t id;
268
270
271 id = fork();
272 if (id == -1) {
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) {
296 }
297
299}
300/* }}} */
301
302/* {{{ Set an alarm clock for delivery of a signal*/
304{
305 zend_long seconds;
306
308 Z_PARAM_LONG(seconds);
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 */
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
360 Z_PARAM_LONG(pid)
361 Z_PARAM_ZVAL(z_status)
364 Z_PARAM_ZVAL(z_rusage)
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) {
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) {
387 }
388
389#ifdef HAVE_WAIT4
390 if (child_id > 0) {
391 PHP_RUSAGE_TO_ARRAY(rusage, z_rusage);
392 }
393#endif
394
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
403{
404 zend_long idtype = P_ALL;
405 zend_long id = 0;
406 bool id_is_null = 1;
407 zval *user_siginfo = NULL;
409
412 Z_PARAM_LONG(idtype)
413 Z_PARAM_LONG_OR_NULL(id, id_is_null)
414 Z_PARAM_ZVAL(user_siginfo)
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) {
426 }
427
428 pcntl_siginfo_to_zval(SIGCHLD, &siginfo, user_siginfo);
429
431}
432#endif
433
434/* {{{ Waits on or returns the status of a forked child as defined by the waitpid() system call */
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
446 Z_PARAM_ZVAL(z_status)
449 Z_PARAM_ZVAL(z_rusage)
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) {
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) {
472 }
473
474#ifdef HAVE_WAIT3
475 if (child_id > 0) {
476 PHP_RUSAGE_TO_ARRAY(rusage, z_rusage);
477 }
478#endif
479
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 */
493{
494 zend_long status_word;
495
497 Z_PARAM_LONG(status_word)
499
500#ifdef WIFEXITED
501 int int_status_word = (int) status_word;
502 if (WIFEXITED(int_status_word)) {
504 }
505#endif
506
508}
509/* }}} */
510
511/* {{{ Returns true if the child status code represents a stopped process (WUNTRACED must have been used with waitpid) */
513{
514 zend_long status_word;
515
517 Z_PARAM_LONG(status_word)
519
520#ifdef WIFSTOPPED
521 int int_status_word = (int) status_word;
522 if (WIFSTOPPED(int_status_word)) {
524 }
525#endif
526
528}
529/* }}} */
530
531/* {{{ Returns true if the child status code represents a process that was terminated due to a signal */
533{
534 zend_long status_word;
535
537 Z_PARAM_LONG(status_word)
539
540#ifdef WIFSIGNALED
541 int int_status_word = (int) status_word;
542 if (WIFSIGNALED(int_status_word)) {
544 }
545#endif
546
548}
549/* }}} */
550
551/* {{{ Returns true if the child status code represents a process that was resumed due to a SIGCONT signal */
553{
554 zend_long status_word;
555
557 Z_PARAM_LONG(status_word)
559
560#ifdef HAVE_WCONTINUED
561 int int_status_word = (int) status_word;
562 if (WIFCONTINUED(int_status_word)) {
564 }
565#endif
567}
568/* }}} */
569
570
571/* {{{ Returns the status code of a child's exit */
573{
574 zend_long status_word;
575
577 Z_PARAM_LONG(status_word)
579
580#ifdef WEXITSTATUS
581 int int_status_word = (int) status_word;
582 RETURN_LONG(WEXITSTATUS(int_status_word));
583#else
585#endif
586}
587/* }}} */
588
589/* {{{ Returns the number of the signal that terminated the process who's status code is passed */
591{
592 zend_long status_word;
593
595 Z_PARAM_LONG(status_word)
597
598#ifdef WTERMSIG
599 int int_status_word = (int) status_word;
600 RETURN_LONG(WTERMSIG(int_status_word));
601#else
603#endif
604}
605/* }}} */
606
607/* {{{ Returns the number of the signal that caused the process to stop who's status code is passed */
609{
610 zend_long status_word;
611
613 Z_PARAM_LONG(status_word)
615
616#ifdef WSTOPSIG
617 int int_status_word = (int) status_word;
618 RETURN_LONG(WSTOPSIG(int_status_word));
619#else
621#endif
622}
623/* }}} */
624
625/* {{{ Executes specified program in current process space as defined by exec(2) */
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;
637 char *path;
638 size_t path_len;
639 zend_ulong key_num;
640
642 Z_PARAM_PATH(path, path_len)
645 Z_PARAM_ARRAY(envs)
647
648 if (ZEND_NUM_ARGS() > 1) {
649 /* Build argument list */
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);
662 }
663
664 *current_arg = Z_STRVAL_P(element);
665 argi++;
666 current_arg++;
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 size_t envp_len = (envc + 1);
682 pair = envp = safe_emalloc(envp_len, sizeof(char *), 0);
683 memset(envp, 0, sizeof(char *) * envp_len);
684 ZEND_HASH_FOREACH_KEY_VAL(envs_hash, key_num, key, element) {
685 if (envi >= envc) break;
686 if (!key) {
687 key = zend_long_to_str(key_num);
688 } else {
689 zend_string_addref(key);
690 }
691
692 if (!try_convert_to_string(element)) {
693 zend_string_release(key);
694 goto cleanup_env_vars;
695 }
696
697 /* Length of element + equal sign + length of key + null */
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 */
707 envi++;
708 pair++;
710 *(pair) = NULL;
711
712 if (execve(path, argv, envp) == -1) {
714 php_error_docref(NULL, E_WARNING, "Error has occurred: (errno %d) %s", errno, strerror(errno));
715 }
716
717cleanup_env_vars:
718 /* Cleanup */
719 for (pair = envp; *pair != NULL; pair++) efree(*pair);
720 efree(envp);
721 } else {
722
723 if (execv(path, argv) == -1) {
725 php_error_docref(NULL, E_WARNING, "Error has occurred: (errno %d) %s", errno, strerror(errno));
726 }
727 }
728
729 efree(argv);
730
732}
733/* }}} */
734
735/* {{{ Assigns a system signal handler to a PHP function */
737{
738 zval *handle;
739 zend_long signo;
740 bool restart_syscalls = 1;
741 bool restart_syscalls_is_null = 1;
742
744 Z_PARAM_LONG(signo)
747 Z_PARAM_BOOL_OR_NULL(restart_syscalls, restart_syscalls_is_null)
749
750 if (signo < 1) {
751 zend_argument_value_error(1, "must be greater than or equal to 1");
753 }
754
755 if (signo >= PCNTL_G(num_signals)) {
756 zend_argument_value_error(1, "must be less than %d", PCNTL_G(num_signals));
758 }
759
760 if (!PCNTL_G(spares)) {
761 /* since calling malloc() from within a signal handler is not portable,
762 * pre-allocate a few records for recording signals */
763 for (unsigned int i = 0; i < PCNTL_G(num_signals); i++) {
764 struct php_pcntl_pending_signal *psig;
765
766 psig = emalloc(sizeof(*psig));
767 psig->next = PCNTL_G(spares);
768 PCNTL_G(spares) = psig;
769 }
770 }
771
772 /* If restart_syscalls was not explicitly specified and the signal is SIGALRM, then default
773 * restart_syscalls to false. PHP used to enforce that restart_syscalls is false for SIGALRM,
774 * so we keep this differing default to reduce the degree of BC breakage. */
775 if (restart_syscalls_is_null && signo == SIGALRM) {
776 restart_syscalls = 0;
777 }
778
779 /* Special long value case for SIG_DFL and SIG_IGN */
780 if (Z_TYPE_P(handle) == IS_LONG) {
782 zend_argument_value_error(2, "must be either SIG_DFL or SIG_IGN when an integer value is given");
784 }
785 if (php_signal(signo, (Sigfunc *) Z_LVAL_P(handle), (int) restart_syscalls) == (void *)SIG_ERR) {
787 php_error_docref(NULL, E_WARNING, "Error assigning signal");
789 }
792 }
793
795 PCNTL_G(last_error) = EINVAL;
796
797 zend_argument_type_error(2, "must be of type callable|int, %s given", zend_zval_value_name(handle));
799 }
800
801 /* Add the function name to our signal table */
804
805 if (php_signal4(signo, pcntl_signal_handler, (int) restart_syscalls, 1) == (void *)SIG_ERR) {
807 php_error_docref(NULL, E_WARNING, "Error assigning signal");
809 }
811}
812/* }}} */
813
814/* {{{ Gets signal handler */
816{
817 zval *prev_handle;
819
823
824 // note: max signal on mac is SIGUSR2 (31), no real time signals.
825 int sigmax = NSIG - 1;
826#if defined(SIGRTMAX)
827 // oddily enough, NSIG on freebsd reports only 32 whereas SIGRTMIN starts at 65.
828 if (sigmax < SIGRTMAX) {
829 sigmax = SIGRTMAX;
830 }
831#endif
832
833 if (signo < 1 || signo > sigmax) {
834 zend_argument_value_error(1, "must be between 1 and %d", sigmax);
836 }
837
838 if ((prev_handle = zend_hash_index_find(&PCNTL_G(php_signal_table), signo)) != NULL) {
839 RETURN_COPY(prev_handle);
840 } else {
842 }
843}
844
845/* {{{ Dispatch signals to signal handlers */
846PHP_FUNCTION(pcntl_signal_dispatch)
847{
849
850 pcntl_signal_dispatch();
852}
853/* }}} */
854
855/* Common helper function for these 3 wrapper functions */
856#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT) || defined(HAVE_SIGPROCMASK)
857static bool php_pcntl_set_user_signal_infos(
858 /* const */ HashTable *const user_signals,
859 sigset_t *const set,
860 size_t arg_num,
861 bool allow_empty_signal_array
862) {
863 if (!allow_empty_signal_array && zend_hash_num_elements(user_signals) == 0) {
865 return false;
866 }
867
868 errno = 0;
869 if (sigemptyset(set) != 0) {
871 php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
872 return false;
873 }
874
875 zval *user_signal_no;
876 ZEND_HASH_FOREACH_VAL(user_signals, user_signal_no) {
877 bool failed = true;
878 ZVAL_DEREF(user_signal_no);
879 zend_long tmp = zval_try_get_long(user_signal_no, &failed);
880
881 if (failed) {
882 zend_argument_type_error(arg_num, "signals must be of type int, %s given", zend_zval_value_name(user_signal_no));
883 return false;
884 }
885 /* Signals are positive integers */
886 if (tmp < 1 || tmp >= PCNTL_G(num_signals)) {
887 /* PCNTL_G(num_signals) stores +1 from the last valid signal */
888 zend_argument_value_error(arg_num, "signals must be between 1 and %d", PCNTL_G(num_signals)-1);
889 return false;
890 }
891
892 int signal_no = (int) tmp;
893 errno = 0;
894 if (sigaddset(set, signal_no) != 0) {
896 php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
897 return false;
898 }
900 return true;
901}
902#endif
903
904#ifdef HAVE_SIGPROCMASK
905/* {{{ Examine and change blocked signals */
907{
908 zend_long how;
909 HashTable *user_set;
910 /* Optional by-ref out-param array of old signals */
911 zval *user_old_set = NULL;
912
914 Z_PARAM_LONG(how)
915 Z_PARAM_ARRAY_HT(user_set)
917 Z_PARAM_ZVAL(user_old_set)
919
920 if (how != SIG_BLOCK && how != SIG_UNBLOCK && how != SIG_SETMASK) {
921 zend_argument_value_error(1, "must be one of SIG_BLOCK, SIG_UNBLOCK, or SIG_SETMASK");
923 }
924
925 errno = 0;
926 sigset_t old_set;
927 if (sigemptyset(&old_set) != 0) {
929 php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
931 }
932
933 sigset_t set;
934 bool status = php_pcntl_set_user_signal_infos(user_set, &set, 2, /* allow_empty_signal_array */ how == SIG_SETMASK);
935 /* Some error occurred */
936 if (!status) {
938 }
939
940 if (sigprocmask(how, &set, &old_set) != 0) {
942 php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
944 }
945
946 if (user_old_set != NULL) {
947 user_old_set = zend_try_array_init(user_old_set);
948 if (!user_old_set) {
950 }
951
952 for (unsigned int signal_no = 1; signal_no < PCNTL_G(num_signals); ++signal_no) {
953 if (sigismember(&old_set, signal_no) != 1) {
954 continue;
955 }
956 add_next_index_long(user_old_set, signal_no);
957 }
958 }
959
961}
962/* }}} */
963#endif
964
965#ifdef HAVE_STRUCT_SIGINFO_T
966# ifdef HAVE_SIGWAITINFO
967
968/* {{{ Synchronously wait for queued signals */
970{
971 HashTable *user_set;
972 /* Optional by-ref array of ints */
973 zval *user_siginfo = NULL;
974
976 Z_PARAM_ARRAY_HT(user_set)
978 Z_PARAM_ZVAL(user_siginfo)
980
981 sigset_t set;
982 bool status = php_pcntl_set_user_signal_infos(user_set, &set, 1, /* allow_empty_signal_array */ false);
983 /* Some error occurred */
984 if (!status) {
986 }
987
988 errno = 0;
989 siginfo_t siginfo;
990 int signal_no = sigwaitinfo(&set, &siginfo);
991 /* sigwaitinfo() never sets errno to EAGAIN according to POSIX */
992 if (signal_no == -1) {
994 php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
996 }
997
998 /* sigwaitinfo can return 0 on success on some platforms, e.g. NetBSD */
999 if (!signal_no && siginfo.si_signo) {
1000 signal_no = siginfo.si_signo;
1001 }
1002
1003 pcntl_siginfo_to_zval(signal_no, &siginfo, user_siginfo);
1004
1005 RETURN_LONG(signal_no);
1006}
1007/* }}} */
1008# endif
1009# ifdef HAVE_SIGTIMEDWAIT
1010/* {{{ Wait for queued signals */
1012{
1013 HashTable *user_set;
1014 /* Optional by-ref array of ints */
1015 zval *user_siginfo = NULL;
1016 zend_long tv_sec = 0;
1017 zend_long tv_nsec = 0;
1018
1020 Z_PARAM_ARRAY_HT(user_set)
1022 Z_PARAM_ZVAL(user_siginfo)
1023 Z_PARAM_LONG(tv_sec)
1024 Z_PARAM_LONG(tv_nsec)
1026
1027 sigset_t set;
1028 bool status = php_pcntl_set_user_signal_infos(user_set, &set, 1, /* allow_empty_signal_array */ false);
1029 /* Some error occurred */
1030 if (!status) {
1032 }
1033 if (tv_sec < 0) {
1034 zend_argument_value_error(3, "must be greater than or equal to 0");
1035 RETURN_THROWS();
1036 }
1037 /* Nanosecond between 0 and 1e9 */
1038 if (tv_nsec < 0 || tv_nsec >= 1000000000) {
1039 zend_argument_value_error(4, "must be between 0 and 1e9");
1040 RETURN_THROWS();
1041 }
1042 if (UNEXPECTED(tv_sec == 0 && tv_nsec == 0)) {
1043 zend_value_error("pcntl_sigtimedwait(): At least one of argument #3 ($seconds) or argument #4 ($nanoseconds) must be greater than 0");
1044 RETURN_THROWS();
1045 }
1046
1047 errno = 0;
1048 siginfo_t siginfo;
1049 struct timespec timeout;
1050 timeout.tv_sec = (time_t) tv_sec;
1051 timeout.tv_nsec = tv_nsec;
1052 int signal_no = sigtimedwait(&set, &siginfo, &timeout);
1053 if (signal_no == -1) {
1054 if (errno != EAGAIN) {
1056 php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
1057 }
1059 }
1060
1061 /* sigtimedwait can return 0 on success on some platforms, e.g. NetBSD */
1062 if (!signal_no && siginfo.si_signo) {
1063 signal_no = siginfo.si_signo;
1064 }
1065
1066 pcntl_siginfo_to_zval(signal_no, &siginfo, user_siginfo);
1067
1068 RETURN_LONG(signal_no);
1069}
1070/* }}} */
1071# endif
1072
1073static void pcntl_siginfo_to_zval(int signo, siginfo_t *siginfo, zval *user_siginfo) /* {{{ */
1074{
1075 if (signo > 0 && user_siginfo) {
1076 user_siginfo = zend_try_array_init(user_siginfo);
1077 if (!user_siginfo) {
1078 return;
1079 }
1080
1081 add_assoc_long_ex(user_siginfo, "signo", sizeof("signo")-1, siginfo->si_signo);
1082 add_assoc_long_ex(user_siginfo, "errno", sizeof("errno")-1, siginfo->si_errno);
1083 add_assoc_long_ex(user_siginfo, "code", sizeof("code")-1, siginfo->si_code);
1084 switch(signo) {
1085#ifdef SIGCHLD
1086 case SIGCHLD:
1087 add_assoc_long_ex(user_siginfo, "status", sizeof("status")-1, siginfo->si_status);
1088# ifdef si_utime
1089 add_assoc_double_ex(user_siginfo, "utime", sizeof("utime")-1, siginfo->si_utime);
1090# endif
1091# ifdef si_stime
1092 add_assoc_double_ex(user_siginfo, "stime", sizeof("stime")-1, siginfo->si_stime);
1093# endif
1094 add_assoc_long_ex(user_siginfo, "pid", sizeof("pid")-1, siginfo->si_pid);
1095 add_assoc_long_ex(user_siginfo, "uid", sizeof("uid")-1, siginfo->si_uid);
1096 break;
1097 case SIGUSR1:
1098 case SIGUSR2:
1099 add_assoc_long_ex(user_siginfo, "pid", sizeof("pid")-1, siginfo->si_pid);
1100 add_assoc_long_ex(user_siginfo, "uid", sizeof("uid")-1, siginfo->si_uid);
1101 break;
1102#endif
1103 case SIGILL:
1104 case SIGFPE:
1105 case SIGSEGV:
1106 case SIGBUS:
1107 add_assoc_double_ex(user_siginfo, "addr", sizeof("addr")-1, (zend_long)siginfo->si_addr);
1108 break;
1109#if defined(SIGPOLL) && !defined(__CYGWIN__)
1110 case SIGPOLL:
1111 add_assoc_long_ex(user_siginfo, "band", sizeof("band")-1, siginfo->si_band);
1112# ifdef si_fd
1113 add_assoc_long_ex(user_siginfo, "fd", sizeof("fd")-1, siginfo->si_fd);
1114# endif
1115 break;
1116#endif
1117
1118#ifdef SIGTRAP
1119 case SIGTRAP:
1120# if defined(si_syscall) && defined(__FreeBSD__)
1121 if (siginfo->si_code == TRAP_CAP) {
1122 add_assoc_long_ex(user_siginfo, "syscall", sizeof("syscall")-1, (zend_long)siginfo->si_syscall);
1123 } else {
1124 add_assoc_long_ex(user_siginfo, "trapno", sizeof("trapno")-1, (zend_long)siginfo->si_trapno);
1125 }
1126
1127# endif
1128 break;
1129
1130#endif
1131 }
1132#if defined(SIGRTMIN) && defined(SIGRTMAX)
1133 if (SIGRTMIN <= signo && signo <= SIGRTMAX) {
1134 add_assoc_long_ex(user_siginfo, "pid", sizeof("pid")-1, siginfo->si_pid);
1135 add_assoc_long_ex(user_siginfo, "uid", sizeof("uid")-1, siginfo->si_uid);
1136 }
1137#endif
1138 }
1139}
1140/* }}} */
1141#endif
1142
1143#ifdef HAVE_GETPRIORITY
1144/* {{{ Get the priority of any process */
1146{
1147 zend_long who = PRIO_PROCESS;
1148 zend_long pid;
1149 bool pid_is_null = 1;
1150 int pri;
1151
1154 Z_PARAM_LONG_OR_NULL(pid, pid_is_null)
1155 Z_PARAM_LONG(who)
1157
1158 /* needs to be cleared, since any returned value is valid */
1159 errno = 0;
1160
1161 pid = pid_is_null ? 0 : pid;
1162 pri = getpriority(who, pid);
1163
1164 if (errno) {
1166 switch (errno) {
1167 case ESRCH:
1168 php_error_docref(NULL, E_WARNING, "Error %d: No process was located using the given parameters", errno);
1169 break;
1170 case EINVAL:
1171#ifdef PRIO_DARWIN_BG
1172 if (who != PRIO_PGRP && who != PRIO_USER && who != PRIO_PROCESS && who != PRIO_DARWIN_THREAD) {
1173 zend_argument_value_error(2, "must be one of PRIO_PGRP, PRIO_USER, PRIO_PROCESS or PRIO_DARWIN_THREAD");
1174 RETURN_THROWS();
1175 } else if (who == PRIO_DARWIN_THREAD && pid != 0) {
1176 zend_argument_value_error(1, "must be 0 (zero) if PRIO_DARWIN_THREAD is provided as second parameter");
1177 RETURN_THROWS();
1178 } else {
1179 zend_argument_value_error(1, "is not a valid process, process group, or user ID");
1180 RETURN_THROWS();
1181 }
1182#else
1183 zend_argument_value_error(2, "must be one of PRIO_PGRP, PRIO_USER, or PRIO_PROCESS");
1184 RETURN_THROWS();
1185#endif
1186
1187 default:
1188 php_error_docref(NULL, E_WARNING, "Unknown error %d has occurred", errno);
1189 break;
1190 }
1192 }
1193
1194 RETURN_LONG(pri);
1195}
1196/* }}} */
1197#endif
1198
1199#ifdef HAVE_SETPRIORITY
1200/* {{{ Change the priority of any process */
1202{
1203 zend_long who = PRIO_PROCESS;
1204 zend_long pid;
1205 bool pid_is_null = 1;
1206 zend_long pri;
1207
1209 Z_PARAM_LONG(pri)
1211 Z_PARAM_LONG_OR_NULL(pid, pid_is_null)
1212 Z_PARAM_LONG(who)
1214
1215 pid = pid_is_null ? 0 : pid;
1216
1217 if (setpriority(who, pid, pri)) {
1219 switch (errno) {
1220 case ESRCH:
1221 php_error_docref(NULL, E_WARNING, "Error %d: No process was located using the given parameters", errno);
1222 break;
1223 case EINVAL:
1224#ifdef PRIO_DARWIN_BG
1225 if (who != PRIO_PGRP && who != PRIO_USER && who != PRIO_PROCESS && who != PRIO_DARWIN_THREAD) {
1226 zend_argument_value_error(3, "must be one of PRIO_PGRP, PRIO_USER, PRIO_PROCESS or PRIO_DARWIN_THREAD");
1227 RETURN_THROWS();
1228 } else if (who == PRIO_DARWIN_THREAD && pid != 0) {
1229 zend_argument_value_error(2, "must be 0 (zero) if PRIO_DARWIN_THREAD is provided as second parameter");
1230 RETURN_THROWS();
1231 } else if (who == PRIO_DARWIN_THREAD && pid == 0 && (pri != 0 && pri != PRIO_DARWIN_BG)) {
1232 zend_argument_value_error(1, "must be either 0 (zero) or PRIO_DARWIN_BG, for mode PRIO_DARWIN_THREAD");
1233 RETURN_THROWS();
1234 } else {
1235 zend_argument_value_error(2, "is not a valid process, process group, or user ID");
1236 RETURN_THROWS();
1237 }
1238#else
1239 zend_argument_value_error(3, "must be one of PRIO_PGRP, PRIO_USER, or PRIO_PROCESS");
1240 RETURN_THROWS();
1241#endif
1242 case EPERM:
1243 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);
1244 break;
1245 case EACCES:
1246 php_error_docref(NULL, E_WARNING, "Error %d: Only a super user may attempt to increase the process priority", errno);
1247 break;
1248 default:
1249 php_error_docref(NULL, E_WARNING, "Unknown error %d has occurred", errno);
1250 break;
1251 }
1253 }
1254
1256}
1257/* }}} */
1258#endif
1259
1260/* {{{ Retrieve the error number set by the last pcntl function which failed. */
1267/* }}} */
1268
1269/* {{{ Retrieve the system error message associated with the given errno. */
1280/* }}} */
1281
1282/* Our custom signal handler that calls the appropriate php_function */
1283#ifdef HAVE_STRUCT_SIGINFO_T
1284static void pcntl_signal_handler(int signo, siginfo_t *siginfo, void *context)
1285#else
1286static void pcntl_signal_handler(int signo)
1287#endif
1288{
1289 struct php_pcntl_pending_signal *psig = PCNTL_G(spares);
1290 if (!psig) {
1291 /* oops, too many signals for us to track, so we'll forget about this one */
1292 return;
1293 }
1294 PCNTL_G(spares) = psig->next;
1295
1296 psig->signo = signo;
1297 psig->next = NULL;
1298
1299#ifdef HAVE_STRUCT_SIGINFO_T
1300 psig->siginfo = *siginfo;
1301#endif
1302
1303 /* the head check is important, as the tick handler cannot atomically clear both
1304 * the head and tail */
1305 if (PCNTL_G(head) && PCNTL_G(tail)) {
1306 PCNTL_G(tail)->next = psig;
1307 } else {
1308 PCNTL_G(head) = psig;
1309 }
1310 PCNTL_G(tail) = psig;
1312 if (PCNTL_G(async_signals)) {
1313 zend_atomic_bool_store_ex(&EG(vm_interrupt), true);
1314 }
1315}
1316
1317void pcntl_signal_dispatch(void)
1318{
1319 zval params[2], *handle, retval;
1320 struct php_pcntl_pending_signal *queue, *next;
1321 sigset_t mask;
1322 sigset_t old_mask;
1323
1324 if(!PCNTL_G(pending_signals)) {
1325 return;
1326 }
1327
1328 /* Mask all signals */
1329 sigfillset(&mask);
1330 sigprocmask(SIG_BLOCK, &mask, &old_mask);
1331
1332 /* Bail if the queue is empty or if we are already playing the queue */
1334 sigprocmask(SIG_SETMASK, &old_mask, NULL);
1335 return;
1336 }
1337
1338 /* Prevent switching fibers when handling signals */
1340
1341 /* Prevent reentrant handler calls */
1343
1344 queue = PCNTL_G(head);
1345 PCNTL_G(head) = NULL; /* simple stores are atomic */
1346
1347 /* Allocate */
1348 while (queue) {
1350 if (Z_TYPE_P(handle) != IS_LONG) {
1351 ZVAL_NULL(&retval);
1352 ZVAL_LONG(&params[0], queue->signo);
1353#ifdef HAVE_STRUCT_SIGINFO_T
1354 array_init(&params[1]);
1355 pcntl_siginfo_to_zval(queue->signo, &queue->siginfo, &params[1]);
1356#else
1357 ZVAL_NULL(&params[1]);
1358#endif
1359
1360 /* Call php signal handler - Note that we do not report errors, and we ignore the return value */
1361 /* FIXME: this is probably broken when multiple signals are handled in this while loop (retval) */
1362 call_user_function(NULL, NULL, handle, &retval, 2, params);
1364#ifdef HAVE_STRUCT_SIGINFO_T
1365 zval_ptr_dtor(&params[1]);
1366#endif
1367 }
1368 }
1369
1370 next = queue->next;
1371 queue->next = PCNTL_G(spares);
1372 PCNTL_G(spares) = queue;
1373 queue = next;
1374 }
1375
1377
1378 /* Re-enable queue */
1380
1381 /* Re-enable fiber switching */
1383
1384 /* return signal mask to previous state */
1385 sigprocmask(SIG_SETMASK, &old_mask, NULL);
1386}
1387
1388static void pcntl_signal_dispatch_tick_function(int dummy_int, void *dummy_pointer)
1389{
1390 return pcntl_signal_dispatch();
1391}
1392
1393/* {{{ Enable/disable asynchronous signal handling and return the old setting. */
1395{
1396 bool on, on_is_null = 1;
1397
1400 Z_PARAM_BOOL_OR_NULL(on, on_is_null)
1402
1403 if (on_is_null) {
1405 }
1406
1408 PCNTL_G(async_signals) = on;
1409}
1410/* }}} */
1411
1412#ifdef HAVE_UNSHARE
1413/* {{{ disassociate parts of the process execution context */
1415{
1417
1421
1422 if (unshare(flags) == -1) {
1424 switch (errno) {
1425#ifdef EINVAL
1426 case EINVAL:
1427 zend_argument_value_error(1, "must be a combination of CLONE_* flags, or at least one flag is unsupported by the kernel");
1428 RETURN_THROWS();
1429 break;
1430#endif
1431#ifdef ENOMEM
1432 case ENOMEM:
1433 php_error_docref(NULL, E_WARNING, "Error %d: Insufficient memory for unshare", errno);
1434 break;
1435#endif
1436#ifdef EPERM
1437 case EPERM:
1438 php_error_docref(NULL, E_WARNING, "Error %d: No privilege to use these flags", errno);
1439 break;
1440#endif
1441#ifdef ENOSPC
1442 case ENOSPC:
1443 php_error_docref(NULL, E_WARNING, "Error %d: Reached the maximum nesting limit for one of the specified namespaces", errno);
1444 break;
1445#endif
1446#ifdef EUSERS
1447 case EUSERS:
1448 php_error_docref(NULL, E_WARNING, "Error %d: Reached the maximum nesting limit for the user namespace", errno);
1449 break;
1450#endif
1451 default:
1452 php_error_docref(NULL, E_WARNING, "Unknown error %d has occurred", errno);
1453 break;
1454 }
1456 }
1457
1459}
1460/* }}} */
1461#endif
1462
1463#ifdef HAVE_RFORK
1464/* {{{ proto bool pcntl_rfork(int flags [, int signal])
1465 More control over the process creation is given over fork/vfork. */
1467{
1469 zend_long csignal = 0;
1470 pid_t pid;
1471
1475 Z_PARAM_LONG(csignal)
1477
1478 /* This is a flag to use with great caution in general, preferably not within PHP */
1479 if ((flags & RFMEM) != 0) {
1480 zend_argument_value_error(1, "must not include RFMEM value, not allowed within this context");
1481 RETURN_THROWS();
1482 }
1483
1484 if ((flags & RFSIGSHARE) != 0) {
1485 zend_argument_value_error(1, "must not include RFSIGSHARE value, not allowed within this context");
1486 RETURN_THROWS();
1487 }
1488
1489 if ((flags & (RFFDG | RFCFDG)) == (RFFDG | RFCFDG)) {
1490 zend_argument_value_error(1, "must not include both RFFDG and RFCFDG, because these flags are mutually exclusive");
1491 RETURN_THROWS();
1492 }
1493
1494 /* A new pid is required */
1495 if (!(flags & (RFPROC))) {
1496 flags |= RFPROC;
1497 }
1498
1499#ifdef RFTSIGZMB
1500 if ((flags & RFTSIGZMB) != 0) {
1501 flags |= RFTSIGFLAGS(csignal);
1502 }
1503#endif
1504
1505 pid = rfork(flags);
1506
1507 if (pid == -1) {
1509 switch (errno) {
1510 case EAGAIN:
1511 php_error_docref(NULL, E_WARNING, "Maximum process creations limit reached\n");
1512 break;
1513
1514 default:
1515 php_error_docref(NULL, E_WARNING, "Error %d", errno);
1516 }
1517 }
1518
1519 RETURN_LONG((zend_long) pid);
1520}
1521#endif
1522/* }}} */
1523
1524#ifdef HAVE_FORKX
1525/* {{{ proto bool pcntl_forkx(int flags)
1526 More elaborated version of fork with the following settings.
1527 FORK_WAITPID: forbid the parent process to wait for multiple pid but one only
1528 FORK_NOSIGCHLD: SIGCHLD signal ignored when the child terminates */
1530{
1532 pid_t pid;
1533
1537
1539 zend_argument_value_error(1, "must be FORK_NOSIGCHLD or FORK_WAITPID");
1540 RETURN_THROWS();
1541 }
1542
1543 pid = forkx(flags);
1544
1545 if (pid == -1) {
1547 switch (errno) {
1548 case EAGAIN:
1549 php_error_docref(NULL, E_WARNING, "Maximum process creations limit reached\n");
1550 break;
1551 case EPERM:
1552 php_error_docref(NULL, E_WARNING, "Calling process not having the proper privileges\n");
1553 break;
1554 case ENOMEM:
1555 php_error_docref(NULL, E_WARNING, "No swap space left\n");
1556 break;
1557 default:
1558 php_error_docref(NULL, E_WARNING, "Error %d", errno);
1559 }
1560 }
1561
1562 RETURN_LONG((zend_long) pid);
1563}
1564#endif
1565/* }}} */
1566
1567#ifdef HAVE_PIDFD_OPEN
1568// The `pidfd_open` syscall is available since 5.3
1569// and `setns` since 3.0.
1571{
1572 zend_long pid, nstype = CLONE_NEWNET;
1573 bool pid_is_null = 1;
1574 int fd, ret;
1575
1578 Z_PARAM_LONG_OR_NULL(pid, pid_is_null)
1579 Z_PARAM_LONG(nstype)
1581
1582 pid = pid_is_null ? getpid() : pid;
1583 fd = syscall(SYS_pidfd_open, pid, 0);
1584 if (errno) {
1586 switch (errno) {
1587 case EINVAL:
1588 case ESRCH:
1589 zend_argument_value_error(1, "is not a valid process (" ZEND_LONG_FMT ")", pid);
1590 RETURN_THROWS();
1591
1592 case ENFILE:
1593 php_error_docref(NULL, E_WARNING, "Error %d: File descriptors per-process limit reached", errno);
1594 break;
1595
1596 case ENODEV:
1597 php_error_docref(NULL, E_WARNING, "Error %d: Anonymous inode fs unsupported", errno);
1598 break;
1599
1600 case ENOMEM:
1601 php_error_docref(NULL, E_WARNING, "Error %d: Insufficient memory for pidfd_open", errno);
1602 break;
1603
1604 default:
1605 php_error_docref(NULL, E_WARNING, "Error %d", errno);
1606 }
1608 }
1609 ret = setns(fd, (int)nstype);
1610 close(fd);
1611
1612 if (ret == -1) {
1614 switch (errno) {
1615 case ESRCH:
1616 zend_argument_value_error(1, "process no longer available (" ZEND_LONG_FMT ")", pid);
1617 RETURN_THROWS();
1618
1619 case EINVAL:
1620 zend_argument_value_error(2, "is an invalid nstype (%d)", nstype);
1621 RETURN_THROWS();
1622
1623 case EPERM:
1624 php_error_docref(NULL, E_WARNING, "Error %d: No required capability for this process", errno);
1625 break;
1626
1627 default:
1628 php_error_docref(NULL, E_WARNING, "Error %d", errno);
1629 }
1631 } else {
1633 }
1634}
1635#endif
1636
1637#ifdef HAVE_SCHED_SETAFFINITY
1639{
1640 zend_long pid;
1641 bool pid_is_null = 1;
1642 cpu_set_t mask;
1643
1646 Z_PARAM_LONG_OR_NULL(pid, pid_is_null)
1648
1649 // 0 == getpid in this context, we're just saving a syscall
1650 pid = pid_is_null ? 0 : pid;
1651
1652 PCNTL_CPU_ZERO(mask);
1653
1654 if (sched_getaffinity(pid, PCNTL_CPUSET_SIZE(mask), PCNTL_CPUSET(mask)) != 0) {
1655 PCNTL_CPU_DESTROY(mask);
1657 switch (errno) {
1658 case ESRCH:
1659 zend_argument_value_error(1, "invalid process (" ZEND_LONG_FMT ")", pid);
1660 RETURN_THROWS();
1661 case EPERM:
1662 php_error_docref(NULL, E_WARNING, "Calling process not having the proper privileges");
1663 break;
1664 case EINVAL:
1665 zend_value_error("invalid cpu affinity mask size");
1666 RETURN_THROWS();
1667 default:
1668 php_error_docref(NULL, E_WARNING, "Error %d", errno);
1669 }
1670
1672 }
1673
1674 zend_ulong maxcpus = (zend_ulong)sysconf(_SC_NPROCESSORS_CONF);
1676
1677 for (zend_ulong i = 0; i < maxcpus; i ++) {
1678 if (PCNTL_CPU_ISSET(i, mask)) {
1680 }
1681 }
1682 PCNTL_CPU_DESTROY(mask);
1683}
1684
1686{
1687 zend_long pid;
1688 bool pid_is_null = 1;
1689 cpu_set_t mask;
1690 zval *hmask = NULL, *ncpu;
1691
1694 Z_PARAM_LONG_OR_NULL(pid, pid_is_null)
1695 Z_PARAM_ARRAY(hmask)
1697
1698 // TODO Why are the arguments optional?
1699 if (!hmask || zend_hash_num_elements(Z_ARRVAL_P(hmask)) == 0) {
1701 RETURN_THROWS();
1702 }
1703
1704 // 0 == getpid in this context, we're just saving a syscall
1705 pid = pid_is_null ? 0 : pid;
1706 zend_long maxcpus = sysconf(_SC_NPROCESSORS_CONF);
1707 PCNTL_CPU_ZERO(mask);
1708
1709 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(hmask), ncpu) {
1710 ZVAL_DEREF(ncpu);
1711 zend_long cpu;
1712 if (Z_TYPE_P(ncpu) != IS_LONG) {
1713 if (Z_TYPE_P(ncpu) == IS_STRING) {
1714 zend_ulong tmp;
1715 if (!ZEND_HANDLE_NUMERIC(Z_STR_P(ncpu), tmp)) {
1716 zend_argument_value_error(2, "cpu id invalid value (%s)", ZSTR_VAL(Z_STR_P(ncpu)));
1717 PCNTL_CPU_DESTROY(mask);
1718 RETURN_THROWS();
1719 }
1720
1721 cpu = (zend_long)tmp;
1722 } else {
1723 zend_argument_type_error(2, "value must be of type int|string, %s given", zend_zval_value_name(ncpu));
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 }
1740
1741 if (sched_setaffinity(pid, PCNTL_CPUSET_SIZE(mask), PCNTL_CPUSET(mask)) != 0) {
1742 PCNTL_CPU_DESTROY(mask);
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 }
1758 } else {
1759 PCNTL_CPU_DESTROY(mask);
1761 }
1762}
1763#endif
1764
1765#if defined(HAVE_SCHED_GETCPU)
1767{
1769
1770 RETURN_LONG(sched_getcpu());
1771}
1772#endif
1773
1774#if defined(HAVE_PTHREAD_SET_QOS_CLASS_SELF_NP)
1775static 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
1793static 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
1820{
1821 qos_class_t qos_class;
1822
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
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
1837{
1838 zval *qos_obj;
1839
1841 Z_PARAM_OBJECT_OF_CLASS(qos_obj, QosClass_ce)
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
1850 zend_throw_error(NULL, "pcntl_setqos_class failed");
1851 RETURN_THROWS();
1852 }
1853}
1854#endif
1855
1856static 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}
DNS_STATUS status
Definition dns_win32.c:49
error($message)
Definition ext_skel.php:22
DL_HANDLE handle
Definition ffi.c:3028
memset(ptr, 0, type->size)
#define SIZE_MAX
Definition funcs.c:51
#define NULL
Definition gdcache.h:45
#define SUCCESS
Definition hash_sha3.c:261
foreach($dp as $el) foreach( $dp as $el) if( $pass2< 2) echo ""
PHPAPI ZEND_COLD void php_error_docref(const char *docref, int type, const char *format,...)
Definition main.c:1173
#define next(ls)
Definition minilua.c:2661
zend_module_entry pcntl_module_entry
Definition pcntl.c:151
#define NSIG
Definition pcntl.c:137
#define PHP_RUSAGE_TO_ARRAY(from, to)
Definition pcntl.c:342
const SIGPOLL
pcntl_signal(int $signal, $handler, bool $restart_syscalls=true)
const PRIO_PGRP
const SIG_BLOCK
pcntl_wait(&$status, int $flags=0, &$resource_usage=[])
const PRIO_DARWIN_THREAD
const SIG_ERR
pcntl_wifstopped(int $status)
pcntl_signal_get_handler(int $signal)
const SIGRTMAX
pcntl_waitpid(int $process_id, &$status, int $flags=0, &$resource_usage=[])
const PRIO_USER
pcntl_setcpuaffinity(?int $process_id=null, array $cpu_ids=[])
const WEXITED
const SIGTRAP
pcntl_exec(string $path, array $args=[], array $env_vars=[])
const SIG_UNBLOCK
const SIG_IGN
pcntl_wifcontinued(int $status)
pcntl_getpriority(?int $process_id=null, int $mode=PRIO_PROCESS)
const RFFDG
pcntl_get_last_error()
pcntl_alarm(int $seconds)
const SIGUSR1
pcntl_wstopsig(int $status)
const FORK_NOSIGCHLD
pcntl_getcpu()
pcntl_wexitstatus(int $status)
const SIGSEGV
const SIGCHLD
const SIGILL
const PRIO_DARWIN_BG
const SIG_DFL
pcntl_strerror(int $error_code)
const CLONE_NEWNET
const RFTSIGZMB
pcntl_getqos_class()
pcntl_sigprocmask(int $mode, array $signals, &$old_signals=null)
pcntl_setpriority(int $priority, ?int $process_id=null, int $mode=PRIO_PROCESS)
const SIGFPE
pcntl_sigtimedwait(array $signals, &$info=[], int $seconds=0, int $nanoseconds=0)
const FORK_WAITPID
const RFPROC
pcntl_wifsignaled(int $status)
const SIGRTMIN
const SIG_SETMASK
pcntl_sigwaitinfo(array $signals, &$info=[])
pcntl_getcpuaffinity(?int $process_id=null)
pcntl_forkx(int $flags)
const SIGBUS
pcntl_rfork(int $flags, int $signal=0)
pcntl_waitid(int $idtype=P_ALL, ?int $id=null, &$info=[], int $flags=WEXITED)
const SIGUSR2
pcntl_wtermsig(int $status)
const RFCFDG
pcntl_setqos_class(Pcntl\QosClass $qos_class=Pcntl\QosClass::Default)
const P_ALL
pcntl_setns(?int $process_id=null, int $nstype=CLONE_NEWNET)
pcntl_unshare(int $flags)
const PRIO_PROCESS
pcntl_wifexited(int $status)
pcntl_fork()
pcntl_async_signals(?bool $enable=null)
php_info_print_table_start()
Definition info.c:1064
php_info_print_table_row(2, "PDO Driver for Firebird", "enabled")
php_info_print_table_end()
Definition info.c:1074
#define PHP_GINIT
Definition php.h:397
#define PHP_FUNCTION
Definition php.h:364
#define PHP_MSHUTDOWN_FUNCTION
Definition php.h:401
#define PHP_MINFO
Definition php.h:396
#define PHP_MINIT_FUNCTION
Definition php.h:400
#define PHP_RINIT
Definition php.h:394
#define strlcpy
Definition php.h:159
#define PHP_MSHUTDOWN
Definition php.h:393
#define strlcat
Definition php.h:169
#define PHP_MINFO_FUNCTION
Definition php.h:404
#define PHP_GINIT_FUNCTION
Definition php.h:405
#define PHP_RSHUTDOWN
Definition php.h:395
#define PHP_RINIT_FUNCTION
Definition php.h:402
#define PHP_RSHUTDOWN_FUNCTION
Definition php.h:403
#define PHP_MINIT
Definition php.h:392
#define PHP_MODULE_GLOBALS
Definition php.h:408
PHP_JSON_API size_t int options
Definition php_json.h:102
bool async_signals
Definition php_pcntl.h:50
unsigned num_signals
Definition php_pcntl.h:51
#define PCNTL_G(v)
Definition php_pcntl.h:59
int last_error
Definition php_pcntl.h:48
#define PHP_PCNTL_VERSION
Definition php_pcntl.h:28
struct php_pcntl_pending_signal * head
Definition php_pcntl.h:47
int processing_signal_queue
Definition php_pcntl.h:46
volatile char pending_signals
Definition php_pcntl.h:49
struct php_pcntl_pending_signal * tail
Definition php_pcntl.h:47
struct php_pcntl_pending_signal * spares
Definition php_pcntl.h:47
HashTable php_signal_table
Definition php_pcntl.h:45
unsigned char key[REFLECTION_KEY_LEN]
Sigfunc * php_signal(int signo, Sigfunc *func, int restart)
Definition php_signal.c:60
Sigfunc * php_signal4(int signo, Sigfunc *func, int restart, int mask_all)
Definition php_signal.c:24
void Sigfunc(int)
Definition php_signal.h:24
PHPAPI void php_add_tick_function(void(*func)(int, void *), void *arg)
Definition php_ticks.c:49
int fd
Definition phpdbg.h:282
#define SIGALRM
Definition signal.h:8
Definition dce.c:49
struct php_pcntl_pending_signal * next
Definition php_pcntl.h:37
long tv_nsec
Definition time.h:35
time_t tv_sec
Definition time.h:34
#define close(a)
#define EUSERS
#define errno
#define EAGAIN
#define ENOSYS
Definition winutil.h:42
ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format,...)
Definition zend.c:1772
ZEND_API ZEND_COLD void zend_value_error(const char *format,...)
Definition zend.c:1849
ZEND_API void(* zend_interrupt_function)(zend_execute_data *execute_data)
Definition zend.c:89
#define ZEND_TSRMLS_CACHE_UPDATE()
Definition zend.h:69
#define ZEND_TSRMLS_CACHE_DEFINE()
Definition zend.h:68
ZEND_API const char * zend_zval_value_name(const zval *arg)
Definition zend_API.c:148
ZEND_API zend_result add_next_index_long(zval *arg, zend_long n)
Definition zend_API.c:2132
ZEND_API void add_assoc_double_ex(zval *arg, const char *key, size_t key_len, double d)
Definition zend_API.c:1964
ZEND_API ZEND_COLD void zend_argument_must_not_be_empty_error(uint32_t arg_num)
Definition zend_API.c:443
ZEND_API bool zend_is_callable_ex(zval *callable, zend_object *object, uint32_t check_flags, zend_string **callable_name, zend_fcall_info_cache *fcc, char **error)
Definition zend_API.c:4271
ZEND_API void add_assoc_long_ex(zval *arg, const char *key, size_t key_len, zend_long n)
Definition zend_API.c:1928
ZEND_API ZEND_COLD void zend_argument_value_error(uint32_t arg_num, const char *format,...)
Definition zend_API.c:433
ZEND_API ZEND_COLD void zend_argument_type_error(uint32_t arg_num, const char *format,...)
Definition zend_API.c:423
#define ZEND_NUM_ARGS()
Definition zend_API.h:530
#define RETURN_STRING(s)
Definition zend_API.h:1043
#define RETURN_COPY(zv)
Definition zend_API.h:1054
#define ZEND_PARSE_PARAMETERS_END()
Definition zend_API.h:1641
#define RETURN_FALSE
Definition zend_API.h:1058
#define ZEND_PARSE_PARAMETERS_NONE()
Definition zend_API.h:1623
#define ZEND_DECLARE_MODULE_GLOBALS(module_name)
Definition zend_API.h:268
#define Z_PARAM_OPTIONAL
Definition zend_API.h:1667
#define ZEND_GET_MODULE(name)
Definition zend_API.h:241
#define ZEND_PARSE_PARAMETERS_START(min_num_args, max_num_args)
Definition zend_API.h:1620
#define Z_PARAM_BOOL_OR_NULL(dest, is_null)
Definition zend_API.h:1729
#define ZEND_TRY_ASSIGN_REF_LONG(zv, lval)
Definition zend_API.h:1205
#define Z_PARAM_LONG(dest)
Definition zend_API.h:1896
#define RETURN_LONG(l)
Definition zend_API.h:1037
#define RETURN_BOOL(b)
Definition zend_API.h:1035
#define RETURN_OBJ_COPY(r)
Definition zend_API.h:1053
#define RETURN_THROWS()
Definition zend_API.h:1060
#define Z_PARAM_ARRAY_HT(dest)
Definition zend_API.h:1852
#define RETVAL_BOOL(b)
Definition zend_API.h:1009
#define Z_PARAM_LONG_OR_NULL(dest, is_null)
Definition zend_API.h:1899
#define call_user_function(function_table, object, function_name, retval_ptr, param_count, params)
Definition zend_API.h:687
#define Z_PARAM_OBJECT_OF_CLASS(dest, _ce)
Definition zend_API.h:1976
#define Z_PARAM_PATH(dest, dest_len)
Definition zend_API.h:2026
#define Z_PARAM_ARRAY(dest)
Definition zend_API.h:1682
#define Z_PARAM_ZVAL(dest)
Definition zend_API.h:2100
#define RETURN_TRUE
Definition zend_API.h:1059
#define array_init(arg)
Definition zend_API.h:537
#define efree(ptr)
Definition zend_alloc.h:155
#define safe_emalloc(nmemb, size, offset)
Definition zend_alloc.h:154
#define emalloc(size)
Definition zend_alloc.h:151
struct _zval_struct zval
zend_string_release_ex(func->internal_function.function_name, 0)
zval * args
ZEND_API zend_object * zend_enum_get_case_cstr(zend_class_entry *ce, const char *name)
Definition zend_enum.c:627
#define E_WARNING
Definition zend_errors.h:24
ZEND_API void(ZEND_FASTCALL *zend_touch_vm_stack_data)(void *vm_stack_data)
ZEND_API void zend_fiber_switch_unblock(void)
ZEND_API void zend_fiber_switch_block(void)
#define EG(v)
ZEND_API void ZEND_FASTCALL zend_hash_destroy(HashTable *ht)
Definition zend_hash.c:1727
ZEND_API zval *ZEND_FASTCALL zend_hash_index_update(HashTable *ht, zend_ulong h, zval *pData)
Definition zend_hash.c:1219
ZEND_API zval *ZEND_FASTCALL zend_hash_index_find(const HashTable *ht, zend_ulong h)
Definition zend_hash.c:2701
#define zend_hash_init(ht, nSize, pHashFunction, pDestructor, persistent)
Definition zend_hash.h:108
#define ZEND_HASH_FOREACH_NUM_KEY_VAL(ht, _h, _val)
Definition zend_hash.h:1156
#define ZEND_HANDLE_NUMERIC(key, idx)
Definition zend_hash.h:420
#define ZEND_HASH_FOREACH_KEY_VAL(ht, _h, _key, _val)
Definition zend_hash.h:1181
#define ZEND_HASH_FOREACH_END()
Definition zend_hash.h:1086
#define ZEND_HASH_FOREACH_VAL(ht, _val)
Definition zend_hash.h:1102
#define ZEND_ULONG_FMT
Definition zend_long.h:88
int32_t zend_long
Definition zend_long.h:42
uint32_t zend_ulong
Definition zend_long.h:43
#define ZEND_LONG_FMT
Definition zend_long.h:87
struct _zend_string zend_string
#define zend_max_execution_timer_init()
#define STANDARD_MODULE_HEADER
struct _zend_module_entry zend_module_entry
#define STANDARD_MODULE_PROPERTIES_EX
ZEND_API zend_string *ZEND_FASTCALL zend_long_to_str(zend_long num)
ZEND_API zend_long ZEND_FASTCALL zval_try_get_long(const zval *op, bool *failed)
#define ZEND_ASSERT(c)
#define UNEXPECTED(condition)
struct _zend_class_entry zend_class_entry
struct _zend_object zend_object
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
#define zend_string_equals_literal(str, literal)
#define ZSTR_LEN(zstr)
Definition zend_string.h:69
#define Z_TYPE_P(zval_p)
Definition zend_types.h:660
#define Z_TRY_ADDREF_P(pz)
#define Z_STRVAL_P(zval_p)
Definition zend_types.h:975
#define Z_ARRVAL_P(zval_p)
Definition zend_types.h:987
#define ZVAL_NULL(z)
#define ZVAL_DEREF(z)
#define ZVAL_LONG(z, l)
#define IS_STRING
Definition zend_types.h:606
struct _zend_array HashTable
Definition zend_types.h:386
#define Z_OBJ_P(zval_p)
Definition zend_types.h:990
#define Z_STR_P(zval_p)
Definition zend_types.h:972
#define Z_STRLEN_P(zval_p)
Definition zend_types.h:978
#define IS_LONG
Definition zend_types.h:604
#define SEPARATE_ARRAY(zv)
struct _zend_execute_data zend_execute_data
Definition zend_types.h:91
#define Z_LVAL_P(zval_p)
Definition zend_types.h:966
ZEND_API void zval_ptr_dtor(zval *zval_ptr)
#define ZVAL_PTR_DTOR
zval retval
zval * return_value
uint32_t arg_num
execute_data
zval * ret