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