Skip to content

ext/pcntl: adding pcntl_setns for Linux > 5.3 #13878

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 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -7,7 +7,7 @@ if test "$PHP_PCNTL" != "no"; then
AC_CHECK_FUNCS([fork], [], [AC_MSG_ERROR([pcntl: fork() not supported by this platform])])
AC_CHECK_FUNCS([waitpid], [], [AC_MSG_ERROR([pcntl: waitpid() not supported by this platform])])
AC_CHECK_FUNCS([sigaction], [], [AC_MSG_ERROR([pcntl: sigaction() not supported by this platform])])
AC_CHECK_FUNCS([getpriority setpriority wait3 wait4 sigwaitinfo sigtimedwait unshare rfork forkx])
AC_CHECK_FUNCS([getpriority setpriority wait3 wait4 sigwaitinfo sigtimedwait unshare rfork forkx pidfd_open])

AC_CHECK_TYPE([siginfo_t],[PCNTL_CFLAGS="-DHAVE_STRUCT_SIGINFO_T"],,[#include <signal.h>])

Expand Down
74 changes: 74 additions & 0 deletions ext/pcntl/pcntl.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
#include <sched.h>
#endif

#ifdef HAVE_PIDFD_OPEN
#include <sys/syscall.h>
#endif

#ifdef HAVE_FORKX
#include <sys/fork.h>
#endif
Expand Down Expand Up @@ -1402,6 +1406,76 @@ PHP_FUNCTION(pcntl_forkx)
#endif
/* }}} */

#ifdef HAVE_PIDFD_OPEN
// The `pidfd_open` syscall is available since 5.3
// and `setns` since 3.0.
PHP_FUNCTION(pcntl_setns)
{
zend_long pid, nstype = CLONE_NEWNET;
bool pid_is_null = 1;
int fd, ret;

ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_LONG_OR_NULL(pid, pid_is_null)
Z_PARAM_LONG(nstype)
ZEND_PARSE_PARAMETERS_END();

pid = pid_is_null ? getpid() : pid;
fd = syscall(SYS_pidfd_open, pid, 0);
if (errno) {
PCNTL_G(last_error) = errno;
switch (errno) {
case EINVAL:
case ESRCH:
zend_argument_value_error(1, "is not a valid process (%d)", pid);
RETURN_THROWS();

case ENFILE:
php_error_docref(NULL, E_WARNING, "Error %d: File descriptors per-process limit reached", errno);
break;

case ENODEV:
php_error_docref(NULL, E_WARNING, "Error %d: Anonymous inode fs unsupported", errno);
break;

case ENOMEM:
php_error_docref(NULL, E_WARNING, "Error %d: Insufficient memory for pidfd_open", errno);
break;

default:
php_error_docref(NULL, E_WARNING, "Error %d", errno);
}
RETURN_FALSE;
}
ret = setns(fd, (int)nstype);
close(fd);

if (ret == -1) {
PCNTL_G(last_error) = errno;
switch (errno) {
case ESRCH:
zend_argument_value_error(1, "process no longer available (%d)", pid);
RETURN_THROWS();

case EINVAL:
zend_argument_value_error(2, "is an invalid nstype (%d)", nstype);
RETURN_THROWS();

case EPERM:
php_error_docref(NULL, E_WARNING, "Error %d: No required capability for this process", errno);
break;

default:
php_error_docref(NULL, E_WARNING, "Error %d", errno);
}
RETURN_FALSE;
} else {
RETURN_TRUE;
}
}
#endif

static void pcntl_interrupt_function(zend_execute_data *execute_data)
{
pcntl_signal_dispatch();
Expand Down
4 changes: 4 additions & 0 deletions ext/pcntl/pcntl.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -990,3 +990,7 @@ function pcntl_rfork(int $flags, int $signal = 0): int{}
#ifdef HAVE_FORKX
function pcntl_forkx(int $flags): int{}
#endif

#ifdef HAVE_PIDFD_OPEN
function pcntl_setns(int $process_id = null, int $nstype = CLONE_NEWNET): bool {}
#endif
15 changes: 14 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.

24 changes: 24 additions & 0 deletions ext/pcntl/tests/pcntl_setns_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
pcntl_setns()
--EXTENSIONS--
pcntl
posix
--SKIPIF--
<?php
if (!function_exists("pcntl_setns")) die("skip pcntl_setns is not available");
if (getenv('SKIP_ASAN')) die('skip Timeouts under ASAN');
?>
--FILE--
<?php
$pid = pcntl_fork();
if ($pid == -1) die("pcntl_fork failed");
if ($pid != 0) {
try {
pcntl_setns($pid, 0);
} catch (\ValueError $e) {
echo $e->getMessage();
}
}
?>
--EXPECTF--
pcntl_setns(): Argument #2 ($nstype) is an invalid nstype (%d)
21 changes: 21 additions & 0 deletions ext/pcntl/tests/pcntl_setns_newpid.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
pcntl_setns()
--EXTENSIONS--
pcntl
posix
--SKIPIF--
<?php
if (!function_exists("pcntl_setns")) die("skip pcntl_setns is not available");
if (posix_getuid() !== 0) die('skip Test needs root user');
if (getenv('SKIP_ASAN')) die('skip Timeouts under ASAN');
?>
--FILE--
<?php
$pid = pcntl_fork();
if ($pid == -1) die("pcntl_fork failed");
if ($pid != 0) {
var_dump(pcntl_setns($pid, CLONE_NEWPID));
}
?>
--EXPECT--
bool(true)
Loading