blob: e68b7c6cfba1469a4f1a58f67f19ea6ccdeaa92a [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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 */
robbiewc277a362001-09-12 18:44:14 +000053#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000054#include "test.h"
plars865695b2001-08-27 22:15:12 +000055
robbiew23499f02002-11-18 19:54:58 +000056#include "ipcmsg.h"
plars865695b2001-08-27 22:15:12 +000057
58void cleanup(void);
59void setup(void);
60
61char *TCID = "msgsnd02";
62int TST_TOTAL = 2;
plars865695b2001-08-27 22:15:12 +000063
robbiewc277a362001-09-12 18:44:14 +000064char nobody_uid[] = "nobody";
65struct passwd *ltpuser;
66
plars865695b2001-08-27 22:15:12 +000067int msg_q_1 = -1; /* The message queue id created in setup */
68MSGBUF msg_buf; /* a buffer for the message to queue */
69int bad_q = -1; /* a value to use as a bad queue ID */
70
71struct 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_modak56207ce2009-03-23 13:35:39 +000077 {
78 &msg_q_1, &msg_buf, EACCES},
79 /* EFAULT - the message buffer address is invalid */
80 {
81&msg_q_1, NULL, EFAULT},};
subrata_modakbdbaec52009-02-26 12:14:51 +000082
robbiew23499f02002-11-18 19:54:58 +000083int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000084{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020085 int lc;
Cyril Hrubis74225622014-06-02 17:54:38 +020086 const char *msg;
plars865695b2001-08-27 22:15:12 +000087 int i;
88
Cyril Hrubis74225622014-06-02 17:54:38 +020089 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080090 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000091
subrata_modak56207ce2009-03-23 13:35:39 +000092 setup(); /* global setup */
plars865695b2001-08-27 22:15:12 +000093
94 /* The following loop checks looping state if -i option given */
95
96 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080097 /* reset tst_count in case we are looping */
98 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000099
100 /*
101 * loop through the test cases
102 */
subrata_modakbdbaec52009-02-26 12:14:51 +0000103
subrata_modak56207ce2009-03-23 13:35:39 +0000104 for (i = 0; i < TST_TOTAL; i++) {
plars865695b2001-08-27 22:15:12 +0000105 TEST(msgsnd(*(TC[i].queue_id), TC[i].buffer, 1, 0));
subrata_modakbdbaec52009-02-26 12:14:51 +0000106
plars865695b2001-08-27 22:15:12 +0000107 if (TEST_RETURN != -1) {
108 tst_resm(TFAIL, "call succeeded unexpectedly");
109 continue;
110 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000111
plars865695b2001-08-27 22:15:12 +0000112 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 Cooper53740502010-12-16 00:04:01 -0800125 tst_exit();
plars865695b2001-08-27 22:15:12 +0000126}
127
128/*
129 * setup() - performs all the ONE TIME setup for this test.
130 */
subrata_modak56207ce2009-03-23 13:35:39 +0000131void setup(void)
plars865695b2001-08-27 22:15:12 +0000132{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200133 tst_require_root(NULL);
Garrett Cooper2c282152010-12-16 00:55:50 -0800134
plars865695b2001-08-27 22:15:12 +0000135 tst_sig(NOFORK, DEF_HANDLER, cleanup);
136
plars865695b2001-08-27 22:15:12 +0000137 TEST_PAUSE;
138
subrata_modak56207ce2009-03-23 13:35:39 +0000139 /* Switch to nobody user for correct error code collection */
subrata_modak56207ce2009-03-23 13:35:39 +0000140 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 }
robbiewc277a362001-09-12 18:44:14 +0000146
plars865695b2001-08-27 22:15:12 +0000147 /*
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_modak56207ce2009-03-23 13:35:39 +0000170void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000171{
172 /* if it exists, remove the message queue that was created */
173 rm_queue(msg_q_1);
174
plars865695b2001-08-27 22:15:12 +0000175 tst_rmdir();
176
Chris Dearmanec6edca2012-10-17 19:54:01 -0700177}