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 | * msgsnd01.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * msgsnd01 - test that msgsnd() enqueues a message correctly |
| 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 | * enqueue the message |
| 32 | * check the return code |
| 33 | * if failure, issue a FAIL message. |
| 34 | * otherwise, |
| 35 | * if doing functionality testing |
| 36 | * stat the message queue |
| 37 | * check for # of bytes = MSGSIZE and # of messages = 1 |
| 38 | * if correct, |
| 39 | * issue a PASS message |
| 40 | * otherwise |
| 41 | * issue a FAIL message |
| 42 | * call cleanup |
| 43 | * |
| 44 | * USAGE: <for command-line> |
| 45 | * msgsnd01 [-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 "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 | void cleanup(void); |
| 65 | void setup(void); |
| 66 | |
| 67 | char *TCID = "msgsnd01"; |
| 68 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | |
| 70 | int msg_q_1; |
| 71 | MSGBUF msg_buf, rd_buf; |
| 72 | |
| 73 | struct msqid_ds qs_buf; |
| 74 | |
robbiew | 23499f0 | 2002-11-18 19:54:58 +0000 | [diff] [blame] | 75 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 77 | int lc; |
Cyril Hrubis | 7422562 | 2014-06-02 17:54:38 +0200 | [diff] [blame] | 78 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | |
Cyril Hrubis | 7422562 | 2014-06-02 17:54:38 +0200 | [diff] [blame] | 80 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 81 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | setup(); /* global setup */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | |
| 85 | /* The following loop checks looping state if -i option given */ |
| 86 | |
| 87 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 88 | /* reset tst_count in case we are looping */ |
| 89 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | |
| 91 | /* |
| 92 | * Use TEST macro to make the call |
| 93 | */ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 94 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | TEST(msgsnd(msg_q_1, &msg_buf, MSGSIZE, 0)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 96 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | if (TEST_RETURN == -1) { |
| 98 | tst_resm(TFAIL, "%s call failed - errno = %d : %s", |
| 99 | TCID, TEST_ERRNO, strerror(TEST_ERRNO)); |
| 100 | continue; |
| 101 | } |
| 102 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 103 | /* get the queue status */ |
| 104 | if (msgctl(msg_q_1, IPC_STAT, &qs_buf) == -1) { |
| 105 | tst_brkm(TBROK, cleanup, "Could not " |
| 106 | "get queue status"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 109 | if (qs_buf.msg_cbytes != MSGSIZE) { |
| 110 | tst_resm(TFAIL, "queue bytes != MSGSIZE"); |
| 111 | } |
| 112 | |
| 113 | if (qs_buf.msg_qnum != 1) { |
| 114 | tst_resm(TFAIL, "queue message != 1"); |
| 115 | } |
| 116 | |
| 117 | tst_resm(TPASS, "queue bytes = MSGSIZE and " |
| 118 | "queue messages = 1"); |
| 119 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | /* |
| 121 | * remove the message by reading from the queue |
| 122 | */ |
| 123 | if (msgrcv(msg_q_1, &rd_buf, MSGSIZE, 1, 0) == -1) { |
| 124 | tst_brkm(TBROK, cleanup, "Could not read from queue"); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 129 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /* |
| 133 | * setup() - performs all the ONE TIME setup for this test. |
| 134 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 135 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 137 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 139 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | TEST_PAUSE; |
| 141 | |
| 142 | /* |
| 143 | * Create a temporary directory and cd into it. |
| 144 | * This helps to ensure that a unique msgkey is created. |
| 145 | * See ../lib/libipc.c for more information. |
| 146 | */ |
| 147 | tst_tmpdir(); |
| 148 | |
| 149 | msgkey = getipckey(); |
| 150 | |
| 151 | /* create a message queue with read/write permissions */ |
| 152 | if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW)) == -1) { |
| 153 | tst_brkm(TBROK, cleanup, "Can't create message queue"); |
| 154 | } |
| 155 | |
| 156 | /* initialize the message buffer */ |
| 157 | init_buf(&msg_buf, MSGTYPE, MSGSIZE); |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * cleanup() - performs all the ONE TIME cleanup for this test at completion |
| 162 | * or premature exit. |
| 163 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 164 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | { |
| 166 | /* if it exists, remove the message queue if it exists */ |
| 167 | rm_queue(msg_q_1); |
| 168 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | tst_rmdir(); |
| 170 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 171 | } |