Module Name:    src
Committed By:   jruoho
Date:           Sat Nov  5 07:45:41 UTC 2011

Modified Files:
        src/distrib/sets/lists/tests: mi
        src/tests/lib/libc/sys: Makefile
Added Files:
        src/tests/lib/libc/sys: t_msgctl.c t_msgget.c t_msgrcv.c t_msgsnd.c

Log Message:
Add 21 unit tests for the XSI message queues.


To generate a diff of this commit:
cvs rdiff -u -r1.414 -r1.415 src/distrib/sets/lists/tests/mi
cvs rdiff -u -r1.17 -r1.18 src/tests/lib/libc/sys/Makefile
cvs rdiff -u -r0 -r1.1 src/tests/lib/libc/sys/t_msgctl.c \
    src/tests/lib/libc/sys/t_msgget.c src/tests/lib/libc/sys/t_msgrcv.c \
    src/tests/lib/libc/sys/t_msgsnd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/distrib/sets/lists/tests/mi
diff -u src/distrib/sets/lists/tests/mi:1.414 src/distrib/sets/lists/tests/mi:1.415
--- src/distrib/sets/lists/tests/mi:1.414	Fri Nov  4 02:25:31 2011
+++ src/distrib/sets/lists/tests/mi	Sat Nov  5 07:45:40 2011
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.414 2011/11/04 02:25:31 christos Exp $
+# $NetBSD: mi,v 1.415 2011/11/05 07:45:40 jruoho Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -511,6 +511,10 @@
 ./usr/libdata/debug/usr/tests/lib/libc/sys/t_mknod.debug		tests-lib-debug		debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/sys/t_mmap.debug			tests-lib-debug		debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/sys/t_mprotect.debug		tests-lib-debug		debug,atf
+./usr/libdata/debug/usr/tests/lib/libc/sys/t_msgctl.debug		tests-lib-debug		debug,atf
+./usr/libdata/debug/usr/tests/lib/libc/sys/t_msgget.debug		tests-lib-debug		debug,atf
+./usr/libdata/debug/usr/tests/lib/libc/sys/t_msgrcv.debug		tests-lib-debug		debug,atf
+./usr/libdata/debug/usr/tests/lib/libc/sys/t_msgsnd.debug		tests-lib-debug		debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/sys/t_msync.debug		tests-lib-debug		debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/sys/t_nanosleep.debug		tests-lib-debug		debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/sys/t_pipe.debug			tests-lib-debug		debug,atf
@@ -2137,6 +2141,10 @@
 ./usr/tests/lib/libc/sys/t_mknod		tests-lib-tests		atf
 ./usr/tests/lib/libc/sys/t_mmap			tests-lib-tests		atf
 ./usr/tests/lib/libc/sys/t_mprotect		tests-lib-tests		atf
+./usr/tests/lib/libc/sys/t_msgctl		tests-lib-tests		atf
+./usr/tests/lib/libc/sys/t_msgget		tests-lib-tests		atf
+./usr/tests/lib/libc/sys/t_msgrcv		tests-lib-tests		atf
+./usr/tests/lib/libc/sys/t_msgsnd		tests-lib-tests		atf
 ./usr/tests/lib/libc/sys/t_msync		tests-lib-tests		atf
 ./usr/tests/lib/libc/sys/t_nanosleep		tests-lib-tests		atf
 ./usr/tests/lib/libc/sys/t_pipe			tests-lib-tests		atf

Index: src/tests/lib/libc/sys/Makefile
diff -u src/tests/lib/libc/sys/Makefile:1.17 src/tests/lib/libc/sys/Makefile:1.18
--- src/tests/lib/libc/sys/Makefile:1.17	Mon Oct 31 15:41:31 2011
+++ src/tests/lib/libc/sys/Makefile	Sat Nov  5 07:45:41 2011
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.17 2011/10/31 15:41:31 christos Exp $
+# $NetBSD: Makefile,v 1.18 2011/11/05 07:45:41 jruoho Exp $
 
 MKMAN=	no
 
@@ -31,6 +31,10 @@ TESTS_C+=		t_mkfifo
 TESTS_C+=		t_mknod
 TESTS_C+=		t_mmap
 TESTS_C+=		t_mprotect
+TESTS_C+=		t_msgctl
+TESTS_C+=		t_msgget
+TESTS_C+=		t_msgrcv
+TESTS_C+=		t_msgsnd
 TESTS_C+=		t_msync
 TESTS_C+=		t_nanosleep
 TESTS_C+=		t_pipe

Added files:

Index: src/tests/lib/libc/sys/t_msgctl.c
diff -u /dev/null src/tests/lib/libc/sys/t_msgctl.c:1.1
--- /dev/null	Sat Nov  5 07:45:41 2011
+++ src/tests/lib/libc/sys/t_msgctl.c	Sat Nov  5 07:45:41 2011
@@ -0,0 +1,358 @@
+/* $NetBSD: t_msgctl.c,v 1.1 2011/11/05 07:45:41 jruoho Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jukka Ruohonen.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: t_msgctl.c,v 1.1 2011/11/05 07:45:41 jruoho Exp $");
+
+#include <sys/msg.h>
+#include <sys/stat.h>
+#include <sys/sysctl.h>
+#include <sys/wait.h>
+
+#include <atf-c.h>
+#include <errno.h>
+#include <pwd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sysexits.h>
+#include <time.h>
+#include <unistd.h>
+
+#define MSG_KEY		12345689
+#define MSG_MTYPE_1	0x41
+
+struct msg {
+	long		 mtype;
+	char		 buf[3];
+};
+
+static void		clean(void);
+
+static void
+clean(void)
+{
+	int id;
+
+	if ((id = msgget(MSG_KEY, 0)) != -1)
+		(void)msgctl(id, IPC_RMID, 0);
+}
+
+ATF_TC_WITH_CLEANUP(msgctl_err);
+ATF_TC_HEAD(msgctl_err, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test errors from msgctl(2)");
+}
+
+ATF_TC_BODY(msgctl_err, tc)
+{
+	const int cmd[] = { IPC_STAT, IPC_SET, IPC_RMID };
+	struct msqid_ds msgds;
+	size_t i;
+	int id;
+
+	(void)memset(&msgds, 0, sizeof(struct msqid_ds));
+
+	id = msgget(MSG_KEY, IPC_CREAT | 0600);
+	ATF_REQUIRE(id != -1);
+
+	errno = 0;
+	ATF_REQUIRE_ERRNO(EINVAL, msgctl(id, INT_MAX, &msgds) == -1);
+
+	errno = 0;
+	ATF_REQUIRE_ERRNO(EFAULT, msgctl(id, IPC_STAT, (void *)-1) == -1);
+
+	for (i = 0; i < __arraycount(cmd); i++) {
+		errno = 0;
+		ATF_REQUIRE_ERRNO(EINVAL, msgctl(-1, cmd[i], &msgds) == -1);
+	}
+
+	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
+}
+
+ATF_TC_CLEANUP(msgctl_err, tc)
+{
+	clean();
+}
+
+ATF_TC_WITH_CLEANUP(msgctl_perm);
+ATF_TC_HEAD(msgctl_perm, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test permissiosn with msgctl(2)");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(msgctl_perm, tc)
+{
+	struct msqid_ds msgds;
+	struct passwd *pw;
+	pid_t pid;
+	int sta;
+	int id;
+
+	(void)memset(&msgds, 0, sizeof(struct msqid_ds));
+
+	pw = getpwnam("nobody");
+	id = msgget(MSG_KEY, IPC_CREAT | 0600);
+
+	ATF_REQUIRE(id != -1);
+	ATF_REQUIRE(pw != NULL);
+	ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
+
+	pid = fork();
+	ATF_REQUIRE(pid >= 0);
+
+	if (pid == 0) {
+
+		if (setuid(pw->pw_uid) != 0)
+			_exit(EX_OSERR);
+
+		msgds.msg_perm.uid = getuid();
+		msgds.msg_perm.gid = getgid();
+
+		errno = 0;
+
+		if (msgctl(id, IPC_SET, &msgds) == 0)
+			_exit(EXIT_FAILURE);
+
+		if (errno != EPERM)
+			_exit(EXIT_FAILURE);
+
+		(void)memset(&msgds, 0, sizeof(struct msqid_ds));
+
+		if (msgctl(id, IPC_STAT, &msgds) != 0)
+			_exit(EX_OSERR);
+
+		msgds.msg_qbytes = 1;
+
+		if (msgctl(id, IPC_SET, &msgds) == 0)
+			_exit(EXIT_FAILURE);
+
+		if (errno != EPERM)
+			_exit(EXIT_FAILURE);
+
+		_exit(EXIT_SUCCESS);
+	}
+
+	(void)wait(&sta);
+
+	if (WIFEXITED(sta) == 0) {
+
+		if (WEXITSTATUS(sta) == EX_OSERR)
+			atf_tc_fail("system call failed");
+
+		if (WEXITSTATUS(sta) == EXIT_FAILURE)
+			atf_tc_fail("UID %u manipulated root's "
+			    "message queue", pw->pw_uid);
+	}
+
+	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
+}
+
+ATF_TC_CLEANUP(msgctl_perm, tc)
+{
+	clean();
+}
+
+ATF_TC_WITH_CLEANUP(msgctl_pid);
+ATF_TC_HEAD(msgctl_pid, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test that PIDs are updated");
+}
+
+ATF_TC_BODY(msgctl_pid, tc)
+{
+	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
+	struct msqid_ds msgds;
+	int id, sta;
+	pid_t pid;
+
+	id = msgget(MSG_KEY, IPC_CREAT | 0600);
+	ATF_REQUIRE(id != -1);
+
+	pid = fork();
+	ATF_REQUIRE(pid >= 0);
+
+	if (pid == 0) {
+
+		(void)msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT);
+
+		_exit(EXIT_SUCCESS);
+	}
+
+	(void)sleep(1);
+	(void)wait(&sta);
+	(void)memset(&msgds, 0, sizeof(struct msqid_ds));
+
+	ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
+
+	if (pid != msgds.msg_lspid)
+		atf_tc_fail("the PID of last msgsnd(2) was not updated");
+
+	pid = fork();
+	ATF_REQUIRE(pid >= 0);
+
+	if (pid == 0) {
+
+		(void)msgrcv(id, &msg,
+		    sizeof(struct msg), MSG_MTYPE_1, IPC_NOWAIT);
+
+		_exit(EXIT_SUCCESS);
+	}
+
+	(void)sleep(1);
+	(void)wait(&sta);
+	(void)memset(&msgds, 0, sizeof(struct msqid_ds));
+
+	ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
+
+	if (pid != msgds.msg_lrpid)
+		atf_tc_fail("the PID of last msgrcv(2) was not updated");
+
+	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
+}
+
+ATF_TC_CLEANUP(msgctl_pid, tc)
+{
+	clean();
+}
+
+ATF_TC_WITH_CLEANUP(msgctl_set);
+ATF_TC_HEAD(msgctl_set, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test msgctl(2) with IPC_SET");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(msgctl_set, tc)
+{
+	struct msqid_ds msgds;
+	struct passwd *pw;
+	int id;
+
+	(void)memset(&msgds, 0, sizeof(struct msqid_ds));
+
+	pw = getpwnam("nobody");
+	id = msgget(MSG_KEY, IPC_CREAT | 0600);
+
+	ATF_REQUIRE(id != -1);
+	ATF_REQUIRE(pw != NULL);
+	ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
+
+	msgds.msg_perm.uid = pw->pw_uid;
+
+	if (msgctl(id, IPC_SET, &msgds) != 0)
+		atf_tc_fail("root failed to change the UID of message queue");
+
+	msgds.msg_perm.uid = getuid();
+	msgds.msg_perm.gid = pw->pw_gid;
+
+	if (msgctl(id, IPC_SET, &msgds) != 0)
+		atf_tc_fail("root failed to change the GID of message queue");
+
+	/*
+	 * Note setting the qbytes to zero fails even as root.
+	 */
+	msgds.msg_qbytes = 1;
+	msgds.msg_perm.gid = getgid();
+
+	if (msgctl(id, IPC_SET, &msgds) != 0)
+		atf_tc_fail("root failed to change qbytes of message queue");
+
+	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
+}
+
+ATF_TC_CLEANUP(msgctl_set, tc)
+{
+	clean();
+}
+
+ATF_TC_WITH_CLEANUP(msgctl_time);
+ATF_TC_HEAD(msgctl_time, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test msgctl(2) updates times");
+}
+
+ATF_TC_BODY(msgctl_time, tc)
+{
+	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
+	struct msqid_ds msgds;
+	time_t t;
+	int id;
+
+	id = msgget(MSG_KEY, IPC_CREAT | 0600);
+	ATF_REQUIRE(id != -1);
+
+	t = time(NULL);
+
+	(void)memset(&msgds, 0, sizeof(struct msqid_ds));
+	(void)msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT);
+	(void)msgctl(id, IPC_STAT, &msgds);
+
+	if (abs(t - msgds.msg_stime) > 1)
+		atf_tc_fail("time of last msgsnd(2) was not updated");
+
+	if (msgds.msg_rtime != 0)
+		atf_tc_fail("time of last msgrcv(2) was updated incorrectly");
+
+	t = time(NULL);
+
+	(void)memset(&msgds, 0, sizeof(struct msqid_ds));
+	(void)msgrcv(id, &msg, sizeof(struct msg), MSG_MTYPE_1, IPC_NOWAIT);
+	(void)msgctl(id, IPC_STAT, &msgds);
+
+	if (abs(t - msgds.msg_rtime) > 1)
+		atf_tc_fail("time of last msgrcv(2) was not updated");
+
+	/*
+	 * Note that this is non-zero even after the memset(3).
+	 */
+	if (msgds.msg_stime == 0)
+		atf_tc_fail("time of last msgsnd(2) was updated incorrectly");
+
+	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
+}
+
+ATF_TC_CLEANUP(msgctl_time, tc)
+{
+	clean();
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+	ATF_TP_ADD_TC(tp, msgctl_err);
+	ATF_TP_ADD_TC(tp, msgctl_perm);
+	ATF_TP_ADD_TC(tp, msgctl_pid);
+	ATF_TP_ADD_TC(tp, msgctl_set);
+	ATF_TP_ADD_TC(tp, msgctl_time);
+
+	return atf_no_error();
+}
Index: src/tests/lib/libc/sys/t_msgget.c
diff -u /dev/null src/tests/lib/libc/sys/t_msgget.c:1.1
--- /dev/null	Sat Nov  5 07:45:41 2011
+++ src/tests/lib/libc/sys/t_msgget.c	Sat Nov  5 07:45:41 2011
@@ -0,0 +1,292 @@
+/* $NetBSD: t_msgget.c,v 1.1 2011/11/05 07:45:41 jruoho Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jukka Ruohonen.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: t_msgget.c,v 1.1 2011/11/05 07:45:41 jruoho Exp $");
+
+#include <sys/msg.h>
+#include <sys/stat.h>
+#include <sys/sysctl.h>
+#include <sys/wait.h>
+
+#include <atf-c.h>
+#include <errno.h>
+#include <pwd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sysexits.h>
+#include <time.h>
+#include <unistd.h>
+
+#define MSG_KEY		12345689
+
+static void		clean(void);
+
+static void
+clean(void)
+{
+	int id;
+
+	if ((id = msgget(MSG_KEY, 0)) != -1)
+		(void)msgctl(id, IPC_RMID, 0);
+}
+
+ATF_TC_WITH_CLEANUP(msgget_excl);
+ATF_TC_HEAD(msgget_excl, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test msgget(2) with IPC_EXCL");
+}
+
+ATF_TC_BODY(msgget_excl, tc)
+{
+	int id;
+
+	/*
+	 * Create a message queue and re-open it with
+	 * O_CREAT and IPC_EXCL set. This should fail.
+	 */
+	id = msgget(MSG_KEY, IPC_CREAT | 0600);
+
+	if (id < 0)
+		atf_tc_fail("failed to create message queue");
+
+	errno = 0;
+
+	if (msgget(MSG_KEY, IPC_CREAT | IPC_EXCL | 0600) != -1)
+		atf_tc_fail("msgget(2) failed for IPC_EXCL");
+
+	ATF_REQUIRE(errno == EEXIST);
+
+	/*
+	 * However, the same call should succeed
+	 * when IPC_EXCL is not set in the flags.
+	 */
+	id = msgget(MSG_KEY, IPC_CREAT | 0600);
+
+	if (id < 0)
+		atf_tc_fail("msgget(2) failed to re-open");
+
+	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
+}
+
+ATF_TC_CLEANUP(msgget_excl, tc)
+{
+	clean();
+}
+
+ATF_TC_WITH_CLEANUP(msgget_exit);
+ATF_TC_HEAD(msgget_exit, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "Test that XSI message queues are "
+	    "not removed when the process exits");
+}
+
+ATF_TC_BODY(msgget_exit, tc)
+{
+	int id, sta;
+	pid_t pid;
+
+	pid = fork();
+	ATF_REQUIRE(pid >= 0);
+
+	if (pid == 0) {
+
+		if (msgget(MSG_KEY, IPC_CREAT | IPC_EXCL | 0600) == -1)
+			_exit(EXIT_FAILURE);
+
+		_exit(EXIT_SUCCESS);
+	}
+
+	(void)wait(&sta);
+
+	if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
+		atf_tc_fail("failed to create message queue");
+
+	id = msgget(MSG_KEY, 0);
+
+	if (id == -1)
+		atf_tc_fail("message queue was removed on process exit");
+
+	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
+}
+
+ATF_TC_CLEANUP(msgget_exit, tc)
+{
+	clean();
+}
+
+ATF_TC_WITH_CLEANUP(msgget_init);
+ATF_TC_HEAD(msgget_init, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "Test that msgget(2) initializes data structures properly");
+}
+
+ATF_TC_BODY(msgget_init, tc)
+{
+	const uid_t uid = geteuid();
+	const gid_t gid = getegid();
+	struct msqid_ds msgds;
+	time_t t;
+	int id;
+
+	(void)memset(&msgds, 0x9, sizeof(struct msqid_ds));
+
+	t = time(NULL);
+	id = msgget(MSG_KEY, IPC_CREAT | 0600);
+
+	ATF_REQUIRE(id !=-1);
+	ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
+
+	ATF_CHECK(msgds.msg_qnum == 0);
+	ATF_CHECK(msgds.msg_lspid == 0);
+	ATF_CHECK(msgds.msg_lrpid == 0);
+	ATF_CHECK(msgds.msg_rtime == 0);
+	ATF_CHECK(msgds.msg_stime == 0);
+	ATF_CHECK(msgds.msg_perm.uid == uid);
+	ATF_CHECK(msgds.msg_perm.gid == gid);
+	ATF_CHECK(msgds.msg_perm.cuid == uid);
+	ATF_CHECK(msgds.msg_perm.cgid == gid);
+	ATF_CHECK(msgds.msg_perm.mode == 0600);
+
+	if (abs(t - msgds.msg_ctime) > 5)
+		atf_tc_fail("msgget(2) initialized current time incorrectly");
+
+	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
+}
+
+ATF_TC_CLEANUP(msgget_init, tc)
+{
+	clean();
+}
+
+ATF_TC(msgget_limit);
+ATF_TC_HEAD(msgget_limit, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test msgget(2) against system limits");
+}
+
+ATF_TC_BODY(msgget_limit, tc)
+{
+	size_t len = sizeof(int);
+	bool fail = false;
+	int i, lim = 0;
+	int *buf;
+
+	if (sysctlbyname("kern.ipc.msgmni", &lim, &len, NULL, 0) != 0)
+		atf_tc_skip("failed to read kern.ipc.msgmni sysctl");
+
+	buf = calloc(lim + 1, sizeof(*buf));
+	ATF_REQUIRE(buf != NULL);
+
+	for (i = 0; i < lim; i++) {
+
+		buf[i] = msgget(MSG_KEY + i, IPC_CREAT | IPC_EXCL | 0600);
+
+		(void)fprintf(stderr, "key[%d] = %d\n", i, buf[i]);
+
+		/*
+		 * This test only works when there are zero existing
+		 * message queues. Thus, bypass the unit test when
+		 * this precondition is not met, for reason or another.
+		 */
+		if (buf[i] == -1)
+			goto out;
+	}
+
+	i++;
+	errno = 0;
+
+	buf[i] = msgget(MSG_KEY + i, IPC_CREAT | IPC_EXCL | 0600);
+
+	if (buf[i] != -1 || errno != ENOSPC)
+		fail = true;
+
+out:	/* Remember to clean-up. */
+	for (i = 0; i < lim; i++)
+		(void)msgctl(buf[i], IPC_RMID, 0);
+
+	free(buf);
+
+	if (fail != false)
+		atf_tc_fail("msgget(2) opened more than %d queues", lim);
+}
+
+ATF_TC_WITH_CLEANUP(msgget_mode);
+ATF_TC_HEAD(msgget_mode, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test different modes with msgget(2)");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(msgget_mode, tc)
+{
+	static const mode_t mode[] = {
+		S_IRWXU, S_IRUSR, S_IWUSR, S_IXUSR, S_IRWXG, S_IRGRP,
+		S_IWGRP, S_IXGRP, S_IRWXO, S_IROTH, S_IWOTH, S_IXOTH
+	};
+
+	struct msqid_ds msgds;
+	size_t i;
+	int id;
+
+	for (i = 0; i < __arraycount(mode); i++) {
+
+		(void)fprintf(stderr, "testing mode %d\n", mode[i]);
+		(void)memset(&msgds, 0, sizeof(struct msqid_ds));
+
+		id = msgget(MSG_KEY, IPC_CREAT | IPC_EXCL | (int)mode[i]);
+
+		ATF_REQUIRE(id != -1);
+		ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0);
+		ATF_REQUIRE(msgds.msg_perm.mode == mode[i]);
+		ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
+	}
+}
+
+ATF_TC_CLEANUP(msgget_mode, tc)
+{
+	clean();
+}
+
+
+ATF_TP_ADD_TCS(tp)
+{
+
+	ATF_TP_ADD_TC(tp, msgget_excl);
+	ATF_TP_ADD_TC(tp, msgget_exit);
+	ATF_TP_ADD_TC(tp, msgget_init);
+	ATF_TP_ADD_TC(tp, msgget_limit);
+	ATF_TP_ADD_TC(tp, msgget_mode);
+
+	return atf_no_error();
+}
Index: src/tests/lib/libc/sys/t_msgrcv.c
diff -u /dev/null src/tests/lib/libc/sys/t_msgrcv.c:1.1
--- /dev/null	Sat Nov  5 07:45:41 2011
+++ src/tests/lib/libc/sys/t_msgrcv.c	Sat Nov  5 07:45:41 2011
@@ -0,0 +1,341 @@
+/* $NetBSD: t_msgrcv.c,v 1.1 2011/11/05 07:45:41 jruoho Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jukka Ruohonen.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: t_msgrcv.c,v 1.1 2011/11/05 07:45:41 jruoho Exp $");
+
+#include <sys/msg.h>
+#include <sys/stat.h>
+#include <sys/sysctl.h>
+#include <sys/wait.h>
+
+#include <atf-c.h>
+#include <errno.h>
+#include <pwd.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sysexits.h>
+#include <time.h>
+#include <unistd.h>
+
+#define MSG_KEY		1234
+#define MSG_MTYPE_1	0x41
+#define	MSG_MTYPE_2	0x42
+#define MSG_MTYPE_3	0x43
+
+struct msg {
+	long		 mtype;
+	char		 buf[3];
+};
+
+static void		clean(void);
+
+static void
+clean(void)
+{
+	int id;
+
+	if ((id = msgget(MSG_KEY, 0)) != -1)
+		(void)msgctl(id, IPC_RMID, 0);
+}
+
+ATF_TC_WITH_CLEANUP(msgrcv_basic);
+ATF_TC_HEAD(msgrcv_basic, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "A basic test of msgrcv(2)");
+}
+
+ATF_TC_BODY(msgrcv_basic, tc)
+{
+	struct msg msg1 = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
+	struct msg msg2 = { MSG_MTYPE_1, { 'x', 'y', 'z' } };
+	int id;
+
+	id = msgget(MSG_KEY, IPC_CREAT | 0600);
+	ATF_REQUIRE(id != -1);
+
+	(void)msgsnd(id, &msg1, sizeof(struct msg), IPC_NOWAIT);
+	(void)msgrcv(id, &msg2, sizeof(struct msg), MSG_MTYPE_1, IPC_NOWAIT);
+
+	ATF_CHECK(msg1.buf[0] == msg2.buf[0]);
+	ATF_CHECK(msg1.buf[1] == msg2.buf[1]);
+	ATF_CHECK(msg1.buf[2] == msg2.buf[2]);
+
+	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
+}
+
+ATF_TC_CLEANUP(msgrcv_basic, tc)
+{
+	clean();
+}
+
+ATF_TC_WITH_CLEANUP(msgrcv_block);
+ATF_TC_HEAD(msgrcv_block, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test that msgrcv(2) blocks");
+}
+
+ATF_TC_BODY(msgrcv_block, tc)
+{
+	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
+	int id, sta;
+	pid_t pid;
+
+	id = msgget(MSG_KEY, IPC_CREAT | 0600);
+	ATF_REQUIRE(id != -1);
+
+	pid = fork();
+	ATF_REQUIRE(pid >= 0);
+
+	if (pid == 0) {
+
+		if (msgrcv(id, &msg, sizeof(struct msg), MSG_MTYPE_1, 0) < 0)
+			_exit(EXIT_FAILURE);
+
+		_exit(EXIT_SUCCESS);
+	}
+
+	/*
+	 * Below msgsnd(2) should unblock the child,
+	 * and hence kill(2) should fail with ESRCH.
+	  */
+	(void)sleep(1);
+	(void)msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT);
+	(void)sleep(1);
+	(void)kill(pid, SIGKILL);
+	(void)wait(&sta);
+
+	if (WIFEXITED(sta) == 0 || WIFSIGNALED(sta) != 0)
+		atf_tc_fail("msgrcv(2) did not block");
+
+	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
+}
+
+ATF_TC_CLEANUP(msgrcv_block, tc)
+{
+	clean();
+}
+
+ATF_TC_WITH_CLEANUP(msgrcv_err);
+ATF_TC_HEAD(msgrcv_err, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test errors from msgrcv(2)");
+}
+
+ATF_TC_BODY(msgrcv_err, tc)
+{
+	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
+	int id, r = 0;
+
+	id = msgget(MSG_KEY, IPC_CREAT | 0600);
+	ATF_REQUIRE(id != -1);
+
+	errno = 0;
+
+	ATF_REQUIRE_ERRNO(ENOMSG, msgrcv(id, &msg,
+		sizeof(struct msg), MSG_MTYPE_1, IPC_NOWAIT) == -1);
+
+	ATF_REQUIRE(msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT) == 0);
+
+	errno = 0;
+
+	ATF_REQUIRE_ERRNO(EFAULT, msgrcv(id, (void *)-1,
+		sizeof(struct msg), MSG_MTYPE_1, IPC_NOWAIT) == -1);
+
+	errno = 0;
+
+	ATF_REQUIRE_ERRNO(EINVAL, msgrcv(-1, &msg,
+		sizeof(struct msg), MSG_MTYPE_1, IPC_NOWAIT) == -1);
+
+	errno = 0;
+
+	ATF_REQUIRE_ERRNO(EINVAL, msgrcv(-1, &msg,
+		SSIZE_MAX, MSG_MTYPE_1, IPC_NOWAIT) == -1);
+
+	ATF_REQUIRE(msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT) == 0);
+
+	errno = 0;
+
+	ATF_REQUIRE_ERRNO(E2BIG, msgrcv(id, &r,
+		sizeof(int), MSG_MTYPE_1, IPC_NOWAIT) == -1);
+
+	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
+}
+
+ATF_TC_CLEANUP(msgrcv_err, tc)
+{
+	clean();
+}
+
+
+ATF_TC_WITH_CLEANUP(msgrcv_mtype);
+ATF_TC_HEAD(msgrcv_mtype, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test message types with msgrcv(2)");
+}
+
+ATF_TC_BODY(msgrcv_mtype, tc)
+{
+	struct msg msg1 = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
+	struct msg msg2 = { MSG_MTYPE_3, { 'x', 'y', 'z' } };
+	int id;
+
+	id = msgget(MSG_KEY, IPC_CREAT | 0600);
+	ATF_REQUIRE(id != -1);
+
+	(void)msgsnd(id, &msg1, sizeof(struct msg), IPC_NOWAIT);
+	(void)msgrcv(id, &msg2, sizeof(struct msg), MSG_MTYPE_2, IPC_NOWAIT);
+
+	ATF_CHECK(msg1.buf[0] != msg2.buf[0]);	/* Different mtype. */
+	ATF_CHECK(msg1.buf[1] != msg2.buf[1]);
+	ATF_CHECK(msg1.buf[2] != msg2.buf[2]);
+
+	(void)msgrcv(id, &msg2, sizeof(struct msg), MSG_MTYPE_1, IPC_NOWAIT);
+
+	ATF_CHECK(msg1.buf[0] == msg2.buf[0]);	/* Same mtype. */
+	ATF_CHECK(msg1.buf[1] == msg2.buf[1]);
+	ATF_CHECK(msg1.buf[2] == msg2.buf[2]);
+
+	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
+}
+
+ATF_TC_CLEANUP(msgrcv_mtype, tc)
+{
+	clean();
+}
+
+ATF_TC_WITH_CLEANUP(msgrcv_nonblock);
+ATF_TC_HEAD(msgrcv_nonblock, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test msgrcv(2) with IPC_NOWAIT");
+	atf_tc_set_md_var(tc, "timeout", "10");
+}
+
+ATF_TC_BODY(msgrcv_nonblock, tc)
+{
+	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
+	const ssize_t n = 10;
+	int id, sta;
+	ssize_t i;
+	pid_t pid;
+
+	id = msgget(MSG_KEY, IPC_CREAT | 0600);
+	ATF_REQUIRE(id != -1);
+
+	for (i = 0; i < n; i++) {
+
+		ATF_REQUIRE(msgsnd(id, &msg,
+			sizeof(struct msg), IPC_NOWAIT) == 0);
+	}
+
+	pid = fork();
+	ATF_REQUIRE(pid >= 0);
+
+	if (pid == 0) {
+
+		while (i != 0) {
+
+			if (msgrcv(id, &msg, sizeof(struct msg),
+				MSG_MTYPE_1, IPC_NOWAIT) == -1)
+				_exit(EXIT_FAILURE);
+
+			i--;
+		}
+
+		_exit(EXIT_SUCCESS);
+	}
+
+	(void)sleep(2);
+	(void)kill(pid, SIGKILL);
+	(void)wait(&sta);
+
+	if (WIFSIGNALED(sta) != 0 || WTERMSIG(sta) == SIGKILL)
+		atf_tc_fail("msgrcv(2) blocked with IPC_NOWAIT");
+
+	if (WIFEXITED(sta) == 0 && WEXITSTATUS(sta) != EXIT_SUCCESS)
+		atf_tc_fail("msgrcv(2) failed");
+
+	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
+}
+
+ATF_TC_CLEANUP(msgrcv_nonblock, tc)
+{
+	clean();
+}
+
+ATF_TC_WITH_CLEANUP(msgrcv_truncate);
+ATF_TC_HEAD(msgrcv_truncate, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test msgrcv(2) with MSG_NOERROR");
+}
+
+ATF_TC_BODY(msgrcv_truncate, tc)
+{
+	struct msgsmall {
+		long		 mtype;
+		char		 buf[2];
+	};
+
+	struct msg msg1 = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
+	struct msgsmall msg2 = { MSG_MTYPE_1, { 'x', 'y' } };
+	int id;
+
+	id = msgget(MSG_KEY, IPC_CREAT | 0600);
+	ATF_REQUIRE(id != -1);
+
+	(void)msgsnd(id, &msg1, sizeof(struct msg), IPC_NOWAIT);
+	(void)msgrcv(id, &msg2, sizeof(struct msgsmall),
+	    MSG_MTYPE_1, IPC_NOWAIT | MSG_NOERROR);
+
+	ATF_CHECK(msg1.buf[0] == msg2.buf[0]);
+	ATF_CHECK(msg1.buf[1] == msg2.buf[1]);
+
+	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
+}
+
+ATF_TC_CLEANUP(msgrcv_truncate, tc)
+{
+	clean();
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+	ATF_TP_ADD_TC(tp, msgrcv_basic);
+	ATF_TP_ADD_TC(tp, msgrcv_block);
+	ATF_TP_ADD_TC(tp, msgrcv_err);
+	ATF_TP_ADD_TC(tp, msgrcv_mtype);
+	ATF_TP_ADD_TC(tp, msgrcv_nonblock);
+	ATF_TP_ADD_TC(tp, msgrcv_truncate);
+
+	return atf_no_error();
+}
Index: src/tests/lib/libc/sys/t_msgsnd.c
diff -u /dev/null src/tests/lib/libc/sys/t_msgsnd.c:1.1
--- /dev/null	Sat Nov  5 07:45:41 2011
+++ src/tests/lib/libc/sys/t_msgsnd.c	Sat Nov  5 07:45:41 2011
@@ -0,0 +1,338 @@
+/* $NetBSD: t_msgsnd.c,v 1.1 2011/11/05 07:45:41 jruoho Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jukka Ruohonen.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: t_msgsnd.c,v 1.1 2011/11/05 07:45:41 jruoho Exp $");
+
+#include <sys/msg.h>
+#include <sys/stat.h>
+#include <sys/sysctl.h>
+#include <sys/wait.h>
+
+#include <atf-c.h>
+#include <errno.h>
+#include <pwd.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sysexits.h>
+#include <time.h>
+#include <unistd.h>
+
+#define MSG_KEY		1234
+#define MSG_MTYPE_1	0x41
+#define	MSG_MTYPE_2	0x42
+#define MSG_MTYPE_3	0x43
+
+struct msg {
+	long		 mtype;
+	char		 buf[3];
+};
+
+static void		clean(void);
+
+static void
+clean(void)
+{
+	int id;
+
+	if ((id = msgget(MSG_KEY, 0)) != -1)
+		(void)msgctl(id, IPC_RMID, 0);
+}
+
+ATF_TC_WITH_CLEANUP(msgsnd_block);
+ATF_TC_HEAD(msgsnd_block, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test msgsnd(2) blocks");
+	atf_tc_set_md_var(tc, "timeout", "10");
+}
+
+ATF_TC_BODY(msgsnd_block, tc)
+{
+	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
+	int id, sta;
+	pid_t pid;
+
+	id = msgget(MSG_KEY, IPC_CREAT | 0600);
+	ATF_REQUIRE(id != -1);
+
+	pid = fork();
+	ATF_REQUIRE(pid >= 0);
+
+	if (pid == 0) {
+
+		/*
+		 * Enqueue messages until some limit (e.g. the maximum
+		 * number of messages in the queue or the maximum number
+		 * of bytes in the queue) is reached. After this the call
+		 * should block when the IPC_NOWAIT is not set.
+		 */
+		for (;;) {
+
+			if (msgsnd(id, &msg, sizeof(struct msg), 0) < 0)
+				_exit(EXIT_FAILURE);
+		}
+	}
+
+	(void)sleep(2);
+	(void)kill(pid, SIGKILL);
+	(void)wait(&sta);
+
+	if (WIFEXITED(sta) != 0 || WIFSIGNALED(sta) == 0)
+		atf_tc_fail("msgsnd(2) did not block");
+
+	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
+}
+
+ATF_TC_CLEANUP(msgsnd_block, tc)
+{
+	clean();
+}
+
+ATF_TC_WITH_CLEANUP(msgsnd_count);
+ATF_TC_HEAD(msgsnd_count, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "Test that msgsnd(2) increments the amount of "
+	    "message in the queue, as given by msgctl(2)");
+	atf_tc_set_md_var(tc, "timeout", "10");
+}
+
+ATF_TC_BODY(msgsnd_count, tc)
+{
+	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
+	struct msqid_ds ds;
+	size_t i = 0;
+	int id, rv;
+
+	id = msgget(MSG_KEY, IPC_CREAT | 0600);
+	ATF_REQUIRE(id != -1);
+
+	for (;;) {
+
+		errno = 0;
+		rv = msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT);
+
+		if (rv == 0) {
+			i++;
+			continue;
+		}
+
+		if (rv == -1 && errno == EAGAIN)
+			break;
+
+		atf_tc_fail("failed to enqueue a message");
+	}
+
+	(void)memset(&ds, 0, sizeof(struct msqid_ds));
+	(void)msgctl(id, IPC_STAT, &ds);
+
+	if (ds.msg_qnum != i)
+		atf_tc_fail("incorrect message count");
+
+	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
+}
+
+ATF_TC_CLEANUP(msgsnd_count, tc)
+{
+	clean();
+}
+
+ATF_TC_WITH_CLEANUP(msgsnd_err);
+ATF_TC_HEAD(msgsnd_err, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test errors from msgsnd(2)");
+}
+
+ATF_TC_BODY(msgsnd_err, tc)
+{
+	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
+	int id;
+
+	id = msgget(MSG_KEY, IPC_CREAT | 0600);
+	ATF_REQUIRE(id != -1);
+
+	errno = 0;
+
+	ATF_REQUIRE_ERRNO(EFAULT, msgsnd(id, (void *)-1,
+		sizeof(struct msg), IPC_NOWAIT) == -1);
+
+	errno = 0;
+
+	ATF_REQUIRE_ERRNO(EINVAL, msgsnd(-1, &msg,
+		sizeof(struct msg), IPC_NOWAIT) == -1);
+
+	errno = 0;
+
+	ATF_REQUIRE_ERRNO(EINVAL, msgsnd(-1, &msg,
+		SSIZE_MAX, IPC_NOWAIT) == -1);
+
+	errno = 0;
+	msg.mtype = 0;
+
+	ATF_REQUIRE_ERRNO(EINVAL, msgsnd(id, &msg,
+		sizeof(struct msg), IPC_NOWAIT) == -1);
+
+	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
+}
+
+ATF_TC_CLEANUP(msgsnd_err, tc)
+{
+	clean();
+}
+
+ATF_TC_WITH_CLEANUP(msgsnd_nonblock);
+ATF_TC_HEAD(msgsnd_nonblock, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test msgsnd(2) with IPC_NOWAIT");
+	atf_tc_set_md_var(tc, "timeout", "10");
+}
+
+ATF_TC_BODY(msgsnd_nonblock, tc)
+{
+	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
+	int id, rv, sta;
+	pid_t pid;
+
+	id = msgget(MSG_KEY, IPC_CREAT | 0600);
+	ATF_REQUIRE(id != -1);
+
+	pid = fork();
+	ATF_REQUIRE(pid >= 0);
+
+	if (pid == 0) {
+
+		for (;;) {
+
+			errno = 0;
+			rv = msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT);
+
+			if (rv == -1 && errno == EAGAIN)
+				_exit(EXIT_SUCCESS);
+		}
+	}
+
+	(void)sleep(2);
+	(void)kill(pid, SIGKILL);
+	(void)wait(&sta);
+
+	if (WIFEXITED(sta) == 0 || WIFSIGNALED(sta) != 0)
+		atf_tc_fail("msgsnd(2) blocked with IPC_NOWAIT");
+
+	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
+}
+
+ATF_TC_CLEANUP(msgsnd_nonblock, tc)
+{
+	clean();
+}
+
+ATF_TC_WITH_CLEANUP(msgsnd_perm);
+ATF_TC_HEAD(msgsnd_perm, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test permissions with msgsnd(2)");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(msgsnd_perm, tc)
+{
+	struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
+	struct passwd *pw;
+	int id, sta;
+	pid_t pid;
+	uid_t uid;
+
+	pw = getpwnam("nobody");
+	id = msgget(MSG_KEY, IPC_CREAT | 0600);
+
+	ATF_REQUIRE(id != -1);
+	ATF_REQUIRE(pw != NULL);
+
+	uid = pw->pw_uid;
+	ATF_REQUIRE(uid != 0);
+
+	pid = fork();
+	ATF_REQUIRE(pid >= 0);
+
+	if (pid == 0) {
+
+		/*
+		 * Try to enqueue a message to the queue
+		 * created by root as RW for owner only.
+		 */
+		if (setuid(uid) != 0)
+			_exit(EX_OSERR);
+
+		id = msgget(MSG_KEY, 0);
+
+		if (id == -1)
+			_exit(EX_OSERR);
+
+		errno = 0;
+
+		if (msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT) == 0)
+			_exit(EXIT_FAILURE);
+
+		if (errno != EACCES)
+			_exit(EXIT_FAILURE);
+
+		_exit(EXIT_SUCCESS);
+	}
+
+	(void)wait(&sta);
+
+	if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) {
+
+		if (errno == EX_OSERR)
+			atf_tc_fail("system call failed");
+
+		atf_tc_fail("UID %u enqueued message to root's queue", uid);
+	}
+
+	ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
+}
+
+ATF_TC_CLEANUP(msgsnd_perm, tc)
+{
+	clean();
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+	ATF_TP_ADD_TC(tp, msgsnd_block);
+	ATF_TP_ADD_TC(tp, msgsnd_count);
+	ATF_TP_ADD_TC(tp, msgsnd_err);
+	ATF_TP_ADD_TC(tp, msgsnd_nonblock);
+	ATF_TP_ADD_TC(tp, msgsnd_perm);
+
+	return atf_no_error();
+}

Reply via email to