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 | * msgrcv02.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * msgrcv02 - test for EACCES and EFAULT errors |
| 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 |
| 31 | * create another message queue without read/write permissions |
| 32 | * loop if that option was specified |
| 33 | * call msgrcv() using two different invalid cases |
| 34 | * check the errno value |
| 35 | * issue a PASS message if we get EACCES or EFAULT |
| 36 | * otherwise, the tests fails |
| 37 | * issue a FAIL message |
| 38 | * call cleanup |
| 39 | * |
| 40 | * USAGE: <for command-line> |
| 41 | * msgrcv02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 42 | * where, -c n : Run n copies concurrently. |
| 43 | * -e : Turn on errno logging. |
| 44 | * -i n : Execute test n times. |
| 45 | * -I x : Execute test for x seconds. |
| 46 | * -P x : Pause for x seconds between iterations. |
| 47 | * -t : Turn on syscall timing. |
| 48 | * |
| 49 | * HISTORY |
| 50 | * 03/2001 - Written by Wayne Boyer |
subrata_modak | 6d2b29e | 2008-04-06 11:46:11 +0000 | [diff] [blame] | 51 | * 14/03/2008 Matthieu Fertré (Matthieu.Fertre@irisa.fr) |
| 52 | * - Fix concurrency issue. The second key used for this test could |
| 53 | * conflict with the key from another task. |
| 54 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | * |
| 56 | * RESTRICTIONS |
| 57 | * none |
| 58 | */ |
| 59 | |
robbiew | c277a36 | 2001-09-12 18:44:14 +0000 | [diff] [blame] | 60 | #include <pwd.h> |
| 61 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | |
| 66 | void cleanup(void); |
| 67 | void setup(void); |
| 68 | |
| 69 | char *TCID = "msgrcv02"; |
| 70 | int TST_TOTAL = 2; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | |
robbiew | c277a36 | 2001-09-12 18:44:14 +0000 | [diff] [blame] | 72 | char nobody_uid[] = "nobody"; |
| 73 | struct passwd *ltpuser; |
| 74 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | int msg_q_1 = -1; /* The message queue ID created in setup */ |
| 76 | int msg_q_2 = -1; /* Another message queue ID created in setup */ |
| 77 | MSGBUF snd_buf, rcv_buf; |
| 78 | |
| 79 | struct test_case_t { |
| 80 | int *queue_id; |
| 81 | MSGBUF *mbuf; |
| 82 | int error; |
| 83 | } TC[] = { |
| 84 | /* EACCES - the queue has no read access */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | { |
| 86 | &msg_q_2, &rcv_buf, EACCES}, |
| 87 | /* EFAULT - the message buffer address is invalid */ |
| 88 | { |
| 89 | &msg_q_1, (MSGBUF *) - 1, EFAULT} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
robbiew | 23499f0 | 2002-11-18 19:54:58 +0000 | [diff] [blame] | 92 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 94 | int lc; |
Cyril Hrubis | 7422562 | 2014-06-02 17:54:38 +0200 | [diff] [blame] | 95 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | int i; |
| 97 | |
Cyril Hrubis | 7422562 | 2014-06-02 17:54:38 +0200 | [diff] [blame] | 98 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 99 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | setup(); /* global setup */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | |
| 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 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 109 | for (i = 0; i < TST_TOTAL; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | |
| 111 | /* |
| 112 | * Use the TEST macro to make the call |
| 113 | */ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 114 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | TEST(msgrcv(*(TC[i].queue_id), TC[i].mbuf, MSGSIZE, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 116 | 1, IPC_NOWAIT)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 117 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | if (TEST_RETURN != -1) { |
| 119 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 120 | continue; |
| 121 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 122 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | if (TEST_ERRNO == TC[i].error) { |
| 124 | tst_resm(TPASS, "expected failure - errno = " |
| 125 | "%d : %s", TEST_ERRNO, |
| 126 | strerror(TEST_ERRNO)); |
| 127 | } else { |
| 128 | tst_resm(TFAIL, "call failed with an " |
| 129 | "unexpected error - %d : %s", |
| 130 | TEST_ERRNO, strerror(TEST_ERRNO)); |
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 | } |
| 134 | |
| 135 | cleanup(); |
| 136 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 137 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /* |
| 141 | * setup() - performs all the ONE TIME setup for this test. |
| 142 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 143 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | { |
subrata_modak | 6d2b29e | 2008-04-06 11:46:11 +0000 | [diff] [blame] | 145 | key_t msgkey2; |
| 146 | |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 147 | tst_require_root(NULL); |
| 148 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 150 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | TEST_PAUSE; |
| 152 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 153 | /* Switch to nobody user for correct error code collection */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 154 | ltpuser = getpwnam(nobody_uid); |
| 155 | if (setuid(ltpuser->pw_uid) == -1) { |
| 156 | tst_resm(TINFO, "setuid failed to " |
| 157 | "to set the effective uid to %d", ltpuser->pw_uid); |
| 158 | perror("setuid"); |
| 159 | } |
robbiew | c277a36 | 2001-09-12 18:44:14 +0000 | [diff] [blame] | 160 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | /* |
| 162 | * Create a temporary directory and cd into it. |
| 163 | * This helps to ensure that a unique msgkey is created. |
| 164 | * See ../lib/libipc.c for more information. |
| 165 | */ |
| 166 | tst_tmpdir(); |
| 167 | |
| 168 | msgkey = getipckey(); |
| 169 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 170 | /* Get an new IPC resource key. */ |
subrata_modak | 6d2b29e | 2008-04-06 11:46:11 +0000 | [diff] [blame] | 171 | msgkey2 = getipckey(); |
| 172 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | /* create a message queue with read/write permission */ |
| 174 | if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW)) == -1) { |
| 175 | tst_brkm(TBROK, cleanup, "Can't create message queue #1"); |
| 176 | } |
| 177 | |
| 178 | /* initialize a message buffer */ |
| 179 | init_buf(&snd_buf, MSGTYPE, MSGSIZE); |
| 180 | |
| 181 | /* put it on msq_q_1 */ |
| 182 | if (msgsnd(msg_q_1, &snd_buf, MSGSIZE, IPC_NOWAIT) == -1) { |
| 183 | tst_brkm(TBROK, cleanup, "Couldn't put message on queue"); |
| 184 | } |
| 185 | |
| 186 | /* create a message queue without read/write permission */ |
subrata_modak | 6d2b29e | 2008-04-06 11:46:11 +0000 | [diff] [blame] | 187 | if ((msg_q_2 = msgget(msgkey2, IPC_CREAT | IPC_EXCL)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 188 | tst_brkm(TBROK, cleanup, "Can't create message queue #2"); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * cleanup() - performs all the ONE TIME cleanup for this test at completion |
| 194 | * or premature exit. |
| 195 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 196 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 197 | { |
| 198 | /* if it exists, remove the message queue #1 */ |
| 199 | rm_queue(msg_q_1); |
| 200 | |
| 201 | /* if it exists, remove the message queue #2 */ |
| 202 | rm_queue(msg_q_2); |
| 203 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | tst_rmdir(); |
| 205 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 206 | } |