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 * msgget04.c
23 *
24 * DESCRIPTION
25 * msgget04 - test for an EACCES error by creating a message queue
26 * with no read or write permission and then attempting
27 * to access it with various permissions.
28 *
29 * ALGORITHM
30 * Create a message queue with no read or write permission
31 * loop if that option was specified
32 * Try to access the message queue with various permissions
33 * check the errno value
34 * issue a PASS message if we get EACCES
35 * otherwise, the tests fails
36 * issue a FAIL message
37 * break any remaining tests
38 * call cleanup
39 *
40 * USAGE: <for command-line>
41 * msgget04 [-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 *
52 * RESTRICTIONS
53 * none
54 */
55 #include <pwd.h>
56 #include "test.h"
57
58 #include "ipcmsg.h"
59
60 char *TCID = "msgget04";
61 int TST_TOTAL = 3;
62
63 char nobody_uid[] = "nobody";
64 struct passwd *ltpuser;
65
66 int msg_q_1 = -1; /* to hold the message queue id */
67
68 int test_flags[] = { MSG_RD, MSG_WR, MSG_RD | MSG_WR };
69
main(int ac,char ** av)70 int main(int ac, char **av)
71 {
72 int lc;
73 int i;
74
75 tst_parse_opts(ac, av, NULL, NULL);
76
77 setup(); /* global setup */
78
79 /* The following loop checks looping state if -i option given */
80
81 for (lc = 0; TEST_LOOPING(lc); lc++) {
82 /* reset tst_count in case we are looping */
83 tst_count = 0;
84
85 /* loop through the test cases */
86
87 for (i = 0; i < TST_TOTAL; i++) {
88 /*
89 * Try to access the message queue with specified
90 * permissions.
91 */
92
93 TEST(msgget(msgkey, test_flags[i]));
94
95 if (TEST_RETURN != -1) {
96 tst_resm(TFAIL, "call succeeded "
97 "when EACCES error expected");
98 continue;
99 }
100
101 switch (TEST_ERRNO) {
102 case EACCES:
103 tst_resm(TPASS, "expected failure - errno = "
104 "%d : %s", TEST_ERRNO,
105 strerror(TEST_ERRNO));
106 break;
107 default:
108 tst_resm(TFAIL, "call failed with an "
109 "unexpected error - %d : %s",
110 TEST_ERRNO, strerror(TEST_ERRNO));
111 break;
112 }
113 }
114 }
115
116 cleanup();
117
118 tst_exit();
119 }
120
121 /*
122 * setup() - performs all the ONE TIME setup for this test.
123 */
setup(void)124 void setup(void)
125 {
126 tst_require_root();
127
128 tst_sig(NOFORK, DEF_HANDLER, cleanup);
129
130 TEST_PAUSE;
131
132 /* Switch to nobody user for correct error code collection */
133 ltpuser = getpwnam(nobody_uid);
134 if (setuid(ltpuser->pw_uid) == -1) {
135 tst_resm(TINFO, "setuid failed to "
136 "to set the effective uid to %d", ltpuser->pw_uid);
137 perror("setuid");
138 }
139
140 /*
141 * Create a temporary directory and cd into it.
142 * This helps to ensure that a unique msgkey is created.
143 * See ../lib/libipc.c for more information.
144 */
145 tst_tmpdir();
146
147 msgkey = getipckey();
148
149 /*
150 * Create the message queue without specifying permissions.
151 */
152 if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL)) == -1) {
153 tst_brkm(TBROK, cleanup, "Could not create message queue"
154 " - errno = %d : %s", errno, strerror(errno));
155 }
156 }
157
158 /*
159 * cleanup() - performs all the ONE TIME cleanup for this test at completion
160 * or premature exit.
161 */
cleanup(void)162 void cleanup(void)
163 {
164 /* if it exists, remove the message queue */
165 rm_queue(msg_q_1);
166
167 tst_rmdir();
168
169 }
170