etc/protocols - sync with NetBSD-8
[minix.git] / tests / lib / libc / sys / t_msgget.c
blobe26cde2ddfe65f2f55f85be008de2c7d11a21c7c
1 /* $NetBSD: t_msgget.c,v 1.2 2014/02/27 00:59:50 joerg 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_msgget.c,v 1.2 2014/02/27 00:59:50 joerg 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 <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <sysexits.h>
46 #include <time.h>
47 #include <unistd.h>
49 #define MSG_KEY 12345689
51 static void clean(void);
53 static void
54 clean(void)
56 int id;
58 if ((id = msgget(MSG_KEY, 0)) != -1)
59 (void)msgctl(id, IPC_RMID, 0);
62 ATF_TC_WITH_CLEANUP(msgget_excl);
63 ATF_TC_HEAD(msgget_excl, tc)
65 atf_tc_set_md_var(tc, "descr", "Test msgget(2) with IPC_EXCL");
68 ATF_TC_BODY(msgget_excl, tc)
70 int id;
73 * Create a message queue and re-open it with
74 * O_CREAT and IPC_EXCL set. This should fail.
76 id = msgget(MSG_KEY, IPC_CREAT | 0600);
78 if (id < 0)
79 atf_tc_fail("failed to create message queue");
81 errno = 0;
83 if (msgget(MSG_KEY, IPC_CREAT | IPC_EXCL | 0600) != -1)
84 atf_tc_fail("msgget(2) failed for IPC_EXCL");
86 ATF_REQUIRE(errno == EEXIST);
89 * However, the same call should succeed
90 * when IPC_EXCL is not set in the flags.
92 id = msgget(MSG_KEY, IPC_CREAT | 0600);
94 if (id < 0)
95 atf_tc_fail("msgget(2) failed to re-open");
97 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
100 ATF_TC_CLEANUP(msgget_excl, tc)
102 clean();
105 ATF_TC_WITH_CLEANUP(msgget_exit);
106 ATF_TC_HEAD(msgget_exit, tc)
108 atf_tc_set_md_var(tc, "descr",
109 "Test that XSI message queues are "
110 "not removed when the process exits");
113 ATF_TC_BODY(msgget_exit, tc)
115 int id, sta;
116 pid_t pid;
118 pid = fork();
119 ATF_REQUIRE(pid >= 0);
121 if (pid == 0) {
123 if (msgget(MSG_KEY, IPC_CREAT | IPC_EXCL | 0600) == -1)
124 _exit(EXIT_FAILURE);
126 _exit(EXIT_SUCCESS);
129 (void)wait(&sta);
131 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
132 atf_tc_fail("failed to create message queue");
134 id = msgget(MSG_KEY, 0);
136 if (id == -1)
137 atf_tc_fail("message queue was removed on process exit");
139 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
142 ATF_TC_CLEANUP(msgget_exit, tc)
144 clean();
147 ATF_TC_WITH_CLEANUP(msgget_init);
148 ATF_TC_HEAD(msgget_init, tc)
150 atf_tc_set_md_var(tc, "descr",
151 "Test that msgget(2) initializes data structures properly");
154 ATF_TC_BODY(msgget_init, tc)
156 const uid_t uid = geteuid();
157 const gid_t gid = getegid();
158 struct msqid_ds msgds;
159 time_t t;
160 int id;
162 (void)memset(&msgds, 0x9, sizeof(struct msqid_ds));
164 t = time(NULL);
165 id = msgget(MSG_KEY, IPC_CREAT | 0600);
167 ATF_REQUIRE(id !=-1);
168 ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
170 ATF_CHECK(msgds.msg_qnum == 0);
171 ATF_CHECK(msgds.msg_lspid == 0);
172 ATF_CHECK(msgds.msg_lrpid == 0);
173 ATF_CHECK(msgds.msg_rtime == 0);
174 ATF_CHECK(msgds.msg_stime == 0);
175 ATF_CHECK(msgds.msg_perm.uid == uid);
176 ATF_CHECK(msgds.msg_perm.gid == gid);
177 ATF_CHECK(msgds.msg_perm.cuid == uid);
178 ATF_CHECK(msgds.msg_perm.cgid == gid);
179 ATF_CHECK(msgds.msg_perm.mode == 0600);
181 if (llabs(t - msgds.msg_ctime) > 5)
182 atf_tc_fail("msgget(2) initialized current time incorrectly");
184 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
187 ATF_TC_CLEANUP(msgget_init, tc)
189 clean();
192 ATF_TC(msgget_limit);
193 ATF_TC_HEAD(msgget_limit, tc)
195 atf_tc_set_md_var(tc, "descr", "Test msgget(2) against system limits");
198 ATF_TC_BODY(msgget_limit, tc)
200 size_t len = sizeof(int);
201 bool fail = false;
202 int i, lim = 0;
203 int *buf;
205 if (sysctlbyname("kern.ipc.msgmni", &lim, &len, NULL, 0) != 0)
206 atf_tc_skip("failed to read kern.ipc.msgmni sysctl");
208 buf = calloc(lim + 1, sizeof(*buf));
209 ATF_REQUIRE(buf != NULL);
211 for (i = 0; i < lim; i++) {
213 buf[i] = msgget(MSG_KEY + i, IPC_CREAT | IPC_EXCL | 0600);
215 (void)fprintf(stderr, "key[%d] = %d\n", i, buf[i]);
218 * This test only works when there are zero existing
219 * message queues. Thus, bypass the unit test when
220 * this precondition is not met, for reason or another.
222 if (buf[i] == -1)
223 goto out;
226 i++;
227 errno = 0;
229 buf[i] = msgget(MSG_KEY + i, IPC_CREAT | IPC_EXCL | 0600);
231 if (buf[i] != -1 || errno != ENOSPC)
232 fail = true;
234 out: /* Remember to clean-up. */
235 for (i = 0; i < lim; i++)
236 (void)msgctl(buf[i], IPC_RMID, 0);
238 free(buf);
240 if (fail != false)
241 atf_tc_fail("msgget(2) opened more than %d queues", lim);
244 ATF_TC_WITH_CLEANUP(msgget_mode);
245 ATF_TC_HEAD(msgget_mode, tc)
247 atf_tc_set_md_var(tc, "descr", "Test different modes with msgget(2)");
248 atf_tc_set_md_var(tc, "require.user", "root");
251 ATF_TC_BODY(msgget_mode, tc)
253 static const mode_t mode[] = {
254 S_IRWXU, S_IRUSR, S_IWUSR, S_IXUSR, S_IRWXG, S_IRGRP,
255 S_IWGRP, S_IXGRP, S_IRWXO, S_IROTH, S_IWOTH, S_IXOTH
258 struct msqid_ds msgds;
259 size_t i;
260 int id;
262 for (i = 0; i < __arraycount(mode); i++) {
264 (void)fprintf(stderr, "testing mode %d\n", mode[i]);
265 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
267 id = msgget(MSG_KEY, IPC_CREAT | IPC_EXCL | (int)mode[i]);
269 ATF_REQUIRE(id != -1);
270 ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
271 ATF_REQUIRE(msgds.msg_perm.mode == mode[i]);
272 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
276 ATF_TC_CLEANUP(msgget_mode, tc)
278 clean();
282 ATF_TP_ADD_TCS(tp)
285 ATF_TP_ADD_TC(tp, msgget_excl);
286 ATF_TP_ADD_TC(tp, msgget_exit);
287 ATF_TP_ADD_TC(tp, msgget_init);
288 ATF_TP_ADD_TC(tp, msgget_limit);
289 ATF_TP_ADD_TC(tp, msgget_mode);
291 return atf_no_error();