blob: 247a3012ed08d2c896f81272e67957294ee340b0 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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"
mreed10b55dbea2006-05-01 17:35:30 +000048#include <linux/fs.h>
plars865695b2001-08-27 22:15:12 +000049
50char *TCID = "setrlimit03";
51int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000052
robbiewd34d5812005-07-11 22:28:09 +000053#if !defined(NR_OPEN)
vapierd39b5ae2006-02-18 06:36:47 +000054//Taken from definition in /usr/include/linux/fs.h
Wanlong Gao354ebb42012-12-07 10:10:04 +080055#define NR_OPEN (1024*1024)
robbiewd34d5812005-07-11 22:28:09 +000056#endif
robbiew0dd84762004-06-01 16:59:10 +000057
plars865695b2001-08-27 22:15:12 +000058void setup();
59void cleanup();
60
plars74948ad2002-11-14 16:16:14 +000061int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000062{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020063 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020064 const char *msg;
plars865695b2001-08-27 22:15:12 +000065 struct rlimit rlim;
66
Garrett Cooper45e285d2010-11-22 12:19:25 -080067 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080068 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Wanlong Gao354ebb42012-12-07 10:10:04 +080069 }
plars865695b2001-08-27 22:15:12 +000070
71 setup();
72
plars865695b2001-08-27 22:15:12 +000073 for (lc = 0; TEST_LOOPING(lc); lc++) {
74
Caspar Zhangd59a6592013-03-07 14:59:12 +080075 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000076
plars36a2e5b2002-12-12 18:03:14 +000077 if (getrlimit(RLIMIT_NOFILE, &rlim) != 0)
78 tst_brkm(TFAIL, cleanup, "getrlimit failed, "
subrata_modak56207ce2009-03-23 13:35:39 +000079 "errno = %d", errno);
plars36a2e5b2002-12-12 18:03:14 +000080 rlim.rlim_max = NR_OPEN + 1;
plars865695b2001-08-27 22:15:12 +000081
82 TEST(setrlimit(RLIMIT_NOFILE, &rlim));
83
84 if (TEST_RETURN != -1) {
85 tst_resm(TFAIL, "call succeeded unexpectedly");
86 continue;
87 }
88
plars36a2e5b2002-12-12 18:03:14 +000089 if (TEST_ERRNO != EPERM) {
90 tst_resm(TFAIL, "Expected EPERM, got %d", TEST_ERRNO);
plars865695b2001-08-27 22:15:12 +000091 } else {
92 tst_resm(TPASS, "got expected EPERM error");
93 }
94 }
95 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -080096 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -080097
plars865695b2001-08-27 22:15:12 +000098}
99
100/*
101 * setup() - performs all ONE TIME setup for this test.
102 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400103void setup(void)
plars865695b2001-08-27 22:15:12 +0000104{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200105 tst_require_root(NULL);
plars865695b2001-08-27 22:15:12 +0000106
plars865695b2001-08-27 22:15:12 +0000107 tst_sig(FORK, DEF_HANDLER, cleanup);
108
plars865695b2001-08-27 22:15:12 +0000109 TEST_PAUSE;
110}
111
112/*
113 * cleanup() - performs all ONE TIME cleanup for this test at
114 * completion or premature exit.
115 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400116void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000117{
plars865695b2001-08-27 22:15:12 +0000118
Chris Dearmanec6edca2012-10-17 19:54:01 -0700119}