blob: bb45cf6c981fcd0c9e9c7654208ab4434bfa7b4e [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"
plars865695b2001-08-27 22:15:12 +000048
49char *TCID = "setrlimit02";
plars865695b2001-08-27 22:15:12 +000050
robbiew1b8acfb2001-09-13 19:45:25 +000051char nobody_uid[] = "nobody";
52struct passwd *ltpuser;
53
plars865695b2001-08-27 22:15:12 +000054struct rlimit rlim;
55
56void setup();
57void cleanup();
58
plars865695b2001-08-27 22:15:12 +000059struct test_case_t {
60 int resource;
61 struct rlimit *rlim;
62 int error;
63} TC[] = {
mreed1081534c32006-08-03 05:21:24 +000064#if !defined(UCLINUX)
plars865695b2001-08-27 22:15:12 +000065 /* rlim points outside the process address space - EFAULT */
subrata_modak56207ce2009-03-23 13:35:39 +000066 {
67 RLIMIT_NOFILE, (void *)-1, EFAULT},
mreed1081534c32006-08-03 05:21:24 +000068#endif
subrata_modak56207ce2009-03-23 13:35:39 +000069 /* 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}
plars865695b2001-08-27 22:15:12 +000075};
76
Cyril Hrubisb863a0b2014-09-24 13:15:29 +020077int TST_TOTAL = ARRAY_SIZE(TC);
vapier70df7d72006-03-04 00:52:53 +000078
plars74948ad2002-11-14 16:16:14 +000079int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000080{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020081 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020082 const char *msg;
plars865695b2001-08-27 22:15:12 +000083 int i;
84
Garrett Cooper45e285d2010-11-22 12:19:25 -080085 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080086 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Wanlong Gao354ebb42012-12-07 10:10:04 +080087 }
plars865695b2001-08-27 22:15:12 +000088
89 setup();
90
plars865695b2001-08-27 22:15:12 +000091 for (lc = 0; TEST_LOOPING(lc); lc++) {
92
Caspar Zhangd59a6592013-03-07 14:59:12 +080093 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000094
95 /* loop through the test cases */
96 for (i = 0; i < TST_TOTAL; i++) {
97
98 TEST(setrlimit(TC[i].resource, TC[i].rlim));
99
100 if (TEST_RETURN != -1) {
101 tst_resm(TFAIL, "call succeeded unexpectedly");
102 continue;
103 }
104
plars865695b2001-08-27 22:15:12 +0000105 if (TEST_ERRNO == TC[i].error) {
106 tst_resm(TPASS, "expected failure - "
107 "errno = %d : %s", TEST_ERRNO,
108 strerror(TEST_ERRNO));
109 } else {
110 tst_resm(TFAIL, "unexpected error - %d : %s - "
111 "expected %d", TEST_ERRNO,
112 strerror(TEST_ERRNO), TC[i].error);
113 }
114 }
115 }
116 cleanup();
117
Garrett Cooper53740502010-12-16 00:04:01 -0800118 tst_exit();
robbiewfa451a12003-03-27 20:52:36 +0000119
plars865695b2001-08-27 22:15:12 +0000120}
121
122/*
123 * setup() - performs all ONE TIME setup for this test.
124 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400125void setup(void)
plars865695b2001-08-27 22:15:12 +0000126{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200127 tst_require_root(NULL);
plars865695b2001-08-27 22:15:12 +0000128
plars865695b2001-08-27 22:15:12 +0000129 tst_sig(NOFORK, DEF_HANDLER, cleanup);
130
plars865695b2001-08-27 22:15:12 +0000131 TEST_PAUSE;
132
subrata_modak56207ce2009-03-23 13:35:39 +0000133 /* Switch to nobody user for correct error code collection */
subrata_modak56207ce2009-03-23 13:35:39 +0000134 ltpuser = getpwnam(nobody_uid);
135 if (setuid(ltpuser->pw_uid) == -1) {
136 tst_resm(TINFO, "setuid failed to "
137 "to set the effective uid to %d", ltpuser->pw_uid);
138 perror("setuid");
139 }
robbiew1b8acfb2001-09-13 19:45:25 +0000140
plars865695b2001-08-27 22:15:12 +0000141 /* set an illegal value for a non-root user - test #3 - EPERM */
robbiew1806c8f2005-05-26 20:34:22 +0000142 getrlimit(RLIMIT_NOFILE, &rlim);
subrata_modak56207ce2009-03-23 13:35:39 +0000143 rlim.rlim_max++;
plars865695b2001-08-27 22:15:12 +0000144}
145
146/*
147 * cleanup() - performs all ONE TIME cleanup for this test at
148 * completion or premature exit.
149 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400150void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000151{
plars865695b2001-08-27 22:15:12 +0000152
Chris Dearmanec6edca2012-10-17 19:54:01 -0700153}