Skip to content

ext/posix: adding posix_mkfifoat/posix_mknodat. #13829

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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/posix/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if test "$PHP_POSIX" = "yes"; then
AC_DEFINE(HAVE_POSIX, 1, [whether to include POSIX-like functions])
PHP_NEW_EXTENSION(posix, posix.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)

AC_CHECK_FUNCS(seteuid setegid setsid getsid getpgid ctermid mkfifo mknod setrlimit getrlimit getgroups initgroups getgrgid_r eaccess)
AC_CHECK_FUNCS(seteuid setegid setsid getsid getpgid ctermid mkfifo mknod setrlimit getrlimit getgroups initgroups getgrgid_r eaccess mkfifoat mknodat)

dnl Check for makedev. If it's defined as a macro, AC_CHECK_FUNCS won't work.
dnl Required headers are included by the AC_HEADER_MAJOR logic.
Expand Down
98 changes: 98 additions & 0 deletions ext/posix/posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -1305,3 +1305,101 @@ PHP_FUNCTION(posix_fpathconf)
RETURN_LONG(ret);
}
#endif

#ifdef HAVE_MKFIFOAT
PHP_FUNCTION(posix_mkfifoat)
{
zend_string *path;
zend_long mode, fd = 0;
zval *z_fd;
int result;

ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_ZVAL(z_fd)
Z_PARAM_PATH_STR(path)
Z_PARAM_LONG(mode)
ZEND_PARSE_PARAMETERS_END();

if (Z_TYPE_P(z_fd) == IS_RESOURCE) {
if (!php_posix_stream_get_fd(z_fd, &fd)) {
RETURN_FALSE;
}
} else {
if (!zend_parse_arg_long(z_fd, &fd, /* is_null */ NULL, /* check_null */ false, /* arg_num */ 1)) {
zend_argument_type_error(1, "must be of type int|resource, %s given",
zend_zval_value_name(z_fd));
RETURN_THROWS();
}
}

if (php_check_open_basedir_ex(ZSTR_VAL(path), 0)) {
RETURN_FALSE;
}

result = mkfifoat(fd, ZSTR_VAL(path), mode);
if (result < 0) {
POSIX_G(last_error) = errno;
RETURN_FALSE;
}

RETURN_TRUE;
}
#endif

#ifdef HAVE_MKNODAT
PHP_FUNCTION(posix_mknodat)
{
zend_string *path;
zend_long mode, fd = 0;
zend_long major = 0, minor = 0;
zval *z_fd;
int result;
dev_t php_dev = 0;

ZEND_PARSE_PARAMETERS_START(3, 5)
Z_PARAM_ZVAL(z_fd)
Z_PARAM_PATH_STR(path)
Z_PARAM_LONG(mode)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(major)
Z_PARAM_LONG(minor)
ZEND_PARSE_PARAMETERS_END();

if (Z_TYPE_P(z_fd) == IS_RESOURCE) {
if (!php_posix_stream_get_fd(z_fd, &fd)) {
RETURN_FALSE;
}
} else {
if (!zend_parse_arg_long(z_fd, &fd, /* is_null */ NULL, /* check_null */ false, /* arg_num */ 1)) {
zend_argument_type_error(1, "must be of type int|resource, %s given",
zend_zval_value_name(z_fd));
RETURN_THROWS();
}
}

if (php_check_open_basedir_ex(ZSTR_VAL(path), 0)) {
RETURN_FALSE;
}

if ((mode & S_IFCHR) || (mode & S_IFBLK)) {
if (major == 0) {
zend_argument_value_error(4, "cannot be 0 for the POSIX_S_IFCHR and POSIX_S_IFBLK modes");
RETURN_THROWS();
} else {
#ifdef HAVE_MAKEDEV
php_dev = makedev(major, minor);
#else
php_error_docref(NULL, E_WARNING, "Cannot create a block or character device, creating a normal file instead");
#endif
}
}

result = mknodat(fd, ZSTR_VAL(path), mode, php_dev);
if (result < 0) {
POSIX_G(last_error) = errno;
RETURN_FALSE;
}

RETURN_TRUE;
}
#endif
10 changes: 10 additions & 0 deletions ext/posix/posix.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,3 +454,13 @@ function posix_pathconf(string $path, int $name): int|false {}
/** @param resource|int $file_descriptor */
function posix_fpathconf($file_descriptor, int $name): int|false {}
#endif

#ifdef HAVE_MKFIFOAT
/** @param resource|int $file_descriptor */
function posix_mkfifoat($file_descriptor, string $path, int $permissions): bool {}
#endif

#ifdef HAVE_MKNODAT
/** @param resource|int $file_descriptor */
function posix_mknodat($file_descriptor, string $path, int $flags, int $major = 0, int $minor = 0): bool {}
#endif
32 changes: 31 additions & 1 deletion ext/posix/posix_arginfo.h

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

44 changes: 44 additions & 0 deletions ext/posix/tests/posix_mknodat_mkfifoat.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--
posix_mknodat/posix_mkfifoat support
--EXTENSIONS--
posix
--SKIPIF--
<?php
// usually, mkfifoat is implemented with mknodat
if (!function_exists('posix_mknodat') || !function_exists('posix_mkfifoat')) die('skip posix_mknodat()/posix_mkfifoat not found');
if (getenv('TRAVIS')) die('skip Currently fails on Travis');
?>
--FILE--
<?php

$fd = fopen(__DIR__, "r");
var_dump(posix_mknodat($fd, 'dev', POSIX_S_IFIFO | 0666, 1, 0));
var_dump(posix_mknodat($fd, '', POSIX_S_IFBLK | 0777, 1, 0));
var_dump(posix_mknodat($fd, __DIR__ . '', POSIX_S_IFBLK | 0777, 1, 0));
try {
posix_mknodat($fd, __DIR__ . '/dev/', POSIX_S_IFBLK | 0777, 0, 0);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
var_dump(posix_mkfifoat($fd, '', POSIX_S_IFBLK | 0777));
var_dump(posix_mkfifoat($fd, __DIR__ . '', POSIX_S_IFBLK | 0777));
try {
posix_mkfifoat(new stdClass(), '/dev/', POSIX_S_IFBLK | 0777);
} catch (TypeError $e) {
echo $e->getMessage() . PHP_EOL;
}
@unlink(__DIR__ . '/dev');
var_dump(posix_mkfifoat($fd, 'dev', POSIX_S_IFIFO | 0666));
fclose($fd);
@unlink(__DIR__ . '/dev');
?>
--EXPECT--
bool(true)
bool(false)
bool(false)
posix_mknodat(): Argument #4 ($major) cannot be 0 for the POSIX_S_IFCHR and POSIX_S_IFBLK modes
bool(false)
bool(false)
posix_mkfifoat(): Argument #1 ($file_descriptor) must be of type int|resource, stdClass given
Comment on lines +37 to +42
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is pretty much just a failure test. Can you write some success test with appropriate skips?

bool(true)