blob: 3b76713caa7da5e031a24295b6279eb47c8b05d3 [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 * 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 *
subrata_modakaf043202008-04-06 11:15:06 +000051 * 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 *
plars865695b2001-08-27 22:15:12 +000056 * 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
64char *TCID = "msgget02";
65int TST_TOTAL = 3;
plars865695b2001-08-27 22:15:12 +000066
67struct test_case_t {
subrata_modak56207ce2009-03-23 13:35:39 +000068 int error;
69 int msgkey;
70 int flags;
plars865695b2001-08-27 22:15:12 +000071} TC[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000072 {
73 EEXIST, 0, IPC_CREAT | IPC_EXCL}, {
74 ENOENT, 1, IPC_PRIVATE}, {
75 ENOENT, 1, IPC_EXCL}
plars865695b2001-08-27 22:15:12 +000076};
77
subrata_modakaf043202008-04-06 11:15:06 +000078key_t msgkey1;
plars865695b2001-08-27 22:15:12 +000079int msg_q_1 = -1; /* The message queue id created in setup */
80
robbiew23499f02002-11-18 19:54:58 +000081int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000082{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020083 int lc;
Cyril Hrubis74225622014-06-02 17:54:38 +020084 const char *msg;
robbiew23499f02002-11-18 19:54:58 +000085 int i;
subrata_modakaf043202008-04-06 11:15:06 +000086 key_t key;
plars865695b2001-08-27 22:15:12 +000087
Cyril Hrubis74225622014-06-02 17:54:38 +020088 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080089 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000090
subrata_modak56207ce2009-03-23 13:35:39 +000091 setup(); /* global setup */
plars865695b2001-08-27 22:15:12 +000092
93 /* The following loop checks looping state if -i option given */
94
95 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080096 /* reset tst_count in case we are looping */
97 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000098
99 /* loop through the test cases */
100
subrata_modak56207ce2009-03-23 13:35:39 +0000101 for (i = 0; i < TST_TOTAL; i++) {
plars865695b2001-08-27 22:15:12 +0000102
subrata_modakaf043202008-04-06 11:15:06 +0000103 if (TC[i].msgkey == 0)
104 key = msgkey;
105 else
106 key = msgkey1;
107
108 TEST(msgget(key, TC[i].flags));
plars865695b2001-08-27 22:15:12 +0000109
110 if (TEST_RETURN != -1) {
111 tst_resm(TFAIL, "msgget() call succeeded "
112 "on expected fail");
113 continue;
114 }
115
subrata_modak56207ce2009-03-23 13:35:39 +0000116 switch (TEST_ERRNO) {
plars865695b2001-08-27 22:15:12 +0000117 case ENOENT:
subrata_modak56207ce2009-03-23 13:35:39 +0000118 /*FALLTHROUGH*/ case EEXIST:
plars865695b2001-08-27 22:15:12 +0000119 if (TEST_ERRNO == TC[i].error) {
120 tst_resm(TPASS, "expected failure - "
subrata_modak56207ce2009-03-23 13:35:39 +0000121 "errno = %d : %s", TEST_ERRNO,
122 strerror(TEST_ERRNO));
plars865695b2001-08-27 22:15:12 +0000123 break;
124 }
subrata_modak56207ce2009-03-23 13:35:39 +0000125 /*FALLTHROUGH*/ default:
plars865695b2001-08-27 22:15:12 +0000126 tst_resm(TFAIL, "call failed with an "
127 "unexpected error - %d : %s",
128 TEST_ERRNO, strerror(TEST_ERRNO));
129 break;
130 }
131 }
132 }
133
134 cleanup();
135
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800136 tst_exit();
plars865695b2001-08-27 22:15:12 +0000137}
138
139/*
140 * setup() - performs all the ONE TIME setup for this test.
141 */
subrata_modak56207ce2009-03-23 13:35:39 +0000142void setup(void)
plars865695b2001-08-27 22:15:12 +0000143{
Garrett Cooper2c282152010-12-16 00:55:50 -0800144
plars865695b2001-08-27 22:15:12 +0000145 tst_sig(NOFORK, DEF_HANDLER, cleanup);
146
plars865695b2001-08-27 22:15:12 +0000147 TEST_PAUSE;
148
149 /*
150 * Create a temporary directory and cd into it.
151 * This helps to ensure that a unique msgkey is created.
152 * See ../lib/libipc.c for more information.
153 */
154 tst_tmpdir();
155
156 msgkey = getipckey();
subrata_modakaf043202008-04-06 11:15:06 +0000157 msgkey1 = getipckey();
plars865695b2001-08-27 22:15:12 +0000158
159 /* now we have a key, so let's create a message queue */
160 if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL)) == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000161 system("ipcs > /tmp/toto");
162 system("ps -aux >> /tmp/toto");
163 tst_brkm(TBROK, cleanup, "Can't create message queue");
plars865695b2001-08-27 22:15:12 +0000164 }
165}
166
167/*
168 * cleanup() - performs all the ONE TIME cleanup for this test at completion
169 * or premature exit.
170 */
subrata_modak56207ce2009-03-23 13:35:39 +0000171void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000172{
173 /* if it exists, remove the message queue that was created. */
174 rm_queue(msg_q_1);
175
plars865695b2001-08-27 22:15:12 +0000176 tst_rmdir();
177
Chris Dearmanec6edca2012-10-17 19:54:01 -0700178}