blob: b403409390f15aef1d7a40cc2877bf1035ca4474 [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/*
plars865695b2001-08-27 22:15:12 +000021 * Testcase to check the basic functionality of the setrlimit system call.
plars865695b2001-08-27 22:15:12 +000022 * Use the different commands like RLIMIT_NOFILE, RLIMIT_CORE,
23 * RLIMIT_FSIZE, and, RLIMIT_NOFILE, and test for different test
24 * conditions.
25 *
plars865695b2001-08-27 22:15:12 +000026 * 07/2001 Ported by Wayne Boyer
plars865695b2001-08-27 22:15:12 +000027 */
Wanlong Gao58c67c62013-12-20 16:08:55 +080028
plars74948ad2002-11-14 16:16:14 +000029#include <sys/types.h>
Garrett Cooperbacc8492011-01-14 00:36:17 -080030#include <sys/resource.h>
plars74948ad2002-11-14 16:16:14 +000031#include <sys/stat.h>
Garrett Cooperbacc8492011-01-14 00:36:17 -080032#include <sys/time.h>
33#include <sys/wait.h>
plars865695b2001-08-27 22:15:12 +000034#include <errno.h>
Garrett Cooperbacc8492011-01-14 00:36:17 -080035#include <fcntl.h>
plars865695b2001-08-27 22:15:12 +000036#include <stdlib.h>
Garrett Cooperbacc8492011-01-14 00:36:17 -080037#include <unistd.h>
plars865695b2001-08-27 22:15:12 +000038#include "test.h"
plars865695b2001-08-27 22:15:12 +000039
40char *TCID = "setrlimit01";
41int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000042
Wanlong Gao58c67c62013-12-20 16:08:55 +080043static void setup(void);
44static void cleanup(void);
45static void test1(void);
46static void test2(void);
47static void test3(void);
48static void test4(void);
49static void sighandler(int);
plars865695b2001-08-27 22:15:12 +000050
Wanlong Gao58c67c62013-12-20 16:08:55 +080051static char filename[40] = "";
52static struct rlimit save_rlim, rlim, rlim1;
53static int nofiles, fd, bytes, i, status;
54static char *buf = "abcdefghijklmnopqrstuvwxyz";
55static pid_t pid;
plars865695b2001-08-27 22:15:12 +000056
plars74948ad2002-11-14 16:16:14 +000057int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000058{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020059 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020060 const char *msg;
plars865695b2001-08-27 22:15:12 +000061
Wanlong Gao58c67c62013-12-20 16:08:55 +080062 msg = parse_opts(ac, av, NULL, NULL);
63 if (msg != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080064 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000065
Wanlong Gao58c67c62013-12-20 16:08:55 +080066 setup();
subrata_modakbdbaec52009-02-26 12:14:51 +000067
plars865695b2001-08-27 22:15:12 +000068 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080069 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000070
71 test1();
72 test2();
73 test3();
74 /* reset saved conditions */
75 if ((setrlimit(RLIMIT_NPROC, &save_rlim)) == -1) {
76 tst_brkm(TBROK, cleanup, "setrlimit failed to reset "
77 "RLIMIT_NPROC, errno = %d", errno);
78 }
79 test4();
80 }
Wanlong Gao58c67c62013-12-20 16:08:55 +080081
plars865695b2001-08-27 22:15:12 +000082 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -080083 tst_exit();
plars865695b2001-08-27 22:15:12 +000084}
subrata_modak56207ce2009-03-23 13:35:39 +000085
plars865695b2001-08-27 22:15:12 +000086/*
87 * test1 - Test for RLIMIT_NOFILE
88 */
Wanlong Gao58c67c62013-12-20 16:08:55 +080089static void test1(void)
plars865695b2001-08-27 22:15:12 +000090{
91 rlim.rlim_cur = 100;
92 rlim.rlim_max = 100;
93
94 TEST(setrlimit(RLIMIT_NOFILE, &rlim));
95
96 if (TEST_RETURN == -1) {
97 tst_resm(TFAIL, "setrlimit failed to set "
98 "RLIMIT_NOFILE, errno = %d", errno);
99 return;
100 }
101
plars865695b2001-08-27 22:15:12 +0000102 nofiles = getdtablesize();
103
104 if (nofiles != 100) {
105 tst_resm(TFAIL, "setrlimit failed, expected "
106 "100, got %d", nofiles);
107 return;
108 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000109
plars865695b2001-08-27 22:15:12 +0000110 tst_resm(TPASS, "RLIMIT_NOFILE functionality is correct");
111}
112
113/*
114 * test2 - Test for RLIMIT_FSIZE
115 */
Wanlong Gao58c67c62013-12-20 16:08:55 +0800116static void test2(void)
plars865695b2001-08-27 22:15:12 +0000117{
118 /*
119 * Since we would be altering the filesize in the child,
120 * we need to "sync", ie. fflush the parent's write buffers
121 * here. This is because the child will inherit the parent's
122 * write buffer, and while exitting it would try to fflush it.
123 * Since its filesize is truncated to only 10 bytes, the
124 * fflush attempt would fail, and the child would exit with
125 * an wired value! So, it is essential to fflush the parent's
126 * write buffer HERE
127 */
Subrata Modak99b7a682010-08-24 16:30:06 +0530128 int pipefd[2];
plars865695b2001-08-27 22:15:12 +0000129 fflush(stdout);
Wanlong Gao58c67c62013-12-20 16:08:55 +0800130 if (pipe(pipefd) == -1)
Subrata Modak99b7a682010-08-24 16:30:06 +0530131 tst_brkm(TBROK | TERRNO, NULL, "pipe creation failed");
plars865695b2001-08-27 22:15:12 +0000132
133 /*
134 * Spawn a child process, and reduce the filesize to
135 * 10 by calling setrlimit(). We can't do this in the
136 * parent, because the parent needs a bigger filesize as its
137 * output will be saved to the logfile (instead of stdout)
138 * when the testcase (parent) is run from the driver.
139 */
Wanlong Gao58c67c62013-12-20 16:08:55 +0800140 pid = FORK_OR_VFORK();
141 if (pid == -1)
plars865695b2001-08-27 22:15:12 +0000142 tst_brkm(TBROK, cleanup, "fork() failed");
plars865695b2001-08-27 22:15:12 +0000143
144 if (pid == 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800145 close(pipefd[0]); /* close unused read end */
plars865695b2001-08-27 22:15:12 +0000146 rlim.rlim_cur = 10;
147 rlim.rlim_max = 10;
Wanlong Gao58c67c62013-12-20 16:08:55 +0800148 if ((setrlimit(RLIMIT_FSIZE, &rlim)) == -1)
plars865695b2001-08-27 22:15:12 +0000149 exit(1);
plars865695b2001-08-27 22:15:12 +0000150
Wanlong Gao58c67c62013-12-20 16:08:55 +0800151 fd = creat(filename, 0644);
152 if (fd < 0)
plars865695b2001-08-27 22:15:12 +0000153 exit(2);
plars865695b2001-08-27 22:15:12 +0000154
Wanlong Gao58c67c62013-12-20 16:08:55 +0800155 bytes = write(fd, buf, 26);
156 if (bytes != 10) {
Subrata Modak99b7a682010-08-24 16:30:06 +0530157 if (write(pipefd[1], &bytes, sizeof(bytes))
Wanlong Gao354ebb42012-12-07 10:10:04 +0800158 < sizeof(bytes)) {
Subrata Modak99b7a682010-08-24 16:30:06 +0530159 perror("child: write to pipe failed");
160 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800161 close(pipefd[1]); /* EOF */
plars865695b2001-08-27 22:15:12 +0000162 exit(3);
163 }
164 exit(0); /* success */
165 }
166
167 /* parent */
Wanlong Gao58c67c62013-12-20 16:08:55 +0800168 if (waitpid(pid, &status, 0) == -1)
plars865695b2001-08-27 22:15:12 +0000169 tst_brkm(TBROK, cleanup, "waitpid() failed");
Wanlong Gao58c67c62013-12-20 16:08:55 +0800170
plars865695b2001-08-27 22:15:12 +0000171 switch (WEXITSTATUS(status)) {
172 case 0:
173 tst_resm(TPASS, "RLIMIT_FSIZE test PASSED");
174 break;
175 case 1:
176 tst_resm(TFAIL, "setrlimit failed to set "
177 "RLIMIT_FSIZE, errno = %d", errno);
178 break;
179 case 2:
180 tst_resm(TFAIL, "creating testfile failed");
181 break;
182 case 3:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800183 close(pipefd[1]); /* close unused write end */
Wanlong Gao58c67c62013-12-20 16:08:55 +0800184 if (read(pipefd[0], &bytes, sizeof(bytes)) < sizeof(bytes))
Subrata Modak99b7a682010-08-24 16:30:06 +0530185 tst_resm(TFAIL, "parent: reading pipe failed");
Wanlong Gao58c67c62013-12-20 16:08:55 +0800186
Subrata Modak99b7a682010-08-24 16:30:06 +0530187 close(pipefd[0]);
plars865695b2001-08-27 22:15:12 +0000188 tst_resm(TFAIL, "setrlimit failed, expected "
189 "10 got %d", bytes);
190 break;
191 default:
192 tst_resm(TFAIL, "child returned bad exit status");
193 }
194}
195
196/*
197 * test3 - Test for RLIMIT_NPROC
198 */
Wanlong Gao58c67c62013-12-20 16:08:55 +0800199static void test3(void)
plars865695b2001-08-27 22:15:12 +0000200{
Wanlong Gao58c67c62013-12-20 16:08:55 +0800201 if (getrlimit(RLIMIT_NPROC, &save_rlim) < 0)
plars865695b2001-08-27 22:15:12 +0000202 tst_brkm(TBROK, cleanup, "getrlimit failed, errno: %d", errno);
plars865695b2001-08-27 22:15:12 +0000203
204 rlim.rlim_cur = 10;
205 rlim.rlim_max = 10;
206
207 TEST(setrlimit(RLIMIT_NPROC, &rlim));
208
209 if (TEST_RETURN == -1) {
210 tst_resm(TFAIL, "setrlimit failed to set "
211 "RLIMIT_NPROC, errno = %d", errno);
212 return;
213 }
214
plars865695b2001-08-27 22:15:12 +0000215 if ((getrlimit(RLIMIT_NPROC, &rlim1)) == -1) {
216 tst_brkm(TBROK, cleanup, "getrlimit failed to get "
217 "values for RLIMIT_NPROC, errno = %d", errno);
218 }
219
220 if ((rlim1.rlim_cur != 10) && (rlim1.rlim_max != 10)) {
221 tst_resm(TFAIL, "setrlimit did not set the proc "
222 "limit correctly");
223 return;
224 }
225
226 for (i = 0; i < 20; i++) {
Wanlong Gao58c67c62013-12-20 16:08:55 +0800227 pid = FORK_OR_VFORK();
228 if (pid == -1) {
plars865695b2001-08-27 22:15:12 +0000229 if (errno != EAGAIN) {
230 tst_resm(TWARN, "Expected EAGAIN got %d",
231 errno);
232 }
233 } else if (pid == 0) {
234 exit(0);
235 }
236 }
237 waitpid(pid, &status, 0);
Wanlong Gao58c67c62013-12-20 16:08:55 +0800238 if (WEXITSTATUS(status) != 0)
plars865695b2001-08-27 22:15:12 +0000239 tst_resm(TFAIL, "RLIMIT_NPROC functionality is not correct");
Wanlong Gao58c67c62013-12-20 16:08:55 +0800240 else
plars865695b2001-08-27 22:15:12 +0000241 tst_resm(TPASS, "RLIMIT_NPROC functionality is correct");
plars865695b2001-08-27 22:15:12 +0000242}
243
244/*
vapier65e67ea2006-11-03 08:13:38 +0000245 * test4() - Test for RLIMIT_CORE by forking a child and
246 * having it cause a segfault
plars865695b2001-08-27 22:15:12 +0000247 */
Wanlong Gao58c67c62013-12-20 16:08:55 +0800248static void test4(void)
plars865695b2001-08-27 22:15:12 +0000249{
250 rlim.rlim_cur = 10;
251 rlim.rlim_max = 10;
252
253 TEST(setrlimit(RLIMIT_CORE, &rlim));
254
255 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800256 tst_resm(TFAIL | TERRNO, "setrlimit failed to set RLIMIT_CORE");
plars865695b2001-08-27 22:15:12 +0000257 return;
258 }
259
Wanlong Gao58c67c62013-12-20 16:08:55 +0800260 pid = FORK_OR_VFORK();
261 if (pid == -1)
plars865695b2001-08-27 22:15:12 +0000262 tst_brkm(TBROK, cleanup, "fork() failed");
plars865695b2001-08-27 22:15:12 +0000263
subrata_modak56207ce2009-03-23 13:35:39 +0000264 if (pid == 0) { /* child */
vapier65e67ea2006-11-03 08:13:38 +0000265 char *testbuf = NULL;
plars865695b2001-08-27 22:15:12 +0000266 strcpy(testbuf, "abcd");
267 exit(0);
268 }
269 wait(&status);
270
271 if (access("core", F_OK) == 0) {
Garrett Cooper52626672010-12-20 15:03:21 -0800272 tst_resm(TFAIL, "core dump dumped unexpectedly");
plars865695b2001-08-27 22:15:12 +0000273 return;
subrata_modak56207ce2009-03-23 13:35:39 +0000274 } else if (errno != ENOENT) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800275 tst_resm(TFAIL | TERRNO, "access failed unexpectedly");
plars865695b2001-08-27 22:15:12 +0000276 return;
277 }
278
279 tst_resm(TPASS, "RLIMIT_CORE functionality is correct");
280}
281
282/*
283 * sighandler() - catch sigsegv when generated by child in test #4
284 */
Wanlong Gao58c67c62013-12-20 16:08:55 +0800285static void sighandler(int sig)
plars865695b2001-08-27 22:15:12 +0000286{
Wanlong Gao55a7a5e2013-12-20 16:14:37 +0800287 if (sig != SIGSEGV && sig != SIGXFSZ && sig != SIGTERM)
Garrett Cooper52626672010-12-20 15:03:21 -0800288 tst_brkm(TBROK, NULL, "caught unexpected signal: %d", sig);
Wanlong Gao58c67c62013-12-20 16:08:55 +0800289
Salvatore Croaeaf7932011-11-22 09:34:48 +0100290 _exit(0);
plars865695b2001-08-27 22:15:12 +0000291}
292
Wanlong Gao58c67c62013-12-20 16:08:55 +0800293static void setup(void)
plars865695b2001-08-27 22:15:12 +0000294{
Wanlong Gao58c67c62013-12-20 16:08:55 +0800295 tst_require_root(NULL);
plars865695b2001-08-27 22:15:12 +0000296
297 umask(0);
298
plars865695b2001-08-27 22:15:12 +0000299 tst_sig(FORK, sighandler, cleanup);
300
plars865695b2001-08-27 22:15:12 +0000301 TEST_PAUSE;
302
plars865695b2001-08-27 22:15:12 +0000303 tst_tmpdir();
304
305 sprintf(filename, "setrlimit1.%d", getpid());
306}
307
Wanlong Gao58c67c62013-12-20 16:08:55 +0800308static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000309{
plars865695b2001-08-27 22:15:12 +0000310 unlink(filename);
311 tst_rmdir();
Garrett Cooper52626672010-12-20 15:03:21 -0800312}