]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
fe39daf2 EV |
2 | |
3 | #include <sys/resource.h> | |
4 | ||
99d4f5e5 | 5 | #include "alloc-util.h" |
fe39daf2 EV |
6 | #include "capability-util.h" |
7 | #include "macro.h" | |
f5947a5e | 8 | #include "missing_resource.h" |
fe39daf2 EV |
9 | #include "rlimit-util.h" |
10 | #include "string-util.h" | |
ca78ad1d | 11 | #include "time-util.h" |
fe39daf2 | 12 | |
99d4f5e5 LP |
13 | static void test_rlimit_parse_format(int resource, const char *string, rlim_t soft, rlim_t hard, int ret, const char *formatted) { |
14 | _cleanup_free_ char *f = NULL; | |
15 | struct rlimit rl = { | |
16 | .rlim_cur = 4711, | |
17 | .rlim_max = 4712, | |
18 | }, rl2 = { | |
19 | .rlim_cur = 4713, | |
20 | .rlim_max = 4714 | |
21 | }; | |
22 | ||
23 | assert_se(rlimit_parse(resource, string, &rl) == ret); | |
24 | if (ret < 0) | |
25 | return; | |
26 | ||
27 | assert_se(rl.rlim_cur == soft); | |
28 | assert_se(rl.rlim_max == hard); | |
29 | ||
30 | assert_se(rlimit_format(&rl, &f) >= 0); | |
31 | assert_se(streq(formatted, f)); | |
32 | ||
33 | assert_se(rlimit_parse(resource, formatted, &rl2) >= 0); | |
34 | assert_se(memcmp(&rl, &rl2, sizeof(struct rlimit)) == 0); | |
35 | } | |
36 | ||
fe39daf2 EV |
37 | int main(int argc, char *argv[]) { |
38 | struct rlimit old, new, high; | |
39 | struct rlimit err = { | |
40 | .rlim_cur = 10, | |
41 | .rlim_max = 5, | |
42 | }; | |
6550c24c | 43 | int i; |
fe39daf2 EV |
44 | |
45 | log_parse_environment(); | |
46 | log_open(); | |
47 | ||
48 | assert_se(drop_capability(CAP_SYS_RESOURCE) == 0); | |
49 | ||
50 | assert_se(getrlimit(RLIMIT_NOFILE, &old) == 0); | |
51 | new.rlim_cur = MIN(5U, old.rlim_max); | |
d5bc32d1 | 52 | new.rlim_max = old.rlim_max; |
fe39daf2 EV |
53 | assert_se(setrlimit(RLIMIT_NOFILE, &new) >= 0); |
54 | ||
6550c24c LP |
55 | assert_se(rlimit_from_string("NOFILE") == RLIMIT_NOFILE); |
56 | assert_se(rlimit_from_string("LimitNOFILE") == -1); | |
57 | assert_se(rlimit_from_string("RLIMIT_NOFILE") == -1); | |
58 | assert_se(rlimit_from_string("xxxNOFILE") == -1); | |
fe39daf2 EV |
59 | assert_se(rlimit_from_string("DefaultLimitNOFILE") == -1); |
60 | ||
6550c24c LP |
61 | assert_se(rlimit_from_string_harder("NOFILE") == RLIMIT_NOFILE); |
62 | assert_se(rlimit_from_string_harder("LimitNOFILE") == RLIMIT_NOFILE); | |
63 | assert_se(rlimit_from_string_harder("RLIMIT_NOFILE") == RLIMIT_NOFILE); | |
64 | assert_se(rlimit_from_string_harder("xxxNOFILE") == -1); | |
65 | assert_se(rlimit_from_string_harder("DefaultLimitNOFILE") == -1); | |
66 | ||
67 | for (i = 0; i < _RLIMIT_MAX; i++) { | |
68 | _cleanup_free_ char *prefixed = NULL; | |
69 | const char *p; | |
70 | ||
71 | assert_se(p = rlimit_to_string(i)); | |
72 | log_info("%i = %s", i, p); | |
73 | ||
74 | assert_se(rlimit_from_string(p) == i); | |
75 | assert_se(rlimit_from_string_harder(p) == i); | |
76 | ||
77 | assert_se(prefixed = strjoin("Limit", p)); | |
78 | ||
79 | assert_se(rlimit_from_string(prefixed) < 0); | |
80 | assert_se(rlimit_from_string_harder(prefixed) == i); | |
81 | ||
82 | prefixed = mfree(prefixed); | |
83 | assert_se(prefixed = strjoin("RLIMIT_", p)); | |
84 | ||
85 | assert_se(rlimit_from_string(prefixed) < 0); | |
86 | assert_se(rlimit_from_string_harder(prefixed) == i); | |
87 | } | |
88 | ||
89 | assert_se(streq_ptr(rlimit_to_string(RLIMIT_NOFILE), "NOFILE")); | |
fe39daf2 EV |
90 | assert_se(rlimit_to_string(-1) == NULL); |
91 | ||
92 | assert_se(getrlimit(RLIMIT_NOFILE, &old) == 0); | |
93 | assert_se(setrlimit_closest(RLIMIT_NOFILE, &old) == 0); | |
94 | assert_se(getrlimit(RLIMIT_NOFILE, &new) == 0); | |
95 | assert_se(old.rlim_cur == new.rlim_cur); | |
96 | assert_se(old.rlim_max == new.rlim_max); | |
97 | ||
98 | assert_se(getrlimit(RLIMIT_NOFILE, &old) == 0); | |
d5bc32d1 | 99 | high = RLIMIT_MAKE_CONST(old.rlim_max == RLIM_INFINITY ? old.rlim_max : old.rlim_max + 1); |
fe39daf2 EV |
100 | assert_se(setrlimit_closest(RLIMIT_NOFILE, &high) == 0); |
101 | assert_se(getrlimit(RLIMIT_NOFILE, &new) == 0); | |
102 | assert_se(new.rlim_max == old.rlim_max); | |
103 | assert_se(new.rlim_cur == new.rlim_max); | |
104 | ||
105 | assert_se(getrlimit(RLIMIT_NOFILE, &old) == 0); | |
106 | assert_se(setrlimit_closest(RLIMIT_NOFILE, &err) == -EINVAL); | |
107 | assert_se(getrlimit(RLIMIT_NOFILE, &new) == 0); | |
108 | assert_se(old.rlim_cur == new.rlim_cur); | |
109 | assert_se(old.rlim_max == new.rlim_max); | |
110 | ||
99d4f5e5 LP |
111 | test_rlimit_parse_format(RLIMIT_NOFILE, "4:5", 4, 5, 0, "4:5"); |
112 | test_rlimit_parse_format(RLIMIT_NOFILE, "6", 6, 6, 0, "6"); | |
113 | test_rlimit_parse_format(RLIMIT_NOFILE, "infinity", RLIM_INFINITY, RLIM_INFINITY, 0, "infinity"); | |
114 | test_rlimit_parse_format(RLIMIT_NOFILE, "infinity:infinity", RLIM_INFINITY, RLIM_INFINITY, 0, "infinity"); | |
115 | test_rlimit_parse_format(RLIMIT_NOFILE, "8:infinity", 8, RLIM_INFINITY, 0, "8:infinity"); | |
116 | test_rlimit_parse_format(RLIMIT_CPU, "25min:13h", (25*USEC_PER_MINUTE) / USEC_PER_SEC, (13*USEC_PER_HOUR) / USEC_PER_SEC, 0, "1500:46800"); | |
117 | test_rlimit_parse_format(RLIMIT_NOFILE, "", 0, 0, -EINVAL, NULL); | |
118 | test_rlimit_parse_format(RLIMIT_NOFILE, "5:4", 0, 0, -EILSEQ, NULL); | |
119 | test_rlimit_parse_format(RLIMIT_NOFILE, "5:4:3", 0, 0, -EINVAL, NULL); | |
29857001 LP |
120 | test_rlimit_parse_format(RLIMIT_NICE, "20", 20, 20, 0, "20"); |
121 | test_rlimit_parse_format(RLIMIT_NICE, "40", 40, 40, 0, "40"); | |
122 | test_rlimit_parse_format(RLIMIT_NICE, "41", 41, 41, -ERANGE, "41"); | |
123 | test_rlimit_parse_format(RLIMIT_NICE, "0", 0, 0, 0, "0"); | |
124 | test_rlimit_parse_format(RLIMIT_NICE, "-7", 27, 27, 0, "27"); | |
125 | test_rlimit_parse_format(RLIMIT_NICE, "-20", 40, 40, 0, "40"); | |
126 | test_rlimit_parse_format(RLIMIT_NICE, "-21", 41, 41, -ERANGE, "41"); | |
127 | test_rlimit_parse_format(RLIMIT_NICE, "-0", 20, 20, 0, "20"); | |
128 | test_rlimit_parse_format(RLIMIT_NICE, "+7", 13, 13, 0, "13"); | |
129 | test_rlimit_parse_format(RLIMIT_NICE, "+19", 1, 1, 0, "1"); | |
130 | test_rlimit_parse_format(RLIMIT_NICE, "+20", 0, 0, -ERANGE, "0"); | |
131 | test_rlimit_parse_format(RLIMIT_NICE, "+0", 20, 20, 0, "20"); | |
99d4f5e5 | 132 | |
fe39daf2 EV |
133 | return 0; |
134 | } |