blob: 0725aafde24ec6125b2b124b5c0835306475146e [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
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
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"
61#include "usctest.h"
62
63#include "../lib/ipcmsg.h"
64
65void cleanup(void);
66void setup(void);
67
68char *TCID = "msgsnd01";
69int TST_TOTAL = 1;
70extern int Tst_count;
71
72int msg_q_1;
73MSGBUF msg_buf, rd_buf;
74
75struct msqid_ds qs_buf;
76
77main(int ac, char **av)
78{
79 int lc; /* loop counter */
80 char *msg; /* message returned from parse_opts */
81
82 /* parse standard options */
83 if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){
84 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
85 }
86
87 setup(); /* global setup */
88
89 /* The following loop checks looping state if -i option given */
90
91 for (lc = 0; TEST_LOOPING(lc); lc++) {
92 /* reset Tst_count in case we are looping */
93 Tst_count = 0;
94
95 /*
96 * Use TEST macro to make the call
97 */
98
99 TEST(msgsnd(msg_q_1, &msg_buf, MSGSIZE, 0));
100
101 if (TEST_RETURN == -1) {
102 tst_resm(TFAIL, "%s call failed - errno = %d : %s",
103 TCID, TEST_ERRNO, strerror(TEST_ERRNO));
104 continue;
105 }
106
107 if (STD_FUNCTIONAL_TEST) {
108
109 /* get the queue status */
110 if (msgctl(msg_q_1, IPC_STAT, &qs_buf) == -1) {
111 tst_brkm(TBROK, cleanup, "Could not "
112 "get queue status");
113 }
114
115 if (qs_buf.msg_cbytes != MSGSIZE) {
116 tst_resm(TFAIL, "queue bytes != MSGSIZE");
117 }
118
119 if (qs_buf.msg_qnum != 1) {
120 tst_resm(TFAIL, "queue message != 1");
121 }
122
123 tst_resm(TPASS, "queue bytes = MSGSIZE and "
124 "queue messages = 1");
125 } else {
126 tst_resm(TPASS, "call succeeded");
127 }
128
129 /*
130 * remove the message by reading from the queue
131 */
132 if (msgrcv(msg_q_1, &rd_buf, MSGSIZE, 1, 0) == -1) {
133 tst_brkm(TBROK, cleanup, "Could not read from queue");
134 }
135 }
136
137 cleanup();
138
139 /*NOTREACHED*/
140}
141
142/*
143 * setup() - performs all the ONE TIME setup for this test.
144 */
145void
146setup(void)
147{
148 /* capture signals */
149 tst_sig(NOFORK, DEF_HANDLER, cleanup);
150
151 /* Pause if that option was specified */
152 TEST_PAUSE;
153
154 /*
155 * Create a temporary directory and cd into it.
156 * This helps to ensure that a unique msgkey is created.
157 * See ../lib/libipc.c for more information.
158 */
159 tst_tmpdir();
160
161 msgkey = getipckey();
162
163 /* create a message queue with read/write permissions */
164 if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW)) == -1) {
165 tst_brkm(TBROK, cleanup, "Can't create message queue");
166 }
167
168 /* initialize the message buffer */
169 init_buf(&msg_buf, MSGTYPE, MSGSIZE);
170}
171
172/*
173 * cleanup() - performs all the ONE TIME cleanup for this test at completion
174 * or premature exit.
175 */
176void
177cleanup(void)
178{
179 /* if it exists, remove the message queue if it exists */
180 rm_queue(msg_q_1);
181
182 /* Remove the temporary directory */
183 tst_rmdir();
184
185 /*
186 * print timing stats if that option was specified.
187 * print errno log if that option was specified.
188 */
189 TEST_CLEANUP;
190
191 /* exit with return code appropriate for results */
192 tst_exit();
193}
194