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/11/2002 Port to LTP dbarrera@us.ibm.com
20 */
21
22 /*
23 * Get and manipulate a message queue.
24 */
25
26 #define _XOPEN_SOURCE 500
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <sys/ipc.h>
30 #include <sys/msg.h>
31 #include <sys/wait.h>
32 #include <signal.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include "test.h"
39 #include "ipcmsg.h"
40 #include "../lib/libmsgctl.h"
41
42 char *TCID = "msgctl09";
43 int TST_TOTAL = 1;
44
45 #define MAXNREPS 1000
46 #ifndef CONFIG_COLDFIRE
47 #define MAXNPROCS 1000000 /* This value is set to an arbitrary high limit. */
48 #else
49 #define MAXNPROCS 100000 /* Coldfire can't deal with 1000000 */
50 #endif
51 #define MAXNKIDS 10
52
53 static key_t keyarray[MAXNPROCS];
54 static int pidarray[MAXNPROCS];
55 static int rkidarray[MAXNKIDS];
56 static int wkidarray[MAXNKIDS];
57 static int tid;
58 static int nprocs, nreps, nkids, MSGMNI;
59 static int procstat;
60
61 void setup(void);
62 void cleanup(void);
63
64 static void term(int);
65 static int dotest(key_t, int);
66 static void cleanup_msgqueue(int i, int tid);
67
68 #ifdef UCLINUX
69 static char *argv0;
70 static key_t key_uclinux;
71 static int i_uclinux;
72 static int pid_uclinux;
73 static int child_process_uclinux;
74 static int rkid_uclinux;
75
76
77 static void do_child_1_uclinux(void);
78 static void do_child_2_uclinux(void);
79 static void do_child_3_uclinux(void);
80 #endif
81
main(int argc,char ** argv)82 int main(int argc, char **argv)
83 {
84 int i, j, ok, pid;
85 int count, status;
86
87 #ifdef UCLINUX
88
89 argv0 = argv[0];
90
91 tst_parse_opts(argc, argv, NULL, NULL);
92
93 maybe_run_child(&do_child_1_uclinux, "ndd", 1, &key_uclinux,
94 &i_uclinux);
95 maybe_run_child(&do_child_2_uclinux, "nddd", 2, &key_uclinux,
96 &pid_uclinux, &child_process_uclinux);
97 maybe_run_child(&do_child_3_uclinux, "nddd", 3, &key_uclinux,
98 &rkid_uclinux, &child_process_uclinux);
99 #endif
100
101 setup();
102
103 if (argc == 1) {
104 /* Set default parameters */
105 nreps = MAXNREPS;
106 nprocs = MSGMNI;
107 nkids = MAXNKIDS;
108 } else if (argc == 4) {
109 if (atoi(argv[1]) > MAXNREPS) {
110 tst_resm(TCONF,
111 "Requested number of iterations too large, setting to Max. of %d",
112 MAXNREPS);
113 nreps = MAXNREPS;
114 } else {
115 nreps = atoi(argv[1]);
116 }
117 if (atoi(argv[2]) > MSGMNI) {
118 tst_resm(TCONF,
119 "Requested number of processes too large, setting to Max. of %d",
120 MSGMNI);
121 nprocs = MSGMNI;
122 } else {
123 nprocs = atoi(argv[2]);
124 }
125 if (atoi(argv[3]) > MAXNKIDS) {
126 tst_resm(TCONF,
127 "Requested number of read/write pairs too large; setting to Max. of %d",
128 MAXNKIDS);
129 nkids = MAXNKIDS;
130 } else {
131 nkids = atoi(argv[3]);
132 }
133 } else {
134 tst_brkm(TCONF,
135 NULL,
136 " Usage: %s [ number of iterations number of processes number of read/write pairs ]",
137 argv[0]);
138 }
139
140 procstat = 0;
141 srand48((unsigned)getpid() + (unsigned)(getppid() << 16));
142 tid = -1;
143
144 /* Setup signal handleing routine */
145 if (sigset(SIGTERM, term) == SIG_ERR) {
146 tst_brkm(TFAIL, NULL, "Sigset SIGTERM failed");
147 }
148 /* Set up array of unique keys for use in allocating message
149 * queues
150 */
151 for (i = 0; i < nprocs; i++) {
152 ok = 1;
153 do {
154 /* Get random key */
155 keyarray[i] = (key_t) lrand48();
156 /* Make sure key is unique and not private */
157 if (keyarray[i] == IPC_PRIVATE) {
158 ok = 0;
159 continue;
160 }
161 for (j = 0; j < i; j++) {
162 if (keyarray[j] == keyarray[i]) {
163 ok = 0;
164 break;
165 }
166 ok = 1;
167 }
168 } while (ok == 0);
169 }
170 /* Fork a number of processes (nprocs), each of which will
171 * create a message queue with several (nkids) reader/writer
172 * pairs which will read and write a number (iterations)
173 * of random length messages with specific values (keys).
174 */
175
176 for (i = 0; i < nprocs; i++) {
177 fflush(stdout);
178 if ((pid = FORK_OR_VFORK()) < 0) {
179 tst_brkm(TFAIL,
180 NULL,
181 "\tFork failed (may be OK if under stress)");
182 }
183 /* Child does this */
184 if (pid == 0) {
185 #ifdef UCLINUX
186 if (self_exec(argv[0], "ndd", 1, keyarray[i], i) < 0) {
187 tst_brkm(TFAIL, NULL, "\tself_exec failed");
188 }
189 #else
190 procstat = 1;
191 exit(dotest(keyarray[i], i));
192 #endif
193 }
194 pidarray[i] = pid;
195 }
196
197 count = 0;
198 while (1) {
199 if ((wait(&status)) > 0) {
200 if (status >> 8 != PASS) {
201 tst_brkm(TFAIL, NULL,
202 "Child exit status = %d",
203 status >> 8);
204 }
205 count++;
206 } else {
207 if (errno != EINTR) {
208 break;
209 }
210 #ifdef DEBUG
211 tst_resm(TINFO, "Signal detected during wait");
212 #endif
213 }
214 }
215 /* Make sure proper number of children exited */
216 if (count != nprocs) {
217 tst_brkm(TFAIL,
218 NULL,
219 "Wrong number of children exited, Saw %d, Expected %d",
220 count, nprocs);
221 }
222
223 tst_resm(TPASS, "msgctl09 ran successfully!");
224
225 cleanup();
226 tst_exit();
227 }
228
229 #ifdef UCLINUX
do_child_1_uclinux(void)230 static void do_child_1_uclinux(void)
231 {
232 procstat = 1;
233 exit(dotest(key_uclinux, i_uclinux));
234 }
235
do_child_2_uclinux(void)236 static void do_child_2_uclinux(void)
237 {
238 procstat = 2;
239 exit(doreader(key_uclinux, tid, pid_uclinux,
240 child_process_uclinux, nreps));
241 }
242
do_child_3_uclinux(void)243 static void do_child_3_uclinux(void)
244 {
245 procstat = 2;
246 exit(dowriter(key_uclinux, tid, rkid_uclinux,
247 child_process_uclinux, nreps));
248 }
249 #endif
250
cleanup_msgqueue(int i,int tid)251 static void cleanup_msgqueue(int i, int tid)
252 {
253 /*
254 * Decrease the value of i by 1 because it
255 * is getting incremented even if the fork
256 * is failing.
257 */
258
259 i--;
260 /*
261 * Kill all children & free message queue.
262 */
263 for (; i >= 0; i--) {
264 (void)kill(rkidarray[i], SIGKILL);
265 (void)kill(wkidarray[i], SIGKILL);
266 }
267
268 if (msgctl(tid, IPC_RMID, 0) < 0) {
269 tst_brkm(TFAIL | TERRNO, NULL, "Msgctl error in cleanup");
270 }
271 }
272
dotest(key_t key,int child_process)273 static int dotest(key_t key, int child_process)
274 {
275 int id, pid;
276 int i, count, status, exit_status;
277
278 sighold(SIGTERM);
279 if ((id = msgget(key, IPC_CREAT | S_IRUSR | S_IWUSR)) < 0) {
280 printf("msgget() error in child %d: %s\n",
281 child_process, strerror(errno));
282 return FAIL;
283 }
284 tid = id;
285 sigrelse(SIGTERM);
286
287 exit_status = PASS;
288
289 for (i = 0; i < nkids; i++) {
290 fflush(stdout);
291 if ((pid = FORK_OR_VFORK()) < 0) {
292 printf("Fork failure in the first child of child group %d\n",
293 child_process);
294 cleanup_msgqueue(i, tid);
295 return FAIL;
296 }
297 /* First child does this */
298 if (pid == 0) {
299 #ifdef UCLINUX
300 if (self_exec(argv0, "nddd", 2, key, getpid(),
301 child_process) < 0) {
302 printf("self_exec failed\n");
303 cleanup_msgqueue(i, tid);
304 return FAIL;
305 }
306 #else
307 procstat = 2;
308 exit(doreader(key, tid, getpid(),
309 child_process, nreps));
310 #endif
311 }
312 rkidarray[i] = pid;
313 fflush(stdout);
314 if ((pid = FORK_OR_VFORK()) < 0) {
315 printf("Fork failure in the second child of child group %d\n",
316 child_process);
317 /*
318 * Kill the reader child process
319 */
320 (void)kill(rkidarray[i], SIGKILL);
321
322 cleanup_msgqueue(i, tid);
323 return FAIL;
324 }
325 /* Second child does this */
326 if (pid == 0) {
327 #ifdef UCLINUX
328 if (self_exec(argv0, "nddd", 3, key, rkidarray[i],
329 child_process) < 0) {
330 printf("\tFork failure in the first child of child group %d\n",
331 child_process);
332 /*
333 * Kill the reader child process
334 */
335 (void)kill(rkidarray[i], SIGKILL);
336
337 cleanup_msgqueue(i, tid);
338 return FAIL;
339 }
340 #else
341 procstat = 2;
342 exit(dowriter(key, tid, rkidarray[i],
343 child_process, nreps));
344 #endif
345 }
346 wkidarray[i] = pid;
347 }
348 /* Parent does this */
349 count = 0;
350 while (1) {
351 if ((wait(&status)) > 0) {
352 if (status >> 8 != PASS) {
353 printf("Child exit status = %d from child group %d\n",
354 status >> 8, child_process);
355 for (i = 0; i < nkids; i++) {
356 kill(rkidarray[i], SIGTERM);
357 kill(wkidarray[i], SIGTERM);
358 }
359 if (msgctl(tid, IPC_RMID, 0) < 0) {
360 printf("msgctl() error: %s\n",
361 strerror(errno));
362 }
363 return FAIL;
364 }
365 count++;
366 } else {
367 if (errno != EINTR) {
368 break;
369 }
370 }
371 }
372 /* Make sure proper number of children exited */
373 if (count != (nkids * 2)) {
374 printf("Wrong number of children exited in child group %d, saw %d, expected %d\n",
375 child_process, count, (nkids * 2));
376 if (msgctl(tid, IPC_RMID, 0) < 0) {
377 printf("msgctl() error: %s\n", strerror(errno));
378 }
379 return FAIL;
380 }
381 if (msgctl(id, IPC_RMID, 0) < 0) {
382 printf("msgctl() failure in child group %d: %s\n",
383 child_process, strerror(errno));
384 return FAIL;
385 }
386 return exit_status;
387 }
388
389 /* ARGSUSED */
term(int sig LTP_ATTRIBUTE_UNUSED)390 static void term(int sig LTP_ATTRIBUTE_UNUSED)
391 {
392 int i;
393
394 if (procstat == 0) {
395 #ifdef DEBUG
396 tst_resm(TINFO, "SIGTERM signal received, test killing kids");
397 #endif
398 for (i = 0; i < nprocs; i++) {
399 if (pidarray[i] > 0) {
400 if (kill(pidarray[i], SIGTERM) < 0) {
401 printf("Kill failed to kill child %d",
402 i);
403 exit(FAIL);
404 }
405 }
406 }
407 return;
408 }
409
410 if (procstat == 2) {
411 fflush(stdout);
412 exit(PASS);
413 }
414
415 if (tid == -1) {
416 exit(FAIL);
417 }
418 for (i = 0; i < nkids; i++) {
419 if (rkidarray[i] > 0)
420 kill(rkidarray[i], SIGTERM);
421 if (wkidarray[i] > 0)
422 kill(wkidarray[i], SIGTERM);
423 }
424 }
425
setup(void)426 void setup(void)
427 {
428 int nr_msgqs;
429
430 tst_tmpdir();
431
432 tst_sig(FORK, DEF_HANDLER, cleanup);
433
434 TEST_PAUSE;
435
436 nr_msgqs = get_max_msgqueues();
437 if (nr_msgqs < 0)
438 cleanup();
439
440 nr_msgqs -= get_used_msgqueues();
441 if (nr_msgqs <= 0) {
442 tst_resm(TBROK,
443 "Max number of message queues already used, cannot create more.");
444 cleanup();
445 }
446
447 /*
448 * Since msgmni scales to the memory size, it may reach huge values
449 * that are not necessary for this test.
450 * That's why we define NR_MSGQUEUES as a high boundary for it.
451 */
452 MSGMNI = min(nr_msgqs, NR_MSGQUEUES);
453 }
454
cleanup(void)455 void cleanup(void)
456 {
457 int status;
458
459 #ifdef DEBUG
460 tst_resm(TINFO, "Removing the message queue");
461 #endif
462 fflush(stdout);
463 (void)msgctl(tid, IPC_RMID, NULL);
464 if ((status = msgctl(tid, IPC_STAT, NULL)) != -1) {
465 (void)msgctl(tid, IPC_RMID, NULL);
466 tst_resm(TFAIL, "msgctl(tid, IPC_RMID) failed");
467
468 }
469
470 fflush(stdout);
471 tst_rmdir();
472 }
473