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