]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-rlimit-util.c
resolved: add code to join/leave mDNS multicast groups
[thirdparty/systemd.git] / src / test / test-rlimit-util.c
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
20 #include "capability-util.h"
21 #include "macro.h"
22 #include "rlimit-util.h"
23 #include "string-util.h"
24 #include "util.h"
25
26 int main(int argc, char *argv[]) {
27 struct rlimit old, new, high;
28 struct rlimit err = {
29 .rlim_cur = 10,
30 .rlim_max = 5,
31 };
32
33 log_parse_environment();
34 log_open();
35
36 assert_se(drop_capability(CAP_SYS_RESOURCE) == 0);
37
38 assert_se(getrlimit(RLIMIT_NOFILE, &old) == 0);
39 new.rlim_cur = MIN(5U, old.rlim_max);
40 new.rlim_max = MIN(10U, old.rlim_max);
41 assert_se(setrlimit(RLIMIT_NOFILE, &new) >= 0);
42
43 assert_se(rlimit_from_string("LimitNOFILE") == RLIMIT_NOFILE);
44 assert_se(rlimit_from_string("DefaultLimitNOFILE") == -1);
45
46 assert_se(streq_ptr(rlimit_to_string(RLIMIT_NOFILE), "LimitNOFILE"));
47 assert_se(rlimit_to_string(-1) == NULL);
48
49 assert_se(getrlimit(RLIMIT_NOFILE, &old) == 0);
50 assert_se(setrlimit_closest(RLIMIT_NOFILE, &old) == 0);
51 assert_se(getrlimit(RLIMIT_NOFILE, &new) == 0);
52 assert_se(old.rlim_cur == new.rlim_cur);
53 assert_se(old.rlim_max == new.rlim_max);
54
55 assert_se(getrlimit(RLIMIT_NOFILE, &old) == 0);
56 high = RLIMIT_MAKE_CONST(old.rlim_max + 1);
57 assert_se(setrlimit_closest(RLIMIT_NOFILE, &high) == 0);
58 assert_se(getrlimit(RLIMIT_NOFILE, &new) == 0);
59 assert_se(new.rlim_max == old.rlim_max);
60 assert_se(new.rlim_cur == new.rlim_max);
61
62 assert_se(getrlimit(RLIMIT_NOFILE, &old) == 0);
63 assert_se(setrlimit_closest(RLIMIT_NOFILE, &err) == -EINVAL);
64 assert_se(getrlimit(RLIMIT_NOFILE, &new) == 0);
65 assert_se(old.rlim_cur == new.rlim_cur);
66 assert_se(old.rlim_max == new.rlim_max);
67
68 return 0;
69 }