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 * msgrcv06.c
23 *
24 * DESCRIPTION
25 * msgrcv06 - test for EIDRM error
26 *
27 * ALGORITHM
28 * loop if that option was specified
29 * create a message queue with read/write permissions
30 * fork a child who sleeps on an attempted read with msgrcv()
31 * parent removes the queue then waits for child to complete
32 * check the errno value
33 * issue a PASS message if we get EIDRM
34 * otherwise, the tests fails
35 * issue a FAIL message
36 * child removes message queue if required
37 * parent callc cleanup
38 *
39 * USAGE: <for command-line>
40 * msgrcv06 [-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
50 * 14/03/2008 Matthieu Fertré (Matthieu.Fertre@irisa.fr)
51 * - Fix concurrency issue. Due to the use of usleep function to
52 * synchronize processes, synchronization issues can occur on a loaded
53 * system. Fix this by using pipes to synchronize processes.
54 *
55 *
56 * RESTRICTIONS
57 * none
58 */
59
60 #include "test.h"
61
62 #include "ipcmsg.h"
63
64 #include <sys/types.h>
65 #include <sys/wait.h>
66
67 void do_child(void);
68 void cleanup(void);
69 void setup(void);
70 #ifdef UCLINUX
71 #define PIPE_NAME "msgrcv06"
72 void do_child_uclinux(void);
73 #endif
74
75 char *TCID = "msgrcv06";
76 int TST_TOTAL = 1;
77
78 int msg_q_1 = -1; /* The message queue id created in setup */
79
80 MSGBUF rcv_buf;
81 pid_t c_pid;
82
main(int ac,char ** av)83 int main(int ac, char **av)
84 {
85 int lc;
86
87 tst_parse_opts(ac, av, NULL, NULL);
88
89 #ifdef UCLINUX
90 maybe_run_child(&do_child_uclinux, "d", &msg_q_1);
91 #endif
92
93 setup(); /* global setup */
94
95 /* The following loop checks looping state if -i option given */
96
97 for (lc = 0; TEST_LOOPING(lc); lc++) {
98 /* reset tst_count in case we are looping */
99 tst_count = 0;
100
101 /*
102 * set up the queue here so that multiple test iterations
103 * will work.
104 */
105 msgkey = getipckey();
106
107 /* create a message queue with read/write permission */
108 if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW))
109 == -1) {
110 tst_brkm(TBROK, cleanup, "Can't create message queue");
111 }
112
113 /*
114 * fork a child that will attempt to read a non-existent
115 * message from the queue
116 */
117 if ((c_pid = FORK_OR_VFORK()) == -1) {
118 tst_brkm(TBROK, cleanup, "could not fork");
119 }
120
121 if (c_pid == 0) { /* child */
122 /*
123 * Attempt to read a message without IPC_NOWAIT.
124 * With no message to read, the child sleeps.
125 */
126
127 #ifdef UCLINUX
128 if (self_exec(av[0], "d", msg_q_1) < 0) {
129 tst_brkm(TBROK, cleanup, "could not self_exec");
130 }
131 #else
132 do_child();
133 #endif
134 } else {
135 TST_PROCESS_STATE_WAIT(cleanup, c_pid, 'S');
136
137 /* remove the queue */
138 rm_queue(msg_q_1);
139
140 waitpid(c_pid, NULL, 0);
141 }
142 }
143
144 tst_exit();
145 }
146
147 /*
148 * do_child()
149 */
do_child(void)150 void do_child(void)
151 {
152 TEST(msgrcv(msg_q_1, &rcv_buf, MSGSIZE, 1, 0));
153
154 if (TEST_RETURN != -1) {
155 tst_resm(TFAIL, "call succeeded when error expected");
156 exit(-1);
157 }
158
159 switch (TEST_ERRNO) {
160 case EIDRM:
161 tst_resm(TPASS, "expected failure - errno = %d : %s",
162 TEST_ERRNO, strerror(TEST_ERRNO));
163
164 /* mark the queue as invalid as it was removed */
165 msg_q_1 = -1;
166 break;
167 default:
168 tst_resm(TFAIL,
169 "call failed with an unexpected error - %d : %s",
170 TEST_ERRNO, strerror(TEST_ERRNO));
171 break;
172 }
173
174 /* if it exists, remove the message queue that was created */
175 rm_queue(msg_q_1);
176
177 exit(0);
178 }
179
180 #ifdef UCLINUX
181 /*
182 * do_child_uclinux() - capture signals again, then run do_child()
183 */
do_child_uclinux(void)184 void do_child_uclinux(void)
185 {
186 tst_sig(FORK, SIG_IGN, cleanup);
187
188 do_child();
189 }
190 #endif
191
192 /*
193 * setup() - performs all the ONE TIME setup for this test.
194 */
setup(void)195 void setup(void)
196 {
197
198 tst_sig(FORK, SIG_IGN, cleanup);
199
200 TEST_PAUSE;
201
202 /*
203 * Create a temporary directory and cd into it.
204 * This helps to ensure that a unique msgkey is created.
205 * See ../lib/libipc.c for more information.
206 */
207 tst_tmpdir();
208 }
209
210 /*
211 * cleanup() - performs all the ONE TIME cleanup for this test at completion
212 * or premature exit.
213 */
cleanup(void)214 void cleanup(void)
215 {
216
217 tst_rmdir();
218
219 }
220