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 | * msgctl04.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * msgctl04 - test for EACCES, EFAULT and EINVAL errors using |
| 26 | * a variety of incorrect calls. |
| 27 | * |
| 28 | * ALGORITHM |
| 29 | * create two message queues |
| 30 | * loop if that option was specified |
| 31 | * try to access a queue with some invalid argument |
| 32 | * check the errno value |
| 33 | * issue a PASS message if we get EACCES, EFAULT or EINVAL |
| 34 | * depending on the test case |
| 35 | * otherwise, the tests fails |
| 36 | * issue a FAIL message |
| 37 | * call cleanup |
| 38 | * |
| 39 | * USAGE: <for command-line> |
| 40 | * msgctl04 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 41 | * where, -c n : Run n copies concurrently. |
| 42 | * -e : Turn on errno logging. |
| 43 | * -i n : Execute test n times. |
| 44 | * -I x : Execute test for x seconds. |
| 45 | * -P x : Pause for x seconds between iterations. |
| 46 | * -t : Turn on syscall timing. |
| 47 | * |
| 48 | * HISTORY |
| 49 | * 03/2001 - Written by Wayne Boyer |
subrata_modak | e8e78f6 | 2008-04-06 11:41:49 +0000 | [diff] [blame] | 50 | * 12/03/2008 Matthieu Fertré (Matthieu.Fertre@irisa.fr) |
| 51 | * - Fix concurrency issue. The second key used for this test could |
| 52 | * conflict with the key from another task. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | * |
| 54 | * RESTRICTIONS |
| 55 | * none |
| 56 | */ |
robbiew | c277a36 | 2001-09-12 18:44:14 +0000 | [diff] [blame] | 57 | #include <pwd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | |
| 59 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | |
robbiew | 23499f0 | 2002-11-18 19:54:58 +0000 | [diff] [blame] | 61 | #include "ipcmsg.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | |
| 63 | char *TCID = "msgctl04"; |
| 64 | int TST_TOTAL = 6; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | |
robbiew | c277a36 | 2001-09-12 18:44:14 +0000 | [diff] [blame] | 66 | char nobody_uid[] = "nobody"; |
| 67 | struct passwd *ltpuser; |
| 68 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | int msg_q_1 = -1; /* The message queue id created in setup */ |
| 70 | int msg_q_2 = -1; /* Another queue id created in setup */ |
| 71 | int bad_q = -1; /* a value to use as a bad queue id */ |
| 72 | |
| 73 | struct msqid_ds q_buf; |
| 74 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 75 | struct test_case_t { /* This allows testing of many negative */ |
| 76 | int *queue_id; /* test cases that can all use the same */ |
| 77 | int ipc_cmd; /* basic test setup. */ |
| 78 | struct msqid_ds *buf; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | int error; |
| 80 | } TC[] = { |
| 81 | /* EACCES - there is no read permission for the queue */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 82 | { |
| 83 | &msg_q_1, IPC_STAT, &q_buf, EACCES}, |
| 84 | /* EFAULT - the structure address is invalid - IPC_STAT */ |
| 85 | { |
| 86 | &msg_q_2, IPC_STAT, (struct msqid_ds *)-1, EFAULT}, |
| 87 | /* EFAULT - the structure address is invalid - IPC_SET */ |
| 88 | { |
| 89 | &msg_q_2, IPC_SET, (struct msqid_ds *)-1, EFAULT}, |
| 90 | /* EINVAL - the command (-1) is invalid */ |
| 91 | { |
| 92 | &msg_q_2, -1, &q_buf, EINVAL}, |
| 93 | /* EINVAL - the queue id is invalid - IPC_STAT */ |
| 94 | { |
| 95 | &bad_q, IPC_STAT, &q_buf, EINVAL}, |
| 96 | /* EINVAL - the queue id is invalid - IPC_SET */ |
| 97 | { |
| 98 | &bad_q, IPC_SET, &q_buf, EINVAL} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
robbiew | 23499f0 | 2002-11-18 19:54:58 +0000 | [diff] [blame] | 101 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 103 | int lc; |
Cyril Hrubis | 7422562 | 2014-06-02 17:54:38 +0200 | [diff] [blame] | 104 | const char *msg; |
robbiew | 23499f0 | 2002-11-18 19:54:58 +0000 | [diff] [blame] | 105 | int i; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | |
Cyril Hrubis | 7422562 | 2014-06-02 17:54:38 +0200 | [diff] [blame] | 107 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 108 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 110 | setup(); /* global setup */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | |
| 112 | /* The following loop checks looping state if -i option given */ |
| 113 | |
| 114 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 115 | /* reset tst_count in case we are looping */ |
| 116 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | |
| 118 | /* loop through the test cases */ |
| 119 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 120 | for (i = 0; i < TST_TOTAL; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | |
| 122 | TEST(msgctl(*(TC[i].queue_id), TC[i].ipc_cmd, |
| 123 | TC[i].buf)); |
| 124 | |
| 125 | if (TEST_RETURN != -1) { |
robbiew | 9ae92ea | 2004-08-11 15:55:29 +0000 | [diff] [blame] | 126 | tst_resm(TFAIL, "msgctl() call succeeded " |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | "on expected fail"); |
| 128 | continue; |
| 129 | } |
| 130 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | if (TEST_ERRNO == TC[i].error) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 132 | tst_resm(TPASS | TTERRNO, "expected failure"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 134 | tst_resm(TFAIL | TTERRNO, "unexpected error"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | tst_resm(TINFO, "expected error is - %d : %s", |
| 136 | TC[i].error, strerror(TC[i].error)); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | cleanup(); |
| 142 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 143 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* |
| 147 | * setup() - performs all the ONE TIME setup for this test. |
| 148 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 149 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | { |
subrata_modak | e8e78f6 | 2008-04-06 11:41:49 +0000 | [diff] [blame] | 151 | key_t msgkey2; |
| 152 | |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 153 | tst_require_root(NULL); |
| 154 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 156 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | TEST_PAUSE; |
| 158 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 159 | /* Switch to nobody user for correct error code collection */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 160 | ltpuser = getpwnam(nobody_uid); |
vapier | ce6a0e3 | 2009-08-28 13:14:39 +0000 | [diff] [blame] | 161 | if (setuid(ltpuser->pw_uid) == -1) |
| 162 | tst_resm(TINFO, "setuid(%d) failed", ltpuser->pw_uid); |
robbiew | c277a36 | 2001-09-12 18:44:14 +0000 | [diff] [blame] | 163 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | /* |
| 165 | * Create a temporary directory and cd into it. |
| 166 | * This helps to ensure that a unique msgkey is created. |
| 167 | * See ../lib/libipc.c for more information. |
| 168 | */ |
| 169 | tst_tmpdir(); |
| 170 | |
| 171 | msgkey = getipckey(); |
| 172 | |
subrata_modak | e8e78f6 | 2008-04-06 11:41:49 +0000 | [diff] [blame] | 173 | /* Get an new IPC resource key. */ |
| 174 | msgkey2 = getipckey(); |
| 175 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | /* now we have a key, so let's create a message queue */ |
| 177 | if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL)) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 178 | tst_brkm(TBROK | TERRNO, cleanup, |
| 179 | "Can't create message queue #1"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /* now let's create another message queue with read & write access */ |
subrata_modak | e8e78f6 | 2008-04-06 11:41:49 +0000 | [diff] [blame] | 183 | if ((msg_q_2 = |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 184 | msgget(msgkey2, IPC_CREAT | IPC_EXCL | MSG_RD | MSG_WR)) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 185 | tst_brkm(TBROK | TERRNO, cleanup, |
| 186 | "Can't create message queue #2"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * cleanup() - performs all the ONE TIME cleanup for this test at completion |
| 192 | * or premature exit. |
| 193 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 194 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 195 | { |
| 196 | /* |
| 197 | * remove the message queues that were created. |
| 198 | */ |
| 199 | rm_queue(msg_q_1); |
| 200 | |
| 201 | rm_queue(msg_q_2); |
| 202 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 203 | tst_rmdir(); |
| 204 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 205 | } |