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