blob: 8e6def75db4335c6b3f4eb9f50c2c76197870afb [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 * 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_modake8e78f62008-04-06 11:41:49 +000050 * 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.
plars865695b2001-08-27 22:15:12 +000053 *
54 * RESTRICTIONS
55 * none
56 */
robbiewc277a362001-09-12 18:44:14 +000057#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000058
59#include "test.h"
plars865695b2001-08-27 22:15:12 +000060
robbiew23499f02002-11-18 19:54:58 +000061#include "ipcmsg.h"
plars865695b2001-08-27 22:15:12 +000062
63char *TCID = "msgctl04";
64int TST_TOTAL = 6;
plars865695b2001-08-27 22:15:12 +000065
robbiewc277a362001-09-12 18:44:14 +000066char nobody_uid[] = "nobody";
67struct passwd *ltpuser;
68
plars865695b2001-08-27 22:15:12 +000069int msg_q_1 = -1; /* The message queue id created in setup */
70int msg_q_2 = -1; /* Another queue id created in setup */
71int bad_q = -1; /* a value to use as a bad queue id */
72
73struct msqid_ds q_buf;
74
subrata_modak56207ce2009-03-23 13:35:39 +000075struct 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;
plars865695b2001-08-27 22:15:12 +000079 int error;
80} TC[] = {
81 /* EACCES - there is no read permission for the queue */
subrata_modak56207ce2009-03-23 13:35:39 +000082 {
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}
plars865695b2001-08-27 22:15:12 +000099};
100
robbiew23499f02002-11-18 19:54:58 +0000101int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000102{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200103 int lc;
Cyril Hrubis74225622014-06-02 17:54:38 +0200104 const char *msg;
robbiew23499f02002-11-18 19:54:58 +0000105 int i;
plars865695b2001-08-27 22:15:12 +0000106
Cyril Hrubis74225622014-06-02 17:54:38 +0200107 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800108 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000109
subrata_modak56207ce2009-03-23 13:35:39 +0000110 setup(); /* global setup */
plars865695b2001-08-27 22:15:12 +0000111
112 /* The following loop checks looping state if -i option given */
113
114 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800115 /* reset tst_count in case we are looping */
116 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000117
118 /* loop through the test cases */
119
subrata_modak56207ce2009-03-23 13:35:39 +0000120 for (i = 0; i < TST_TOTAL; i++) {
plars865695b2001-08-27 22:15:12 +0000121
122 TEST(msgctl(*(TC[i].queue_id), TC[i].ipc_cmd,
123 TC[i].buf));
124
125 if (TEST_RETURN != -1) {
robbiew9ae92ea2004-08-11 15:55:29 +0000126 tst_resm(TFAIL, "msgctl() call succeeded "
plars865695b2001-08-27 22:15:12 +0000127 "on expected fail");
128 continue;
129 }
130
plars865695b2001-08-27 22:15:12 +0000131 if (TEST_ERRNO == TC[i].error) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800132 tst_resm(TPASS | TTERRNO, "expected failure");
plars865695b2001-08-27 22:15:12 +0000133 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800134 tst_resm(TFAIL | TTERRNO, "unexpected error");
plars865695b2001-08-27 22:15:12 +0000135 tst_resm(TINFO, "expected error is - %d : %s",
136 TC[i].error, strerror(TC[i].error));
137 }
138 }
139 }
140
141 cleanup();
142
Garrett Cooper53740502010-12-16 00:04:01 -0800143 tst_exit();
plars865695b2001-08-27 22:15:12 +0000144}
145
146/*
147 * setup() - performs all the ONE TIME setup for this test.
148 */
subrata_modak56207ce2009-03-23 13:35:39 +0000149void setup(void)
plars865695b2001-08-27 22:15:12 +0000150{
subrata_modake8e78f62008-04-06 11:41:49 +0000151 key_t msgkey2;
152
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200153 tst_require_root(NULL);
154
plars865695b2001-08-27 22:15:12 +0000155 tst_sig(NOFORK, DEF_HANDLER, cleanup);
156
plars865695b2001-08-27 22:15:12 +0000157 TEST_PAUSE;
158
subrata_modak56207ce2009-03-23 13:35:39 +0000159 /* Switch to nobody user for correct error code collection */
subrata_modak56207ce2009-03-23 13:35:39 +0000160 ltpuser = getpwnam(nobody_uid);
vapierce6a0e32009-08-28 13:14:39 +0000161 if (setuid(ltpuser->pw_uid) == -1)
162 tst_resm(TINFO, "setuid(%d) failed", ltpuser->pw_uid);
robbiewc277a362001-09-12 18:44:14 +0000163
plars865695b2001-08-27 22:15:12 +0000164 /*
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_modake8e78f62008-04-06 11:41:49 +0000173 /* Get an new IPC resource key. */
174 msgkey2 = getipckey();
175
plars865695b2001-08-27 22:15:12 +0000176 /* 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 Gao354ebb42012-12-07 10:10:04 +0800178 tst_brkm(TBROK | TERRNO, cleanup,
179 "Can't create message queue #1");
plars865695b2001-08-27 22:15:12 +0000180 }
181
182 /* now let's create another message queue with read & write access */
subrata_modake8e78f62008-04-06 11:41:49 +0000183 if ((msg_q_2 =
subrata_modak56207ce2009-03-23 13:35:39 +0000184 msgget(msgkey2, IPC_CREAT | IPC_EXCL | MSG_RD | MSG_WR)) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800185 tst_brkm(TBROK | TERRNO, cleanup,
186 "Can't create message queue #2");
plars865695b2001-08-27 22:15:12 +0000187 }
188}
189
190/*
191 * cleanup() - performs all the ONE TIME cleanup for this test at completion
192 * or premature exit.
193 */
subrata_modak56207ce2009-03-23 13:35:39 +0000194void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000195{
196 /*
197 * remove the message queues that were created.
198 */
199 rm_queue(msg_q_1);
200
201 rm_queue(msg_q_2);
202
plars865695b2001-08-27 22:15:12 +0000203 tst_rmdir();
204
Chris Dearmanec6edca2012-10-17 19:54:01 -0700205}