1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2017 Xiao Yang <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker * DESCRIPTION
9*49cdfc7eSAndroid Build Coastguard Worker * 1) Test for EPERM when the super-user tries to increase RLIMIT_NOFILE
10*49cdfc7eSAndroid Build Coastguard Worker * beyond the system limit.
11*49cdfc7eSAndroid Build Coastguard Worker * 2) Test for EINVAL when rlim->rlim_cur is greater than rlim->rlim_max.
12*49cdfc7eSAndroid Build Coastguard Worker */
13*49cdfc7eSAndroid Build Coastguard Worker
14*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <sys/time.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/resource.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <linux/fs.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker #if !defined(NR_OPEN)
21*49cdfc7eSAndroid Build Coastguard Worker // Taken from definition in /usr/include/linux/fs.h
22*49cdfc7eSAndroid Build Coastguard Worker # define NR_OPEN (1024*1024)
23*49cdfc7eSAndroid Build Coastguard Worker #endif
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker #define NR_OPEN_PATH "/proc/sys/fs/nr_open"
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker static struct rlimit rlim1, rlim2;
28*49cdfc7eSAndroid Build Coastguard Worker static unsigned int nr_open = NR_OPEN;
29*49cdfc7eSAndroid Build Coastguard Worker
30*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
31*49cdfc7eSAndroid Build Coastguard Worker struct rlimit *rlimt;
32*49cdfc7eSAndroid Build Coastguard Worker int exp_err;
33*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
34*49cdfc7eSAndroid Build Coastguard Worker {&rlim1, EPERM},
35*49cdfc7eSAndroid Build Coastguard Worker {&rlim2, EINVAL}
36*49cdfc7eSAndroid Build Coastguard Worker };
37*49cdfc7eSAndroid Build Coastguard Worker
verify_setrlimit(unsigned int n)38*49cdfc7eSAndroid Build Coastguard Worker static void verify_setrlimit(unsigned int n)
39*49cdfc7eSAndroid Build Coastguard Worker {
40*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[n];
41*49cdfc7eSAndroid Build Coastguard Worker
42*49cdfc7eSAndroid Build Coastguard Worker TEST(setrlimit(RLIMIT_NOFILE, tc->rlimt));
43*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET != -1) {
44*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "call succeeded unexpectedly "
45*49cdfc7eSAndroid Build Coastguard Worker "(nr_open=%u rlim_cur=%lu rlim_max=%lu)", nr_open,
46*49cdfc7eSAndroid Build Coastguard Worker (unsigned long)(tc->rlimt->rlim_cur),
47*49cdfc7eSAndroid Build Coastguard Worker (unsigned long)(tc->rlimt->rlim_max));
48*49cdfc7eSAndroid Build Coastguard Worker return;
49*49cdfc7eSAndroid Build Coastguard Worker }
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker if (TST_ERR != tc->exp_err) {
52*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "setrlimit() should fail with %s, got",
53*49cdfc7eSAndroid Build Coastguard Worker tst_strerrno(tc->exp_err));
54*49cdfc7eSAndroid Build Coastguard Worker } else {
55*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TTERRNO, "setrlimit() failed as expected");
56*49cdfc7eSAndroid Build Coastguard Worker }
57*49cdfc7eSAndroid Build Coastguard Worker }
58*49cdfc7eSAndroid Build Coastguard Worker
setup(void)59*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
60*49cdfc7eSAndroid Build Coastguard Worker {
61*49cdfc7eSAndroid Build Coastguard Worker if (!access(NR_OPEN_PATH, F_OK))
62*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_SCANF(NR_OPEN_PATH, "%u", &nr_open);
63*49cdfc7eSAndroid Build Coastguard Worker
64*49cdfc7eSAndroid Build Coastguard Worker SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlim1);
65*49cdfc7eSAndroid Build Coastguard Worker rlim2.rlim_max = rlim1.rlim_cur;
66*49cdfc7eSAndroid Build Coastguard Worker rlim2.rlim_cur = rlim1.rlim_max + 1;
67*49cdfc7eSAndroid Build Coastguard Worker rlim1.rlim_max = nr_open + 1;
68*49cdfc7eSAndroid Build Coastguard Worker }
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
71*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
72*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
73*49cdfc7eSAndroid Build Coastguard Worker .test = verify_setrlimit,
74*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1
75*49cdfc7eSAndroid Build Coastguard Worker };
76