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