From f1a659274ddbae95a1604e24a7825f3a08ce645c Mon Sep 17 00:00:00 2001 From: Ilija Tovilo Date: Thu, 20 Oct 2022 20:53:39 +0200 Subject: [PATCH] Allow specifying resource in posix_getrlimit() for single result --- ext/posix/posix.c | 37 ++++++++++++++++--- ext/posix/posix.stub.php | 2 +- ext/posix/posix_arginfo.h | 3 +- .../posix_getrlimit_single_resource.phpt | 28 ++++++++++++++ 4 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 ext/posix/tests/posix_getrlimit_single_resource.phpt diff --git a/ext/posix/posix.c b/ext/posix/posix.c index b462cc8b7ad6..271b11567016 100644 --- a/ext/posix/posix.c +++ b/ext/posix/posix.c @@ -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); + } } } /* }}} */ diff --git a/ext/posix/posix.stub.php b/ext/posix/posix.stub.php index ecb462695ccf..006efe648dc0 100644 --- a/ext/posix/posix.stub.php +++ b/ext/posix/posix.stub.php @@ -338,7 +338,7 @@ function posix_getpwuid(int $user_id): array|false {} * @return array|false * @refcount 1 */ -function posix_getrlimit(): array|false {} +function posix_getrlimit(?int $resource = null): array|false {} #endif #ifdef HAVE_SETRLIMIT diff --git a/ext/posix/posix_arginfo.h b/ext/posix/posix_arginfo.h index f45e8644b29b..acbbfa91fbfc 100644 --- a/ext/posix/posix_arginfo.h +++ b/ext/posix/posix_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 82454cec6f55336a530c23663efeb7ac71932bba */ + * Stub hash: 2738ee335d62cb797a0e9969e0912019fca71e59 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_posix_kill, 0, 2, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, process_id, IS_LONG, 0) @@ -133,6 +133,7 @@ ZEND_END_ARG_INFO() #if defined(HAVE_GETRLIMIT) ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_posix_getrlimit, 0, 0, MAY_BE_ARRAY|MAY_BE_FALSE) + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, resource, IS_LONG, 1, "null") ZEND_END_ARG_INFO() #endif diff --git a/ext/posix/tests/posix_getrlimit_single_resource.phpt b/ext/posix/tests/posix_getrlimit_single_resource.phpt new file mode 100644 index 000000000000..c97eefeb5917 --- /dev/null +++ b/ext/posix/tests/posix_getrlimit_single_resource.phpt @@ -0,0 +1,28 @@ +--TEST-- +posix_getrlimit() for single resource +--EXTENSIONS-- +posix +--SKIPIF-- + +--FILE-- + +--EXPECT-- +int(2048) +string(9) "unlimited" +int(1024) +int(2048)