diff --git a/contrib/netbsd-tests/lib/libc/sys/t_msgctl.c b/contrib/netbsd-tests/lib/libc/sys/t_msgctl.c index 4c145cc89047..098ee91373bf 100644 --- a/contrib/netbsd-tests/lib/libc/sys/t_msgctl.c +++ b/contrib/netbsd-tests/lib/libc/sys/t_msgctl.c @@ -1,367 +1,365 @@ /* $NetBSD: t_msgctl.c,v 1.5 2017/01/13 20:44:45 christos 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 __RCSID("$NetBSD: t_msgctl.c,v 1.5 2017/01/13 20:44:45 christos Exp $"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #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 permissions 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) { #ifdef __FreeBSD__ (void)msgsnd(id, &msg, sizeof(msg.buf), IPC_NOWAIT); #else (void)msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT); #endif _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 that access times are updated"); } 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)); #ifdef __FreeBSD__ (void)msgsnd(id, &msg, sizeof(msg.buf), IPC_NOWAIT); #else (void)msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT); #endif (void)msgctl(id, IPC_STAT, &msgds); if (llabs(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 (llabs(t - msgds.msg_rtime) > 1) atf_tc_fail("time of last msgrcv(2) was not updated"); /* * Note: 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(); } diff --git a/contrib/netbsd-tests/lib/libc/sys/t_msgrcv.c b/contrib/netbsd-tests/lib/libc/sys/t_msgrcv.c index 522ceb82cd38..1bbf6e855acc 100644 --- a/contrib/netbsd-tests/lib/libc/sys/t_msgrcv.c +++ b/contrib/netbsd-tests/lib/libc/sys/t_msgrcv.c @@ -1,343 +1,341 @@ /* $NetBSD: t_msgrcv.c,v 1.4 2017/01/13 20:44:45 christos 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 __RCSID("$NetBSD: t_msgrcv.c,v 1.4 2017/01/13 20:44:45 christos Exp $"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define MSG_KEY 1234 #define MSG_MTYPE_1 0x41 #define MSG_MTYPE_2 0x42 #define MSG_MTYPE_3 0x43 #define MSG_LEN 3 struct msg { long mtype; char buf[MSG_LEN]; }; 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, MSG_LEN, IPC_NOWAIT); (void)msgrcv(id, &msg2, MSG_LEN, 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, MSG_LEN, 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, MSG_LEN, 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, MSG_LEN, MSG_MTYPE_1, IPC_NOWAIT) == -1); ATF_REQUIRE(msgsnd(id, &msg, MSG_LEN, IPC_NOWAIT) == 0); errno = 0; ATF_REQUIRE_ERRNO(EFAULT, msgrcv(id, (void *)-1, MSG_LEN, MSG_MTYPE_1, IPC_NOWAIT) == -1); errno = 0; ATF_REQUIRE_ERRNO(EINVAL, msgrcv(-1, &msg, MSG_LEN, 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, MSG_LEN, IPC_NOWAIT) == 0); errno = 0; ATF_REQUIRE_ERRNO(E2BIG, msgrcv(id, &r, MSG_LEN - 1, 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, MSG_LEN, IPC_NOWAIT); (void)msgrcv(id, &msg2, MSG_LEN, 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, MSG_LEN, 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, MSG_LEN, IPC_NOWAIT) == 0); } pid = fork(); ATF_REQUIRE(pid >= 0); if (pid == 0) { while (i != 0) { if (msgrcv(id, &msg, MSG_LEN, 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) { #define MSG_SMALLLEN 2 struct msgsmall { long mtype; char buf[MSG_SMALLLEN]; }; 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, MSG_LEN, IPC_NOWAIT); (void)msgrcv(id, &msg2, MSG_SMALLLEN, 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(); } diff --git a/contrib/netbsd-tests/lib/libc/sys/t_msgsnd.c b/contrib/netbsd-tests/lib/libc/sys/t_msgsnd.c index 562602a28a79..0251b42ff3d4 100644 --- a/contrib/netbsd-tests/lib/libc/sys/t_msgsnd.c +++ b/contrib/netbsd-tests/lib/libc/sys/t_msgsnd.c @@ -1,367 +1,365 @@ /* $NetBSD: t_msgsnd.c,v 1.3 2017/01/13 20:44:45 christos 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 __RCSID("$NetBSD: t_msgsnd.c,v 1.3 2017/01/13 20:44:45 christos Exp $"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #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 that 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 (;;) { #ifdef __FreeBSD__ if (msgsnd(id, &msg, sizeof(msg.buf), 0) < 0) #else if (msgsnd(id, &msg, sizeof(struct msg), 0) < 0) #endif _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; #ifdef __FreeBSD__ rv = msgsnd(id, &msg, sizeof(msg.buf), IPC_NOWAIT); #else rv = msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT); #endif 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, #ifdef __FreeBSD__ sizeof(msg.buf), IPC_NOWAIT) == -1); #else sizeof(struct msg), IPC_NOWAIT) == -1); #endif errno = 0; ATF_REQUIRE_ERRNO(EINVAL, msgsnd(-1, &msg, #ifdef __FreeBSD__ sizeof(msg.buf), IPC_NOWAIT) == -1); #else sizeof(struct msg), IPC_NOWAIT) == -1); #endif 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, #ifdef __FreeBSD__ sizeof(msg.buf), IPC_NOWAIT) == -1); #else sizeof(struct msg), IPC_NOWAIT) == -1); #endif 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; #ifdef __FreeBSD__ rv = msgsnd(id, &msg, sizeof(msg.buf), IPC_NOWAIT); #else rv = msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT); #endif 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; #ifdef __FreeBSD__ if (msgsnd(id, &msg, sizeof(msg.buf), IPC_NOWAIT) == 0) #else if (msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT) == 0) #endif _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(); }