1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2001
4  *
5  *   This program is free software;  you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *   the GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program;  if not, write to the Free Software
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /*
21  * NAME
22  *	msgrcv01.c
23  *
24  * DESCRIPTION
25  *	msgrcv01 - test that msgrcv() receives the expected message
26  *
27  * ALGORITHM
28  *	create a message queue
29  *	initialize a message buffer with a known message and type
30  *	loop if that option was specified
31  *	fork a child to receive the message
32  *	parent enqueues the message then exits
33  *	check the return code
34  *	  if failure, issue a FAIL message.
35  *	otherwise,
36  *	  if doing functionality testing
37  *		build a new message and compare it to the one received
38  *	  	if they are the same,
39  *			issue a PASS message
40  *		otherwise
41  *			issue a FAIL message
42  *	call cleanup
43  *
44  * USAGE:  <for command-line>
45  *  msgrcv01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
46  *     where,  -c n : Run n copies concurrently.
47  *             -f   : Turn off functionality Testing.
48  *	       -i n : Execute test n times.
49  *	       -I x : Execute test for x seconds.
50  *	       -P x : Pause for x seconds between iterations.
51  *	       -t   : Turn on syscall timing.
52  *
53  * HISTORY
54  *	03/2001 - Written by Wayne Boyer
55  *
56  * RESTRICTIONS
57  *	none
58  */
59 
60 #include <string.h>
61 #include <sys/wait.h>
62 
63 #include "test.h"
64 
65 #include "ipcmsg.h"
66 
67 void cleanup(void);
68 void setup(void);
69 void do_child(void);
70 
71 char *TCID = "msgrcv01";
72 int TST_TOTAL = 1;
73 
74 int msg_q_1;
75 MSGBUF snd_buf, rcv_buf, cmp_buf;
76 
77 pid_t c_pid;
78 
main(int ac,char ** av)79 int main(int ac, char **av)
80 {
81 	int lc;
82 	void check_functionality(void);
83 	int status, e_code;
84 
85 	tst_parse_opts(ac, av, NULL, NULL);
86 
87 #ifdef UCLINUX
88 	maybe_run_child(&do_child, "d", &msg_q_1);
89 #endif
90 
91 	setup();		/* global setup */
92 
93 	/* The following loop checks looping state if -i option given */
94 
95 	for (lc = 0; TEST_LOOPING(lc); lc++) {
96 		/* reset tst_count in case we are looping */
97 		tst_count = 0;
98 
99 		/*
100 		 * fork a child to read from the queue while the parent
101 		 * enqueues the message to be read.
102 		 */
103 		if ((c_pid = FORK_OR_VFORK()) == -1) {
104 			tst_brkm(TBROK, cleanup, "could not fork");
105 		}
106 
107 		if (c_pid == 0) {	/* child */
108 #ifdef UCLINUX
109 			if (self_exec(av[0], "d", msg_q_1) < 0) {
110 				tst_brkm(TBROK, cleanup, "could not self_exec");
111 			}
112 #else
113 			do_child();
114 #endif
115 		} else {	/* parent */
116 			/* put the message on the queue */
117 			if (msgsnd(msg_q_1, &snd_buf, MSGSIZE, 0) == -1) {
118 				tst_brkm(TBROK, cleanup,
119 					 "Couldn't enqueue" " message");
120 			}
121 			/* wait for the child to finish */
122 			wait(&status);
123 			/* make sure the child returned a good exit status */
124 			e_code = status >> 8;
125 			if (e_code != 0) {
126 				tst_resm(TFAIL, "Failures reported above");
127 			}
128 
129 		}
130 	}
131 
132 	cleanup();
133 	tst_exit();
134 
135     /** NOT REACHED **/
136 
137 }
138 
139 /*
140  * do_child()
141  */
do_child(void)142 void do_child(void)
143 {
144 	int retval = 0;
145 
146 	TEST(msgrcv(msg_q_1, &rcv_buf, MSGSIZE, 1, 0));
147 
148 	if (TEST_RETURN == -1) {
149 		retval = 1;
150 		tst_resm(TFAIL, "%s call failed - errno = %d : %s",
151 			 TCID, TEST_ERRNO, strerror(TEST_ERRNO));
152 	} else {
153 		/*
154 		 * Build a new message and compare it
155 		 * with the one received.
156 		 */
157 		init_buf(&cmp_buf, MSGTYPE, MSGSIZE);
158 
159 		if (strcmp(rcv_buf.mtext, cmp_buf.mtext) == 0) {
160 			tst_resm(TPASS,
161 				 "message received = " "message sent");
162 		} else {
163 			retval = 1;
164 			tst_resm(TFAIL,
165 				 "message received != " "message sent");
166 		}
167 	}
168 	exit(retval);
169 }
170 
171 /*
172  * setup() - performs all the ONE TIME setup for this test.
173  */
setup(void)174 void setup(void)
175 {
176 
177 	tst_sig(FORK, DEF_HANDLER, cleanup);
178 
179 	TEST_PAUSE;
180 
181 	/*
182 	 * Create a temporary directory and cd into it.
183 	 * This helps to ensure that a unique msgkey is created.
184 	 * See ../lib/libipc.c for more information.
185 	 */
186 	tst_tmpdir();
187 
188 	msgkey = getipckey();
189 
190 	/* create a message queue with read/write permission */
191 	if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW)) == -1) {
192 		tst_brkm(TBROK, cleanup, "Can't create message queue");
193 	}
194 
195 	/* initialize the message buffer */
196 	init_buf(&snd_buf, MSGTYPE, MSGSIZE);
197 }
198 
199 /*
200  * cleanup() - performs all the ONE TIME cleanup for this test at completion
201  * 	       or premature exit.
202  */
cleanup(void)203 void cleanup(void)
204 {
205 	/* if it exists, remove the message queue that was created */
206 	rm_queue(msg_q_1);
207 
208 	tst_rmdir();
209 
210 }
211