Skip to content

Commit d10a04b

Browse files
committed
Allow specifying resource in posix_getrlimit() for single result
Closes GH-9790
1 parent 6d298cc commit d10a04b

File tree

5 files changed

+67
-7
lines changed

5 files changed

+67
-7
lines changed

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ PHP 8.3 UPGRADE NOTES
2323
2. New Features
2424
========================================
2525

26+
- Posix
27+
. posix_getrlimit() now takes an optional $res parameter to allow fetching a
28+
single resource limit.
29+
2630
========================================
2731
3. Changes in SAPI modules
2832
========================================

ext/posix/posix.c

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,16 +1056,43 @@ static const struct limitlist {
10561056
PHP_FUNCTION(posix_getrlimit)
10571057
{
10581058
const struct limitlist *l = NULL;
1059+
zend_long res;
1060+
bool res_is_null = true;
10591061

1060-
ZEND_PARSE_PARAMETERS_NONE();
1062+
ZEND_PARSE_PARAMETERS_START(0, 1)
1063+
Z_PARAM_OPTIONAL
1064+
Z_PARAM_LONG_OR_NULL(res, res_is_null)
1065+
ZEND_PARSE_PARAMETERS_END();
10611066

1062-
array_init(return_value);
1067+
if (res_is_null) {
1068+
array_init(return_value);
10631069

1064-
for (l=limits; l->name; l++) {
1065-
if (posix_addlimit(l->limit, l->name, return_value) == FAILURE) {
1066-
zend_array_destroy(Z_ARR_P(return_value));
1070+
for (l=limits; l->name; l++) {
1071+
if (posix_addlimit(l->limit, l->name, return_value) == FAILURE) {
1072+
zend_array_destroy(Z_ARR_P(return_value));
1073+
RETURN_FALSE;
1074+
}
1075+
}
1076+
} else {
1077+
struct rlimit rl;
1078+
int result = getrlimit(res, &rl);
1079+
if (result < 0) {
1080+
POSIX_G(last_error) = errno;
10671081
RETURN_FALSE;
10681082
}
1083+
1084+
array_init(return_value);
1085+
if (rl.rlim_cur == RLIM_INFINITY) {
1086+
add_next_index_stringl(return_value, UNLIMITED_STRING, sizeof(UNLIMITED_STRING)-1);
1087+
} else {
1088+
add_next_index_long(return_value, rl.rlim_cur);
1089+
}
1090+
1091+
if (rl.rlim_max == RLIM_INFINITY) {
1092+
add_next_index_stringl(return_value, UNLIMITED_STRING, sizeof(UNLIMITED_STRING)-1);
1093+
} else {
1094+
add_next_index_long(return_value, rl.rlim_max);
1095+
}
10691096
}
10701097
}
10711098
/* }}} */

ext/posix/posix.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ function posix_getpwuid(int $user_id): array|false {}
338338
* @return array<string, int|string>|false
339339
* @refcount 1
340340
*/
341-
function posix_getrlimit(): array|false {}
341+
function posix_getrlimit(?int $resource = null): array|false {}
342342
#endif
343343

344344
#ifdef HAVE_SETRLIMIT

ext/posix/posix_arginfo.h

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
posix_getrlimit() for single resource
3+
--EXTENSIONS--
4+
posix
5+
--SKIPIF--
6+
<?php
7+
if (!function_exists('posix_getrlimit')) die('skip posix_getrlimit() not found');
8+
if (!function_exists('posix_setrlimit') || !posix_setrlimit(POSIX_RLIMIT_CORE, 2048, -1)) {
9+
die('skip Failed to set POSIX_RLIMIT_CORE');
10+
}
11+
?>
12+
--FILE--
13+
<?php
14+
15+
posix_setrlimit(POSIX_RLIMIT_CORE, 2048, -1);
16+
[$hard, $soft] = posix_getrlimit(POSIX_RLIMIT_CORE);
17+
var_dump($hard, $soft);
18+
19+
posix_setrlimit(POSIX_RLIMIT_CORE, 1024, 2048);
20+
[$hard, $soft] = posix_getrlimit(POSIX_RLIMIT_CORE);
21+
var_dump($hard, $soft);
22+
23+
?>
24+
--EXPECT--
25+
int(2048)
26+
string(9) "unlimited"
27+
int(1024)
28+
int(2048)

0 commit comments

Comments
 (0)