• 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 	/* Each forked child forks once, take it into account here. */
114 	if (nprocs * 2 >= free_pids) {
115 		tst_resm(TINFO,
116 			 "Requested number of processes higher than limit (%d > %d), "
117 			 "setting to %d", nprocs * 2, free_pids, free_pids);
118 		nprocs = free_pids / 2;
119 	}
120 
121 	srand(getpid());
122 	tid = -1;
123 
124 	/* Setup signal handling routine */
125 	memset(&act, 0, sizeof(act));
126 	act.sa_handler = sig_handler;
127 	sigemptyset(&act.sa_mask);
128 	sigaddset(&act.sa_mask, SIGTERM);
129 	if (sigaction(SIGTERM, &act, NULL) < 0) {
130 		tst_brkm(TFAIL, NULL, "Sigset SIGTERM failed");
131 	}
132 	/* Set up array of unique keys for use in allocating message
133 	 * queues
134 	 */
135 	for (i = 0; i < nprocs; i++) {
136 		ok = 1;
137 		do {
138 			/* Get random key */
139 			keyarray[i] = (key_t) rand();
140 			/* Make sure key is unique and not private */
141 			if (keyarray[i] == IPC_PRIVATE) {
142 				ok = 0;
143 				continue;
144 			}
145 			for (j = 0; j < i; j++) {
146 				if (keyarray[j] == keyarray[i]) {
147 					ok = 0;
148 					break;
149 				}
150 				ok = 1;
151 			}
152 		} while (ok == 0);
153 	}
154 
155 	/* Fork a number of processes, each of which will
156 	 * create a message queue with one reader/writer
157 	 * pair which will read and write a number (iterations)
158 	 * of random length messages with specific values.
159 	 */
160 
161 	for (i = 0; i < nprocs; i++) {
162 		fflush(stdout);
163 		if ((pid = FORK_OR_VFORK()) < 0) {
164 			tst_brkm(TFAIL,
165 				 NULL,
166 				 "\tFork failed (may be OK if under stress)");
167 		}
168 		/* Child does this */
169 		if (pid == 0) {
170 			procstat = 1;
171 			exit(dotest(keyarray[i], i));
172 		}
173 		pidarray[i] = pid;
174 	}
175 
176 	count = 0;
177 	while (1) {
178 		if ((wait(&status)) > 0) {
179 			if (status >> 8 != 0) {
180 				tst_brkm(TFAIL, NULL,
181 					 "Child exit status = %d",
182 					 status >> 8);
183 			}
184 			count++;
185 		} else {
186 			if (errno != EINTR) {
187 				break;
188 			}
189 #ifdef DEBUG
190 			tst_resm(TINFO, "Signal detected during wait");
191 #endif
192 		}
193 	}
194 	/* Make sure proper number of children exited */
195 	if (count != nprocs) {
196 		tst_brkm(TFAIL,
197 			 NULL,
198 			 "Wrong number of children exited, Saw %d, Expected %d",
199 			 count, nprocs);
200 	}
201 
202 	tst_resm(TPASS, "Test ran successfully!");
203 
204 	cleanup();
205 	tst_exit();
206 }
207 
dotest(key_t key,int child_process)208 static int dotest(key_t key, int child_process)
209 {
210 	int id, pid;
211 	int ret, status;
212 
213 	sighold(SIGTERM);
214 	TEST(msgget(key, IPC_CREAT | S_IRUSR | S_IWUSR));
215 	if (TEST_RETURN < 0) {
216 		printf("msgget() error in child %d: %s\n",
217 			child_process, strerror(TEST_ERRNO));
218 		return FAIL;
219 	}
220 	tid = id = TEST_RETURN;
221 	sigrelse(SIGTERM);
222 
223 	fflush(stdout);
224 	if ((pid = FORK_OR_VFORK()) < 0) {
225 		printf("Fork failed (may be OK if under stress)\n");
226 		TEST(msgctl(tid, IPC_RMID, 0));
227 		if (TEST_RETURN < 0) {
228 			printf("msgctl() error in cleanup: %s\n",
229 				strerror(TEST_ERRNO));
230 		}
231 		return FAIL;
232 	}
233 	if (pid == 0)
234 		exit(doreader(key, id, 1, child_process, nreps));
235 
236 	mykid = pid;
237 	procstat = 2;
238 	ret = dowriter(key, id, 1, child_process, nreps);
239 	wait(&status);
240 
241 	if (ret != PASS)
242 		exit(FAIL);
243 
244 	if ((!WIFEXITED(status) || (WEXITSTATUS(status) != PASS)))
245 		exit(FAIL);
246 
247 	TEST(msgctl(id, IPC_RMID, 0));
248 	if (TEST_RETURN < 0) {
249 		printf("msgctl() failed: %s\n",
250 			strerror(TEST_ERRNO));
251 		return FAIL;
252 	}
253 	return PASS;
254 }
255 
sig_handler(int signo LTP_ATTRIBUTE_UNUSED)256 static void sig_handler(int signo LTP_ATTRIBUTE_UNUSED)
257 {
258 }
259 
setup(void)260 void setup(void)
261 {
262 	int nr_msgqs;
263 
264 	tst_tmpdir();
265 
266 	tst_sig(FORK, DEF_HANDLER, cleanup);
267 
268 	TEST_PAUSE;
269 
270 	nr_msgqs = get_max_msgqueues();
271 	if (nr_msgqs < 0)
272 		cleanup();
273 
274 	MSGMNI = nr_msgqs - get_used_msgqueues();
275 	if (MSGMNI > MAXNPROCS)
276 		MSGMNI = MAXNPROCS;
277 	if (MSGMNI <= 0) {
278 		tst_resm(TBROK,
279 			 "Max number of message queues already used, cannot create more.");
280 		cleanup();
281 	}
282 }
283 
cleanup(void)284 void cleanup(void)
285 {
286 	int status;
287 
288 #ifdef DEBUG
289 	tst_resm(TINFO, "Removing the message queue");
290 #endif
291 	(void)msgctl(tid, IPC_RMID, NULL);
292 	if ((status = msgctl(tid, IPC_STAT, NULL)) != -1) {
293 		(void)msgctl(tid, IPC_RMID, NULL);
294 		tst_resm(TFAIL, "msgctl(tid, IPC_RMID) failed");
295 
296 	}
297 
298 	tst_rmdir();
299 }
300