setrlimit02.c revision 43088e16aa60d69e3ec5a69cdd8bdd45b8891127
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 <pwd.h>
47#include "test.h"
48#include "usctest.h"
49
50char *TCID = "setrlimit02";
51extern int Tst_count;
52
53char nobody_uid[] = "nobody";
54struct passwd *ltpuser;
55
56struct rlimit rlim;
57
58void setup();
59void cleanup();
60
61int exp_enos[] = { EFAULT, EINVAL, EPERM, 0 };
62
63struct test_case_t {
64	int resource;
65	struct rlimit *rlim;
66	int error;
67} TC[] = {
68#if !defined(UCLINUX)
69	/* rlim points outside the process address space - EFAULT */
70	{
71	RLIMIT_NOFILE, (void *)-1, EFAULT},
72#endif
73	    /* the resource is invalid - EINVAL */
74	{
75	-1, &rlim, EINVAL},
76	    /* a non-root user attemps to increase the rlim_max value - EPERM */
77	{
78	RLIMIT_NOFILE, &rlim, EPERM}
79};
80
81int TST_TOTAL = sizeof(TC) / sizeof(*TC);
82
83int main(int ac, char **av)
84{
85	int lc;			/* loop counter */
86	char *msg;		/* message returned from parse_opts */
87	int i;
88
89	/* parse standard options */
90	if ((msg = parse_opts(ac, av, NULL) {
91		tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg);
92	 /*NOTREACHED*/}
93
94	setup();
95
96	/* set up the expected errnos */
97	TEST_EXP_ENOS(exp_enos);
98
99	/* check looping state if -i option given */
100	for (lc = 0; TEST_LOOPING(lc); lc++) {
101
102		/* reset Tst_count in case we are looping. */
103		Tst_count = 0;
104
105		/* loop through the test cases */
106		for (i = 0; i < TST_TOTAL; i++) {
107
108			TEST(setrlimit(TC[i].resource, TC[i].rlim));
109
110			if (TEST_RETURN != -1) {
111				tst_resm(TFAIL, "call succeeded unexpectedly");
112				continue;
113			}
114
115			TEST_ERROR_LOG(TEST_ERRNO);
116
117			if (TEST_ERRNO == TC[i].error) {
118				tst_resm(TPASS, "expected failure - "
119					 "errno = %d : %s", TEST_ERRNO,
120					 strerror(TEST_ERRNO));
121			} else {
122				tst_resm(TFAIL, "unexpected error - %d : %s - "
123					 "expected %d", TEST_ERRNO,
124					 strerror(TEST_ERRNO), TC[i].error);
125			}
126		}
127	}
128	cleanup();
129
130	 /*NOTREACHED*/ return 0;
131
132}
133
134/*
135 * setup() - performs all ONE TIME setup for this test.
136 */
137void setup()
138{
139
140	/* capture signals */
141	tst_sig(NOFORK, DEF_HANDLER, cleanup);
142
143	/* Pause if that option was specified */
144	TEST_PAUSE;
145
146	/* Switch to nobody user for correct error code collection */
147	if (geteuid() != 0) {
148		tst_brkm(TBROK, tst_exit, "Test must be run as root");
149	}
150	ltpuser = getpwnam(nobody_uid);
151	if (setuid(ltpuser->pw_uid) == -1) {
152		tst_resm(TINFO, "setuid failed to "
153			 "to set the effective uid to %d", ltpuser->pw_uid);
154		perror("setuid");
155	}
156
157	/* set an illegal value for a non-root user - test #3 - EPERM */
158	getrlimit(RLIMIT_NOFILE, &rlim);
159	rlim.rlim_max++;
160}
161
162/*
163 * cleanup() - performs all ONE TIME cleanup for this test at
164 *	       completion or premature exit.
165 */
166void cleanup()
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