1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 08:12:21 +01:00

Allow specifying resource in posix_getrlimit() for single result

Closes GH-9790
This commit is contained in:
Ilija Tovilo
2022-10-20 20:53:39 +02:00
parent 6d298cc784
commit d10a04b391
5 changed files with 67 additions and 7 deletions

View File

@@ -23,6 +23,10 @@ PHP 8.3 UPGRADE NOTES
2. New Features
========================================
- Posix
. posix_getrlimit() now takes an optional $res parameter to allow fetching a
single resource limit.
========================================
3. Changes in SAPI modules
========================================

View File

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

View File

@@ -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

View File

@@ -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

View File

@@ -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)