1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2001
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  * NAME
22  *	setrlimit03.c
23  *
24  * DESCRIPTION
25  *	Test for EPERM when the super-user tries to increase RLIMIT_NOFILE
26  *	beyond the system limit.
27  *
28  * USAGE:  <for command-line>
29  *  setrlimit03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
30  *     where,  -c n : Run n copies concurrently.
31  *             -e   : Turn on errno logging.
32  *             -i n : Execute test n times.
33  *             -I x : Execute test for x seconds.
34  *             -P x : Pause for x seconds between iterations.
35  *             -t   : Turn on syscall timing.
36  *
37  * HISTORY
38  *	07/2001 Ported by Wayne Boyer
39  *
40  * RESTRICTIONS
41  *	Must run test as root.
42  */
43 #include <sys/time.h>
44 #include <sys/resource.h>
45 #include <unistd.h>
46 #include <errno.h>
47 #include "test.h"
48 #include <linux/fs.h>
49 
50 char *TCID = "setrlimit03";
51 int TST_TOTAL = 1;
52 
53 #if !defined(NR_OPEN)
54 //Taken from definition in /usr/include/linux/fs.h
55 #define NR_OPEN (1024*1024)
56 #endif
57 
58 void setup();
59 void cleanup();
60 
main(int ac,char ** av)61 int main(int ac, char **av)
62 {
63 	int lc;
64 	struct rlimit rlim;
65 
66 	tst_parse_opts(ac, av, NULL, NULL);
67 
68 	setup();
69 
70 	for (lc = 0; TEST_LOOPING(lc); lc++) {
71 
72 		tst_count = 0;
73 
74 		if (getrlimit(RLIMIT_NOFILE, &rlim) != 0)
75 			tst_brkm(TFAIL, cleanup, "getrlimit failed, "
76 				 "errno = %d", errno);
77 		rlim.rlim_max = NR_OPEN + 1;
78 
79 		TEST(setrlimit(RLIMIT_NOFILE, &rlim));
80 
81 		if (TEST_RETURN != -1) {
82 			tst_resm(TFAIL, "call succeeded unexpectedly");
83 			continue;
84 		}
85 
86 		if (TEST_ERRNO != EPERM) {
87 			tst_resm(TFAIL, "Expected EPERM, got %d", TEST_ERRNO);
88 		} else {
89 			tst_resm(TPASS, "got expected EPERM error");
90 		}
91 	}
92 	cleanup();
93 	tst_exit();
94 
95 }
96 
97 /*
98  * setup() - performs all ONE TIME setup for this test.
99  */
setup(void)100 void setup(void)
101 {
102 	tst_require_root();
103 
104 	tst_sig(FORK, DEF_HANDLER, cleanup);
105 
106 	TEST_PAUSE;
107 }
108 
109 /*
110  * cleanup() - performs all ONE TIME cleanup for this test at
111  *	       completion or premature exit.
112  */
cleanup(void)113 void cleanup(void)
114 {
115 
116 }
117