Skip to content

Implement pcntl_waitid #14617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 44 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
1fe4de1
ext/pcntl: Added new function pcntl_waitid
vrza Jun 20, 2024
0483f22
Validate flags, fill siginfo array
vrza Jun 20, 2024
ecefc18
Expose needed constants from wait.h
vrza Jun 20, 2024
c8ff3b6
Add ifdef guards around WEXITED, WSTOPPED, WNOWAIT
vrza Jun 20, 2024
2dd62b9
Unit tests for pcntl_waitid
vrza Jun 20, 2024
0ccd92f
Throw a ValueError if parameter validation fails
vrza Jun 20, 2024
712b536
Evaluate in numerical order
vrza Jun 20, 2024
dfe3bfe
Guard Linux-specific P_PIDFD constant
vrza Jun 20, 2024
e23bc2d
Linux-specific iptype validation
vrza Jun 20, 2024
651df1f
Include linux/wait.h on Linux to check for idtype constants
vrza Jun 20, 2024
3ab725d
Leave idtype and flags validation out as it is platform-specific
vrza Jun 20, 2024
93f4fad
Added FreeBSD specific idtypes
vrza Jun 20, 2024
ee6b65d
Added NetBSD specific idtypes
vrza Jun 20, 2024
4830687
Added check for linux/wait.h
vrza Jun 20, 2024
e3cfd08
Guard pcntl_waitid behind HAVE_WAITID
vrza Jun 20, 2024
ba5d347
Test pcntl_waitid failing due to invalid arguments
vrza Jun 20, 2024
324b79c
Pick a better bad value for idtype in test
vrza Jun 20, 2024
ab07bc0
Removed unnecessary comments
vrza Jun 20, 2024
f01aeb3
Comment on NetBSD specific idtypes
vrza Jun 20, 2024
52fce3d
Renamed variable success to status
vrza Jun 21, 2024
d39033a
Made the id param to waitid nullable
vrza Jun 21, 2024
5deaba1
Expect warnings about invalid arguments
vrza Jun 21, 2024
e1399cf
More tests, including testing error messages
vrza Jun 21, 2024
d173be1
Implemented weak idtype validation
vrza Jun 21, 2024
5793dcd
Added test for weak idtype validation
vrza Jun 21, 2024
509c92f
Added tests with PHP_INT_MAX
vrza Jun 21, 2024
31f3856
Throw ValueError on errno == EINVAL
vrza Jun 21, 2024
a0df466
Added test for null id when idtype != P_ALL
vrza Jun 22, 2024
8763548
Moved idtype constants detection to autoconf
vrza Jun 23, 2024
5021e88
Fixed typo in comment
vrza Jun 23, 2024
1bf8873
FreeBSD fixes
vrza Jun 23, 2024
25fc4db
macOS fills in siginfo_t even on no state change
vrza Jun 23, 2024
2df267a
Changed API to closely match wait and waitpid
vrza Jun 23, 2024
9124b1e
AC_CHECK_DECLS for WEXITED, WSTOPPED, WNOWAIT
vrza Jun 23, 2024
80a1da8
Test pcntl_get_last_error()
vrza Jun 24, 2024
ea33122
AC_CHECK_DECLS superseded importing linux/wait.h
vrza Jun 24, 2024
5f540d8
Improved m4 code intentation
vrza Jun 24, 2024
595b9c4
Better default params
vrza Jun 25, 2024
0011541
Fixed bad value for zend long
vrza Jun 25, 2024
1993ec1
Stricter preprocessor guards
vrza Jun 25, 2024
19e0612
Updated NEWS
vrza Jun 26, 2024
207ea17
Fixed race condition in unit tests
vrza Jun 27, 2024
e69f6c7
Marked unused param with underscore
vrza Jun 27, 2024
ed38397
m4 formatting
vrza Jun 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/pcntl/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions ext/pcntl/pcntl.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,57 @@ 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;
/* Optional by-ref array of ints */
zval *user_siginfo = NULL;
zend_long options = WEXITED;
int success;

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();

// 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;
}

errno = 0;
siginfo_t siginfo;

success = waitid((idtype_t) idtype, (id_t) id, &siginfo, (int) options);

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;
}
/* }}} */

/* {{{ Waits on or returns the status of a forked child as defined by the waitpid() system call */
PHP_FUNCTION(pcntl_wait)
{
Expand Down
46 changes: 46 additions & 0 deletions ext/pcntl/pcntl.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,49 @@
*/
const WCONTINUED = UNKNOWN;
#endif
#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 */
/**
* @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 */

Expand Down Expand Up @@ -927,6 +970,9 @@ 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 {}

/**
* @param int $status
* @param array $resource_usage
Expand Down
24 changes: 23 additions & 1 deletion ext/pcntl/pcntl_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading