1 /* $NetBSD: t_msgrcv.c,v 1.3 2013/07/24 11:44:10 skrll Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_msgrcv.c,v 1.3 2013/07/24 11:44:10 skrll Exp $");
36 #include <sys/sysctl.h>
51 #define MSG_MTYPE_1 0x41
52 #define MSG_MTYPE_2 0x42
53 #define MSG_MTYPE_3 0x43
61 static void clean(void);
68 if ((id
= msgget(MSG_KEY
, 0)) != -1)
69 (void)msgctl(id
, IPC_RMID
, 0);
72 ATF_TC_WITH_CLEANUP(msgrcv_basic
);
73 ATF_TC_HEAD(msgrcv_basic
, tc
)
75 atf_tc_set_md_var(tc
, "descr", "A basic test of msgrcv(2)");
78 ATF_TC_BODY(msgrcv_basic
, tc
)
80 struct msg msg1
= { MSG_MTYPE_1
, { 'a', 'b', 'c' } };
81 struct msg msg2
= { MSG_MTYPE_1
, { 'x', 'y', 'z' } };
84 id
= msgget(MSG_KEY
, IPC_CREAT
| 0600);
85 ATF_REQUIRE(id
!= -1);
87 (void)msgsnd(id
, &msg1
, MSG_LEN
, IPC_NOWAIT
);
88 (void)msgrcv(id
, &msg2
, MSG_LEN
, MSG_MTYPE_1
, IPC_NOWAIT
);
90 ATF_CHECK(msg1
.buf
[0] == msg2
.buf
[0]);
91 ATF_CHECK(msg1
.buf
[1] == msg2
.buf
[1]);
92 ATF_CHECK(msg1
.buf
[2] == msg2
.buf
[2]);
94 ATF_REQUIRE(msgctl(id
, IPC_RMID
, 0) == 0);
97 ATF_TC_CLEANUP(msgrcv_basic
, tc
)
102 ATF_TC_WITH_CLEANUP(msgrcv_block
);
103 ATF_TC_HEAD(msgrcv_block
, tc
)
105 atf_tc_set_md_var(tc
, "descr", "Test that msgrcv(2) blocks");
108 ATF_TC_BODY(msgrcv_block
, tc
)
110 struct msg msg
= { MSG_MTYPE_1
, { 'a', 'b', 'c' } };
114 id
= msgget(MSG_KEY
, IPC_CREAT
| 0600);
115 ATF_REQUIRE(id
!= -1);
118 ATF_REQUIRE(pid
>= 0);
122 if (msgrcv(id
, &msg
, MSG_LEN
, MSG_MTYPE_1
, 0) < 0)
129 * Below msgsnd(2) should unblock the child,
130 * and hence kill(2) should fail with ESRCH.
133 (void)msgsnd(id
, &msg
, MSG_LEN
, IPC_NOWAIT
);
135 (void)kill(pid
, SIGKILL
);
138 if (WIFEXITED(sta
) == 0 || WIFSIGNALED(sta
) != 0)
139 atf_tc_fail("msgrcv(2) did not block");
141 ATF_REQUIRE(msgctl(id
, IPC_RMID
, 0) == 0);
144 ATF_TC_CLEANUP(msgrcv_block
, tc
)
149 ATF_TC_WITH_CLEANUP(msgrcv_err
);
150 ATF_TC_HEAD(msgrcv_err
, tc
)
152 atf_tc_set_md_var(tc
, "descr", "Test errors from msgrcv(2)");
155 ATF_TC_BODY(msgrcv_err
, tc
)
157 struct msg msg
= { MSG_MTYPE_1
, { 'a', 'b', 'c' } };
160 id
= msgget(MSG_KEY
, IPC_CREAT
| 0600);
161 ATF_REQUIRE(id
!= -1);
165 ATF_REQUIRE_ERRNO(ENOMSG
, msgrcv(id
, &msg
,
166 MSG_LEN
, MSG_MTYPE_1
, IPC_NOWAIT
) == -1);
168 ATF_REQUIRE(msgsnd(id
, &msg
, MSG_LEN
, IPC_NOWAIT
) == 0);
172 ATF_REQUIRE_ERRNO(EFAULT
, msgrcv(id
, (void *)-1,
173 MSG_LEN
, MSG_MTYPE_1
, IPC_NOWAIT
) == -1);
177 ATF_REQUIRE_ERRNO(EINVAL
, msgrcv(-1, &msg
,
178 MSG_LEN
, MSG_MTYPE_1
, IPC_NOWAIT
) == -1);
182 ATF_REQUIRE_ERRNO(EINVAL
, msgrcv(-1, &msg
,
183 SSIZE_MAX
, MSG_MTYPE_1
, IPC_NOWAIT
) == -1);
185 ATF_REQUIRE(msgsnd(id
, &msg
, MSG_LEN
, IPC_NOWAIT
) == 0);
189 ATF_REQUIRE_ERRNO(E2BIG
, msgrcv(id
, &r
,
190 MSG_LEN
- 1, MSG_MTYPE_1
, IPC_NOWAIT
) == -1);
192 ATF_REQUIRE(msgctl(id
, IPC_RMID
, 0) == 0);
195 ATF_TC_CLEANUP(msgrcv_err
, tc
)
201 ATF_TC_WITH_CLEANUP(msgrcv_mtype
);
202 ATF_TC_HEAD(msgrcv_mtype
, tc
)
204 atf_tc_set_md_var(tc
, "descr", "Test message types with msgrcv(2)");
207 ATF_TC_BODY(msgrcv_mtype
, tc
)
209 struct msg msg1
= { MSG_MTYPE_1
, { 'a', 'b', 'c' } };
210 struct msg msg2
= { MSG_MTYPE_3
, { 'x', 'y', 'z' } };
213 id
= msgget(MSG_KEY
, IPC_CREAT
| 0600);
214 ATF_REQUIRE(id
!= -1);
216 (void)msgsnd(id
, &msg1
, MSG_LEN
, IPC_NOWAIT
);
217 (void)msgrcv(id
, &msg2
, MSG_LEN
, MSG_MTYPE_2
, IPC_NOWAIT
);
219 ATF_CHECK(msg1
.buf
[0] != msg2
.buf
[0]); /* Different mtype. */
220 ATF_CHECK(msg1
.buf
[1] != msg2
.buf
[1]);
221 ATF_CHECK(msg1
.buf
[2] != msg2
.buf
[2]);
223 (void)msgrcv(id
, &msg2
, MSG_LEN
, MSG_MTYPE_1
, IPC_NOWAIT
);
225 ATF_CHECK(msg1
.buf
[0] == msg2
.buf
[0]); /* Same mtype. */
226 ATF_CHECK(msg1
.buf
[1] == msg2
.buf
[1]);
227 ATF_CHECK(msg1
.buf
[2] == msg2
.buf
[2]);
229 ATF_REQUIRE(msgctl(id
, IPC_RMID
, 0) == 0);
232 ATF_TC_CLEANUP(msgrcv_mtype
, tc
)
237 ATF_TC_WITH_CLEANUP(msgrcv_nonblock
);
238 ATF_TC_HEAD(msgrcv_nonblock
, tc
)
240 atf_tc_set_md_var(tc
, "descr", "Test msgrcv(2) with IPC_NOWAIT");
241 atf_tc_set_md_var(tc
, "timeout", "10");
244 ATF_TC_BODY(msgrcv_nonblock
, tc
)
246 struct msg msg
= { MSG_MTYPE_1
, { 'a', 'b', 'c' } };
247 const ssize_t n
= 10;
252 id
= msgget(MSG_KEY
, IPC_CREAT
| 0600);
253 ATF_REQUIRE(id
!= -1);
255 for (i
= 0; i
< n
; i
++) {
257 ATF_REQUIRE(msgsnd(id
, &msg
, MSG_LEN
, IPC_NOWAIT
) == 0);
261 ATF_REQUIRE(pid
>= 0);
267 if (msgrcv(id
, &msg
, MSG_LEN
, MSG_MTYPE_1
,
278 (void)kill(pid
, SIGKILL
);
281 if (WIFSIGNALED(sta
) != 0 || WTERMSIG(sta
) == SIGKILL
)
282 atf_tc_fail("msgrcv(2) blocked with IPC_NOWAIT");
284 if (WIFEXITED(sta
) == 0 && WEXITSTATUS(sta
) != EXIT_SUCCESS
)
285 atf_tc_fail("msgrcv(2) failed");
287 ATF_REQUIRE(msgctl(id
, IPC_RMID
, 0) == 0);
290 ATF_TC_CLEANUP(msgrcv_nonblock
, tc
)
295 ATF_TC_WITH_CLEANUP(msgrcv_truncate
);
296 ATF_TC_HEAD(msgrcv_truncate
, tc
)
298 atf_tc_set_md_var(tc
, "descr", "Test msgrcv(2) with MSG_NOERROR");
301 ATF_TC_BODY(msgrcv_truncate
, tc
)
303 #define MSG_SMALLLEN 2
306 char buf
[MSG_SMALLLEN
];
309 struct msg msg1
= { MSG_MTYPE_1
, { 'a', 'b', 'c' } };
310 struct msgsmall msg2
= { MSG_MTYPE_1
, { 'x', 'y' } };
313 id
= msgget(MSG_KEY
, IPC_CREAT
| 0600);
314 ATF_REQUIRE(id
!= -1);
316 (void)msgsnd(id
, &msg1
, MSG_LEN
, IPC_NOWAIT
);
317 (void)msgrcv(id
, &msg2
, MSG_SMALLLEN
,
318 MSG_MTYPE_1
, IPC_NOWAIT
| MSG_NOERROR
);
320 ATF_CHECK(msg1
.buf
[0] == msg2
.buf
[0]);
321 ATF_CHECK(msg1
.buf
[1] == msg2
.buf
[1]);
323 ATF_REQUIRE(msgctl(id
, IPC_RMID
, 0) == 0);
326 ATF_TC_CLEANUP(msgrcv_truncate
, tc
)
334 ATF_TP_ADD_TC(tp
, msgrcv_basic
);
335 ATF_TP_ADD_TC(tp
, msgrcv_block
);
336 ATF_TP_ADD_TC(tp
, msgrcv_err
);
337 ATF_TP_ADD_TC(tp
, msgrcv_mtype
);
338 ATF_TP_ADD_TC(tp
, msgrcv_nonblock
);
339 ATF_TP_ADD_TC(tp
, msgrcv_truncate
);
341 return atf_no_error();