blob: 61bc26ae0a856d13eb08039df9766037af21f018 [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 * 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_modak6d2b29e2008-04-06 11:46:11 +000051 * 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
plars865695b2001-08-27 22:15:12 +000055 *
56 * RESTRICTIONS
57 * none
58 */
59
robbiewc277a362001-09-12 18:44:14 +000060#include <pwd.h>
61
plars865695b2001-08-27 22:15:12 +000062#include "test.h"
plars865695b2001-08-27 22:15:12 +000063
robbiew23499f02002-11-18 19:54:58 +000064#include "ipcmsg.h"
plars865695b2001-08-27 22:15:12 +000065
66void cleanup(void);
67void setup(void);
68
69char *TCID = "msgrcv02";
70int TST_TOTAL = 2;
plars865695b2001-08-27 22:15:12 +000071
robbiewc277a362001-09-12 18:44:14 +000072char nobody_uid[] = "nobody";
73struct passwd *ltpuser;
74
plars865695b2001-08-27 22:15:12 +000075int msg_q_1 = -1; /* The message queue ID created in setup */
76int msg_q_2 = -1; /* Another message queue ID created in setup */
77MSGBUF snd_buf, rcv_buf;
78
79struct test_case_t {
80 int *queue_id;
81 MSGBUF *mbuf;
82 int error;
83} TC[] = {
84 /* EACCES - the queue has no read access */
subrata_modak56207ce2009-03-23 13:35:39 +000085 {
86 &msg_q_2, &rcv_buf, EACCES},
87 /* EFAULT - the message buffer address is invalid */
88 {
89 &msg_q_1, (MSGBUF *) - 1, EFAULT}
plars865695b2001-08-27 22:15:12 +000090};
91
robbiew23499f02002-11-18 19:54:58 +000092int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000093{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020094 int lc;
Cyril Hrubis74225622014-06-02 17:54:38 +020095 const char *msg;
plars865695b2001-08-27 22:15:12 +000096 int i;
97
Cyril Hrubis74225622014-06-02 17:54:38 +020098 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080099 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000100
subrata_modak56207ce2009-03-23 13:35:39 +0000101 setup(); /* global setup */
plars865695b2001-08-27 22:15:12 +0000102
103 /* The following loop checks looping state if -i option given */
104
105 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800106 /* reset tst_count in case we are looping */
107 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000108
subrata_modak56207ce2009-03-23 13:35:39 +0000109 for (i = 0; i < TST_TOTAL; i++) {
plars865695b2001-08-27 22:15:12 +0000110
111 /*
112 * Use the TEST macro to make the call
113 */
subrata_modakbdbaec52009-02-26 12:14:51 +0000114
plars865695b2001-08-27 22:15:12 +0000115 TEST(msgrcv(*(TC[i].queue_id), TC[i].mbuf, MSGSIZE,
subrata_modak56207ce2009-03-23 13:35:39 +0000116 1, IPC_NOWAIT));
subrata_modakbdbaec52009-02-26 12:14:51 +0000117
plars865695b2001-08-27 22:15:12 +0000118 if (TEST_RETURN != -1) {
119 tst_resm(TFAIL, "call succeeded unexpectedly");
120 continue;
121 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000122
plars865695b2001-08-27 22:15:12 +0000123 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_modak56207ce2009-03-23 13:35:39 +0000131 }
plars865695b2001-08-27 22:15:12 +0000132 }
133 }
134
135 cleanup();
136
Garrett Cooper53740502010-12-16 00:04:01 -0800137 tst_exit();
plars865695b2001-08-27 22:15:12 +0000138}
139
140/*
141 * setup() - performs all the ONE TIME setup for this test.
142 */
subrata_modak56207ce2009-03-23 13:35:39 +0000143void setup(void)
plars865695b2001-08-27 22:15:12 +0000144{
subrata_modak6d2b29e2008-04-06 11:46:11 +0000145 key_t msgkey2;
146
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200147 tst_require_root(NULL);
148
plars865695b2001-08-27 22:15:12 +0000149 tst_sig(NOFORK, DEF_HANDLER, cleanup);
150
plars865695b2001-08-27 22:15:12 +0000151 TEST_PAUSE;
152
subrata_modak56207ce2009-03-23 13:35:39 +0000153 /* Switch to nobody user for correct error code collection */
subrata_modak56207ce2009-03-23 13:35:39 +0000154 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 }
robbiewc277a362001-09-12 18:44:14 +0000160
plars865695b2001-08-27 22:15:12 +0000161 /*
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_modak56207ce2009-03-23 13:35:39 +0000170 /* Get an new IPC resource key. */
subrata_modak6d2b29e2008-04-06 11:46:11 +0000171 msgkey2 = getipckey();
172
plars865695b2001-08-27 22:15:12 +0000173 /* 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_modak6d2b29e2008-04-06 11:46:11 +0000187 if ((msg_q_2 = msgget(msgkey2, IPC_CREAT | IPC_EXCL)) == -1) {
plars865695b2001-08-27 22:15:12 +0000188 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_modak56207ce2009-03-23 13:35:39 +0000196void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000197{
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
plars865695b2001-08-27 22:15:12 +0000204 tst_rmdir();
205
Chris Dearmanec6edca2012-10-17 19:54:01 -0700206}