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  *	msgget01.c
23  *
24  * DESCRIPTION
25  *	msgget01 - create a message queue, write a message to it and
26  *		   read it back.
27  *
28  * ALGORITHM
29  *	loop if that option was specified
30  *	create a message queue
31  *	check the return code
32  *	  if failure, issue a FAIL message.
33  *	otherwise,
34  *	  if doing functionality testing by writting a message to the queue,
35  *	  reading it back and comparing the two.
36  *	  	if the messages are the same,
37  *			issue a PASS message
38  *		otherwise
39  *			issue a FAIL message
40  *	call cleanup
41  *
42  * USAGE:  <for command-line>
43  *  msgget01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
44  *     where,  -c n : Run n copies concurrently.
45  *             -f   : Turn off functionality Testing.
46  *	       -i n : Execute test n times.
47  *	       -I x : Execute test for x seconds.
48  *	       -P x : Pause for x seconds between iterations.
49  *	       -t   : Turn on syscall timing.
50  *
51  * HISTORY
52  *	03/2001 - Written by Wayne Boyer
53  *
54  * RESTRICTIONS
55  *	none
56  */
57 
58 #include "ipcmsg.h"
59 
60 #include <string.h>
61 
62 char *TCID = "msgget01";
63 int TST_TOTAL = 1;
64 
65 int msg_q_1 = -1;		/* to hold the message queue ID */
66 
main(int ac,char ** av)67 int main(int ac, char **av)
68 {
69 	int lc;
70 	void check_functionality(void);
71 
72 	tst_parse_opts(ac, av, NULL, NULL);
73 
74 	setup();		/* global setup */
75 
76 	/* The following loop checks looping state if -i option given */
77 
78 	for (lc = 0; TEST_LOOPING(lc); lc++) {
79 		/* reset tst_count in case we are looping */
80 		tst_count = 0;
81 
82 		/*
83 		 * Use TEST macro to make the call to create the message queue
84 		 */
85 
86 		TEST(msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RD | MSG_WR));
87 
88 		if (TEST_RETURN == -1) {
89 			tst_resm(TFAIL, "%s call failed - errno = %d : %s",
90 				 TCID, TEST_ERRNO, strerror(TEST_ERRNO));
91 		} else {
92 			msg_q_1 = TEST_RETURN;
93 			/*
94 			 * write a message to the queue.
95 			 * read back the message.
96 			 * PASS the test if they are the same.
97 			 */
98 			check_functionality();
99 		}
100 
101 		/*
102 		 * remove the message queue that was created and mark the ID
103 		 * as invalid.
104 		 */
105 		if (msg_q_1 != -1) {
106 			rm_queue(msg_q_1);
107 			msg_q_1 = -1;
108 		}
109 	}
110 
111 	cleanup();
112 	tst_exit();
113 }
114 
115 /*
116  * check_functionality() - check the functionality of the tested system call.
117  */
check_functionality(void)118 void check_functionality(void)
119 {
120 	int i = 0;
121 	MSGBUF snd_buf, rcv_buf;
122 
123 	/* EAGLE: Houston, Tranquility Base here. The Eagle has landed! */
124 	char *queue_msg =
125 	    "Qston, check_functionality here.  The message has queued!";
126 
127 	/*
128 	 * copy our message into the buffer and then set the type.
129 	 */
130 	do {
131 		snd_buf.mtext[i++] = *queue_msg;
132 	} while (*queue_msg++ != '\0');
133 
134 	snd_buf.mtype = MSGTYPE;
135 
136 	/* send the message */
137 	if (msgsnd(msg_q_1, &snd_buf, MSGSIZE, 0) == -1) {
138 		tst_brkm(TBROK, cleanup, "Could not send a message in the "
139 			 "check_functionality() routine.");
140 	}
141 
142 	/* receive the message */
143 	if (msgrcv(msg_q_1, &rcv_buf, MSGSIZE, MSGTYPE, IPC_NOWAIT) == -1) {
144 		tst_brkm(TBROK, cleanup, "Could not read a messages in the "
145 			 "check_functionality() routine.");
146 	}
147 
148 	if (strcmp(snd_buf.mtext, rcv_buf.mtext) == 0) {
149 		tst_resm(TPASS, "message received = message sent");
150 	} else {
151 		tst_resm(TFAIL, "message received != message sent");
152 	}
153 }
154 
155 /*
156  * setup() - performs all the ONE TIME setup for this test.
157  */
setup(void)158 void setup(void)
159 {
160 
161 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
162 
163 	TEST_PAUSE;
164 
165 	/*
166 	 * Create a temporary directory and cd into it.
167 	 * This helps to ensure that a unique msgkey is created.
168 	 * See ../lib/libipc.c for more information.
169 	 */
170 	tst_tmpdir();
171 
172 	msgkey = getipckey();
173 }
174 
175 /*
176  * cleanup() - performs all the ONE TIME cleanup for this test at completion
177  * 	       or premature exit.
178  */
cleanup(void)179 void cleanup(void)
180 {
181 	/* if it exists, remove the message queue that was created */
182 	rm_queue(msg_q_1);
183 
184 	tst_rmdir();
185 
186 }
187