• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *	shmget03.c
23  *
24  * DESCRIPTION
25  *	shmget03 - test for ENOSPC error
26  *
27  * ALGORITHM
28  *	create shared memory segments in a loop until reaching the system limit
29  *	loop if that option was specified
30  *	  attempt to create yet another shared memory segment
31  *	  check the errno value
32  *	    issue a PASS message if we get ENOSPC
33  *	  otherwise, the tests fails
34  *	    issue a FAIL message
35  *	call cleanup
36  *
37  * USAGE:  <for command-line>
38  *  shmget03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
39  *     where,  -c n : Run n copies concurrently.
40  *             -e   : Turn on errno logging.
41  *	       -i n : Execute test n times.
42  *	       -I x : Execute test for x seconds.
43  *	       -P x : Pause for x seconds between iterations.
44  *	       -t   : Turn on syscall timing.
45  *
46  * HISTORY
47  *	03/2001 - Written by Wayne Boyer
48  *
49  * RESTRICTIONS
50  *	none
51  */
52 
53 #include "ipcshm.h"
54 
55 char *TCID = "shmget03";
56 int TST_TOTAL = 1;
57 
58 /*
59  * The MAXIDS value is somewhat arbitrary and may need to be increased
60  * depending on the system being tested.
61  */
62 #define MAXIDS	8192
63 
64 int shm_id_1 = -1;
65 int num_shms = 0;
66 
67 int shm_id_arr[MAXIDS];
68 
main(int ac,char ** av)69 int main(int ac, char **av)
70 {
71 	int lc;
72 
73 	tst_parse_opts(ac, av, NULL, NULL);
74 
75 	setup();		/* global setup */
76 
77 	/* The following loop checks looping state if -i option given */
78 
79 	for (lc = 0; TEST_LOOPING(lc); lc++) {
80 		/* reset tst_count in case we are looping */
81 		tst_count = 0;
82 
83 		/*
84 		 * use the TEST() macro to make the call
85 		 */
86 
87 		TEST(shmget(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | IPC_EXCL
88 			    | SHM_RW));
89 
90 		if (TEST_RETURN != -1) {
91 			tst_resm(TFAIL, "call succeeded when error expected");
92 			continue;
93 		}
94 
95 		switch (TEST_ERRNO) {
96 		case ENOSPC:
97 			tst_resm(TPASS, "expected failure - errno = "
98 				 "%d : %s", TEST_ERRNO, strerror(TEST_ERRNO));
99 			break;
100 		default:
101 			tst_resm(TFAIL, "call failed with an "
102 				 "unexpected error - %d : %s",
103 				 TEST_ERRNO, strerror(TEST_ERRNO));
104 			break;
105 		}
106 	}
107 
108 	cleanup();
109 
110 	tst_exit();
111 }
112 
113 /*
114  * setup() - performs all the ONE TIME setup for this test.
115  */
setup(void)116 void setup(void)
117 {
118 
119 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
120 
121 	TEST_PAUSE;
122 
123 	/*
124 	 * Create a temporary directory and cd into it.
125 	 * This helps to ensure that a unique msgkey is created.
126 	 * See ../lib/libipc.c for more information.
127 	 */
128 	tst_tmpdir();
129 
130 	/* get an IPC resource key */
131 	shmkey = getipckey();
132 
133 	/*
134 	 * Use a while loop to create the maximum number of memory segments.
135 	 * If the loop exceeds MAXIDS, then break the test and cleanup.
136 	 */
137 	while ((shm_id_1 = shmget(IPC_PRIVATE, SHM_SIZE, IPC_CREAT |
138 				  IPC_EXCL | SHM_RW)) != -1) {
139 		shm_id_arr[num_shms++] = shm_id_1;
140 		if (num_shms == MAXIDS) {
141 			tst_brkm(TBROK, cleanup, "The maximum number of shared "
142 				 "memory ID's has been\n\t reached.  Please "
143 				 "increase the MAXIDS value in the test.");
144 		}
145 	}
146 
147 	/*
148 	 * If the errno is other than ENOSPC, then something else is wrong.
149 	 */
150 	if (errno != ENOSPC) {
151 		tst_resm(TINFO, "errno = %d : %s", errno, strerror(errno));
152 		tst_brkm(TBROK, cleanup, "Didn't get ENOSPC in test setup");
153 	}
154 }
155 
156 /*
157  * cleanup() - performs all the ONE TIME cleanup for this test at completion
158  * 	       or premature exit.
159  */
cleanup(void)160 void cleanup(void)
161 {
162 	int i;
163 
164 	/* remove the shared memory resources that were created */
165 	for (i = 0; i < num_shms; i++) {
166 		rm_shm(shm_id_arr[i]);
167 	}
168 
169 	tst_rmdir();
170 
171 }
172