1 /* Copyright (C) 2018-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
20 #include <sys/resource.h>
21 #include <support/check.h>
23 static int resources
[] = {
24 /* The following 7 limits are part of POSIX and must exist. */
34 #define nresources (sizeof (resources) / sizeof (resources[0]))
36 /* Assume that the prlimit64 function calls the prlimit64 syscall without
37 mangling the arguments. */
38 #define PRLIMIT64_INFINITY ((rlim64_t) -1)
40 /* As we don't know which limit will be modified, use a sufficiently high
41 value to not shoot ourself in the foot. Use a 32-bit value to test
42 both the 32- and 64-bit versions, and keep the highest bit clear to
43 avoid sign extension. */
44 #define PRLIMIT64_TESTVAL ((rlim64_t) 0x42420000)
47 test_getrlimit (int resource
, rlim_t exp_cur
, rlim_t exp_max
)
50 TEST_VERIFY_EXIT (getrlimit (resource
, &r
) == 0);
51 TEST_COMPARE (r
.rlim_cur
, exp_cur
);
52 TEST_COMPARE (r
.rlim_max
, exp_max
);
56 test_getrlimit64 (int resource
, rlim64_t exp_cur
, rlim64_t exp_max
)
59 TEST_VERIFY_EXIT (getrlimit64 (resource
, &r
) == 0);
60 TEST_COMPARE (r
.rlim_cur
, exp_cur
);
61 TEST_COMPARE (r
.rlim_max
, exp_max
);
65 test_prlimit_get (int resource
, rlim_t exp_cur
, rlim_t exp_max
)
68 TEST_VERIFY_EXIT (prlimit (0, resource
, NULL
, &r
) == 0);
69 TEST_COMPARE (r
.rlim_cur
, exp_cur
);
70 TEST_COMPARE (r
.rlim_max
, exp_max
);
74 test_prlimit64_get (int resource
, rlim64_t exp_cur
, rlim64_t exp_max
)
77 TEST_COMPARE (prlimit64 (0, resource
, NULL
, &r
), 0);
78 TEST_COMPARE (r
.rlim_cur
, exp_cur
);
79 TEST_COMPARE (r
.rlim_max
, exp_max
);
83 test_setrlimit (int resource
, rlim_t new_cur
, rlim_t new_max
)
85 struct rlimit r
= { new_cur
, new_max
};
86 TEST_COMPARE (setrlimit (resource
, &r
), 0);
90 test_setrlimit64 (int resource
, rlim64_t new_cur
, rlim64_t new_max
)
92 struct rlimit64 r
= { new_cur
, new_max
};
93 TEST_COMPARE (setrlimit64 (resource
, &r
), 0);
97 test_prlimit_set (int resource
, rlim_t new_cur
, rlim_t new_max
)
99 struct rlimit r
= { new_cur
, new_max
};
100 TEST_COMPARE (prlimit (0, resource
, &r
, NULL
), 0);
104 test_prlimit64_set (int resource
, rlim64_t new_cur
, rlim64_t new_max
)
106 struct rlimit64 r
= { new_cur
, new_max
};
107 TEST_COMPARE (prlimit64 (0, resource
, &r
, NULL
), 0);
115 /* Find a resource with hard limit set to infinity, so that the soft limit
116 can be manipulated to any value. */
117 for (int i
= 0; i
< nresources
; ++i
)
120 int res
= prlimit64 (0, resources
[i
], NULL
, &r64
);
121 if ((res
== 0) && (r64
.rlim_max
== PRLIMIT64_INFINITY
))
123 resource
= resources
[i
];
130 ("Could not find and limit with hard limit set to infinity.");
132 /* First check that the get functions work correctly with the test value. */
133 test_prlimit64_set (resource
, PRLIMIT64_TESTVAL
, PRLIMIT64_INFINITY
);
134 test_getrlimit (resource
, PRLIMIT64_TESTVAL
, RLIM_INFINITY
);
135 test_getrlimit64 (resource
, PRLIMIT64_TESTVAL
, RLIM64_INFINITY
);
136 test_prlimit_get (resource
, PRLIMIT64_TESTVAL
, RLIM_INFINITY
);
137 test_prlimit64_get (resource
, PRLIMIT64_TESTVAL
, RLIM64_INFINITY
);
139 /* Then check that the get functions work correctly with infinity. */
140 test_prlimit64_set (resource
, PRLIMIT64_INFINITY
, PRLIMIT64_INFINITY
);
141 test_getrlimit (resource
, RLIM_INFINITY
, RLIM_INFINITY
);
142 test_getrlimit64 (resource
, RLIM64_INFINITY
, RLIM64_INFINITY
);
143 test_prlimit_get (resource
, RLIM_INFINITY
, RLIM_INFINITY
);
144 test_prlimit64_get (resource
, RLIM64_INFINITY
, RLIM64_INFINITY
);
146 /* Then check that setrlimit works correctly with the test value. */
147 test_setrlimit (resource
, PRLIMIT64_TESTVAL
, RLIM_INFINITY
);
148 test_prlimit64_get (resource
, PRLIMIT64_TESTVAL
, PRLIMIT64_INFINITY
);
150 /* Then check that setrlimit works correctly with infinity. */
151 test_setrlimit (resource
, RLIM_INFINITY
, RLIM_INFINITY
);
152 test_prlimit64_get (resource
, PRLIMIT64_INFINITY
, PRLIMIT64_INFINITY
);
154 /* Then check that setrlimit64 works correctly with the test value. */
155 test_setrlimit64 (resource
, PRLIMIT64_TESTVAL
, RLIM64_INFINITY
);
156 test_prlimit64_get (resource
, PRLIMIT64_TESTVAL
, PRLIMIT64_INFINITY
);
158 /* Then check that setrlimit64 works correctly with infinity. */
159 test_setrlimit64 (resource
, RLIM64_INFINITY
, RLIM64_INFINITY
);
160 test_prlimit64_get (resource
, PRLIMIT64_INFINITY
, PRLIMIT64_INFINITY
);
162 /* Then check that prlimit works correctly with the test value. */
163 test_prlimit_set (resource
, RLIM_INFINITY
, RLIM_INFINITY
);
164 test_prlimit64_get (resource
, PRLIMIT64_INFINITY
, PRLIMIT64_INFINITY
);
166 /* Finally check that prlimit works correctly with infinity. */
167 test_prlimit_set (resource
, PRLIMIT64_TESTVAL
, RLIM_INFINITY
);
168 test_prlimit64_get (resource
, PRLIMIT64_TESTVAL
, PRLIMIT64_INFINITY
);
173 #include <support/test-driver.c>