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