plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 | * |
subrata_modak | af04320 | 2008-04-06 11:15:06 +0000 | [diff] [blame] | 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 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | * RESTRICTIONS |
| 57 | * none |
| 58 | */ |
| 59 | |
| 60 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | |
robbiew | 23499f0 | 2002-11-18 19:54:58 +0000 | [diff] [blame] | 62 | #include "ipcmsg.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | |
| 64 | char *TCID = "msgget02"; |
| 65 | int TST_TOTAL = 3; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | |
| 67 | struct test_case_t { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 68 | int error; |
| 69 | int msgkey; |
| 70 | int flags; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | } TC[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 72 | { |
| 73 | EEXIST, 0, IPC_CREAT | IPC_EXCL}, { |
| 74 | ENOENT, 1, IPC_PRIVATE}, { |
| 75 | ENOENT, 1, IPC_EXCL} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
subrata_modak | af04320 | 2008-04-06 11:15:06 +0000 | [diff] [blame] | 78 | key_t msgkey1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | int msg_q_1 = -1; /* The message queue id created in setup */ |
| 80 | |
robbiew | 23499f0 | 2002-11-18 19:54:58 +0000 | [diff] [blame] | 81 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 83 | int lc; |
Cyril Hrubis | 7422562 | 2014-06-02 17:54:38 +0200 | [diff] [blame] | 84 | const char *msg; |
robbiew | 23499f0 | 2002-11-18 19:54:58 +0000 | [diff] [blame] | 85 | int i; |
subrata_modak | af04320 | 2008-04-06 11:15:06 +0000 | [diff] [blame] | 86 | key_t key; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | |
Cyril Hrubis | 7422562 | 2014-06-02 17:54:38 +0200 | [diff] [blame] | 88 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 89 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 91 | setup(); /* global setup */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | |
| 93 | /* The following loop checks looping state if -i option given */ |
| 94 | |
| 95 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 96 | /* reset tst_count in case we are looping */ |
| 97 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | |
| 99 | /* loop through the test cases */ |
| 100 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | for (i = 0; i < TST_TOTAL; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | |
subrata_modak | af04320 | 2008-04-06 11:15:06 +0000 | [diff] [blame] | 103 | if (TC[i].msgkey == 0) |
| 104 | key = msgkey; |
| 105 | else |
| 106 | key = msgkey1; |
| 107 | |
| 108 | TEST(msgget(key, TC[i].flags)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | |
| 110 | if (TEST_RETURN != -1) { |
| 111 | tst_resm(TFAIL, "msgget() call succeeded " |
| 112 | "on expected fail"); |
| 113 | continue; |
| 114 | } |
| 115 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 116 | switch (TEST_ERRNO) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | case ENOENT: |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 118 | /*FALLTHROUGH*/ case EEXIST: |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | if (TEST_ERRNO == TC[i].error) { |
| 120 | tst_resm(TPASS, "expected failure - " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 121 | "errno = %d : %s", TEST_ERRNO, |
| 122 | strerror(TEST_ERRNO)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | break; |
| 124 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 125 | /*FALLTHROUGH*/ default: |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | 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 Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 136 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /* |
| 140 | * setup() - performs all the ONE TIME setup for this test. |
| 141 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 142 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 144 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 146 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | 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_modak | af04320 | 2008-04-06 11:15:06 +0000 | [diff] [blame] | 157 | msgkey1 = getipckey(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 161 | system("ipcs > /tmp/toto"); |
| 162 | system("ps -aux >> /tmp/toto"); |
| 163 | tst_brkm(TBROK, cleanup, "Can't create message queue"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * cleanup() - performs all the ONE TIME cleanup for this test at completion |
| 169 | * or premature exit. |
| 170 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 171 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 172 | { |
| 173 | /* if it exists, remove the message queue that was created. */ |
| 174 | rm_queue(msg_q_1); |
| 175 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | tst_rmdir(); |
| 177 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 178 | } |