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 | * msgsnd02.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * msgsnd02 - test for EACCES and EFAULT errors |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * create a message queue without write permission |
| 29 | * create a trivial message buffer |
| 30 | * loop if that option was specified |
| 31 | * call msgsnd() using two different invalid cases |
| 32 | * check the errno value |
| 33 | * issue a PASS message if we get EACCES or EFAULT |
| 34 | * otherwise, the tests fails |
| 35 | * issue a FAIL message |
| 36 | * call cleanup |
| 37 | * |
| 38 | * USAGE: <for command-line> |
| 39 | * msgsnd02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 40 | * where, -c n : Run n copies concurrently. |
| 41 | * -e : Turn on errno logging. |
| 42 | * -i n : Execute test n times. |
| 43 | * -I x : Execute test for x seconds. |
| 44 | * -P x : Pause for x seconds between iterations. |
| 45 | * -t : Turn on syscall timing. |
| 46 | * |
| 47 | * HISTORY |
| 48 | * 03/2001 - Written by Wayne Boyer |
| 49 | * |
| 50 | * RESTRICTIONS |
| 51 | * none |
| 52 | */ |
robbiew | c277a36 | 2001-09-12 18:44:14 +0000 | [diff] [blame] | 53 | #include <pwd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
robbiew | 23499f0 | 2002-11-18 19:54:58 +0000 | [diff] [blame] | 56 | #include "ipcmsg.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | |
| 58 | void cleanup(void); |
| 59 | void setup(void); |
| 60 | |
| 61 | char *TCID = "msgsnd02"; |
| 62 | int TST_TOTAL = 2; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | |
robbiew | c277a36 | 2001-09-12 18:44:14 +0000 | [diff] [blame] | 64 | char nobody_uid[] = "nobody"; |
| 65 | struct passwd *ltpuser; |
| 66 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | int msg_q_1 = -1; /* The message queue id created in setup */ |
| 68 | MSGBUF msg_buf; /* a buffer for the message to queue */ |
| 69 | int bad_q = -1; /* a value to use as a bad queue ID */ |
| 70 | |
| 71 | struct test_case_t { |
| 72 | int *queue_id; |
| 73 | MSGBUF *buffer; |
| 74 | int error; |
| 75 | } TC[] = { |
| 76 | /* EACCES - there is no write permission for the queue */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 77 | { |
| 78 | &msg_q_1, &msg_buf, EACCES}, |
| 79 | /* EFAULT - the message buffer address is invalid */ |
| 80 | { |
| 81 | &msg_q_1, NULL, EFAULT},}; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 82 | |
robbiew | 23499f0 | 2002-11-18 19:54:58 +0000 | [diff] [blame] | 83 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 85 | int lc; |
Cyril Hrubis | 7422562 | 2014-06-02 17:54:38 +0200 | [diff] [blame] | 86 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | int i; |
| 88 | |
Cyril Hrubis | 7422562 | 2014-06-02 17:54:38 +0200 | [diff] [blame] | 89 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 90 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 92 | setup(); /* global setup */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | |
| 94 | /* The following loop checks looping state if -i option given */ |
| 95 | |
| 96 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 97 | /* reset tst_count in case we are looping */ |
| 98 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | |
| 100 | /* |
| 101 | * loop through the test cases |
| 102 | */ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 103 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 104 | for (i = 0; i < TST_TOTAL; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | TEST(msgsnd(*(TC[i].queue_id), TC[i].buffer, 1, 0)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 106 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | if (TEST_RETURN != -1) { |
| 108 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 109 | continue; |
| 110 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 111 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | if (TEST_ERRNO == TC[i].error) { |
| 113 | tst_resm(TPASS, "expected failure - " |
| 114 | "errno = %d : %s", TEST_ERRNO, |
| 115 | strerror(TEST_ERRNO)); |
| 116 | } else { |
| 117 | tst_resm(TFAIL, "unexpected error - %d : %s", |
| 118 | TEST_ERRNO, strerror(TEST_ERRNO)); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | cleanup(); |
| 124 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 125 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /* |
| 129 | * setup() - performs all the ONE TIME setup for this test. |
| 130 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 131 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | { |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 133 | tst_require_root(NULL); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 134 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 136 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | TEST_PAUSE; |
| 138 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 139 | /* Switch to nobody user for correct error code collection */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 140 | ltpuser = getpwnam(nobody_uid); |
| 141 | if (setuid(ltpuser->pw_uid) == -1) { |
| 142 | tst_resm(TINFO, "setuid failed to " |
| 143 | "to set the effective uid to %d", ltpuser->pw_uid); |
| 144 | perror("setuid"); |
| 145 | } |
robbiew | c277a36 | 2001-09-12 18:44:14 +0000 | [diff] [blame] | 146 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | /* |
| 148 | * Create a temporary directory and cd into it. |
| 149 | * This helps to ensure that a unique msgkey is created. |
| 150 | * See ../lib/libipc.c for more information. |
| 151 | */ |
| 152 | tst_tmpdir(); |
| 153 | |
| 154 | msgkey = getipckey(); |
| 155 | |
| 156 | /* create a message queue without write permission */ |
| 157 | if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RD)) == -1) { |
| 158 | tst_brkm(TBROK, cleanup, "Can't create message queue"); |
| 159 | } |
| 160 | |
| 161 | /* initialize the message buffer with something trivial */ |
| 162 | msg_buf.mtype = MSGTYPE; |
| 163 | msg_buf.mtext[0] = 'a'; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * cleanup() - performs all the ONE TIME cleanup for this test at completion |
| 168 | * or premature exit. |
| 169 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 170 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | { |
| 172 | /* if it exists, remove the message queue that was created */ |
| 173 | rm_queue(msg_q_1); |
| 174 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | tst_rmdir(); |
| 176 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 177 | } |