blob: 95d46a6fd4d06b2322268c8af8fdc8efcab7c0f0 [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 * msgsnd04.c
23 *
24 * DESCRIPTION
25 * msgsnd04 - test for EAGAIN error
26 *
27 * ALGORITHM
28 * create a message queue with read/write permissions
29 * initialize a message buffer with a known message and type
30 * enqueue the message in a loop until the queue is full
31 * loop if that option was specified
32 * attempt to enqueue another message - msgsnd()
33 * check the errno value
34 * issue a PASS message if we get EAGAIN
35 * otherwise, the tests fails
36 * issue a FAIL message
37 * call cleanup
38 *
39 * USAGE: <for command-line>
40 * msgsnd04 [-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 *
51 * RESTRICTIONS
52 * none
53 */
54
55#include "test.h"
plars865695b2001-08-27 22:15:12 +000056
robbiew23499f02002-11-18 19:54:58 +000057#include "ipcmsg.h"
plars865695b2001-08-27 22:15:12 +000058
59void cleanup(void);
60void setup(void);
61
62char *TCID = "msgsnd04";
63int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000064
plars865695b2001-08-27 22:15:12 +000065int msg_q_1 = -1; /* The message queue id created in setup */
66MSGBUF msg_buf;
67
robbiew23499f02002-11-18 19:54:58 +000068int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000069{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020070 int lc;
Cyril Hrubis74225622014-06-02 17:54:38 +020071 const char *msg;
plars865695b2001-08-27 22:15:12 +000072
Cyril Hrubis74225622014-06-02 17:54:38 +020073 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080074 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000075
subrata_modak56207ce2009-03-23 13:35:39 +000076 setup(); /* global setup */
plars865695b2001-08-27 22:15:12 +000077
78 /* The following loop checks looping state if -i option given */
79
80 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080081 /* reset tst_count in case we are looping */
82 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000083
84 /*
85 * Attempt to write another message to the full queue.
86 */
subrata_modakbdbaec52009-02-26 12:14:51 +000087
plars865695b2001-08-27 22:15:12 +000088 TEST(msgsnd(msg_q_1, &msg_buf, MSGSIZE, IPC_NOWAIT));
subrata_modakbdbaec52009-02-26 12:14:51 +000089
plars865695b2001-08-27 22:15:12 +000090 if (TEST_RETURN != -1) {
91 tst_resm(TFAIL, "call succeeded when error expected");
92 continue;
93 }
subrata_modakbdbaec52009-02-26 12:14:51 +000094
subrata_modak56207ce2009-03-23 13:35:39 +000095 switch (TEST_ERRNO) {
plars865695b2001-08-27 22:15:12 +000096 case EAGAIN:
97 tst_resm(TPASS, "expected failure - errno = %d : %s",
98 TEST_ERRNO, strerror(TEST_ERRNO));
99 break;
100 default:
101 tst_resm(TFAIL, "call failed with an "
102 "unexpected error - %d : %s",
103 TEST_ERRNO, strerror(TEST_ERRNO));
104 break;
105 }
106 }
107
108 cleanup();
109
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800110 tst_exit();
plars865695b2001-08-27 22:15:12 +0000111}
112
113/*
114 * setup() - performs all the ONE TIME setup for this test.
115 */
subrata_modak56207ce2009-03-23 13:35:39 +0000116void setup(void)
plars865695b2001-08-27 22:15:12 +0000117{
Garrett Cooper2c282152010-12-16 00:55:50 -0800118
plars865695b2001-08-27 22:15:12 +0000119 tst_sig(NOFORK, DEF_HANDLER, cleanup);
120
plars865695b2001-08-27 22:15:12 +0000121 TEST_PAUSE;
122
123 /*
124 * Create a temporary directory and cd into it.
125 * This helps to ensure that a unique msgkey is created.
126 * See ../lib/libipc.c for more information.
127 */
128 tst_tmpdir();
129
130 msgkey = getipckey();
131
132 /* create a message queue with read/write permission */
133 if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW)) == -1) {
134 tst_brkm(TBROK, cleanup, "Can't create message queue");
135 }
136
137 /* initialize the message buffer */
138 init_buf(&msg_buf, MSGTYPE, MSGSIZE);
139
140 /* write messages to the queue until it is full */
141 while (msgsnd(msg_q_1, &msg_buf, MSGSIZE, IPC_NOWAIT) != -1) {
142 msg_buf.mtype += 1;
143 }
144}
145
146/*
147 * cleanup() - performs all the ONE TIME cleanup for this test at completion
148 * or premature exit.
149 */
subrata_modak56207ce2009-03-23 13:35:39 +0000150void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000151{
152 /* if it exists, remove the message queue that was created */
153 rm_queue(msg_q_1);
154
plars865695b2001-08-27 22:15:12 +0000155 tst_rmdir();
156
Chris Dearmanec6edca2012-10-17 19:54:01 -0700157}