blob: 0caff557e9aae3981e8e28398f100119e368aefe [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 * msgsnd01.c
23 *
24 * DESCRIPTION
25 * msgsnd01 - test that msgsnd() enqueues a message correctly
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 * enqueue the message
32 * check the return code
33 * if failure, issue a FAIL message.
34 * otherwise,
35 * if doing functionality testing
36 * stat the message queue
37 * check for # of bytes = MSGSIZE and # of messages = 1
38 * if correct,
39 * issue a PASS message
40 * otherwise
41 * issue a FAIL message
42 * call cleanup
43 *
44 * USAGE: <for command-line>
45 * msgsnd01 [-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 "test.h"
plars865695b2001-08-27 22:15:12 +000061
robbiew23499f02002-11-18 19:54:58 +000062#include "ipcmsg.h"
plars865695b2001-08-27 22:15:12 +000063
64void cleanup(void);
65void setup(void);
66
67char *TCID = "msgsnd01";
68int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000069
70int msg_q_1;
71MSGBUF msg_buf, rd_buf;
72
73struct msqid_ds qs_buf;
74
robbiew23499f02002-11-18 19:54:58 +000075int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000076{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020077 int lc;
Cyril Hrubis74225622014-06-02 17:54:38 +020078 const char *msg;
plars865695b2001-08-27 22:15:12 +000079
Cyril Hrubis74225622014-06-02 17:54:38 +020080 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080081 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000082
subrata_modak56207ce2009-03-23 13:35:39 +000083 setup(); /* global setup */
plars865695b2001-08-27 22:15:12 +000084
85 /* The following loop checks looping state if -i option given */
86
87 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080088 /* reset tst_count in case we are looping */
89 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000090
91 /*
92 * Use TEST macro to make the call
93 */
subrata_modakbdbaec52009-02-26 12:14:51 +000094
plars865695b2001-08-27 22:15:12 +000095 TEST(msgsnd(msg_q_1, &msg_buf, MSGSIZE, 0));
subrata_modakbdbaec52009-02-26 12:14:51 +000096
plars865695b2001-08-27 22:15:12 +000097 if (TEST_RETURN == -1) {
98 tst_resm(TFAIL, "%s call failed - errno = %d : %s",
99 TCID, TEST_ERRNO, strerror(TEST_ERRNO));
100 continue;
101 }
102
Cyril Hrubise38b9612014-06-02 17:20:57 +0200103 /* get the queue status */
104 if (msgctl(msg_q_1, IPC_STAT, &qs_buf) == -1) {
105 tst_brkm(TBROK, cleanup, "Could not "
106 "get queue status");
plars865695b2001-08-27 22:15:12 +0000107 }
108
Cyril Hrubise38b9612014-06-02 17:20:57 +0200109 if (qs_buf.msg_cbytes != MSGSIZE) {
110 tst_resm(TFAIL, "queue bytes != MSGSIZE");
111 }
112
113 if (qs_buf.msg_qnum != 1) {
114 tst_resm(TFAIL, "queue message != 1");
115 }
116
117 tst_resm(TPASS, "queue bytes = MSGSIZE and "
118 "queue messages = 1");
119
plars865695b2001-08-27 22:15:12 +0000120 /*
121 * remove the message by reading from the queue
122 */
123 if (msgrcv(msg_q_1, &rd_buf, MSGSIZE, 1, 0) == -1) {
124 tst_brkm(TBROK, cleanup, "Could not read from queue");
125 }
126 }
127
128 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800129 tst_exit();
plars865695b2001-08-27 22:15:12 +0000130}
131
132/*
133 * setup() - performs all the ONE TIME setup for this test.
134 */
subrata_modak56207ce2009-03-23 13:35:39 +0000135void setup(void)
plars865695b2001-08-27 22:15:12 +0000136{
Garrett Cooper2c282152010-12-16 00:55:50 -0800137
plars865695b2001-08-27 22:15:12 +0000138 tst_sig(NOFORK, DEF_HANDLER, cleanup);
139
plars865695b2001-08-27 22:15:12 +0000140 TEST_PAUSE;
141
142 /*
143 * Create a temporary directory and cd into it.
144 * This helps to ensure that a unique msgkey is created.
145 * See ../lib/libipc.c for more information.
146 */
147 tst_tmpdir();
148
149 msgkey = getipckey();
150
151 /* create a message queue with read/write permissions */
152 if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW)) == -1) {
153 tst_brkm(TBROK, cleanup, "Can't create message queue");
154 }
155
156 /* initialize the message buffer */
157 init_buf(&msg_buf, MSGTYPE, MSGSIZE);
158}
159
160/*
161 * cleanup() - performs all the ONE TIME cleanup for this test at completion
162 * or premature exit.
163 */
subrata_modak56207ce2009-03-23 13:35:39 +0000164void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000165{
166 /* if it exists, remove the message queue if it exists */
167 rm_queue(msg_q_1);
168
plars865695b2001-08-27 22:15:12 +0000169 tst_rmdir();
170
Chris Dearmanec6edca2012-10-17 19:54:01 -0700171}