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 * 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"
56
57 #include "ipcmsg.h"
58
59 void cleanup(void);
60 void setup(void);
61
62 char *TCID = "msgsnd04";
63 int TST_TOTAL = 1;
64
65 int msg_q_1 = -1; /* The message queue id created in setup */
66 MSGBUF msg_buf;
67
main(int ac,char ** av)68 int main(int ac, char **av)
69 {
70 int lc;
71
72 tst_parse_opts(ac, av, NULL, NULL);
73
74 setup(); /* global setup */
75
76 /* The following loop checks looping state if -i option given */
77
78 for (lc = 0; TEST_LOOPING(lc); lc++) {
79 /* reset tst_count in case we are looping */
80 tst_count = 0;
81
82 /*
83 * Attempt to write another message to the full queue.
84 */
85
86 TEST(msgsnd(msg_q_1, &msg_buf, MSGSIZE, IPC_NOWAIT));
87
88 if (TEST_RETURN != -1) {
89 tst_resm(TFAIL, "call succeeded when error expected");
90 continue;
91 }
92
93 switch (TEST_ERRNO) {
94 case EAGAIN:
95 tst_resm(TPASS, "expected failure - errno = %d : %s",
96 TEST_ERRNO, strerror(TEST_ERRNO));
97 break;
98 default:
99 tst_resm(TFAIL, "call failed with an "
100 "unexpected error - %d : %s",
101 TEST_ERRNO, strerror(TEST_ERRNO));
102 break;
103 }
104 }
105
106 cleanup();
107
108 tst_exit();
109 }
110
111 /*
112 * setup() - performs all the ONE TIME setup for this test.
113 */
setup(void)114 void setup(void)
115 {
116
117 tst_sig(NOFORK, DEF_HANDLER, cleanup);
118
119 TEST_PAUSE;
120
121 /*
122 * Create a temporary directory and cd into it.
123 * This helps to ensure that a unique msgkey is created.
124 * See ../lib/libipc.c for more information.
125 */
126 tst_tmpdir();
127
128 msgkey = getipckey();
129
130 /* create a message queue with read/write permission */
131 if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW)) == -1) {
132 tst_brkm(TBROK, cleanup, "Can't create message queue");
133 }
134
135 /* initialize the message buffer */
136 init_buf(&msg_buf, MSGTYPE, MSGSIZE);
137
138 /* write messages to the queue until it is full */
139 while (msgsnd(msg_q_1, &msg_buf, MSGSIZE, IPC_NOWAIT) != -1) {
140 msg_buf.mtype += 1;
141 }
142 }
143
144 /*
145 * cleanup() - performs all the ONE TIME cleanup for this test at completion
146 * or premature exit.
147 */
cleanup(void)148 void cleanup(void)
149 {
150 /* if it exists, remove the message queue that was created */
151 rm_queue(msg_q_1);
152
153 tst_rmdir();
154
155 }
156