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 *	msgrcv04.c
23 *
24 * DESCRIPTION
25 *	msgrcv04 - test for E2BIG and ENOMSG errors
26 *
27 * ALGORITHM
28 *	create a message queue with read/write permissions
29 *	initialize a message buffer with a known message and type
30 *	enqueue the message
31 *	loop if that option was specified
32 *	call msgrcv() using two different invalid cases
33 *	check the errno value
34 *	  issue a PASS message if we get E2BIG or ENOMSG
35 *	otherwise, the tests fails
36 *	  issue a FAIL message
37 *	call cleanup
38 *
39 * USAGE:  <for command-line>
40 *  msgrcv04 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
41 *     where,  -c n : Run n copies concurrently.
42 *             -e   : Turn on errno logging.
43 *	       -i n : Execute test n times.
44 *	       -I x : Execute test for x seconds.
45 *	       -P x : Pause for x seconds between iterations.
46 *	       -t   : Turn on syscall timing.
47 *
48 * HISTORY
49 *	03/2001 - Written by Wayne Boyer
50 *
51 * RESTRICTIONS
52 *	none
53 */
54
55#include "test.h"
56
57#include "ipcmsg.h"
58
59void cleanup(void);
60void setup(void);
61
62char *TCID = "msgrcv04";
63int TST_TOTAL = 2;
64
65int msg_q_1 = -1;		/* The message queue id created in setup */
66
67#define SMSIZE	512
68
69MSGBUF snd_buf, rcv_buf;
70
71struct test_case_t {
72	int size;
73	int type;
74	int flags;
75	int error;
76} TC[] = {
77	/*
78	 * E2BIG - The receive buffer is too small for the message and
79	 *         MSG_NOERROR isn't asserted in the flags.
80	 */
81	{
82	SMSIZE, 1, 0, E2BIG},
83	    /*
84	     * ENOMSG - There is no message with the requested type and
85	     *          IPC_NOWAIT is asserted in the flags.
86	     */
87	{
88	MSGSIZE, 2, IPC_NOWAIT, ENOMSG}
89};
90
91int main(int ac, char **av)
92{
93	int lc;
94	int i;
95
96	tst_parse_opts(ac, av, NULL, NULL);
97
98	setup();		/* global setup */
99
100	/* The following loop checks looping state if -i option given */
101
102	for (lc = 0; TEST_LOOPING(lc); lc++) {
103		/* reset tst_count in case we are looping */
104		tst_count = 0;
105
106		for (i = 0; i < TST_TOTAL; i++) {
107
108			/*
109			 * Use the TEST macro to make the call
110			 */
111
112			TEST(msgrcv(msg_q_1, &rcv_buf, TC[i].size, TC[i].type,
113				    TC[i].flags));
114
115			if (TEST_RETURN != -1) {
116				tst_resm(TFAIL, "call succeeded unexpectedly");
117				continue;
118			}
119
120			if (TEST_ERRNO == TC[i].error) {
121				tst_resm(TPASS, "expected failure - errno = "
122					 "%d : %s", TEST_ERRNO,
123					 strerror(TEST_ERRNO));
124			} else {
125				tst_resm(TFAIL, "call failed with an "
126					 "unexpected error - %d : %s",
127					 TEST_ERRNO, strerror(TEST_ERRNO));
128			}
129		}
130	}
131
132	cleanup();
133
134	tst_exit();
135}
136
137/*
138 * setup() - performs all the ONE TIME setup for this test.
139 */
140void setup(void)
141{
142
143	tst_sig(NOFORK, DEF_HANDLER, cleanup);
144
145	TEST_PAUSE;
146
147	/*
148	 * Create a temporary directory and cd into it.
149	 * This helps to ensure that a unique msgkey is created.
150	 * See ../lib/libipc.c for more information.
151	 */
152	tst_tmpdir();
153
154	msgkey = getipckey();
155
156	/* create a message queue with read/write permission */
157	if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW)) == -1) {
158		tst_brkm(TBROK, cleanup, "Can't create message queue");
159	}
160
161	/* initialize a buffer */
162	init_buf(&snd_buf, MSGTYPE, MSGSIZE);
163
164	/* put the message on the queue */
165	if (msgsnd(msg_q_1, &snd_buf, MSGSIZE, 0) == -1) {
166		tst_brkm(TBROK, cleanup, "Can't enqueue message");
167	}
168}
169
170/*
171 * cleanup() - performs all the ONE TIME cleanup for this test at completion
172 * 	       or premature exit.
173 */
174void cleanup(void)
175{
176	/* if it exists, remove the message queue that was created */
177	rm_queue(msg_q_1);
178
179	tst_rmdir();
180
181}
182