setrlimit02.c revision 74948ad801d4aba1c872652580b8b331e41aefca
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/*
21 * NAME
22 *	setrlimit02.c
23 *
24 * DESCRIPTION
25 *	Testcase to test the different errnos set by setrlimit(2) system call.
26 *
27 * USAGE:  <for command-line>
28 *  setrlimit02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
29 *     where,  -c n : Run n copies concurrently.
30 *             -e   : Turn on errno logging.
31 *             -i n : Execute test n times.
32 *             -I x : Execute test for x seconds.
33 *             -P x : Pause for x seconds between iterations.
34 *             -t   : Turn on syscall timing.
35 *
36 * HISTORY
37 *	07/2001 Ported by Wayne Boyer
38 *
39 * RESTRICTIONS
40 *	NONE
41 */
42#include <sys/time.h>
43#include <sys/resource.h>
44#include <unistd.h>
45#include <errno.h>
46#include <linux/fs.h>
47#include <pwd.h>
48#include "test.h"
49#include "usctest.h"
50
51char *TCID = "setrlimit02";
52int TST_TOTAL = 3;
53extern int Tst_count;
54
55char nobody_uid[] = "nobody";
56struct passwd *ltpuser;
57
58struct rlimit rlim;
59
60void setup();
61void cleanup();
62
63int exp_enos[]={EFAULT, EINVAL, EPERM, 0};
64
65struct test_case_t {
66	int resource;
67	struct rlimit *rlim;
68	int error;
69} TC[] = {
70	/* rlim points outside the process address space - EFAULT */
71	{RLIMIT_NOFILE, (void *)-1, EFAULT},
72
73	/* the resource is invalid - EINVAL */
74	{-1, &rlim, EINVAL},
75
76	/* a non-root user attemps to increase the rlim_max value - EPERM */
77	{RLIMIT_NOFILE, &rlim, EPERM}
78};
79
80int main(int ac, char **av)
81{
82	int lc;				/* loop counter */
83	char *msg;			/* message returned from parse_opts */
84	int i;
85
86	/* parse standard options */
87	if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){
88		tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg);
89		/*NOTREACHED*/
90	}
91
92	setup();
93
94	/* set up the expected errnos */
95	TEST_EXP_ENOS(exp_enos);
96
97	/* check looping state if -i option given */
98	for (lc = 0; TEST_LOOPING(lc); lc++) {
99
100		/* reset Tst_count in case we are looping. */
101		Tst_count = 0;
102
103		/* loop through the test cases */
104		for (i = 0; i < TST_TOTAL; i++) {
105
106			TEST(setrlimit(TC[i].resource, TC[i].rlim));
107
108			if (TEST_RETURN != -1) {
109				tst_resm(TFAIL, "call succeeded unexpectedly");
110				continue;
111			}
112
113			TEST_ERROR_LOG(TEST_ERRNO);
114
115			if (TEST_ERRNO == TC[i].error) {
116				tst_resm(TPASS, "expected failure - "
117					 "errno = %d : %s", TEST_ERRNO,
118					 strerror(TEST_ERRNO));
119			} else {
120				tst_resm(TFAIL, "unexpected error - %d : %s - "
121					 "expected %d", TEST_ERRNO,
122					 strerror(TEST_ERRNO), TC[i].error);
123			}
124		}
125	}
126	cleanup();
127
128	/*NOTREACHED*/
129}
130
131/*
132 * setup() - performs all ONE TIME setup for this test.
133 */
134void
135setup()
136{
137
138	/* capture signals */
139	tst_sig(NOFORK, DEF_HANDLER, cleanup);
140
141	/* Pause if that option was specified */
142	TEST_PAUSE;
143
144	 /* Switch to nobody user for correct error code collection */
145        if (geteuid() != 0) {
146                tst_brkm(TBROK, tst_exit, "Test must be run as root");
147        }
148        ltpuser = getpwnam(nobody_uid);
149        if (setuid(ltpuser->pw_uid) == -1) {
150                tst_resm(TINFO, "setuid failed to "
151                         "to set the effective uid to %d",
152                         ltpuser->pw_uid);
153                perror("setuid");
154        }
155
156
157	/* set an illegal value for a non-root user - test #3 - EPERM */
158	rlim.rlim_max = NR_OPEN + 1;
159}
160
161/*
162 * cleanup() - performs all ONE TIME cleanup for this test at
163 *	       completion or premature exit.
164 */
165void
166cleanup()
167{
168	/*
169	 * print timing stats if that option was specified.
170	 * print errno log if that option was specified.
171	 */
172	TEST_CLEANUP;
173
174	/* exit with return code appropriate for results */
175	tst_exit();
176}
177