From 1fe4de1dc762c82c500e5ac987f714bfcddaedcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Thu, 20 Jun 2024 14:47:27 +0200 Subject: [PATCH 01/44] ext/pcntl: Added new function pcntl_waitid --- ext/pcntl/config.m4 | 2 +- ext/pcntl/pcntl.c | 32 ++++++++++++++++++++++++++++++++ ext/pcntl/pcntl.stub.php | 2 ++ ext/pcntl/pcntl_arginfo.h | 10 +++++++++- 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/ext/pcntl/config.m4 b/ext/pcntl/config.m4 index 5357e7dbc01f0..a2d1edbe9bf8f 100644 --- a/ext/pcntl/config.m4 +++ b/ext/pcntl/config.m4 @@ -4,7 +4,7 @@ PHP_ARG_ENABLE([pcntl], [Enable pcntl support (CLI/CGI only)])]) if test "$PHP_PCNTL" != "no"; then - for function in fork sigaction waitpid; do + for function in fork sigaction waitpid waitid; do AC_CHECK_FUNC([$function],, [AC_MSG_ERROR([ext/pcntl: required function $function() not found.])]) done diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index b26bde1ff8fcc..a8aac61426b61 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -385,6 +385,38 @@ PHP_FUNCTION(pcntl_waitpid) } /* }}} */ +/* {{{ Waits on or returns the status of a forked child as defined by the waitid() system call */ +PHP_FUNCTION(pcntl_waitid) +{ + zend_long idtype = P_PID; + zend_long id = 0; + zend_long options = WEXITED; + pid_t child_id; + siginfo_t *infop = emalloc(sizeof(siginfo_t)); + + ZEND_PARSE_PARAMETERS_START(2, 3) + Z_PARAM_LONG(idtype) + Z_PARAM_LONG(id) + Z_PARAM_OPTIONAL + Z_PARAM_LONG(options) + ZEND_PARSE_PARAMETERS_END(); + + // TODO check idtype is one of P_PID, P_PIDFD, P_PGID, P_ALL + // TODO check flags, minimum one of WEXITED, WSTOPPED, WCONTINUED, + // additionally WNOHANG or WNOWAIT + + child_id = waitid((idtype_t) idtype, (id_t) id, infop, (int) options); + + // TODO parse infop into array and return it + + if (child_id == -1) { + RETURN_LONG((zend_long) errno); + } else { + RETURN_LONG((zend_long) child_id); + } +} +/* }}} */ + /* {{{ Waits on or returns the status of a forked child as defined by the waitpid() system call */ PHP_FUNCTION(pcntl_wait) { diff --git a/ext/pcntl/pcntl.stub.php b/ext/pcntl/pcntl.stub.php index 7db1e8ed594e2..97a1a1b2e67a3 100644 --- a/ext/pcntl/pcntl.stub.php +++ b/ext/pcntl/pcntl.stub.php @@ -927,6 +927,8 @@ function pcntl_fork(): int {} */ function pcntl_waitpid(int $process_id, &$status, int $flags = 0, &$resource_usage = []): int {} +function pcntl_waitid(int $idtype, int $id, int $flags = 0): int {} + /** * @param int $status * @param array $resource_usage diff --git a/ext/pcntl/pcntl_arginfo.h b/ext/pcntl/pcntl_arginfo.h index 003f9cb5393d5..bdced07b1042e 100644 --- a/ext/pcntl/pcntl_arginfo.h +++ b/ext/pcntl/pcntl_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 775838bf2abbf32933f5cec6e4a85e07e8cea247 */ + * Stub hash: 9735a82c5881d5d89d7feabfafd63b7c59108cd9 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_fork, 0, 0, IS_LONG, 0) ZEND_END_ARG_INFO() @@ -11,6 +11,12 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_waitpid, 0, 2, IS_LONG, 0) ZEND_ARG_INFO_WITH_DEFAULT_VALUE(1, resource_usage, "[]") ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_waitid, 0, 2, IS_LONG, 0) + ZEND_ARG_TYPE_INFO(0, idtype, IS_LONG, 0) + ZEND_ARG_TYPE_INFO(0, id, IS_LONG, 0) + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "0") +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_wait, 0, 1, IS_LONG, 0) ZEND_ARG_INFO(1, status) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "0") @@ -170,6 +176,7 @@ ZEND_END_ARG_INFO() ZEND_FUNCTION(pcntl_fork); ZEND_FUNCTION(pcntl_waitpid); +ZEND_FUNCTION(pcntl_waitid); ZEND_FUNCTION(pcntl_wait); ZEND_FUNCTION(pcntl_signal); ZEND_FUNCTION(pcntl_signal_get_handler); @@ -234,6 +241,7 @@ ZEND_FUNCTION(pcntl_setqos_class); static const zend_function_entry ext_functions[] = { ZEND_FE(pcntl_fork, arginfo_pcntl_fork) ZEND_FE(pcntl_waitpid, arginfo_pcntl_waitpid) + ZEND_FE(pcntl_waitid, arginfo_pcntl_waitid) ZEND_FE(pcntl_wait, arginfo_pcntl_wait) ZEND_FE(pcntl_signal, arginfo_pcntl_signal) ZEND_FE(pcntl_signal_get_handler, arginfo_pcntl_signal_get_handler) From 0483f2286ec32c528facd4628c87ea94adfe6ed2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Thu, 20 Jun 2024 15:49:28 +0200 Subject: [PATCH 02/44] Validate flags, fill siginfo array --- ext/pcntl/pcntl.c | 43 ++++++++++++++++++++++++++++----------- ext/pcntl/pcntl.stub.php | 3 ++- ext/pcntl/pcntl_arginfo.h | 5 +++-- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index a8aac61426b61..c4f0a0a528f23 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -390,30 +390,49 @@ PHP_FUNCTION(pcntl_waitid) { zend_long idtype = P_PID; zend_long id = 0; + /* Optional by-ref array of ints */ + zval *user_siginfo = NULL; zend_long options = WEXITED; - pid_t child_id; - siginfo_t *infop = emalloc(sizeof(siginfo_t)); + int success; - ZEND_PARSE_PARAMETERS_START(2, 3) + ZEND_PARSE_PARAMETERS_START(2, 4) Z_PARAM_LONG(idtype) Z_PARAM_LONG(id) Z_PARAM_OPTIONAL + Z_PARAM_ZVAL(user_siginfo) Z_PARAM_LONG(options) ZEND_PARSE_PARAMETERS_END(); - // TODO check idtype is one of P_PID, P_PIDFD, P_PGID, P_ALL - // TODO check flags, minimum one of WEXITED, WSTOPPED, WCONTINUED, - // additionally WNOHANG or WNOWAIT + // check idtype is one of P_PID, P_PIDFD, P_PGID, P_ALL + if (idtype != P_ALL && idtype != P_PID && idtype != P_PIDFD && idtype != P_PGID) { + php_error_docref(NULL, E_WARNING, "idtype must be one of P_PID, P_PIDFD, P_PGID, P_ALL"); + RETURN_FALSE; + } + + // check flags, minimum one of WEXITED, WSTOPPED, WCONTINUED, + // additionally WNOHANG or WNOWAIT can be set + if ((options & WEXITED) != WEXITED && + (options & WSTOPPED) != WSTOPPED && + (options & WCONTINUED) != WCONTINUED) { + php_error_docref(NULL, E_WARNING, "flags must include at least WEXITED, WSTOPPED or WCONTINUED"); + RETURN_FALSE; + } - child_id = waitid((idtype_t) idtype, (id_t) id, infop, (int) options); + errno = 0; + siginfo_t siginfo; - // TODO parse infop into array and return it + success = waitid((idtype_t) idtype, (id_t) id, &siginfo, (int) options); - if (child_id == -1) { - RETURN_LONG((zend_long) errno); - } else { - RETURN_LONG((zend_long) child_id); + if (success == -1) { + PCNTL_G(last_error) = errno; + php_error_docref(NULL, E_WARNING, "%s", strerror(errno)); + RETURN_FALSE; } + + // TODO verify that passing SIGCHLD does what we need + pcntl_siginfo_to_zval(SIGCHLD, &siginfo, user_siginfo); + + RETURN_TRUE; } /* }}} */ diff --git a/ext/pcntl/pcntl.stub.php b/ext/pcntl/pcntl.stub.php index 97a1a1b2e67a3..e698669d0b15a 100644 --- a/ext/pcntl/pcntl.stub.php +++ b/ext/pcntl/pcntl.stub.php @@ -927,7 +927,8 @@ function pcntl_fork(): int {} */ function pcntl_waitpid(int $process_id, &$status, int $flags = 0, &$resource_usage = []): int {} -function pcntl_waitid(int $idtype, int $id, int $flags = 0): int {} +/** @param array $info */ +function pcntl_waitid(int $idtype, int $id, &$info = [], int $flags = 0): bool {} /** * @param int $status diff --git a/ext/pcntl/pcntl_arginfo.h b/ext/pcntl/pcntl_arginfo.h index bdced07b1042e..c169057cae3ce 100644 --- a/ext/pcntl/pcntl_arginfo.h +++ b/ext/pcntl/pcntl_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 9735a82c5881d5d89d7feabfafd63b7c59108cd9 */ + * Stub hash: 4e5706dc73454d8d3b8f49d49a21b8248946aebd */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_fork, 0, 0, IS_LONG, 0) ZEND_END_ARG_INFO() @@ -11,9 +11,10 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_waitpid, 0, 2, IS_LONG, 0) ZEND_ARG_INFO_WITH_DEFAULT_VALUE(1, resource_usage, "[]") ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_waitid, 0, 2, IS_LONG, 0) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_waitid, 0, 2, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, idtype, IS_LONG, 0) ZEND_ARG_TYPE_INFO(0, id, IS_LONG, 0) + ZEND_ARG_INFO_WITH_DEFAULT_VALUE(1, info, "[]") ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "0") ZEND_END_ARG_INFO() From ecefc18887f1518c147b379a9100dad34024c2ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Thu, 20 Jun 2024 16:06:24 +0200 Subject: [PATCH 03/44] Expose needed constants from wait.h --- ext/pcntl/pcntl.stub.php | 39 +++++++++++++++++++++++++++++++++++++++ ext/pcntl/pcntl_arginfo.h | 9 ++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/ext/pcntl/pcntl.stub.php b/ext/pcntl/pcntl.stub.php index e698669d0b15a..e24ff0aeb4b6f 100644 --- a/ext/pcntl/pcntl.stub.php +++ b/ext/pcntl/pcntl.stub.php @@ -26,6 +26,45 @@ const WCONTINUED = UNKNOWN; #endif +/* Additoinal waitid constants: */ +/** + * @var int + * @cvalue LONG_CONST(WEXITED) + */ +const WEXITED = UNKNOWN; +/** + * @var int + * @cvalue LONG_CONST(WSTOPPED) + */ +const WSTOPPED = UNKNOWN; +/** + * @var int + * @cvalue LONG_CONST(WNOWAIT) + */ +const WNOWAIT = UNKNOWN; + +/* First argument to waitid */ +/** + * @var int + * @cvalue LONG_CONST(P_ALL) + */ +const P_ALL = UNKNOWN; +/** + * @var int + * @cvalue LONG_CONST(P_PID) + */ +const P_PID = UNKNOWN; +/** + * @var int + * @cvalue LONG_CONST(P_PGID) + */ +const P_PGID = UNKNOWN; +/** + * @var int + * @cvalue LONG_CONST(P_PIDFD) + */ +const P_PIDFD = UNKNOWN; + /* Signal Constants */ /** diff --git a/ext/pcntl/pcntl_arginfo.h b/ext/pcntl/pcntl_arginfo.h index c169057cae3ce..20ff642d710ac 100644 --- a/ext/pcntl/pcntl_arginfo.h +++ b/ext/pcntl/pcntl_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 4e5706dc73454d8d3b8f49d49a21b8248946aebd */ + * Stub hash: bcdd09720e3209accab2b9c0bd164ad04189fc90 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_fork, 0, 0, IS_LONG, 0) ZEND_END_ARG_INFO() @@ -324,6 +324,13 @@ static void register_pcntl_symbols(int module_number) #if defined(HAVE_WCONTINUED) REGISTER_LONG_CONSTANT("WCONTINUED", LONG_CONST(WCONTINUED), CONST_PERSISTENT); #endif + REGISTER_LONG_CONSTANT("WEXITED", LONG_CONST(WEXITED), CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("WSTOPPED", LONG_CONST(WSTOPPED), CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("WNOWAIT", LONG_CONST(WNOWAIT), CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("P_ALL", LONG_CONST(P_ALL), CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("P_PID", LONG_CONST(P_PID), CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("P_PGID", LONG_CONST(P_PGID), CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("P_PIDFD", LONG_CONST(P_PIDFD), CONST_PERSISTENT); REGISTER_LONG_CONSTANT("SIG_IGN", LONG_CONST(SIG_IGN), CONST_PERSISTENT); REGISTER_LONG_CONSTANT("SIG_DFL", LONG_CONST(SIG_DFL), CONST_PERSISTENT); REGISTER_LONG_CONSTANT("SIG_ERR", LONG_CONST(SIG_ERR), CONST_PERSISTENT); From c8ff3b6042331d8b693b5d38b237cd64f39e0ea7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Thu, 20 Jun 2024 16:25:18 +0200 Subject: [PATCH 04/44] Add ifdef guards around WEXITED, WSTOPPED, WNOWAIT --- ext/pcntl/pcntl.stub.php | 8 ++++++-- ext/pcntl/pcntl_arginfo.h | 8 +++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/ext/pcntl/pcntl.stub.php b/ext/pcntl/pcntl.stub.php index e24ff0aeb4b6f..492ba6a42ceac 100644 --- a/ext/pcntl/pcntl.stub.php +++ b/ext/pcntl/pcntl.stub.php @@ -25,23 +25,27 @@ */ const WCONTINUED = UNKNOWN; #endif - -/* Additoinal waitid constants: */ +#ifdef WEXITED /** * @var int * @cvalue LONG_CONST(WEXITED) */ const WEXITED = UNKNOWN; +#endif +#ifdef WSTOPPED /** * @var int * @cvalue LONG_CONST(WSTOPPED) */ const WSTOPPED = UNKNOWN; +#endif +#ifdef WNOWAIT /** * @var int * @cvalue LONG_CONST(WNOWAIT) */ const WNOWAIT = UNKNOWN; +#endif /* First argument to waitid */ /** diff --git a/ext/pcntl/pcntl_arginfo.h b/ext/pcntl/pcntl_arginfo.h index 20ff642d710ac..5421f66cea4c4 100644 --- a/ext/pcntl/pcntl_arginfo.h +++ b/ext/pcntl/pcntl_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: bcdd09720e3209accab2b9c0bd164ad04189fc90 */ + * Stub hash: 083fb769fd36ca0f6662ce9675b92e30f3c2f983 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_fork, 0, 0, IS_LONG, 0) ZEND_END_ARG_INFO() @@ -324,9 +324,15 @@ static void register_pcntl_symbols(int module_number) #if defined(HAVE_WCONTINUED) REGISTER_LONG_CONSTANT("WCONTINUED", LONG_CONST(WCONTINUED), CONST_PERSISTENT); #endif +#if defined(WEXITED) REGISTER_LONG_CONSTANT("WEXITED", LONG_CONST(WEXITED), CONST_PERSISTENT); +#endif +#if defined(WSTOPPED) REGISTER_LONG_CONSTANT("WSTOPPED", LONG_CONST(WSTOPPED), CONST_PERSISTENT); +#endif +#if defined(WNOWAIT) REGISTER_LONG_CONSTANT("WNOWAIT", LONG_CONST(WNOWAIT), CONST_PERSISTENT); +#endif REGISTER_LONG_CONSTANT("P_ALL", LONG_CONST(P_ALL), CONST_PERSISTENT); REGISTER_LONG_CONSTANT("P_PID", LONG_CONST(P_PID), CONST_PERSISTENT); REGISTER_LONG_CONSTANT("P_PGID", LONG_CONST(P_PGID), CONST_PERSISTENT); From 2dd62b9e2b294f1dd8aa581376ea4a57b5ec2542 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Thu, 20 Jun 2024 17:16:03 +0200 Subject: [PATCH 05/44] Unit tests for pcntl_waitid --- ext/pcntl/tests/pcntl_waitid.phpt | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 ext/pcntl/tests/pcntl_waitid.phpt diff --git a/ext/pcntl/tests/pcntl_waitid.phpt b/ext/pcntl/tests/pcntl_waitid.phpt new file mode 100644 index 0000000000000..2889e0cfeef01 --- /dev/null +++ b/ext/pcntl/tests/pcntl_waitid.phpt @@ -0,0 +1,30 @@ +--TEST-- +pcntl_waitid() +--EXTENSIONS-- +pcntl +posix +--FILE-- + +--EXPECT-- +bool(true) +bool(true) +bool(true) +int(42) From 0ccd92f09687ab8ed440bfeac4fd0918e91dba63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Thu, 20 Jun 2024 17:27:31 +0200 Subject: [PATCH 06/44] Throw a ValueError if parameter validation fails --- ext/pcntl/pcntl.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index c4f0a0a528f23..ac28d28c3ce56 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -403,19 +403,17 @@ PHP_FUNCTION(pcntl_waitid) Z_PARAM_LONG(options) ZEND_PARSE_PARAMETERS_END(); - // check idtype is one of P_PID, P_PIDFD, P_PGID, P_ALL if (idtype != P_ALL && idtype != P_PID && idtype != P_PIDFD && idtype != P_PGID) { - php_error_docref(NULL, E_WARNING, "idtype must be one of P_PID, P_PIDFD, P_PGID, P_ALL"); - RETURN_FALSE; + zend_argument_value_error(1, "must be one of P_ALL, P_PID, P_PGID, P_PIDFD"); + RETURN_THROWS(); } - // check flags, minimum one of WEXITED, WSTOPPED, WCONTINUED, - // additionally WNOHANG or WNOWAIT can be set if ((options & WEXITED) != WEXITED && (options & WSTOPPED) != WSTOPPED && (options & WCONTINUED) != WCONTINUED) { - php_error_docref(NULL, E_WARNING, "flags must include at least WEXITED, WSTOPPED or WCONTINUED"); - RETURN_FALSE; + zend_argument_value_error(4, "must include at least WEXITED, WSTOPPED or WCONTINUED"); + // additionally, WNOHANG or WNOWAIT can be set + RETURN_THROWS(); } errno = 0; From 712b536aa0f1b3ff6f03c682c5a63ca958224794 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Thu, 20 Jun 2024 17:35:04 +0200 Subject: [PATCH 07/44] Evaluate in numerical order --- ext/pcntl/pcntl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index ac28d28c3ce56..9edf4c68c0b76 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -403,7 +403,7 @@ PHP_FUNCTION(pcntl_waitid) Z_PARAM_LONG(options) ZEND_PARSE_PARAMETERS_END(); - if (idtype != P_ALL && idtype != P_PID && idtype != P_PIDFD && idtype != P_PGID) { + if (idtype != P_ALL && idtype != P_PID && idtype != P_PGID && idtype != P_PIDFD) { zend_argument_value_error(1, "must be one of P_ALL, P_PID, P_PGID, P_PIDFD"); RETURN_THROWS(); } From dfe3bfea465f3460a25896e03e115f05c2084efa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Thu, 20 Jun 2024 17:35:29 +0200 Subject: [PATCH 08/44] Guard Linux-specific P_PIDFD constant --- ext/pcntl/pcntl.stub.php | 2 ++ ext/pcntl/pcntl_arginfo.h | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/ext/pcntl/pcntl.stub.php b/ext/pcntl/pcntl.stub.php index 492ba6a42ceac..12d544cb141ad 100644 --- a/ext/pcntl/pcntl.stub.php +++ b/ext/pcntl/pcntl.stub.php @@ -63,11 +63,13 @@ * @cvalue LONG_CONST(P_PGID) */ const P_PGID = UNKNOWN; +#ifdef __linux__ /** * @var int * @cvalue LONG_CONST(P_PIDFD) */ const P_PIDFD = UNKNOWN; +#endif /* Signal Constants */ diff --git a/ext/pcntl/pcntl_arginfo.h b/ext/pcntl/pcntl_arginfo.h index 5421f66cea4c4..5f2b38f1dd685 100644 --- a/ext/pcntl/pcntl_arginfo.h +++ b/ext/pcntl/pcntl_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 083fb769fd36ca0f6662ce9675b92e30f3c2f983 */ + * Stub hash: b029170da681cb9ff577c37cf134fe119f7605bc */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_fork, 0, 0, IS_LONG, 0) ZEND_END_ARG_INFO() @@ -336,7 +336,9 @@ static void register_pcntl_symbols(int module_number) REGISTER_LONG_CONSTANT("P_ALL", LONG_CONST(P_ALL), CONST_PERSISTENT); REGISTER_LONG_CONSTANT("P_PID", LONG_CONST(P_PID), CONST_PERSISTENT); REGISTER_LONG_CONSTANT("P_PGID", LONG_CONST(P_PGID), CONST_PERSISTENT); +#if defined(__linux__) REGISTER_LONG_CONSTANT("P_PIDFD", LONG_CONST(P_PIDFD), CONST_PERSISTENT); +#endif REGISTER_LONG_CONSTANT("SIG_IGN", LONG_CONST(SIG_IGN), CONST_PERSISTENT); REGISTER_LONG_CONSTANT("SIG_DFL", LONG_CONST(SIG_DFL), CONST_PERSISTENT); REGISTER_LONG_CONSTANT("SIG_ERR", LONG_CONST(SIG_ERR), CONST_PERSISTENT); From e23bc2d50c3663d0c04a02902223ba48b9932df5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Thu, 20 Jun 2024 18:15:11 +0200 Subject: [PATCH 09/44] Linux-specific iptype validation --- ext/pcntl/pcntl.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index 9edf4c68c0b76..a51f5c0ab4e28 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -403,10 +403,17 @@ PHP_FUNCTION(pcntl_waitid) Z_PARAM_LONG(options) ZEND_PARSE_PARAMETERS_END(); +#ifdef __linux__ if (idtype != P_ALL && idtype != P_PID && idtype != P_PGID && idtype != P_PIDFD) { zend_argument_value_error(1, "must be one of P_ALL, P_PID, P_PGID, P_PIDFD"); RETURN_THROWS(); } +#else + if (idtype != P_ALL && idtype != P_PID && idtype != P_PGID) { + zend_argument_value_error(1, "must be one of P_ALL, P_PID, P_PGID"); + RETURN_THROWS(); + } +#endif if ((options & WEXITED) != WEXITED && (options & WSTOPPED) != WSTOPPED && From 651df1f92741e2d5b6ff00e539eea5112dc1d06f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Thu, 20 Jun 2024 19:11:17 +0200 Subject: [PATCH 10/44] Include linux/wait.h on Linux to check for idtype constants --- ext/pcntl/pcntl.c | 12 +++++++++++- ext/pcntl/pcntl.stub.php | 4 +++- ext/pcntl/pcntl_arginfo.h | 10 ++++++++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index a51f5c0ab4e28..bb36e86fa4b5e 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -125,6 +125,16 @@ static zend_class_entry *QosClass_ce; #define LONG_CONST(c) (zend_long) c +#if defined(__linux__) +#include +#endif +#if defined (P_ALL) +#define HAVE_P_ALL 1 +#endif +#if defined (P_PIDFD) +#define HAVE_P_PIDFD 1 +#endif + #include "Zend/zend_enum.h" #include "Zend/zend_max_execution_timer.h" @@ -403,7 +413,7 @@ PHP_FUNCTION(pcntl_waitid) Z_PARAM_LONG(options) ZEND_PARSE_PARAMETERS_END(); -#ifdef __linux__ +#ifdef P_PIDFD if (idtype != P_ALL && idtype != P_PID && idtype != P_PGID && idtype != P_PIDFD) { zend_argument_value_error(1, "must be one of P_ALL, P_PID, P_PGID, P_PIDFD"); RETURN_THROWS(); diff --git a/ext/pcntl/pcntl.stub.php b/ext/pcntl/pcntl.stub.php index 12d544cb141ad..3273e84896462 100644 --- a/ext/pcntl/pcntl.stub.php +++ b/ext/pcntl/pcntl.stub.php @@ -48,6 +48,7 @@ #endif /* First argument to waitid */ +#ifdef HAVE_P_ALL /** * @var int * @cvalue LONG_CONST(P_ALL) @@ -63,7 +64,8 @@ * @cvalue LONG_CONST(P_PGID) */ const P_PGID = UNKNOWN; -#ifdef __linux__ +#endif +#ifdef HAVE_P_PIDFD /** * @var int * @cvalue LONG_CONST(P_PIDFD) diff --git a/ext/pcntl/pcntl_arginfo.h b/ext/pcntl/pcntl_arginfo.h index 5f2b38f1dd685..d8823605bd925 100644 --- a/ext/pcntl/pcntl_arginfo.h +++ b/ext/pcntl/pcntl_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: b029170da681cb9ff577c37cf134fe119f7605bc */ + * Stub hash: fa1ef1aeb65c0c7b98c71453f1f7193befa5ad35 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_fork, 0, 0, IS_LONG, 0) ZEND_END_ARG_INFO() @@ -333,10 +333,16 @@ static void register_pcntl_symbols(int module_number) #if defined(WNOWAIT) REGISTER_LONG_CONSTANT("WNOWAIT", LONG_CONST(WNOWAIT), CONST_PERSISTENT); #endif +#if defined(HAVE_P_ALL) REGISTER_LONG_CONSTANT("P_ALL", LONG_CONST(P_ALL), CONST_PERSISTENT); +#endif +#if defined(HAVE_P_ALL) REGISTER_LONG_CONSTANT("P_PID", LONG_CONST(P_PID), CONST_PERSISTENT); +#endif +#if defined(HAVE_P_ALL) REGISTER_LONG_CONSTANT("P_PGID", LONG_CONST(P_PGID), CONST_PERSISTENT); -#if defined(__linux__) +#endif +#if defined(HAVE_P_PIDFD) REGISTER_LONG_CONSTANT("P_PIDFD", LONG_CONST(P_PIDFD), CONST_PERSISTENT); #endif REGISTER_LONG_CONSTANT("SIG_IGN", LONG_CONST(SIG_IGN), CONST_PERSISTENT); From 3ab725db266df0b96bcdf686315743cb73b1557d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Thu, 20 Jun 2024 19:15:47 +0200 Subject: [PATCH 11/44] Leave idtype and flags validation out as it is platform-specific --- ext/pcntl/pcntl.c | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index bb36e86fa4b5e..394567e4f006b 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -413,26 +413,6 @@ PHP_FUNCTION(pcntl_waitid) Z_PARAM_LONG(options) ZEND_PARSE_PARAMETERS_END(); -#ifdef P_PIDFD - if (idtype != P_ALL && idtype != P_PID && idtype != P_PGID && idtype != P_PIDFD) { - zend_argument_value_error(1, "must be one of P_ALL, P_PID, P_PGID, P_PIDFD"); - RETURN_THROWS(); - } -#else - if (idtype != P_ALL && idtype != P_PID && idtype != P_PGID) { - zend_argument_value_error(1, "must be one of P_ALL, P_PID, P_PGID"); - RETURN_THROWS(); - } -#endif - - if ((options & WEXITED) != WEXITED && - (options & WSTOPPED) != WSTOPPED && - (options & WCONTINUED) != WCONTINUED) { - zend_argument_value_error(4, "must include at least WEXITED, WSTOPPED or WCONTINUED"); - // additionally, WNOHANG or WNOWAIT can be set - RETURN_THROWS(); - } - errno = 0; siginfo_t siginfo; From 93f4fad1b102d774f21ba1faf17cb2512c6b558c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Thu, 20 Jun 2024 19:24:13 +0200 Subject: [PATCH 12/44] Added FreeBSD specific idtypes --- ext/pcntl/pcntl.c | 7 +++++-- ext/pcntl/pcntl.stub.php | 23 +++++++++++++++++++++-- ext/pcntl/pcntl_arginfo.h | 19 ++++++++++++++----- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index 394567e4f006b..66f6275d7e2e8 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -129,10 +129,13 @@ static zend_class_entry *QosClass_ce; #include #endif #if defined (P_ALL) -#define HAVE_P_ALL 1 +#define HAVE_POSIX_IDTYPES 1 #endif #if defined (P_PIDFD) -#define HAVE_P_PIDFD 1 +#define HAVE_LINUX_IDTYPES 1 +#endif +#if defined (P_JAILID) +#define HAVE_FREEBSD_IDTYPES 1 #endif #include "Zend/zend_enum.h" diff --git a/ext/pcntl/pcntl.stub.php b/ext/pcntl/pcntl.stub.php index 3273e84896462..88e54dc74ca2c 100644 --- a/ext/pcntl/pcntl.stub.php +++ b/ext/pcntl/pcntl.stub.php @@ -48,7 +48,7 @@ #endif /* First argument to waitid */ -#ifdef HAVE_P_ALL +#ifdef HAVE_POSIX_IDTYPES /** * @var int * @cvalue LONG_CONST(P_ALL) @@ -65,13 +65,32 @@ */ const P_PGID = UNKNOWN; #endif -#ifdef HAVE_P_PIDFD +/* Linux specific idtype */ +#ifdef HAVE_LINUX_IDTYPES /** * @var int * @cvalue LONG_CONST(P_PIDFD) */ const P_PIDFD = UNKNOWN; #endif +/* FreeBSD specific idtypes */ +#ifdef HAVE_FREEBSD_IDTYPES +/** + * @var int + * @cvalue LONG_CONST(P_UID) + */ +const P_UID = UNKNOWN; +/** + * @var int + * @cvalue LONG_CONST(P_GID) + */ +const P_SID = UNKNOWN; +/** + * @var int + * @cvalue LONG_CONST(P_JAILID) + */ +const P_JAILID = UNKNOWN; +#endif /* Signal Constants */ diff --git a/ext/pcntl/pcntl_arginfo.h b/ext/pcntl/pcntl_arginfo.h index d8823605bd925..8ecd8786c0e63 100644 --- a/ext/pcntl/pcntl_arginfo.h +++ b/ext/pcntl/pcntl_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: fa1ef1aeb65c0c7b98c71453f1f7193befa5ad35 */ + * Stub hash: 9fc28327bc0ba06cadfc4c768806dbe119b0d7aa */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_fork, 0, 0, IS_LONG, 0) ZEND_END_ARG_INFO() @@ -333,17 +333,26 @@ static void register_pcntl_symbols(int module_number) #if defined(WNOWAIT) REGISTER_LONG_CONSTANT("WNOWAIT", LONG_CONST(WNOWAIT), CONST_PERSISTENT); #endif -#if defined(HAVE_P_ALL) +#if defined(HAVE_POSIX_IDTYPES) REGISTER_LONG_CONSTANT("P_ALL", LONG_CONST(P_ALL), CONST_PERSISTENT); #endif -#if defined(HAVE_P_ALL) +#if defined(HAVE_POSIX_IDTYPES) REGISTER_LONG_CONSTANT("P_PID", LONG_CONST(P_PID), CONST_PERSISTENT); #endif -#if defined(HAVE_P_ALL) +#if defined(HAVE_POSIX_IDTYPES) REGISTER_LONG_CONSTANT("P_PGID", LONG_CONST(P_PGID), CONST_PERSISTENT); #endif -#if defined(HAVE_P_PIDFD) +#if defined(HAVE_LINUX_IDTYPES) REGISTER_LONG_CONSTANT("P_PIDFD", LONG_CONST(P_PIDFD), CONST_PERSISTENT); +#endif +#if defined(HAVE_FREEBSD_IDTYPES) + REGISTER_LONG_CONSTANT("P_UID", LONG_CONST(P_UID), CONST_PERSISTENT); +#endif +#if defined(HAVE_FREEBSD_IDTYPES) + REGISTER_LONG_CONSTANT("P_SID", LONG_CONST(P_GID), CONST_PERSISTENT); +#endif +#if defined(HAVE_FREEBSD_IDTYPES) + REGISTER_LONG_CONSTANT("P_JAILID", LONG_CONST(P_JAILID), CONST_PERSISTENT); #endif REGISTER_LONG_CONSTANT("SIG_IGN", LONG_CONST(SIG_IGN), CONST_PERSISTENT); REGISTER_LONG_CONSTANT("SIG_DFL", LONG_CONST(SIG_DFL), CONST_PERSISTENT); From ee6b65d73de3d0f1eb15cd8a57f270b807ce9cd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Thu, 20 Jun 2024 19:27:39 +0200 Subject: [PATCH 13/44] Added NetBSD specific idtypes --- ext/pcntl/pcntl.c | 3 +++ ext/pcntl/pcntl.stub.php | 9 ++++++++- ext/pcntl/pcntl_arginfo.h | 11 +++++++---- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index 66f6275d7e2e8..807d9249d690b 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -134,6 +134,9 @@ static zend_class_entry *QosClass_ce; #if defined (P_PIDFD) #define HAVE_LINUX_IDTYPES 1 #endif +#if defined (P_UID) +#define HAVE_NETBSD_IDTYPES 1 +#endif #if defined (P_JAILID) #define HAVE_FREEBSD_IDTYPES 1 #endif diff --git a/ext/pcntl/pcntl.stub.php b/ext/pcntl/pcntl.stub.php index 88e54dc74ca2c..7fdea5c8ac626 100644 --- a/ext/pcntl/pcntl.stub.php +++ b/ext/pcntl/pcntl.stub.php @@ -74,7 +74,7 @@ const P_PIDFD = UNKNOWN; #endif /* FreeBSD specific idtypes */ -#ifdef HAVE_FREEBSD_IDTYPES +#ifdef HAVE_NETBSD_IDTYPES /** * @var int * @cvalue LONG_CONST(P_UID) @@ -84,7 +84,14 @@ * @var int * @cvalue LONG_CONST(P_GID) */ +const P_GID = UNKNOWN; +/** + * @var int + * @cvalue LONG_CONST(P_SID) + */ const P_SID = UNKNOWN; +#endif +#ifdef HAVE_FREEBSD_IDTYPES /** * @var int * @cvalue LONG_CONST(P_JAILID) diff --git a/ext/pcntl/pcntl_arginfo.h b/ext/pcntl/pcntl_arginfo.h index 8ecd8786c0e63..d46786834f2a7 100644 --- a/ext/pcntl/pcntl_arginfo.h +++ b/ext/pcntl/pcntl_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 9fc28327bc0ba06cadfc4c768806dbe119b0d7aa */ + * Stub hash: 12421a8b3bfc7fe8807df04c4eed85a80184f439 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_fork, 0, 0, IS_LONG, 0) ZEND_END_ARG_INFO() @@ -345,11 +345,14 @@ static void register_pcntl_symbols(int module_number) #if defined(HAVE_LINUX_IDTYPES) REGISTER_LONG_CONSTANT("P_PIDFD", LONG_CONST(P_PIDFD), CONST_PERSISTENT); #endif -#if defined(HAVE_FREEBSD_IDTYPES) +#if defined(HAVE_NETBSD_IDTYPES) REGISTER_LONG_CONSTANT("P_UID", LONG_CONST(P_UID), CONST_PERSISTENT); #endif -#if defined(HAVE_FREEBSD_IDTYPES) - REGISTER_LONG_CONSTANT("P_SID", LONG_CONST(P_GID), CONST_PERSISTENT); +#if defined(HAVE_NETBSD_IDTYPES) + REGISTER_LONG_CONSTANT("P_GID", LONG_CONST(P_GID), CONST_PERSISTENT); +#endif +#if defined(HAVE_NETBSD_IDTYPES) + REGISTER_LONG_CONSTANT("P_SID", LONG_CONST(P_SID), CONST_PERSISTENT); #endif #if defined(HAVE_FREEBSD_IDTYPES) REGISTER_LONG_CONSTANT("P_JAILID", LONG_CONST(P_JAILID), CONST_PERSISTENT); From 4830687b1cf61b3ab0782a117f02f0b4fe44eb9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Thu, 20 Jun 2024 19:41:55 +0200 Subject: [PATCH 14/44] Added check for linux/wait.h --- ext/pcntl/config.m4 | 3 +++ ext/pcntl/pcntl.c | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/ext/pcntl/config.m4 b/ext/pcntl/config.m4 index a2d1edbe9bf8f..28af35abc5e07 100644 --- a/ext/pcntl/config.m4 +++ b/ext/pcntl/config.m4 @@ -26,6 +26,9 @@ if test "$PHP_PCNTL" != "no"; then wait4 ])) + AC_CHECK_HEADER([linux/wait.h], + [AC_DEFINE([HAVE_LINUX_WAIT_H], [1], [linux/wait.h found])]) + dnl if unsupported, -1 means automatically ENOSYS in this context AC_CACHE_CHECK([if sched_getcpu is supported], [php_cv_func_sched_getcpu], [AC_RUN_IFELSE([AC_LANG_SOURCE([ diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index 807d9249d690b..508c341317dad 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -125,7 +125,7 @@ static zend_class_entry *QosClass_ce; #define LONG_CONST(c) (zend_long) c -#if defined(__linux__) +#if defined(HAVE_LINUX_WAIT_H) #include #endif #if defined (P_ALL) From e3cfd08e66b7b030a63eb7a0405085372fc4c8c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Thu, 20 Jun 2024 20:11:53 +0200 Subject: [PATCH 15/44] Guard pcntl_waitid behind HAVE_WAITID --- ext/pcntl/config.m4 | 3 ++- ext/pcntl/pcntl.c | 5 ++++- ext/pcntl/pcntl.stub.php | 2 ++ ext/pcntl/pcntl_arginfo.h | 18 +++++++++--------- ext/pcntl/tests/pcntl_waitid.phpt | 4 ++++ 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/ext/pcntl/config.m4 b/ext/pcntl/config.m4 index 28af35abc5e07..c9fe1da136918 100644 --- a/ext/pcntl/config.m4 +++ b/ext/pcntl/config.m4 @@ -4,7 +4,7 @@ PHP_ARG_ENABLE([pcntl], [Enable pcntl support (CLI/CGI only)])]) if test "$PHP_PCNTL" != "no"; then - for function in fork sigaction waitpid waitid; do + for function in fork sigaction waitpid; do AC_CHECK_FUNC([$function],, [AC_MSG_ERROR([ext/pcntl: required function $function() not found.])]) done @@ -24,6 +24,7 @@ if test "$PHP_PCNTL" != "no"; then unshare wait3 wait4 + waitid ])) AC_CHECK_HEADER([linux/wait.h], diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index 508c341317dad..8a7dbb8e1777d 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -125,6 +125,7 @@ static zend_class_entry *QosClass_ce; #define LONG_CONST(c) (zend_long) c +#ifdef HAVE_WAITID #if defined(HAVE_LINUX_WAIT_H) #include #endif @@ -140,6 +141,7 @@ static zend_class_entry *QosClass_ce; #if defined (P_JAILID) #define HAVE_FREEBSD_IDTYPES 1 #endif +#endif #include "Zend/zend_enum.h" #include "Zend/zend_max_execution_timer.h" @@ -401,7 +403,7 @@ PHP_FUNCTION(pcntl_waitpid) } /* }}} */ -/* {{{ Waits on or returns the status of a forked child as defined by the waitid() system call */ +#ifdef HAVE_WAITID PHP_FUNCTION(pcntl_waitid) { zend_long idtype = P_PID; @@ -435,6 +437,7 @@ PHP_FUNCTION(pcntl_waitid) RETURN_TRUE; } +#endif /* }}} */ /* {{{ Waits on or returns the status of a forked child as defined by the waitpid() system call */ diff --git a/ext/pcntl/pcntl.stub.php b/ext/pcntl/pcntl.stub.php index 7fdea5c8ac626..8a4a698612567 100644 --- a/ext/pcntl/pcntl.stub.php +++ b/ext/pcntl/pcntl.stub.php @@ -47,6 +47,7 @@ const WNOWAIT = UNKNOWN; #endif +#ifdef HAVE_WAITID /* First argument to waitid */ #ifdef HAVE_POSIX_IDTYPES /** @@ -98,6 +99,7 @@ */ const P_JAILID = UNKNOWN; #endif +#endif /* Signal Constants */ diff --git a/ext/pcntl/pcntl_arginfo.h b/ext/pcntl/pcntl_arginfo.h index d46786834f2a7..cb2acf035d89b 100644 --- a/ext/pcntl/pcntl_arginfo.h +++ b/ext/pcntl/pcntl_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 12421a8b3bfc7fe8807df04c4eed85a80184f439 */ + * Stub hash: 375360e87a060f9a3171db76fc652a2e95f3c86c */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_fork, 0, 0, IS_LONG, 0) ZEND_END_ARG_INFO() @@ -333,28 +333,28 @@ static void register_pcntl_symbols(int module_number) #if defined(WNOWAIT) REGISTER_LONG_CONSTANT("WNOWAIT", LONG_CONST(WNOWAIT), CONST_PERSISTENT); #endif -#if defined(HAVE_POSIX_IDTYPES) +#if defined(HAVE_WAITID) && defined(HAVE_POSIX_IDTYPES) REGISTER_LONG_CONSTANT("P_ALL", LONG_CONST(P_ALL), CONST_PERSISTENT); #endif -#if defined(HAVE_POSIX_IDTYPES) +#if defined(HAVE_WAITID) && defined(HAVE_POSIX_IDTYPES) REGISTER_LONG_CONSTANT("P_PID", LONG_CONST(P_PID), CONST_PERSISTENT); #endif -#if defined(HAVE_POSIX_IDTYPES) +#if defined(HAVE_WAITID) && defined(HAVE_POSIX_IDTYPES) REGISTER_LONG_CONSTANT("P_PGID", LONG_CONST(P_PGID), CONST_PERSISTENT); #endif -#if defined(HAVE_LINUX_IDTYPES) +#if defined(HAVE_WAITID) && defined(HAVE_LINUX_IDTYPES) REGISTER_LONG_CONSTANT("P_PIDFD", LONG_CONST(P_PIDFD), CONST_PERSISTENT); #endif -#if defined(HAVE_NETBSD_IDTYPES) +#if defined(HAVE_WAITID) && defined(HAVE_NETBSD_IDTYPES) REGISTER_LONG_CONSTANT("P_UID", LONG_CONST(P_UID), CONST_PERSISTENT); #endif -#if defined(HAVE_NETBSD_IDTYPES) +#if defined(HAVE_WAITID) && defined(HAVE_NETBSD_IDTYPES) REGISTER_LONG_CONSTANT("P_GID", LONG_CONST(P_GID), CONST_PERSISTENT); #endif -#if defined(HAVE_NETBSD_IDTYPES) +#if defined(HAVE_WAITID) && defined(HAVE_NETBSD_IDTYPES) REGISTER_LONG_CONSTANT("P_SID", LONG_CONST(P_SID), CONST_PERSISTENT); #endif -#if defined(HAVE_FREEBSD_IDTYPES) +#if defined(HAVE_WAITID) && defined(HAVE_FREEBSD_IDTYPES) REGISTER_LONG_CONSTANT("P_JAILID", LONG_CONST(P_JAILID), CONST_PERSISTENT); #endif REGISTER_LONG_CONSTANT("SIG_IGN", LONG_CONST(SIG_IGN), CONST_PERSISTENT); diff --git a/ext/pcntl/tests/pcntl_waitid.phpt b/ext/pcntl/tests/pcntl_waitid.phpt index 2889e0cfeef01..3862ac1c82cb4 100644 --- a/ext/pcntl/tests/pcntl_waitid.phpt +++ b/ext/pcntl/tests/pcntl_waitid.phpt @@ -3,6 +3,10 @@ pcntl_waitid() --EXTENSIONS-- pcntl posix +--SKIPIF-- + --FILE-- Date: Thu, 20 Jun 2024 20:30:23 +0200 Subject: [PATCH 16/44] Test pcntl_waitid failing due to invalid arguments --- ext/pcntl/tests/pcntl_waitid.phpt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/ext/pcntl/tests/pcntl_waitid.phpt b/ext/pcntl/tests/pcntl_waitid.phpt index 3862ac1c82cb4..c89728f83f500 100644 --- a/ext/pcntl/tests/pcntl_waitid.phpt +++ b/ext/pcntl/tests/pcntl_waitid.phpt @@ -13,12 +13,17 @@ $pid = pcntl_fork(); if ($pid == -1) { die("failed"); } else if ($pid) { - $status = 0; + // test invalid arguments + var_dump(@pcntl_waitid(6, $pid, $siginfo, WSTOPPED)); + var_dump(@pcntl_waitid(P_PID, $pid, $siginfo, WNOHANG)); + $result = pcntl_waitid(P_PID, $pid, $siginfo, WSTOPPED); var_dump($result); + posix_kill($pid, SIGCONT); $result = pcntl_waitid(P_PID, $pid, $siginfo, WCONTINUED); var_dump($result); + $result = pcntl_waitid(P_PID, $pid, $siginfo, WEXITED); var_dump($result); var_dump($siginfo["status"]); @@ -28,6 +33,8 @@ if ($pid == -1) { } ?> --EXPECT-- +bool(false) +bool(false) bool(true) bool(true) bool(true) From 324b79c9d5d637e565ca83f700d56b3c7e8ac52a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Thu, 20 Jun 2024 20:43:22 +0200 Subject: [PATCH 17/44] Pick a better bad value for idtype in test --- ext/pcntl/tests/pcntl_waitid.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/pcntl/tests/pcntl_waitid.phpt b/ext/pcntl/tests/pcntl_waitid.phpt index c89728f83f500..e2ff9061704f4 100644 --- a/ext/pcntl/tests/pcntl_waitid.phpt +++ b/ext/pcntl/tests/pcntl_waitid.phpt @@ -14,7 +14,7 @@ if ($pid == -1) { die("failed"); } else if ($pid) { // test invalid arguments - var_dump(@pcntl_waitid(6, $pid, $siginfo, WSTOPPED)); + var_dump(@pcntl_waitid(-2, $pid, $siginfo, WSTOPPED)); var_dump(@pcntl_waitid(P_PID, $pid, $siginfo, WNOHANG)); $result = pcntl_waitid(P_PID, $pid, $siginfo, WSTOPPED); From ab07bc02eb7bae4434ca93f4cf033f9cd60bd3e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Thu, 20 Jun 2024 21:02:25 +0200 Subject: [PATCH 18/44] Removed unnecessary comments --- ext/pcntl/pcntl.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index 8a7dbb8e1777d..1ffa8236abc4f 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -408,7 +408,6 @@ PHP_FUNCTION(pcntl_waitid) { zend_long idtype = P_PID; zend_long id = 0; - /* Optional by-ref array of ints */ zval *user_siginfo = NULL; zend_long options = WEXITED; int success; @@ -438,7 +437,6 @@ PHP_FUNCTION(pcntl_waitid) RETURN_TRUE; } #endif -/* }}} */ /* {{{ Waits on or returns the status of a forked child as defined by the waitpid() system call */ PHP_FUNCTION(pcntl_wait) From f01aeb3538fdb4b82c37cd330a384ff36a2b740d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Thu, 20 Jun 2024 21:05:05 +0200 Subject: [PATCH 19/44] Comment on NetBSD specific idtypes --- ext/pcntl/pcntl.stub.php | 3 ++- ext/pcntl/pcntl_arginfo.h | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/ext/pcntl/pcntl.stub.php b/ext/pcntl/pcntl.stub.php index 8a4a698612567..791c29f4bccc7 100644 --- a/ext/pcntl/pcntl.stub.php +++ b/ext/pcntl/pcntl.stub.php @@ -74,7 +74,7 @@ */ const P_PIDFD = UNKNOWN; #endif -/* FreeBSD specific idtypes */ +/* NetBSD specific idtypes */ #ifdef HAVE_NETBSD_IDTYPES /** * @var int @@ -92,6 +92,7 @@ */ const P_SID = UNKNOWN; #endif +/* FreeBSD specific idtype */ #ifdef HAVE_FREEBSD_IDTYPES /** * @var int diff --git a/ext/pcntl/pcntl_arginfo.h b/ext/pcntl/pcntl_arginfo.h index cb2acf035d89b..91402e9eb6f36 100644 --- a/ext/pcntl/pcntl_arginfo.h +++ b/ext/pcntl/pcntl_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 375360e87a060f9a3171db76fc652a2e95f3c86c */ + * Stub hash: 90e285cc6490be4843029862633a0cb1afd99102 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_fork, 0, 0, IS_LONG, 0) ZEND_END_ARG_INFO() From 52fce3d611e664919846a5894ddb3705a8ece5f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Fri, 21 Jun 2024 14:21:18 +0200 Subject: [PATCH 20/44] Renamed variable success to status --- ext/pcntl/pcntl.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index 1ffa8236abc4f..c78e9a1b3930b 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -410,7 +410,6 @@ PHP_FUNCTION(pcntl_waitid) zend_long id = 0; zval *user_siginfo = NULL; zend_long options = WEXITED; - int success; ZEND_PARSE_PARAMETERS_START(2, 4) Z_PARAM_LONG(idtype) @@ -423,9 +422,9 @@ PHP_FUNCTION(pcntl_waitid) errno = 0; siginfo_t siginfo; - success = waitid((idtype_t) idtype, (id_t) id, &siginfo, (int) options); + int status = waitid((idtype_t) idtype, (id_t) id, &siginfo, (int) options); - if (success == -1) { + if (status == -1) { PCNTL_G(last_error) = errno; php_error_docref(NULL, E_WARNING, "%s", strerror(errno)); RETURN_FALSE; @@ -1738,7 +1737,7 @@ PHP_FUNCTION(pcntl_setcpuaffinity) zend_argument_value_error(2, "cpu id must be between 0 and " ZEND_ULONG_FMT " (" ZEND_LONG_FMT ")", maxcpus, cpu); RETURN_THROWS(); } - + if (!PCNTL_CPU_ISSET(cpu, mask)) { PCNTL_CPU_SET(cpu, mask); } From d39033a5529eb26ef939bc307df277b8eb7f16f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Fri, 21 Jun 2024 15:24:09 +0200 Subject: [PATCH 21/44] Made the id param to waitid nullable --- ext/pcntl/pcntl.c | 3 ++- ext/pcntl/pcntl.stub.php | 2 +- ext/pcntl/pcntl_arginfo.h | 6 +++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index c78e9a1b3930b..512b3353c5a6c 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -408,12 +408,13 @@ PHP_FUNCTION(pcntl_waitid) { zend_long idtype = P_PID; zend_long id = 0; + bool id_is_null = 1; zval *user_siginfo = NULL; zend_long options = WEXITED; ZEND_PARSE_PARAMETERS_START(2, 4) Z_PARAM_LONG(idtype) - Z_PARAM_LONG(id) + Z_PARAM_LONG_OR_NULL(id, id_is_null) Z_PARAM_OPTIONAL Z_PARAM_ZVAL(user_siginfo) Z_PARAM_LONG(options) diff --git a/ext/pcntl/pcntl.stub.php b/ext/pcntl/pcntl.stub.php index 791c29f4bccc7..982446fda5d1c 100644 --- a/ext/pcntl/pcntl.stub.php +++ b/ext/pcntl/pcntl.stub.php @@ -1004,7 +1004,7 @@ function pcntl_fork(): int {} function pcntl_waitpid(int $process_id, &$status, int $flags = 0, &$resource_usage = []): int {} /** @param array $info */ -function pcntl_waitid(int $idtype, int $id, &$info = [], int $flags = 0): bool {} +function pcntl_waitid(int $idtype, ?int $id = null, &$info = [], int $flags = 0): bool {} /** * @param int $status diff --git a/ext/pcntl/pcntl_arginfo.h b/ext/pcntl/pcntl_arginfo.h index 91402e9eb6f36..9ac9182ca6db4 100644 --- a/ext/pcntl/pcntl_arginfo.h +++ b/ext/pcntl/pcntl_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 90e285cc6490be4843029862633a0cb1afd99102 */ + * Stub hash: 697e1874dc5a6ce4763edeb50d9db7cc2513bcef */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_fork, 0, 0, IS_LONG, 0) ZEND_END_ARG_INFO() @@ -11,9 +11,9 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_waitpid, 0, 2, IS_LONG, 0) ZEND_ARG_INFO_WITH_DEFAULT_VALUE(1, resource_usage, "[]") ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_waitid, 0, 2, _IS_BOOL, 0) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_waitid, 0, 1, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, idtype, IS_LONG, 0) - ZEND_ARG_TYPE_INFO(0, id, IS_LONG, 0) + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, id, IS_LONG, 1, "null") ZEND_ARG_INFO_WITH_DEFAULT_VALUE(1, info, "[]") ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "0") ZEND_END_ARG_INFO() From 5deaba16dd5cb0c5e7622330e065c2db9f47cd87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Fri, 21 Jun 2024 15:24:40 +0200 Subject: [PATCH 22/44] Expect warnings about invalid arguments --- ext/pcntl/tests/pcntl_waitid.phpt | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ext/pcntl/tests/pcntl_waitid.phpt b/ext/pcntl/tests/pcntl_waitid.phpt index e2ff9061704f4..750948639671c 100644 --- a/ext/pcntl/tests/pcntl_waitid.phpt +++ b/ext/pcntl/tests/pcntl_waitid.phpt @@ -14,8 +14,8 @@ if ($pid == -1) { die("failed"); } else if ($pid) { // test invalid arguments - var_dump(@pcntl_waitid(-2, $pid, $siginfo, WSTOPPED)); - var_dump(@pcntl_waitid(P_PID, $pid, $siginfo, WNOHANG)); + var_dump(pcntl_waitid(-2, $pid, $siginfo, WSTOPPED)); + var_dump(pcntl_waitid(P_PID, $pid, $siginfo, WNOHANG)); $result = pcntl_waitid(P_PID, $pid, $siginfo, WSTOPPED); var_dump($result); @@ -32,8 +32,11 @@ if ($pid == -1) { exit(42); } ?> ---EXPECT-- +--EXPECTF-- +Warning: pcntl_waitid(): Invalid argument in %s on line 7 bool(false) + +Warning: pcntl_waitid(): Invalid argument in %s on line 8 bool(false) bool(true) bool(true) From e1399cf41946fce50e572e1470d8227a0b359688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Fri, 21 Jun 2024 15:39:24 +0200 Subject: [PATCH 23/44] More tests, including testing error messages --- ext/pcntl/tests/pcntl_waitid.phpt | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/ext/pcntl/tests/pcntl_waitid.phpt b/ext/pcntl/tests/pcntl_waitid.phpt index 750948639671c..972ce6aa17d8c 100644 --- a/ext/pcntl/tests/pcntl_waitid.phpt +++ b/ext/pcntl/tests/pcntl_waitid.phpt @@ -13,10 +13,18 @@ $pid = pcntl_fork(); if ($pid == -1) { die("failed"); } else if ($pid) { - // test invalid arguments - var_dump(pcntl_waitid(-2, $pid, $siginfo, WSTOPPED)); + // invalid idtype + var_dump(pcntl_waitid(-42, $pid, $siginfo, WSTOPPED)); + // invalid flags + var_dump(pcntl_waitid(P_PID, $pid, $siginfo, -42)); + // need at least one of WEXITED, WSTOPPED, WCONTINUED flagd var_dump(pcntl_waitid(P_PID, $pid, $siginfo, WNOHANG)); + // with WNOHANG, call succeeds but there is no PID state change + $result = pcntl_waitid(P_PID, $pid, $siginfo, WNOHANG | WEXITED); + var_dump($result); + var_dump($siginfo["pid"]); + $result = pcntl_waitid(P_PID, $pid, $siginfo, WSTOPPED); var_dump($result); @@ -36,8 +44,13 @@ if ($pid == -1) { Warning: pcntl_waitid(): Invalid argument in %s on line 7 bool(false) -Warning: pcntl_waitid(): Invalid argument in %s on line 8 +Warning: pcntl_waitid(): Invalid argument in %s on line 9 bool(false) + +Warning: pcntl_waitid(): Invalid argument in %s on line 11 +bool(false) +bool(true) +int(0) bool(true) bool(true) bool(true) From d173be10b99800ce80e56c4485cbe3e641992138 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Fri, 21 Jun 2024 16:11:33 +0200 Subject: [PATCH 24/44] Implemented weak idtype validation --- ext/pcntl/pcntl.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index 512b3353c5a6c..7b3cf063c9739 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -420,6 +420,11 @@ PHP_FUNCTION(pcntl_waitid) Z_PARAM_LONG(options) ZEND_PARSE_PARAMETERS_END(); + if (idtype < -1) { + zend_argument_value_error(1, "must be either one of P_ALL, P_PID, P_PGID (POSIX) or a platform-specific value"); + RETURN_THROWS(); + } + errno = 0; siginfo_t siginfo; From 5793dcdd3fe14ad3dd121c448235e1ae28ff56d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Fri, 21 Jun 2024 16:42:58 +0200 Subject: [PATCH 25/44] Added test for weak idtype validation --- ext/pcntl/tests/pcntl_waitid.phpt | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/ext/pcntl/tests/pcntl_waitid.phpt b/ext/pcntl/tests/pcntl_waitid.phpt index 972ce6aa17d8c..8032f66fe2ef3 100644 --- a/ext/pcntl/tests/pcntl_waitid.phpt +++ b/ext/pcntl/tests/pcntl_waitid.phpt @@ -14,7 +14,11 @@ if ($pid == -1) { die("failed"); } else if ($pid) { // invalid idtype - var_dump(pcntl_waitid(-42, $pid, $siginfo, WSTOPPED)); + try { + var_dump(pcntl_waitid(-42, $pid, $siginfo, WSTOPPED)); + } catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; + } // invalid flags var_dump(pcntl_waitid(P_PID, $pid, $siginfo, -42)); // need at least one of WEXITED, WSTOPPED, WCONTINUED flagd @@ -41,13 +45,12 @@ if ($pid == -1) { } ?> --EXPECTF-- -Warning: pcntl_waitid(): Invalid argument in %s on line 7 -bool(false) +pcntl_waitid(): Argument #1 ($idtype) must be either one of P_ALL, P_PID, P_PGID (POSIX) or a platform-specific value -Warning: pcntl_waitid(): Invalid argument in %s on line 9 +Warning: pcntl_waitid(): FOO Invalid argument FOO in /home/random/github/php-src/ext/pcntl/tests/pcntl_waitid.php on line 13 bool(false) -Warning: pcntl_waitid(): Invalid argument in %s on line 11 +Warning: pcntl_waitid(): FOO Invalid argument FOO in /home/random/github/php-src/ext/pcntl/tests/pcntl_waitid.php on line 15 bool(false) bool(true) int(0) From 509c92f1f2377d120d41ace876eda3808c0cb05b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Fri, 21 Jun 2024 16:50:21 +0200 Subject: [PATCH 26/44] Added tests with PHP_INT_MAX --- ext/pcntl/tests/pcntl_waitid.phpt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/ext/pcntl/tests/pcntl_waitid.phpt b/ext/pcntl/tests/pcntl_waitid.phpt index 8032f66fe2ef3..d6305224b044e 100644 --- a/ext/pcntl/tests/pcntl_waitid.phpt +++ b/ext/pcntl/tests/pcntl_waitid.phpt @@ -19,13 +19,15 @@ if ($pid == -1) { } catch (\ValueError $e) { echo $e->getMessage() . \PHP_EOL; } + var_dump(pcntl_waitid(PHP_INT_MAX, $pid, $siginfo, WSTOPPED)); // invalid flags var_dump(pcntl_waitid(P_PID, $pid, $siginfo, -42)); + var_dump(pcntl_waitid(P_PID, $pid, $siginfo, PHP_INT_MAX)); // need at least one of WEXITED, WSTOPPED, WCONTINUED flagd var_dump(pcntl_waitid(P_PID, $pid, $siginfo, WNOHANG)); // with WNOHANG, call succeeds but there is no PID state change - $result = pcntl_waitid(P_PID, $pid, $siginfo, WNOHANG | WEXITED); + $result = pcntl_waitid(P_PID, $pid, $siginfo, WEXITED | WNOHANG); var_dump($result); var_dump($siginfo["pid"]); @@ -47,11 +49,17 @@ if ($pid == -1) { --EXPECTF-- pcntl_waitid(): Argument #1 ($idtype) must be either one of P_ALL, P_PID, P_PGID (POSIX) or a platform-specific value -Warning: pcntl_waitid(): FOO Invalid argument FOO in /home/random/github/php-src/ext/pcntl/tests/pcntl_waitid.php on line 13 +Warning: pcntl_waitid(): FOO Invalid argument FOO in /home/random/github/php-src/ext/pcntl/tests/pcntl_waitid.php on line 12 +bool(false) + +Warning: pcntl_waitid(): FOO Invalid argument FOO in /home/random/github/php-src/ext/pcntl/tests/pcntl_waitid.php on line 14 bool(false) Warning: pcntl_waitid(): FOO Invalid argument FOO in /home/random/github/php-src/ext/pcntl/tests/pcntl_waitid.php on line 15 bool(false) + +Warning: pcntl_waitid(): FOO Invalid argument FOO in /home/random/github/php-src/ext/pcntl/tests/pcntl_waitid.php on line 17 +bool(false) bool(true) int(0) bool(true) From 31f385685329f48f1c65140e5dde9cffadd0e030 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Fri, 21 Jun 2024 17:42:19 +0200 Subject: [PATCH 27/44] Throw ValueError on errno == EINVAL --- ext/pcntl/pcntl.c | 9 +++---- ext/pcntl/tests/pcntl_waitid.phpt | 44 ++++++++++++++++++------------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index 7b3cf063c9739..6eb452800be55 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -420,17 +420,16 @@ PHP_FUNCTION(pcntl_waitid) Z_PARAM_LONG(options) ZEND_PARSE_PARAMETERS_END(); - if (idtype < -1) { - zend_argument_value_error(1, "must be either one of P_ALL, P_PID, P_PGID (POSIX) or a platform-specific value"); - RETURN_THROWS(); - } - errno = 0; siginfo_t siginfo; int status = waitid((idtype_t) idtype, (id_t) id, &siginfo, (int) options); if (status == -1) { + if (errno == EINVAL) { + zend_value_error("An invalid value was specified for options, or idtype and id specify an invalid set of processes"); + RETURN_THROWS(); + } PCNTL_G(last_error) = errno; php_error_docref(NULL, E_WARNING, "%s", strerror(errno)); RETURN_FALSE; diff --git a/ext/pcntl/tests/pcntl_waitid.phpt b/ext/pcntl/tests/pcntl_waitid.phpt index d6305224b044e..5673a75edd30e 100644 --- a/ext/pcntl/tests/pcntl_waitid.phpt +++ b/ext/pcntl/tests/pcntl_waitid.phpt @@ -15,16 +15,32 @@ if ($pid == -1) { } else if ($pid) { // invalid idtype try { - var_dump(pcntl_waitid(-42, $pid, $siginfo, WSTOPPED)); + pcntl_waitid(-42, $pid, $siginfo, WSTOPPED); + } catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; + } + try { + pcntl_waitid(PHP_INT_MAX, $pid, $siginfo, WSTOPPED); } catch (\ValueError $e) { echo $e->getMessage() . \PHP_EOL; } - var_dump(pcntl_waitid(PHP_INT_MAX, $pid, $siginfo, WSTOPPED)); // invalid flags - var_dump(pcntl_waitid(P_PID, $pid, $siginfo, -42)); - var_dump(pcntl_waitid(P_PID, $pid, $siginfo, PHP_INT_MAX)); + try { + pcntl_waitid(P_PID, $pid, $siginfo, -42); + } catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; + } + try { + pcntl_waitid(P_PID, $pid, $siginfo, PHP_INT_MAX); + } catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; + } // need at least one of WEXITED, WSTOPPED, WCONTINUED flagd - var_dump(pcntl_waitid(P_PID, $pid, $siginfo, WNOHANG)); + try { + pcntl_waitid(P_PID, $pid, $siginfo, WNOHANG); + } catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; + } // with WNOHANG, call succeeds but there is no PID state change $result = pcntl_waitid(P_PID, $pid, $siginfo, WEXITED | WNOHANG); @@ -47,19 +63,11 @@ if ($pid == -1) { } ?> --EXPECTF-- -pcntl_waitid(): Argument #1 ($idtype) must be either one of P_ALL, P_PID, P_PGID (POSIX) or a platform-specific value - -Warning: pcntl_waitid(): FOO Invalid argument FOO in /home/random/github/php-src/ext/pcntl/tests/pcntl_waitid.php on line 12 -bool(false) - -Warning: pcntl_waitid(): FOO Invalid argument FOO in /home/random/github/php-src/ext/pcntl/tests/pcntl_waitid.php on line 14 -bool(false) - -Warning: pcntl_waitid(): FOO Invalid argument FOO in /home/random/github/php-src/ext/pcntl/tests/pcntl_waitid.php on line 15 -bool(false) - -Warning: pcntl_waitid(): FOO Invalid argument FOO in /home/random/github/php-src/ext/pcntl/tests/pcntl_waitid.php on line 17 -bool(false) +An invalid value was specified for options, or idtype and id specify an invalid set of processes +An invalid value was specified for options, or idtype and id specify an invalid set of processes +An invalid value was specified for options, or idtype and id specify an invalid set of processes +An invalid value was specified for options, or idtype and id specify an invalid set of processes +An invalid value was specified for options, or idtype and id specify an invalid set of processes bool(true) int(0) bool(true) From a0df4666b863c9851070ef64e43c8ec03507c2cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Sat, 22 Jun 2024 12:36:21 +0200 Subject: [PATCH 28/44] Added test for null id when idtype != P_ALL --- ext/pcntl/tests/pcntl_waitid.phpt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ext/pcntl/tests/pcntl_waitid.phpt b/ext/pcntl/tests/pcntl_waitid.phpt index 5673a75edd30e..933ddfaaaa466 100644 --- a/ext/pcntl/tests/pcntl_waitid.phpt +++ b/ext/pcntl/tests/pcntl_waitid.phpt @@ -24,6 +24,12 @@ if ($pid == -1) { } catch (\ValueError $e) { echo $e->getMessage() . \PHP_EOL; } + // invalid combination of idtype and id + try { + pcntl_waitid(P_PID, null, $siginfo, WSTOPPED); + } catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; + } // invalid flags try { pcntl_waitid(P_PID, $pid, $siginfo, -42); @@ -68,6 +74,7 @@ An invalid value was specified for options, or idtype and id specify an invalid An invalid value was specified for options, or idtype and id specify an invalid set of processes An invalid value was specified for options, or idtype and id specify an invalid set of processes An invalid value was specified for options, or idtype and id specify an invalid set of processes +An invalid value was specified for options, or idtype and id specify an invalid set of processes bool(true) int(0) bool(true) From 8763548d0a0bc8a17055c1381a21d803c090d5b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Sun, 23 Jun 2024 14:33:22 +0200 Subject: [PATCH 29/44] Moved idtype constants detection to autoconf On some systems these constants are preprocessor defines, on others they are part of an enum --- ext/pcntl/config.m4 | 9 +++++++++ ext/pcntl/pcntl.c | 20 ++++++++++---------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/ext/pcntl/config.m4 b/ext/pcntl/config.m4 index c9fe1da136918..f6be4a15e8210 100644 --- a/ext/pcntl/config.m4 +++ b/ext/pcntl/config.m4 @@ -30,6 +30,15 @@ if test "$PHP_PCNTL" != "no"; then AC_CHECK_HEADER([linux/wait.h], [AC_DEFINE([HAVE_LINUX_WAIT_H], [1], [linux/wait.h found])]) + AC_CHECK_DECLS([P_ALL, P_PIDFD, P_UID, P_JAILID], , , + [ + #include + #if defined(HAVE_LINUX_WAIT_H) + # include + #endif + ] + ) + dnl if unsupported, -1 means automatically ENOSYS in this context AC_CACHE_CHECK([if sched_getcpu is supported], [php_cv_func_sched_getcpu], [AC_RUN_IFELSE([AC_LANG_SOURCE([ diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index 6eb452800be55..fb143613dfb5d 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -119,30 +119,30 @@ static zend_class_entry *QosClass_ce; #include #endif -#ifndef NSIG -# define NSIG 32 -#endif - -#define LONG_CONST(c) (zend_long) c - #ifdef HAVE_WAITID #if defined(HAVE_LINUX_WAIT_H) #include #endif -#if defined (P_ALL) +#if defined (HAVE_DECL_P_ALL) && HAVE_DECL_P_ALL == 1 #define HAVE_POSIX_IDTYPES 1 #endif -#if defined (P_PIDFD) +#if defined (HAVE_DECL_P_PIDFD) && HAVE_DECL_P_PIDFD == 1 #define HAVE_LINUX_IDTYPES 1 #endif -#if defined (P_UID) +#if defined (HAVE_DECL_P_UID) && HAVE_DECL_P_UID == 1 #define HAVE_NETBSD_IDTYPES 1 #endif -#if defined (P_JAILID) +#if defined (HAVE_DECL_P_JAILID) && HAVE_DECL_P_JAILID == 1 #define HAVE_FREEBSD_IDTYPES 1 #endif #endif +#ifndef NSIG +# define NSIG 32 +#endif + +#define LONG_CONST(c) (zend_long) c + #include "Zend/zend_enum.h" #include "Zend/zend_max_execution_timer.h" From 5021e889b56a56142de451fc518abd7fd68590a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Sun, 23 Jun 2024 18:37:30 +0200 Subject: [PATCH 30/44] Fixed typo in comment --- ext/pcntl/tests/pcntl_waitid.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/pcntl/tests/pcntl_waitid.phpt b/ext/pcntl/tests/pcntl_waitid.phpt index 933ddfaaaa466..45195b8fb7a24 100644 --- a/ext/pcntl/tests/pcntl_waitid.phpt +++ b/ext/pcntl/tests/pcntl_waitid.phpt @@ -41,7 +41,7 @@ if ($pid == -1) { } catch (\ValueError $e) { echo $e->getMessage() . \PHP_EOL; } - // need at least one of WEXITED, WSTOPPED, WCONTINUED flagd + // need at least one of WEXITED, WSTOPPED, WCONTINUED flags try { pcntl_waitid(P_PID, $pid, $siginfo, WNOHANG); } catch (\ValueError $e) { From 1bf8873489e0f80d2d090732da82c9dea2e2d2ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Sun, 23 Jun 2024 19:54:06 +0200 Subject: [PATCH 31/44] FreeBSD fixes --- ext/pcntl/config.m4 | 7 +++++- ext/pcntl/pcntl.c | 38 +++++++++++++++---------------- ext/pcntl/php_pcntl.h | 2 +- ext/pcntl/tests/pcntl_waitid.phpt | 20 ---------------- 4 files changed, 26 insertions(+), 41 deletions(-) diff --git a/ext/pcntl/config.m4 b/ext/pcntl/config.m4 index f6be4a15e8210..480a5dc4b80f2 100644 --- a/ext/pcntl/config.m4 +++ b/ext/pcntl/config.m4 @@ -30,7 +30,12 @@ if test "$PHP_PCNTL" != "no"; then AC_CHECK_HEADER([linux/wait.h], [AC_DEFINE([HAVE_LINUX_WAIT_H], [1], [linux/wait.h found])]) - AC_CHECK_DECLS([P_ALL, P_PIDFD, P_UID, P_JAILID], , , + AC_CHECK_FUNCS([WIFCONTINUED],, + [AC_CHECK_DECL([WIFCONTINUED], [AC_DEFINE([HAVE_WIFCONTINUED], [1])],, [ + #include + ])]) + + AC_CHECK_DECLS([WCONTINUED, P_ALL, P_PIDFD, P_UID, P_JAILID], , , [ #include #if defined(HAVE_LINUX_WAIT_H) diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index fb143613dfb5d..e8b2db6576846 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -29,7 +29,6 @@ #include "php.h" #include "ext/standard/info.h" -#include "php_pcntl.h" #include "php_signal.h" #include "php_ticks.h" #include "zend_fibers.h" @@ -40,6 +39,25 @@ #include #endif +#ifdef HAVE_WAITID +#if defined(HAVE_LINUX_WAIT_H) +#include +#endif +#if defined (HAVE_DECL_P_ALL) && HAVE_DECL_P_ALL == 1 +#define HAVE_POSIX_IDTYPES 1 +#endif +#if defined (HAVE_DECL_P_PIDFD) && HAVE_DECL_P_PIDFD == 1 +#define HAVE_LINUX_IDTYPES 1 +#endif +#if defined (HAVE_DECL_P_UID) && HAVE_DECL_P_UID == 1 +#define HAVE_NETBSD_IDTYPES 1 +#endif +#if defined (HAVE_DECL_P_JAILID) && HAVE_DECL_P_JAILID == 1 +#define HAVE_FREEBSD_IDTYPES 1 +#endif +#endif + +#include "php_pcntl.h" #include #if defined(HAVE_UNSHARE) || defined(HAVE_SCHED_SETAFFINITY) || defined(HAVE_SCHED_GETCPU) #include @@ -119,24 +137,6 @@ static zend_class_entry *QosClass_ce; #include #endif -#ifdef HAVE_WAITID -#if defined(HAVE_LINUX_WAIT_H) -#include -#endif -#if defined (HAVE_DECL_P_ALL) && HAVE_DECL_P_ALL == 1 -#define HAVE_POSIX_IDTYPES 1 -#endif -#if defined (HAVE_DECL_P_PIDFD) && HAVE_DECL_P_PIDFD == 1 -#define HAVE_LINUX_IDTYPES 1 -#endif -#if defined (HAVE_DECL_P_UID) && HAVE_DECL_P_UID == 1 -#define HAVE_NETBSD_IDTYPES 1 -#endif -#if defined (HAVE_DECL_P_JAILID) && HAVE_DECL_P_JAILID == 1 -#define HAVE_FREEBSD_IDTYPES 1 -#endif -#endif - #ifndef NSIG # define NSIG 32 #endif diff --git a/ext/pcntl/php_pcntl.h b/ext/pcntl/php_pcntl.h index d34771062be7b..f09ccadcd3cdb 100644 --- a/ext/pcntl/php_pcntl.h +++ b/ext/pcntl/php_pcntl.h @@ -17,7 +17,7 @@ #ifndef PHP_PCNTL_H #define PHP_PCNTL_H -#if defined(WCONTINUED) && defined(WIFCONTINUED) +#if defined(HAVE_DECL_WCONTINUED) && HAVE_DECL_WCONTINUED == 1 && defined(HAVE_WIFCONTINUED) && HAVE_WIFCONTINUED == 1 #define HAVE_WCONTINUED 1 #endif diff --git a/ext/pcntl/tests/pcntl_waitid.phpt b/ext/pcntl/tests/pcntl_waitid.phpt index 45195b8fb7a24..9c3ff704d7790 100644 --- a/ext/pcntl/tests/pcntl_waitid.phpt +++ b/ext/pcntl/tests/pcntl_waitid.phpt @@ -13,23 +13,6 @@ $pid = pcntl_fork(); if ($pid == -1) { die("failed"); } else if ($pid) { - // invalid idtype - try { - pcntl_waitid(-42, $pid, $siginfo, WSTOPPED); - } catch (\ValueError $e) { - echo $e->getMessage() . \PHP_EOL; - } - try { - pcntl_waitid(PHP_INT_MAX, $pid, $siginfo, WSTOPPED); - } catch (\ValueError $e) { - echo $e->getMessage() . \PHP_EOL; - } - // invalid combination of idtype and id - try { - pcntl_waitid(P_PID, null, $siginfo, WSTOPPED); - } catch (\ValueError $e) { - echo $e->getMessage() . \PHP_EOL; - } // invalid flags try { pcntl_waitid(P_PID, $pid, $siginfo, -42); @@ -72,9 +55,6 @@ if ($pid == -1) { An invalid value was specified for options, or idtype and id specify an invalid set of processes An invalid value was specified for options, or idtype and id specify an invalid set of processes An invalid value was specified for options, or idtype and id specify an invalid set of processes -An invalid value was specified for options, or idtype and id specify an invalid set of processes -An invalid value was specified for options, or idtype and id specify an invalid set of processes -An invalid value was specified for options, or idtype and id specify an invalid set of processes bool(true) int(0) bool(true) From 25fc4db383775ce59c7f74972d39dbeb647de254 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Mon, 24 Jun 2024 00:54:00 +0200 Subject: [PATCH 32/44] macOS fills in siginfo_t even on no state change --- ext/pcntl/tests/pcntl_waitid.phpt | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/ext/pcntl/tests/pcntl_waitid.phpt b/ext/pcntl/tests/pcntl_waitid.phpt index 9c3ff704d7790..5f23910cd178a 100644 --- a/ext/pcntl/tests/pcntl_waitid.phpt +++ b/ext/pcntl/tests/pcntl_waitid.phpt @@ -19,22 +19,12 @@ if ($pid == -1) { } catch (\ValueError $e) { echo $e->getMessage() . \PHP_EOL; } + try { pcntl_waitid(P_PID, $pid, $siginfo, PHP_INT_MAX); } catch (\ValueError $e) { echo $e->getMessage() . \PHP_EOL; } - // need at least one of WEXITED, WSTOPPED, WCONTINUED flags - try { - pcntl_waitid(P_PID, $pid, $siginfo, WNOHANG); - } catch (\ValueError $e) { - echo $e->getMessage() . \PHP_EOL; - } - - // with WNOHANG, call succeeds but there is no PID state change - $result = pcntl_waitid(P_PID, $pid, $siginfo, WEXITED | WNOHANG); - var_dump($result); - var_dump($siginfo["pid"]); $result = pcntl_waitid(P_PID, $pid, $siginfo, WSTOPPED); var_dump($result); @@ -54,9 +44,6 @@ if ($pid == -1) { --EXPECTF-- An invalid value was specified for options, or idtype and id specify an invalid set of processes An invalid value was specified for options, or idtype and id specify an invalid set of processes -An invalid value was specified for options, or idtype and id specify an invalid set of processes -bool(true) -int(0) bool(true) bool(true) bool(true) From 2df267a946bd9eb0951580fbbb1125746857d42c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Mon, 24 Jun 2024 01:22:14 +0200 Subject: [PATCH 33/44] Changed API to closely match wait and waitpid --- ext/pcntl/pcntl.c | 6 ------ ext/pcntl/tests/pcntl_waitid.phpt | 26 +++++--------------------- 2 files changed, 5 insertions(+), 27 deletions(-) diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index e8b2db6576846..819b80e930222 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -426,16 +426,10 @@ PHP_FUNCTION(pcntl_waitid) int status = waitid((idtype_t) idtype, (id_t) id, &siginfo, (int) options); if (status == -1) { - if (errno == EINVAL) { - zend_value_error("An invalid value was specified for options, or idtype and id specify an invalid set of processes"); - RETURN_THROWS(); - } PCNTL_G(last_error) = errno; - php_error_docref(NULL, E_WARNING, "%s", strerror(errno)); RETURN_FALSE; } - // TODO verify that passing SIGCHLD does what we need pcntl_siginfo_to_zval(SIGCHLD, &siginfo, user_siginfo); RETURN_TRUE; diff --git a/ext/pcntl/tests/pcntl_waitid.phpt b/ext/pcntl/tests/pcntl_waitid.phpt index 5f23910cd178a..f1d9e2f80f285 100644 --- a/ext/pcntl/tests/pcntl_waitid.phpt +++ b/ext/pcntl/tests/pcntl_waitid.phpt @@ -14,27 +14,12 @@ if ($pid == -1) { die("failed"); } else if ($pid) { // invalid flags - try { - pcntl_waitid(P_PID, $pid, $siginfo, -42); - } catch (\ValueError $e) { - echo $e->getMessage() . \PHP_EOL; - } - - try { - pcntl_waitid(P_PID, $pid, $siginfo, PHP_INT_MAX); - } catch (\ValueError $e) { - echo $e->getMessage() . \PHP_EOL; - } - - $result = pcntl_waitid(P_PID, $pid, $siginfo, WSTOPPED); - var_dump($result); + var_dump(pcntl_waitid(P_PID, $pid, $siginfo, 8192)); + var_dump(pcntl_waitid(P_PID, $pid, $siginfo, WSTOPPED)); posix_kill($pid, SIGCONT); - $result = pcntl_waitid(P_PID, $pid, $siginfo, WCONTINUED); - var_dump($result); - - $result = pcntl_waitid(P_PID, $pid, $siginfo, WEXITED); - var_dump($result); + var_dump(pcntl_waitid(P_PID, $pid, $siginfo, WCONTINUED)); + var_dump(pcntl_waitid(P_PID, $pid, $siginfo, WEXITED)); var_dump($siginfo["status"]); } else { posix_kill(posix_getpid(), SIGSTOP); @@ -42,8 +27,7 @@ if ($pid == -1) { } ?> --EXPECTF-- -An invalid value was specified for options, or idtype and id specify an invalid set of processes -An invalid value was specified for options, or idtype and id specify an invalid set of processes +bool(false) bool(true) bool(true) bool(true) From 9124b1e5dcb96d55162ea805fc4e800aa86bcd61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Mon, 24 Jun 2024 01:48:32 +0200 Subject: [PATCH 34/44] AC_CHECK_DECLS for WEXITED, WSTOPPED, WNOWAIT --- ext/pcntl/config.m4 | 3 ++- ext/pcntl/pcntl.stub.php | 6 +++--- ext/pcntl/pcntl_arginfo.h | 8 ++++---- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/ext/pcntl/config.m4 b/ext/pcntl/config.m4 index 480a5dc4b80f2..3e4ca4b72b78d 100644 --- a/ext/pcntl/config.m4 +++ b/ext/pcntl/config.m4 @@ -35,7 +35,8 @@ if test "$PHP_PCNTL" != "no"; then #include ])]) - AC_CHECK_DECLS([WCONTINUED, P_ALL, P_PIDFD, P_UID, P_JAILID], , , + AC_CHECK_DECLS([WCONTINUED, WEXITED, WSTOPPED, WNOWAIT, + P_ALL, P_PIDFD, P_UID, P_JAILID],,, [ #include #if defined(HAVE_LINUX_WAIT_H) diff --git a/ext/pcntl/pcntl.stub.php b/ext/pcntl/pcntl.stub.php index 982446fda5d1c..0821ffc3f423a 100644 --- a/ext/pcntl/pcntl.stub.php +++ b/ext/pcntl/pcntl.stub.php @@ -25,21 +25,21 @@ */ const WCONTINUED = UNKNOWN; #endif -#ifdef WEXITED +#if defined (HAVE_DECL_WEXITED) && HAVE_DECL_WEXITED == 1 /** * @var int * @cvalue LONG_CONST(WEXITED) */ const WEXITED = UNKNOWN; #endif -#ifdef WSTOPPED +#if defined (HAVE_DECL_WSTOPPED) && HAVE_DECL_WSTOPPED == 1 /** * @var int * @cvalue LONG_CONST(WSTOPPED) */ const WSTOPPED = UNKNOWN; #endif -#ifdef WNOWAIT +#if defined (HAVE_DECL_WNOWAIT) && HAVE_DECL_WNOWAIT== 1 /** * @var int * @cvalue LONG_CONST(WNOWAIT) diff --git a/ext/pcntl/pcntl_arginfo.h b/ext/pcntl/pcntl_arginfo.h index 9ac9182ca6db4..ad23062c122e0 100644 --- a/ext/pcntl/pcntl_arginfo.h +++ b/ext/pcntl/pcntl_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 697e1874dc5a6ce4763edeb50d9db7cc2513bcef */ + * Stub hash: be81ffed1720914c3f9d17f263cbcc78272a9e15 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_fork, 0, 0, IS_LONG, 0) ZEND_END_ARG_INFO() @@ -324,13 +324,13 @@ static void register_pcntl_symbols(int module_number) #if defined(HAVE_WCONTINUED) REGISTER_LONG_CONSTANT("WCONTINUED", LONG_CONST(WCONTINUED), CONST_PERSISTENT); #endif -#if defined(WEXITED) +#if defined (HAVE_DECL_WEXITED) && HAVE_DECL_WEXITED == 1 REGISTER_LONG_CONSTANT("WEXITED", LONG_CONST(WEXITED), CONST_PERSISTENT); #endif -#if defined(WSTOPPED) +#if defined (HAVE_DECL_WSTOPPED) && HAVE_DECL_WSTOPPED == 1 REGISTER_LONG_CONSTANT("WSTOPPED", LONG_CONST(WSTOPPED), CONST_PERSISTENT); #endif -#if defined(WNOWAIT) +#if defined (HAVE_DECL_WNOWAIT) && HAVE_DECL_WNOWAIT== 1 REGISTER_LONG_CONSTANT("WNOWAIT", LONG_CONST(WNOWAIT), CONST_PERSISTENT); #endif #if defined(HAVE_WAITID) && defined(HAVE_POSIX_IDTYPES) From 80a1da8868d4ab2021feac6801a98798ec6d2561 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Mon, 24 Jun 2024 11:25:09 +0200 Subject: [PATCH 35/44] Test pcntl_get_last_error() --- ext/pcntl/tests/pcntl_waitid.phpt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ext/pcntl/tests/pcntl_waitid.phpt b/ext/pcntl/tests/pcntl_waitid.phpt index f1d9e2f80f285..0d443eb86c7a2 100644 --- a/ext/pcntl/tests/pcntl_waitid.phpt +++ b/ext/pcntl/tests/pcntl_waitid.phpt @@ -14,7 +14,8 @@ if ($pid == -1) { die("failed"); } else if ($pid) { // invalid flags - var_dump(pcntl_waitid(P_PID, $pid, $siginfo, 8192)); + var_dump(pcntl_waitid(P_PID, $pid, $siginfo, 0)); + var_dump(pcntl_get_last_error() == PCNTL_EINVAL); var_dump(pcntl_waitid(P_PID, $pid, $siginfo, WSTOPPED)); posix_kill($pid, SIGCONT); @@ -31,4 +32,5 @@ bool(false) bool(true) bool(true) bool(true) +bool(true) int(42) From ea3312205829d584e58affe58552141eb6d257ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Mon, 24 Jun 2024 18:48:31 +0200 Subject: [PATCH 36/44] AC_CHECK_DECLS superseded importing linux/wait.h --- ext/pcntl/config.m4 | 6 ------ ext/pcntl/pcntl.c | 3 --- 2 files changed, 9 deletions(-) diff --git a/ext/pcntl/config.m4 b/ext/pcntl/config.m4 index 3e4ca4b72b78d..f905c3f8540af 100644 --- a/ext/pcntl/config.m4 +++ b/ext/pcntl/config.m4 @@ -27,9 +27,6 @@ if test "$PHP_PCNTL" != "no"; then waitid ])) - AC_CHECK_HEADER([linux/wait.h], - [AC_DEFINE([HAVE_LINUX_WAIT_H], [1], [linux/wait.h found])]) - AC_CHECK_FUNCS([WIFCONTINUED],, [AC_CHECK_DECL([WIFCONTINUED], [AC_DEFINE([HAVE_WIFCONTINUED], [1])],, [ #include @@ -39,9 +36,6 @@ if test "$PHP_PCNTL" != "no"; then P_ALL, P_PIDFD, P_UID, P_JAILID],,, [ #include - #if defined(HAVE_LINUX_WAIT_H) - # include - #endif ] ) diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index 819b80e930222..c8111233947f6 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -40,9 +40,6 @@ #endif #ifdef HAVE_WAITID -#if defined(HAVE_LINUX_WAIT_H) -#include -#endif #if defined (HAVE_DECL_P_ALL) && HAVE_DECL_P_ALL == 1 #define HAVE_POSIX_IDTYPES 1 #endif From 5f540d8057dcf883ba8a709d84af0c5004571ca6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Mon, 24 Jun 2024 18:58:51 +0200 Subject: [PATCH 37/44] Improved m4 code intentation --- ext/pcntl/config.m4 | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/ext/pcntl/config.m4 b/ext/pcntl/config.m4 index f905c3f8540af..df6df0da12849 100644 --- a/ext/pcntl/config.m4 +++ b/ext/pcntl/config.m4 @@ -28,17 +28,15 @@ if test "$PHP_PCNTL" != "no"; then ])) AC_CHECK_FUNCS([WIFCONTINUED],, - [AC_CHECK_DECL([WIFCONTINUED], [AC_DEFINE([HAVE_WIFCONTINUED], [1])],, [ + [AC_CHECK_DECL([WIFCONTINUED], [AC_DEFINE([HAVE_WIFCONTINUED], [1])],,[ #include - ])]) - - AC_CHECK_DECLS([WCONTINUED, WEXITED, WSTOPPED, WNOWAIT, - P_ALL, P_PIDFD, P_UID, P_JAILID],,, - [ - #include - ] + ])] ) + AC_CHECK_DECLS([WCONTINUED, WEXITED, WSTOPPED, WNOWAIT, P_ALL, P_PIDFD, P_UID, P_JAILID],,,[ + #include + ]) + dnl if unsupported, -1 means automatically ENOSYS in this context AC_CACHE_CHECK([if sched_getcpu is supported], [php_cv_func_sched_getcpu], [AC_RUN_IFELSE([AC_LANG_SOURCE([ From 595b9c44189e77a762090bfad5e39bd43f010081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Tue, 25 Jun 2024 14:31:08 +0200 Subject: [PATCH 38/44] Better default params --- ext/pcntl/pcntl.c | 8 ++++---- ext/pcntl/pcntl.stub.php | 2 +- ext/pcntl/pcntl_arginfo.h | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index c8111233947f6..2a8bba86ed0eb 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -403,16 +403,16 @@ PHP_FUNCTION(pcntl_waitpid) #ifdef HAVE_WAITID PHP_FUNCTION(pcntl_waitid) { - zend_long idtype = P_PID; - zend_long id = 0; + zend_long idtype = P_ALL; + zend_long id = NULL; bool id_is_null = 1; zval *user_siginfo = NULL; zend_long options = WEXITED; - ZEND_PARSE_PARAMETERS_START(2, 4) + ZEND_PARSE_PARAMETERS_START(0, 4) + Z_PARAM_OPTIONAL Z_PARAM_LONG(idtype) Z_PARAM_LONG_OR_NULL(id, id_is_null) - Z_PARAM_OPTIONAL Z_PARAM_ZVAL(user_siginfo) Z_PARAM_LONG(options) ZEND_PARSE_PARAMETERS_END(); diff --git a/ext/pcntl/pcntl.stub.php b/ext/pcntl/pcntl.stub.php index 0821ffc3f423a..52f486cae5748 100644 --- a/ext/pcntl/pcntl.stub.php +++ b/ext/pcntl/pcntl.stub.php @@ -1004,7 +1004,7 @@ function pcntl_fork(): int {} function pcntl_waitpid(int $process_id, &$status, int $flags = 0, &$resource_usage = []): int {} /** @param array $info */ -function pcntl_waitid(int $idtype, ?int $id = null, &$info = [], int $flags = 0): bool {} +function pcntl_waitid(int $idtype = P_ALL, ?int $id = null, &$info = [], int $flags = WEXITED): bool {} /** * @param int $status diff --git a/ext/pcntl/pcntl_arginfo.h b/ext/pcntl/pcntl_arginfo.h index ad23062c122e0..0a0ee475922c2 100644 --- a/ext/pcntl/pcntl_arginfo.h +++ b/ext/pcntl/pcntl_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: be81ffed1720914c3f9d17f263cbcc78272a9e15 */ + * Stub hash: 07bcc89fe7b83e396c483e0b9f27d8e4aa4935f0 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_fork, 0, 0, IS_LONG, 0) ZEND_END_ARG_INFO() @@ -11,11 +11,11 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_waitpid, 0, 2, IS_LONG, 0) ZEND_ARG_INFO_WITH_DEFAULT_VALUE(1, resource_usage, "[]") ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_waitid, 0, 1, _IS_BOOL, 0) - ZEND_ARG_TYPE_INFO(0, idtype, IS_LONG, 0) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_waitid, 0, 0, _IS_BOOL, 0) + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, idtype, IS_LONG, 0, "P_ALL") ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, id, IS_LONG, 1, "null") ZEND_ARG_INFO_WITH_DEFAULT_VALUE(1, info, "[]") - ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "0") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "WEXITED") ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_wait, 0, 1, IS_LONG, 0) From 00115419acc0285ca9778848464b626b3e4ecbaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Tue, 25 Jun 2024 14:57:18 +0200 Subject: [PATCH 39/44] Fixed bad value for zend long --- ext/pcntl/pcntl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index 2a8bba86ed0eb..dbbfeb15888a5 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -404,7 +404,7 @@ PHP_FUNCTION(pcntl_waitpid) PHP_FUNCTION(pcntl_waitid) { zend_long idtype = P_ALL; - zend_long id = NULL; + zend_long id = 0; bool id_is_null = 1; zval *user_siginfo = NULL; zend_long options = WEXITED; From 1993ec1c38ad3242c95397972020df517ebc0ac0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Tue, 25 Jun 2024 15:18:11 +0200 Subject: [PATCH 40/44] Stricter preprocessor guards --- ext/pcntl/pcntl.c | 2 +- ext/pcntl/pcntl.stub.php | 2 ++ ext/pcntl/pcntl_arginfo.h | 8 +++++++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index dbbfeb15888a5..f687fecf6c24f 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -400,7 +400,7 @@ PHP_FUNCTION(pcntl_waitpid) } /* }}} */ -#ifdef HAVE_WAITID +#if defined (HAVE_WAITID) && defined (HAVE_POSIX_IDTYPES) && defined (HAVE_DECL_WEXITED) && HAVE_DECL_WEXITED == 1 PHP_FUNCTION(pcntl_waitid) { zend_long idtype = P_ALL; diff --git a/ext/pcntl/pcntl.stub.php b/ext/pcntl/pcntl.stub.php index 52f486cae5748..7b72c00453966 100644 --- a/ext/pcntl/pcntl.stub.php +++ b/ext/pcntl/pcntl.stub.php @@ -1003,8 +1003,10 @@ function pcntl_fork(): int {} */ function pcntl_waitpid(int $process_id, &$status, int $flags = 0, &$resource_usage = []): int {} +#if defined (HAVE_WAITID) && defined (HAVE_POSIX_IDTYPES) && defined (HAVE_DECL_WEXITED) && HAVE_DECL_WEXITED == 1 /** @param array $info */ function pcntl_waitid(int $idtype = P_ALL, ?int $id = null, &$info = [], int $flags = WEXITED): bool {} +#endif /** * @param int $status diff --git a/ext/pcntl/pcntl_arginfo.h b/ext/pcntl/pcntl_arginfo.h index 0a0ee475922c2..c919bbc1007bc 100644 --- a/ext/pcntl/pcntl_arginfo.h +++ b/ext/pcntl/pcntl_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 07bcc89fe7b83e396c483e0b9f27d8e4aa4935f0 */ + * Stub hash: 71d0cbd8d2b1ae57d289ec421e9dcaa4040d857b */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_fork, 0, 0, IS_LONG, 0) ZEND_END_ARG_INFO() @@ -11,12 +11,14 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_waitpid, 0, 2, IS_LONG, 0) ZEND_ARG_INFO_WITH_DEFAULT_VALUE(1, resource_usage, "[]") ZEND_END_ARG_INFO() +#if defined (HAVE_WAITID) && defined (HAVE_POSIX_IDTYPES) && defined (HAVE_DECL_WEXITED) && HAVE_DECL_WEXITED == 1 ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_waitid, 0, 0, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, idtype, IS_LONG, 0, "P_ALL") ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, id, IS_LONG, 1, "null") ZEND_ARG_INFO_WITH_DEFAULT_VALUE(1, info, "[]") ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "WEXITED") ZEND_END_ARG_INFO() +#endif ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_wait, 0, 1, IS_LONG, 0) ZEND_ARG_INFO(1, status) @@ -177,7 +179,9 @@ ZEND_END_ARG_INFO() ZEND_FUNCTION(pcntl_fork); ZEND_FUNCTION(pcntl_waitpid); +#if defined (HAVE_WAITID) && defined (HAVE_POSIX_IDTYPES) && defined (HAVE_DECL_WEXITED) && HAVE_DECL_WEXITED == 1 ZEND_FUNCTION(pcntl_waitid); +#endif ZEND_FUNCTION(pcntl_wait); ZEND_FUNCTION(pcntl_signal); ZEND_FUNCTION(pcntl_signal_get_handler); @@ -242,7 +246,9 @@ ZEND_FUNCTION(pcntl_setqos_class); static const zend_function_entry ext_functions[] = { ZEND_FE(pcntl_fork, arginfo_pcntl_fork) ZEND_FE(pcntl_waitpid, arginfo_pcntl_waitpid) +#if defined (HAVE_WAITID) && defined (HAVE_POSIX_IDTYPES) && defined (HAVE_DECL_WEXITED) && HAVE_DECL_WEXITED == 1 ZEND_FE(pcntl_waitid, arginfo_pcntl_waitid) +#endif ZEND_FE(pcntl_wait, arginfo_pcntl_wait) ZEND_FE(pcntl_signal, arginfo_pcntl_signal) ZEND_FE(pcntl_signal_get_handler, arginfo_pcntl_signal_get_handler) From 19e061225563846e91734c07b23e8f9e2715b882 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Wed, 26 Jun 2024 02:39:59 +0200 Subject: [PATCH 41/44] Updated NEWS --- NEWS | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS b/NEWS index 6feb44d4b9b64..5416ffec8d7f7 100644 --- a/NEWS +++ b/NEWS @@ -164,6 +164,7 @@ PHP NEWS . Added pcntl_getqos_class/pcntl_setqos_class for macOs. (David Carlier) . Added SIGCKPT/SIGCKPTEXIT constants for DragonFlyBSD. (David Carlier) . Added FreeBSD's SIGTRAP handling to pcntl_siginfo_to_zval. (David Carlier) + . Added POSIX pcntl_waitid. (Vladimir Vrzić) - PCRE: . Upgrade bundled pcre2lib to version 10.43. (nielsdos) From 207ea1772115b10673db43dce59334aaadd47672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Thu, 27 Jun 2024 12:22:18 +0200 Subject: [PATCH 42/44] Fixed race condition in unit tests --- ext/pcntl/tests/pcntl_waitid.phpt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ext/pcntl/tests/pcntl_waitid.phpt b/ext/pcntl/tests/pcntl_waitid.phpt index 0d443eb86c7a2..a745176499b34 100644 --- a/ext/pcntl/tests/pcntl_waitid.phpt +++ b/ext/pcntl/tests/pcntl_waitid.phpt @@ -20,11 +20,16 @@ if ($pid == -1) { var_dump(pcntl_waitid(P_PID, $pid, $siginfo, WSTOPPED)); posix_kill($pid, SIGCONT); var_dump(pcntl_waitid(P_PID, $pid, $siginfo, WCONTINUED)); + posix_kill($pid, SIGUSR1); var_dump(pcntl_waitid(P_PID, $pid, $siginfo, WEXITED)); var_dump($siginfo["status"]); } else { + pcntl_signal(SIGUSR1, function (int $signo, $_siginfo) { exit(42); }); posix_kill(posix_getpid(), SIGSTOP); - exit(42); + pcntl_signal_dispatch(); + sleep(42); + pcntl_signal_dispatch(); + exit(6); } ?> --EXPECTF-- From e69f6c7836eb2409bb3523dbe65660e8b99dd15f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Thu, 27 Jun 2024 13:52:34 +0200 Subject: [PATCH 43/44] Marked unused param with underscore --- ext/pcntl/tests/pcntl_waitid.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/pcntl/tests/pcntl_waitid.phpt b/ext/pcntl/tests/pcntl_waitid.phpt index a745176499b34..b00f26f81fa2e 100644 --- a/ext/pcntl/tests/pcntl_waitid.phpt +++ b/ext/pcntl/tests/pcntl_waitid.phpt @@ -24,7 +24,7 @@ if ($pid == -1) { var_dump(pcntl_waitid(P_PID, $pid, $siginfo, WEXITED)); var_dump($siginfo["status"]); } else { - pcntl_signal(SIGUSR1, function (int $signo, $_siginfo) { exit(42); }); + pcntl_signal(SIGUSR1, function ($_signo, $_siginfo) { exit(42); }); posix_kill(posix_getpid(), SIGSTOP); pcntl_signal_dispatch(); sleep(42); From ed383972844ef0acc5084737b8245e1e70a3b72c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Thu, 27 Jun 2024 18:33:12 +0200 Subject: [PATCH 44/44] m4 formatting --- ext/pcntl/config.m4 | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/ext/pcntl/config.m4 b/ext/pcntl/config.m4 index df6df0da12849..c6a25171ec5c4 100644 --- a/ext/pcntl/config.m4 +++ b/ext/pcntl/config.m4 @@ -28,14 +28,11 @@ if test "$PHP_PCNTL" != "no"; then ])) AC_CHECK_FUNCS([WIFCONTINUED],, - [AC_CHECK_DECL([WIFCONTINUED], [AC_DEFINE([HAVE_WIFCONTINUED], [1])],,[ - #include - ])] - ) + [AC_CHECK_DECL([WIFCONTINUED], [AC_DEFINE([HAVE_WIFCONTINUED], [1])],, + [#include ])]) - AC_CHECK_DECLS([WCONTINUED, WEXITED, WSTOPPED, WNOWAIT, P_ALL, P_PIDFD, P_UID, P_JAILID],,,[ - #include - ]) + AC_CHECK_DECLS([WCONTINUED, WEXITED, WSTOPPED, WNOWAIT, P_ALL, P_PIDFD, P_UID, P_JAILID],,, + [#include ]) dnl if unsupported, -1 means automatically ENOSYS in this context AC_CACHE_CHECK([if sched_getcpu is supported], [php_cv_func_sched_getcpu],