• 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 msgctl08 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 "../lib/libmsgctl.h"
44 
45 char *TCID = "msgctl10";
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 #ifdef UCLINUX
65 static char *argv0;
66 static key_t key_uclinux;
67 static int i_uclinux;
68 static int id_uclinux;
69 static int child_process_uclinux;
70 
71 static void do_child_1_uclinux(void);
72 static void do_child_2_uclinux(void);
73 #endif
74 
main(int argc,char ** argv)75 int main(int argc, char **argv)
76 {
77 	int i, j, ok, pid;
78 	int count, status;
79 	struct sigaction act;
80 
81 #ifdef UCLINUX
82 
83 	argv0 = argv[0];
84 
85 	tst_parse_opts(argc, argv, NULL, NULL);
86 
87 	maybe_run_child(&do_child_1_uclinux, "ndd", 1, &key_uclinux,
88 			&i_uclinux);
89 	maybe_run_child(&do_child_2_uclinux, "nddd", 2, &id_uclinux,
90 			&key_uclinux, &child_process_uclinux);
91 #endif
92 
93 	setup();
94 
95 	if (argc == 1) {
96 		/* Set default parameters */
97 		nreps = MAXNREPS;
98 		nprocs = MSGMNI;
99 	} else if (argc == 3) {
100 		if (atoi(argv[1]) > MAXNREPS) {
101 			tst_resm(TCONF,
102 				 "Requested number of iterations too large, setting to Max. of %d",
103 				 MAXNREPS);
104 			nreps = MAXNREPS;
105 		} else {
106 			nreps = atoi(argv[1]);
107 		}
108 		if (atoi(argv[2]) > MSGMNI) {
109 			tst_resm(TCONF,
110 				 "Requested number of processes too large, setting to Max. of %d",
111 				 MSGMNI);
112 			nprocs = MSGMNI;
113 		} else {
114 			nprocs = atoi(argv[2]);
115 		}
116 	} else {
117 		tst_brkm(TCONF,
118 			 NULL,
119 			 " Usage: %s [ number of iterations  number of processes ]",
120 			 argv[0]);
121 	}
122 
123 	srand(getpid());
124 	tid = -1;
125 
126 	/* Setup signal handling routine */
127 	memset(&act, 0, sizeof(act));
128 	act.sa_handler = sig_handler;
129 	sigemptyset(&act.sa_mask);
130 	sigaddset(&act.sa_mask, SIGTERM);
131 	if (sigaction(SIGTERM, &act, NULL) < 0) {
132 		tst_brkm(TFAIL, NULL, "Sigset SIGTERM failed");
133 	}
134 	/* Set up array of unique keys for use in allocating message
135 	 * queues
136 	 */
137 	for (i = 0; i < nprocs; i++) {
138 		ok = 1;
139 		do {
140 			/* Get random key */
141 			keyarray[i] = (key_t) rand();
142 			/* Make sure key is unique and not private */
143 			if (keyarray[i] == IPC_PRIVATE) {
144 				ok = 0;
145 				continue;
146 			}
147 			for (j = 0; j < i; j++) {
148 				if (keyarray[j] == keyarray[i]) {
149 					ok = 0;
150 					break;
151 				}
152 				ok = 1;
153 			}
154 		} while (ok == 0);
155 	}
156 
157 	/* Fork a number of processes, each of which will
158 	 * create a message queue with one reader/writer
159 	 * pair which will read and write a number (iterations)
160 	 * of random length messages with specific values.
161 	 */
162 
163 	for (i = 0; i < nprocs; i++) {
164 		fflush(stdout);
165 		if ((pid = FORK_OR_VFORK()) < 0) {
166 			tst_brkm(TFAIL,
167 				 NULL,
168 				 "\tFork failed (may be OK if under stress)");
169 		}
170 		/* Child does this */
171 		if (pid == 0) {
172 #ifdef UCLINUX
173 			if (self_exec(argv[0], "ndd", 1, keyarray[i], i) < 0)
174 				tst_brkm(TFAIL, NULL, "\tself_exec failed");
175 #else
176 			procstat = 1;
177 			exit(dotest(keyarray[i], i));
178 #endif
179 		}
180 		pidarray[i] = pid;
181 	}
182 
183 	count = 0;
184 	while (1) {
185 		if ((wait(&status)) > 0) {
186 			if (status >> 8 != 0) {
187 				tst_brkm(TFAIL, NULL,
188 					 "Child exit status = %d",
189 					 status >> 8);
190 			}
191 			count++;
192 		} else {
193 			if (errno != EINTR) {
194 				break;
195 			}
196 #ifdef DEBUG
197 			tst_resm(TINFO, "Signal detected during wait");
198 #endif
199 		}
200 	}
201 	/* Make sure proper number of children exited */
202 	if (count != nprocs) {
203 		tst_brkm(TFAIL,
204 			 NULL,
205 			 "Wrong number of children exited, Saw %d, Expected %d",
206 			 count, nprocs);
207 	}
208 
209 	tst_resm(TPASS, "msgctl10 ran successfully!");
210 
211 	cleanup();
212 	tst_exit();
213 }
214 
215 #ifdef UCLINUX
do_child_1_uclinux(void)216 static void do_child_1_uclinux(void)
217 {
218 	procstat = 1;
219 	exit(dotest(key_uclinux, i_uclinux));
220 }
221 
do_child_2_uclinux(void)222 static void do_child_2_uclinux(void)
223 {
224 	exit(doreader(key_uclinux, id_uclinux, 1,
225 			child_process_uclinux, nreps));
226 }
227 #endif
228 
dotest(key_t key,int child_process)229 static int dotest(key_t key, int child_process)
230 {
231 	int id, pid;
232 	int ret, status;
233 
234 	sighold(SIGTERM);
235 	TEST(msgget(key, IPC_CREAT | S_IRUSR | S_IWUSR));
236 	if (TEST_RETURN < 0) {
237 		printf("msgget() error in child %d: %s\n",
238 			child_process, strerror(TEST_ERRNO));
239 		return FAIL;
240 	}
241 	tid = id = TEST_RETURN;
242 	sigrelse(SIGTERM);
243 
244 	fflush(stdout);
245 	if ((pid = FORK_OR_VFORK()) < 0) {
246 		printf("Fork failed (may be OK if under stress)\n");
247 		TEST(msgctl(tid, IPC_RMID, 0));
248 		if (TEST_RETURN < 0) {
249 			printf("msgctl() error in cleanup: %s\n",
250 				strerror(TEST_ERRNO));
251 		}
252 		return FAIL;
253 	}
254 	/* Child does this */
255 	if (pid == 0) {
256 #ifdef UCLINUX
257 		if (self_exec(argv0, "nddd", 2, id, key, child_process) < 0) {
258 			printf("self_exec failed\n");
259 			TEST(msgctl(tid, IPC_RMID, 0));
260 			if (TEST_RETURN < 0) {
261 				printf("msgctl() error in cleanup: %s\n",
262 					strerror(TEST_ERRNO));
263 			}
264 			return FAIL;
265 		}
266 #else
267 		exit(doreader(key, id, 1, child_process, nreps));
268 #endif
269 	}
270 	/* Parent does this */
271 	mykid = pid;
272 	procstat = 2;
273 	ret = dowriter(key, id, 1, child_process, nreps);
274 	wait(&status);
275 
276 	if (ret != PASS)
277 		exit(FAIL);
278 
279 	if ((!WIFEXITED(status) || (WEXITSTATUS(status) != PASS)))
280 		exit(FAIL);
281 
282 	TEST(msgctl(id, IPC_RMID, 0));
283 	if (TEST_RETURN < 0) {
284 		printf("msgctl() failed: %s\n",
285 			strerror(TEST_ERRNO));
286 		return FAIL;
287 	}
288 	return PASS;
289 }
290 
sig_handler(int signo LTP_ATTRIBUTE_UNUSED)291 static void sig_handler(int signo LTP_ATTRIBUTE_UNUSED)
292 {
293 }
294 
setup(void)295 void setup(void)
296 {
297 	int nr_msgqs;
298 
299 	tst_tmpdir();
300 
301 	tst_sig(FORK, DEF_HANDLER, cleanup);
302 
303 	TEST_PAUSE;
304 
305 	nr_msgqs = get_max_msgqueues();
306 	if (nr_msgqs < 0)
307 		cleanup();
308 
309 	MSGMNI = nr_msgqs - get_used_msgqueues();
310 	if (MSGMNI > MAXNPROCS)
311 		MSGMNI = MAXNPROCS;
312 	if (MSGMNI <= 0) {
313 		tst_resm(TBROK,
314 			 "Max number of message queues already used, cannot create more.");
315 		cleanup();
316 	}
317 }
318 
cleanup(void)319 void cleanup(void)
320 {
321 	int status;
322 
323 #ifdef DEBUG
324 	tst_resm(TINFO, "Removing the message queue");
325 #endif
326 	fflush(stdout);
327 	(void)msgctl(tid, IPC_RMID, NULL);
328 	if ((status = msgctl(tid, IPC_STAT, NULL)) != -1) {
329 		(void)msgctl(tid, IPC_RMID, NULL);
330 		tst_resm(TFAIL, "msgctl(tid, IPC_RMID) failed");
331 
332 	}
333 
334 	fflush(stdout);
335 
336 	tst_rmdir();
337 }
338