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 *	msgget02.c
23 *
24 * DESCRIPTION
25 *	msgget02 - test for EEXIST and ENOENT errors
26 *
27 * ALGORITHM
28 *	create a message queue
29 *	loop if that option was specified
30 *	try to recreate the same queue - test #1
31 *	try to access a queue that doesn't exist - tests #2 & #3
32 *	check the errno value
33 *	  issue a PASS message if we get EEXIST or ENOENT depening on test
34 *	otherwise, the tests fails
35 *	  issue a FAIL message
36 *	  break any remaining tests
37 *	  call cleanup
38 *
39 * USAGE:  <for command-line>
40 *  msgget02 [-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 *      28/03/2008 Renaud Lottiaux (Renaud.Lottiaux@kerlabs.com)
52 *      - Fix concurrency issue. The second key used for this test was
53 *        sometime conflicting with the key from another task.
54 *        Generate a valid second key through getipckey to avoid conflicts.
55 *
56 * RESTRICTIONS
57 *	none
58 */
59
60#include "test.h"
61
62#include "ipcmsg.h"
63
64char *TCID = "msgget02";
65int TST_TOTAL = 3;
66
67struct test_case_t {
68	int error;
69	int msgkey;
70	int flags;
71} TC[] = {
72	{
73	EEXIST, 0, IPC_CREAT | IPC_EXCL}, {
74	ENOENT, 1, IPC_PRIVATE}, {
75	ENOENT, 1, IPC_EXCL}
76};
77
78key_t msgkey1;
79int msg_q_1 = -1;		/* The message queue id created in setup */
80
81int main(int ac, char **av)
82{
83	int lc;
84	int i;
85	key_t key;
86
87	tst_parse_opts(ac, av, NULL, NULL);
88
89	setup();		/* global setup */
90
91	/* The following loop checks looping state if -i option given */
92
93	for (lc = 0; TEST_LOOPING(lc); lc++) {
94		/* reset tst_count in case we are looping */
95		tst_count = 0;
96
97		/* loop through the test cases */
98
99		for (i = 0; i < TST_TOTAL; i++) {
100
101			if (TC[i].msgkey == 0)
102				key = msgkey;
103			else
104				key = msgkey1;
105
106			TEST(msgget(key, TC[i].flags));
107
108			if (TEST_RETURN != -1) {
109				tst_resm(TFAIL, "msgget() call succeeded "
110					 "on expected fail");
111				continue;
112			}
113
114			switch (TEST_ERRNO) {
115			case ENOENT:
116			 /*FALLTHROUGH*/ case EEXIST:
117				if (TEST_ERRNO == TC[i].error) {
118					tst_resm(TPASS, "expected failure - "
119						 "errno = %d : %s", TEST_ERRNO,
120						 strerror(TEST_ERRNO));
121					break;
122				}
123			/*FALLTHROUGH*/ default:
124				tst_resm(TFAIL, "call failed with an "
125					 "unexpected error - %d : %s",
126					 TEST_ERRNO, strerror(TEST_ERRNO));
127				break;
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	msgkey1 = getipckey();
156
157	/* now we have a key, so let's create a message queue */
158	if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL)) == -1) {
159		system("ipcs > /tmp/toto");
160		system("ps -aux >> /tmp/toto");
161		tst_brkm(TBROK, cleanup, "Can't create message queue");
162	}
163}
164
165/*
166 * cleanup() - performs all the ONE TIME cleanup for this test at completion
167 * 	       or premature exit.
168 */
169void cleanup(void)
170{
171	/* if it exists, remove the message queue that was created. */
172	rm_queue(msg_q_1);
173
174	tst_rmdir();
175
176}
177