blob: 6f6f6e8349a516a33fc74f8bcbd900eb58baba9b [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 * 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>
robbiew1b8acfb2001-09-13 19:45:25 +000046#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000047#include "test.h"
48#include "usctest.h"
49
50char *TCID = "setrlimit02";
plars865695b2001-08-27 22:15:12 +000051
robbiew1b8acfb2001-09-13 19:45:25 +000052char nobody_uid[] = "nobody";
53struct passwd *ltpuser;
54
plars865695b2001-08-27 22:15:12 +000055struct rlimit rlim;
56
57void setup();
58void cleanup();
59
subrata_modak56207ce2009-03-23 13:35:39 +000060int exp_enos[] = { EFAULT, EINVAL, EPERM, 0 };
plars865695b2001-08-27 22:15:12 +000061
62struct test_case_t {
63 int resource;
64 struct rlimit *rlim;
65 int error;
66} TC[] = {
mreed1081534c32006-08-03 05:21:24 +000067#if !defined(UCLINUX)
plars865695b2001-08-27 22:15:12 +000068 /* rlim points outside the process address space - EFAULT */
subrata_modak56207ce2009-03-23 13:35:39 +000069 {
70 RLIMIT_NOFILE, (void *)-1, EFAULT},
mreed1081534c32006-08-03 05:21:24 +000071#endif
subrata_modak56207ce2009-03-23 13:35:39 +000072 /* 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}
plars865695b2001-08-27 22:15:12 +000078};
79
subrata_modak56207ce2009-03-23 13:35:39 +000080int TST_TOTAL = sizeof(TC) / sizeof(*TC);
vapier70df7d72006-03-04 00:52:53 +000081
plars74948ad2002-11-14 16:16:14 +000082int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000083{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020084 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020085 const char *msg;
plars865695b2001-08-27 22:15:12 +000086 int i;
87
Garrett Cooper45e285d2010-11-22 12:19:25 -080088 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080089 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Wanlong Gao354ebb42012-12-07 10:10:04 +080090 }
plars865695b2001-08-27 22:15:12 +000091
92 setup();
93
94 /* set up the expected errnos */
95 TEST_EXP_ENOS(exp_enos);
96
plars865695b2001-08-27 22:15:12 +000097 for (lc = 0; TEST_LOOPING(lc); lc++) {
98
Caspar Zhangd59a6592013-03-07 14:59:12 +080099 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000100
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
Garrett Cooper53740502010-12-16 00:04:01 -0800126 tst_exit();
robbiewfa451a12003-03-27 20:52:36 +0000127
plars865695b2001-08-27 22:15:12 +0000128}
129
130/*
131 * setup() - performs all ONE TIME setup for this test.
132 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400133void setup(void)
plars865695b2001-08-27 22:15:12 +0000134{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200135 tst_require_root(NULL);
plars865695b2001-08-27 22:15:12 +0000136
plars865695b2001-08-27 22:15:12 +0000137 tst_sig(NOFORK, DEF_HANDLER, cleanup);
138
plars865695b2001-08-27 22:15:12 +0000139 TEST_PAUSE;
140
subrata_modak56207ce2009-03-23 13:35:39 +0000141 /* Switch to nobody user for correct error code collection */
subrata_modak56207ce2009-03-23 13:35:39 +0000142 ltpuser = getpwnam(nobody_uid);
143 if (setuid(ltpuser->pw_uid) == -1) {
144 tst_resm(TINFO, "setuid failed to "
145 "to set the effective uid to %d", ltpuser->pw_uid);
146 perror("setuid");
147 }
robbiew1b8acfb2001-09-13 19:45:25 +0000148
plars865695b2001-08-27 22:15:12 +0000149 /* set an illegal value for a non-root user - test #3 - EPERM */
robbiew1806c8f2005-05-26 20:34:22 +0000150 getrlimit(RLIMIT_NOFILE, &rlim);
subrata_modak56207ce2009-03-23 13:35:39 +0000151 rlim.rlim_max++;
plars865695b2001-08-27 22:15:12 +0000152}
153
154/*
155 * cleanup() - performs all ONE TIME cleanup for this test at
156 * completion or premature exit.
157 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400158void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000159{
160 /*
161 * print timing stats if that option was specified.
162 * print errno log if that option was specified.
163 */
164 TEST_CLEANUP;
165
Chris Dearmanec6edca2012-10-17 19:54:01 -0700166}