core: accept time units for time-based resource limits
Let's make sure "LimitCPU=30min" can be parsed properly, following the usual logic how we parse time values. Similar for LimitRTTIME=. While we are at it, extend a bit on the man page section about resource limits. Fixes: #1772
This commit is contained in:
@@ -681,6 +681,66 @@ static void test_config_parse_bounding_set(void) {
|
||||
assert_se(capability_bounding_set_drop == ~(make_cap(CAP_NET_RAW) | make_cap(CAP_NET_ADMIN)));
|
||||
}
|
||||
|
||||
static void test_config_parse_rlimit(void) {
|
||||
struct rlimit * rl[_RLIMIT_MAX] = {};
|
||||
|
||||
assert_se(config_parse_limit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "55", rl, NULL) >= 0);
|
||||
assert_se(rl[RLIMIT_NOFILE]);
|
||||
assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 55);
|
||||
assert_se(rl[RLIMIT_NOFILE]->rlim_cur == rl[RLIMIT_NOFILE]->rlim_max);
|
||||
|
||||
assert_se(config_parse_limit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "infinity", rl, NULL) >= 0);
|
||||
assert_se(rl[RLIMIT_NOFILE]);
|
||||
assert_se(rl[RLIMIT_NOFILE]->rlim_cur == RLIM_INFINITY);
|
||||
assert_se(rl[RLIMIT_NOFILE]->rlim_cur == rl[RLIMIT_NOFILE]->rlim_max);
|
||||
|
||||
rl[RLIMIT_NOFILE] = mfree(rl[RLIMIT_NOFILE]);
|
||||
|
||||
assert_se(config_parse_sec_limit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "56", rl, NULL) >= 0);
|
||||
assert_se(rl[RLIMIT_CPU]);
|
||||
assert_se(rl[RLIMIT_CPU]->rlim_cur == 56);
|
||||
assert_se(rl[RLIMIT_CPU]->rlim_cur == rl[RLIMIT_CPU]->rlim_max);
|
||||
|
||||
assert_se(config_parse_sec_limit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "57s", rl, NULL) >= 0);
|
||||
assert_se(rl[RLIMIT_CPU]);
|
||||
assert_se(rl[RLIMIT_CPU]->rlim_cur == 57);
|
||||
assert_se(rl[RLIMIT_CPU]->rlim_cur == rl[RLIMIT_CPU]->rlim_max);
|
||||
|
||||
assert_se(config_parse_sec_limit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "infinity", rl, NULL) >= 0);
|
||||
assert_se(rl[RLIMIT_CPU]);
|
||||
assert_se(rl[RLIMIT_CPU]->rlim_cur == RLIM_INFINITY);
|
||||
assert_se(rl[RLIMIT_CPU]->rlim_cur == rl[RLIMIT_CPU]->rlim_max);
|
||||
|
||||
assert_se(config_parse_sec_limit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "1234ms", rl, NULL) >= 0);
|
||||
assert_se(rl[RLIMIT_CPU]);
|
||||
assert_se(rl[RLIMIT_CPU]->rlim_cur == 2);
|
||||
assert_se(rl[RLIMIT_CPU]->rlim_cur == rl[RLIMIT_CPU]->rlim_max);
|
||||
|
||||
rl[RLIMIT_CPU] = mfree(rl[RLIMIT_CPU]);
|
||||
|
||||
assert_se(config_parse_usec_limit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "58", rl, NULL) >= 0);
|
||||
assert_se(rl[RLIMIT_RTTIME]);
|
||||
assert_se(rl[RLIMIT_RTTIME]->rlim_cur == 58);
|
||||
assert_se(rl[RLIMIT_RTTIME]->rlim_cur == rl[RLIMIT_RTTIME]->rlim_max);
|
||||
|
||||
assert_se(config_parse_usec_limit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "59s", rl, NULL) >= 0);
|
||||
assert_se(rl[RLIMIT_RTTIME]);
|
||||
assert_se(rl[RLIMIT_RTTIME]->rlim_cur == 59 * USEC_PER_SEC);
|
||||
assert_se(rl[RLIMIT_RTTIME]->rlim_cur == rl[RLIMIT_RTTIME]->rlim_max);
|
||||
|
||||
assert_se(config_parse_usec_limit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "infinity", rl, NULL) >= 0);
|
||||
assert_se(rl[RLIMIT_RTTIME]);
|
||||
assert_se(rl[RLIMIT_RTTIME]->rlim_cur == RLIM_INFINITY);
|
||||
assert_se(rl[RLIMIT_RTTIME]->rlim_cur == rl[RLIMIT_RTTIME]->rlim_max);
|
||||
|
||||
assert_se(config_parse_usec_limit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "2345ms", rl, NULL) >= 0);
|
||||
assert_se(rl[RLIMIT_RTTIME]);
|
||||
assert_se(rl[RLIMIT_RTTIME]->rlim_cur == 2345 * USEC_PER_MSEC);
|
||||
assert_se(rl[RLIMIT_RTTIME]->rlim_cur == rl[RLIMIT_RTTIME]->rlim_max);
|
||||
|
||||
rl[RLIMIT_RTTIME] = mfree(rl[RLIMIT_RTTIME]);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int r;
|
||||
|
||||
@@ -690,6 +750,7 @@ int main(int argc, char *argv[]) {
|
||||
r = test_unit_file_get_set();
|
||||
test_config_parse_exec();
|
||||
test_config_parse_bounding_set();
|
||||
test_config_parse_rlimit();
|
||||
test_load_env_file_1();
|
||||
test_load_env_file_2();
|
||||
test_load_env_file_3();
|
||||
|
||||
Reference in New Issue
Block a user