• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) International Business Machines  Corp., 2002
3  *
4  * This program is free software;  you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
12  * the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program;  if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * 06/30/2001   Port to Linux   nsharoff@us.ibm.com
19  * 11/06/2002   Port to LTP     dbarrera@us.ibm.com
20  */
21 
22 /*
23  * Get and manipulate a message queue.
24  * Same as msgstress01 but gets the actual msgmni value under procfs.
25  */
26 
27 #define _XOPEN_SOURCE 500
28 #include <signal.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <values.h>
36 #include <sys/types.h>
37 #include <sys/wait.h>
38 #include <sys/stat.h>
39 #include <sys/ipc.h>
40 #include <sys/msg.h>
41 #include "test.h"
42 #include "ipcmsg.h"
43 #include "libmsgctl.h"
44 
45 char *TCID = "msgstress03";
46 int TST_TOTAL = 1;
47 
48 #define MAXNPROCS	10000	/*These should be sufficient */
49 #define MAXNREPS	10000	/*Else they srewup the system un-necessarily */
50 
51 static key_t keyarray[MAXNPROCS];
52 static int pidarray[MAXNPROCS];
53 static int tid;
54 static int MSGMNI, nprocs, nreps;
55 static int procstat;
56 static int mykid;
57 
58 void setup(void);
59 void cleanup(void);
60 
61 static int dotest(key_t key, int child_process);
62 static void sig_handler(int signo);
63 
64 static char *opt_nprocs;
65 static char *opt_nreps;
66 
67 static option_t options[] = {
68 	{"n:", NULL, &opt_nprocs},
69 	{"l:", NULL, &opt_nreps},
70 	{NULL, NULL, NULL},
71 };
72 
usage(void)73 static void usage(void)
74 {
75 	printf("  -n      Number of processes\n");
76 	printf("  -l      Number of iterations\n");
77 }
78 
main(int argc,char ** argv)79 int main(int argc, char **argv)
80 {
81 	int i, j, ok, pid, free_pids;
82 	int count, status;
83 	struct sigaction act;
84 
85 	tst_parse_opts(argc, argv, options, usage);
86 
87 	setup();
88 
89 	nreps = MAXNREPS;
90 	nprocs = MSGMNI;
91 
92 	if (opt_nreps) {
93 		nreps = atoi(opt_nreps);
94 		if (nreps > MAXNREPS) {
95 			tst_resm(TINFO,
96 				 "Requested number of iterations too large, "
97 				 "setting to Max. of %d", MAXNREPS);
98 			nreps = MAXNREPS;
99 		}
100 	}
101 
102 	if (opt_nprocs) {
103 		nprocs = atoi(opt_nprocs);
104 		if (nprocs > MSGMNI) {
105 			tst_resm(TINFO,
106 				 "Requested number of processes too large, "
107 				 "setting to Max. of %d", MSGMNI);
108 			nprocs = MSGMNI;
109 		}
110 	}
111 
112 	free_pids = tst_get_free_pids(cleanup);
113 	if (nprocs >= free_pids) {
114 		tst_resm(TINFO,
115 			 "Requested number of processes higher than limit (%d > %d), "
116 			 "setting to %d", nprocs, free_pids, free_pids);
117 		nprocs = free_pids;
118 	}
119 
120 	srand(getpid());
121 	tid = -1;
122 
123 	/* Setup signal handling routine */
124 	memset(&act, 0, sizeof(act));
125 	act.sa_handler = sig_handler;
126 	sigemptyset(&act.sa_mask);
127 	sigaddset(&act.sa_mask, SIGTERM);
128 	if (sigaction(SIGTERM, &act, NULL) < 0) {
129 		tst_brkm(TFAIL, NULL, "Sigset SIGTERM failed");
130 	}
131 	/* Set up array of unique keys for use in allocating message
132 	 * queues
133 	 */
134 	for (i = 0; i < nprocs; i++) {
135 		ok = 1;
136 		do {
137 			/* Get random key */
138 			keyarray[i] = (key_t) rand();
139 			/* Make sure key is unique and not private */
140 			if (keyarray[i] == IPC_PRIVATE) {
141 				ok = 0;
142 				continue;
143 			}
144 			for (j = 0; j < i; j++) {
145 				if (keyarray[j] == keyarray[i]) {
146 					ok = 0;
147 					break;
148 				}
149 				ok = 1;
150 			}
151 		} while (ok == 0);
152 	}
153 
154 	/* Fork a number of processes, each of which will
155 	 * create a message queue with one reader/writer
156 	 * pair which will read and write a number (iterations)
157 	 * of random length messages with specific values.
158 	 */
159 
160 	for (i = 0; i < nprocs; i++) {
161 		fflush(stdout);
162 		if ((pid = FORK_OR_VFORK()) < 0) {
163 			tst_brkm(TFAIL,
164 				 NULL,
165 				 "\tFork failed (may be OK if under stress)");
166 		}
167 		/* Child does this */
168 		if (pid == 0) {
169 			procstat = 1;
170 			exit(dotest(keyarray[i], i));
171 		}
172 		pidarray[i] = pid;
173 	}
174 
175 	count = 0;
176 	while (1) {
177 		if ((wait(&status)) > 0) {
178 			if (status >> 8 != 0) {
179 				tst_brkm(TFAIL, NULL,
180 					 "Child exit status = %d",
181 					 status >> 8);
182 			}
183 			count++;
184 		} else {
185 			if (errno != EINTR) {
186 				break;
187 			}
188 #ifdef DEBUG
189 			tst_resm(TINFO, "Signal detected during wait");
190 #endif
191 		}
192 	}
193 	/* Make sure proper number of children exited */
194 	if (count != nprocs) {
195 		tst_brkm(TFAIL,
196 			 NULL,
197 			 "Wrong number of children exited, Saw %d, Expected %d",
198 			 count, nprocs);
199 	}
200 
201 	tst_resm(TPASS, "Test ran successfully!");
202 
203 	cleanup();
204 	tst_exit();
205 }
206 
dotest(key_t key,int child_process)207 static int dotest(key_t key, int child_process)
208 {
209 	int id, pid;
210 	int ret, status;
211 
212 	sighold(SIGTERM);
213 	TEST(msgget(key, IPC_CREAT | S_IRUSR | S_IWUSR));
214 	if (TEST_RETURN < 0) {
215 		printf("msgget() error in child %d: %s\n",
216 			child_process, strerror(TEST_ERRNO));
217 		return FAIL;
218 	}
219 	tid = id = TEST_RETURN;
220 	sigrelse(SIGTERM);
221 
222 	fflush(stdout);
223 	if ((pid = FORK_OR_VFORK()) < 0) {
224 		printf("Fork failed (may be OK if under stress)\n");
225 		TEST(msgctl(tid, IPC_RMID, 0));
226 		if (TEST_RETURN < 0) {
227 			printf("msgctl() error in cleanup: %s\n",
228 				strerror(TEST_ERRNO));
229 		}
230 		return FAIL;
231 	}
232 	if (pid == 0)
233 		exit(doreader(key, id, 1, child_process, nreps));
234 
235 	mykid = pid;
236 	procstat = 2;
237 	ret = dowriter(key, id, 1, child_process, nreps);
238 	wait(&status);
239 
240 	if (ret != PASS)
241 		exit(FAIL);
242 
243 	if ((!WIFEXITED(status) || (WEXITSTATUS(status) != PASS)))
244 		exit(FAIL);
245 
246 	TEST(msgctl(id, IPC_RMID, 0));
247 	if (TEST_RETURN < 0) {
248 		printf("msgctl() failed: %s\n",
249 			strerror(TEST_ERRNO));
250 		return FAIL;
251 	}
252 	return PASS;
253 }
254 
sig_handler(int signo LTP_ATTRIBUTE_UNUSED)255 static void sig_handler(int signo LTP_ATTRIBUTE_UNUSED)
256 {
257 }
258 
setup(void)259 void setup(void)
260 {
261 	int nr_msgqs;
262 
263 	tst_tmpdir();
264 
265 	tst_sig(FORK, DEF_HANDLER, cleanup);
266 
267 	TEST_PAUSE;
268 
269 	nr_msgqs = get_max_msgqueues();
270 	if (nr_msgqs < 0)
271 		cleanup();
272 
273 	MSGMNI = nr_msgqs - get_used_msgqueues();
274 	if (MSGMNI > MAXNPROCS)
275 		MSGMNI = MAXNPROCS;
276 	if (MSGMNI <= 0) {
277 		tst_resm(TBROK,
278 			 "Max number of message queues already used, cannot create more.");
279 		cleanup();
280 	}
281 }
282 
cleanup(void)283 void cleanup(void)
284 {
285 	int status;
286 
287 #ifdef DEBUG
288 	tst_resm(TINFO, "Removing the message queue");
289 #endif
290 	(void)msgctl(tid, IPC_RMID, NULL);
291 	if ((status = msgctl(tid, IPC_STAT, NULL)) != -1) {
292 		(void)msgctl(tid, IPC_RMID, NULL);
293 		tst_resm(TFAIL, "msgctl(tid, IPC_RMID) failed");
294 
295 	}
296 
297 	tst_rmdir();
298 }
299