]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-rlimit-util.c
2 This file is part of systemd
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.
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.
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/>.
18 #include <sys/resource.h>
20 #include "capability-util.h"
22 #include "rlimit-util.h"
23 #include "string-util.h"
26 int main(int argc
, char *argv
[]) {
27 struct rlimit old
, new, high
;
33 log_parse_environment();
36 assert_se(drop_capability(CAP_SYS_RESOURCE
) == 0);
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);
43 assert_se(rlimit_from_string("LimitNOFILE") == RLIMIT_NOFILE
);
44 assert_se(rlimit_from_string("DefaultLimitNOFILE") == -1);
46 assert_se(streq_ptr(rlimit_to_string(RLIMIT_NOFILE
), "LimitNOFILE"));
47 assert_se(rlimit_to_string(-1) == NULL
);
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
);
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
);
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
);