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 | * msgrcv06.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * msgrcv06 - test for EIDRM error |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * loop if that option was specified |
| 29 | * create a message queue with read/write permissions |
| 30 | * fork a child who sleeps on an attempted read with msgrcv() |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 31 | * parent removes the queue then waits for child to complete |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 32 | * check the errno value |
| 33 | * issue a PASS message if we get EIDRM |
| 34 | * otherwise, the tests fails |
| 35 | * issue a FAIL message |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 36 | * child removes message queue if required |
| 37 | * parent callc cleanup |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 38 | * |
| 39 | * USAGE: <for command-line> |
| 40 | * msgrcv06 [-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 |
subrata_modak | 2191ca8 | 2008-04-06 11:50:24 +0000 | [diff] [blame] | 50 | * 14/03/2008 Matthieu Fertré (Matthieu.Fertre@irisa.fr) |
| 51 | * - Fix concurrency issue. Due to the use of usleep function to |
| 52 | * synchronize processes, synchronization issues can occur on a loaded |
| 53 | * system. Fix this by using pipes to synchronize processes. |
| 54 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | * |
| 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" |
subrata_modak | 2191ca8 | 2008-04-06 11:50:24 +0000 | [diff] [blame] | 63 | #include "libtestsuite.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 65 | #include <sys/types.h> |
| 66 | #include <sys/wait.h> |
| 67 | |
| 68 | void do_child(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | void cleanup(void); |
| 70 | void setup(void); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 71 | #ifdef UCLINUX |
subrata_modak | 4b13c89 | 2009-01-22 09:51:33 +0000 | [diff] [blame] | 72 | #define PIPE_NAME "msgrcv06" |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 73 | void do_child_uclinux(void); |
| 74 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
| 76 | char *TCID = "msgrcv06"; |
| 77 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
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 | |
subrata_modak | 4b13c89 | 2009-01-22 09:51:33 +0000 | [diff] [blame] | 81 | int sync_pipes[2]; |
| 82 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | MSGBUF rcv_buf; |
| 84 | pid_t c_pid; |
| 85 | |
robbiew | 23499f0 | 2002-11-18 19:54:58 +0000 | [diff] [blame] | 86 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 88 | int lc; |
Cyril Hrubis | 7422562 | 2014-06-02 17:54:38 +0200 | [diff] [blame] | 89 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | |
Cyril Hrubis | 7422562 | 2014-06-02 17:54:38 +0200 | [diff] [blame] | 91 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 92 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Cyril Hrubis | 7422562 | 2014-06-02 17:54:38 +0200 | [diff] [blame] | 93 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 94 | #ifdef UCLINUX |
| 95 | maybe_run_child(&do_child_uclinux, "d", &msg_q_1); |
| 96 | #endif |
| 97 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 98 | setup(); /* global setup */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | |
subrata_modak | 4b13c89 | 2009-01-22 09:51:33 +0000 | [diff] [blame] | 100 | if (sync_pipe_create(sync_pipes, PIPE_NAME) == -1) |
subrata_modak | 8791f67 | 2008-05-20 10:11:00 +0000 | [diff] [blame] | 101 | tst_brkm(TBROK, cleanup, "sync_pipe_create failed"); |
subrata_modak | 2191ca8 | 2008-04-06 11:50:24 +0000 | [diff] [blame] | 102 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | /* The following loop checks looping state if -i option given */ |
| 104 | |
| 105 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 106 | /* reset tst_count in case we are looping */ |
| 107 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | |
| 109 | /* |
| 110 | * set up the queue here so that multiple test iterations |
| 111 | * will work. |
| 112 | */ |
| 113 | msgkey = getipckey(); |
| 114 | |
| 115 | /* create a message queue with read/write permission */ |
| 116 | if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW)) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 117 | == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | tst_brkm(TBROK, cleanup, "Can't create message queue"); |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * fork a child that will attempt to read a non-existent |
| 123 | * message from the queue |
| 124 | */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 125 | if ((c_pid = FORK_OR_VFORK()) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | tst_brkm(TBROK, cleanup, "could not fork"); |
| 127 | } |
| 128 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 129 | if (c_pid == 0) { /* child */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | /* |
| 131 | * Attempt to read a message without IPC_NOWAIT. |
| 132 | * With no message to read, the child sleeps. |
| 133 | */ |
subrata_modak | 2191ca8 | 2008-04-06 11:50:24 +0000 | [diff] [blame] | 134 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 135 | #ifdef UCLINUX |
| 136 | if (self_exec(av[0], "d", msg_q_1) < 0) { |
| 137 | tst_brkm(TBROK, cleanup, "could not self_exec"); |
| 138 | } |
| 139 | #else |
| 140 | do_child(); |
| 141 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 142 | } else { /* parent */ |
subrata_modak | 2191ca8 | 2008-04-06 11:50:24 +0000 | [diff] [blame] | 143 | |
subrata_modak | 8791f67 | 2008-05-20 10:11:00 +0000 | [diff] [blame] | 144 | if (sync_pipe_wait(sync_pipes) == -1) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 145 | tst_brkm(TBROK, cleanup, |
| 146 | "sync_pipe_wait failed"); |
subrata_modak | 8791f67 | 2008-05-20 10:11:00 +0000 | [diff] [blame] | 147 | |
subrata_modak | 4b13c89 | 2009-01-22 09:51:33 +0000 | [diff] [blame] | 148 | if (sync_pipe_close(sync_pipes, PIPE_NAME) == -1) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 149 | tst_brkm(TBROK, cleanup, |
| 150 | "sync_pipe_close failed"); |
subrata_modak | 8791f67 | 2008-05-20 10:11:00 +0000 | [diff] [blame] | 151 | |
subrata_modak | 2191ca8 | 2008-04-06 11:50:24 +0000 | [diff] [blame] | 152 | sleep(1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | |
| 154 | /* remove the queue */ |
| 155 | rm_queue(msg_q_1); |
| 156 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 157 | waitpid(c_pid, NULL, 0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 161 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 165 | * do_child() |
| 166 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 167 | void do_child(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 168 | { |
subrata_modak | 4b13c89 | 2009-01-22 09:51:33 +0000 | [diff] [blame] | 169 | if (sync_pipe_notify(sync_pipes) == -1) |
| 170 | tst_brkm(TBROK, cleanup, "sync_pipe_notify failed"); |
| 171 | |
| 172 | if (sync_pipe_close(sync_pipes, PIPE_NAME) == -1) |
| 173 | tst_brkm(TBROK, cleanup, "sync_pipe_close failed"); |
| 174 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 175 | TEST(msgrcv(msg_q_1, &rcv_buf, MSGSIZE, 1, 0)); |
| 176 | |
| 177 | if (TEST_RETURN != -1) { |
| 178 | tst_resm(TFAIL, "call succeeded when error expected"); |
| 179 | exit(-1); |
| 180 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 181 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 182 | switch (TEST_ERRNO) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 183 | case EIDRM: |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 184 | tst_resm(TPASS, "expected failure - errno = %d : %s", |
| 185 | TEST_ERRNO, strerror(TEST_ERRNO)); |
| 186 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 187 | /* mark the queue as invalid as it was removed */ |
| 188 | msg_q_1 = -1; |
| 189 | break; |
| 190 | default: |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 191 | tst_resm(TFAIL, |
| 192 | "call failed with an unexpected error - %d : %s", |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 193 | TEST_ERRNO, strerror(TEST_ERRNO)); |
| 194 | break; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 195 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 196 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 197 | /* if it exists, remove the message queue that was created */ |
| 198 | rm_queue(msg_q_1); |
| 199 | |
| 200 | exit(0); |
| 201 | } |
| 202 | |
| 203 | #ifdef UCLINUX |
| 204 | /* |
| 205 | * do_child_uclinux() - capture signals again, then run do_child() |
| 206 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 207 | void do_child_uclinux(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 208 | { |
subrata_modak | 4b13c89 | 2009-01-22 09:51:33 +0000 | [diff] [blame] | 209 | if (sync_pipe_create(sync_pipes, PIPE_NAME) == -1) |
| 210 | tst_brkm(TBROK, cleanup, "sync_pipe_create failed"); |
| 211 | |
Garrett Cooper | 5262667 | 2010-12-20 15:03:21 -0800 | [diff] [blame] | 212 | tst_sig(FORK, SIG_IGN, cleanup); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 213 | |
| 214 | do_child(); |
| 215 | } |
| 216 | #endif |
| 217 | |
| 218 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 219 | * setup() - performs all the ONE TIME setup for this test. |
| 220 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 221 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 222 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 223 | |
Garrett Cooper | 5262667 | 2010-12-20 15:03:21 -0800 | [diff] [blame] | 224 | tst_sig(FORK, SIG_IGN, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 225 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 226 | TEST_PAUSE; |
| 227 | |
| 228 | /* |
| 229 | * Create a temporary directory and cd into it. |
| 230 | * This helps to ensure that a unique msgkey is created. |
| 231 | * See ../lib/libipc.c for more information. |
| 232 | */ |
| 233 | tst_tmpdir(); |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * cleanup() - performs all the ONE TIME cleanup for this test at completion |
| 238 | * or premature exit. |
| 239 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 240 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 241 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 242 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 243 | tst_rmdir(); |
| 244 | |
Garrett Cooper | 5262667 | 2010-12-20 15:03:21 -0800 | [diff] [blame] | 245 | } |