xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/ulimit/ulimit01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or modify it
5*49cdfc7eSAndroid Build Coastguard Worker  * under the terms of version 2 of the GNU General Public License as
6*49cdfc7eSAndroid Build Coastguard Worker  * published by the Free Software Foundation.
7*49cdfc7eSAndroid Build Coastguard Worker  *
8*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful, but
9*49cdfc7eSAndroid Build Coastguard Worker  * WITHOUT ANY WARRANTY; without even the implied warranty of
10*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * Further, this software is distributed without any warranty that it is
13*49cdfc7eSAndroid Build Coastguard Worker  * free of the rightful claim of any third person regarding infringement
14*49cdfc7eSAndroid Build Coastguard Worker  * or the like.  Any license provided herein, whether implied or
15*49cdfc7eSAndroid Build Coastguard Worker  * otherwise, applies only to this software file.  Patent licenses, if
16*49cdfc7eSAndroid Build Coastguard Worker  * any, provided herein do not apply to combinations of this program with
17*49cdfc7eSAndroid Build Coastguard Worker  * other software, or any other product whatsoever.
18*49cdfc7eSAndroid Build Coastguard Worker  *
19*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License along
20*49cdfc7eSAndroid Build Coastguard Worker  * with this program; if not, write the Free Software Foundation, Inc.,
21*49cdfc7eSAndroid Build Coastguard Worker  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22*49cdfc7eSAndroid Build Coastguard Worker  *
23*49cdfc7eSAndroid Build Coastguard Worker  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24*49cdfc7eSAndroid Build Coastguard Worker  * Mountain View, CA  94043, or:
25*49cdfc7eSAndroid Build Coastguard Worker  *
26*49cdfc7eSAndroid Build Coastguard Worker  * http://www.sgi.com
27*49cdfc7eSAndroid Build Coastguard Worker  *
28*49cdfc7eSAndroid Build Coastguard Worker  * For further information regarding this notice, see:
29*49cdfc7eSAndroid Build Coastguard Worker  *
30*49cdfc7eSAndroid Build Coastguard Worker  * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31*49cdfc7eSAndroid Build Coastguard Worker  */
32*49cdfc7eSAndroid Build Coastguard Worker /* $Id: ulimit01.c,v 1.6 2009/11/02 13:57:19 subrata_modak Exp $ */
33*49cdfc7eSAndroid Build Coastguard Worker /**********************************************************
34*49cdfc7eSAndroid Build Coastguard Worker  *
35*49cdfc7eSAndroid Build Coastguard Worker  *    OS Test - Silicon Graphics, Inc.
36*49cdfc7eSAndroid Build Coastguard Worker  *
37*49cdfc7eSAndroid Build Coastguard Worker  *    TEST IDENTIFIER	: ulimit01
38*49cdfc7eSAndroid Build Coastguard Worker  *
39*49cdfc7eSAndroid Build Coastguard Worker  *    EXECUTED BY	: anyone
40*49cdfc7eSAndroid Build Coastguard Worker  *
41*49cdfc7eSAndroid Build Coastguard Worker  *    TEST TITLE	: Basic test for ulimit(2)
42*49cdfc7eSAndroid Build Coastguard Worker  *
43*49cdfc7eSAndroid Build Coastguard Worker  *    PARENT DOCUMENT	: usctpl01
44*49cdfc7eSAndroid Build Coastguard Worker  *
45*49cdfc7eSAndroid Build Coastguard Worker  *    TEST CASE TOTAL	: 6
46*49cdfc7eSAndroid Build Coastguard Worker  *
47*49cdfc7eSAndroid Build Coastguard Worker  *    WALL CLOCK TIME	: 1
48*49cdfc7eSAndroid Build Coastguard Worker  *
49*49cdfc7eSAndroid Build Coastguard Worker  *    CPU TYPES		: ALL
50*49cdfc7eSAndroid Build Coastguard Worker  *
51*49cdfc7eSAndroid Build Coastguard Worker  *    AUTHOR		: William Roske
52*49cdfc7eSAndroid Build Coastguard Worker  *
53*49cdfc7eSAndroid Build Coastguard Worker  *    CO-PILOT		: Dave Fenner
54*49cdfc7eSAndroid Build Coastguard Worker  *
55*49cdfc7eSAndroid Build Coastguard Worker  *    DATE STARTED	: 03/30/92
56*49cdfc7eSAndroid Build Coastguard Worker  *
57*49cdfc7eSAndroid Build Coastguard Worker  *    INITIAL RELEASE	: UNICOS 7.0
58*49cdfc7eSAndroid Build Coastguard Worker  *
59*49cdfc7eSAndroid Build Coastguard Worker  *    TEST CASES
60*49cdfc7eSAndroid Build Coastguard Worker  *
61*49cdfc7eSAndroid Build Coastguard Worker  * 	1.) ulimit(2) returns...(See Description)
62*49cdfc7eSAndroid Build Coastguard Worker  *
63*49cdfc7eSAndroid Build Coastguard Worker  *    INPUT SPECIFICATIONS
64*49cdfc7eSAndroid Build Coastguard Worker  * 	The standard options for system call tests are accepted.
65*49cdfc7eSAndroid Build Coastguard Worker  *	(See the parse_opts(3) man page).
66*49cdfc7eSAndroid Build Coastguard Worker  *
67*49cdfc7eSAndroid Build Coastguard Worker  *    OUTPUT SPECIFICATIONS
68*49cdfc7eSAndroid Build Coastguard Worker  *$
69*49cdfc7eSAndroid Build Coastguard Worker  *    DURATION
70*49cdfc7eSAndroid Build Coastguard Worker  * 	Terminates - with frequency and infinite modes.
71*49cdfc7eSAndroid Build Coastguard Worker  *
72*49cdfc7eSAndroid Build Coastguard Worker  *    SIGNALS
73*49cdfc7eSAndroid Build Coastguard Worker  * 	Uses SIGUSR1 to pause before test if option set.
74*49cdfc7eSAndroid Build Coastguard Worker  * 	(See the parse_opts(3) man page).
75*49cdfc7eSAndroid Build Coastguard Worker  *
76*49cdfc7eSAndroid Build Coastguard Worker  *    RESOURCES
77*49cdfc7eSAndroid Build Coastguard Worker  * 	None
78*49cdfc7eSAndroid Build Coastguard Worker  *
79*49cdfc7eSAndroid Build Coastguard Worker  *    ENVIRONMENTAL NEEDS
80*49cdfc7eSAndroid Build Coastguard Worker  * 	The libcuts.a and libsys.a libraries must be included in
81*49cdfc7eSAndroid Build Coastguard Worker  *	the compilation of this test.
82*49cdfc7eSAndroid Build Coastguard Worker  *
83*49cdfc7eSAndroid Build Coastguard Worker  *    SPECIAL PROCEDURAL REQUIREMENTS
84*49cdfc7eSAndroid Build Coastguard Worker  * 	None
85*49cdfc7eSAndroid Build Coastguard Worker  *
86*49cdfc7eSAndroid Build Coastguard Worker  *    INTERCASE DEPENDENCIES
87*49cdfc7eSAndroid Build Coastguard Worker  * 	None
88*49cdfc7eSAndroid Build Coastguard Worker  *
89*49cdfc7eSAndroid Build Coastguard Worker  *    DETAILED DESCRIPTION
90*49cdfc7eSAndroid Build Coastguard Worker  *	This is a Phase I test for the ulimit(2) system call.  It is intended
91*49cdfc7eSAndroid Build Coastguard Worker  *	to provide a limited exposure of the system call, for now.  It
92*49cdfc7eSAndroid Build Coastguard Worker  *	should/will be extended when full functional tests are written for
93*49cdfc7eSAndroid Build Coastguard Worker  *	ulimit(2).
94*49cdfc7eSAndroid Build Coastguard Worker  *
95*49cdfc7eSAndroid Build Coastguard Worker  * 	Setup:
96*49cdfc7eSAndroid Build Coastguard Worker  * 	  Setup signal handling.
97*49cdfc7eSAndroid Build Coastguard Worker  *	  Pause for SIGUSR1 if option specified.
98*49cdfc7eSAndroid Build Coastguard Worker  *
99*49cdfc7eSAndroid Build Coastguard Worker  * 	Test:
100*49cdfc7eSAndroid Build Coastguard Worker  *	 Loop if the proper options are given.
101*49cdfc7eSAndroid Build Coastguard Worker  * 	  Execute system call
102*49cdfc7eSAndroid Build Coastguard Worker  *	  Check return code, if system call failed (return=-1)
103*49cdfc7eSAndroid Build Coastguard Worker  *		Log the errno and Issue a FAIL message.
104*49cdfc7eSAndroid Build Coastguard Worker  *	  Otherwise, Issue a PASS message.
105*49cdfc7eSAndroid Build Coastguard Worker  *
106*49cdfc7eSAndroid Build Coastguard Worker  * 	Cleanup:
107*49cdfc7eSAndroid Build Coastguard Worker  * 	  Print errno log and/or timing stats if options given
108*49cdfc7eSAndroid Build Coastguard Worker  *
109*49cdfc7eSAndroid Build Coastguard Worker  *
110*49cdfc7eSAndroid Build Coastguard Worker  *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
111*49cdfc7eSAndroid Build Coastguard Worker 
112*49cdfc7eSAndroid Build Coastguard Worker #include <ulimit.h>
113*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
114*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
115*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
116*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
117*49cdfc7eSAndroid Build Coastguard Worker 
118*49cdfc7eSAndroid Build Coastguard Worker void setup();
119*49cdfc7eSAndroid Build Coastguard Worker void cleanup();
120*49cdfc7eSAndroid Build Coastguard Worker 
121*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "ulimit01";
122*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 6;
123*49cdfc7eSAndroid Build Coastguard Worker 
124*49cdfc7eSAndroid Build Coastguard Worker int cmd;
125*49cdfc7eSAndroid Build Coastguard Worker long limit;			/* saved limit */
126*49cdfc7eSAndroid Build Coastguard Worker 
127*49cdfc7eSAndroid Build Coastguard Worker struct limits_t {
128*49cdfc7eSAndroid Build Coastguard Worker 	int cmd;
129*49cdfc7eSAndroid Build Coastguard Worker 	long newlimit;
130*49cdfc7eSAndroid Build Coastguard Worker 	int nlim_flag;		/* special flag for UL_SETFSIZE records  */
131*49cdfc7eSAndroid Build Coastguard Worker 	int exp_fail;
132*49cdfc7eSAndroid Build Coastguard Worker } Scenarios[] = {
133*49cdfc7eSAndroid Build Coastguard Worker 
134*49cdfc7eSAndroid Build Coastguard Worker 	{
135*49cdfc7eSAndroid Build Coastguard Worker 	UL_GETFSIZE, -1, 0, 0}, {
136*49cdfc7eSAndroid Build Coastguard Worker 	UL_SETFSIZE, -2, 1, 0},	/* case case: must be after UL_GETFSIZE */
137*49cdfc7eSAndroid Build Coastguard Worker 	{
138*49cdfc7eSAndroid Build Coastguard Worker 	UL_SETFSIZE, -2, 2, 0},	/* case case: must be after UL_GETFSIZE */
139*49cdfc7eSAndroid Build Coastguard Worker #if UL_GMEMLIM
140*49cdfc7eSAndroid Build Coastguard Worker 	{
141*49cdfc7eSAndroid Build Coastguard Worker 	UL_GMEMLIM, -1, 0, 0},
142*49cdfc7eSAndroid Build Coastguard Worker #endif
143*49cdfc7eSAndroid Build Coastguard Worker #if UL_GDESLIM
144*49cdfc7eSAndroid Build Coastguard Worker 	{
145*49cdfc7eSAndroid Build Coastguard Worker 	UL_GDESLIM, -1, 0, 0},
146*49cdfc7eSAndroid Build Coastguard Worker #endif
147*49cdfc7eSAndroid Build Coastguard Worker #if UL_GSHMEMLIM
148*49cdfc7eSAndroid Build Coastguard Worker 	{
149*49cdfc7eSAndroid Build Coastguard Worker 	UL_GSHMEMLIM, -1, 0, 0},
150*49cdfc7eSAndroid Build Coastguard Worker #endif
151*49cdfc7eSAndroid Build Coastguard Worker };
152*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)153*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
154*49cdfc7eSAndroid Build Coastguard Worker {
155*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
156*49cdfc7eSAndroid Build Coastguard Worker 	int i;
157*49cdfc7eSAndroid Build Coastguard Worker 	int tmp;
158*49cdfc7eSAndroid Build Coastguard Worker 
159*49cdfc7eSAndroid Build Coastguard Worker 	TST_TOTAL = sizeof(Scenarios) / sizeof(struct limits_t);
160*49cdfc7eSAndroid Build Coastguard Worker 
161*49cdfc7eSAndroid Build Coastguard Worker     /***************************************************************
162*49cdfc7eSAndroid Build Coastguard Worker      * parse standard options
163*49cdfc7eSAndroid Build Coastguard Worker      ***************************************************************/
164*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
165*49cdfc7eSAndroid Build Coastguard Worker 
166*49cdfc7eSAndroid Build Coastguard Worker     /***************************************************************
167*49cdfc7eSAndroid Build Coastguard Worker      * perform global setup for test
168*49cdfc7eSAndroid Build Coastguard Worker      ***************************************************************/
169*49cdfc7eSAndroid Build Coastguard Worker 	setup();
170*49cdfc7eSAndroid Build Coastguard Worker 
171*49cdfc7eSAndroid Build Coastguard Worker     /***************************************************************
172*49cdfc7eSAndroid Build Coastguard Worker      * check looping state if -c option given
173*49cdfc7eSAndroid Build Coastguard Worker      ***************************************************************/
174*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
175*49cdfc7eSAndroid Build Coastguard Worker 
176*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
177*49cdfc7eSAndroid Build Coastguard Worker 
178*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < TST_TOTAL; i++) {
179*49cdfc7eSAndroid Build Coastguard Worker 
180*49cdfc7eSAndroid Build Coastguard Worker 			cmd = Scenarios[i].cmd;
181*49cdfc7eSAndroid Build Coastguard Worker 			limit = Scenarios[i].newlimit;
182*49cdfc7eSAndroid Build Coastguard Worker 
183*49cdfc7eSAndroid Build Coastguard Worker 			/*
184*49cdfc7eSAndroid Build Coastguard Worker 			 * Call ulimit(2)
185*49cdfc7eSAndroid Build Coastguard Worker 			 */
186*49cdfc7eSAndroid Build Coastguard Worker 			TEST(ulimit(cmd, limit));
187*49cdfc7eSAndroid Build Coastguard Worker 
188*49cdfc7eSAndroid Build Coastguard Worker 			/* check return code */
189*49cdfc7eSAndroid Build Coastguard Worker 			if (TEST_RETURN == -1) {
190*49cdfc7eSAndroid Build Coastguard Worker 				if (Scenarios[i].exp_fail) {
191*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TPASS | TTERRNO,
192*49cdfc7eSAndroid Build Coastguard Worker 						 "ulimit(%d, %ld) Failed expectedly",
193*49cdfc7eSAndroid Build Coastguard Worker 						 cmd, limit);
194*49cdfc7eSAndroid Build Coastguard Worker 				} else {
195*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TFAIL | TTERRNO,
196*49cdfc7eSAndroid Build Coastguard Worker 						 "ulimit(%d, %ld) Failed",
197*49cdfc7eSAndroid Build Coastguard Worker 						 cmd, limit);
198*49cdfc7eSAndroid Build Coastguard Worker 				}
199*49cdfc7eSAndroid Build Coastguard Worker 			} else {
200*49cdfc7eSAndroid Build Coastguard Worker 				if (Scenarios[i].exp_fail) {
201*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TFAIL,
202*49cdfc7eSAndroid Build Coastguard Worker 						 "ulimit(%d, %ld) returned %ld, succeeded unexpectedly",
203*49cdfc7eSAndroid Build Coastguard Worker 						 cmd, limit, TEST_RETURN);
204*49cdfc7eSAndroid Build Coastguard Worker 				} else {
205*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TPASS,
206*49cdfc7eSAndroid Build Coastguard Worker 						 "ulimit(%d, %ld) returned %ld",
207*49cdfc7eSAndroid Build Coastguard Worker 						 cmd, limit, TEST_RETURN);
208*49cdfc7eSAndroid Build Coastguard Worker 				}
209*49cdfc7eSAndroid Build Coastguard Worker 
210*49cdfc7eSAndroid Build Coastguard Worker 				/*
211*49cdfc7eSAndroid Build Coastguard Worker 				 * Save the UL_GETFSIZE return value in the newlimit field
212*49cdfc7eSAndroid Build Coastguard Worker 				 * for UL_SETFSIZE test cases.
213*49cdfc7eSAndroid Build Coastguard Worker 				 */
214*49cdfc7eSAndroid Build Coastguard Worker 				if (cmd == UL_GETFSIZE) {
215*49cdfc7eSAndroid Build Coastguard Worker 					for (tmp = i + 1; tmp < TST_TOTAL;
216*49cdfc7eSAndroid Build Coastguard Worker 					     tmp++) {
217*49cdfc7eSAndroid Build Coastguard Worker 						if (Scenarios[tmp].nlim_flag ==
218*49cdfc7eSAndroid Build Coastguard Worker 						    1) {
219*49cdfc7eSAndroid Build Coastguard Worker 							Scenarios[tmp].newlimit
220*49cdfc7eSAndroid Build Coastguard Worker 							    = TEST_RETURN;
221*49cdfc7eSAndroid Build Coastguard Worker 						}
222*49cdfc7eSAndroid Build Coastguard Worker 						if (Scenarios[tmp].nlim_flag ==
223*49cdfc7eSAndroid Build Coastguard Worker 						    2) {
224*49cdfc7eSAndroid Build Coastguard Worker 							Scenarios[tmp].newlimit
225*49cdfc7eSAndroid Build Coastguard Worker 							    = TEST_RETURN - 1;
226*49cdfc7eSAndroid Build Coastguard Worker 						}
227*49cdfc7eSAndroid Build Coastguard Worker 					}
228*49cdfc7eSAndroid Build Coastguard Worker 				}
229*49cdfc7eSAndroid Build Coastguard Worker 			}
230*49cdfc7eSAndroid Build Coastguard Worker 		}
231*49cdfc7eSAndroid Build Coastguard Worker 	}
232*49cdfc7eSAndroid Build Coastguard Worker 
233*49cdfc7eSAndroid Build Coastguard Worker     /***************************************************************
234*49cdfc7eSAndroid Build Coastguard Worker      * cleanup and exit
235*49cdfc7eSAndroid Build Coastguard Worker      ***************************************************************/
236*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
237*49cdfc7eSAndroid Build Coastguard Worker 
238*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
239*49cdfc7eSAndroid Build Coastguard Worker }
240*49cdfc7eSAndroid Build Coastguard Worker 
241*49cdfc7eSAndroid Build Coastguard Worker /***************************************************************
242*49cdfc7eSAndroid Build Coastguard Worker  * setup() - performs all ONE TIME setup for this test.
243*49cdfc7eSAndroid Build Coastguard Worker  ***************************************************************/
setup(void)244*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
245*49cdfc7eSAndroid Build Coastguard Worker {
246*49cdfc7eSAndroid Build Coastguard Worker 
247*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
248*49cdfc7eSAndroid Build Coastguard Worker 
249*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
250*49cdfc7eSAndroid Build Coastguard Worker 
251*49cdfc7eSAndroid Build Coastguard Worker }
252*49cdfc7eSAndroid Build Coastguard Worker 
253*49cdfc7eSAndroid Build Coastguard Worker /***************************************************************
254*49cdfc7eSAndroid Build Coastguard Worker  * cleanup() - performs all ONE TIME cleanup for this test at
255*49cdfc7eSAndroid Build Coastguard Worker  *		completion or premature exit.
256*49cdfc7eSAndroid Build Coastguard Worker  ***************************************************************/
cleanup(void)257*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
258*49cdfc7eSAndroid Build Coastguard Worker {
259*49cdfc7eSAndroid Build Coastguard Worker 
260*49cdfc7eSAndroid Build Coastguard Worker }
261