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