Skip to content

Allow specifying resource in posix_getrlimit() for single result #9790

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
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
37 changes: 32 additions & 5 deletions ext/posix/posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -1056,16 +1056,43 @@ static const struct limitlist {
PHP_FUNCTION(posix_getrlimit)
{
const struct limitlist *l = NULL;
zend_long res;
bool res_is_null = true;

ZEND_PARSE_PARAMETERS_NONE();
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_LONG_OR_NULL(res, res_is_null)
ZEND_PARSE_PARAMETERS_END();

array_init(return_value);
if (res_is_null) {
array_init(return_value);

for (l=limits; l->name; l++) {
if (posix_addlimit(l->limit, l->name, return_value) == FAILURE) {
zend_array_destroy(Z_ARR_P(return_value));
for (l=limits; l->name; l++) {
if (posix_addlimit(l->limit, l->name, return_value) == FAILURE) {
zend_array_destroy(Z_ARR_P(return_value));
RETURN_FALSE;
}
}
} else {
struct rlimit rl;
int result = getrlimit(res, &rl);
if (result < 0) {
POSIX_G(last_error) = errno;
RETURN_FALSE;
}

array_init(return_value);
if (rl.rlim_cur == RLIM_INFINITY) {
add_next_index_stringl(return_value, UNLIMITED_STRING, sizeof(UNLIMITED_STRING)-1);
} else {
add_next_index_long(return_value, rl.rlim_cur);
}

if (rl.rlim_max == RLIM_INFINITY) {
add_next_index_stringl(return_value, UNLIMITED_STRING, sizeof(UNLIMITED_STRING)-1);
} else {
add_next_index_long(return_value, rl.rlim_max);
}
}
}
/* }}} */
Expand Down
2 changes: 1 addition & 1 deletion ext/posix/posix.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ function posix_getpwuid(int $user_id): array|false {}
* @return array<string, int|string>|false
* @refcount 1
*/
function posix_getrlimit(): array|false {}
function posix_getrlimit(?int $resource = null): array|false {}
#endif

#ifdef HAVE_SETRLIMIT
Expand Down
3 changes: 2 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.

28 changes: 28 additions & 0 deletions ext/posix/tests/posix_getrlimit_single_resource.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
posix_getrlimit() for single resource
--EXTENSIONS--
posix
--SKIPIF--
<?php
if (!function_exists('posix_getrlimit')) die('skip posix_getrlimit() not found');
if (!function_exists('posix_setrlimit') || !posix_setrlimit(POSIX_RLIMIT_CORE, 2048, -1)) {
die('skip Failed to set POSIX_RLIMIT_CORE');
}
?>
--FILE--
<?php

posix_setrlimit(POSIX_RLIMIT_CORE, 2048, -1);
[$hard, $soft] = posix_getrlimit(POSIX_RLIMIT_CORE);
var_dump($hard, $soft);

posix_setrlimit(POSIX_RLIMIT_CORE, 1024, 2048);
[$hard, $soft] = posix_getrlimit(POSIX_RLIMIT_CORE);
var_dump($hard, $soft);

?>
--EXPECT--
int(2048)
string(9) "unlimited"
int(1024)
int(2048)