]>
Commit | Line | Data |
---|---|---|
fe39daf2 EV |
1 | /*** |
2 | This file is part of systemd | |
3 | ||
4 | systemd is free software; you can redistribute it and/or modify it | |
5 | under the terms of the GNU Lesser General Public License as published by | |
6 | the Free Software Foundation; either version 2.1 of the License, or | |
7 | (at your option) any later version. | |
8 | ||
9 | systemd is distributed in the hope that it will be useful, but | |
10 | 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. | |
13 | ||
14 | You should have received a copy of the GNU Lesser General Public License | |
15 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
16 | ***/ | |
17 | ||
18 | #include <sys/resource.h> | |
19 | ||
99d4f5e5 | 20 | #include "alloc-util.h" |
fe39daf2 EV |
21 | #include "capability-util.h" |
22 | #include "macro.h" | |
23 | #include "rlimit-util.h" | |
24 | #include "string-util.h" | |
25 | #include "util.h" | |
26 | ||
99d4f5e5 LP |
27 | static void test_rlimit_parse_format(int resource, const char *string, rlim_t soft, rlim_t hard, int ret, const char *formatted) { |
28 | _cleanup_free_ char *f = NULL; | |
29 | struct rlimit rl = { | |
30 | .rlim_cur = 4711, | |
31 | .rlim_max = 4712, | |
32 | }, rl2 = { | |
33 | .rlim_cur = 4713, | |
34 | .rlim_max = 4714 | |
35 | }; | |
36 | ||
37 | assert_se(rlimit_parse(resource, string, &rl) == ret); | |
38 | if (ret < 0) | |
39 | return; | |
40 | ||
41 | assert_se(rl.rlim_cur == soft); | |
42 | assert_se(rl.rlim_max == hard); | |
43 | ||
44 | assert_se(rlimit_format(&rl, &f) >= 0); | |
45 | assert_se(streq(formatted, f)); | |
46 | ||
47 | assert_se(rlimit_parse(resource, formatted, &rl2) >= 0); | |
48 | assert_se(memcmp(&rl, &rl2, sizeof(struct rlimit)) == 0); | |
49 | } | |
50 | ||
fe39daf2 EV |
51 | int main(int argc, char *argv[]) { |
52 | struct rlimit old, new, high; | |
53 | struct rlimit err = { | |
54 | .rlim_cur = 10, | |
55 | .rlim_max = 5, | |
56 | }; | |
57 | ||
58 | log_parse_environment(); | |
59 | log_open(); | |
60 | ||
61 | assert_se(drop_capability(CAP_SYS_RESOURCE) == 0); | |
62 | ||
63 | assert_se(getrlimit(RLIMIT_NOFILE, &old) == 0); | |
64 | new.rlim_cur = MIN(5U, old.rlim_max); | |
d5bc32d1 | 65 | new.rlim_max = old.rlim_max; |
fe39daf2 EV |
66 | assert_se(setrlimit(RLIMIT_NOFILE, &new) >= 0); |
67 | ||
68 | assert_se(rlimit_from_string("LimitNOFILE") == RLIMIT_NOFILE); | |
69 | assert_se(rlimit_from_string("DefaultLimitNOFILE") == -1); | |
70 | ||
71 | assert_se(streq_ptr(rlimit_to_string(RLIMIT_NOFILE), "LimitNOFILE")); | |
72 | assert_se(rlimit_to_string(-1) == NULL); | |
73 | ||
74 | assert_se(getrlimit(RLIMIT_NOFILE, &old) == 0); | |
75 | assert_se(setrlimit_closest(RLIMIT_NOFILE, &old) == 0); | |
76 | assert_se(getrlimit(RLIMIT_NOFILE, &new) == 0); | |
77 | assert_se(old.rlim_cur == new.rlim_cur); | |
78 | assert_se(old.rlim_max == new.rlim_max); | |
79 | ||
80 | assert_se(getrlimit(RLIMIT_NOFILE, &old) == 0); | |
d5bc32d1 | 81 | high = RLIMIT_MAKE_CONST(old.rlim_max == RLIM_INFINITY ? old.rlim_max : old.rlim_max + 1); |
fe39daf2 EV |
82 | assert_se(setrlimit_closest(RLIMIT_NOFILE, &high) == 0); |
83 | assert_se(getrlimit(RLIMIT_NOFILE, &new) == 0); | |
84 | assert_se(new.rlim_max == old.rlim_max); | |
85 | assert_se(new.rlim_cur == new.rlim_max); | |
86 | ||
87 | assert_se(getrlimit(RLIMIT_NOFILE, &old) == 0); | |
88 | assert_se(setrlimit_closest(RLIMIT_NOFILE, &err) == -EINVAL); | |
89 | assert_se(getrlimit(RLIMIT_NOFILE, &new) == 0); | |
90 | assert_se(old.rlim_cur == new.rlim_cur); | |
91 | assert_se(old.rlim_max == new.rlim_max); | |
92 | ||
99d4f5e5 LP |
93 | test_rlimit_parse_format(RLIMIT_NOFILE, "4:5", 4, 5, 0, "4:5"); |
94 | test_rlimit_parse_format(RLIMIT_NOFILE, "6", 6, 6, 0, "6"); | |
95 | test_rlimit_parse_format(RLIMIT_NOFILE, "infinity", RLIM_INFINITY, RLIM_INFINITY, 0, "infinity"); | |
96 | test_rlimit_parse_format(RLIMIT_NOFILE, "infinity:infinity", RLIM_INFINITY, RLIM_INFINITY, 0, "infinity"); | |
97 | test_rlimit_parse_format(RLIMIT_NOFILE, "8:infinity", 8, RLIM_INFINITY, 0, "8:infinity"); | |
98 | 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"); | |
99 | test_rlimit_parse_format(RLIMIT_NOFILE, "", 0, 0, -EINVAL, NULL); | |
100 | test_rlimit_parse_format(RLIMIT_NOFILE, "5:4", 0, 0, -EILSEQ, NULL); | |
101 | test_rlimit_parse_format(RLIMIT_NOFILE, "5:4:3", 0, 0, -EINVAL, NULL); | |
29857001 LP |
102 | test_rlimit_parse_format(RLIMIT_NICE, "20", 20, 20, 0, "20"); |
103 | test_rlimit_parse_format(RLIMIT_NICE, "40", 40, 40, 0, "40"); | |
104 | test_rlimit_parse_format(RLIMIT_NICE, "41", 41, 41, -ERANGE, "41"); | |
105 | test_rlimit_parse_format(RLIMIT_NICE, "0", 0, 0, 0, "0"); | |
106 | test_rlimit_parse_format(RLIMIT_NICE, "-7", 27, 27, 0, "27"); | |
107 | test_rlimit_parse_format(RLIMIT_NICE, "-20", 40, 40, 0, "40"); | |
108 | test_rlimit_parse_format(RLIMIT_NICE, "-21", 41, 41, -ERANGE, "41"); | |
109 | test_rlimit_parse_format(RLIMIT_NICE, "-0", 20, 20, 0, "20"); | |
110 | test_rlimit_parse_format(RLIMIT_NICE, "+7", 13, 13, 0, "13"); | |
111 | test_rlimit_parse_format(RLIMIT_NICE, "+19", 1, 1, 0, "1"); | |
112 | test_rlimit_parse_format(RLIMIT_NICE, "+20", 0, 0, -ERANGE, "0"); | |
113 | test_rlimit_parse_format(RLIMIT_NICE, "+0", 20, 20, 0, "20"); | |
99d4f5e5 | 114 | |
fe39daf2 EV |
115 | return 0; |
116 | } |