blob: cb2c306b4a45f0891945121e09afdd8a5277526f [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 * 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()
robbiewd34d5812005-07-11 22:28:09 +000031 * parent removes the queue then waits for child to complete
plars865695b2001-08-27 22:15:12 +000032 * check the errno value
33 * issue a PASS message if we get EIDRM
34 * otherwise, the tests fails
35 * issue a FAIL message
robbiewd34d5812005-07-11 22:28:09 +000036 * child removes message queue if required
37 * parent callc cleanup
plars865695b2001-08-27 22:15:12 +000038 *
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
subrata_modak2191ca82008-04-06 11:50:24 +000050 * 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 *
plars865695b2001-08-27 22:15:12 +000055 *
56 * RESTRICTIONS
57 * none
58 */
59
60#include "test.h"
plars865695b2001-08-27 22:15:12 +000061
robbiew23499f02002-11-18 19:54:58 +000062#include "ipcmsg.h"
subrata_modak2191ca82008-04-06 11:50:24 +000063#include "libtestsuite.h"
plars865695b2001-08-27 22:15:12 +000064
robbiewd34d5812005-07-11 22:28:09 +000065#include <sys/types.h>
66#include <sys/wait.h>
67
68void do_child(void);
plars865695b2001-08-27 22:15:12 +000069void cleanup(void);
70void setup(void);
robbiewd34d5812005-07-11 22:28:09 +000071#ifdef UCLINUX
subrata_modak4b13c892009-01-22 09:51:33 +000072#define PIPE_NAME "msgrcv06"
robbiewd34d5812005-07-11 22:28:09 +000073void do_child_uclinux(void);
74#endif
plars865695b2001-08-27 22:15:12 +000075
76char *TCID = "msgrcv06";
77int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000078
plars865695b2001-08-27 22:15:12 +000079int msg_q_1 = -1; /* The message queue id created in setup */
80
subrata_modak4b13c892009-01-22 09:51:33 +000081int sync_pipes[2];
82
plars865695b2001-08-27 22:15:12 +000083MSGBUF rcv_buf;
84pid_t c_pid;
85
robbiew23499f02002-11-18 19:54:58 +000086int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000087{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020088 int lc;
Cyril Hrubis74225622014-06-02 17:54:38 +020089 const char *msg;
plars865695b2001-08-27 22:15:12 +000090
Cyril Hrubis74225622014-06-02 17:54:38 +020091 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080092 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Cyril Hrubis74225622014-06-02 17:54:38 +020093
robbiewd34d5812005-07-11 22:28:09 +000094#ifdef UCLINUX
95 maybe_run_child(&do_child_uclinux, "d", &msg_q_1);
96#endif
97
subrata_modak56207ce2009-03-23 13:35:39 +000098 setup(); /* global setup */
plars865695b2001-08-27 22:15:12 +000099
subrata_modak4b13c892009-01-22 09:51:33 +0000100 if (sync_pipe_create(sync_pipes, PIPE_NAME) == -1)
subrata_modak8791f672008-05-20 10:11:00 +0000101 tst_brkm(TBROK, cleanup, "sync_pipe_create failed");
subrata_modak2191ca82008-04-06 11:50:24 +0000102
plars865695b2001-08-27 22:15:12 +0000103 /* The following loop checks looping state if -i option given */
104
105 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800106 /* reset tst_count in case we are looping */
107 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000108
109 /*
110 * set up the queue here so that multiple test iterations
111 * will work.
112 */
113 msgkey = getipckey();
114
115 /* create a message queue with read/write permission */
116 if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW))
subrata_modak56207ce2009-03-23 13:35:39 +0000117 == -1) {
plars865695b2001-08-27 22:15:12 +0000118 tst_brkm(TBROK, cleanup, "Can't create message queue");
119 }
120
121 /*
122 * fork a child that will attempt to read a non-existent
123 * message from the queue
124 */
robbiewd34d5812005-07-11 22:28:09 +0000125 if ((c_pid = FORK_OR_VFORK()) == -1) {
plars865695b2001-08-27 22:15:12 +0000126 tst_brkm(TBROK, cleanup, "could not fork");
127 }
128
subrata_modak56207ce2009-03-23 13:35:39 +0000129 if (c_pid == 0) { /* child */
plars865695b2001-08-27 22:15:12 +0000130 /*
131 * Attempt to read a message without IPC_NOWAIT.
132 * With no message to read, the child sleeps.
133 */
subrata_modak2191ca82008-04-06 11:50:24 +0000134
robbiewd34d5812005-07-11 22:28:09 +0000135#ifdef UCLINUX
136 if (self_exec(av[0], "d", msg_q_1) < 0) {
137 tst_brkm(TBROK, cleanup, "could not self_exec");
138 }
139#else
140 do_child();
141#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000142 } else { /* parent */
subrata_modak2191ca82008-04-06 11:50:24 +0000143
subrata_modak8791f672008-05-20 10:11:00 +0000144 if (sync_pipe_wait(sync_pipes) == -1)
subrata_modak56207ce2009-03-23 13:35:39 +0000145 tst_brkm(TBROK, cleanup,
146 "sync_pipe_wait failed");
subrata_modak8791f672008-05-20 10:11:00 +0000147
subrata_modak4b13c892009-01-22 09:51:33 +0000148 if (sync_pipe_close(sync_pipes, PIPE_NAME) == -1)
subrata_modak56207ce2009-03-23 13:35:39 +0000149 tst_brkm(TBROK, cleanup,
150 "sync_pipe_close failed");
subrata_modak8791f672008-05-20 10:11:00 +0000151
subrata_modak2191ca82008-04-06 11:50:24 +0000152 sleep(1);
plars865695b2001-08-27 22:15:12 +0000153
154 /* remove the queue */
155 rm_queue(msg_q_1);
156
robbiewd34d5812005-07-11 22:28:09 +0000157 waitpid(c_pid, NULL, 0);
plars865695b2001-08-27 22:15:12 +0000158 }
plars865695b2001-08-27 22:15:12 +0000159 }
160
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800161 tst_exit();
plars865695b2001-08-27 22:15:12 +0000162}
163
164/*
robbiewd34d5812005-07-11 22:28:09 +0000165 * do_child()
166 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400167void do_child(void)
robbiewd34d5812005-07-11 22:28:09 +0000168{
subrata_modak4b13c892009-01-22 09:51:33 +0000169 if (sync_pipe_notify(sync_pipes) == -1)
170 tst_brkm(TBROK, cleanup, "sync_pipe_notify failed");
171
172 if (sync_pipe_close(sync_pipes, PIPE_NAME) == -1)
173 tst_brkm(TBROK, cleanup, "sync_pipe_close failed");
174
robbiewd34d5812005-07-11 22:28:09 +0000175 TEST(msgrcv(msg_q_1, &rcv_buf, MSGSIZE, 1, 0));
176
177 if (TEST_RETURN != -1) {
178 tst_resm(TFAIL, "call succeeded when error expected");
179 exit(-1);
180 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000181
subrata_modak56207ce2009-03-23 13:35:39 +0000182 switch (TEST_ERRNO) {
robbiewd34d5812005-07-11 22:28:09 +0000183 case EIDRM:
subrata_modak56207ce2009-03-23 13:35:39 +0000184 tst_resm(TPASS, "expected failure - errno = %d : %s",
185 TEST_ERRNO, strerror(TEST_ERRNO));
186
robbiewd34d5812005-07-11 22:28:09 +0000187 /* mark the queue as invalid as it was removed */
188 msg_q_1 = -1;
189 break;
190 default:
subrata_modak56207ce2009-03-23 13:35:39 +0000191 tst_resm(TFAIL,
192 "call failed with an unexpected error - %d : %s",
robbiewd34d5812005-07-11 22:28:09 +0000193 TEST_ERRNO, strerror(TEST_ERRNO));
194 break;
subrata_modak56207ce2009-03-23 13:35:39 +0000195 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000196
robbiewd34d5812005-07-11 22:28:09 +0000197 /* if it exists, remove the message queue that was created */
198 rm_queue(msg_q_1);
199
200 exit(0);
201}
202
203#ifdef UCLINUX
204/*
205 * do_child_uclinux() - capture signals again, then run do_child()
206 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400207void do_child_uclinux(void)
robbiewd34d5812005-07-11 22:28:09 +0000208{
subrata_modak4b13c892009-01-22 09:51:33 +0000209 if (sync_pipe_create(sync_pipes, PIPE_NAME) == -1)
210 tst_brkm(TBROK, cleanup, "sync_pipe_create failed");
211
Garrett Cooper52626672010-12-20 15:03:21 -0800212 tst_sig(FORK, SIG_IGN, cleanup);
robbiewd34d5812005-07-11 22:28:09 +0000213
214 do_child();
215}
216#endif
217
218/*
plars865695b2001-08-27 22:15:12 +0000219 * setup() - performs all the ONE TIME setup for this test.
220 */
subrata_modak56207ce2009-03-23 13:35:39 +0000221void setup(void)
plars865695b2001-08-27 22:15:12 +0000222{
Garrett Cooper2c282152010-12-16 00:55:50 -0800223
Garrett Cooper52626672010-12-20 15:03:21 -0800224 tst_sig(FORK, SIG_IGN, cleanup);
plars865695b2001-08-27 22:15:12 +0000225
plars865695b2001-08-27 22:15:12 +0000226 TEST_PAUSE;
227
228 /*
229 * Create a temporary directory and cd into it.
230 * This helps to ensure that a unique msgkey is created.
231 * See ../lib/libipc.c for more information.
232 */
233 tst_tmpdir();
234}
235
236/*
237 * cleanup() - performs all the ONE TIME cleanup for this test at completion
238 * or premature exit.
239 */
subrata_modak56207ce2009-03-23 13:35:39 +0000240void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000241{
Garrett Cooper2c282152010-12-16 00:55:50 -0800242
plars865695b2001-08-27 22:15:12 +0000243 tst_rmdir();
244
Garrett Cooper52626672010-12-20 15:03:21 -0800245}