]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: fix rlimit parsing
authorEvgeny Vereshchagin <evvers@ya.ru>
Fri, 27 Nov 2015 08:54:42 +0000 (08:54 +0000)
committerEvgeny Vereshchagin <evvers@ya.ru>
Fri, 27 Nov 2015 11:26:37 +0000 (11:26 +0000)
* refuse limits if soft > hard
* print an actual value instead of (null)

see https://github.com/systemd/systemd/pull/1994#issuecomment-159999123

src/core/load-fragment.c
src/test/test-unit-file.c

index 8847578bd7067390ee54b2f65bcfe80e2eec96c5..3c124495b6c74efa461dbb50d4eee0fe6152fe14 100644 (file)
@@ -1173,6 +1173,7 @@ static int parse_rlimit_range(
                 struct rlimit **rl,
                 int (*rlim_parser)(const char *, rlim_t *)) {
 
+        const char *whole_value = value;
         rlim_t soft, hard;
         _cleanup_free_ char *sword = NULL, *hword = NULL;
         int nwords, r;
@@ -1188,9 +1189,11 @@ static int parse_rlimit_range(
         if (r == 0 && nwords == 2)
                 r = rlim_parser(hword, &hard);
         if (r < 0) {
-                log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse resource value, ignoring: %s", value);
+                log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse resource value, ignoring: %s", whole_value);
                 return 0;
         }
+        if (nwords == 2 && soft > hard)
+                return log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid resource value ("RLIM_FMT" > "RLIM_FMT"), ignoring: %s", soft, hard, whole_value);
 
         if (!*rl) {
                 *rl = new(struct rlimit, 1);
index 854f60be80f9cbcfd8e206bb0637caf7befb7832..0b3630f77c7dd8eb9221ec4d0ddd3414e9a096b4 100644 (file)
@@ -695,6 +695,27 @@ static void test_config_parse_rlimit(void) {
         assert_se(rl[RLIMIT_NOFILE]->rlim_cur == RLIM_INFINITY);
         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, "10:20:30", rl, NULL) >= 0);
+        assert_se(rl[RLIMIT_NOFILE]);
+        assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 10);
+        assert_se(rl[RLIMIT_NOFILE]->rlim_max == 20);
+
+        /* Invalid values don't change rl */
+        assert_se(config_parse_limit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "wat:wat", rl, NULL) >= 0);
+        assert_se(rl[RLIMIT_NOFILE]);
+        assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 10);
+        assert_se(rl[RLIMIT_NOFILE]->rlim_max == 20);
+
+        assert_se(config_parse_limit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "66:wat", rl, NULL) >= 0);
+        assert_se(rl[RLIMIT_NOFILE]);
+        assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 10);
+        assert_se(rl[RLIMIT_NOFILE]->rlim_max == 20);
+
+        assert_se(config_parse_limit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "200:100", rl, NULL) >= 0);
+        assert_se(rl[RLIMIT_NOFILE]);
+        assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 10);
+        assert_se(rl[RLIMIT_NOFILE]->rlim_max == 20);
+
         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);