blob: 95b52bcede157d8a21b78b6b45a7c3476683e374 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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>
robbiew23499f02002-11-18 19:54:58 +000061#include <sys/wait.h>
plars865695b2001-08-27 22:15:12 +000062
63#include "test.h"
plars865695b2001-08-27 22:15:12 +000064
robbiew23499f02002-11-18 19:54:58 +000065#include "ipcmsg.h"
plars865695b2001-08-27 22:15:12 +000066
67void cleanup(void);
68void setup(void);
robbiewd34d5812005-07-11 22:28:09 +000069void do_child(void);
plars865695b2001-08-27 22:15:12 +000070
71char *TCID = "msgrcv01";
72int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000073
74int msg_q_1;
75MSGBUF snd_buf, rcv_buf, cmp_buf;
76
77pid_t c_pid;
78
robbiew23499f02002-11-18 19:54:58 +000079int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000080{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020081 int lc;
Cyril Hrubis74225622014-06-02 17:54:38 +020082 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +000083 void check_functionality(void);
84 int status, e_code;
plars865695b2001-08-27 22:15:12 +000085
Cyril Hrubis74225622014-06-02 17:54:38 +020086 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080087 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Cyril Hrubis74225622014-06-02 17:54:38 +020088
robbiewd34d5812005-07-11 22:28:09 +000089#ifdef UCLINUX
subrata_modak56207ce2009-03-23 13:35:39 +000090 maybe_run_child(&do_child, "d", &msg_q_1);
robbiewd34d5812005-07-11 22:28:09 +000091#endif
92
subrata_modak56207ce2009-03-23 13:35:39 +000093 setup(); /* global setup */
plars5e718c32001-09-14 15:56:02 +000094
subrata_modak56207ce2009-03-23 13:35:39 +000095 /* The following loop checks looping state if -i option given */
plars5e718c32001-09-14 15:56:02 +000096
subrata_modak56207ce2009-03-23 13:35:39 +000097 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080098 /* reset tst_count in case we are looping */
99 tst_count = 0;
plars5e718c32001-09-14 15:56:02 +0000100
subrata_modak56207ce2009-03-23 13:35:39 +0000101 /*
102 * fork a child to read from the queue while the parent
103 * enqueues the message to be read.
104 */
105 if ((c_pid = FORK_OR_VFORK()) == -1) {
106 tst_brkm(TBROK, cleanup, "could not fork");
107 }
plars865695b2001-08-27 22:15:12 +0000108
subrata_modak56207ce2009-03-23 13:35:39 +0000109 if (c_pid == 0) { /* child */
robbiewd34d5812005-07-11 22:28:09 +0000110#ifdef UCLINUX
subrata_modak56207ce2009-03-23 13:35:39 +0000111 if (self_exec(av[0], "d", msg_q_1) < 0) {
112 tst_brkm(TBROK, cleanup, "could not self_exec");
113 }
robbiewd34d5812005-07-11 22:28:09 +0000114#else
subrata_modak56207ce2009-03-23 13:35:39 +0000115 do_child();
robbiewd34d5812005-07-11 22:28:09 +0000116#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000117 } else { /* parent */
118 /* put the message on the queue */
119 if (msgsnd(msg_q_1, &snd_buf, MSGSIZE, 0) == -1) {
120 tst_brkm(TBROK, cleanup,
121 "Couldn't enqueue" " message");
122 }
123 /* wait for the child to finish */
124 wait(&status);
125 /* make sure the child returned a good exit status */
126 e_code = status >> 8;
127 if (e_code != 0) {
128 tst_resm(TFAIL, "Failures reported above");
129 }
plars5e718c32001-09-14 15:56:02 +0000130
subrata_modak56207ce2009-03-23 13:35:39 +0000131 }
plars865695b2001-08-27 22:15:12 +0000132 }
133
subrata_modak56207ce2009-03-23 13:35:39 +0000134 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800135 tst_exit();
plars865695b2001-08-27 22:15:12 +0000136
robbiew23499f02002-11-18 19:54:58 +0000137 /** NOT REACHED **/
robbiew23499f02002-11-18 19:54:58 +0000138
plars865695b2001-08-27 22:15:12 +0000139}
140
141/*
robbiewd34d5812005-07-11 22:28:09 +0000142 * do_child()
143 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400144void do_child(void)
robbiewd34d5812005-07-11 22:28:09 +0000145{
subrata_modak56207ce2009-03-23 13:35:39 +0000146 int retval = 0;
robbiewd34d5812005-07-11 22:28:09 +0000147
subrata_modak56207ce2009-03-23 13:35:39 +0000148 TEST(msgrcv(msg_q_1, &rcv_buf, MSGSIZE, 1, 0));
149
150 if (TEST_RETURN == -1) {
robbiewd34d5812005-07-11 22:28:09 +0000151 retval = 1;
subrata_modak56207ce2009-03-23 13:35:39 +0000152 tst_resm(TFAIL, "%s call failed - errno = %d : %s",
153 TCID, TEST_ERRNO, strerror(TEST_ERRNO));
robbiewd34d5812005-07-11 22:28:09 +0000154 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200155 /*
156 * Build a new message and compare it
157 * with the one received.
158 */
159 init_buf(&cmp_buf, MSGTYPE, MSGSIZE);
subrata_modak56207ce2009-03-23 13:35:39 +0000160
Cyril Hrubise38b9612014-06-02 17:20:57 +0200161 if (strcmp(rcv_buf.mtext, cmp_buf.mtext) == 0) {
162 tst_resm(TPASS,
163 "message received = " "message sent");
subrata_modak56207ce2009-03-23 13:35:39 +0000164 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200165 retval = 1;
166 tst_resm(TFAIL,
167 "message received != " "message sent");
subrata_modak56207ce2009-03-23 13:35:39 +0000168 }
robbiewd34d5812005-07-11 22:28:09 +0000169 }
subrata_modak56207ce2009-03-23 13:35:39 +0000170 exit(retval);
robbiewd34d5812005-07-11 22:28:09 +0000171}
172
173/*
plars865695b2001-08-27 22:15:12 +0000174 * setup() - performs all the ONE TIME setup for this test.
175 */
plars5e718c32001-09-14 15:56:02 +0000176void setup(void)
plars865695b2001-08-27 22:15:12 +0000177{
Garrett Cooper2c282152010-12-16 00:55:50 -0800178
subrata_modak56207ce2009-03-23 13:35:39 +0000179 tst_sig(FORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000180
subrata_modak56207ce2009-03-23 13:35:39 +0000181 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000182
subrata_modak56207ce2009-03-23 13:35:39 +0000183 /*
184 * Create a temporary directory and cd into it.
185 * This helps to ensure that a unique msgkey is created.
186 * See ../lib/libipc.c for more information.
187 */
188 tst_tmpdir();
plars865695b2001-08-27 22:15:12 +0000189
subrata_modak56207ce2009-03-23 13:35:39 +0000190 msgkey = getipckey();
plars865695b2001-08-27 22:15:12 +0000191
subrata_modak56207ce2009-03-23 13:35:39 +0000192 /* create a message queue with read/write permission */
193 if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW)) == -1) {
194 tst_brkm(TBROK, cleanup, "Can't create message queue");
195 }
plars865695b2001-08-27 22:15:12 +0000196
subrata_modak56207ce2009-03-23 13:35:39 +0000197 /* initialize the message buffer */
198 init_buf(&snd_buf, MSGTYPE, MSGSIZE);
plars865695b2001-08-27 22:15:12 +0000199}
200
201/*
202 * cleanup() - performs all the ONE TIME cleanup for this test at completion
203 * or premature exit.
204 */
plars5e718c32001-09-14 15:56:02 +0000205void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000206{
subrata_modak56207ce2009-03-23 13:35:39 +0000207 /* if it exists, remove the message queue that was created */
208 rm_queue(msg_q_1);
plars865695b2001-08-27 22:15:12 +0000209
subrata_modak56207ce2009-03-23 13:35:39 +0000210 tst_rmdir();
plars865695b2001-08-27 22:15:12 +0000211
Chris Dearmanec6edca2012-10-17 19:54:01 -0700212}