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 | * msgrcv01.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * msgrcv01 - test that msgrcv() receives the expected message |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * create a message queue |
| 29 | * initialize a message buffer with a known message and type |
| 30 | * loop if that option was specified |
| 31 | * fork a child to receive the message |
| 32 | * parent enqueues the message then exits |
| 33 | * check the return code |
| 34 | * if failure, issue a FAIL message. |
| 35 | * otherwise, |
| 36 | * if doing functionality testing |
| 37 | * build a new message and compare it to the one received |
| 38 | * if they are the same, |
| 39 | * issue a PASS message |
| 40 | * otherwise |
| 41 | * issue a FAIL message |
| 42 | * call cleanup |
| 43 | * |
| 44 | * USAGE: <for command-line> |
| 45 | * msgrcv01 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 46 | * where, -c n : Run n copies concurrently. |
| 47 | * -f : Turn off functionality Testing. |
| 48 | * -i n : Execute test n times. |
| 49 | * -I x : Execute test for x seconds. |
| 50 | * -P x : Pause for x seconds between iterations. |
| 51 | * -t : Turn on syscall timing. |
| 52 | * |
| 53 | * HISTORY |
| 54 | * 03/2001 - Written by Wayne Boyer |
| 55 | * |
| 56 | * RESTRICTIONS |
| 57 | * none |
| 58 | */ |
| 59 | |
| 60 | #include <string.h> |
robbiew | 23499f0 | 2002-11-18 19:54:58 +0000 | [diff] [blame] | 61 | #include <sys/wait.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | |
| 63 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | |
robbiew | 23499f0 | 2002-11-18 19:54:58 +0000 | [diff] [blame] | 65 | #include "ipcmsg.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | |
| 67 | void cleanup(void); |
| 68 | void setup(void); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 69 | void do_child(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | |
| 71 | char *TCID = "msgrcv01"; |
| 72 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | |
| 74 | int msg_q_1; |
| 75 | MSGBUF snd_buf, rcv_buf, cmp_buf; |
| 76 | |
| 77 | pid_t c_pid; |
| 78 | |
robbiew | 23499f0 | 2002-11-18 19:54:58 +0000 | [diff] [blame] | 79 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 81 | int lc; |
Cyril Hrubis | 7422562 | 2014-06-02 17:54:38 +0200 | [diff] [blame] | 82 | const char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | void check_functionality(void); |
| 84 | int status, e_code; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | |
Cyril Hrubis | 7422562 | 2014-06-02 17:54:38 +0200 | [diff] [blame] | 86 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 87 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Cyril Hrubis | 7422562 | 2014-06-02 17:54:38 +0200 | [diff] [blame] | 88 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 89 | #ifdef UCLINUX |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 90 | maybe_run_child(&do_child, "d", &msg_q_1); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 91 | #endif |
| 92 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 93 | setup(); /* global setup */ |
plars | 5e718c3 | 2001-09-14 15:56:02 +0000 | [diff] [blame] | 94 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 95 | /* The following loop checks looping state if -i option given */ |
plars | 5e718c3 | 2001-09-14 15:56:02 +0000 | [diff] [blame] | 96 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 97 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 98 | /* reset tst_count in case we are looping */ |
| 99 | tst_count = 0; |
plars | 5e718c3 | 2001-09-14 15:56:02 +0000 | [diff] [blame] | 100 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | /* |
| 102 | * fork a child to read from the queue while the parent |
| 103 | * enqueues the message to be read. |
| 104 | */ |
| 105 | if ((c_pid = FORK_OR_VFORK()) == -1) { |
| 106 | tst_brkm(TBROK, cleanup, "could not fork"); |
| 107 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 109 | if (c_pid == 0) { /* child */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 110 | #ifdef UCLINUX |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 111 | if (self_exec(av[0], "d", msg_q_1) < 0) { |
| 112 | tst_brkm(TBROK, cleanup, "could not self_exec"); |
| 113 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 114 | #else |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 115 | do_child(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 116 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 117 | } else { /* parent */ |
| 118 | /* put the message on the queue */ |
| 119 | if (msgsnd(msg_q_1, &snd_buf, MSGSIZE, 0) == -1) { |
| 120 | tst_brkm(TBROK, cleanup, |
| 121 | "Couldn't enqueue" " message"); |
| 122 | } |
| 123 | /* wait for the child to finish */ |
| 124 | wait(&status); |
| 125 | /* make sure the child returned a good exit status */ |
| 126 | e_code = status >> 8; |
| 127 | if (e_code != 0) { |
| 128 | tst_resm(TFAIL, "Failures reported above"); |
| 129 | } |
plars | 5e718c3 | 2001-09-14 15:56:02 +0000 | [diff] [blame] | 130 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 131 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | } |
| 133 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 134 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 135 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | |
robbiew | 23499f0 | 2002-11-18 19:54:58 +0000 | [diff] [blame] | 137 | /** NOT REACHED **/ |
robbiew | 23499f0 | 2002-11-18 19:54:58 +0000 | [diff] [blame] | 138 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /* |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 142 | * do_child() |
| 143 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 144 | void do_child(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 145 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 146 | int retval = 0; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 147 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 148 | TEST(msgrcv(msg_q_1, &rcv_buf, MSGSIZE, 1, 0)); |
| 149 | |
| 150 | if (TEST_RETURN == -1) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 151 | retval = 1; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 152 | tst_resm(TFAIL, "%s call failed - errno = %d : %s", |
| 153 | TCID, TEST_ERRNO, strerror(TEST_ERRNO)); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 154 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 155 | /* |
| 156 | * Build a new message and compare it |
| 157 | * with the one received. |
| 158 | */ |
| 159 | init_buf(&cmp_buf, MSGTYPE, MSGSIZE); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 160 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 161 | if (strcmp(rcv_buf.mtext, cmp_buf.mtext) == 0) { |
| 162 | tst_resm(TPASS, |
| 163 | "message received = " "message sent"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 164 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 165 | retval = 1; |
| 166 | tst_resm(TFAIL, |
| 167 | "message received != " "message sent"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 168 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 169 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 170 | exit(retval); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 174 | * setup() - performs all the ONE TIME setup for this test. |
| 175 | */ |
plars | 5e718c3 | 2001-09-14 15:56:02 +0000 | [diff] [blame] | 176 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 178 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 179 | tst_sig(FORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 180 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 181 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 183 | /* |
| 184 | * Create a temporary directory and cd into it. |
| 185 | * This helps to ensure that a unique msgkey is created. |
| 186 | * See ../lib/libipc.c for more information. |
| 187 | */ |
| 188 | tst_tmpdir(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 189 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 190 | msgkey = getipckey(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 192 | /* create a message queue with read/write permission */ |
| 193 | if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW)) == -1) { |
| 194 | tst_brkm(TBROK, cleanup, "Can't create message queue"); |
| 195 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 196 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 197 | /* initialize the message buffer */ |
| 198 | init_buf(&snd_buf, MSGTYPE, MSGSIZE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /* |
| 202 | * cleanup() - performs all the ONE TIME cleanup for this test at completion |
| 203 | * or premature exit. |
| 204 | */ |
plars | 5e718c3 | 2001-09-14 15:56:02 +0000 | [diff] [blame] | 205 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 206 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 207 | /* if it exists, remove the message queue that was created */ |
| 208 | rm_queue(msg_q_1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 209 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 210 | tst_rmdir(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 211 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 212 | } |