• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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
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 
55  *
56  * RESTRICTIONS
57  *	none
58  */
59 
60 #include <pwd.h>
61 
62 #include "test.h"
63 
64 #include "ipcmsg.h"
65 
66 void cleanup(void);
67 void setup(void);
68 
69 char *TCID = "msgrcv02";
70 int TST_TOTAL = 2;
71 
72 char nobody_uid[] = "nobody";
73 struct passwd *ltpuser;
74 
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 */
85 	{
86 	&msg_q_2, &rcv_buf, EACCES},
87 	    /* EFAULT - the message buffer address is invalid */
88 	{
89 	&msg_q_1, (MSGBUF *) - 1, EFAULT}
90 };
91 
main(int ac,char ** av)92 int main(int ac, char **av)
93 {
94 	int lc;
95 	int i;
96 
97 	tst_parse_opts(ac, av, NULL, NULL);
98 
99 	setup();		/* global setup */
100 
101 	/* The following loop checks looping state if -i option given */
102 
103 	for (lc = 0; TEST_LOOPING(lc); lc++) {
104 		/* reset tst_count in case we are looping */
105 		tst_count = 0;
106 
107 		for (i = 0; i < TST_TOTAL; i++) {
108 
109 			/*
110 			 * Use the TEST macro to make the call
111 			 */
112 
113 			TEST(msgrcv(*(TC[i].queue_id), TC[i].mbuf, MSGSIZE,
114 				    1, IPC_NOWAIT));
115 
116 			if (TEST_RETURN != -1) {
117 				tst_resm(TFAIL, "call succeeded unexpectedly");
118 				continue;
119 			}
120 
121 			if (TEST_ERRNO == TC[i].error) {
122 				tst_resm(TPASS, "expected failure - errno = "
123 					 "%d : %s", TEST_ERRNO,
124 					 strerror(TEST_ERRNO));
125 			} else {
126 				tst_resm(TFAIL, "call failed with an "
127 					 "unexpected error - %d : %s",
128 					 TEST_ERRNO, strerror(TEST_ERRNO));
129 			}
130 		}
131 	}
132 
133 	cleanup();
134 
135 	tst_exit();
136 }
137 
138 /*
139  * setup() - performs all the ONE TIME setup for this test.
140  */
setup(void)141 void setup(void)
142 {
143 	key_t msgkey2;
144 
145 	tst_require_root();
146 
147 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
148 
149 	TEST_PAUSE;
150 
151 	/* Switch to nobody user for correct error code collection */
152 	ltpuser = getpwnam(nobody_uid);
153 	if (setuid(ltpuser->pw_uid) == -1) {
154 		tst_resm(TINFO, "setuid failed to "
155 			 "to set the effective uid to %d", ltpuser->pw_uid);
156 		perror("setuid");
157 	}
158 
159 	/*
160 	 * Create a temporary directory and cd into it.
161 	 * This helps to ensure that a unique msgkey is created.
162 	 * See ../lib/libipc.c for more information.
163 	 */
164 	tst_tmpdir();
165 
166 	msgkey = getipckey();
167 
168 	/* Get an new IPC resource key. */
169 	msgkey2 = getipckey();
170 
171 	/* create a message queue with read/write permission */
172 	if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW)) == -1) {
173 		tst_brkm(TBROK, cleanup, "Can't create message queue #1");
174 	}
175 
176 	/* initialize a message buffer */
177 	init_buf(&snd_buf, MSGTYPE, MSGSIZE);
178 
179 	/* put it on msq_q_1 */
180 	if (msgsnd(msg_q_1, &snd_buf, MSGSIZE, IPC_NOWAIT) == -1) {
181 		tst_brkm(TBROK, cleanup, "Couldn't put message on queue");
182 	}
183 
184 	/* create a message queue without read/write permission */
185 	if ((msg_q_2 = msgget(msgkey2, IPC_CREAT | IPC_EXCL)) == -1) {
186 		tst_brkm(TBROK, cleanup, "Can't create message queue #2");
187 	}
188 }
189 
190 /*
191  * cleanup() - performs all the ONE TIME cleanup for this test at completion
192  * 	       or premature exit.
193  */
cleanup(void)194 void cleanup(void)
195 {
196 	/* if it exists, remove the message queue #1 */
197 	rm_queue(msg_q_1);
198 
199 	/* if it exists, remove the message queue #2 */
200 	rm_queue(msg_q_2);
201 
202 	tst_rmdir();
203 
204 }
205