Remove building with NOCRYPTO option
[minix3.git] / tests / lib / libc / sys / t_msgctl.c
blob1e74d60703ed507a3c23e070f62fae733d940681
1 /* $NetBSD: t_msgctl.c,v 1.4 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_msgctl.c,v 1.4 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
50 #define MSG_MTYPE_1 0x41
52 struct msg {
53 long mtype;
54 char buf[3];
57 static void clean(void);
59 static void
60 clean(void)
62 int id;
64 if ((id = msgget(MSG_KEY, 0)) != -1)
65 (void)msgctl(id, IPC_RMID, 0);
68 ATF_TC_WITH_CLEANUP(msgctl_err);
69 ATF_TC_HEAD(msgctl_err, tc)
71 atf_tc_set_md_var(tc, "descr", "Test errors from msgctl(2)");
74 ATF_TC_BODY(msgctl_err, tc)
76 const int cmd[] = { IPC_STAT, IPC_SET, IPC_RMID };
77 struct msqid_ds msgds;
78 size_t i;
79 int id;
81 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
83 id = msgget(MSG_KEY, IPC_CREAT | 0600);
84 ATF_REQUIRE(id != -1);
86 errno = 0;
87 ATF_REQUIRE_ERRNO(EINVAL, msgctl(id, INT_MAX, &msgds) == -1);
89 errno = 0;
90 ATF_REQUIRE_ERRNO(EFAULT, msgctl(id, IPC_STAT, (void *)-1) == -1);
92 for (i = 0; i < __arraycount(cmd); i++) {
93 errno = 0;
94 ATF_REQUIRE_ERRNO(EINVAL, msgctl(-1, cmd[i], &msgds) == -1);
97 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
100 ATF_TC_CLEANUP(msgctl_err, tc)
102 clean();
105 ATF_TC_WITH_CLEANUP(msgctl_perm);
106 ATF_TC_HEAD(msgctl_perm, tc)
108 atf_tc_set_md_var(tc, "descr", "Test permissions with msgctl(2)");
109 atf_tc_set_md_var(tc, "require.user", "root");
112 ATF_TC_BODY(msgctl_perm, tc)
114 struct msqid_ds msgds;
115 struct passwd *pw;
116 pid_t pid;
117 int sta;
118 int id;
120 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
122 pw = getpwnam("nobody");
123 id = msgget(MSG_KEY, IPC_CREAT | 0600);
125 ATF_REQUIRE(id != -1);
126 ATF_REQUIRE(pw != NULL);
127 ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
129 pid = fork();
130 ATF_REQUIRE(pid >= 0);
132 if (pid == 0) {
134 if (setuid(pw->pw_uid) != 0)
135 _exit(EX_OSERR);
137 msgds.msg_perm.uid = getuid();
138 msgds.msg_perm.gid = getgid();
140 errno = 0;
142 if (msgctl(id, IPC_SET, &msgds) == 0)
143 _exit(EXIT_FAILURE);
145 if (errno != EPERM)
146 _exit(EXIT_FAILURE);
148 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
150 if (msgctl(id, IPC_STAT, &msgds) != 0)
151 _exit(EX_OSERR);
153 msgds.msg_qbytes = 1;
155 if (msgctl(id, IPC_SET, &msgds) == 0)
156 _exit(EXIT_FAILURE);
158 if (errno != EPERM)
159 _exit(EXIT_FAILURE);
161 _exit(EXIT_SUCCESS);
164 (void)wait(&sta);
166 if (WIFEXITED(sta) == 0) {
168 if (WEXITSTATUS(sta) == EX_OSERR)
169 atf_tc_fail("system call failed");
171 if (WEXITSTATUS(sta) == EXIT_FAILURE)
172 atf_tc_fail("UID %u manipulated root's "
173 "message queue", pw->pw_uid);
176 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
179 ATF_TC_CLEANUP(msgctl_perm, tc)
181 clean();
184 ATF_TC_WITH_CLEANUP(msgctl_pid);
185 ATF_TC_HEAD(msgctl_pid, tc)
187 atf_tc_set_md_var(tc, "descr", "Test that PIDs are updated");
190 ATF_TC_BODY(msgctl_pid, tc)
192 struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
193 struct msqid_ds msgds;
194 int id, sta;
195 pid_t pid;
197 id = msgget(MSG_KEY, IPC_CREAT | 0600);
198 ATF_REQUIRE(id != -1);
200 pid = fork();
201 ATF_REQUIRE(pid >= 0);
203 if (pid == 0) {
205 (void)msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT);
207 _exit(EXIT_SUCCESS);
210 (void)sleep(1);
211 (void)wait(&sta);
212 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
214 ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
216 if (pid != msgds.msg_lspid)
217 atf_tc_fail("the PID of last msgsnd(2) was not updated");
219 pid = fork();
220 ATF_REQUIRE(pid >= 0);
222 if (pid == 0) {
224 (void)msgrcv(id, &msg,
225 sizeof(struct msg), MSG_MTYPE_1, IPC_NOWAIT);
227 _exit(EXIT_SUCCESS);
230 (void)sleep(1);
231 (void)wait(&sta);
232 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
234 ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
236 if (pid != msgds.msg_lrpid)
237 atf_tc_fail("the PID of last msgrcv(2) was not updated");
239 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
242 ATF_TC_CLEANUP(msgctl_pid, tc)
244 clean();
247 ATF_TC_WITH_CLEANUP(msgctl_set);
248 ATF_TC_HEAD(msgctl_set, tc)
250 atf_tc_set_md_var(tc, "descr", "Test msgctl(2) with IPC_SET");
251 atf_tc_set_md_var(tc, "require.user", "root");
254 ATF_TC_BODY(msgctl_set, tc)
256 struct msqid_ds msgds;
257 struct passwd *pw;
258 int id;
260 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
262 pw = getpwnam("nobody");
263 id = msgget(MSG_KEY, IPC_CREAT | 0600);
265 ATF_REQUIRE(id != -1);
266 ATF_REQUIRE(pw != NULL);
267 ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
269 msgds.msg_perm.uid = pw->pw_uid;
271 if (msgctl(id, IPC_SET, &msgds) != 0)
272 atf_tc_fail("root failed to change the UID of message queue");
274 msgds.msg_perm.uid = getuid();
275 msgds.msg_perm.gid = pw->pw_gid;
277 if (msgctl(id, IPC_SET, &msgds) != 0)
278 atf_tc_fail("root failed to change the GID of message queue");
281 * Note: setting the qbytes to zero fails even as root.
283 msgds.msg_qbytes = 1;
284 msgds.msg_perm.gid = getgid();
286 if (msgctl(id, IPC_SET, &msgds) != 0)
287 atf_tc_fail("root failed to change qbytes of message queue");
289 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
292 ATF_TC_CLEANUP(msgctl_set, tc)
294 clean();
297 ATF_TC_WITH_CLEANUP(msgctl_time);
298 ATF_TC_HEAD(msgctl_time, tc)
300 atf_tc_set_md_var(tc, "descr", "Test that access times are updated");
303 ATF_TC_BODY(msgctl_time, tc)
305 struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
306 struct msqid_ds msgds;
307 time_t t;
308 int id;
310 id = msgget(MSG_KEY, IPC_CREAT | 0600);
311 ATF_REQUIRE(id != -1);
313 t = time(NULL);
315 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
316 (void)msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT);
317 (void)msgctl(id, IPC_STAT, &msgds);
319 if (llabs(t - msgds.msg_stime) > 1)
320 atf_tc_fail("time of last msgsnd(2) was not updated");
322 if (msgds.msg_rtime != 0)
323 atf_tc_fail("time of last msgrcv(2) was updated incorrectly");
325 t = time(NULL);
327 (void)memset(&msgds, 0, sizeof(struct msqid_ds));
328 (void)msgrcv(id, &msg, sizeof(struct msg), MSG_MTYPE_1, IPC_NOWAIT);
329 (void)msgctl(id, IPC_STAT, &msgds);
331 if (llabs(t - msgds.msg_rtime) > 1)
332 atf_tc_fail("time of last msgrcv(2) was not updated");
335 * Note: this is non-zero even after the memset(3).
337 if (msgds.msg_stime == 0)
338 atf_tc_fail("time of last msgsnd(2) was updated incorrectly");
340 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
343 ATF_TC_CLEANUP(msgctl_time, tc)
345 clean();
348 ATF_TP_ADD_TCS(tp)
351 ATF_TP_ADD_TC(tp, msgctl_err);
352 ATF_TP_ADD_TC(tp, msgctl_perm);
353 ATF_TP_ADD_TC(tp, msgctl_pid);
354 ATF_TP_ADD_TC(tp, msgctl_set);
355 ATF_TP_ADD_TC(tp, msgctl_time);
357 return atf_no_error();