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