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 * 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 */
53 #include <pwd.h>
54 #include "test.h"
55
56 #include "ipcmsg.h"
57
58 void cleanup(void);
59 void setup(void);
60
61 char *TCID = "msgsnd02";
62 int TST_TOTAL = 2;
63
64 char nobody_uid[] = "nobody";
65 struct passwd *ltpuser;
66
67 int msg_q_1 = -1; /* The message queue id created in setup */
68 MSGBUF msg_buf; /* a buffer for the message to queue */
69 int bad_q = -1; /* a value to use as a bad queue ID */
70
71 struct 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 */
77 {
78 &msg_q_1, &msg_buf, EACCES},
79 /* EFAULT - the message buffer address is invalid */
80 {
81 &msg_q_1, NULL, EFAULT},};
82
main(int ac,char ** av)83 int main(int ac, char **av)
84 {
85 int lc;
86 int i;
87
88 tst_parse_opts(ac, av, NULL, NULL);
89
90 setup(); /* global setup */
91
92 /* The following loop checks looping state if -i option given */
93
94 for (lc = 0; TEST_LOOPING(lc); lc++) {
95 /* reset tst_count in case we are looping */
96 tst_count = 0;
97
98 /*
99 * loop through the test cases
100 */
101
102 for (i = 0; i < TST_TOTAL; i++) {
103 TEST(msgsnd(*(TC[i].queue_id), TC[i].buffer, 1, 0));
104
105 if (TEST_RETURN != -1) {
106 tst_resm(TFAIL, "call succeeded unexpectedly");
107 continue;
108 }
109
110 if (TEST_ERRNO == TC[i].error) {
111 tst_resm(TPASS, "expected failure - "
112 "errno = %d : %s", TEST_ERRNO,
113 strerror(TEST_ERRNO));
114 } else {
115 tst_resm(TFAIL, "unexpected error - %d : %s",
116 TEST_ERRNO, strerror(TEST_ERRNO));
117 }
118 }
119 }
120
121 cleanup();
122
123 tst_exit();
124 }
125
126 /*
127 * setup() - performs all the ONE TIME setup for this test.
128 */
setup(void)129 void setup(void)
130 {
131 tst_require_root();
132
133 tst_sig(NOFORK, DEF_HANDLER, cleanup);
134
135 TEST_PAUSE;
136
137 /* Switch to nobody user for correct error code collection */
138 ltpuser = getpwnam(nobody_uid);
139 if (setuid(ltpuser->pw_uid) == -1) {
140 tst_resm(TINFO, "setuid failed to "
141 "to set the effective uid to %d", ltpuser->pw_uid);
142 perror("setuid");
143 }
144
145 /*
146 * Create a temporary directory and cd into it.
147 * This helps to ensure that a unique msgkey is created.
148 * See ../lib/libipc.c for more information.
149 */
150 tst_tmpdir();
151
152 msgkey = getipckey();
153
154 /* create a message queue without write permission */
155 if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RD)) == -1) {
156 tst_brkm(TBROK, cleanup, "Can't create message queue");
157 }
158
159 /* initialize the message buffer with something trivial */
160 msg_buf.mtype = MSGTYPE;
161 msg_buf.mtext[0] = 'a';
162 }
163
164 /*
165 * cleanup() - performs all the ONE TIME cleanup for this test at completion
166 * or premature exit.
167 */
cleanup(void)168 void cleanup(void)
169 {
170 /* if it exists, remove the message queue that was created */
171 rm_queue(msg_q_1);
172
173 tst_rmdir();
174
175 }
176