1 /*
2 * Copyright (c) International Business Machines Corp., 2001
3 * Copyright (c) 2017 Xiao Yang <yangx.jy@cn.fujitsu.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /*
21 * DESCRIPTION
22 * 1) Test for EPERM when the super-user tries to increase RLIMIT_NOFILE
23 * beyond the system limit.
24 * 2) Test for EINVAL when rlim->rlim_cur is greater than rlim->rlim_max.
25 */
26
27 #include <errno.h>
28 #include <sys/time.h>
29 #include <sys/resource.h>
30 #include <linux/fs.h>
31 #include "tst_test.h"
32
33 #if !defined(NR_OPEN)
34 // Taken from definition in /usr/include/linux/fs.h
35 # define NR_OPEN (1024*1024)
36 #endif
37
38 static struct rlimit rlim1, rlim2;
39
40 static struct tcase {
41 struct rlimit *rlimt;
42 int exp_err;
43 } tcases[] = {
44 {&rlim1, EPERM},
45 {&rlim2, EINVAL}
46 };
47
verify_setrlimit(unsigned int n)48 static void verify_setrlimit(unsigned int n)
49 {
50 struct tcase *tc = &tcases[n];
51
52 TEST(setrlimit(RLIMIT_NOFILE, tc->rlimt));
53 if (TST_RET != -1) {
54 tst_res(TFAIL, "call succeeded unexpectedly");
55 return;
56 }
57
58 if (TST_ERR != tc->exp_err) {
59 tst_res(TFAIL | TTERRNO, "setrlimit() should fail with %s, got",
60 tst_strerrno(tc->exp_err));
61 } else {
62 tst_res(TPASS | TTERRNO, "setrlimit() failed as expected");
63 }
64 }
65
setup(void)66 static void setup(void)
67 {
68 SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlim1);
69 rlim2.rlim_max = rlim1.rlim_cur;
70 rlim2.rlim_cur = rlim1.rlim_max + 1;
71 rlim1.rlim_max = NR_OPEN + 1;
72 }
73
74 static struct tst_test test = {
75 .setup = setup,
76 .tcnt = ARRAY_SIZE(tcases),
77 .test = verify_setrlimit,
78 .needs_root = 1
79 };
80