Remove building with NOCRYPTO option
[minix3.git] / tests / lib / libc / sys / t_msgrcv.c
blobe32be262aa15067e510e9652c0a87a3fb3c6b971
1 /* $NetBSD: t_msgrcv.c,v 1.3 2013/07/24 11:44:10 skrll Exp $ */
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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 $");
34 #include <sys/msg.h>
35 #include <sys/stat.h>
36 #include <sys/sysctl.h>
37 #include <sys/wait.h>
39 #include <atf-c.h>
40 #include <errno.h>
41 #include <pwd.h>
42 #include <signal.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <sysexits.h>
47 #include <time.h>
48 #include <unistd.h>
50 #define MSG_KEY 1234
51 #define MSG_MTYPE_1 0x41
52 #define MSG_MTYPE_2 0x42
53 #define MSG_MTYPE_3 0x43
54 #define MSG_LEN 3
56 struct msg {
57 long mtype;
58 char buf[MSG_LEN];
61 static void clean(void);
63 static void
64 clean(void)
66 int id;
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' } };
82 int id;
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)
99 clean();
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' } };
111 int id, sta;
112 pid_t pid;
114 id = msgget(MSG_KEY, IPC_CREAT | 0600);
115 ATF_REQUIRE(id != -1);
117 pid = fork();
118 ATF_REQUIRE(pid >= 0);
120 if (pid == 0) {
122 if (msgrcv(id, &msg, MSG_LEN, MSG_MTYPE_1, 0) < 0)
123 _exit(EXIT_FAILURE);
125 _exit(EXIT_SUCCESS);
129 * Below msgsnd(2) should unblock the child,
130 * and hence kill(2) should fail with ESRCH.
132 (void)sleep(1);
133 (void)msgsnd(id, &msg, MSG_LEN, IPC_NOWAIT);
134 (void)sleep(1);
135 (void)kill(pid, SIGKILL);
136 (void)wait(&sta);
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)
146 clean();
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' } };
158 int id, r = 0;
160 id = msgget(MSG_KEY, IPC_CREAT | 0600);
161 ATF_REQUIRE(id != -1);
163 errno = 0;
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);
170 errno = 0;
172 ATF_REQUIRE_ERRNO(EFAULT, msgrcv(id, (void *)-1,
173 MSG_LEN, MSG_MTYPE_1, IPC_NOWAIT) == -1);
175 errno = 0;
177 ATF_REQUIRE_ERRNO(EINVAL, msgrcv(-1, &msg,
178 MSG_LEN, MSG_MTYPE_1, IPC_NOWAIT) == -1);
180 errno = 0;
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);
187 errno = 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)
197 clean();
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' } };
211 int id;
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)
234 clean();
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;
248 int id, sta;
249 ssize_t i;
250 pid_t pid;
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);
260 pid = fork();
261 ATF_REQUIRE(pid >= 0);
263 if (pid == 0) {
265 while (i != 0) {
267 if (msgrcv(id, &msg, MSG_LEN, MSG_MTYPE_1,
268 IPC_NOWAIT) == -1)
269 _exit(EXIT_FAILURE);
271 i--;
274 _exit(EXIT_SUCCESS);
277 (void)sleep(2);
278 (void)kill(pid, SIGKILL);
279 (void)wait(&sta);
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)
292 clean();
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
304 struct msgsmall {
305 long mtype;
306 char buf[MSG_SMALLLEN];
309 struct msg msg1 = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
310 struct msgsmall msg2 = { MSG_MTYPE_1, { 'x', 'y' } };
311 int id;
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)
328 clean();
331 ATF_TP_ADD_TCS(tp)
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();