aboutsummaryrefslogtreecommitdiff
path: root/contrib/netbsd-tests/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/netbsd-tests/kernel')
-rw-r--r--contrib/netbsd-tests/kernel/arch/amd64/t_ptrace_wait.c1606
-rw-r--r--contrib/netbsd-tests/kernel/arch/amd64/t_ptrace_wait3.c30
-rw-r--r--contrib/netbsd-tests/kernel/arch/amd64/t_ptrace_wait4.c30
-rw-r--r--contrib/netbsd-tests/kernel/arch/amd64/t_ptrace_wait6.c30
-rw-r--r--contrib/netbsd-tests/kernel/arch/amd64/t_ptrace_waitid.c30
-rw-r--r--contrib/netbsd-tests/kernel/arch/amd64/t_ptrace_waitpid.c30
-rw-r--r--contrib/netbsd-tests/kernel/arch/i386/t_ptrace_wait.c141
-rw-r--r--contrib/netbsd-tests/kernel/arch/i386/t_ptrace_wait3.c30
-rw-r--r--contrib/netbsd-tests/kernel/arch/i386/t_ptrace_wait4.c30
-rw-r--r--contrib/netbsd-tests/kernel/arch/i386/t_ptrace_wait6.c30
-rw-r--r--contrib/netbsd-tests/kernel/arch/i386/t_ptrace_waitid.c30
-rw-r--r--contrib/netbsd-tests/kernel/arch/i386/t_ptrace_waitpid.c30
-rwxr-xr-xcontrib/netbsd-tests/kernel/gen_t_subr_prf134
-rw-r--r--contrib/netbsd-tests/kernel/h_ps_strings1.c74
-rw-r--r--contrib/netbsd-tests/kernel/h_ps_strings2.c69
-rw-r--r--contrib/netbsd-tests/kernel/kqueue/read/t_fifo.c102
-rw-r--r--contrib/netbsd-tests/kernel/kqueue/read/t_file.c143
-rw-r--r--contrib/netbsd-tests/kernel/kqueue/read/t_file2.c79
-rw-r--r--contrib/netbsd-tests/kernel/kqueue/read/t_pipe.c88
-rw-r--r--contrib/netbsd-tests/kernel/kqueue/read/t_ttypty.c148
-rw-r--r--contrib/netbsd-tests/kernel/kqueue/t_ioctl.c116
-rw-r--r--contrib/netbsd-tests/kernel/kqueue/t_proc1.c155
-rw-r--r--contrib/netbsd-tests/kernel/kqueue/t_proc2.c139
-rw-r--r--contrib/netbsd-tests/kernel/kqueue/t_proc3.c99
-rw-r--r--contrib/netbsd-tests/kernel/kqueue/t_sig.c143
-rw-r--r--contrib/netbsd-tests/kernel/kqueue/t_vnode.c533
-rw-r--r--contrib/netbsd-tests/kernel/kqueue/write/t_fifo.c102
-rw-r--r--contrib/netbsd-tests/kernel/kqueue/write/t_pipe.c147
-rw-r--r--contrib/netbsd-tests/kernel/kqueue/write/t_ttypty.c130
-rw-r--r--contrib/netbsd-tests/kernel/msg.h136
-rw-r--r--contrib/netbsd-tests/kernel/t_extattrctl.c28
-rw-r--r--contrib/netbsd-tests/kernel/t_extent.c385
-rw-r--r--contrib/netbsd-tests/kernel/t_filedesc.c108
-rw-r--r--contrib/netbsd-tests/kernel/t_kauth_pr_47598.c169
-rw-r--r--contrib/netbsd-tests/kernel/t_lock.c87
-rw-r--r--contrib/netbsd-tests/kernel/t_lockf.c265
-rw-r--r--contrib/netbsd-tests/kernel/t_mqueue.c154
-rwxr-xr-xcontrib/netbsd-tests/kernel/t_ps_strings.sh85
-rw-r--r--contrib/netbsd-tests/kernel/t_ptrace.c208
-rw-r--r--contrib/netbsd-tests/kernel/t_ptrace_wait.c6700
-rw-r--r--contrib/netbsd-tests/kernel/t_ptrace_wait.h431
-rw-r--r--contrib/netbsd-tests/kernel/t_ptrace_wait3.c30
-rw-r--r--contrib/netbsd-tests/kernel/t_ptrace_wait4.c30
-rw-r--r--contrib/netbsd-tests/kernel/t_ptrace_wait6.c30
-rw-r--r--contrib/netbsd-tests/kernel/t_ptrace_waitid.c30
-rw-r--r--contrib/netbsd-tests/kernel/t_ptrace_waitpid.c30
-rw-r--r--contrib/netbsd-tests/kernel/t_pty.c351
-rw-r--r--contrib/netbsd-tests/kernel/t_rnd.c114
-rw-r--r--contrib/netbsd-tests/kernel/t_sysctl.c74
-rw-r--r--contrib/netbsd-tests/kernel/t_sysv.c819
-rwxr-xr-xcontrib/netbsd-tests/kernel/t_umount.sh95
-rwxr-xr-xcontrib/netbsd-tests/kernel/t_umountstress.sh209
-rw-r--r--contrib/netbsd-tests/kernel/tty/t_pr.c186
53 files changed, 15202 insertions, 0 deletions
diff --git a/contrib/netbsd-tests/kernel/arch/amd64/t_ptrace_wait.c b/contrib/netbsd-tests/kernel/arch/amd64/t_ptrace_wait.c
new file mode 100644
index 000000000000..09c17e125291
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/arch/amd64/t_ptrace_wait.c
@@ -0,0 +1,1606 @@
+/* $NetBSD: t_ptrace_wait.c,v 1.11 2017/01/18 05:14:34 kamil Exp $ */
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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_ptrace_wait.c,v 1.11 2017/01/18 05:14:34 kamil Exp $");
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/ptrace.h>
+#include <sys/resource.h>
+#include <sys/stat.h>
+#include <sys/sysctl.h>
+#include <sys/wait.h>
+#include <machine/reg.h>
+#include <x86/dbregs.h>
+#include <err.h>
+#include <errno.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+#include "h_macros.h"
+
+#include "../../t_ptrace_wait.h"
+
+
+#if defined(HAVE_GPREGS)
+ATF_TC(regs1);
+ATF_TC_HEAD(regs1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Call PT_GETREGS and iterate over General Purpose registers");
+}
+
+ATF_TC_BODY(regs1, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ struct reg r;
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Call GETREGS for the child process\n");
+ ATF_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
+
+ printf("RAX=%#" PRIxREGISTER "\n", r.regs[_REG_RAX]);
+ printf("RBX=%#" PRIxREGISTER "\n", r.regs[_REG_RBX]);
+ printf("RCX=%#" PRIxREGISTER "\n", r.regs[_REG_RCX]);
+ printf("RDX=%#" PRIxREGISTER "\n", r.regs[_REG_RDX]);
+
+ printf("RDI=%#" PRIxREGISTER "\n", r.regs[_REG_RDI]);
+ printf("RSI=%#" PRIxREGISTER "\n", r.regs[_REG_RSI]);
+
+ printf("GS=%#" PRIxREGISTER "\n", r.regs[_REG_GS]);
+ printf("FS=%#" PRIxREGISTER "\n", r.regs[_REG_FS]);
+ printf("ES=%#" PRIxREGISTER "\n", r.regs[_REG_ES]);
+ printf("DS=%#" PRIxREGISTER "\n", r.regs[_REG_DS]);
+ printf("CS=%#" PRIxREGISTER "\n", r.regs[_REG_CS]);
+ printf("SS=%#" PRIxREGISTER "\n", r.regs[_REG_SS]);
+
+ printf("RSP=%#" PRIxREGISTER "\n", r.regs[_REG_RSP]);
+ printf("RIP=%#" PRIxREGISTER "\n", r.regs[_REG_RIP]);
+
+ printf("RFLAGS=%#" PRIxREGISTER "\n", r.regs[_REG_RFLAGS]);
+
+ printf("R8=%#" PRIxREGISTER "\n", r.regs[_REG_R8]);
+ printf("R9=%#" PRIxREGISTER "\n", r.regs[_REG_R9]);
+ printf("R10=%#" PRIxREGISTER "\n", r.regs[_REG_R10]);
+ printf("R11=%#" PRIxREGISTER "\n", r.regs[_REG_R11]);
+ printf("R12=%#" PRIxREGISTER "\n", r.regs[_REG_R12]);
+ printf("R13=%#" PRIxREGISTER "\n", r.regs[_REG_R13]);
+ printf("R14=%#" PRIxREGISTER "\n", r.regs[_REG_R14]);
+ printf("R15=%#" PRIxREGISTER "\n", r.regs[_REG_R15]);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(__HAVE_PTRACE_WATCHPOINTS)
+ATF_TC(watchpoint_count);
+ATF_TC_HEAD(watchpoint_count, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Call PT_COUNT_WATCHPOINTS and assert four available watchpoints");
+}
+
+ATF_TC_BODY(watchpoint_count, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ int N;
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Call GETREGS for the child process\n");
+ ATF_REQUIRE((N = ptrace(PT_COUNT_WATCHPOINTS, child, NULL, 0)) != -1);
+ printf("Reported %d watchpoints\n", N);
+
+ ATF_REQUIRE_EQ_MSG(N, 4, "Expected 4 hw watchpoints - got %d", N);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(__HAVE_PTRACE_WATCHPOINTS)
+ATF_TC(watchpoint_read);
+ATF_TC_HEAD(watchpoint_read, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Call PT_COUNT_WATCHPOINTS and assert four available watchpoints");
+}
+
+ATF_TC_BODY(watchpoint_read, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ int i, N;
+ struct ptrace_watchpoint pw;
+ int len = sizeof(pw);
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Call GETREGS for the child process\n");
+ ATF_REQUIRE((N = ptrace(PT_COUNT_WATCHPOINTS, child, NULL, 0)) != -1);
+
+ ATF_REQUIRE_EQ_MSG(N, 4, "Expected 4 hw watchpoints - got %d", N);
+
+ for (i = 0; i < N; i++) {
+ printf("Before reading watchpoint %d\n", i);
+ pw.pw_index = i;
+ ATF_REQUIRE(ptrace(PT_READ_WATCHPOINT, child, &pw, len) != -1);
+
+ printf("struct ptrace {\n");
+ printf("\t.pw_index=%d\n", pw.pw_index);
+ printf("\t.pw_lwpid=%d\n", pw.pw_lwpid);
+ printf("\t.pw_type=%#x\n", pw.pw_type);
+ printf("\t.pw_md.md_address=%p\n", pw.pw_md.md_address);
+ printf("\t.pw_md.md_condition=%#x\n", pw.pw_md.md_condition);
+ printf("\t.pw_md.md_length=%#x\n", pw.pw_md.md_length);
+ printf("}\n");
+ }
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(__HAVE_PTRACE_WATCHPOINTS)
+ATF_TC(watchpoint_write_unmodified);
+ATF_TC_HEAD(watchpoint_write_unmodified, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Call PT_COUNT_WATCHPOINTS and assert functional write of "
+ "unmodified data");
+}
+
+ATF_TC_BODY(watchpoint_write_unmodified, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ int i, N;
+ struct ptrace_watchpoint pw;
+ int len = sizeof(pw);
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Call GETREGS for the child process\n");
+ ATF_REQUIRE((N = ptrace(PT_COUNT_WATCHPOINTS, child, NULL, 0)) != -1);
+
+ ATF_REQUIRE_EQ_MSG(N, 4, "Expected 4 hw watchpoints - got %d", N);
+
+ for (i = 0; i < N; i++) {
+ printf("Before reading watchpoint %d\n", i);
+ pw.pw_index = i;
+ ATF_REQUIRE(ptrace(PT_READ_WATCHPOINT, child, &pw, len) != -1);
+
+ printf("struct ptrace {\n");
+ printf("\t.pw_index=%d\n", pw.pw_index);
+ printf("\t.pw_lwpid=%d\n", pw.pw_lwpid);
+ printf("\t.pw_type=%#x\n", pw.pw_type);
+ printf("\t.pw_md.md_address=%p\n", pw.pw_md.md_address);
+ printf("\t.pw_md.md_condition=%#x\n", pw.pw_md.md_condition);
+ printf("\t.pw_md.md_length=%#x\n", pw.pw_md.md_length);
+ printf("}\n");
+
+ printf("Before writing watchpoint %d (unmodified)\n", i);
+ ATF_REQUIRE(ptrace(PT_WRITE_WATCHPOINT, child, &pw, len)
+ != -1);
+ }
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(__HAVE_PTRACE_WATCHPOINTS)
+ATF_TC(watchpoint_trap_code0);
+ATF_TC_HEAD(watchpoint_trap_code0, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Call PT_COUNT_WATCHPOINTS and test code trap with watchpoint 0");
+}
+
+ATF_TC_BODY(watchpoint_trap_code0, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ const int i = 0;
+ struct ptrace_watchpoint pw;
+ int len = sizeof(pw);
+ int watchme = 1234;
+ struct ptrace_siginfo info;
+ memset(&info, 0, sizeof(info));
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("check_happy(%d)=%d\n", watchme, check_happy(watchme));
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Preparing code watchpoint trap %d\n", i);
+
+ pw.pw_index = i;
+ pw.pw_lwpid = 0;
+ pw.pw_type = PTRACE_PW_TYPE_DBREGS;
+ pw.pw_md.md_address = (void *)check_happy;
+ pw.pw_md.md_condition = X86_HW_WATCHPOINT_DR7_CONDITION_EXECUTION;
+ pw.pw_md.md_length = X86_HW_WATCHPOINT_DR7_LENGTH_BYTE;
+
+ printf("struct ptrace {\n");
+ printf("\t.pw_index=%d\n", pw.pw_index);
+ printf("\t.pw_lwpid=%d\n", pw.pw_lwpid);
+ printf("\t.pw_type=%#x\n", pw.pw_type);
+ printf("\t.pw_md.md_address=%p\n", pw.pw_md.md_address);
+ printf("\t.pw_md.md_condition=%#x\n", pw.pw_md.md_condition);
+ printf("\t.pw_md.md_length=%#x\n", pw.pw_md.md_length);
+ printf("}\n");
+
+ printf("Before writing watchpoint %d\n", i);
+ ATF_REQUIRE(ptrace(PT_WRITE_WATCHPOINT, child, &pw, len) != -1);
+
+ printf("Before resuming the child process where it left off "
+ "and without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Signal traced to lwpid=%d\n", info.psi_lwpid);
+ printf("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
+ info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
+ info.psi_siginfo.si_errno);
+
+ printf("Before checking siginfo_t\n");
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_HWWPT);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_trap2, 0);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_trap3, X86_HW_WATCHPOINT_EVENT_FIRED);
+
+ pw.pw_md.md_address = NULL;
+ printf("Before writing watchpoint %d (disable it)\n", i);
+ ATF_REQUIRE(ptrace(PT_WRITE_WATCHPOINT, child, &pw, len) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(__HAVE_PTRACE_WATCHPOINTS)
+ATF_TC(watchpoint_trap_code1);
+ATF_TC_HEAD(watchpoint_trap_code1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Call PT_COUNT_WATCHPOINTS and test code trap with watchpoint 1");
+}
+
+ATF_TC_BODY(watchpoint_trap_code1, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ const int i = 1;
+ struct ptrace_watchpoint pw;
+ int len = sizeof(pw);
+ int watchme = 1234;
+ struct ptrace_siginfo info;
+ memset(&info, 0, sizeof(info));
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("check_happy(%d)=%d\n", watchme, check_happy(watchme));
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Preparing code watchpoint trap %d\n", i);
+
+ pw.pw_index = i;
+ pw.pw_lwpid = 0;
+ pw.pw_type = PTRACE_PW_TYPE_DBREGS;
+ pw.pw_md.md_address = (void *)check_happy;
+ pw.pw_md.md_condition = X86_HW_WATCHPOINT_DR7_CONDITION_EXECUTION;
+ pw.pw_md.md_length = X86_HW_WATCHPOINT_DR7_LENGTH_BYTE;
+
+ printf("struct ptrace {\n");
+ printf("\t.pw_index=%d\n", pw.pw_index);
+ printf("\t.pw_lwpid=%d\n", pw.pw_lwpid);
+ printf("\t.pw_type=%d\n", pw.pw_type);
+ printf("\t.pw_md.md_address=%p\n", pw.pw_md.md_address);
+ printf("\t.pw_md.md_condition=%#x\n", pw.pw_md.md_condition);
+ printf("\t.pw_md.md_length=%#x\n", pw.pw_md.md_length);
+ printf("}\n");
+
+ printf("Before writing watchpoint %d\n", i);
+ ATF_REQUIRE(ptrace(PT_WRITE_WATCHPOINT, child, &pw, len) != -1);
+
+ printf("Before resuming the child process where it left off "
+ "and without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Signal traced to lwpid=%d\n", info.psi_lwpid);
+ printf("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
+ info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
+ info.psi_siginfo.si_errno);
+
+ printf("Before checking siginfo_t\n");
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_HWWPT);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_trap2, 1);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_trap3, X86_HW_WATCHPOINT_EVENT_FIRED);
+
+ pw.pw_md.md_address = NULL;
+ printf("Before writing watchpoint %d (disable it)\n", i);
+ ATF_REQUIRE(ptrace(PT_WRITE_WATCHPOINT, child, &pw, len) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(__HAVE_PTRACE_WATCHPOINTS)
+ATF_TC(watchpoint_trap_code2);
+ATF_TC_HEAD(watchpoint_trap_code2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Call PT_COUNT_WATCHPOINTS and test code trap with watchpoint 2");
+}
+
+ATF_TC_BODY(watchpoint_trap_code2, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ const int i = 2;
+ struct ptrace_watchpoint pw;
+ int len = sizeof(pw);
+ int watchme = 1234;
+ struct ptrace_siginfo info;
+ memset(&info, 0, sizeof(info));
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("check_happy(%d)=%d\n", watchme, check_happy(watchme));
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Preparing code watchpoint trap %d\n", i);
+
+ pw.pw_index = i;
+ pw.pw_lwpid = 0;
+ pw.pw_type = PTRACE_PW_TYPE_DBREGS;
+ pw.pw_md.md_address = (void *)check_happy;
+ pw.pw_md.md_condition = X86_HW_WATCHPOINT_DR7_CONDITION_EXECUTION;
+ pw.pw_md.md_length = X86_HW_WATCHPOINT_DR7_LENGTH_BYTE;
+
+ printf("struct ptrace {\n");
+ printf("\t.pw_index=%d\n", pw.pw_index);
+ printf("\t.pw_lwpid=%d\n", pw.pw_lwpid);
+ printf("\t.pw_type=%#x\n", pw.pw_type);
+ printf("\t.pw_md.md_address=%p\n", pw.pw_md.md_address);
+ printf("\t.pw_md.md_condition=%#x\n", pw.pw_md.md_condition);
+ printf("\t.pw_md.md_length=%#x\n", pw.pw_md.md_length);
+ printf("}\n");
+
+ printf("Before writing watchpoint %d\n", i);
+ ATF_REQUIRE(ptrace(PT_WRITE_WATCHPOINT, child, &pw, len) != -1);
+
+ printf("Before resuming the child process where it left off "
+ "and without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Signal traced to lwpid=%d\n", info.psi_lwpid);
+ printf("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
+ info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
+ info.psi_siginfo.si_errno);
+
+ printf("Before checking siginfo_t\n");
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_HWWPT);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_trap2, 2);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_trap3, X86_HW_WATCHPOINT_EVENT_FIRED);
+
+ pw.pw_md.md_address = NULL;
+ printf("Before writing watchpoint %d (disable it)\n", i);
+ ATF_REQUIRE(ptrace(PT_WRITE_WATCHPOINT, child, &pw, len) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(__HAVE_PTRACE_WATCHPOINTS)
+ATF_TC(watchpoint_trap_code3);
+ATF_TC_HEAD(watchpoint_trap_code3, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Call PT_COUNT_WATCHPOINTS and test code trap with watchpoint 3");
+}
+
+ATF_TC_BODY(watchpoint_trap_code3, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ const int i = 3;
+ struct ptrace_watchpoint pw;
+ int len = sizeof(pw);
+ int watchme = 1234;
+ struct ptrace_siginfo info;
+ memset(&info, 0, sizeof(info));
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("check_happy(%d)=%d\n", watchme, check_happy(watchme));
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Preparing code watchpoint trap %d\n", i);
+
+ pw.pw_index = i;
+ pw.pw_lwpid = 0;
+ pw.pw_type = PTRACE_PW_TYPE_DBREGS;
+ pw.pw_md.md_address = (void *)check_happy;
+ pw.pw_md.md_condition = X86_HW_WATCHPOINT_DR7_CONDITION_EXECUTION;
+ pw.pw_md.md_length = X86_HW_WATCHPOINT_DR7_LENGTH_BYTE;
+
+ printf("struct ptrace {\n");
+ printf("\t.pw_index=%d\n", pw.pw_index);
+ printf("\t.pw_lwpid=%d\n", pw.pw_lwpid);
+ printf("\t.pw_type=%#x\n", pw.pw_type);
+ printf("\t.pw_md.md_address=%p\n", pw.pw_md.md_address);
+ printf("\t.pw_md.md_condition=%#x\n", pw.pw_md.md_condition);
+ printf("\t.pw_md.md_length=%#x\n", pw.pw_md.md_length);
+ printf("}\n");
+
+ printf("Before writing watchpoint %d\n", i);
+ ATF_REQUIRE(ptrace(PT_WRITE_WATCHPOINT, child, &pw, len) != -1);
+
+ printf("Before resuming the child process where it left off "
+ "and without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Signal traced to lwpid=%d\n", info.psi_lwpid);
+ printf("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
+ info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
+ info.psi_siginfo.si_errno);
+
+ printf("Before checking siginfo_t\n");
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_HWWPT);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_trap2, 3);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_trap3, X86_HW_WATCHPOINT_EVENT_FIRED);
+
+ pw.pw_md.md_address = NULL;
+ printf("Before writing watchpoint %d (disable it)\n", i);
+ ATF_REQUIRE(ptrace(PT_WRITE_WATCHPOINT, child, &pw, len) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(__HAVE_PTRACE_WATCHPOINTS)
+ATF_TC(watchpoint_trap_data_write0);
+ATF_TC_HEAD(watchpoint_trap_data_write0, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Call PT_COUNT_WATCHPOINTS and test write trap with watchpoint 0");
+}
+
+ATF_TC_BODY(watchpoint_trap_data_write0, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ const int i = 0;
+ struct ptrace_watchpoint pw;
+ int len = sizeof(pw);
+ int watchme = 1234;
+ struct ptrace_siginfo info;
+ memset(&info, 0, sizeof(info));
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ ++watchme;
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Preparing code watchpoint trap %d\n", i);
+
+ pw.pw_index = 0;
+ pw.pw_type = PTRACE_PW_TYPE_DBREGS;
+ pw.pw_md.md_address = &watchme;
+ pw.pw_md.md_condition = X86_HW_WATCHPOINT_DR7_CONDITION_DATA_WRITE;
+ pw.pw_md.md_length = X86_HW_WATCHPOINT_DR7_LENGTH_BYTE;
+
+ printf("struct ptrace {\n");
+ printf("\t.pw_index=%d\n", pw.pw_index);
+ printf("\t.pw_lwpid=%d\n", pw.pw_lwpid);
+ printf("\t.pw_type=%#x\n", pw.pw_type);
+ printf("\t.pw_md.md_address=%p\n", pw.pw_md.md_address);
+ printf("\t.pw_md.md_condition=%#x\n", pw.pw_md.md_condition);
+ printf("\t.pw_md.md_length=%#x\n", pw.pw_md.md_length);
+ printf("}\n");
+
+ printf("Before writing watchpoint %d\n", i);
+ ATF_REQUIRE(ptrace(PT_WRITE_WATCHPOINT, child, &pw, len) != -1);
+
+ printf("Before resuming the child process where it left off "
+ "and without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Signal traced to lwpid=%d\n", info.psi_lwpid);
+ printf("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
+ info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
+ info.psi_siginfo.si_errno);
+
+ printf("Before checking siginfo_t\n");
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_HWWPT);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_trap2, 0);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_trap3, X86_HW_WATCHPOINT_EVENT_FIRED);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(__HAVE_PTRACE_WATCHPOINTS)
+ATF_TC(watchpoint_trap_data_write1);
+ATF_TC_HEAD(watchpoint_trap_data_write1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Call PT_COUNT_WATCHPOINTS and test write trap with watchpoint 1");
+}
+
+ATF_TC_BODY(watchpoint_trap_data_write1, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ const int i = 1;
+ struct ptrace_watchpoint pw;
+ int len = sizeof(pw);
+ int watchme = 1234;
+ struct ptrace_siginfo info;
+ memset(&info, 0, sizeof(info));
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ ++watchme;
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Preparing code watchpoint trap %d\n", i);
+
+ pw.pw_index = i;
+ pw.pw_type = PTRACE_PW_TYPE_DBREGS;
+ pw.pw_md.md_address = &watchme;
+ pw.pw_md.md_condition = X86_HW_WATCHPOINT_DR7_CONDITION_DATA_WRITE;
+ pw.pw_md.md_length = X86_HW_WATCHPOINT_DR7_LENGTH_BYTE;
+
+ printf("struct ptrace {\n");
+ printf("\t.pw_index=%d\n", pw.pw_index);
+ printf("\t.pw_lwpid=%d\n", pw.pw_lwpid);
+ printf("\t.pw_type=%#x\n", pw.pw_type);
+ printf("\t.pw_md.md_address=%p\n", pw.pw_md.md_address);
+ printf("\t.pw_md.md_condition=%#x\n", pw.pw_md.md_condition);
+ printf("\t.pw_md.md_length=%#x\n", pw.pw_md.md_length);
+ printf("}\n");
+
+ printf("Before writing watchpoint %d\n", i);
+ ATF_REQUIRE(ptrace(PT_WRITE_WATCHPOINT, child, &pw, len) != -1);
+
+ printf("Before resuming the child process where it left off "
+ "and without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Signal traced to lwpid=%d\n", info.psi_lwpid);
+ printf("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
+ info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
+ info.psi_siginfo.si_errno);
+
+ printf("Before checking siginfo_t\n");
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_HWWPT);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_trap2, 1);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_trap3, X86_HW_WATCHPOINT_EVENT_FIRED);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(__HAVE_PTRACE_WATCHPOINTS)
+ATF_TC(watchpoint_trap_data_write2);
+ATF_TC_HEAD(watchpoint_trap_data_write2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Call PT_COUNT_WATCHPOINTS and test write trap with watchpoint 2");
+}
+
+ATF_TC_BODY(watchpoint_trap_data_write2, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ const int i = 2;
+ struct ptrace_watchpoint pw;
+ int len = sizeof(pw);
+ int watchme = 1234;
+ struct ptrace_siginfo info;
+ memset(&info, 0, sizeof(info));
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ ++watchme;
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Preparing code watchpoint trap %d\n", i);
+
+ pw.pw_index = i;
+ pw.pw_type = PTRACE_PW_TYPE_DBREGS;
+ pw.pw_md.md_address = &watchme;
+ pw.pw_md.md_condition = X86_HW_WATCHPOINT_DR7_CONDITION_DATA_WRITE;
+ pw.pw_md.md_length = X86_HW_WATCHPOINT_DR7_LENGTH_BYTE;
+
+ printf("struct ptrace {\n");
+ printf("\t.pw_index=%d\n", pw.pw_index);
+ printf("\t.pw_lwpid=%d\n", pw.pw_lwpid);
+ printf("\t.pw_type=%#x\n", pw.pw_type);
+ printf("\t.pw_md.md_address=%p\n", pw.pw_md.md_address);
+ printf("\t.pw_md.md_condition=%#x\n", pw.pw_md.md_condition);
+ printf("\t.pw_md.md_length=%#x\n", pw.pw_md.md_length);
+ printf("}\n");
+
+ printf("Before writing watchpoint %d\n", i);
+ ATF_REQUIRE(ptrace(PT_WRITE_WATCHPOINT, child, &pw, len) != -1);
+
+ printf("Before resuming the child process where it left off "
+ "and without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Signal traced to lwpid=%d\n", info.psi_lwpid);
+ printf("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
+ info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
+ info.psi_siginfo.si_errno);
+
+ printf("Before checking siginfo_t\n");
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_HWWPT);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_trap2, 2);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_trap3, X86_HW_WATCHPOINT_EVENT_FIRED);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+
+#if defined(__HAVE_PTRACE_WATCHPOINTS)
+ATF_TC(watchpoint_trap_data_write3);
+ATF_TC_HEAD(watchpoint_trap_data_write3, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Call PT_COUNT_WATCHPOINTS and test write trap with watchpoint 3");
+}
+
+ATF_TC_BODY(watchpoint_trap_data_write3, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ const int i = 3;
+ struct ptrace_watchpoint pw;
+ int len = sizeof(pw);
+ int watchme = 1234;
+ struct ptrace_siginfo info;
+ memset(&info, 0, sizeof(info));
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ ++watchme;
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Preparing code watchpoint trap %d\n", i);
+
+ pw.pw_index = i;
+ pw.pw_type = PTRACE_PW_TYPE_DBREGS;
+ pw.pw_md.md_address = &watchme;
+ pw.pw_md.md_condition = X86_HW_WATCHPOINT_DR7_CONDITION_DATA_WRITE;
+ pw.pw_md.md_length = X86_HW_WATCHPOINT_DR7_LENGTH_BYTE;
+
+ printf("struct ptrace {\n");
+ printf("\t.pw_index=%d\n", pw.pw_index);
+ printf("\t.pw_lwpid=%d\n", pw.pw_lwpid);
+ printf("\t.pw_type=%#x\n", pw.pw_type);
+ printf("\t.pw_md.md_address=%p\n", pw.pw_md.md_address);
+ printf("\t.pw_md.md_condition=%#x\n", pw.pw_md.md_condition);
+ printf("\t.pw_md.md_length=%#x\n", pw.pw_md.md_length);
+ printf("}\n");
+
+ printf("Before writing watchpoint %d\n", i);
+ ATF_REQUIRE(ptrace(PT_WRITE_WATCHPOINT, child, &pw, len) != -1);
+
+ printf("Before resuming the child process where it left off "
+ "and without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Signal traced to lwpid=%d\n", info.psi_lwpid);
+ printf("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
+ info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
+ info.psi_siginfo.si_errno);
+
+ printf("Before checking siginfo_t\n");
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_HWWPT);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_trap2, 3);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_trap3, X86_HW_WATCHPOINT_EVENT_FIRED);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(__HAVE_PTRACE_WATCHPOINTS)
+ATF_TC(watchpoint_trap_data_rw0);
+ATF_TC_HEAD(watchpoint_trap_data_rw0, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Call PT_COUNT_WATCHPOINTS and test write trap with watchpoint 0");
+}
+
+ATF_TC_BODY(watchpoint_trap_data_rw0, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ const int i = 0;
+ struct ptrace_watchpoint pw;
+ int len = sizeof(pw);
+ int watchme = 1234;
+ struct ptrace_siginfo info;
+ memset(&info, 0, sizeof(info));
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("watchme=%d\n", watchme);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Preparing code watchpoint trap %d\n", i);
+
+ pw.pw_index = i;
+ pw.pw_type = PTRACE_PW_TYPE_DBREGS;
+ pw.pw_md.md_address = &watchme;
+ pw.pw_md.md_condition = X86_HW_WATCHPOINT_DR7_CONDITION_DATA_READWRITE;
+ pw.pw_md.md_length = X86_HW_WATCHPOINT_DR7_LENGTH_BYTE;
+
+ printf("struct ptrace {\n");
+ printf("\t.pw_index=%d\n", pw.pw_index);
+ printf("\t.pw_lwpid=%d\n", pw.pw_lwpid);
+ printf("\t.pw_type=%#x\n", pw.pw_type);
+ printf("\t.pw_md.md_address=%p\n", pw.pw_md.md_address);
+ printf("\t.pw_md.md_condition=%#x\n", pw.pw_md.md_condition);
+ printf("\t.pw_md.md_length=%#x\n", pw.pw_md.md_length);
+ printf("}\n");
+
+ printf("Before writing watchpoint %d\n", i);
+ ATF_REQUIRE(ptrace(PT_WRITE_WATCHPOINT, child, &pw, len) != -1);
+
+ printf("Before resuming the child process where it left off "
+ "and without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Signal traced to lwpid=%d\n", info.psi_lwpid);
+ printf("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
+ info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
+ info.psi_siginfo.si_errno);
+
+ printf("Before checking siginfo_t\n");
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_HWWPT);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_trap2, 0);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_trap3, X86_HW_WATCHPOINT_EVENT_FIRED);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(__HAVE_PTRACE_WATCHPOINTS)
+ATF_TC(watchpoint_trap_data_rw1);
+ATF_TC_HEAD(watchpoint_trap_data_rw1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Call PT_COUNT_WATCHPOINTS and test write trap with watchpoint 1");
+}
+
+ATF_TC_BODY(watchpoint_trap_data_rw1, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ const int i = 1;
+ struct ptrace_watchpoint pw;
+ int len = sizeof(pw);
+ int watchme = 1234;
+ struct ptrace_siginfo info;
+ memset(&info, 0, sizeof(info));
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("watchme=%d\n", watchme);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Preparing code watchpoint trap %d\n", i);
+
+ pw.pw_index = i;
+ pw.pw_type = PTRACE_PW_TYPE_DBREGS;
+ pw.pw_md.md_address = &watchme;
+ pw.pw_md.md_condition = X86_HW_WATCHPOINT_DR7_CONDITION_DATA_READWRITE;
+ pw.pw_md.md_length = X86_HW_WATCHPOINT_DR7_LENGTH_BYTE;
+
+ printf("struct ptrace {\n");
+ printf("\t.pw_index=%d\n", pw.pw_index);
+ printf("\t.pw_lwpid=%d\n", pw.pw_lwpid);
+ printf("\t.pw_type=%#x\n", pw.pw_type);
+ printf("\t.pw_md.md_address=%p\n", pw.pw_md.md_address);
+ printf("\t.pw_md.md_condition=%#x\n", pw.pw_md.md_condition);
+ printf("\t.pw_md.md_length=%#x\n", pw.pw_md.md_length);
+ printf("}\n");
+
+ printf("Before writing watchpoint %d\n", i);
+ ATF_REQUIRE(ptrace(PT_WRITE_WATCHPOINT, child, &pw, len) != -1);
+
+ printf("Before resuming the child process where it left off "
+ "and without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Signal traced to lwpid=%d\n", info.psi_lwpid);
+ printf("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
+ info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
+ info.psi_siginfo.si_errno);
+
+ printf("Before checking siginfo_t\n");
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_HWWPT);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_trap2, 1);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_trap3, X86_HW_WATCHPOINT_EVENT_FIRED);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(__HAVE_PTRACE_WATCHPOINTS)
+ATF_TC(watchpoint_trap_data_rw2);
+ATF_TC_HEAD(watchpoint_trap_data_rw2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Call PT_COUNT_WATCHPOINTS and test write trap with watchpoint 2");
+}
+
+ATF_TC_BODY(watchpoint_trap_data_rw2, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ const int i = 2;
+ struct ptrace_watchpoint pw;
+ int len = sizeof(pw);
+ int watchme = 1234;
+ struct ptrace_siginfo info;
+ memset(&info, 0, sizeof(info));
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("watchme=%d\n", watchme);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Preparing code watchpoint trap %d\n", i);
+
+ pw.pw_index = i;
+ pw.pw_type = PTRACE_PW_TYPE_DBREGS;
+ pw.pw_md.md_address = &watchme;
+ pw.pw_md.md_condition = X86_HW_WATCHPOINT_DR7_CONDITION_DATA_READWRITE;
+ pw.pw_md.md_length = X86_HW_WATCHPOINT_DR7_LENGTH_BYTE;
+
+ printf("struct ptrace {\n");
+ printf("\t.pw_index=%d\n", pw.pw_index);
+ printf("\t.pw_lwpid=%d\n", pw.pw_lwpid);
+ printf("\t.pw_type=%#x\n", pw.pw_type);
+ printf("\t.pw_md.md_address=%p\n", pw.pw_md.md_address);
+ printf("\t.pw_md.md_condition=%#x\n", pw.pw_md.md_condition);
+ printf("\t.pw_md.md_length=%#x\n", pw.pw_md.md_length);
+ printf("}\n");
+
+ printf("Before writing watchpoint %d\n", i);
+ ATF_REQUIRE(ptrace(PT_WRITE_WATCHPOINT, child, &pw, len) != -1);
+
+ printf("Before resuming the child process where it left off "
+ "and without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Signal traced to lwpid=%d\n", info.psi_lwpid);
+ printf("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
+ info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
+ info.psi_siginfo.si_errno);
+
+ printf("Before checking siginfo_t\n");
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_HWWPT);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_trap2, 2);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_trap3, X86_HW_WATCHPOINT_EVENT_FIRED);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(__HAVE_PTRACE_WATCHPOINTS)
+ATF_TC(watchpoint_trap_data_rw3);
+ATF_TC_HEAD(watchpoint_trap_data_rw3, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Call PT_COUNT_WATCHPOINTS and test write trap with watchpoint 3");
+}
+
+ATF_TC_BODY(watchpoint_trap_data_rw3, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ const int i = 3;
+ struct ptrace_watchpoint pw;
+ int len = sizeof(pw);
+ int watchme = 1234;
+ struct ptrace_siginfo info;
+ memset(&info, 0, sizeof(info));
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("watchme=%d\n", watchme);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Preparing code watchpoint trap %d\n", i);
+
+ pw.pw_index = i;
+ pw.pw_type = PTRACE_PW_TYPE_DBREGS;
+ pw.pw_md.md_address = &watchme;
+ pw.pw_md.md_condition = X86_HW_WATCHPOINT_DR7_CONDITION_DATA_READWRITE;
+ pw.pw_md.md_length = X86_HW_WATCHPOINT_DR7_LENGTH_BYTE;
+
+ printf("struct ptrace {\n");
+ printf("\t.pw_index=%d\n", pw.pw_index);
+ printf("\t.pw_lwpid=%d\n", pw.pw_lwpid);
+ printf("\t.pw_type=%#x\n", pw.pw_type);
+ printf("\t.pw_md.md_address=%p\n", pw.pw_md.md_address);
+ printf("\t.pw_md.md_condition=%#x\n", pw.pw_md.md_condition);
+ printf("\t.pw_md.md_length=%#x\n", pw.pw_md.md_length);
+ printf("}\n");
+
+ printf("Before writing watchpoint %d\n", i);
+ ATF_REQUIRE(ptrace(PT_WRITE_WATCHPOINT, child, &pw, len) != -1);
+
+ printf("Before resuming the child process where it left off "
+ "and without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Signal traced to lwpid=%d\n", info.psi_lwpid);
+ printf("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
+ info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
+ info.psi_siginfo.si_errno);
+
+ printf("Before checking siginfo_t\n");
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_HWWPT);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_trap2, 3);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_trap3, X86_HW_WATCHPOINT_EVENT_FIRED);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+ATF_TP_ADD_TCS(tp)
+{
+ setvbuf(stdout, NULL, _IONBF, 0);
+ setvbuf(stderr, NULL, _IONBF, 0);
+
+ ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs1);
+
+ ATF_TP_ADD_TC_HAVE_PTRACE_WATCHPOINTS(tp, watchpoint_count);
+ ATF_TP_ADD_TC_HAVE_PTRACE_WATCHPOINTS(tp, watchpoint_read);
+ ATF_TP_ADD_TC_HAVE_PTRACE_WATCHPOINTS(tp, watchpoint_write_unmodified);
+ ATF_TP_ADD_TC_HAVE_PTRACE_WATCHPOINTS(tp, watchpoint_trap_code0);
+ ATF_TP_ADD_TC_HAVE_PTRACE_WATCHPOINTS(tp, watchpoint_trap_code1);
+ ATF_TP_ADD_TC_HAVE_PTRACE_WATCHPOINTS(tp, watchpoint_trap_code2);
+ ATF_TP_ADD_TC_HAVE_PTRACE_WATCHPOINTS(tp, watchpoint_trap_code3);
+ ATF_TP_ADD_TC_HAVE_PTRACE_WATCHPOINTS(tp, watchpoint_trap_data_write0);
+ ATF_TP_ADD_TC_HAVE_PTRACE_WATCHPOINTS(tp, watchpoint_trap_data_write1);
+ ATF_TP_ADD_TC_HAVE_PTRACE_WATCHPOINTS(tp, watchpoint_trap_data_write2);
+ ATF_TP_ADD_TC_HAVE_PTRACE_WATCHPOINTS(tp, watchpoint_trap_data_write3);
+ ATF_TP_ADD_TC_HAVE_PTRACE_WATCHPOINTS(tp, watchpoint_trap_data_rw0);
+ ATF_TP_ADD_TC_HAVE_PTRACE_WATCHPOINTS(tp, watchpoint_trap_data_rw1);
+ ATF_TP_ADD_TC_HAVE_PTRACE_WATCHPOINTS(tp, watchpoint_trap_data_rw2);
+ ATF_TP_ADD_TC_HAVE_PTRACE_WATCHPOINTS(tp, watchpoint_trap_data_rw3);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/arch/amd64/t_ptrace_wait3.c b/contrib/netbsd-tests/kernel/arch/amd64/t_ptrace_wait3.c
new file mode 100644
index 000000000000..c5dfe7358738
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/arch/amd64/t_ptrace_wait3.c
@@ -0,0 +1,30 @@
+/* $NetBSD: t_ptrace_wait3.c,v 1.1 2016/12/02 05:54:15 kamil Exp $ */
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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.
+ */
+
+#define TWAIT_WAIT3
+#include "t_ptrace_wait.c"
diff --git a/contrib/netbsd-tests/kernel/arch/amd64/t_ptrace_wait4.c b/contrib/netbsd-tests/kernel/arch/amd64/t_ptrace_wait4.c
new file mode 100644
index 000000000000..7e1fed95ee62
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/arch/amd64/t_ptrace_wait4.c
@@ -0,0 +1,30 @@
+/* $NetBSD: t_ptrace_wait4.c,v 1.1 2016/12/02 05:54:15 kamil Exp $ */
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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.
+ */
+
+#define TWAIT_WAIT4
+#include "t_ptrace_wait.c"
diff --git a/contrib/netbsd-tests/kernel/arch/amd64/t_ptrace_wait6.c b/contrib/netbsd-tests/kernel/arch/amd64/t_ptrace_wait6.c
new file mode 100644
index 000000000000..601efe3f3d62
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/arch/amd64/t_ptrace_wait6.c
@@ -0,0 +1,30 @@
+/* $NetBSD: t_ptrace_wait6.c,v 1.1 2016/12/02 05:54:15 kamil Exp $ */
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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.
+ */
+
+#define TWAIT_WAIT6
+#include "t_ptrace_wait.c"
diff --git a/contrib/netbsd-tests/kernel/arch/amd64/t_ptrace_waitid.c b/contrib/netbsd-tests/kernel/arch/amd64/t_ptrace_waitid.c
new file mode 100644
index 000000000000..5ec17a9b8d24
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/arch/amd64/t_ptrace_waitid.c
@@ -0,0 +1,30 @@
+/* $NetBSD: t_ptrace_waitid.c,v 1.1 2016/12/02 05:54:15 kamil Exp $ */
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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.
+ */
+
+#define TWAIT_WAITID
+#include "t_ptrace_wait.c"
diff --git a/contrib/netbsd-tests/kernel/arch/amd64/t_ptrace_waitpid.c b/contrib/netbsd-tests/kernel/arch/amd64/t_ptrace_waitpid.c
new file mode 100644
index 000000000000..78b341c111f3
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/arch/amd64/t_ptrace_waitpid.c
@@ -0,0 +1,30 @@
+/* $NetBSD: t_ptrace_waitpid.c,v 1.1 2016/12/02 05:54:15 kamil Exp $ */
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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.
+ */
+
+#define TWAIT_WAITPID
+#include "t_ptrace_wait.c"
diff --git a/contrib/netbsd-tests/kernel/arch/i386/t_ptrace_wait.c b/contrib/netbsd-tests/kernel/arch/i386/t_ptrace_wait.c
new file mode 100644
index 000000000000..698573bb0985
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/arch/i386/t_ptrace_wait.c
@@ -0,0 +1,141 @@
+/* $NetBSD: t_ptrace_wait.c,v 1.2 2017/01/13 21:30:41 christos Exp $ */
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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_ptrace_wait.c,v 1.2 2017/01/13 21:30:41 christos Exp $");
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/ptrace.h>
+#include <sys/resource.h>
+#include <sys/stat.h>
+#include <sys/sysctl.h>
+#include <sys/wait.h>
+#include <machine/reg.h>
+#include <err.h>
+#include <errno.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+#include "h_macros.h"
+
+#include "../../t_ptrace_wait.h"
+
+
+#if defined(HAVE_GPREGS)
+ATF_TC(regs1);
+ATF_TC_HEAD(regs1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Call PT_GETREGS and iterate over General Purpose registers");
+}
+
+ATF_TC_BODY(regs1, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ struct reg r;
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Call GETREGS for the child process\n");
+ ATF_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
+
+ printf("EAX=%#" PRIxREGISTER "\n", r.r_eax);
+ printf("EBX=%#" PRIxREGISTER "\n", r.r_ebx);
+ printf("ECX=%#" PRIxREGISTER "\n", r.r_ecx);
+ printf("EDX=%#" PRIxREGISTER "\n", r.r_edx);
+
+ printf("ESP=%#" PRIxREGISTER "\n", r.r_esp);
+ printf("EBP=%#" PRIxREGISTER "\n", r.r_ebp);
+
+ printf("ESI=%#" PRIxREGISTER "\n", r.r_esi);
+ printf("EDI=%#" PRIxREGISTER "\n", r.r_edi);
+
+ printf("EIP=%#" PRIxREGISTER "\n", r.r_eip);
+
+ printf("EFLAGS=%#" PRIxREGISTER "\n", r.r_eflags);
+
+ printf("CS=%#" PRIxREGISTER "\n", r.r_cs);
+ printf("SS=%#" PRIxREGISTER "\n", r.r_ss);
+ printf("DS=%#" PRIxREGISTER "\n", r.r_ds);
+ printf("ES=%#" PRIxREGISTER "\n", r.r_es);
+ printf("FS=%#" PRIxREGISTER "\n", r.r_fs);
+ printf("GS=%#" PRIxREGISTER "\n", r.r_gs);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+ATF_TP_ADD_TCS(tp)
+{
+ setvbuf(stdout, NULL, _IONBF, 0);
+ setvbuf(stderr, NULL, _IONBF, 0);
+
+ ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs1);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/arch/i386/t_ptrace_wait3.c b/contrib/netbsd-tests/kernel/arch/i386/t_ptrace_wait3.c
new file mode 100644
index 000000000000..56dda8e6978d
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/arch/i386/t_ptrace_wait3.c
@@ -0,0 +1,30 @@
+/* $NetBSD: t_ptrace_wait3.c,v 1.1 2016/12/13 18:00:10 kamil Exp $ */
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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.
+ */
+
+#define TWAIT_WAIT3
+#include "t_ptrace_wait.c"
diff --git a/contrib/netbsd-tests/kernel/arch/i386/t_ptrace_wait4.c b/contrib/netbsd-tests/kernel/arch/i386/t_ptrace_wait4.c
new file mode 100644
index 000000000000..5fbdb53503b9
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/arch/i386/t_ptrace_wait4.c
@@ -0,0 +1,30 @@
+/* $NetBSD: t_ptrace_wait4.c,v 1.1 2016/12/13 18:00:10 kamil Exp $ */
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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.
+ */
+
+#define TWAIT_WAIT4
+#include "t_ptrace_wait.c"
diff --git a/contrib/netbsd-tests/kernel/arch/i386/t_ptrace_wait6.c b/contrib/netbsd-tests/kernel/arch/i386/t_ptrace_wait6.c
new file mode 100644
index 000000000000..117c844123c9
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/arch/i386/t_ptrace_wait6.c
@@ -0,0 +1,30 @@
+/* $NetBSD: t_ptrace_wait6.c,v 1.1 2016/12/13 18:00:10 kamil Exp $ */
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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.
+ */
+
+#define TWAIT_WAIT6
+#include "t_ptrace_wait.c"
diff --git a/contrib/netbsd-tests/kernel/arch/i386/t_ptrace_waitid.c b/contrib/netbsd-tests/kernel/arch/i386/t_ptrace_waitid.c
new file mode 100644
index 000000000000..274e8bd9f19c
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/arch/i386/t_ptrace_waitid.c
@@ -0,0 +1,30 @@
+/* $NetBSD: t_ptrace_waitid.c,v 1.1 2016/12/13 18:00:10 kamil Exp $ */
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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.
+ */
+
+#define TWAIT_WAITID
+#include "t_ptrace_wait.c"
diff --git a/contrib/netbsd-tests/kernel/arch/i386/t_ptrace_waitpid.c b/contrib/netbsd-tests/kernel/arch/i386/t_ptrace_waitpid.c
new file mode 100644
index 000000000000..2fe5c570f9f7
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/arch/i386/t_ptrace_waitpid.c
@@ -0,0 +1,30 @@
+/* $NetBSD: t_ptrace_waitpid.c,v 1.1 2016/12/13 18:00:10 kamil Exp $ */
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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.
+ */
+
+#define TWAIT_WAITPID
+#include "t_ptrace_wait.c"
diff --git a/contrib/netbsd-tests/kernel/gen_t_subr_prf b/contrib/netbsd-tests/kernel/gen_t_subr_prf
new file mode 100755
index 000000000000..b58a6ba425d5
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/gen_t_subr_prf
@@ -0,0 +1,134 @@
+#!/bin/sh
+
+cat << _EOF > $2
+#include <sys/types.h>
+#include <sys/time.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdint.h>
+#include <string.h>
+#include <sha2.h>
+
+#include <atf-c.h>
+
+/* Avoid SSP re-definitions */
+#undef snprintf
+#undef vsnprintf
+#undef sprintf
+#undef vsprintf
+
+#define KPRINTF_BUFSIZE 1024
+#undef putchar
+#define putchar xputchar
+
+static int putchar(char c, int foo, void *b)
+{
+ return fputc(c, stderr);
+}
+
+#define TOBUFONLY 1
+static const char HEXDIGITS[] = "0123456789ABCDEF";
+static const char hexdigits[] = "0123456789abcdef";
+
+typedef int device_t;
+
+#if 0
+static SHA512_CTX kprnd_sha;
+#endif
+
+#define timespec timeval
+#define nanotime(ts) gettimeofday(ts, NULL)
+
+#define device_xname(a) ""
+int kprintf(const char *, int, void *, char *, va_list) __printflike(1, 0);
+void device_printf(device_t, const char *, ...) __printflike(2, 3);
+
+static void
+empty(void)
+{
+}
+
+static void (*v_flush)(void) = empty;
+
+ATF_TC(snprintf_print);
+ATF_TC_HEAD(snprintf_print, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "checks snprintf print");
+}
+
+ATF_TC_BODY(snprintf_print, tc)
+{
+ char buf[10];
+ int i;
+
+ memset(buf, 'x', sizeof(buf));
+ i = snprintf(buf, sizeof(buf), "number %d", 10);
+ ATF_CHECK_EQ(i, 9);
+ ATF_CHECK_STREQ(buf, "number 10");
+}
+
+ATF_TC(snprintf_print_overflow);
+ATF_TC_HEAD(snprintf_print_overflow, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "checks snprintf print with overflow");
+}
+
+ATF_TC_BODY(snprintf_print_overflow, tc)
+{
+ char buf[10];
+ int i;
+
+ memset(buf, 'x', sizeof(buf));
+ i = snprintf(buf, sizeof(buf), "fjsdfsdjfsdf %d\n", 10);
+ ATF_CHECK_EQ(i, 16);
+ ATF_CHECK_STREQ(buf, "fjsdfsdjf");
+}
+
+ATF_TC(snprintf_count);
+ATF_TC_HEAD(snprintf_count, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "checks snprintf count");
+}
+
+ATF_TC_BODY(snprintf_count, tc)
+{
+ int i;
+
+ i = snprintf(NULL, 20, "number %d", 10);
+ ATF_CHECK_EQ(i, 9);
+}
+
+ATF_TC(snprintf_count_overflow);
+ATF_TC_HEAD(snprintf_count_overflow, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "checks snprintf count with overflow");
+}
+
+ATF_TC_BODY(snprintf_count_overflow, tc)
+{
+ int i;
+
+ i = snprintf(NULL, 10, "fjsdfsdjfsdf %d\n", 10);
+ ATF_CHECK_EQ(i, 16);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, snprintf_print);
+ ATF_TP_ADD_TC(tp, snprintf_print_overflow);
+ ATF_TP_ADD_TC(tp, snprintf_count);
+ ATF_TP_ADD_TC(tp, snprintf_count_overflow);
+
+ return atf_no_error();
+}
+_EOF
+
+awk '
+/^snprintf\(/ {
+ print prevline
+ out = 1
+}
+{
+ if (out) print
+ else prevline = $0
+}' $1 >>$2
diff --git a/contrib/netbsd-tests/kernel/h_ps_strings1.c b/contrib/netbsd-tests/kernel/h_ps_strings1.c
new file mode 100644
index 000000000000..64d8c717d755
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/h_ps_strings1.c
@@ -0,0 +1,74 @@
+/* $NetBSD: h_ps_strings1.c,v 1.1 2011/03/05 18:14:33 pgoyette Exp $ */
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Joerg Sonnenberger.
+ *
+ * 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 COPYRIGHT HOLDERS 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
+ * COPYRIGHT HOLDERS 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/types.h>
+#include <sys/exec.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+extern struct ps_strings *__ps_strings;
+
+int
+main(int argc, char **argv, char **environ)
+{
+ int ret = 0;
+ int nenv;
+
+ if (__ps_strings->ps_nargvstr != argc) {
+ static const char nargv_err[] = "Wrong argc in ps_strings";
+ write(STDOUT_FILENO, nargv_err, sizeof(nargv_err));
+ ret = 1;
+ }
+
+ if (__ps_strings->ps_argvstr != argv) {
+ static const char argv_err[] = "Wrong argv in ps_strings";
+ write(STDOUT_FILENO, argv_err, sizeof(argv_err));
+ ret = 1;
+ }
+
+ if (__ps_strings->ps_envstr != environ) {
+ static const char env_err[] = "Wrong env in ps_strings";
+ write(STDOUT_FILENO, env_err, sizeof(env_err));
+ ret = 1;
+ }
+ nenv = 0;
+ while (environ[nenv])
+ ++nenv;
+ if (__ps_strings->ps_nenvstr != nenv) {
+ static const char nenv_err[] = "Wrong nenv in ps_strings";
+ write(STDOUT_FILENO, nenv_err, sizeof(nenv_err));
+ ret = 1;
+ }
+
+ return ret;
+}
diff --git a/contrib/netbsd-tests/kernel/h_ps_strings2.c b/contrib/netbsd-tests/kernel/h_ps_strings2.c
new file mode 100644
index 000000000000..d9c1c24a6cc8
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/h_ps_strings2.c
@@ -0,0 +1,69 @@
+/* $NetBSD: h_ps_strings2.c,v 1.1 2011/03/05 18:14:33 pgoyette Exp $ */
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Joerg Sonnenberger.
+ *
+ * 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 COPYRIGHT HOLDERS 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
+ * COPYRIGHT HOLDERS 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/types.h>
+#include <sys/exec.h>
+#include <err.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define LEN 16384
+
+extern struct ps_strings *__ps_strings;
+
+int
+main(void)
+{
+ size_t i;
+ char buf[16];
+ char **argv;
+
+ if ((argv = calloc(LEN, sizeof(*argv))) == NULL)
+ errx(1, "calloc failed");
+ for (i = 0; i < LEN; ++i) {
+ snprintf(buf, sizeof(buf), "arg%04zx", i);
+ if ((argv[i] = strdup(buf)) == NULL)
+ errx(1, "strdup failed");
+ }
+ __ps_strings->ps_argvstr = argv;
+ __ps_strings->ps_nargvstr = LEN;
+
+ printf("Sleeping forever...\n");
+ do {
+ sleep(UINT_MAX);
+ } while /* CONSTCOND */ (1);
+ return 0;
+}
diff --git a/contrib/netbsd-tests/kernel/kqueue/read/t_fifo.c b/contrib/netbsd-tests/kernel/kqueue/read/t_fifo.c
new file mode 100644
index 000000000000..1f40ada392a5
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/kqueue/read/t_fifo.c
@@ -0,0 +1,102 @@
+/* $NetBSD: t_fifo.c,v 1.4 2017/01/13 21:30:41 christos Exp $ */
+
+/*-
+ * Copyright (c) 2008 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Luke Mewburn and Jaromir Dolecek.
+ *
+ * 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>
+__COPYRIGHT("@(#) Copyright (c) 2008\
+ The NetBSD Foundation, inc. All rights reserved.");
+__RCSID("$NetBSD: t_fifo.c,v 1.4 2017/01/13 21:30:41 christos Exp $");
+
+#include <sys/event.h>
+#include <sys/stat.h>
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <inttypes.h>
+
+#include <atf-c.h>
+
+#include "h_macros.h"
+
+#define FIFONAME "fifo"
+
+ATF_TC(fifo);
+ATF_TC_HEAD(fifo, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Checks EVFILT_READ on fifo");
+}
+ATF_TC_BODY(fifo, tc)
+{
+ int kq, n, fd;
+ struct kevent event[1];
+ char buffer[128];
+
+ RL(mkfifo(FIFONAME, 0644));
+ RL(fd = open(FIFONAME, O_RDWR, 0644));
+
+ RL(kq = kqueue());
+
+ EV_SET(&event[0], fd, EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, 0);
+ RL(kevent(kq, event, 1, NULL, 0, NULL));
+
+ /* make sure there is something in the fifo */
+ RL(write(fd, "foo", 3));
+ (void)printf("fifo: wrote 'foo'\n");
+
+ (void)memset(event, 0, sizeof(event));
+
+ RL(n = kevent(kq, NULL, 0, event, 1, NULL));
+
+ (void)printf("kevent num %d filt %d flags: %#x, fflags: %#x, "
+#ifdef __FreeBSD__
+ "data: %" PRIdPTR "\n", n, event[0].filter, event[0].flags,
+#else
+ "data: %" PRId64 "\n", n, event[0].filter, event[0].flags,
+#endif
+ event[0].fflags, event[0].data);
+
+ ATF_REQUIRE_EQ(event[0].filter, EVFILT_READ);
+
+ RL(n = read(fd, buffer, event[0].data));
+ buffer[n] = '\0';
+ (void)printf("fifo: read '%s'\n", buffer);
+
+ RL(close(fd));
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, fifo);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/kqueue/read/t_file.c b/contrib/netbsd-tests/kernel/kqueue/read/t_file.c
new file mode 100644
index 000000000000..f4669d24a09a
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/kqueue/read/t_file.c
@@ -0,0 +1,143 @@
+/* $NetBSD: t_file.c,v 1.4 2017/01/13 21:30:41 christos Exp $ */
+
+/*-
+ * Copyright (c) 2002, 2008 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Luke Mewburn.
+ *
+ * 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>
+__COPYRIGHT("@(#) Copyright (c) 2008\
+ The NetBSD Foundation, inc. All rights reserved.");
+__RCSID("$NetBSD: t_file.c,v 1.4 2017/01/13 21:30:41 christos Exp $");
+
+#include <sys/param.h>
+#include <sys/event.h>
+#include <sys/mount.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <inttypes.h>
+
+#include <atf-c.h>
+
+#include "h_macros.h"
+
+#define FILENAME "file"
+#define NLINES 5
+
+static void
+child(void)
+{
+ int i, n, fd;
+
+ (void)sleep(1);
+
+ for (i = 0; i < NLINES; ++i) {
+ fd = open(FILENAME, O_WRONLY|O_APPEND, 0644);
+ if (fd < 0)
+ err(EXIT_FAILURE, "open()");
+
+ n = write(fd, "foo\n", 4);
+ if (n < 0)
+ err(EXIT_FAILURE, "write()");
+
+ (void)close(fd);
+ (void)sleep(1);
+ }
+ _exit(0);
+}
+
+ATF_TC(file);
+ATF_TC_HEAD(file, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Checks EVFILT_READ on regular file");
+}
+ATF_TC_BODY(file, tc)
+{
+ char buffer[128];
+ struct kevent event[1];
+ pid_t pid;
+ int fd, kq, n, num, status;
+
+ RL(pid = fork());
+ if (pid == 0) {
+ child();
+ /* NOTREACHED */
+ }
+
+ RL(fd = open(FILENAME, O_RDONLY|O_CREAT, 0644));
+
+#if 1 /* XXX: why was this disabled? */
+ RL(lseek(fd, 0, SEEK_END));
+#endif
+
+ RL(kq = kqueue());
+
+ EV_SET(&event[0], fd, EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, 0);
+ RL(kevent(kq, event, 1, NULL, 0, NULL));
+
+ for (num = 0; num < NLINES;) {
+ RL(n = kevent(kq, NULL, 0, event, 1, NULL));
+ num += n;
+
+ (void)printf("kevent num %d flags: %#x, fflags: %#x, data: "
+#ifdef __FreeBSD__
+ "%" PRIdPTR "\n", n, event[0].flags, event[0].fflags,
+#else
+ "%" PRId64 "\n", n, event[0].flags, event[0].fflags,
+#endif
+ event[0].data);
+
+ if (event[0].data < 0)
+#if 1 /* XXXLUKEM */
+ RL(lseek(fd, 0, SEEK_END));
+#else
+ RL(lseek(fd, event[0].data, SEEK_END));
+#endif
+
+ RL(n = read(fd, buffer, 128));
+ buffer[n] = '\0';
+ (void)printf("file(%d): %s", num, buffer);
+ }
+
+ (void)waitpid(pid, &status, 0);
+
+ (void)printf("read: successful end\n");
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, file);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/kqueue/read/t_file2.c b/contrib/netbsd-tests/kernel/kqueue/read/t_file2.c
new file mode 100644
index 000000000000..733caebbf1e4
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/kqueue/read/t_file2.c
@@ -0,0 +1,79 @@
+/* $NetBSD: t_file2.c,v 1.4 2017/01/13 21:30:41 christos Exp $ */
+
+/*-
+ * Copyright (c) 2002, 2008 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jaromir Dolecek.
+ *
+ * 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>
+__COPYRIGHT("@(#) Copyright (c) 2008\
+ The NetBSD Foundation, inc. All rights reserved.");
+__RCSID("$NetBSD: t_file2.c,v 1.4 2017/01/13 21:30:41 christos Exp $");
+
+#include <sys/event.h>
+
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+#include "h_macros.h"
+
+ATF_TC(file2);
+ATF_TC_HEAD(file2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Checks EVFILT_READ for regular files. This test used to "
+ "trigger deadlock caused by problem fixed in revision 1.79.2.10 "
+ "of sys/kern/kern_descrip.c");
+}
+ATF_TC_BODY(file2, tc)
+{
+ int fd1, fd2, kq;
+ struct kevent event[1];
+
+ RL(fd1 = open("afile", O_RDONLY|O_CREAT, 0644));
+ RL(fd2 = open("bfile", O_RDONLY|O_CREAT, 0644));
+
+#if 1 /* XXX: why was this disabled? */
+ RL(lseek(fd1, 0, SEEK_END));
+#endif
+
+ RL(kq = kqueue());
+
+ EV_SET(&event[0], fd1, EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, 0);
+ RL(kevent(kq, event, 1, NULL, 0, NULL));
+
+ RL(dup2(fd2, fd1));
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, file2);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/kqueue/read/t_pipe.c b/contrib/netbsd-tests/kernel/kqueue/read/t_pipe.c
new file mode 100644
index 000000000000..56cb037e9529
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/kqueue/read/t_pipe.c
@@ -0,0 +1,88 @@
+/* $NetBSD: t_pipe.c,v 1.2 2017/01/13 21:30:41 christos Exp $ */
+
+/*-
+ * Copyright (c) 2002, 2008 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Luke Mewburn and Jaromir Dolecek.
+ *
+ * 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>
+__COPYRIGHT("@(#) Copyright (c) 2008\
+ The NetBSD Foundation, inc. All rights reserved.");
+__RCSID("$NetBSD: t_pipe.c,v 1.2 2017/01/13 21:30:41 christos Exp $");
+
+#include <sys/event.h>
+
+#include <stdio.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+#include "h_macros.h"
+
+ATF_TC(pipe);
+ATF_TC_HEAD(pipe, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Checks EVFILT_READ for pipes");
+}
+ATF_TC_BODY(pipe, tc)
+{
+ struct kevent event[1];
+ char buffer[128];
+ int fds[2];
+ int kq, n;
+
+ RL(pipe(fds));
+ RL(kq = kqueue());
+
+ EV_SET(&event[0], fds[0], EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, 0);
+ RL(kevent(kq, event, 1, NULL, 0, NULL));
+
+ /* make sure there is something in the pipe */
+ RL(write(fds[1], "foo", 3));
+ (void)printf("pipe: wrote 'foo' to pipe\n");
+
+ RL(n = kevent(kq, NULL, 0, event, 1, NULL));
+ (void)printf("kevent num %d flags: %#x, fflags: %#x, data: "
+#ifdef __FreeBSD__
+ "%" PRIdPTR "\n", n, event[0].flags, event[0].fflags, event[0].data);
+#else
+ "%" PRId64 "\n", n, event[0].flags, event[0].fflags, event[0].data);
+#endif
+
+ RL(n = read(fds[0], buffer, event[0].data));
+ buffer[n] = '\0';
+
+ (void)printf("pipe: read '%s'\n", buffer);
+ (void)printf("pipe: successful end\n");
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, pipe);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/kqueue/read/t_ttypty.c b/contrib/netbsd-tests/kernel/kqueue/read/t_ttypty.c
new file mode 100644
index 000000000000..37038299956a
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/kqueue/read/t_ttypty.c
@@ -0,0 +1,148 @@
+/* $NetBSD: t_ttypty.c,v 1.2 2017/01/13 21:30:41 christos Exp $ */
+
+/*-
+ * Copyright (c) 2002, 2008 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Luke Mewburn and Jaromir Dolecek.
+ *
+ * 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>
+__COPYRIGHT("@(#) Copyright (c) 2008\
+ The NetBSD Foundation, inc. All rights reserved.");
+__RCSID("$NetBSD: t_ttypty.c,v 1.2 2017/01/13 21:30:41 christos Exp $");
+
+#include <sys/event.h>
+#include <sys/wait.h>
+
+#include <poll.h>
+#include <stdio.h>
+#include <termios.h>
+#include <unistd.h>
+#include <util.h>
+
+#include <atf-c.h>
+
+#include "h_macros.h"
+
+static void
+h_check(bool check_master)
+{
+ char slavetty[1024];
+ char buffer[128];
+ struct kevent event[1];
+ pid_t child;
+ int amaster, aslave, acurrent;
+ int kq, n, status;
+#if 0
+ int fl;
+#endif
+ struct pollfd pfd;
+ struct termios tio;
+
+ RL(openpty(&amaster, &aslave, slavetty, NULL, NULL));
+
+ (void)printf("tty: openpty master %d slave %d tty '%s'\n",
+ amaster, aslave, slavetty);
+ acurrent = check_master ? amaster : aslave;
+
+ RL(child = fork());
+ if (child == 0) {
+ sleep(1);
+
+ (void)printf("tty: child writing 'f00\\n'\n");
+ (void)write(check_master ? aslave : amaster, "f00\n", 4);
+
+ _exit(0);
+ }
+
+ /* switch ONLCR off, to not get confused by newline translation */
+ RL(tcgetattr(acurrent, &tio));
+ tio.c_oflag &= ~ONLCR;
+ RL(tcsetattr(acurrent, TCSADRAIN, &tio));
+
+ pfd.fd = acurrent;
+ pfd.events = POLLIN;
+ (void)printf("tty: polling ...\n");
+ RL(poll(&pfd, 1, INFTIM));
+ (void)printf("tty: returned from poll - %d\n", pfd.revents);
+
+#if 0
+ fl = 1;
+ if (ioctl(acurrent, TIOCPKT, &fl) < 0)
+ err(1, "ioctl");
+#endif
+
+ RL(kq = kqueue());
+
+ EV_SET(&event[0], acurrent, EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, 0);
+ RL(kevent(kq, event, 1, NULL, 0, NULL));
+
+ RL(n = kevent(kq, NULL, 0, event, 1, NULL));
+
+ (void)printf("kevent num %d filt %d flags: %#x, fflags: %#x, "
+#ifdef __FreeBSD__
+ "data: %" PRIdPTR "\n", n, event[0].filter, event[0].flags,
+#else
+ "data: %" PRId64 "\n", n, event[0].filter, event[0].flags,
+#endif
+ event[0].fflags, event[0].data);
+
+ ATF_REQUIRE_EQ(event[0].filter, EVFILT_READ);
+
+ RL(n = read(acurrent, buffer, 128));
+ (void)printf("tty: read '%.*s' (n=%d)\n", n, buffer, n);
+
+ (void)waitpid(child, &status, 0);
+ (void)printf("tty: successful end\n");
+}
+
+ATF_TC(master);
+ATF_TC_HEAD(master, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Checks EVFILT_READ for master tty");
+}
+ATF_TC_BODY(master, tc)
+{
+ h_check(true);
+}
+
+ATF_TC(slave);
+ATF_TC_HEAD(slave, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Checks EVFILT_READ for slave tty");
+}
+ATF_TC_BODY(slave, tc)
+{
+ h_check(false);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, master);
+ ATF_TP_ADD_TC(tp, slave);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/kqueue/t_ioctl.c b/contrib/netbsd-tests/kernel/kqueue/t_ioctl.c
new file mode 100644
index 000000000000..78b058733457
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/kqueue/t_ioctl.c
@@ -0,0 +1,116 @@
+/* $NetBSD: t_ioctl.c,v 1.3 2017/01/13 21:30:41 christos Exp $ */
+
+/*-
+ * Copyright (c) 2002, 2008 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Luke Mewburn.
+ *
+ * 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>
+__COPYRIGHT("@(#) Copyright (c) 2008\
+ The NetBSD Foundation, inc. All rights reserved.");
+__RCSID("$NetBSD: t_ioctl.c,v 1.3 2017/01/13 21:30:41 christos Exp $");
+
+#include <sys/event.h>
+#include <sys/ioctl.h>
+
+#include <stdio.h>
+#include <string.h>
+
+#include <atf-c.h>
+
+#include "h_macros.h"
+
+ATF_TC(kfilter_byfilter);
+ATF_TC_HEAD(kfilter_byfilter, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Checks KFILTER_BYFILTER ioctl");
+}
+ATF_TC_BODY(kfilter_byfilter, tc)
+{
+ char buf[32];
+ struct kfilter_mapping km;
+ int kq;
+ uint32_t i;
+
+ RL(kq = kqueue());
+
+ km.name = buf;
+ km.len = sizeof(buf) - 1;
+
+ for (i = 0; i < 7; ++i) {
+ km.filter = i;
+ RL(ioctl(kq, KFILTER_BYFILTER, &km));
+ (void)printf(" map %d -> %s\n", km.filter, km.name);
+ }
+
+ km.filter = 7;
+ ATF_REQUIRE_EQ(ioctl(kq, KFILTER_BYFILTER, &km), -1);
+}
+
+ATF_TC(kfilter_byname);
+ATF_TC_HEAD(kfilter_byname, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Checks KFILTER_BYNAME ioctl");
+}
+ATF_TC_BODY(kfilter_byname, tc)
+{
+ const char *tests[] = {
+ "EVFILT_READ",
+ "EVFILT_WRITE",
+ "EVFILT_AIO",
+ "EVFILT_VNODE",
+ "EVFILT_PROC",
+ "EVFILT_SIGNAL",
+ "EVFILT_TIMER",
+ NULL
+ };
+ char buf[32];
+ struct kfilter_mapping km;
+ const char **test;
+ int kq;
+
+ RL(kq = kqueue());
+
+ km.name = buf;
+
+ for (test = &tests[0]; *test != NULL; ++test) {
+ (void)strlcpy(buf, *test, sizeof(buf));
+ RL(ioctl(kq, KFILTER_BYNAME, &km));
+ (void)printf(" map %s -> %d\n", km.name, km.filter);
+ }
+
+ (void)strlcpy(buf, "NOTREG_FILTER", sizeof(buf));
+ ATF_REQUIRE_EQ(ioctl(kq, KFILTER_BYNAME, &km), -1);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, kfilter_byfilter);
+ ATF_TP_ADD_TC(tp, kfilter_byname);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/kqueue/t_proc1.c b/contrib/netbsd-tests/kernel/kqueue/t_proc1.c
new file mode 100644
index 000000000000..947e6e317a0a
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/kqueue/t_proc1.c
@@ -0,0 +1,155 @@
+/* $NetBSD: t_proc1.c,v 1.3 2017/01/13 21:30:41 christos Exp $ */
+
+/*-
+ * Copyright (c) 2002, 2008 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Luke Mewburn and Jaromir Dolecek.
+ *
+ * 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>
+__COPYRIGHT("@(#) Copyright (c) 2008\
+ The NetBSD Foundation, inc. All rights reserved.");
+__RCSID("$NetBSD: t_proc1.c,v 1.3 2017/01/13 21:30:41 christos Exp $");
+
+/*
+ * this also used to trigger problem fixed in
+ * rev. 1.1.1.1.2.13 of sys/kern/kern_event.c
+ */
+
+#include <sys/param.h>
+#include <sys/event.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <inttypes.h>
+
+#include <atf-c.h>
+
+#include "h_macros.h"
+
+static int
+child(void)
+{
+ pid_t ch;
+ int status;
+ char *argv[] = { NULL, NULL };
+ char *envp[] = { NULL, NULL };
+
+ if ((argv[0] = strdup("true")) == NULL)
+ err(EXIT_FAILURE, "strdup(\"true\")");
+
+ if ((envp[0] = strdup("FOO=BAZ")) == NULL)
+ err(EXIT_FAILURE, "strdup(\"FOO=BAZ\")");
+
+ /* Ensure parent is ready */
+ (void)sleep(2);
+
+ /* Do fork */
+ switch (ch = fork()) {
+ case -1:
+ return EXIT_FAILURE;
+ /* NOTREACHED */
+ case 0:
+ return EXIT_SUCCESS;
+ /* NOTREACHED */
+ default:
+ wait(&status);
+ break;
+ }
+
+ /* Exec */
+ execve("/usr/bin/true", argv, envp);
+
+ /* NOTREACHED */
+ return EXIT_FAILURE;
+}
+
+ATF_TC(proc1);
+ATF_TC_HEAD(proc1, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Checks EVFILT_PROC");
+}
+ATF_TC_BODY(proc1, tc)
+{
+ struct kevent event[1];
+ pid_t pid;
+ int kq, status;
+ u_int want;
+
+ RL(kq = kqueue());
+
+ /* fork a child for doing the events */
+ RL(pid = fork());
+ if (pid == 0) {
+ _exit(child());
+ /* NOTREACHED */
+ }
+
+ (void)sleep(1); /* give child some time to come up */
+
+ event[0].ident = (uintptr_t)pid;
+ event[0].filter = EVFILT_PROC;
+ event[0].flags = EV_ADD | EV_ENABLE;
+ event[0].fflags = NOTE_EXIT | NOTE_FORK | NOTE_EXEC; /* | NOTE_TRACK;*/
+ want = NOTE_EXIT | NOTE_FORK | NOTE_EXEC;
+
+ RL(kevent(kq, event, 1, NULL, 0, NULL));
+
+ /* wait until we get all events we want */
+ while (want) {
+ RL(kevent(kq, NULL, 0, event, 1, NULL));
+ printf("%ld:", (long)event[0].ident);
+
+ if (event[0].fflags & NOTE_EXIT) {
+ want &= ~NOTE_EXIT;
+ printf(" NOTE_EXIT");
+ }
+ if (event[0].fflags & NOTE_EXEC) {
+ want &= ~NOTE_EXEC;
+ printf(" NOTE_EXEC");
+ }
+ if (event[0].fflags & NOTE_FORK) {
+ want &= ~NOTE_FORK;
+ printf(" NOTE_FORK");
+ }
+ if (event[0].fflags & NOTE_CHILD)
+ printf(" NOTE_CHILD, parent = %" PRId64, event[0].data);
+
+ printf("\n");
+ }
+
+ (void)waitpid(pid, &status, 0);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, proc1);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/kqueue/t_proc2.c b/contrib/netbsd-tests/kernel/kqueue/t_proc2.c
new file mode 100644
index 000000000000..2d905ed740d9
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/kqueue/t_proc2.c
@@ -0,0 +1,139 @@
+/* $NetBSD: t_proc2.c,v 1.3 2017/01/13 21:30:41 christos Exp $ */
+
+/*-
+ * Copyright (c) 2008 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Peter Werner <Peter.Werner@wgsn.com>.
+ *
+ * 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>
+__COPYRIGHT("@(#) Copyright (c) 2008\
+ The NetBSD Foundation, inc. All rights reserved.");
+__RCSID("$NetBSD: t_proc2.c,v 1.3 2017/01/13 21:30:41 christos Exp $");
+
+#include <sys/event.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <pwd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+#include "h_macros.h"
+
+static void
+child_two(void)
+{
+ _exit(EXIT_SUCCESS);
+}
+
+static void
+child_one(void)
+{
+ pid_t pid;
+ struct passwd *pwd;
+ const char *nam = "nobody";
+
+ pwd = getpwnam(nam);
+ if (pwd == NULL)
+ err(EXIT_FAILURE, "getpwnam(\"%s\")", nam);
+
+ if ((setuid(pwd->pw_uid)) == -1)
+ err(EXIT_FAILURE, "setuid(%d)", pwd->pw_uid);
+
+ pid = fork();
+ if (pid == -1)
+ err(EXIT_FAILURE, "fork()");
+
+ if (pid == 0)
+ child_two();
+
+ _exit(EXIT_SUCCESS);
+}
+
+ATF_TC(proc2);
+ATF_TC_HEAD(proc2, tc)
+{
+ atf_tc_set_md_var(tc, "require.user", "root");
+ atf_tc_set_md_var(tc, "descr",
+ "Checks EVFILT_PROC for NOTE_FORK|NOTE_TRACK error path problem "
+ "fixed in rev. 1.1.1.1.2.17 of sys/kern/kern_event.c");
+}
+ATF_TC_BODY(proc2, tc)
+{
+ pid_t pid = 0;
+ int kq, status;
+ struct kevent ke;
+ struct timespec timeout;
+
+ RL(kq = kqueue());
+
+ timeout.tv_sec = 0;
+ timeout.tv_nsec = 0;
+
+ RL(pid = fork());
+ if (pid == 0) {
+ (void)sleep(1); /* let parent set kevent */
+ child_one();
+ /* NOTREACHED */
+ }
+
+ EV_SET(&ke, (uintptr_t)pid, EVFILT_PROC, EV_ADD, NOTE_FORK|NOTE_TRACK,
+ 0, 0);
+
+ RL(kevent(kq, &ke, 1, NULL, 0, &timeout));
+
+ (void)sleep(2);
+
+ ke.ident = 0;
+ ke.fflags = 0;
+ ke.flags = EV_ENABLE;
+
+ RL(kevent(kq, NULL, 0, &ke, 1, &timeout));
+ RL(close(kq));
+
+ RL(waitpid(pid, &status, 0));
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE_EQ(WEXITSTATUS(status), EXIT_SUCCESS);
+
+ /*
+ * we are expecting an error here as we should not have
+ * been able to add a knote to child 2.
+ */
+ ATF_REQUIRE(ke.fflags & NOTE_TRACKERR);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, proc2);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/kqueue/t_proc3.c b/contrib/netbsd-tests/kernel/kqueue/t_proc3.c
new file mode 100644
index 000000000000..6def4094b1bf
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/kqueue/t_proc3.c
@@ -0,0 +1,99 @@
+/* $NetBSD: t_proc3.c,v 1.3 2017/01/13 21:30:41 christos Exp $ */
+
+/*-
+ * Copyright (c) 2012 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Joerg Sonnenberger.
+ *
+ * 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_proc3.c,v 1.3 2017/01/13 21:30:41 christos Exp $");
+
+#include <sys/event.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <pwd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+#include "h_macros.h"
+
+ATF_TC(proc3);
+ATF_TC_HEAD(proc3, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Checks EVFILT_PROC for NOTE_TRACK on self bug ");
+}
+
+ATF_TC_BODY(proc3, tc)
+{
+ pid_t pid = 0;
+ int kq, status;
+ struct kevent ke;
+ struct timespec timeout;
+
+ RL(kq = kqueue());
+
+ EV_SET(&ke, (uintptr_t)getpid(), EVFILT_PROC, EV_ADD, NOTE_TRACK, 0, 0);
+
+ RL(kevent(kq, &ke, 1, NULL, 0, NULL));
+
+ RL(pid = fork());
+ if (pid == 0) {
+ _exit(EXIT_SUCCESS);
+ /* NOTREACHED */
+ }
+
+ RL(waitpid(pid, &status, 0));
+ ATF_REQUIRE(WIFEXITED(status));
+ ATF_REQUIRE_EQ(WEXITSTATUS(status), EXIT_SUCCESS);
+
+ timeout.tv_sec = 0;
+ timeout.tv_nsec = 0;
+ ke.ident = 0;
+ ke.fflags = 0;
+ ke.flags = EV_ENABLE;
+
+ RL(kevent(kq, NULL, 0, &ke, 1, &timeout));
+ RL(close(kq));
+
+ ATF_REQUIRE(ke.fflags & NOTE_CHILD);
+ ATF_REQUIRE((ke.fflags & NOTE_TRACKERR) == 0);
+ ATF_REQUIRE_EQ((pid_t)ke.ident, pid);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, proc3);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/kqueue/t_sig.c b/contrib/netbsd-tests/kernel/kqueue/t_sig.c
new file mode 100644
index 000000000000..ea707e9af9e2
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/kqueue/t_sig.c
@@ -0,0 +1,143 @@
+/* $NetBSD: t_sig.c,v 1.3 2017/01/13 21:30:41 christos Exp $ */
+
+/*-
+ * Copyright (c) 2002, 2008 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Luke Mewburn and Jaromir Dolecek.
+ *
+ * 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>
+__COPYRIGHT("@(#) Copyright (c) 2008\
+ The NetBSD Foundation, inc. All rights reserved.");
+__RCSID("$NetBSD: t_sig.c,v 1.3 2017/01/13 21:30:41 christos Exp $");
+
+#include <sys/event.h>
+#include <sys/ioctl.h>
+#include <sys/param.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+
+#include <inttypes.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+#include "h_macros.h"
+
+#define NSIGNALS 5
+
+ATF_TC(sig);
+ATF_TC_HEAD(sig, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Checks EVFILT_SIGNAL");
+}
+ATF_TC_BODY(sig, tc)
+{
+ struct timespec timeout;
+#ifdef __NetBSD__
+ struct kfilter_mapping km;
+#endif
+ struct kevent event[1];
+#ifdef __NetBSD__
+ char namebuf[32];
+#endif
+ pid_t pid, child;
+ int kq, n, num, status;
+
+ pid = getpid();
+ (void)printf("my pid: %d\n", pid);
+
+ /* fork a child to send signals */
+ RL(child = fork());
+ if (child == 0) {
+ int i;
+ (void)sleep(2);
+ for(i = 0; i < NSIGNALS; ++i) {
+ (void)kill(pid, SIGUSR1);
+ (void)sleep(2);
+ }
+ _exit(0);
+ /* NOTREACHED */
+ }
+
+ RL(kq = kqueue());
+
+#ifdef __NetBSD__
+ (void)strlcpy(namebuf, "EVFILT_SIGNAL", sizeof(namebuf));
+ km.name = namebuf;
+ RL(ioctl(kq, KFILTER_BYNAME, &km));
+ (void)printf("got %d as filter number for `%s'.\n", km.filter, km.name);
+#endif
+
+ /* ignore the signal to avoid taking it for real */
+ REQUIRE_LIBC(signal(SIGUSR1, SIG_IGN), SIG_ERR);
+
+ event[0].ident = SIGUSR1;
+#ifdef __NetBSD__
+ event[0].filter = km.filter;
+#else
+ event[0].filter = EVFILT_SIGNAL;
+#endif
+ event[0].flags = EV_ADD | EV_ENABLE;
+
+ RL(kevent(kq, event, 1, NULL, 0, NULL));
+
+ (void)sleep(1);
+
+ timeout.tv_sec = 1;
+ timeout.tv_nsec = 0;
+
+ for (num = 0; num < NSIGNALS; num += n) {
+ struct timeval then, now, diff;
+
+ RL(gettimeofday(&then, NULL));
+ RL(n = kevent(kq, NULL, 0, event, 1, &timeout));
+ RL(gettimeofday(&now, NULL));
+ timersub(&now, &then, &diff);
+
+ (void)printf("sig: kevent returned %d in %lld.%06ld\n",
+ n, (long long)diff.tv_sec, (long)diff.tv_usec);
+
+ if (n == 0)
+ continue;
+
+ (void)printf("sig: kevent flags: 0x%x, data: %" PRId64 " (# "
+ "times signal posted)\n", event[0].flags, event[0].data);
+ }
+
+ (void)waitpid(child, &status, 0);
+ (void)printf("sig: finished successfully\n");
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, sig);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/kqueue/t_vnode.c b/contrib/netbsd-tests/kernel/kqueue/t_vnode.c
new file mode 100644
index 000000000000..e87c2b2c2d6f
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/kqueue/t_vnode.c
@@ -0,0 +1,533 @@
+#include <sys/event.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+/*
+ * Test cases for events triggered by manipulating a target directory
+ * content. Using EVFILT_VNODE filter on the target directory descriptor.
+ *
+ */
+
+static const char *dir_target = "foo";
+static const char *dir_inside1 = "foo/bar1";
+static const char *dir_inside2 = "foo/bar2";
+static const char *dir_outside = "bar";
+static const char *file_inside1 = "foo/baz1";
+static const char *file_inside2 = "foo/baz2";
+static const char *file_outside = "qux";
+static const struct timespec ts = {0, 0};
+static int kq = -1;
+static int target = -1;
+
+int init_target(void);
+int init_kqueue(void);
+int create_file(const char *);
+void cleanup(void);
+
+int
+init_target(void)
+{
+ if (mkdir(dir_target, S_IRWXU) < 0) {
+ return -1;
+ }
+ target = open(dir_target, O_RDONLY, 0);
+ return target;
+}
+
+int
+init_kqueue(void)
+{
+ struct kevent eventlist[1];
+
+ kq = kqueue();
+ if (kq < 0) {
+ return -1;
+ }
+ EV_SET(&eventlist[0], (uintptr_t)target, EVFILT_VNODE,
+ EV_ADD | EV_ONESHOT, NOTE_DELETE |
+ NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB |
+ NOTE_LINK | NOTE_RENAME | NOTE_REVOKE, 0, 0);
+ return kevent(kq, eventlist, 1, NULL, 0, NULL);
+}
+
+int
+create_file(const char *file)
+{
+ int fd;
+
+ fd = open(file, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
+ if (fd < 0) {
+ return -1;
+ }
+ return close(fd);
+}
+
+void
+cleanup(void)
+{
+ (void)unlink(file_inside1);
+ (void)unlink(file_inside2);
+ (void)unlink(file_outside);
+ (void)rmdir(dir_inside1);
+ (void)rmdir(dir_inside2);
+ (void)rmdir(dir_outside);
+ (void)rmdir(dir_target);
+ (void)close(kq);
+ (void)close(target);
+}
+
+ATF_TC_WITH_CLEANUP(dir_no_note_link_create_file_in);
+ATF_TC_HEAD(dir_no_note_link_create_file_in, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "This test case ensures "
+ "that kevent(2) does not return NOTE_LINK for the directory "
+ "'foo' if a file 'foo/baz' is created.");
+}
+ATF_TC_BODY(dir_no_note_link_create_file_in, tc)
+{
+ struct kevent changelist[1];
+
+ ATF_REQUIRE(init_target() != -1);
+ ATF_REQUIRE(init_kqueue() != -1);
+
+ ATF_REQUIRE(create_file(file_inside1) != -1);
+ ATF_REQUIRE(kevent(kq, NULL, 0, changelist, 1, &ts) != -1);
+ ATF_CHECK_EQ(changelist[0].fflags & NOTE_LINK, 0);
+}
+ATF_TC_CLEANUP(dir_no_note_link_create_file_in, tc)
+{
+ cleanup();
+}
+
+ATF_TC_WITH_CLEANUP(dir_no_note_link_delete_file_in);
+ATF_TC_HEAD(dir_no_note_link_delete_file_in, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "This test case ensures "
+ "that kevent(2) does not return NOTE_LINK for the directory "
+ "'foo' if a file 'foo/baz' is deleted.");
+}
+ATF_TC_BODY(dir_no_note_link_delete_file_in, tc)
+{
+ struct kevent changelist[1];
+
+ ATF_REQUIRE(init_target() != -1);
+ ATF_REQUIRE(create_file(file_inside1) != -1);
+ ATF_REQUIRE(init_kqueue() != -1);
+
+ ATF_REQUIRE(unlink(file_inside1) != -1);
+ ATF_REQUIRE(kevent(kq, NULL, 0, changelist, 1, &ts) != -1);
+ ATF_CHECK_EQ(changelist[0].fflags & NOTE_LINK, 0);
+}
+ATF_TC_CLEANUP(dir_no_note_link_delete_file_in, tc)
+{
+ cleanup();
+}
+
+ATF_TC_WITH_CLEANUP(dir_no_note_link_mv_dir_within);
+ATF_TC_HEAD(dir_no_note_link_mv_dir_within, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "This test case ensures "
+ "that kevent(2) does not return NOTE_LINK for the directory "
+ "'foo' if a directory 'foo/bar' is renamed to 'foo/baz'.");
+}
+ATF_TC_BODY(dir_no_note_link_mv_dir_within, tc)
+{
+ struct kevent changelist[1];
+
+ ATF_REQUIRE(init_target() != -1);
+ ATF_REQUIRE(mkdir(dir_inside1, S_IRWXU) != -1);
+ ATF_REQUIRE(init_kqueue() != -1);
+
+ ATF_REQUIRE(rename(dir_inside1, dir_inside2) != -1);
+ ATF_REQUIRE(kevent(kq, NULL, 0, changelist, 1, &ts) != -1);
+ ATF_CHECK_EQ(changelist[0].fflags & NOTE_LINK, 0);
+}
+ATF_TC_CLEANUP(dir_no_note_link_mv_dir_within, tc)
+{
+ cleanup();
+}
+
+ATF_TC_WITH_CLEANUP(dir_no_note_link_mv_file_within);
+ATF_TC_HEAD(dir_no_note_link_mv_file_within, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "This test case ensures "
+ "that kevent(2) does not return NOTE_LINK for the directory "
+ "'foo' if a file 'foo/baz' is renamed to 'foo/qux'.");
+}
+ATF_TC_BODY(dir_no_note_link_mv_file_within, tc)
+{
+ struct kevent changelist[1];
+
+ ATF_REQUIRE(init_target() != -1);
+ ATF_REQUIRE(create_file(file_inside1) != -1);
+ ATF_REQUIRE(init_kqueue() != -1);
+
+ ATF_REQUIRE(rename(file_inside1, file_inside2) != -1);
+ ATF_REQUIRE(kevent(kq, NULL, 0, changelist, 1, &ts) != -1);
+ ATF_CHECK_EQ(changelist[0].fflags & NOTE_LINK, 0);
+}
+ATF_TC_CLEANUP(dir_no_note_link_mv_file_within, tc)
+{
+ cleanup();
+}
+
+ATF_TC_WITH_CLEANUP(dir_note_link_create_dir_in);
+ATF_TC_HEAD(dir_note_link_create_dir_in, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "This test case ensures "
+ "that kevent(2) returns NOTE_LINK for the directory "
+ "'foo' if a directory 'foo/bar' is created.");
+}
+ATF_TC_BODY(dir_note_link_create_dir_in, tc)
+{
+ struct kevent changelist[1];
+
+ ATF_REQUIRE(init_target() != -1);
+ ATF_REQUIRE(init_kqueue() != -1);
+
+ ATF_REQUIRE(mkdir(dir_inside1, S_IRWXU) != -1);
+ ATF_REQUIRE(kevent(kq, NULL, 0, changelist, 1, &ts) != -1);
+ ATF_CHECK_EQ(changelist[0].fflags & NOTE_LINK, NOTE_LINK);
+}
+ATF_TC_CLEANUP(dir_note_link_create_dir_in, tc)
+{
+ cleanup();
+}
+
+ATF_TC_WITH_CLEANUP(dir_note_link_delete_dir_in);
+ATF_TC_HEAD(dir_note_link_delete_dir_in, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "This test case ensures "
+ "that kevent(2) returns NOTE_LINK for the directory "
+ "'foo' if a directory 'foo/bar' is deleted.");
+}
+ATF_TC_BODY(dir_note_link_delete_dir_in, tc)
+{
+ struct kevent changelist[1];
+
+ ATF_REQUIRE(init_target() != -1);
+ ATF_REQUIRE(mkdir(dir_inside1, S_IRWXU) != -1);
+ ATF_REQUIRE(init_kqueue() != -1);
+
+ ATF_REQUIRE(rmdir(dir_inside1) != -1);
+ ATF_REQUIRE(kevent(kq, NULL, 0, changelist, 1, &ts) != -1);
+ ATF_CHECK_EQ(changelist[0].fflags & NOTE_LINK, NOTE_LINK);
+}
+ATF_TC_CLEANUP(dir_note_link_delete_dir_in, tc)
+{
+ cleanup();
+}
+
+ATF_TC_WITH_CLEANUP(dir_note_link_mv_dir_in);
+ATF_TC_HEAD(dir_note_link_mv_dir_in, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "This test case ensures "
+ "that kevent(2) returns NOTE_LINK for the directory "
+ "'foo' if a directory 'bar' is renamed to 'foo/bar'.");
+}
+ATF_TC_BODY(dir_note_link_mv_dir_in, tc)
+{
+ struct kevent changelist[1];
+
+ ATF_REQUIRE(init_target() != -1);
+ ATF_REQUIRE(mkdir(dir_outside, S_IRWXU) != -1);
+ ATF_REQUIRE(init_kqueue() != -1);
+
+ ATF_REQUIRE(rename(dir_outside, dir_inside1) != -1);
+ ATF_REQUIRE(kevent(kq, NULL, 0, changelist, 1, &ts) != -1);
+ ATF_CHECK_EQ(changelist[0].fflags & NOTE_LINK, NOTE_LINK);
+}
+ATF_TC_CLEANUP(dir_note_link_mv_dir_in, tc)
+{
+ cleanup();
+}
+
+ATF_TC_WITH_CLEANUP(dir_note_link_mv_dir_out);
+ATF_TC_HEAD(dir_note_link_mv_dir_out, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "This test case ensures "
+ "that kevent(2) returns NOTE_LINK for the directory "
+ "'foo' if a directory 'foo/bar' is renamed to 'bar'.");
+}
+ATF_TC_BODY(dir_note_link_mv_dir_out, tc)
+{
+ struct kevent changelist[1];
+
+ ATF_REQUIRE(init_target() != -1);
+ ATF_REQUIRE(mkdir(dir_inside1, S_IRWXU) != -1);
+ ATF_REQUIRE(init_kqueue() != -1);
+
+ ATF_REQUIRE(rename(dir_inside1, dir_outside) != -1);
+ ATF_REQUIRE(kevent(kq, NULL, 0, changelist, 1, &ts) != -1);
+ ATF_CHECK_EQ(changelist[0].fflags & NOTE_LINK, NOTE_LINK);
+}
+ATF_TC_CLEANUP(dir_note_link_mv_dir_out, tc)
+{
+ cleanup();
+}
+
+ATF_TC_WITH_CLEANUP(dir_note_write_create_dir_in);
+ATF_TC_HEAD(dir_note_write_create_dir_in, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "This test case ensures "
+ "that kevent(2) returns NOTE_WRITE for the directory "
+ "'foo' if a directory 'foo/bar' is created.");
+}
+ATF_TC_BODY(dir_note_write_create_dir_in, tc)
+{
+ struct kevent changelist[1];
+
+ ATF_REQUIRE(init_target() != -1);
+ ATF_REQUIRE(init_kqueue() != -1);
+
+ ATF_REQUIRE(mkdir(dir_inside1, S_IRWXU) != -1);
+ ATF_REQUIRE(kevent(kq, NULL, 0, changelist, 1, &ts) != -1);
+ ATF_CHECK_EQ(changelist[0].fflags & NOTE_WRITE, NOTE_WRITE);
+}
+ATF_TC_CLEANUP(dir_note_write_create_dir_in, tc)
+{
+ cleanup();
+}
+
+ATF_TC_WITH_CLEANUP(dir_note_write_create_file_in);
+ATF_TC_HEAD(dir_note_write_create_file_in, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "This test case ensures "
+ "that kevent(2) returns NOTE_WRITE for the directory "
+ "'foo' if a file 'foo/baz' is created.");
+}
+ATF_TC_BODY(dir_note_write_create_file_in, tc)
+{
+ struct kevent changelist[1];
+
+ ATF_REQUIRE(init_target() != -1);
+ ATF_REQUIRE(init_kqueue() != -1);
+
+ ATF_REQUIRE(create_file(file_inside1) != -1);
+ ATF_REQUIRE(kevent(kq, NULL, 0, changelist, 1, &ts) != -1);
+ ATF_CHECK_EQ(changelist[0].fflags & NOTE_WRITE, NOTE_WRITE);
+}
+ATF_TC_CLEANUP(dir_note_write_create_file_in, tc)
+{
+ cleanup();
+}
+
+ATF_TC_WITH_CLEANUP(dir_note_write_delete_dir_in);
+ATF_TC_HEAD(dir_note_write_delete_dir_in, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "This test case ensures "
+ "that kevent(2) returns NOTE_WRITE for the directory "
+ "'foo' if a directory 'foo/bar' is deleted.");
+}
+ATF_TC_BODY(dir_note_write_delete_dir_in, tc)
+{
+ struct kevent changelist[1];
+
+ ATF_REQUIRE(init_target() != -1);
+ ATF_REQUIRE(mkdir(dir_inside1, S_IRWXU) != -1);
+ ATF_REQUIRE(init_kqueue() != -1);
+
+ ATF_REQUIRE(rmdir(dir_inside1) != -1);
+ ATF_REQUIRE(kevent(kq, NULL, 0, changelist, 1, &ts) != -1);
+ ATF_CHECK_EQ(changelist[0].fflags & NOTE_WRITE, NOTE_WRITE);
+}
+ATF_TC_CLEANUP(dir_note_write_delete_dir_in, tc)
+{
+ cleanup();
+}
+
+ATF_TC_WITH_CLEANUP(dir_note_write_delete_file_in);
+ATF_TC_HEAD(dir_note_write_delete_file_in, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "This test case ensures "
+ "that kevent(2) returns NOTE_WRITE for the directory "
+ "'foo' if a file 'foo/baz' is deleted.");
+}
+ATF_TC_BODY(dir_note_write_delete_file_in, tc)
+{
+ struct kevent changelist[1];
+
+ ATF_REQUIRE(init_target() != -1);
+ ATF_REQUIRE(create_file(file_inside1) != -1);
+ ATF_REQUIRE(init_kqueue() != -1);
+
+ ATF_REQUIRE(unlink(file_inside1) != -1);
+ ATF_REQUIRE(kevent(kq, NULL, 0, changelist, 1, &ts) != -1);
+ ATF_CHECK_EQ(changelist[0].fflags & NOTE_WRITE, NOTE_WRITE);
+}
+ATF_TC_CLEANUP(dir_note_write_delete_file_in, tc)
+{
+ cleanup();
+}
+
+ATF_TC_WITH_CLEANUP(dir_note_write_mv_dir_in);
+ATF_TC_HEAD(dir_note_write_mv_dir_in, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "This test case ensures "
+ "that kevent(2) returns NOTE_WRITE for the directory "
+ "'foo' if a directory 'bar' is renamed to 'foo/bar'.");
+}
+ATF_TC_BODY(dir_note_write_mv_dir_in, tc)
+{
+ struct kevent changelist[1];
+
+ ATF_REQUIRE(init_target() != -1);
+ ATF_REQUIRE(mkdir(dir_outside, S_IRWXU) != -1);
+ ATF_REQUIRE(init_kqueue() != -1);
+
+ ATF_REQUIRE(rename(dir_outside, dir_inside1) != -1);
+ ATF_REQUIRE(kevent(kq, NULL, 0, changelist, 1, &ts) != -1);
+ ATF_CHECK_EQ(changelist[0].fflags & NOTE_WRITE, NOTE_WRITE);
+}
+ATF_TC_CLEANUP(dir_note_write_mv_dir_in, tc)
+{
+ cleanup();
+}
+
+ATF_TC_WITH_CLEANUP(dir_note_write_mv_dir_out);
+ATF_TC_HEAD(dir_note_write_mv_dir_out, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "This test case ensures "
+ "that kevent(2) returns NOTE_WRITE for the directory "
+ "'foo' if a directory 'foo/bar' is renamed to 'bar'.");
+}
+ATF_TC_BODY(dir_note_write_mv_dir_out, tc)
+{
+ struct kevent changelist[1];
+
+ ATF_REQUIRE(init_target() != -1);
+ ATF_REQUIRE(mkdir(dir_inside1, S_IRWXU) != -1);
+ ATF_REQUIRE(init_kqueue() != -1);
+
+ ATF_REQUIRE(rename(dir_inside1, dir_outside) != -1);
+ ATF_REQUIRE(kevent(kq, NULL, 0, changelist, 1, &ts) != -1);
+ ATF_CHECK_EQ(changelist[0].fflags & NOTE_WRITE, NOTE_WRITE);
+}
+ATF_TC_CLEANUP(dir_note_write_mv_dir_out, tc)
+{
+ cleanup();
+}
+
+ATF_TC_WITH_CLEANUP(dir_note_write_mv_dir_within);
+ATF_TC_HEAD(dir_note_write_mv_dir_within, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "This test case ensures "
+ "that kevent(2) returns NOTE_WRITE for the directory "
+ "'foo' if a directory 'foo/bar' is renamed to 'foo/baz'.");
+}
+ATF_TC_BODY(dir_note_write_mv_dir_within, tc)
+{
+ struct kevent changelist[1];
+
+ ATF_REQUIRE(init_target() != -1);
+ ATF_REQUIRE(mkdir(dir_inside1, S_IRWXU) != -1);
+ ATF_REQUIRE(init_kqueue() != -1);
+
+ ATF_REQUIRE(rename(dir_inside1, dir_inside2) != -1);
+ ATF_REQUIRE(kevent(kq, NULL, 0, changelist, 1, &ts) != -1);
+ ATF_CHECK_EQ(changelist[0].fflags & NOTE_WRITE, NOTE_WRITE);
+}
+ATF_TC_CLEANUP(dir_note_write_mv_dir_within, tc)
+{
+ cleanup();
+}
+
+ATF_TC_WITH_CLEANUP(dir_note_write_mv_file_in);
+ATF_TC_HEAD(dir_note_write_mv_file_in, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "This test case ensures "
+ "that kevent(2) returns NOTE_WRITE for the directory "
+ "'foo' if a file 'qux' is renamed to 'foo/baz'.");
+}
+ATF_TC_BODY(dir_note_write_mv_file_in, tc)
+{
+ struct kevent changelist[1];
+
+ ATF_REQUIRE(init_target() != -1);
+ ATF_REQUIRE(create_file(file_outside) != -1);
+ ATF_REQUIRE(init_kqueue() != -1);
+
+ ATF_REQUIRE(rename(file_outside, file_inside1) != -1);
+ ATF_REQUIRE(kevent(kq, NULL, 0, changelist, 1, &ts) != -1);
+ ATF_CHECK_EQ(changelist[0].fflags & NOTE_WRITE, NOTE_WRITE);
+}
+ATF_TC_CLEANUP(dir_note_write_mv_file_in, tc)
+{
+ cleanup();
+}
+
+ATF_TC_WITH_CLEANUP(dir_note_write_mv_file_out);
+ATF_TC_HEAD(dir_note_write_mv_file_out, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "This test case ensures "
+ "that kevent(2) returns NOTE_WRITE for the directory "
+ "'foo' if a file 'foo/baz' is renamed to 'qux'.");
+}
+ATF_TC_BODY(dir_note_write_mv_file_out, tc)
+{
+ struct kevent changelist[1];
+
+ ATF_REQUIRE(init_target() != -1);
+ ATF_REQUIRE(create_file(file_inside1) != -1);
+ ATF_REQUIRE(init_kqueue() != -1);
+
+ ATF_REQUIRE(rename(file_inside1, file_outside) != -1);
+ ATF_REQUIRE(kevent(kq, NULL, 0, changelist, 1, &ts) != -1);
+ ATF_CHECK_EQ(changelist[0].fflags & NOTE_WRITE, NOTE_WRITE);
+}
+ATF_TC_CLEANUP(dir_note_write_mv_file_out, tc)
+{
+ cleanup();
+}
+
+ATF_TC_WITH_CLEANUP(dir_note_write_mv_file_within);
+ATF_TC_HEAD(dir_note_write_mv_file_within, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "This test case ensures "
+ "that kevent(2) returns NOTE_WRITE for the directory "
+ "'foo' if a file 'foo/baz' is renamed to 'foo/qux'.");
+}
+ATF_TC_BODY(dir_note_write_mv_file_within, tc)
+{
+ struct kevent changelist[1];
+
+ ATF_REQUIRE(init_target() != -1);
+ ATF_REQUIRE(create_file(file_inside1) != -1);
+ ATF_REQUIRE(init_kqueue() != -1);
+
+ ATF_REQUIRE(rename(file_inside1, file_inside2) != -1);
+ ATF_REQUIRE(kevent(kq, NULL, 0, changelist, 1, &ts) != -1);
+ ATF_CHECK_EQ(changelist[0].fflags & NOTE_WRITE, NOTE_WRITE);
+}
+ATF_TC_CLEANUP(dir_note_write_mv_file_within, tc)
+{
+ cleanup();
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, dir_no_note_link_create_file_in);
+ ATF_TP_ADD_TC(tp, dir_no_note_link_delete_file_in);
+ ATF_TP_ADD_TC(tp, dir_no_note_link_mv_dir_within);
+ ATF_TP_ADD_TC(tp, dir_no_note_link_mv_file_within);
+ ATF_TP_ADD_TC(tp, dir_note_link_create_dir_in);
+ ATF_TP_ADD_TC(tp, dir_note_link_delete_dir_in);
+ ATF_TP_ADD_TC(tp, dir_note_link_mv_dir_in);
+ ATF_TP_ADD_TC(tp, dir_note_link_mv_dir_out);
+ ATF_TP_ADD_TC(tp, dir_note_write_create_dir_in);
+ ATF_TP_ADD_TC(tp, dir_note_write_create_file_in);
+ ATF_TP_ADD_TC(tp, dir_note_write_delete_dir_in);
+ ATF_TP_ADD_TC(tp, dir_note_write_delete_file_in);
+ ATF_TP_ADD_TC(tp, dir_note_write_mv_dir_in);
+ ATF_TP_ADD_TC(tp, dir_note_write_mv_dir_out);
+ ATF_TP_ADD_TC(tp, dir_note_write_mv_dir_within);
+ ATF_TP_ADD_TC(tp, dir_note_write_mv_file_in);
+ ATF_TP_ADD_TC(tp, dir_note_write_mv_file_out);
+ ATF_TP_ADD_TC(tp, dir_note_write_mv_file_within);
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/kqueue/write/t_fifo.c b/contrib/netbsd-tests/kernel/kqueue/write/t_fifo.c
new file mode 100644
index 000000000000..fe375b8fa943
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/kqueue/write/t_fifo.c
@@ -0,0 +1,102 @@
+/* $NetBSD: t_fifo.c,v 1.4 2017/01/13 21:30:41 christos Exp $ */
+
+/*-
+ * Copyright (c) 2002, 2008 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Luke Mewburn and Jaromir Dolecek.
+ *
+ * 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>
+__COPYRIGHT("@(#) Copyright (c) 2008\
+ The NetBSD Foundation, inc. All rights reserved.");
+__RCSID("$NetBSD: t_fifo.c,v 1.4 2017/01/13 21:30:41 christos Exp $");
+
+#include <sys/event.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+#include "h_macros.h"
+
+#define FIFONAME "fifo"
+
+ATF_TC(fifo);
+ATF_TC_HEAD(fifo, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Checks EVFILT_WRITE for fifo");
+}
+ATF_TC_BODY(fifo, tc)
+{
+ char buffer[128];
+ struct kevent event[1];
+ pid_t child;
+ int kq, n, fd, status;
+
+ RL(mkfifo(FIFONAME, 0644));
+ RL(fd = open(FIFONAME, O_RDWR, 0644));
+ RL(kq = kqueue());
+
+ /* spawn child reader */
+ RL(child = fork());
+ if (child == 0) {
+ int sz = read(fd, buffer, 128);
+ if (sz > 0)
+ (void)printf("fifo: child read '%.*s'\n", sz, buffer);
+ _exit(sz <= 0);
+ }
+
+ EV_SET(&event[0], fd, EVFILT_WRITE, EV_ADD|EV_ENABLE, 0, 0, 0);
+ RL(kevent(kq, event, 1, NULL, 0, NULL));
+
+ (void)memset(event, 0, sizeof(event));
+ RL(n = kevent(kq, NULL, 0, event, 1, NULL));
+
+ (void)printf("kevent num %d filt %d flags: %#x, fflags: %#x, "
+ "data: %" PRId64 "\n", n, event[0].filter, event[0].flags,
+ event[0].fflags, event[0].data);
+
+ ATF_REQUIRE_EQ(event[0].filter, EVFILT_WRITE);
+
+ RL(write(fd, "foo", 3));
+ (void)printf("fifo: wrote 'foo'\n");
+ RL(close(fd));
+
+ (void)waitpid(child, &status, 0);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, fifo);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/kqueue/write/t_pipe.c b/contrib/netbsd-tests/kernel/kqueue/write/t_pipe.c
new file mode 100644
index 000000000000..5b2ca8c22f5e
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/kqueue/write/t_pipe.c
@@ -0,0 +1,147 @@
+/* $NetBSD: t_pipe.c,v 1.2 2017/01/13 21:30:41 christos Exp $ */
+
+/*-
+ * Copyright (c) 2002 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Luke Mewburn and Jaromir Dolecek.
+ *
+ * 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>
+__COPYRIGHT("@(#) Copyright (c) 2008\
+ The NetBSD Foundation, inc. All rights reserved.");
+__RCSID("$NetBSD: t_pipe.c,v 1.2 2017/01/13 21:30:41 christos Exp $");
+
+#include <sys/event.h>
+#include <sys/wait.h>
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+#include "h_macros.h"
+
+ATF_TC(pipe1);
+ATF_TC_HEAD(pipe1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Checks EVFILT_WRITE for pipes. This test used to trigger "
+ "problem fixed in rev. 1.5.2.7 of sys/kern/sys_pipe.c");
+}
+ATF_TC_BODY(pipe1, tc)
+{
+ struct kevent event[1];
+ int fds[2];
+ int kq, n;
+
+ RL(pipe(fds));
+ RL(kq = kqueue());
+ RL(close(fds[0]));
+
+ EV_SET(&event[0], fds[1], EVFILT_WRITE, EV_ADD|EV_ENABLE, 0, 0, 0);
+ ATF_REQUIRE_EQ_MSG((n = kevent(kq, event, 1, NULL, 0, NULL)),
+ -1, "got: %d", n);
+ ATF_REQUIRE_EQ_MSG(errno, EBADF, "got: %s", strerror(errno));
+}
+
+ATF_TC(pipe2);
+ATF_TC_HEAD(pipe2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Checks EVFILT_WRITE for pipes. This test used to trigger problem "
+ "fixed in rev. 1.5.2.9 of sys/kern/sys_pipe.c");
+}
+ATF_TC_BODY(pipe2, tc)
+{
+ struct kevent event[1];
+ char buffer[128];
+ int fds[2];
+ int kq, n;
+ int status;
+ pid_t child;
+
+ RL(pipe(fds));
+ RL(kq = kqueue());
+
+ EV_SET(&event[0], fds[1], EVFILT_WRITE, EV_ADD|EV_ENABLE, 0, 0, 0);
+ RL(kevent(kq, event, 1, NULL, 0, NULL));
+
+ /* spawn child reader */
+ RL(child = fork());
+ if (child == 0) {
+ int sz = read(fds[0], buffer, 128);
+ if (sz > 0)
+ (void)printf("pipe: child read '%.*s'\n", sz, buffer);
+ exit(sz <= 0);
+ }
+
+ RL(n = kevent(kq, NULL, 0, event, 1, NULL));
+
+ (void)printf("kevent num %d flags: %#x, fflags: %#x, data: "
+ "%" PRId64 "\n", n, event[0].flags, event[0].fflags, event[0].data);
+
+ RL(n = write(fds[1], "foo", 3));
+ RL(close(fds[1]));
+
+ (void)waitpid(child, &status, 0);
+}
+
+ATF_TC(pipe3);
+ATF_TC_HEAD(pipe3, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Checks EVFILT_WRITE for pipes. This test used to trigger problem "
+ "fixed in rev. 1.5.2.10 of sys/kern/sys_pipe.c");
+}
+ATF_TC_BODY(pipe3, tc)
+{
+ struct kevent event[1];
+ int fds[2];
+ int kq;
+
+ RL(pipe(fds));
+ RL(kq = kqueue());
+
+ EV_SET(&event[0], fds[1], EVFILT_WRITE, EV_ADD|EV_ENABLE, 0, 0, 0);
+ RL(kevent(kq, event, 1, NULL, 0, NULL));
+
+ /* close 'read' end first, then 'write' */
+
+ RL(close(fds[0]));
+ RL(close(fds[1]));
+}
+
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, pipe1);
+ ATF_TP_ADD_TC(tp, pipe2);
+ ATF_TP_ADD_TC(tp, pipe3);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/kqueue/write/t_ttypty.c b/contrib/netbsd-tests/kernel/kqueue/write/t_ttypty.c
new file mode 100644
index 000000000000..45a67500f5b8
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/kqueue/write/t_ttypty.c
@@ -0,0 +1,130 @@
+/* $NetBSD: t_ttypty.c,v 1.2 2017/01/13 21:30:41 christos Exp $ */
+
+/*-
+ * Copyright (c) 2002, 2008 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Luke Mewburn and Jaromir Dolecek.
+ *
+ * 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>
+__COPYRIGHT("@(#) Copyright (c) 2008\
+ The NetBSD Foundation, inc. All rights reserved.");
+__RCSID("$NetBSD: t_ttypty.c,v 1.2 2017/01/13 21:30:41 christos Exp $");
+
+#include <sys/event.h>
+#include <sys/wait.h>
+
+#include <fcntl.h>
+#include <poll.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <termios.h>
+#include <unistd.h>
+#include <util.h>
+
+#include <atf-c.h>
+
+#include "h_macros.h"
+
+static void
+h_check(bool check_master)
+{
+ char slavetty[1024];
+ char buffer[128];
+ struct kevent event[1];
+ struct pollfd pfd;
+ pid_t child;
+ int status, kq, n;
+ int amaster, aslave, acurrent;
+
+ RL(openpty(&amaster, &aslave, slavetty, NULL, NULL));
+ (void)printf("tty: openpty master %d slave %d tty '%s'\n",
+ amaster, aslave, slavetty);
+ acurrent = check_master ? amaster : aslave;
+
+ RL(child = fork());
+ if (child == 0) {
+ (void)sleep(1);
+
+ n = read(check_master ? aslave : amaster, buffer, 128);
+ (void)printf("tty: child read '%.*s'\n", n, buffer);
+
+ _exit(0);
+ }
+
+ pfd.fd = acurrent;
+ pfd.events = POLLOUT;
+ (void)printf("tty: polling ...\n");
+ RL(poll(&pfd, 1, INFTIM));
+ (void)printf("tty: returned from poll - %d\n", pfd.revents);
+
+ RL(kq = kqueue());
+
+ EV_SET(&event[0], acurrent, EVFILT_WRITE, EV_ADD|EV_ENABLE, 0, 0, 0);
+ RL(kevent(kq, event, 1, NULL, 0, NULL));
+
+ RL(n = kevent(kq, NULL, 0, event, 1, NULL));
+
+ (void)printf("kevent num %d filt %d flags: %#x, fflags: %#x, data: "
+ "%" PRId64 "\n", n, event[0].filter, event[0].flags, event[0].fflags,
+ event[0].data);
+
+ ATF_REQUIRE_EQ(event[0].filter, EVFILT_WRITE);
+
+ RL(n = write(acurrent, "f00\n", 4));
+ (void)printf("tty: wrote 'f00\\n' (wrote %d characters)\n", n);
+
+ (void)waitpid(child, &status, 0);
+ (void)printf("tty: successful end\n");
+}
+
+ATF_TC(master);
+ATF_TC_HEAD(master, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Checks EVFILT_WRITE for master tty");
+}
+ATF_TC_BODY(master, tc)
+{
+ h_check(true);
+}
+
+ATF_TC(slave);
+ATF_TC_HEAD(slave, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Checks EVFILT_WRITE for slave tty");
+}
+ATF_TC_BODY(slave, tc)
+{
+ h_check(false);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, master);
+ ATF_TP_ADD_TC(tp, slave);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/msg.h b/contrib/netbsd-tests/kernel/msg.h
new file mode 100644
index 000000000000..547400ee73a6
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/msg.h
@@ -0,0 +1,136 @@
+/* $NetBSD: msg.h,v 1.1 2016/12/05 20:10:10 christos Exp $ */
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * 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.
+ */
+
+struct msg_fds {
+ int pfd[2];
+ int cfd[2];
+};
+
+#define CLOSEFD(fd) do { \
+ if (fd != -1) { \
+ close(fd); \
+ fd = -1; \
+ } \
+} while (/*CONSTCOND*/ 0)
+
+static int
+msg_open(struct msg_fds *fds)
+{
+ if (pipe(fds->pfd) == -1)
+ return -1;
+ if (pipe(fds->cfd) == -1) {
+ close(fds->pfd[0]);
+ close(fds->pfd[1]);
+ return -1;
+ }
+ return 0;
+}
+
+static void
+msg_close(struct msg_fds *fds)
+{
+ CLOSEFD(fds->pfd[0]);
+ CLOSEFD(fds->pfd[1]);
+ CLOSEFD(fds->cfd[0]);
+ CLOSEFD(fds->cfd[1]);
+}
+
+static int
+msg_write_child(const char *info, struct msg_fds *fds, void *msg, size_t len)
+{
+ ssize_t rv;
+ CLOSEFD(fds->cfd[1]);
+ CLOSEFD(fds->pfd[0]);
+
+ printf("Send %s\n", info);
+ rv = write(fds->pfd[1], msg, len);
+ if (rv != (ssize_t)len)
+ return 1;
+// printf("Wait %s\n", info);
+ rv = read(fds->cfd[0], msg, len);
+ if (rv != (ssize_t)len)
+ return 1;
+ return 0;
+}
+
+static int
+msg_write_parent(const char *info, struct msg_fds *fds, void *msg, size_t len)
+{
+ ssize_t rv;
+ CLOSEFD(fds->pfd[1]);
+ CLOSEFD(fds->cfd[0]);
+
+ printf("Send %s\n", info);
+ rv = write(fds->cfd[1], msg, len);
+ if (rv != (ssize_t)len)
+ return 1;
+// printf("Wait %s\n", info);
+ rv = read(fds->pfd[0], msg, len);
+ if (rv != (ssize_t)len)
+ return 1;
+ return 0;
+}
+
+static int
+msg_read_parent(const char *info, struct msg_fds *fds, void *msg, size_t len)
+{
+ ssize_t rv;
+ CLOSEFD(fds->pfd[1]);
+ CLOSEFD(fds->cfd[0]);
+
+ printf("Wait %s\n", info);
+ rv = read(fds->pfd[0], msg, len);
+ if (rv != (ssize_t)len)
+ return 1;
+// printf("Send %s\n", info);
+ rv = write(fds->cfd[1], msg, len);
+ if (rv != (ssize_t)len)
+ return 1;
+ return 0;
+}
+
+static int
+msg_read_child(const char *info, struct msg_fds *fds, void *msg, size_t len)
+{
+ ssize_t rv;
+ CLOSEFD(fds->cfd[1]);
+ CLOSEFD(fds->pfd[0]);
+
+ printf("Wait %s\n", info);
+ rv = read(fds->cfd[0], msg, len);
+ if (rv != (ssize_t)len)
+ return 1;
+// printf("Send %s\n", info);
+ rv = write(fds->pfd[1], msg, len);
+ if (rv != (ssize_t)len)
+ return 1;
+ return 0;
+}
diff --git a/contrib/netbsd-tests/kernel/t_extattrctl.c b/contrib/netbsd-tests/kernel/t_extattrctl.c
new file mode 100644
index 000000000000..2f8932e3c87e
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/t_extattrctl.c
@@ -0,0 +1,28 @@
+#include <sys/types.h>
+
+#include <rump/rump.h>
+#include <rump/rump_syscalls.h>
+
+#include <atf-c.h>
+
+ATF_TC(extattrctl_namei);
+ATF_TC_HEAD(extattrctl_namei, tc)
+{
+
+ atf_tc_set_md_var(tc, "descr", "extattrctl namei safety (kern/43328)");
+}
+
+ATF_TC_BODY(extattrctl_namei, tc)
+{
+
+ rump_init();
+
+ rump_sys_extattrctl("/anyfile", 0, "/", 0, 0);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, extattrctl_namei);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/t_extent.c b/contrib/netbsd-tests/kernel/t_extent.c
new file mode 100644
index 000000000000..57dfc2b6780f
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/t_extent.c
@@ -0,0 +1,385 @@
+/* $NetBSD: t_extent.c,v 1.5 2017/01/13 21:30:41 christos Exp $ */
+
+/*-
+ * Copyright (c) 2008 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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>
+__COPYRIGHT("@(#) Copyright (c) 2008\
+ The NetBSD Foundation, inc. All rights reserved.");
+__RCSID("$NetBSD: t_extent.c,v 1.5 2017/01/13 21:30:41 christos Exp $");
+
+#include <sys/types.h>
+#include <sys/queue.h>
+#include <sys/extent.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <atf-c.h>
+
+#include "h_macros.h"
+
+static int ret;
+static struct extent *ex;
+
+#define h_create(name, start, end, flags) \
+ ATF_REQUIRE((ex = extent_create(name, \
+ start, end, 0, 0, flags)) != NULL);
+
+#define h_alloc_region(start, size) \
+ ATF_REQUIRE_EQ_MSG(ret = extent_alloc_region(ex, \
+ start, size, 0), 0, "%s", strerror(ret));
+
+#define h_free(start, size) \
+ ATF_REQUIRE_EQ_MSG(ret = extent_free(ex, \
+ start, size, 0), 0, "%s", strerror(ret));
+
+static void
+h_alloc_subregion(u_long substart, u_long subend, u_long size,
+ u_long alignment, u_long boundary, int expret, u_long expres)
+{
+ u_long result;
+
+#define FAIL(fmt, ...) \
+ atf_tc_fail("extent_alloc_subregion1(ex, %#lx, %#lx, %#lx, %#lx, 0, " \
+ "%#lx, 0, &result): " fmt, substart, subend, size, alignment, \
+ boundary, ##__VA_ARGS__)
+
+ ret = extent_alloc_subregion1(ex, substart, subend, size,
+ alignment, 0, boundary, 0, &result);
+
+ if (ret != expret)
+ FAIL("%s", strerror(errno));
+
+ if (expret == 0 && result != expres)
+ FAIL("result should be: %#lx, got: %#lx", expres, result);
+#undef FAIL
+}
+
+static void
+h_require(const char *name, u_long start,
+ u_long end, int flags, const char *exp)
+{
+ char buf[4096];
+ struct extent_region *rp;
+ int n = 0;
+
+ ATF_REQUIRE_STREQ_MSG(ex->ex_name, name,
+ "expected: \"%s\", got: \"%s\"", name, ex->ex_name);
+ ATF_REQUIRE_EQ_MSG(ex->ex_start, start,
+ "expected: %#lx, got: %#lx", start, ex->ex_start);
+ ATF_REQUIRE_EQ_MSG(ex->ex_end, end,
+ "expected: %#lx, got: %#lx", end, ex->ex_end);
+ ATF_REQUIRE_EQ_MSG(ex->ex_flags, flags,
+ "expected: %#x, got: %#x", flags, ex->ex_flags);
+
+ (void)memset(buf, 0, sizeof(buf));
+ LIST_FOREACH(rp, &ex->ex_regions, er_link)
+ n += snprintf(buf + n, sizeof(buf) - n,
+ "0x%lx - 0x%lx\n", rp->er_start, rp->er_end);
+
+ if (strcmp(buf, exp) == 0)
+ return;
+
+ printf("Incorrect extent map\n");
+ printf("Expected:\n%s\n", exp);
+ printf("Got:\n%s\n", buf);
+ atf_tc_fail("incorrect extent map");
+}
+
+ATF_TC(coalesce);
+ATF_TC_HEAD(coalesce, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Checks coalescing of regions");
+}
+ATF_TC_BODY(coalesce, tc)
+{
+ h_create("test1", 0, 0x4f, 0);
+
+ h_alloc_region(0x00, 0x10);
+ h_alloc_region(0x20, 0x10);
+ h_alloc_region(0x40, 0x10);
+ h_alloc_region(0x10, 0x10);
+ h_alloc_subregion(0, 0x4f, 0x10, EX_NOALIGN, EX_NOBOUNDARY, 0, 0x30);
+
+ h_require("test1", 0x00, 0x4f, 0x00,
+ "0x0 - 0x4f\n");
+
+ extent_destroy(ex);
+}
+
+ATF_TC(subregion1);
+ATF_TC_HEAD(subregion1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Checks that subregions work (PR kern/7539)");
+}
+ATF_TC_BODY(subregion1, tc)
+{
+ h_create("test2", 0, 0x2f, EX_NOCOALESCE);
+
+ h_alloc_region(0x00, 0x10);
+ h_alloc_subregion(0x20, 0x30, 0x10, EX_NOALIGN, EX_NOBOUNDARY, 0, 0x20);
+
+ h_require("test2", 0x00, 0x2f, 0x2,
+ "0x0 - 0xf\n"
+ "0x20 - 0x2f\n");
+
+ extent_destroy(ex);
+}
+
+ATF_TC(subregion2);
+ATF_TC_HEAD(subregion2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Checks that subregion allocations don't overlap with existing "
+ "ones (fixed in 1.25)");
+}
+ATF_TC_BODY(subregion2, tc)
+{
+ h_create("test3", 0, 0x3f, EX_NOCOALESCE);
+
+ h_alloc_region(0x00, 0x20);
+ h_alloc_region(0x30, 0x10);
+ h_alloc_subregion(0x10, 0x3f, 0x10,
+ EX_NOALIGN, EX_NOBOUNDARY, 0, 0x20);
+
+ h_require("test3", 0x00, 0x3f, 0x2,
+ "0x0 - 0x1f\n"
+ "0x20 - 0x2f\n"
+ "0x30 - 0x3f\n");
+
+ extent_destroy(ex);
+}
+
+ATF_TC(bound1);
+ATF_TC_HEAD(bound1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Checks for overflow in boundary check, before an allocated region "
+ "(fixed in 1.32)");
+}
+ATF_TC_BODY(bound1, tc)
+{
+ h_create("test4", 0xf0000000, 0xffffffff, 0);
+
+ h_alloc_region(0xf1000000, 0x1);
+ h_alloc_subregion(0xf0000000, 0xffffffff, 0x1,
+ EX_NOALIGN, 0x20000000, 0, 0xf0000000);
+
+ h_require("test4", 0xf0000000, 0xffffffff, 0x0,
+ "0xf0000000 - 0xf0000000\n"
+ "0xf1000000 - 0xf1000000\n");
+
+ extent_destroy(ex);
+}
+
+ATF_TC(bound2);
+ATF_TC_HEAD(bound2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Checks for overflow in boundary checks, before the subregion end "
+ "(fixed in 1.32)");
+}
+ATF_TC_BODY(bound2, tc)
+{
+ h_create("test5", 0xf0000000, 0xffffffff, 0);
+
+ h_alloc_subregion(0xf0000000, 0xffffffff, 0x1,
+ EX_NOALIGN, 0x20000000, 0, 0xf0000000);
+
+ h_require("test5", 0xf0000000, 0xffffffff, 0x0,
+ "0xf0000000 - 0xf0000000\n");
+
+ extent_destroy(ex);
+}
+
+ATF_TC(bound3);
+ATF_TC_HEAD(bound3, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Checks allocation beyond last boundary line: last two "
+ "allocations should succeed without boundary \"fixups\"");
+}
+ATF_TC_BODY(bound3, tc)
+{
+ h_create("test6", 0, 11, 0);
+
+ h_alloc_subregion(0, 11, 8, EX_NOALIGN, 8, 0, 0);
+ h_alloc_subregion(0, 11, 2, EX_NOALIGN, 8, 0, 0x8);
+ h_alloc_subregion(0, 11, 2, EX_NOALIGN, 8, 0, 0xa);
+
+ h_require("test6", 0x0, 0xb, 0x0, "0x0 - 0xb\n");
+
+ extent_destroy(ex);
+}
+
+ATF_TC(bound4);
+ATF_TC_HEAD(bound4, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Checks allocation beyond last boundary line: last allocation "
+ "should be bumped to the next boundary and exactly fit the "
+ "remaining space");
+}
+ATF_TC_BODY(bound4, tc)
+{
+ h_create("test7", 0, 11, 0);
+
+ h_alloc_subregion(0, 11, 7, EX_NOALIGN, 8, 0, 0);
+ h_alloc_subregion(0, 11, 4, EX_NOALIGN, 8, 0, 8);
+
+ h_require("test7", 0x0, 0xb, 0x0,
+ "0x0 - 0x6\n"
+ "0x8 - 0xb\n");
+
+ extent_destroy(ex);
+}
+
+ATF_TC(subregion3);
+ATF_TC_HEAD(subregion3, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Checks that we don't allocate a region pasts the end of "
+ "subregion (i.e., the second alloc_subregion should fail). "
+ "subr_extent.c prior to rev. 1.43 allocated region starting "
+ "from 0x10");
+}
+ATF_TC_BODY(subregion3, tc)
+{
+ h_create("test8", 0, 0x4f, EX_NOCOALESCE);
+
+ h_alloc_region(0x30, 0x10);
+ h_alloc_subregion(0, 0xf, 0x10, EX_NOALIGN, EX_NOBOUNDARY, 0, 0);
+ h_alloc_subregion(0, 0xf, 0x10, EX_NOALIGN, EX_NOBOUNDARY, EAGAIN, 0);
+
+ h_require("test8", 0x0, 0x4f, 0x2,
+ "0x0 - 0xf\n"
+ "0x30 - 0x3f\n");
+
+ extent_destroy(ex);
+}
+
+ATF_TC(bound5);
+ATF_TC_HEAD(bound5, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "When allocating a region with a boundary constraint, checks "
+ "proper detection of overflaps once the candidate region has "
+ "been aligned. subr_extent.c prior 1.45 could corrupt the extent "
+ "map in this situation");
+}
+ATF_TC_BODY(bound5, tc)
+{
+ h_create("test9", 0, 0x4f, 0);
+
+ h_alloc_subregion(0, 0x10, 4, EX_NOALIGN, 0, 0, 0);
+ h_alloc_subregion(0xd, 0x20, 2, EX_NOALIGN, 0, 0, 0xd);
+ h_alloc_subregion(0, 0x4f, 8, EX_NOALIGN, 8, 0, 0x10);
+
+ h_require("test9", 0x0, 0x4f, 0x0,
+ "0x0 - 0x3\n"
+ "0xd - 0xe\n"
+ "0x10 - 0x17\n");
+
+ extent_destroy(ex);
+}
+
+ATF_TC(free);
+ATF_TC_HEAD(free, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Checks extent_free()");
+}
+ATF_TC_BODY(free, tc)
+{
+ h_create("test10", 0xc0002000, 0xffffe000, EX_BOUNDZERO);
+
+ h_alloc_subregion(0xc0002000, 0xffffe000, 0x2000,
+ 0x10000, 0x10000, 0, 0xc0010000);
+ h_alloc_subregion(0xc0002000, 0xffffe000, 0x2000,
+ 0x10000, 0x10000, 0, 0xc0020000);
+
+ h_require("test10", 0xc0002000, 0xffffe000, 0x0,
+ "0xc0010000 - 0xc0011fff\n"
+ "0xc0020000 - 0xc0021fff\n");
+
+ h_free(0xc0020000, 0x2000);
+ h_require("test10", 0xc0002000, 0xffffe000, 0x0,
+ "0xc0010000 - 0xc0011fff\n");
+
+ h_alloc_subregion(0xc0002000, 0xffffe000, 0x10000,
+ 0x10000, 0x10000, 0, 0xc0022000);
+
+ h_require("test10", 0xc0002000, 0xffffe000, 0x0,
+ "0xc0010000 - 0xc0011fff\n"
+ "0xc0022000 - 0xc0031fff\n");
+
+ extent_destroy(ex);
+}
+
+ATF_TC(subregion4);
+ATF_TC_HEAD(subregion4, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Checks for off-by-one bug which would cause a region at the end "
+ "of the extent to be allocated multiple times (fixed in 1.51)");
+}
+ATF_TC_BODY(subregion4, tc)
+{
+ h_create("test11", 0x10, 0x20, EX_NOCOALESCE);
+
+ h_alloc_subregion(0x10, 0x13, 0x4, EX_NOALIGN, EX_NOBOUNDARY, 0, 0x10);
+ h_alloc_subregion(0x1e, 0x1f, 0x2, EX_NOALIGN, EX_NOBOUNDARY, 0, 0x1e);
+ h_alloc_subregion(0x20, 0x20, 0x1, EX_NOALIGN, EX_NOBOUNDARY, 0, 0x20);
+ h_alloc_subregion(0x20, 0x20, 0x1, EX_NOALIGN, EX_NOBOUNDARY, EAGAIN, 0);
+ h_alloc_subregion(0x10, 0x20, 0x1, EX_NOALIGN, EX_NOBOUNDARY, 0, 0x14);
+
+ h_require("test11", 0x10, 0x20, 0x2,
+ "0x10 - 0x13\n"
+ "0x14 - 0x14\n"
+ "0x1e - 0x1f\n"
+ "0x20 - 0x20\n");
+
+ extent_destroy(ex);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, coalesce);
+ ATF_TP_ADD_TC(tp, subregion1);
+ ATF_TP_ADD_TC(tp, subregion2);
+ ATF_TP_ADD_TC(tp, bound1);
+ ATF_TP_ADD_TC(tp, bound2);
+ ATF_TP_ADD_TC(tp, bound3);
+ ATF_TP_ADD_TC(tp, bound4);
+ ATF_TP_ADD_TC(tp, subregion3);
+ ATF_TP_ADD_TC(tp, bound5);
+ ATF_TP_ADD_TC(tp, free);
+ ATF_TP_ADD_TC(tp, subregion4);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/t_filedesc.c b/contrib/netbsd-tests/kernel/t_filedesc.c
new file mode 100644
index 000000000000..a938d6f0b178
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/t_filedesc.c
@@ -0,0 +1,108 @@
+/* $NetBSD: t_filedesc.c,v 1.6 2017/01/13 21:30:41 christos Exp $ */
+
+/*-
+ * Copyright (c) 2010 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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_filedesc.c,v 1.6 2017/01/13 21:30:41 christos Exp $");
+
+#include <sys/types.h>
+
+#include <assert.h>
+#include <atf-c.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <pthread.h>
+
+#include <rump/rump.h>
+#include <rump/rump_syscalls.h>
+
+#include "h_macros.h"
+
+ATF_TC(getfilerace);
+ATF_TC_HEAD(getfilerace, tc)
+{
+
+ atf_tc_set_md_var(tc, "descr", "race between multithreaded proc. "
+ "fd_getfile() and fd_close() (PR kern/43694)");
+}
+
+static int fd;
+static volatile bool quit;
+
+static void *
+wrkwrk(void *arg)
+{
+
+ /* just something to cause fd_getfile() to be called */
+ while (!quit)
+ rump_sys_write(fd, &fd, sizeof(fd));
+
+ return NULL;
+}
+
+/* for me, 1000 triggers extremely seldom, 10k sometimes, 100k almost always */
+#define DEFAULT_ITERATIONS 10000
+
+ATF_TC_BODY(getfilerace, tc)
+{
+ pthread_t pt;
+ int fd_wrk;
+ int i, iters;
+
+ /*
+ * Want a multiprocessor virtual kernel. A multiprocessor host
+ * probably helps too, but that's harder to do in software...
+ */
+ setenv("RUMP_NCPU", "2", 1);
+ rump_init();
+
+ fd = fd_wrk = rump_sys_open("/dev/null", O_RDWR, 0);
+ if (fd == -1)
+ atf_tc_fail_errno("cannot open /dev/null");
+
+ if (atf_tc_has_config_var(tc, "iters"))
+ iters = atoi(atf_tc_get_config_var(tc, "iters"));
+ else
+ iters = DEFAULT_ITERATIONS;
+
+ pthread_create(&pt, NULL, wrkwrk, NULL);
+ for (i = 0; i < iters; i++) {
+ rump_sys_close(fd_wrk);
+ fd_wrk = rump_sys_open("/dev/null", O_RDWR, 0);
+ assert(fd == fd_wrk);
+ }
+
+ quit = true;
+ pthread_join(pt, NULL);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, getfilerace);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/t_kauth_pr_47598.c b/contrib/netbsd-tests/kernel/t_kauth_pr_47598.c
new file mode 100644
index 000000000000..f54b289974de
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/t_kauth_pr_47598.c
@@ -0,0 +1,169 @@
+/*-
+ * Copyright (c) 2013 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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>
+__COPYRIGHT("@(#) Copyright (c) 2013\
+ The NetBSD Foundation, inc. All rights reserved.");
+__RCSID("$NetBSD: t_kauth_pr_47598.c,v 1.3 2014/04/28 08:34:16 martin Exp $");
+
+#include <errno.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/sysctl.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <atf-c.h>
+
+/*
+ * helper function
+ */
+static const char curtain_name[] = "security.models.bsd44.curtain";
+static const char securelevel_name[] = "security.models.bsd44.securelevel";
+
+static bool may_lower_curtain(void);
+static int get_curtain(void);
+static void set_curtain(int newval);
+
+static bool
+may_lower_curtain(void)
+{
+ int seclevel;
+ size_t len = sizeof(seclevel);
+
+ if (sysctlbyname(securelevel_name, &seclevel, &len, NULL, 0) != 0)
+ atf_tc_fail("failed to read %s", securelevel_name);
+
+ return seclevel <= 0;
+}
+
+static int
+get_curtain(void)
+{
+ int curtain;
+ size_t len = sizeof(curtain);
+
+ if (sysctlbyname(curtain_name, &curtain, &len, NULL, 0) != 0)
+ atf_tc_fail("failed to read %s", curtain_name);
+
+ return curtain;
+}
+
+static void
+set_curtain(int newval)
+{
+
+ if (sysctlbyname(curtain_name, NULL, 0, &newval, sizeof(newval)) != 0)
+ atf_tc_fail("failed to set %s to %d", curtain_name, newval);
+}
+
+/*
+ * PR kern/47598: if security.models.extensions.curtain = 1 we crash when
+ * doing a netstat while an embryonic (not yet fully accepted) connection
+ * exists.
+ * This has been fixed with rev. 1.5 of
+ * src/sys/secmodel/extensions/secmodel_extensions.c.
+ */
+
+
+ATF_TC(kauth_curtain);
+ATF_TC_HEAD(kauth_curtain, tc)
+{
+ atf_tc_set_md_var(tc, "require.user", "root");
+ atf_tc_set_md_var(tc, "require.progs", "netstat");
+ atf_tc_set_md_var(tc, "descr",
+ "Checks for kernel crash with curtain active (PR kern/47598)");
+}
+
+ATF_TC_BODY(kauth_curtain, tc)
+{
+
+ int old_curtain, s, s2, err;
+ socklen_t slen;
+ struct sockaddr_in sa;
+
+ /*
+ * save old value of "curtain" and enable it
+ */
+ old_curtain = get_curtain();
+ if (old_curtain < 1 && !may_lower_curtain())
+ atf_tc_skip("curtain is not enabled and we would not be able"
+ " to drop it later due to securelevel settings");
+
+ set_curtain(1);
+
+ /*
+ * create a socket and bind it to some arbitray free port
+ */
+ s = socket(PF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0);
+ ATF_REQUIRE(s != -1);
+ memset(&sa, 0, sizeof(sa));
+ sa.sin_family = AF_INET;
+ sa.sin_len = sizeof(sa);
+ sa.sin_addr.s_addr = inet_addr("127.0.0.1");
+ ATF_REQUIRE(bind(s, (struct sockaddr *)&sa, sizeof(sa))==0);
+ ATF_REQUIRE(listen(s, 16)==0);
+
+ /*
+ * extract address and open a connection to the port
+ */
+ slen = sizeof(sa);
+ ATF_REQUIRE(getsockname(s, (struct sockaddr *)&sa, &slen)==0);
+ s2 = socket(PF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0);
+ ATF_REQUIRE(s2 != -1);
+ printf("port is %d\n", ntohs(sa.sin_port));
+ err = connect(s2, (struct sockaddr *)&sa, sizeof(sa));
+ ATF_REQUIRE_MSG(err == -1 && errno == EINPROGRESS,
+ "conect returned %d with errno %d", err, errno);
+ fflush(stdout);
+ fflush(stderr);
+
+ /*
+ * we now have a pending, not yet accepted connection - run netstat
+ */
+ system("netstat -aA");
+
+ /*
+ * cleanup
+ */
+ close(s2);
+ close(s);
+
+ /*
+ * restore old value of curtain
+ */
+ set_curtain(old_curtain);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, kauth_curtain);
+
+ return atf_no_error();
+}
+
+
diff --git a/contrib/netbsd-tests/kernel/t_lock.c b/contrib/netbsd-tests/kernel/t_lock.c
new file mode 100644
index 000000000000..bd83abe97933
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/t_lock.c
@@ -0,0 +1,87 @@
+/* $NetBSD: t_lock.c,v 1.2 2017/01/13 21:30:41 christos Exp $ */
+
+/*-
+ * Copyright (c) 2002, 2008 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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>
+__COPYRIGHT("@(#) Copyright (c) 2008\
+ The NetBSD Foundation, inc. All rights reserved.");
+__RCSID("$NetBSD: t_lock.c,v 1.2 2017/01/13 21:30:41 christos Exp $");
+
+#include <sys/time.h>
+
+#include <machine/lock.h>
+#include <signal.h>
+
+#include <atf-c.h>
+
+#include "h_macros.h"
+
+__cpu_simple_lock_t lk;
+volatile int handled = 0;
+
+static void
+handler(int sig)
+{
+ handled = 1;
+ __cpu_simple_unlock(&lk);
+}
+
+ATF_TC(lock);
+ATF_TC_HEAD(lock, tc)
+{
+ atf_tc_set_md_var(tc, "timeout", "3");
+ atf_tc_set_md_var(tc, "descr",
+ "Checks __cpu_simple_lock()/__cpu_simple_unlock()");
+}
+ATF_TC_BODY(lock, tc)
+{
+ struct itimerval itv;
+
+ __cpu_simple_lock_init(&lk);
+
+ REQUIRE_LIBC(signal(SIGVTALRM, handler), SIG_ERR);
+
+ itv.it_interval.tv_sec = 0;
+ itv.it_interval.tv_usec = 0;
+ itv.it_value.tv_sec = 1;
+ itv.it_value.tv_usec = 0;
+ RL(setitimer(ITIMER_VIRTUAL, &itv, NULL));
+
+ __cpu_simple_lock(&lk);
+ __cpu_simple_lock(&lk);
+
+ ATF_REQUIRE(handled);
+
+ __cpu_simple_unlock(&lk);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, lock);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/t_lockf.c b/contrib/netbsd-tests/kernel/t_lockf.c
new file mode 100644
index 000000000000..702e6121eb76
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/t_lockf.c
@@ -0,0 +1,265 @@
+/* $NetBSD: t_lockf.c,v 1.9 2013/10/19 17:45:00 christos Exp $ */
+
+/*-
+ * Copyright (c) 2000 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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 <atf-c.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/ptrace.h>
+
+/*
+ * lockf1 regression test:
+ *
+ * Tests:
+ * Fork N child processes, each of which gets M random byte range locks
+ * on a common file. We ignore all lock errors (practically speaking,
+ * this means EDEADLK or ENOLOCK), but we make numerous passes over all
+ * the children to make sure that they are still awake. (We do this by
+ * verifying that we can ptrace(ATTACH/DETACH) to the children and get
+ * their status via waitpid().)
+ * When finished, reap all the children.
+ */
+
+#define nlocks 500 /* number of locks per thread */
+#define nprocs 10 /* number of processes to spawn */
+#define npasses 50 /* number of passes to make over the children */
+#define sleeptime 150000 /* sleep time between locks, usec */
+#define filesize 8192 /* size of file to lock */
+
+const char *lockfile = "lockf_test";
+
+static u_int32_t
+random_uint32(void)
+{
+ return lrand48();
+}
+
+static void
+trylocks(int id)
+{
+ int i, fd;
+
+ srand48(getpid());
+
+ fd = open (lockfile, O_RDWR, 0);
+
+ if (fd < 0)
+ err(1, "%s", lockfile);
+
+ printf("%d: start\n", id);
+
+ for (i = 0; i < nlocks; i++) {
+ struct flock fl;
+
+ fl.l_start = random_uint32() % filesize;
+ fl.l_len = random_uint32() % filesize;
+ switch (random_uint32() % 3) {
+ case 0:
+ fl.l_type = F_RDLCK;
+ break;
+ case 1:
+ fl.l_type = F_WRLCK;
+ break;
+ case 2:
+ fl.l_type = F_UNLCK;
+ break;
+ }
+ fl.l_whence = SEEK_SET;
+
+ (void)fcntl(fd, F_SETLKW, &fl);
+
+ if (usleep(sleeptime) < 0)
+#if defined(__FreeBSD__)
+ if (errno != EINTR)
+#endif
+ err(1, "usleep");
+ }
+ printf("%d: done\n", id);
+ close (fd);
+}
+
+ATF_TC(randlock);
+ATF_TC_HEAD(randlock, tc)
+{
+
+ atf_tc_set_md_var(tc, "timeout", "300");
+ atf_tc_set_md_var(tc, "descr", "Checks fcntl(2) locking");
+}
+
+ATF_TC_BODY(randlock, tc)
+{
+ int i, j, fd;
+ int pipe_fd[2];
+ pid_t *pid;
+ int status;
+ char pipe_in, pipe_out;
+ const char pipe_errmsg[] = "child: pipe write failed\n";
+
+ (void)unlink(lockfile);
+
+ fd = open (lockfile, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0666);
+ ATF_REQUIRE_MSG(fd >= 0, "open(%s): %s", lockfile, strerror(errno));
+
+ ATF_REQUIRE_MSG(ftruncate(fd, filesize) >= 0,
+ "ftruncate(%s): %s", lockfile, strerror(errno));
+
+ ATF_REQUIRE_MSG(pipe(pipe_fd) == 0, "pipe: %s", strerror(errno));
+
+ fsync(fd);
+ close(fd);
+
+ pid = malloc(nprocs * sizeof(pid_t));
+
+ for (i = 0; i < nprocs; i++) {
+ pipe_out = (char)('A' + i);
+ pid[i] = fork();
+ switch (pid[i]) {
+ case 0:
+ if (write(pipe_fd[1], &pipe_out, 1) != 1)
+ write(STDERR_FILENO, pipe_errmsg,
+ __arraycount(pipe_errmsg) - 1);
+ else
+ trylocks(i);
+ _exit(0);
+ break;
+ case -1:
+ atf_tc_fail("fork %d failed", i);
+ break;
+ default:
+ ATF_REQUIRE_MSG(read(pipe_fd[0], &pipe_in, 1) == 1,
+ "parent: read_pipe(%i): %s", i, strerror(errno));
+ ATF_REQUIRE_MSG(pipe_in == pipe_out,
+ "parent: pipe does not match");
+ break;
+ }
+ }
+ for (j = 0; j < npasses; j++) {
+ printf("parent: run %i\n", j+1);
+ for (i = 0; i < nprocs; i++) {
+ ATF_REQUIRE_MSG(ptrace(PT_ATTACH, pid[i], 0, 0) >= 0,
+ "ptrace attach %d", pid[i]);
+ ATF_REQUIRE_MSG(waitpid(pid[i], &status, WUNTRACED) >= 0,
+ "waitpid(ptrace)");
+ usleep(sleeptime / 3);
+ ATF_REQUIRE_MSG(ptrace(PT_DETACH, pid[i], (caddr_t)1,
+ 0) >= 0,
+ "ptrace detach %d", pid[i]);
+ usleep(sleeptime / 3);
+ }
+ }
+ for (i = 0; i < nprocs; i++) {
+ printf("reap %d: ", i);
+ fflush(stdout);
+ kill(pid[i], SIGINT);
+ waitpid(pid[i], &status, 0);
+ printf(" status %d\n", status);
+ }
+ atf_tc_pass();
+}
+
+static int
+dolock(int fd, int op, off_t lk_off, off_t lk_size)
+{
+ off_t result;
+ int ret;
+
+ result = lseek(fd, lk_off, SEEK_SET);
+ if (result == -1) {
+ return errno;
+ }
+ ATF_REQUIRE_MSG(result == lk_off, "lseek to wrong offset");
+ ret = lockf(fd, op, lk_size);
+ if (ret == -1) {
+ return errno;
+ }
+ return 0;
+}
+
+ATF_TC(deadlock);
+ATF_TC_HEAD(deadlock, tc)
+{
+
+ atf_tc_set_md_var(tc, "timeout", "30");
+ atf_tc_set_md_var(tc, "descr", "Checks fcntl(2) deadlock detection");
+}
+
+ATF_TC_BODY(deadlock, tc)
+{
+ int fd;
+ int error;
+ int ret;
+ pid_t pid;
+
+ (void)unlink(lockfile);
+
+ fd = open (lockfile, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0666);
+ ATF_REQUIRE_MSG(fd >= 0, "open(%s): %s", lockfile, strerror(errno));
+
+ ATF_REQUIRE_MSG(ftruncate(fd, filesize) >= 0,
+ "ftruncate(%s): %s", lockfile, strerror(errno));
+
+ fsync(fd);
+
+ error = dolock(fd, F_LOCK, 0, 1);
+ ATF_REQUIRE_MSG(error == 0, "initial dolock: %s", strerror(errno));
+
+ pid = fork();
+ ATF_REQUIRE_MSG(pid != -1, "fork failed: %s", strerror(errno));
+ if (pid == 0) {
+ error = dolock(fd, F_LOCK, 1, 1);
+ ATF_REQUIRE_MSG(error == 0, "child dolock: %s",
+ strerror(errno));
+ dolock(fd, F_LOCK, 0, 1); /* will block */
+ atf_tc_fail("child did not block");
+ }
+ sleep(1); /* give child time to grab its lock then block */
+
+ error = dolock(fd, F_LOCK, 1, 1);
+ ATF_REQUIRE_MSG(error == EDEADLK, "parent did not detect deadlock: %s",
+ strerror(errno));
+ ret = kill(pid, SIGKILL);
+ ATF_REQUIRE_MSG(ret != -1, "failed to kill child: %s", strerror(errno));
+
+ atf_tc_pass();
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, randlock);
+ ATF_TP_ADD_TC(tp, deadlock);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/t_mqueue.c b/contrib/netbsd-tests/kernel/t_mqueue.c
new file mode 100644
index 000000000000..7edbbc9cc924
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/t_mqueue.c
@@ -0,0 +1,154 @@
+/* $NetBSD: t_mqueue.c,v 1.6 2017/01/14 20:57:24 christos Exp $ */
+
+/*
+ * Test for POSIX message queue priority handling.
+ *
+ * This file is in the Public Domain.
+ */
+
+#include <sys/stat.h>
+
+#include <atf-c.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <mqueue.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#ifdef __FreeBSD__
+#include "freebsd_test_suite/macros.h"
+#endif
+
+#define MQ_PRIO_BASE 24
+
+static void
+send_msgs(mqd_t mqfd)
+{
+ char msg[2];
+
+ msg[1] = '\0';
+
+ msg[0] = 'a';
+ ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE) != -1,
+ "mq_send 1 failed: %d", errno);
+
+ msg[0] = 'b';
+ ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE + 1) != -1,
+ "mq_send 2 failed: %d", errno);
+
+ msg[0] = 'c';
+ ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE) != -1,
+ "mq_send 3 failed: %d", errno);
+
+ msg[0] = 'd';
+ ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE - 1) != -1,
+ "mq_send 4 failed: %d", errno);
+
+ msg[0] = 'e';
+ ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), 0) != -1,
+ "mq_send 5 failed: %d", errno);
+
+ msg[0] = 'f';
+ ATF_REQUIRE_MSG(mq_send(mqfd, msg, sizeof(msg), MQ_PRIO_BASE + 1) != -1,
+ "mq_send 6 failed: %d", errno);
+}
+
+static void
+receive_msgs(mqd_t mqfd)
+{
+ struct mq_attr mqa;
+ char *m;
+ unsigned p;
+ int len;
+
+ ATF_REQUIRE_MSG(mq_getattr(mqfd, &mqa) != -1, "mq_getattr failed %d",
+ errno);
+
+ len = mqa.mq_msgsize;
+ m = calloc(1, len);
+ ATF_REQUIRE_MSG(m != NULL, "calloc failed");
+
+ ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
+ "mq_receive 1 failed: %d", errno);
+ ATF_REQUIRE_MSG(p == (MQ_PRIO_BASE + 1) && m[0] == 'b',
+ "mq_receive 1 prio/data mismatch");
+
+ ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
+ "mq_receive 2 failed: %d", errno);
+ ATF_REQUIRE_MSG(p == (MQ_PRIO_BASE + 1) && m[0] == 'f',
+ "mq_receive 2 prio/data mismatch");
+
+ ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
+ "mq_receive 3 failed: %d", errno);
+ ATF_REQUIRE_MSG(p == MQ_PRIO_BASE && m[0] == 'a',
+ "mq_receive 3 prio/data mismatch");
+
+ ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
+ "mq_receive 4 failed: %d", errno);
+ ATF_REQUIRE_MSG(p == MQ_PRIO_BASE && m[0] == 'c',
+ "mq_receive 4 prio/data mismatch");
+
+ ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
+ "mq_receive 5 failed: %d", errno);
+ ATF_REQUIRE_MSG(p == (MQ_PRIO_BASE - 1) && m[0] == 'd',
+ "mq_receive 5 prio/data mismatch");
+
+ ATF_REQUIRE_MSG(mq_receive(mqfd, m, len, &p) != -1,
+ "mq_receive 6 failed: %d", errno);
+ ATF_REQUIRE_MSG(p == 0 && m[0] == 'e',
+ "mq_receive 6 prio/data mismatch");
+}
+
+ATF_TC(mqueue);
+ATF_TC_HEAD(mqueue, tc)
+{
+
+ atf_tc_set_md_var(tc, "timeout", "3");
+ atf_tc_set_md_var(tc, "descr", "Checks mqueue send/receive");
+}
+
+ATF_TC_BODY(mqueue, tc)
+{
+ int status;
+ char *tmpdir;
+ char template[32];
+ char mq_name[64];
+
+#ifdef __FreeBSD__
+ ATF_REQUIRE_KERNEL_MODULE("mqueuefs");
+#endif
+
+ strlcpy(template, "./t_mqueue.XXXXXX", sizeof(template));
+ tmpdir = mkdtemp(template);
+ ATF_REQUIRE_MSG(tmpdir != NULL, "mkdtemp failed: %d", errno);
+#ifdef __FreeBSD__
+ snprintf(mq_name, sizeof(mq_name), "/t_mqueue");
+#else
+ snprintf(mq_name, sizeof(mq_name), "%s/mq", tmpdir);
+#endif
+
+ mqd_t mqfd;
+
+ mqfd = mq_open(mq_name, O_RDWR | O_CREAT,
+ S_IRUSR | S_IRWXG | S_IROTH, NULL);
+#ifdef __FreeBSD__
+ ATF_REQUIRE_MSG(mqfd != (mqd_t)-1, "mq_open failed: %d", errno);
+#else
+ ATF_REQUIRE_MSG(mqfd != -1, "mq_open failed: %d", errno);
+#endif
+
+ send_msgs(mqfd);
+ receive_msgs(mqfd);
+
+ status = mq_close(mqfd);
+ ATF_REQUIRE_MSG(status == 0, "mq_close failed: %d", errno);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, mqueue);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/t_ps_strings.sh b/contrib/netbsd-tests/kernel/t_ps_strings.sh
new file mode 100755
index 000000000000..8adba54a7861
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/t_ps_strings.sh
@@ -0,0 +1,85 @@
+# $NetBSD: t_ps_strings.sh,v 1.1 2011/03/05 18:14:33 pgoyette Exp $
+#
+# Copyright (c) 2008 The NetBSD Foundation, Inc.
+# All rights reserved.
+#
+# 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.
+#
+
+atf_test_case validate
+validate_head()
+{
+ atf_set "descr" "Validates ps_strings passed to program"
+}
+validate_body()
+{
+ atf_check -s exit:0 -o ignore -e ignore \
+ $(atf_get_srcdir)/h_ps_strings1
+}
+
+# Function to parse and validate the output from ps
+
+parse_ps() {
+ local pid seq arg
+
+ pid="$1" ; shift
+
+ while [ "$1" != "$pid" ] ; do
+ echo $1
+ shift
+ done
+ if [ $# -eq 0 ] ; then
+ echo "NO_PID"
+ return
+ fi
+ shift
+
+ seq=0
+ while [ $# -gt 1 ] ; do
+ arg=$(printf "arg%04x" $seq)
+ if [ "$arg" != "$1" ] ; then
+ echo BAD_$seq
+ return
+ fi
+ shift
+ done
+ echo "OK"
+}
+
+atf_test_case update
+update_head()
+{
+ atf_set "descr" "Check updating of ps_strings"
+}
+update_body()
+{
+ $(atf_get_srcdir)/h_ps_strings2 > /dev/null 2>&1 &
+ h_pid=$!
+ parse=$(parse_ps $h_pid $(ps -wwo pid,args -p $h_pid) )
+ kill $h_pid
+}
+
+atf_init_test_cases()
+{
+ atf_add_test_case validate
+ atf_add_test_case update
+}
diff --git a/contrib/netbsd-tests/kernel/t_ptrace.c b/contrib/netbsd-tests/kernel/t_ptrace.c
new file mode 100644
index 000000000000..dc0bbdb6e14b
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/t_ptrace.c
@@ -0,0 +1,208 @@
+/* $NetBSD: t_ptrace.c,v 1.18 2017/01/13 21:30:41 christos Exp $ */
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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_ptrace.c,v 1.18 2017/01/13 21:30:41 christos Exp $");
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/ptrace.h>
+#include <sys/stat.h>
+#include <sys/sysctl.h>
+#include <err.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+#include "h_macros.h"
+
+/*
+ * A child process cannot call atf functions and expect them to magically
+ * work like in the parent.
+ * The printf(3) messaging from a child will not work out of the box as well
+ * without estabilishing a communication protocol with its parent. To not
+ * overcomplicate the tests - do not log from a child and use err(3)/errx(3)
+ * wrapped with FORKEE_ASSERT()/FORKEE_ASSERTX() as that is guaranteed to work.
+ */
+#define FORKEE_ASSERTX(x) \
+do { \
+ int ret = (x); \
+ if (!ret) \
+ errx(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: %s", \
+ __FILE__, __LINE__, __func__, #x); \
+} while (0)
+
+#define FORKEE_ASSERT(x) \
+do { \
+ int ret = (x); \
+ if (!ret) \
+ err(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: %s", \
+ __FILE__, __LINE__, __func__, #x); \
+} while (0)
+
+ATF_TC(attach_pid0);
+ATF_TC_HEAD(attach_pid0, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Assert that a debugger cannot attach to PID 0");
+}
+
+ATF_TC_BODY(attach_pid0, tc)
+{
+ errno = 0;
+ ATF_REQUIRE_ERRNO(EPERM, ptrace(PT_ATTACH, 0, NULL, 0) == -1);
+}
+
+ATF_TC(attach_pid1);
+ATF_TC_HEAD(attach_pid1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Assert that a debugger cannot attach to PID 1 (as non-root)");
+
+ atf_tc_set_md_var(tc, "require.user", "unprivileged");
+}
+
+ATF_TC_BODY(attach_pid1, tc)
+{
+ ATF_REQUIRE_ERRNO(EPERM, ptrace(PT_ATTACH, 1, NULL, 0) == -1);
+}
+
+ATF_TC(attach_pid1_securelevel);
+ATF_TC_HEAD(attach_pid1_securelevel, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Assert that a debugger cannot attach to PID 1 with "
+ "securelevel >= 1 (as root)");
+
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(attach_pid1_securelevel, tc)
+{
+ int level;
+ size_t len = sizeof(level);
+
+ ATF_REQUIRE(sysctlbyname("kern.securelevel", &level, &len, NULL, 0)
+ != -1);
+
+ if (level < 1) {
+ atf_tc_skip("Test must be run with securelevel >= 1");
+ }
+
+ ATF_REQUIRE_ERRNO(EPERM, ptrace(PT_ATTACH, 1, NULL, 0) == -1);
+}
+
+ATF_TC(attach_self);
+ATF_TC_HEAD(attach_self, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Assert that a debugger cannot attach to self (as it's nonsense)");
+}
+
+ATF_TC_BODY(attach_self, tc)
+{
+ ATF_REQUIRE_ERRNO(EINVAL, ptrace(PT_ATTACH, getpid(), NULL, 0) == -1);
+}
+
+ATF_TC(attach_chroot);
+ATF_TC_HEAD(attach_chroot, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Assert that a debugger cannot trace another process unless the "
+ "process's root directory is at or below the tracing process's "
+ "root");
+
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(attach_chroot, tc)
+{
+ char buf[PATH_MAX];
+ pid_t child;
+ int fds_toparent[2], fds_fromparent[2];
+ int rv;
+ uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
+
+ (void)memset(buf, '\0', sizeof(buf));
+ ATF_REQUIRE(getcwd(buf, sizeof(buf)) != NULL);
+ (void)strlcat(buf, "/dir", sizeof(buf));
+
+ ATF_REQUIRE(mkdir(buf, 0500) == 0);
+ ATF_REQUIRE(chdir(buf) == 0);
+
+ ATF_REQUIRE(pipe(fds_toparent) == 0);
+ ATF_REQUIRE(pipe(fds_fromparent) == 0);
+ child = atf_utils_fork();
+ if (child == 0) {
+ FORKEE_ASSERT(close(fds_toparent[0]) == 0);
+ FORKEE_ASSERT(close(fds_fromparent[1]) == 0);
+
+ FORKEE_ASSERT(chroot(buf) == 0);
+
+ rv = write(fds_toparent[1], &msg, sizeof(msg));
+ FORKEE_ASSERTX(rv == sizeof(msg));
+
+ ATF_REQUIRE_ERRNO(EPERM,
+ ptrace(PT_ATTACH, getppid(), NULL, 0) == -1);
+
+ rv = read(fds_fromparent[0], &msg, sizeof(msg));
+ FORKEE_ASSERTX(rv == sizeof(msg));
+
+ _exit(0);
+ }
+ ATF_REQUIRE(close(fds_toparent[1]) == 0);
+ ATF_REQUIRE(close(fds_fromparent[0]) == 0);
+
+ printf("Waiting for chrooting of the child PID %d", child);
+ rv = read(fds_toparent[0], &msg, sizeof(msg));
+ ATF_REQUIRE(rv == sizeof(msg));
+
+ printf("Child is ready, it will try to PT_ATTACH to parent\n");
+ rv = write(fds_fromparent[1], &msg, sizeof(msg));
+ ATF_REQUIRE(rv == sizeof(msg));
+
+ printf("fds_fromparent is no longer needed - close it\n");
+ ATF_REQUIRE(close(fds_fromparent[1]) == 0);
+
+ printf("fds_toparent is no longer needed - close it\n");
+ ATF_REQUIRE(close(fds_toparent[0]) == 0);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ setvbuf(stdout, NULL, _IONBF, 0);
+ setvbuf(stderr, NULL, _IONBF, 0);
+ ATF_TP_ADD_TC(tp, attach_pid0);
+ ATF_TP_ADD_TC(tp, attach_pid1);
+ ATF_TP_ADD_TC(tp, attach_pid1_securelevel);
+ ATF_TP_ADD_TC(tp, attach_self);
+ ATF_TP_ADD_TC(tp, attach_chroot);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/t_ptrace_wait.c b/contrib/netbsd-tests/kernel/t_ptrace_wait.c
new file mode 100644
index 000000000000..9d08c0e9648d
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/t_ptrace_wait.c
@@ -0,0 +1,6700 @@
+/* $NetBSD: t_ptrace_wait.c,v 1.69 2017/01/27 16:43:07 kamil Exp $ */
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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_ptrace_wait.c,v 1.69 2017/01/27 16:43:07 kamil Exp $");
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/ptrace.h>
+#include <sys/resource.h>
+#include <sys/stat.h>
+#include <sys/sysctl.h>
+#include <sys/wait.h>
+#include <machine/reg.h>
+#include <elf.h>
+#include <err.h>
+#include <errno.h>
+#include <lwp.h>
+#include <signal.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+#include "h_macros.h"
+
+#include "t_ptrace_wait.h"
+#include "msg.h"
+
+#define PARENT_TO_CHILD(info, fds, msg) \
+ ATF_REQUIRE(msg_write_child(info " to child " # fds, &fds, &msg, sizeof(msg)) == 0)
+
+#define CHILD_FROM_PARENT(info, fds, msg) \
+ FORKEE_ASSERT(msg_read_parent(info " from parent " # fds, &fds, &msg, sizeof(msg)) == 0)
+
+#define CHILD_TO_PARENT(info, fds, msg) \
+ FORKEE_ASSERT(msg_write_parent(info " to parent " # fds, &fds, &msg, sizeof(msg)) == 0)
+
+#define PARENT_FROM_CHILD(info, fds, msg) \
+ ATF_REQUIRE(msg_read_child(info " from parent " # fds, &fds, &msg, sizeof(msg)) == 0)
+
+
+ATF_TC(traceme1);
+ATF_TC_HEAD(traceme1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify SIGSTOP followed by _exit(2) in a child");
+}
+
+ATF_TC_BODY(traceme1, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(traceme2);
+ATF_TC_HEAD(traceme2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify SIGSTOP followed by _exit(2) in a child");
+}
+
+static int traceme2_caught = 0;
+
+static void
+traceme2_sighandler(int sig)
+{
+ FORKEE_ASSERT_EQ(sig, SIGINT);
+
+ ++traceme2_caught;
+}
+
+ATF_TC_BODY(traceme2, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP, sigsent = SIGINT;
+ pid_t child, wpid;
+ struct sigaction sa;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ sa.sa_handler = traceme2_sighandler;
+ sa.sa_flags = SA_SIGINFO;
+ sigemptyset(&sa.sa_mask);
+
+ FORKEE_ASSERT(sigaction(sigsent, &sa, NULL) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT_EQ(traceme2_caught, 1);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Before resuming the child process where it left off and with "
+ "signal %s to be sent\n", strsignal(sigsent));
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the exited child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(traceme3);
+ATF_TC_HEAD(traceme3, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify SIGSTOP followed by termination by a signal in a child");
+}
+
+ATF_TC_BODY(traceme3, tc)
+{
+ const int sigval = SIGSTOP, sigsent = SIGINT /* Without core-dump */;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ /* NOTREACHED */
+ FORKEE_ASSERTX(0 &&
+ "Child should be terminated by a signal from its parent");
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Before resuming the child process where it left off and with "
+ "signal %s to be sent\n", strsignal(sigsent));
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_signaled(status, sigsent, 0);
+
+ printf("Before calling %s() for the exited child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(traceme4);
+ATF_TC_HEAD(traceme4, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify SIGSTOP followed by SIGCONT and _exit(2) in a child");
+}
+
+ATF_TC_BODY(traceme4, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP, sigsent = SIGCONT;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before raising %s from child\n", strsignal(sigsent));
+ FORKEE_ASSERT(raise(sigsent) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(),child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigsent);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the exited child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+#if defined(TWAIT_HAVE_PID)
+ATF_TC(attach1);
+ATF_TC_HEAD(attach1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Assert that tracer sees process termination before the parent");
+}
+
+ATF_TC_BODY(attach1, tc)
+{
+ struct msg_fds parent_tracee, parent_tracer;
+ const int exitval_tracee = 5;
+ const int exitval_tracer = 10;
+ pid_t tracee, tracer, wpid;
+ uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Spawn tracee\n");
+ ATF_REQUIRE(msg_open(&parent_tracee) == 0);
+ tracee = atf_utils_fork();
+ if (tracee == 0) {
+ // Wait for parent to let us exit
+ CHILD_FROM_PARENT("exit tracee", parent_tracee, msg);
+ _exit(exitval_tracee);
+ }
+
+ printf("Spawn debugger\n");
+ ATF_REQUIRE(msg_open(&parent_tracer) == 0);
+ tracer = atf_utils_fork();
+ if (tracer == 0) {
+ printf("Before calling PT_ATTACH from tracee %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
+
+ /* Wait for tracee and assert that it was stopped w/ SIGSTOP */
+ FORKEE_REQUIRE_SUCCESS(
+ wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
+
+ forkee_status_stopped(status, SIGSTOP);
+
+ /* Resume tracee with PT_CONTINUE */
+ FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
+
+ /* Inform parent that tracer has attached to tracee */
+ CHILD_TO_PARENT("tracer ready", parent_tracer, msg);
+
+ /* Wait for parent to tell use that tracee should have exited */
+ CHILD_FROM_PARENT("wait for tracee exit", parent_tracer, msg);
+
+ /* Wait for tracee and assert that it exited */
+ FORKEE_REQUIRE_SUCCESS(
+ wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
+
+ forkee_status_exited(status, exitval_tracee);
+ printf("Tracee %d exited with %d\n", tracee, exitval_tracee);
+
+ printf("Before exiting of the tracer process\n");
+ _exit(exitval_tracer);
+ }
+
+ printf("Wait for the tracer to attach to the tracee\n");
+ PARENT_FROM_CHILD("tracer ready", parent_tracer, msg);
+
+ printf("Resume the tracee and let it exit\n");
+ PARENT_TO_CHILD("exit tracee", parent_tracee, msg);
+
+ printf("Detect that tracee is zombie\n");
+ await_zombie(tracee);
+
+
+ printf("Assert that there is no status about tracee %d - "
+ "Tracer must detect zombie first - calling %s()\n", tracee,
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(
+ wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
+
+ printf("Tell the tracer child should have exited\n");
+ PARENT_TO_CHILD("wait for tracee exit", parent_tracer, msg);
+ printf("Wait for tracer to finish its job and exit - calling %s()\n",
+ TWAIT_FNAME);
+
+ printf("Wait from tracer child to complete waiting for tracee\n");
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
+ tracer);
+
+ validate_status_exited(status, exitval_tracer);
+
+ printf("Wait for tracee to finish its job and exit - calling %s()\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG),
+ tracee);
+
+ validate_status_exited(status, exitval_tracee);
+
+ msg_close(&parent_tracer);
+ msg_close(&parent_tracee);
+}
+#endif
+
+#if defined(TWAIT_HAVE_PID)
+ATF_TC(attach2);
+ATF_TC_HEAD(attach2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Assert that any tracer sees process termination before its "
+ "parent");
+}
+
+ATF_TC_BODY(attach2, tc)
+{
+ struct msg_fds parent_tracer, parent_tracee;
+ const int exitval_tracee = 5;
+ const int exitval_tracer1 = 10, exitval_tracer2 = 20;
+ pid_t tracee, tracer, wpid;
+ uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Spawn tracee\n");
+ ATF_REQUIRE(msg_open(&parent_tracee) == 0);
+ tracee = atf_utils_fork();
+ if (tracee == 0) {
+ /* Wait for message from the parent */
+ CHILD_FROM_PARENT("Message 1", parent_tracee, msg);
+ _exit(exitval_tracee);
+ }
+
+ printf("Spawn debugger\n");
+ ATF_REQUIRE(msg_open(&parent_tracer) == 0);
+ tracer = atf_utils_fork();
+ if (tracer == 0) {
+ /* Fork again and drop parent to reattach to PID 1 */
+ tracer = atf_utils_fork();
+ if (tracer != 0)
+ _exit(exitval_tracer1);
+
+ printf("Before calling PT_ATTACH from tracee %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
+
+ /* Wait for tracee and assert that it was stopped w/ SIGSTOP */
+ FORKEE_REQUIRE_SUCCESS(
+ wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
+
+ forkee_status_stopped(status, SIGSTOP);
+
+ /* Resume tracee with PT_CONTINUE */
+ FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
+
+ /* Inform parent that tracer has attached to tracee */
+ CHILD_TO_PARENT("Message 1", parent_tracer, msg);
+ CHILD_FROM_PARENT("Message 2", parent_tracer, msg);
+
+ /* Wait for tracee and assert that it exited */
+ FORKEE_REQUIRE_SUCCESS(
+ wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
+
+ forkee_status_exited(status, exitval_tracee);
+
+ printf("Before exiting of the tracer process\n");
+ _exit(exitval_tracer2);
+ }
+ printf("Wait for the tracer process (direct child) to exit calling "
+ "%s()\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(
+ wpid = TWAIT_GENERIC(tracer, &status, 0), tracer);
+
+ validate_status_exited(status, exitval_tracer1);
+
+ printf("Wait for the non-exited tracee process with %s()\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(
+ wpid = TWAIT_GENERIC(tracee, NULL, WNOHANG), 0);
+
+ printf("Wait for the tracer to attach to the tracee\n");
+ PARENT_FROM_CHILD("Message 1", parent_tracer, msg);
+ printf("Resume the tracee and let it exit\n");
+ PARENT_TO_CHILD("Message 1", parent_tracee, msg);
+
+ printf("Detect that tracee is zombie\n");
+ await_zombie(tracee);
+
+ printf("Assert that there is no status about tracee - "
+ "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(
+ wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
+
+ printf("Resume the tracer and let it detect exited tracee\n");
+ PARENT_TO_CHILD("Message 2", parent_tracer, msg);
+
+ printf("Wait for tracee to finish its job and exit - calling %s()\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG),
+ tracee);
+
+ validate_status_exited(status, exitval_tracee);
+
+ msg_close(&parent_tracer);
+ msg_close(&parent_tracee);
+
+}
+#endif
+
+ATF_TC(attach3);
+ATF_TC_HEAD(attach3, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Assert that tracer parent can PT_ATTACH to its child");
+}
+
+ATF_TC_BODY(attach3, tc)
+{
+ struct msg_fds parent_tracee;
+ const int exitval_tracee = 5;
+ pid_t tracee, wpid;
+ uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Spawn tracee\n");
+ ATF_REQUIRE(msg_open(&parent_tracee) == 0);
+ tracee = atf_utils_fork();
+ if (tracee == 0) {
+ CHILD_FROM_PARENT("Message 1", parent_tracee, msg);
+ printf("Parent should now attach to tracee\n");
+
+ CHILD_FROM_PARENT("Message 2", parent_tracee, msg);
+ /* Wait for message from the parent */
+ _exit(exitval_tracee);
+ }
+ PARENT_TO_CHILD("Message 1", parent_tracee, msg);
+
+ printf("Before calling PT_ATTACH for tracee %d\n", tracee);
+ ATF_REQUIRE(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
+
+ printf("Wait for the stopped tracee process with %s()\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(
+ wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
+
+ validate_status_stopped(status, SIGSTOP);
+
+ printf("Resume tracee with PT_CONTINUE\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
+
+ printf("Let the tracee exit now\n");
+ PARENT_TO_CHILD("Message 2", parent_tracee, msg);
+
+ printf("Wait for tracee to exit with %s()\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(
+ wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
+
+ validate_status_exited(status, exitval_tracee);
+
+ printf("Before calling %s() for tracee\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD,
+ wpid = TWAIT_GENERIC(tracee, &status, 0));
+
+ msg_close(&parent_tracee);
+}
+
+ATF_TC(attach4);
+ATF_TC_HEAD(attach4, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Assert that tracer child can PT_ATTACH to its parent");
+}
+
+ATF_TC_BODY(attach4, tc)
+{
+ struct msg_fds parent_tracee;
+ const int exitval_tracer = 5;
+ pid_t tracer, wpid;
+ uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Spawn tracer\n");
+ ATF_REQUIRE(msg_open(&parent_tracee) == 0);
+ tracer = atf_utils_fork();
+ if (tracer == 0) {
+
+ /* Wait for message from the parent */
+ CHILD_FROM_PARENT("Message 1", parent_tracee, msg);
+
+ printf("Attach to parent PID %d with PT_ATTACH from child\n",
+ getppid());
+ FORKEE_ASSERT(ptrace(PT_ATTACH, getppid(), NULL, 0) != -1);
+
+ printf("Wait for the stopped parent process with %s()\n",
+ TWAIT_FNAME);
+ FORKEE_REQUIRE_SUCCESS(
+ wpid = TWAIT_GENERIC(getppid(), &status, 0), getppid());
+
+ forkee_status_stopped(status, SIGSTOP);
+
+ printf("Resume parent with PT_DETACH\n");
+ FORKEE_ASSERT(ptrace(PT_DETACH, getppid(), (void *)1, 0)
+ != -1);
+
+ /* Tell parent we are ready */
+ CHILD_TO_PARENT("Message 1", parent_tracee, msg);
+
+ _exit(exitval_tracer);
+ }
+
+ printf("Wait for the tracer to become ready\n");
+ PARENT_TO_CHILD("Message 1", parent_tracee, msg);
+ printf("Allow the tracer to exit now\n");
+ PARENT_FROM_CHILD("Message 1", parent_tracee, msg);
+
+ printf("Wait for tracer to exit with %s()\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(
+ wpid = TWAIT_GENERIC(tracer, &status, 0), tracer);
+
+ validate_status_exited(status, exitval_tracer);
+
+ printf("Before calling %s() for tracer\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD,
+ wpid = TWAIT_GENERIC(tracer, &status, 0));
+
+ msg_close(&parent_tracee);
+}
+
+#if defined(TWAIT_HAVE_PID)
+ATF_TC(attach5);
+ATF_TC_HEAD(attach5, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Assert that tracer sees its parent when attached to tracer "
+ "(check getppid(2))");
+}
+
+ATF_TC_BODY(attach5, tc)
+{
+ struct msg_fds parent_tracer, parent_tracee;
+ const int exitval_tracee = 5;
+ const int exitval_tracer = 10;
+ pid_t parent, tracee, tracer, wpid;
+ uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Spawn tracee\n");
+ ATF_REQUIRE(msg_open(&parent_tracer) == 0);
+ ATF_REQUIRE(msg_open(&parent_tracee) == 0);
+ tracee = atf_utils_fork();
+ if (tracee == 0) {
+ parent = getppid();
+
+ /* Emit message to the parent */
+ CHILD_TO_PARENT("tracee ready", parent_tracee, msg);
+ CHILD_FROM_PARENT("exit tracee", parent_tracee, msg);
+
+ FORKEE_ASSERT_EQ(parent, getppid());
+
+ _exit(exitval_tracee);
+ }
+ printf("Wait for child to record its parent identifier (pid)\n");
+ PARENT_FROM_CHILD("tracee ready", parent_tracee, msg);
+
+ printf("Spawn debugger\n");
+ tracer = atf_utils_fork();
+ if (tracer == 0) {
+ /* No IPC to communicate with the child */
+ printf("Before calling PT_ATTACH from tracee %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
+
+ /* Wait for tracee and assert that it was stopped w/ SIGSTOP */
+ FORKEE_REQUIRE_SUCCESS(
+ wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
+
+ forkee_status_stopped(status, SIGSTOP);
+
+ /* Resume tracee with PT_CONTINUE */
+ FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
+
+ /* Inform parent that tracer has attached to tracee */
+ CHILD_TO_PARENT("tracer ready", parent_tracer, msg);
+
+ /* Wait for parent to tell use that tracee should have exited */
+ CHILD_FROM_PARENT("wait for tracee exit", parent_tracer, msg);
+
+ /* Wait for tracee and assert that it exited */
+ FORKEE_REQUIRE_SUCCESS(
+ wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
+
+ forkee_status_exited(status, exitval_tracee);
+
+ printf("Before exiting of the tracer process\n");
+ _exit(exitval_tracer);
+ }
+
+ printf("Wait for the tracer to attach to the tracee\n");
+ PARENT_FROM_CHILD("tracer ready", parent_tracer, msg);
+
+ printf("Resume the tracee and let it exit\n");
+ PARENT_TO_CHILD("exit tracee", parent_tracee, msg);
+
+ printf("Detect that tracee is zombie\n");
+ await_zombie(tracee);
+
+ printf("Assert that there is no status about tracee - "
+ "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(
+ wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
+
+ printf("Tell the tracer child should have exited\n");
+ PARENT_TO_CHILD("wait for tracee exit", parent_tracer, msg);
+
+ printf("Wait from tracer child to complete waiting for tracee\n");
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
+ tracer);
+
+ validate_status_exited(status, exitval_tracer);
+
+ printf("Wait for tracee to finish its job and exit - calling %s()\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG),
+ tracee);
+
+ validate_status_exited(status, exitval_tracee);
+
+ msg_close(&parent_tracer);
+ msg_close(&parent_tracee);
+}
+#endif
+
+#if defined(TWAIT_HAVE_PID)
+ATF_TC(attach6);
+ATF_TC_HEAD(attach6, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Assert that tracer sees its parent when attached to tracer "
+ "(check sysctl(7) and struct kinfo_proc2)");
+}
+
+ATF_TC_BODY(attach6, tc)
+{
+ struct msg_fds parent_tracee, parent_tracer;
+ const int exitval_tracee = 5;
+ const int exitval_tracer = 10;
+ pid_t parent, tracee, tracer, wpid;
+ uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ int name[CTL_MAXNAME];
+ struct kinfo_proc2 kp;
+ size_t len = sizeof(kp);
+ unsigned int namelen;
+
+ printf("Spawn tracee\n");
+ ATF_REQUIRE(msg_open(&parent_tracee) == 0);
+ ATF_REQUIRE(msg_open(&parent_tracer) == 0);
+ tracee = atf_utils_fork();
+ if (tracee == 0) {
+ parent = getppid();
+
+ /* Emit message to the parent */
+ CHILD_TO_PARENT("Message 1", parent_tracee, msg);
+ CHILD_FROM_PARENT("Message 2", parent_tracee, msg);
+
+ namelen = 0;
+ name[namelen++] = CTL_KERN;
+ name[namelen++] = KERN_PROC2;
+ name[namelen++] = KERN_PROC_PID;
+ name[namelen++] = getpid();
+ name[namelen++] = len;
+ name[namelen++] = 1;
+
+ FORKEE_ASSERT(sysctl(name, namelen, &kp, &len, NULL, 0) == 0);
+ FORKEE_ASSERT_EQ(parent, kp.p_ppid);
+
+ _exit(exitval_tracee);
+ }
+
+ printf("Wait for child to record its parent identifier (pid)\n");
+ PARENT_FROM_CHILD("Message 1", parent_tracee, msg);
+
+ printf("Spawn debugger\n");
+ tracer = atf_utils_fork();
+ if (tracer == 0) {
+ /* No IPC to communicate with the child */
+ printf("Before calling PT_ATTACH from tracee %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
+
+ /* Wait for tracee and assert that it was stopped w/ SIGSTOP */
+ FORKEE_REQUIRE_SUCCESS(
+ wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
+
+ forkee_status_stopped(status, SIGSTOP);
+
+ /* Resume tracee with PT_CONTINUE */
+ FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
+
+ /* Inform parent that tracer has attached to tracee */
+ CHILD_TO_PARENT("Message 1", parent_tracer, msg);
+
+ CHILD_FROM_PARENT("Message 2", parent_tracer, msg);
+
+ /* Wait for tracee and assert that it exited */
+ FORKEE_REQUIRE_SUCCESS(
+ wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
+
+ forkee_status_exited(status, exitval_tracee);
+
+ printf("Before exiting of the tracer process\n");
+ _exit(exitval_tracer);
+ }
+
+ printf("Wait for the tracer to attach to the tracee\n");
+ PARENT_FROM_CHILD("Message 1", parent_tracer, msg);
+
+ printf("Resume the tracee and let it exit\n");
+ PARENT_TO_CHILD("Message 1", parent_tracee, msg);
+
+ printf("Detect that tracee is zombie\n");
+ await_zombie(tracee);
+
+ printf("Assert that there is no status about tracee - "
+ "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(
+ wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
+
+ printf("Resume the tracer and let it detect exited tracee\n");
+ PARENT_TO_CHILD("Message 2", parent_tracer, msg);
+
+ printf("Wait for tracer to finish its job and exit - calling %s()\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
+ tracer);
+
+ validate_status_exited(status, exitval_tracer);
+
+ printf("Wait for tracee to finish its job and exit - calling %s()\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG),
+ tracee);
+
+ validate_status_exited(status, exitval_tracee);
+
+ msg_close(&parent_tracee);
+ msg_close(&parent_tracer);
+}
+#endif
+
+#if defined(TWAIT_HAVE_PID)
+ATF_TC(attach7);
+ATF_TC_HEAD(attach7, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Assert that tracer sees its parent when attached to tracer "
+ "(check /proc/curproc/status 3rd column)");
+}
+
+ATF_TC_BODY(attach7, tc)
+{
+ struct msg_fds parent_tracee, parent_tracer;
+ int rv;
+ const int exitval_tracee = 5;
+ const int exitval_tracer = 10;
+ pid_t parent, tracee, tracer, wpid;
+ uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ FILE *fp;
+ struct stat st;
+ const char *fname = "/proc/curproc/status";
+ char s_executable[MAXPATHLEN];
+ int s_pid, s_ppid;
+ /*
+ * Format:
+ * EXECUTABLE PID PPID ...
+ */
+
+ ATF_REQUIRE((rv = stat(fname, &st)) == 0 || (errno == ENOENT));
+ if (rv != 0) {
+ atf_tc_skip("/proc/curproc/status not found");
+ }
+
+ printf("Spawn tracee\n");
+ ATF_REQUIRE(msg_open(&parent_tracee) == 0);
+ ATF_REQUIRE(msg_open(&parent_tracer) == 0);
+ tracee = atf_utils_fork();
+ if (tracee == 0) {
+ parent = getppid();
+
+ // Wait for parent to let us exit
+ CHILD_TO_PARENT("tracee ready", parent_tracee, msg);
+ CHILD_FROM_PARENT("tracee exit", parent_tracee, msg);
+
+ FORKEE_ASSERT((fp = fopen(fname, "r")) != NULL);
+ fscanf(fp, "%s %d %d", s_executable, &s_pid, &s_ppid);
+ FORKEE_ASSERT(fclose(fp) == 0);
+ FORKEE_ASSERT_EQ(parent, s_ppid);
+
+ _exit(exitval_tracee);
+ }
+
+ printf("Wait for child to record its parent identifier (pid)\n");
+ PARENT_FROM_CHILD("tracee ready", parent_tracee, msg);
+
+ printf("Spawn debugger\n");
+ tracer = atf_utils_fork();
+ if (tracer == 0) {
+ printf("Before calling PT_ATTACH from tracee %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
+
+ /* Wait for tracee and assert that it was stopped w/ SIGSTOP */
+ FORKEE_REQUIRE_SUCCESS(
+ wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
+
+ forkee_status_stopped(status, SIGSTOP);
+
+ /* Resume tracee with PT_CONTINUE */
+ FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
+
+ /* Inform parent that tracer has attached to tracee */
+ CHILD_TO_PARENT("tracer ready", parent_tracer, msg);
+
+ /* Wait for parent to tell use that tracee should have exited */
+ CHILD_FROM_PARENT("wait for tracee exit", parent_tracer, msg);
+
+ /* Wait for tracee and assert that it exited */
+ FORKEE_REQUIRE_SUCCESS(
+ wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
+
+ forkee_status_exited(status, exitval_tracee);
+
+ printf("Before exiting of the tracer process\n");
+ _exit(exitval_tracer);
+ }
+ printf("Wait for the tracer to attach to the tracee\n");
+ PARENT_FROM_CHILD("tracer ready", parent_tracer, msg);
+ printf("Resume the tracee and let it exit\n");
+ PARENT_TO_CHILD("tracee exit", parent_tracee, msg);
+
+ printf("Detect that tracee is zombie\n");
+ await_zombie(tracee);
+
+ printf("Assert that there is no status about tracee - "
+ "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(
+ wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
+
+ printf("Resume the tracer and let it detect exited tracee\n");
+ PARENT_TO_CHILD("Message 2", parent_tracer, msg);
+
+ printf("Wait for tracer to finish its job and exit - calling %s()\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
+ tracer);
+
+ validate_status_exited(status, exitval_tracer);
+
+ printf("Wait for tracee to finish its job and exit - calling %s()\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG),
+ tracee);
+
+ validate_status_exited(status, exitval_tracee);
+
+ msg_close(&parent_tracee);
+ msg_close(&parent_tracer);
+}
+#endif
+
+ATF_TC(eventmask1);
+ATF_TC_HEAD(eventmask1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that empty EVENT_MASK is preserved");
+}
+
+ATF_TC_BODY(eventmask1, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ ptrace_event_t set_event, get_event;
+ const int len = sizeof(ptrace_event_t);
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ set_event.pe_set_event = 0;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &set_event, len) != -1);
+ ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, child, &get_event, len) != -1);
+ ATF_REQUIRE(memcmp(&set_event, &get_event, len) == 0);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(eventmask2);
+ATF_TC_HEAD(eventmask2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that PTRACE_FORK in EVENT_MASK is preserved");
+}
+
+ATF_TC_BODY(eventmask2, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ ptrace_event_t set_event, get_event;
+ const int len = sizeof(ptrace_event_t);
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ set_event.pe_set_event = PTRACE_FORK;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &set_event, len) != -1);
+ ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, child, &get_event, len) != -1);
+ ATF_REQUIRE(memcmp(&set_event, &get_event, len) == 0);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(eventmask3);
+ATF_TC_HEAD(eventmask3, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that PTRACE_VFORK in EVENT_MASK is preserved");
+}
+
+ATF_TC_BODY(eventmask3, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ ptrace_event_t set_event, get_event;
+ const int len = sizeof(ptrace_event_t);
+
+ atf_tc_expect_fail("PR kern/51630");
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ set_event.pe_set_event = PTRACE_VFORK;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &set_event, len) != -1);
+ ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, child, &get_event, len) != -1);
+ ATF_REQUIRE(memcmp(&set_event, &get_event, len) == 0);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(eventmask4);
+ATF_TC_HEAD(eventmask4, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that PTRACE_VFORK_DONE in EVENT_MASK is preserved");
+}
+
+ATF_TC_BODY(eventmask4, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ ptrace_event_t set_event, get_event;
+ const int len = sizeof(ptrace_event_t);
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ set_event.pe_set_event = PTRACE_VFORK_DONE;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &set_event, len) != -1);
+ ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, child, &get_event, len) != -1);
+ ATF_REQUIRE(memcmp(&set_event, &get_event, len) == 0);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(eventmask5);
+ATF_TC_HEAD(eventmask5, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that PTRACE_LWP_CREATE in EVENT_MASK is preserved");
+}
+
+ATF_TC_BODY(eventmask5, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ ptrace_event_t set_event, get_event;
+ const int len = sizeof(ptrace_event_t);
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ set_event.pe_set_event = PTRACE_LWP_CREATE;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &set_event, len) != -1);
+ ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, child, &get_event, len) != -1);
+ ATF_REQUIRE(memcmp(&set_event, &get_event, len) == 0);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(eventmask6);
+ATF_TC_HEAD(eventmask6, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that PTRACE_LWP_EXIT in EVENT_MASK is preserved");
+}
+
+ATF_TC_BODY(eventmask6, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ ptrace_event_t set_event, get_event;
+ const int len = sizeof(ptrace_event_t);
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ set_event.pe_set_event = PTRACE_LWP_EXIT;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &set_event, len) != -1);
+ ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, child, &get_event, len) != -1);
+ ATF_REQUIRE(memcmp(&set_event, &get_event, len) == 0);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+#if defined(TWAIT_HAVE_PID)
+ATF_TC(fork1);
+ATF_TC_HEAD(fork1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that fork(2) is intercepted by ptrace(2) with EVENT_MASK "
+ "set to PTRACE_FORK");
+}
+
+ATF_TC_BODY(fork1, tc)
+{
+ const int exitval = 5;
+ const int exitval2 = 15;
+ const int sigval = SIGSTOP;
+ pid_t child, child2, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ ptrace_state_t state;
+ const int slen = sizeof(state);
+ ptrace_event_t event;
+ const int elen = sizeof(event);
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT((child2 = fork()) != 1);
+
+ if (child2 == 0)
+ _exit(exitval2);
+
+ FORKEE_REQUIRE_SUCCESS
+ (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
+
+ forkee_status_exited(status, exitval2);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Enable PTRACE_FORK in EVENT_MASK for the child %d\n", child);
+ event.pe_set_event = PTRACE_FORK;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child %d\n", TWAIT_FNAME, child);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
+ ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
+
+ child2 = state.pe_other_pid;
+ printf("Reported PTRACE_FORK event with forkee %d\n", child2);
+
+ printf("Before calling %s() for the forkee %d of the child %d\n",
+ TWAIT_FNAME, child2, child);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
+ child2);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
+ ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
+ ATF_REQUIRE_EQ(state.pe_other_pid, child);
+
+ printf("Before resuming the forkee process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the forkee - expected exited\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
+ child2);
+
+ validate_status_exited(status, exitval2);
+
+ printf("Before calling %s() for the forkee - expected no process\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD,
+ wpid = TWAIT_GENERIC(child2, &status, 0));
+
+ printf("Before calling %s() for the child - expected stopped "
+ "SIGCHLD\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGCHLD);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child - expected exited\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child - expected no process\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+ATF_TC(fork2);
+ATF_TC_HEAD(fork2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that fork(2) is not intercepted by ptrace(2) with empty "
+ "EVENT_MASK");
+}
+
+ATF_TC_BODY(fork2, tc)
+{
+ const int exitval = 5;
+ const int exitval2 = 15;
+ const int sigval = SIGSTOP;
+ pid_t child, child2, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ ptrace_event_t event;
+ const int elen = sizeof(event);
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT((child2 = fork()) != 1);
+
+ if (child2 == 0)
+ _exit(exitval2);
+
+ FORKEE_REQUIRE_SUCCESS
+ (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
+
+ forkee_status_exited(status, exitval2);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Set empty EVENT_MASK for the child %d\n", child);
+ event.pe_set_event = 0;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child - expected stopped "
+ "SIGCHLD\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGCHLD);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child - expected exited\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child - expected no process\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+#if defined(TWAIT_HAVE_PID)
+ATF_TC(vfork1);
+ATF_TC_HEAD(vfork1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that vfork(2) is intercepted by ptrace(2) with EVENT_MASK "
+ "set to PTRACE_VFORK");
+}
+
+ATF_TC_BODY(vfork1, tc)
+{
+ const int exitval = 5;
+ const int exitval2 = 15;
+ const int sigval = SIGSTOP;
+ pid_t child, child2, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ ptrace_state_t state;
+ const int slen = sizeof(state);
+ ptrace_event_t event;
+ const int elen = sizeof(event);
+
+ atf_tc_expect_fail("PR kern/51630");
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT((child2 = vfork()) != 1);
+
+ if (child2 == 0)
+ _exit(exitval2);
+
+ FORKEE_REQUIRE_SUCCESS
+ (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
+
+ forkee_status_exited(status, exitval2);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Enable PTRACE_VFORK in EVENT_MASK for the child %d\n", child);
+ event.pe_set_event = PTRACE_VFORK;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child %d\n", TWAIT_FNAME, child);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
+ ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK);
+
+ child2 = state.pe_other_pid;
+ printf("Reported PTRACE_VFORK event with forkee %d\n", child2);
+
+ printf("Before calling %s() for the forkee %d of the child %d\n",
+ TWAIT_FNAME, child2, child);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
+ child2);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
+ ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK);
+ ATF_REQUIRE_EQ(state.pe_other_pid, child);
+
+ printf("Before resuming the forkee process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the forkee - expected exited\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
+ child2);
+
+ validate_status_exited(status, exitval2);
+
+ printf("Before calling %s() for the forkee - expected no process\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD,
+ wpid = TWAIT_GENERIC(child2, &status, 0));
+
+ printf("Before calling %s() for the child - expected stopped "
+ "SIGCHLD\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGCHLD);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child - expected exited\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child - expected no process\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+ATF_TC(vfork2);
+ATF_TC_HEAD(vfork2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that vfork(2) is not intercepted by ptrace(2) with empty "
+ "EVENT_MASK");
+}
+
+ATF_TC_BODY(vfork2, tc)
+{
+ const int exitval = 5;
+ const int exitval2 = 15;
+ const int sigval = SIGSTOP;
+ pid_t child, child2, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ ptrace_event_t event;
+ const int elen = sizeof(event);
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT((child2 = vfork()) != 1);
+
+ if (child2 == 0)
+ _exit(exitval2);
+
+ FORKEE_REQUIRE_SUCCESS
+ (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
+
+ forkee_status_exited(status, exitval2);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Set empty EVENT_MASK for the child %d\n", child);
+ event.pe_set_event = 0;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child - expected stopped "
+ "SIGCHLD\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGCHLD);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child - expected exited\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child - expected no process\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(vforkdone1);
+ATF_TC_HEAD(vforkdone1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that vfork(2) is intercepted by ptrace(2) with EVENT_MASK "
+ "set to PTRACE_VFORK_DONE");
+}
+
+ATF_TC_BODY(vforkdone1, tc)
+{
+ const int exitval = 5;
+ const int exitval2 = 15;
+ const int sigval = SIGSTOP;
+ pid_t child, child2, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ ptrace_state_t state;
+ const int slen = sizeof(state);
+ ptrace_event_t event;
+ const int elen = sizeof(event);
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT((child2 = vfork()) != 1);
+
+ if (child2 == 0)
+ _exit(exitval2);
+
+ FORKEE_REQUIRE_SUCCESS
+ (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
+
+ forkee_status_exited(status, exitval2);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Enable PTRACE_VFORK_DONE in EVENT_MASK for the child %d\n",
+ child);
+ event.pe_set_event = PTRACE_VFORK_DONE;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child %d\n", TWAIT_FNAME, child);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
+ ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK_DONE);
+
+ child2 = state.pe_other_pid;
+ printf("Reported PTRACE_VFORK_DONE event with forkee %d\n", child2);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child - expected stopped "
+ "SIGCHLD\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGCHLD);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child - expected exited\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child - expected no process\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(vforkdone2);
+ATF_TC_HEAD(vforkdone2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that vfork(2) is intercepted by ptrace(2) with EVENT_MASK "
+ "set to PTRACE_FORK | PTRACE_VFORK_DONE");
+}
+
+ATF_TC_BODY(vforkdone2, tc)
+{
+ const int exitval = 5;
+ const int exitval2 = 15;
+ const int sigval = SIGSTOP;
+ pid_t child, child2, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ ptrace_state_t state;
+ const int slen = sizeof(state);
+ ptrace_event_t event;
+ const int elen = sizeof(event);
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT((child2 = vfork()) != 1);
+
+ if (child2 == 0)
+ _exit(exitval2);
+
+ FORKEE_REQUIRE_SUCCESS
+ (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
+
+ forkee_status_exited(status, exitval2);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Enable PTRACE_VFORK in EVENT_MASK for the child %d\n", child);
+ event.pe_set_event = PTRACE_FORK | PTRACE_VFORK_DONE;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child %d\n", TWAIT_FNAME, child);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
+ ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK_DONE);
+
+ child2 = state.pe_other_pid;
+ printf("Reported PTRACE_VFORK_DONE event with forkee %d\n", child2);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child - expected stopped "
+ "SIGCHLD\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGCHLD);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child - expected exited\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child - expected no process\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(io_read_d1);
+ATF_TC_HEAD(io_read_d1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_IO with PIOD_READ_D and len = sizeof(uint8_t)");
+}
+
+ATF_TC_BODY(io_read_d1, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ uint8_t lookup_me = 0;
+ const uint8_t magic = 0xab;
+ struct ptrace_io_desc io = {
+ .piod_op = PIOD_READ_D,
+ .piod_offs = &lookup_me,
+ .piod_addr = &lookup_me,
+ .piod_len = sizeof(lookup_me)
+ };
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ lookup_me = magic;
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Read lookup_me from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me, magic,
+ "got value %" PRIx8 " != expected %" PRIx8, lookup_me, magic);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(io_read_d2);
+ATF_TC_HEAD(io_read_d2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_IO with PIOD_READ_D and len = sizeof(uint16_t)");
+}
+
+ATF_TC_BODY(io_read_d2, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ uint16_t lookup_me = 0;
+ const uint16_t magic = 0x1234;
+ struct ptrace_io_desc io = {
+ .piod_op = PIOD_READ_D,
+ .piod_offs = &lookup_me,
+ .piod_addr = &lookup_me,
+ .piod_len = sizeof(lookup_me)
+ };
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ lookup_me = magic;
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Read lookup_me from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me, magic,
+ "got value %" PRIx16 " != expected %" PRIx16, lookup_me, magic);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(io_read_d3);
+ATF_TC_HEAD(io_read_d3, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_IO with PIOD_READ_D and len = sizeof(uint32_t)");
+}
+
+ATF_TC_BODY(io_read_d3, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ uint32_t lookup_me = 0;
+ const uint32_t magic = 0x1234abcd;
+ struct ptrace_io_desc io = {
+ .piod_op = PIOD_READ_D,
+ .piod_offs = &lookup_me,
+ .piod_addr = &lookup_me,
+ .piod_len = sizeof(lookup_me)
+ };
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ lookup_me = magic;
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Read lookup_me from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me, magic,
+ "got value %" PRIx32 " != expected %" PRIx32, lookup_me, magic);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(io_read_d4);
+ATF_TC_HEAD(io_read_d4, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_IO with PIOD_READ_D and len = sizeof(uint64_t)");
+}
+
+ATF_TC_BODY(io_read_d4, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ uint64_t lookup_me = 0;
+ const uint64_t magic = 0x1234abcd9876dcfa;
+ struct ptrace_io_desc io = {
+ .piod_op = PIOD_READ_D,
+ .piod_offs = &lookup_me,
+ .piod_addr = &lookup_me,
+ .piod_len = sizeof(lookup_me)
+ };
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ lookup_me = magic;
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Read lookup_me from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me, magic,
+ "got value %" PRIx64 " != expected %" PRIx64, lookup_me, magic);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(io_write_d1);
+ATF_TC_HEAD(io_write_d1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_IO with PIOD_WRITE_D and len = sizeof(uint8_t)");
+}
+
+ATF_TC_BODY(io_write_d1, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ uint8_t lookup_me = 0;
+ const uint8_t magic = 0xab;
+ struct ptrace_io_desc io = {
+ .piod_op = PIOD_WRITE_D,
+ .piod_offs = &lookup_me,
+ .piod_addr = &lookup_me,
+ .piod_len = sizeof(lookup_me)
+ };
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT_EQ(lookup_me, magic);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ lookup_me = magic;
+
+ printf("Write new lookup_me to tracee (PID=%d) from tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(io_write_d2);
+ATF_TC_HEAD(io_write_d2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_IO with PIOD_WRITE_D and len = sizeof(uint16_t)");
+}
+
+ATF_TC_BODY(io_write_d2, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ uint16_t lookup_me = 0;
+ const uint16_t magic = 0xab12;
+ struct ptrace_io_desc io = {
+ .piod_op = PIOD_WRITE_D,
+ .piod_offs = &lookup_me,
+ .piod_addr = &lookup_me,
+ .piod_len = sizeof(lookup_me)
+ };
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT_EQ(lookup_me, magic);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ lookup_me = magic;
+
+ printf("Write new lookup_me to tracee (PID=%d) from tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(io_write_d3);
+ATF_TC_HEAD(io_write_d3, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_IO with PIOD_WRITE_D and len = sizeof(uint32_t)");
+}
+
+ATF_TC_BODY(io_write_d3, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ uint32_t lookup_me = 0;
+ const uint32_t magic = 0xab127643;
+ struct ptrace_io_desc io = {
+ .piod_op = PIOD_WRITE_D,
+ .piod_offs = &lookup_me,
+ .piod_addr = &lookup_me,
+ .piod_len = sizeof(lookup_me)
+ };
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT_EQ(lookup_me, magic);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ lookup_me = magic;
+
+ printf("Write new lookup_me to tracee (PID=%d) from tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(io_write_d4);
+ATF_TC_HEAD(io_write_d4, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_IO with PIOD_WRITE_D and len = sizeof(uint64_t)");
+}
+
+ATF_TC_BODY(io_write_d4, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ uint64_t lookup_me = 0;
+ const uint64_t magic = 0xab12764376490123;
+ struct ptrace_io_desc io = {
+ .piod_op = PIOD_WRITE_D,
+ .piod_offs = &lookup_me,
+ .piod_addr = &lookup_me,
+ .piod_len = sizeof(lookup_me)
+ };
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT_EQ(lookup_me, magic);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ lookup_me = magic;
+
+ printf("Write new lookup_me to tracee (PID=%d) from tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(io_read_auxv1);
+ATF_TC_HEAD(io_read_auxv1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_READ_AUXV called for tracee");
+}
+
+ATF_TC_BODY(io_read_auxv1, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ AuxInfo ai[100], *aip;
+ struct ptrace_io_desc io = {
+ .piod_op = PIOD_READ_AUXV,
+ .piod_offs = 0,
+ .piod_addr = ai,
+ .piod_len = sizeof(ai)
+ };
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Read new AUXV from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
+
+ printf("Asserting that AUXV length (%zu) is > 0\n", io.piod_len);
+ ATF_REQUIRE(io.piod_len > 0);
+
+ for (aip = ai; aip->a_type != AT_NULL; aip++)
+ printf("a_type=%#llx a_v=%#llx\n",
+ (long long int)aip->a_type, (long long int)aip->a_v);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(read_d1);
+ATF_TC_HEAD(read_d1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_READ_D called once");
+}
+
+ATF_TC_BODY(read_d1, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ int lookup_me = 0;
+ const int magic = (int)random();
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ lookup_me = magic;
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Read new lookup_me from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ errno = 0;
+ lookup_me = ptrace(PT_READ_D, child, &lookup_me, 0);
+ ATF_REQUIRE_EQ(errno, 0);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me, magic,
+ "got value %#x != expected %#x", lookup_me, magic);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(read_d2);
+ATF_TC_HEAD(read_d2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_READ_D called twice");
+}
+
+ATF_TC_BODY(read_d2, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ int lookup_me1 = 0;
+ int lookup_me2 = 0;
+ const int magic1 = (int)random();
+ const int magic2 = (int)random();
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ lookup_me1 = magic1;
+ lookup_me2 = magic2;
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Read new lookup_me1 from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ errno = 0;
+ lookup_me1 = ptrace(PT_READ_D, child, &lookup_me1, 0);
+ ATF_REQUIRE_EQ(errno, 0);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me1, magic1,
+ "got value %#x != expected %#x", lookup_me1, magic1);
+
+ printf("Read new lookup_me2 from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ errno = 0;
+ lookup_me2 = ptrace(PT_READ_D, child, &lookup_me2, 0);
+ ATF_REQUIRE_EQ(errno, 0);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me2, magic2,
+ "got value %#x != expected %#x", lookup_me2, magic2);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(read_d3);
+ATF_TC_HEAD(read_d3, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_READ_D called three times");
+}
+
+ATF_TC_BODY(read_d3, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ int lookup_me1 = 0;
+ int lookup_me2 = 0;
+ int lookup_me3 = 0;
+ const int magic1 = (int)random();
+ const int magic2 = (int)random();
+ const int magic3 = (int)random();
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ lookup_me1 = magic1;
+ lookup_me2 = magic2;
+ lookup_me3 = magic3;
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Read new lookup_me1 from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ errno = 0;
+ lookup_me1 = ptrace(PT_READ_D, child, &lookup_me1, 0);
+ ATF_REQUIRE_EQ(errno, 0);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me1, magic1,
+ "got value %#x != expected %#x", lookup_me1, magic1);
+
+ printf("Read new lookup_me2 from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ errno = 0;
+ lookup_me2 = ptrace(PT_READ_D, child, &lookup_me2, 0);
+ ATF_REQUIRE_EQ(errno, 0);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me2, magic2,
+ "got value %#x != expected %#x", lookup_me2, magic2);
+
+ printf("Read new lookup_me3 from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ errno = 0;
+ lookup_me3 = ptrace(PT_READ_D, child, &lookup_me3, 0);
+ ATF_REQUIRE_EQ(errno, 0);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me3, magic3,
+ "got value %#x != expected %#x", lookup_me3, magic3);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(read_d4);
+ATF_TC_HEAD(read_d4, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_READ_D called four times");
+}
+
+ATF_TC_BODY(read_d4, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ int lookup_me1 = 0;
+ int lookup_me2 = 0;
+ int lookup_me3 = 0;
+ int lookup_me4 = 0;
+ const int magic1 = (int)random();
+ const int magic2 = (int)random();
+ const int magic3 = (int)random();
+ const int magic4 = (int)random();
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ lookup_me1 = magic1;
+ lookup_me2 = magic2;
+ lookup_me3 = magic3;
+ lookup_me4 = magic4;
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Read new lookup_me1 from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ errno = 0;
+ lookup_me1 = ptrace(PT_READ_D, child, &lookup_me1, 0);
+ ATF_REQUIRE_EQ(errno, 0);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me1, magic1,
+ "got value %#x != expected %#x", lookup_me1, magic1);
+
+ printf("Read new lookup_me2 from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ errno = 0;
+ lookup_me2 = ptrace(PT_READ_D, child, &lookup_me2, 0);
+ ATF_REQUIRE_EQ(errno, 0);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me2, magic2,
+ "got value %#x != expected %#x", lookup_me2, magic2);
+
+ printf("Read new lookup_me3 from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ errno = 0;
+ lookup_me3 = ptrace(PT_READ_D, child, &lookup_me3, 0);
+ ATF_REQUIRE_EQ(errno, 0);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me3, magic3,
+ "got value %#x != expected %#x", lookup_me3, magic3);
+
+ printf("Read new lookup_me4 from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ errno = 0;
+ lookup_me4 = ptrace(PT_READ_D, child, &lookup_me4, 0);
+ ATF_REQUIRE_EQ(errno, 0);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me4, magic4,
+ "got value %#x != expected %#x", lookup_me4, magic4);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(write_d1);
+ATF_TC_HEAD(write_d1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_WRITE_D called once");
+}
+
+ATF_TC_BODY(write_d1, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ int lookup_me = 0;
+ const int magic = (int)random();
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT_EQ(lookup_me, magic);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Write new lookup_me to tracee (PID=%d) from tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me, magic) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(write_d2);
+ATF_TC_HEAD(write_d2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_WRITE_D called twice");
+}
+
+ATF_TC_BODY(write_d2, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ int lookup_me1 = 0;
+ int lookup_me2 = 0;
+ const int magic1 = (int)random();
+ const int magic2 = (int)random();
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT_EQ(lookup_me1, magic1);
+ FORKEE_ASSERT_EQ(lookup_me2, magic2);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Write new lookup_me1 to tracee (PID=%d) from tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me1, magic1) != -1);
+
+ printf("Write new lookup_me2 to tracee (PID=%d) from tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me2, magic2) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(write_d3);
+ATF_TC_HEAD(write_d3, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_WRITE_D called three times");
+}
+
+ATF_TC_BODY(write_d3, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ int lookup_me1 = 0;
+ int lookup_me2 = 0;
+ int lookup_me3 = 0;
+ const int magic1 = (int)random();
+ const int magic2 = (int)random();
+ const int magic3 = (int)random();
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT_EQ(lookup_me1, magic1);
+ FORKEE_ASSERT_EQ(lookup_me2, magic2);
+ FORKEE_ASSERT_EQ(lookup_me3, magic3);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Write new lookup_me1 to tracee (PID=%d) from tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me1, magic1) != -1);
+
+ printf("Write new lookup_me2 to tracee (PID=%d) from tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me2, magic2) != -1);
+
+ printf("Write new lookup_me3 to tracee (PID=%d) from tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me3, magic3) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(write_d4);
+ATF_TC_HEAD(write_d4, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_WRITE_D called four times");
+}
+
+ATF_TC_BODY(write_d4, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ int lookup_me1 = 0;
+ int lookup_me2 = 0;
+ int lookup_me3 = 0;
+ int lookup_me4 = 0;
+ const int magic1 = (int)random();
+ const int magic2 = (int)random();
+ const int magic3 = (int)random();
+ const int magic4 = (int)random();
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT_EQ(lookup_me1, magic1);
+ FORKEE_ASSERT_EQ(lookup_me2, magic2);
+ FORKEE_ASSERT_EQ(lookup_me3, magic3);
+ FORKEE_ASSERT_EQ(lookup_me4, magic4);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Write new lookup_me1 to tracee (PID=%d) from tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me1, magic1) != -1);
+
+ printf("Write new lookup_me2 to tracee (PID=%d) from tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me2, magic2) != -1);
+
+ printf("Write new lookup_me3 to tracee (PID=%d) from tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me3, magic3) != -1);
+
+ printf("Write new lookup_me4 to tracee (PID=%d) from tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me4, magic4) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(io_read_d_write_d_handshake1);
+ATF_TC_HEAD(io_read_d_write_d_handshake1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_IO with PIOD_READ_D and PIOD_WRITE_D handshake");
+}
+
+ATF_TC_BODY(io_read_d_write_d_handshake1, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ uint8_t lookup_me_fromtracee = 0;
+ const uint8_t magic_fromtracee = (uint8_t)random();
+ uint8_t lookup_me_totracee = 0;
+ const uint8_t magic_totracee = (uint8_t)random();
+ struct ptrace_io_desc io_fromtracee = {
+ .piod_op = PIOD_READ_D,
+ .piod_offs = &lookup_me_fromtracee,
+ .piod_addr = &lookup_me_fromtracee,
+ .piod_len = sizeof(lookup_me_fromtracee)
+ };
+ struct ptrace_io_desc io_totracee = {
+ .piod_op = PIOD_WRITE_D,
+ .piod_offs = &lookup_me_totracee,
+ .piod_addr = &lookup_me_totracee,
+ .piod_len = sizeof(lookup_me_totracee)
+ };
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ lookup_me_fromtracee = magic_fromtracee;
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT_EQ(lookup_me_totracee, magic_totracee);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Read lookup_me_fromtracee PID=%d by tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_IO, child, &io_fromtracee, 0) != -1);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me_fromtracee, magic_fromtracee,
+ "got value %" PRIx8 " != expected %" PRIx8, lookup_me_fromtracee,
+ magic_fromtracee);
+
+ lookup_me_totracee = magic_totracee;
+
+ printf("Write lookup_me_totracee to PID=%d by tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_IO, child, &io_totracee, 0) != -1);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me_totracee, magic_totracee,
+ "got value %" PRIx8 " != expected %" PRIx8, lookup_me_totracee,
+ magic_totracee);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(io_read_d_write_d_handshake2);
+ATF_TC_HEAD(io_read_d_write_d_handshake2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_IO with PIOD_WRITE_D and PIOD_READ_D handshake");
+}
+
+ATF_TC_BODY(io_read_d_write_d_handshake2, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ uint8_t lookup_me_fromtracee = 0;
+ const uint8_t magic_fromtracee = (uint8_t)random();
+ uint8_t lookup_me_totracee = 0;
+ const uint8_t magic_totracee = (uint8_t)random();
+ struct ptrace_io_desc io_fromtracee = {
+ .piod_op = PIOD_READ_D,
+ .piod_offs = &lookup_me_fromtracee,
+ .piod_addr = &lookup_me_fromtracee,
+ .piod_len = sizeof(lookup_me_fromtracee)
+ };
+ struct ptrace_io_desc io_totracee = {
+ .piod_op = PIOD_WRITE_D,
+ .piod_offs = &lookup_me_totracee,
+ .piod_addr = &lookup_me_totracee,
+ .piod_len = sizeof(lookup_me_totracee)
+ };
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ lookup_me_fromtracee = magic_fromtracee;
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT_EQ(lookup_me_totracee, magic_totracee);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ lookup_me_totracee = magic_totracee;
+
+ printf("Write lookup_me_totracee to PID=%d by tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_IO, child, &io_totracee, 0) != -1);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me_totracee, magic_totracee,
+ "got value %" PRIx8 " != expected %" PRIx8, lookup_me_totracee,
+ magic_totracee);
+
+ printf("Read lookup_me_fromtracee PID=%d by tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_IO, child, &io_fromtracee, 0) != -1);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me_fromtracee, magic_fromtracee,
+ "got value %" PRIx8 " != expected %" PRIx8, lookup_me_fromtracee,
+ magic_fromtracee);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(read_d_write_d_handshake1);
+ATF_TC_HEAD(read_d_write_d_handshake1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_READ_D with PT_WRITE_D handshake");
+}
+
+ATF_TC_BODY(read_d_write_d_handshake1, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ int lookup_me_fromtracee = 0;
+ const int magic_fromtracee = (int)random();
+ int lookup_me_totracee = 0;
+ const int magic_totracee = (int)random();
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ lookup_me_fromtracee = magic_fromtracee;
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT_EQ(lookup_me_totracee, magic_totracee);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Read new lookup_me_fromtracee PID=%d by tracer (PID=%d)\n",
+ child, getpid());
+ errno = 0;
+ lookup_me_fromtracee =
+ ptrace(PT_READ_D, child, &lookup_me_fromtracee, 0);
+ ATF_REQUIRE_EQ(errno, 0);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me_fromtracee, magic_fromtracee,
+ "got value %#x != expected %#x", lookup_me_fromtracee,
+ magic_fromtracee);
+
+ printf("Write new lookup_me_totracee to PID=%d from tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE
+ (ptrace(PT_WRITE_D, child, &lookup_me_totracee, magic_totracee)
+ != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(read_d_write_d_handshake2);
+ATF_TC_HEAD(read_d_write_d_handshake2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_WRITE_D with PT_READ_D handshake");
+}
+
+ATF_TC_BODY(read_d_write_d_handshake2, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ int lookup_me_fromtracee = 0;
+ const int magic_fromtracee = (int)random();
+ int lookup_me_totracee = 0;
+ const int magic_totracee = (int)random();
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ lookup_me_fromtracee = magic_fromtracee;
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT_EQ(lookup_me_totracee, magic_totracee);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Write new lookup_me_totracee to PID=%d from tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE
+ (ptrace(PT_WRITE_D, child, &lookup_me_totracee, magic_totracee)
+ != -1);
+
+ printf("Read new lookup_me_fromtracee PID=%d by tracer (PID=%d)\n",
+ child, getpid());
+ errno = 0;
+ lookup_me_fromtracee =
+ ptrace(PT_READ_D, child, &lookup_me_fromtracee, 0);
+ ATF_REQUIRE_EQ(errno, 0);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me_fromtracee, magic_fromtracee,
+ "got value %#x != expected %#x", lookup_me_fromtracee,
+ magic_fromtracee);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+/* These dummy functions are used to be copied with ptrace(2) calls */
+static int __used
+dummy_fn1(int a, int b, int c, int d)
+{
+
+ a *= 1;
+ b += 2;
+ c -= 3;
+ d /= 4;
+
+ return a + b * c - d;
+}
+
+static int __used
+dummy_fn2(int a, int b, int c, int d)
+{
+
+ a *= 4;
+ b += 3;
+ c -= 2;
+ d /= 1;
+
+ return a + b * c - d;
+}
+
+static int __used
+dummy_fn3(int a, int b, int c, int d)
+{
+
+ a *= 10;
+ b += 20;
+ c -= 30;
+ d /= 40;
+
+ return a + b * c - d;
+}
+
+static int __used
+dummy_fn4(int a, int b, int c, int d)
+{
+
+ a *= 40;
+ b += 30;
+ c -= 20;
+ d /= 10;
+
+ return a + b * c - d;
+}
+
+ATF_TC(io_read_i1);
+ATF_TC_HEAD(io_read_i1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_IO with PIOD_READ_I and len = sizeof(uint8_t)");
+}
+
+ATF_TC_BODY(io_read_i1, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ uint8_t lookup_me = 0;
+ uint8_t magic;
+ memcpy(&magic, dummy_fn1, sizeof(magic));
+ struct ptrace_io_desc io = {
+ .piod_op = PIOD_READ_I,
+ .piod_offs = dummy_fn1,
+ .piod_addr = &lookup_me,
+ .piod_len = sizeof(lookup_me)
+ };
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Read lookup_me from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me, magic,
+ "got value %" PRIx8 " != expected %" PRIx8, lookup_me, magic);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(io_read_i2);
+ATF_TC_HEAD(io_read_i2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_IO with PIOD_READ_I and len = sizeof(uint16_t)");
+}
+
+ATF_TC_BODY(io_read_i2, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ uint16_t lookup_me = 0;
+ uint16_t magic;
+ memcpy(&magic, dummy_fn1, sizeof(magic));
+ struct ptrace_io_desc io = {
+ .piod_op = PIOD_READ_I,
+ .piod_offs = dummy_fn1,
+ .piod_addr = &lookup_me,
+ .piod_len = sizeof(lookup_me)
+ };
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Read lookup_me from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me, magic,
+ "got value %" PRIx16 " != expected %" PRIx16, lookup_me, magic);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(io_read_i3);
+ATF_TC_HEAD(io_read_i3, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_IO with PIOD_READ_I and len = sizeof(uint32_t)");
+}
+
+ATF_TC_BODY(io_read_i3, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ uint32_t lookup_me = 0;
+ uint32_t magic;
+ memcpy(&magic, dummy_fn1, sizeof(magic));
+ struct ptrace_io_desc io = {
+ .piod_op = PIOD_READ_I,
+ .piod_offs = dummy_fn1,
+ .piod_addr = &lookup_me,
+ .piod_len = sizeof(lookup_me)
+ };
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Read lookup_me from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me, magic,
+ "got value %" PRIx32 " != expected %" PRIx32, lookup_me, magic);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(io_read_i4);
+ATF_TC_HEAD(io_read_i4, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_IO with PIOD_READ_I and len = sizeof(uint64_t)");
+}
+
+ATF_TC_BODY(io_read_i4, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ uint64_t lookup_me = 0;
+ uint64_t magic;
+ memcpy(&magic, dummy_fn1, sizeof(magic));
+ struct ptrace_io_desc io = {
+ .piod_op = PIOD_READ_I,
+ .piod_offs = dummy_fn1,
+ .piod_addr = &lookup_me,
+ .piod_len = sizeof(lookup_me)
+ };
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Read lookup_me from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ ATF_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me, magic,
+ "got value %" PRIx64 " != expected %" PRIx64, lookup_me, magic);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(read_i1);
+ATF_TC_HEAD(read_i1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_READ_I called once");
+}
+
+ATF_TC_BODY(read_i1, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ int lookup_me = 0;
+ int magic;
+ memcpy(&magic, dummy_fn1, sizeof(magic));
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Read new lookup_me from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ errno = 0;
+ lookup_me = ptrace(PT_READ_I, child, dummy_fn1, 0);
+ ATF_REQUIRE_EQ(errno, 0);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me, magic,
+ "got value %#x != expected %#x", lookup_me, magic);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(read_i2);
+ATF_TC_HEAD(read_i2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_READ_I called twice");
+}
+
+ATF_TC_BODY(read_i2, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ int lookup_me1 = 0;
+ int lookup_me2 = 0;
+ int magic1;
+ int magic2;
+ memcpy(&magic1, dummy_fn1, sizeof(magic1));
+ memcpy(&magic2, dummy_fn2, sizeof(magic2));
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Read new lookup_me1 from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ errno = 0;
+ lookup_me1 = ptrace(PT_READ_I, child, dummy_fn1, 0);
+ ATF_REQUIRE_EQ(errno, 0);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me1, magic1,
+ "got value %#x != expected %#x", lookup_me1, magic1);
+
+ printf("Read new lookup_me2 from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ errno = 0;
+ lookup_me2 = ptrace(PT_READ_I, child, dummy_fn2, 0);
+ ATF_REQUIRE_EQ(errno, 0);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me2, magic2,
+ "got value %#x != expected %#x", lookup_me2, magic2);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(read_i3);
+ATF_TC_HEAD(read_i3, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_READ_I called three times");
+}
+
+ATF_TC_BODY(read_i3, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ int lookup_me1 = 0;
+ int lookup_me2 = 0;
+ int lookup_me3 = 0;
+ int magic1;
+ int magic2;
+ int magic3;
+ memcpy(&magic1, dummy_fn1, sizeof(magic1));
+ memcpy(&magic2, dummy_fn2, sizeof(magic2));
+ memcpy(&magic3, dummy_fn3, sizeof(magic3));
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Read new lookup_me1 from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ errno = 0;
+ lookup_me1 = ptrace(PT_READ_I, child, dummy_fn1, 0);
+ ATF_REQUIRE_EQ(errno, 0);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me1, magic1,
+ "got value %#x != expected %#x", lookup_me1, magic1);
+
+ printf("Read new lookup_me2 from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ errno = 0;
+ lookup_me2 = ptrace(PT_READ_I, child, dummy_fn2, 0);
+ ATF_REQUIRE_EQ(errno, 0);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me2, magic2,
+ "got value %#x != expected %#x", lookup_me2, magic2);
+
+ printf("Read new lookup_me3 from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ errno = 0;
+ lookup_me3 = ptrace(PT_READ_I, child, dummy_fn3, 0);
+ ATF_REQUIRE_EQ(errno, 0);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me3, magic3,
+ "got value %#x != expected %#x", lookup_me3, magic3);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(read_i4);
+ATF_TC_HEAD(read_i4, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_READ_I called four times");
+}
+
+ATF_TC_BODY(read_i4, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+ int lookup_me1 = 0;
+ int lookup_me2 = 0;
+ int lookup_me3 = 0;
+ int lookup_me4 = 0;
+ int magic1;
+ int magic2;
+ int magic3;
+ int magic4;
+ memcpy(&magic1, dummy_fn1, sizeof(magic1));
+ memcpy(&magic2, dummy_fn2, sizeof(magic2));
+ memcpy(&magic3, dummy_fn3, sizeof(magic3));
+ memcpy(&magic4, dummy_fn4, sizeof(magic4));
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Read new lookup_me1 from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ errno = 0;
+ lookup_me1 = ptrace(PT_READ_I, child, dummy_fn1, 0);
+ ATF_REQUIRE_EQ(errno, 0);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me1, magic1,
+ "got value %#x != expected %#x", lookup_me1, magic1);
+
+ printf("Read new lookup_me2 from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ errno = 0;
+ lookup_me2 = ptrace(PT_READ_I, child, dummy_fn2, 0);
+ ATF_REQUIRE_EQ(errno, 0);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me2, magic2,
+ "got value %#x != expected %#x", lookup_me2, magic2);
+
+ printf("Read new lookup_me3 from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ errno = 0;
+ lookup_me3 = ptrace(PT_READ_I, child, dummy_fn3, 0);
+ ATF_REQUIRE_EQ(errno, 0);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me3, magic3,
+ "got value %#x != expected %#x", lookup_me3, magic3);
+
+ printf("Read new lookup_me4 from tracee (PID=%d) by tracer (PID=%d)\n",
+ child, getpid());
+ errno = 0;
+ lookup_me4 = ptrace(PT_READ_I, child, dummy_fn4, 0);
+ ATF_REQUIRE_EQ(errno, 0);
+
+ ATF_REQUIRE_EQ_MSG(lookup_me4, magic4,
+ "got value %#x != expected %#x", lookup_me4, magic4);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+#if defined(HAVE_GPREGS)
+ATF_TC(regs1);
+ATF_TC_HEAD(regs1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify plain PT_GETREGS call without further steps");
+}
+
+ATF_TC_BODY(regs1, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ struct reg r;
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Call GETREGS for the child process\n");
+ ATF_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(HAVE_GPREGS)
+ATF_TC(regs2);
+ATF_TC_HEAD(regs2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify plain PT_GETREGS call and retrieve PC");
+}
+
+ATF_TC_BODY(regs2, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ struct reg r;
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Call GETREGS for the child process\n");
+ ATF_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
+
+ printf("Retrieved PC=%" PRIxREGISTER "\n", PTRACE_REG_PC(&r));
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(HAVE_GPREGS)
+ATF_TC(regs3);
+ATF_TC_HEAD(regs3, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify plain PT_GETREGS call and retrieve SP");
+}
+
+ATF_TC_BODY(regs3, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ struct reg r;
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Call GETREGS for the child process\n");
+ ATF_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
+
+ printf("Retrieved SP=%" PRIxREGISTER "\n", PTRACE_REG_SP(&r));
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(HAVE_GPREGS)
+ATF_TC(regs4);
+ATF_TC_HEAD(regs4, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify plain PT_GETREGS call and retrieve INTRV");
+}
+
+ATF_TC_BODY(regs4, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ struct reg r;
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Call GETREGS for the child process\n");
+ ATF_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
+
+ printf("Retrieved INTRV=%" PRIxREGISTER "\n", PTRACE_REG_INTRV(&r));
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(HAVE_GPREGS)
+ATF_TC(regs5);
+ATF_TC_HEAD(regs5, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_GETREGS and PT_SETREGS calls without changing regs");
+}
+
+ATF_TC_BODY(regs5, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ struct reg r;
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Call GETREGS for the child process\n");
+ ATF_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
+
+ printf("Call SETREGS for the child process (without changed regs)\n");
+ ATF_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(HAVE_FPREGS)
+ATF_TC(fpregs1);
+ATF_TC_HEAD(fpregs1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify plain PT_GETFPREGS call without further steps");
+}
+
+ATF_TC_BODY(fpregs1, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ struct fpreg r;
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Call GETFPREGS for the child process\n");
+ ATF_REQUIRE(ptrace(PT_GETFPREGS, child, &r, 0) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(HAVE_FPREGS)
+ATF_TC(fpregs2);
+ATF_TC_HEAD(fpregs2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_GETFPREGS and PT_SETFPREGS calls without changing "
+ "regs");
+}
+
+ATF_TC_BODY(fpregs2, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ struct fpreg r;
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Call GETFPREGS for the child process\n");
+ ATF_REQUIRE(ptrace(PT_GETFPREGS, child, &r, 0) != -1);
+
+ printf("Call SETFPREGS for the child (without changed regs)\n");
+ ATF_REQUIRE(ptrace(PT_SETFPREGS, child, &r, 0) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(PT_STEP)
+ATF_TC(step1);
+ATF_TC_HEAD(step1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify single PT_STEP call");
+}
+
+ATF_TC_BODY(step1, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ int happy;
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ happy = check_happy(100);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT_EQ(happy, check_happy(100));
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent (use PT_STEP)\n");
+ ATF_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(PT_STEP)
+ATF_TC(step2);
+ATF_TC_HEAD(step2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_STEP called twice");
+}
+
+ATF_TC_BODY(step2, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ int happy;
+ int N = 2;
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ happy = check_happy(999);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT_EQ(happy, check_happy(999));
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ while (N --> 0) {
+ printf("Before resuming the child process where it left off "
+ "and without signal to be sent (use PT_STEP)\n");
+ ATF_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
+ child);
+
+ validate_status_stopped(status, SIGTRAP);
+ }
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(PT_STEP)
+ATF_TC(step3);
+ATF_TC_HEAD(step3, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_STEP called three times");
+}
+
+ATF_TC_BODY(step3, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ int happy;
+ int N = 3;
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ happy = check_happy(999);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT_EQ(happy, check_happy(999));
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ while (N --> 0) {
+ printf("Before resuming the child process where it left off "
+ "and without signal to be sent (use PT_STEP)\n");
+ ATF_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
+ child);
+
+ validate_status_stopped(status, SIGTRAP);
+ }
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(PT_STEP)
+ATF_TC(step4);
+ATF_TC_HEAD(step4, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify PT_STEP called four times");
+}
+
+ATF_TC_BODY(step4, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ int happy;
+ int N = 4;
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ happy = check_happy(999);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT_EQ(happy, check_happy(999));
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ while (N --> 0) {
+ printf("Before resuming the child process where it left off "
+ "and without signal to be sent (use PT_STEP)\n");
+ ATF_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
+ child);
+
+ validate_status_stopped(status, SIGTRAP);
+ }
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+ATF_TC(kill1);
+ATF_TC_HEAD(kill1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that PT_CONTINUE with SIGKILL terminates child");
+}
+
+ATF_TC_BODY(kill1, tc)
+{
+ const int sigval = SIGSTOP, sigsent = SIGKILL;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ /* NOTREACHED */
+ FORKEE_ASSERTX(0 &&
+ "Child should be terminated by a signal from its parent");
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_signaled(status, sigsent, 0);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(kill2);
+ATF_TC_HEAD(kill2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that PT_KILL terminates child");
+}
+
+ATF_TC_BODY(kill2, tc)
+{
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ /* NOTREACHED */
+ FORKEE_ASSERTX(0 &&
+ "Child should be terminated by a signal from its parent");
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_KILL, child, (void*)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_signaled(status, SIGKILL, 0);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(lwpinfo1);
+ATF_TC_HEAD(lwpinfo1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify basic LWPINFO call for single thread (PT_TRACE_ME)");
+}
+
+ATF_TC_BODY(lwpinfo1, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ struct ptrace_lwpinfo info = {0, 0};
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Before calling ptrace(2) with PT_LWPINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_LWPINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Assert that there exists a thread\n");
+ ATF_REQUIRE(info.pl_lwpid > 0);
+
+ printf("Assert that lwp thread %d received event PL_EVENT_SIGNAL\n",
+ info.pl_lwpid);
+ ATF_REQUIRE_EQ_MSG(info.pl_event, PL_EVENT_SIGNAL,
+ "Received event %d != expected event %d",
+ info.pl_event, PL_EVENT_SIGNAL);
+
+ printf("Before calling ptrace(2) with PT_LWPINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_LWPINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Assert that there are no more lwp threads in child\n");
+ ATF_REQUIRE_EQ(info.pl_lwpid, 0);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+#if defined(TWAIT_HAVE_PID)
+ATF_TC(lwpinfo2);
+ATF_TC_HEAD(lwpinfo2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify basic LWPINFO call for single thread (PT_ATTACH from "
+ "tracer)");
+}
+
+ATF_TC_BODY(lwpinfo2, tc)
+{
+ struct msg_fds parent_tracee, parent_tracer;
+ const int exitval_tracee = 5;
+ const int exitval_tracer = 10;
+ pid_t tracee, tracer, wpid;
+ uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ struct ptrace_lwpinfo info = {0, 0};
+
+ printf("Spawn tracee\n");
+ ATF_REQUIRE(msg_open(&parent_tracee) == 0);
+ ATF_REQUIRE(msg_open(&parent_tracer) == 0);
+ tracee = atf_utils_fork();
+ if (tracee == 0) {
+
+ /* Wait for message from the parent */
+ CHILD_TO_PARENT("tracee ready", parent_tracee, msg);
+ CHILD_FROM_PARENT("tracee exit", parent_tracee, msg);
+
+ _exit(exitval_tracee);
+ }
+ PARENT_FROM_CHILD("tracee ready", parent_tracee, msg);
+
+ printf("Spawn debugger\n");
+ tracer = atf_utils_fork();
+ if (tracer == 0) {
+ /* No IPC to communicate with the child */
+ printf("Before calling PT_ATTACH from tracee %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
+
+ /* Wait for tracee and assert that it was stopped w/ SIGSTOP */
+ FORKEE_REQUIRE_SUCCESS(
+ wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
+
+ forkee_status_stopped(status, SIGSTOP);
+
+ printf("Before calling ptrace(2) with PT_LWPINFO for child\n");
+ FORKEE_ASSERT(ptrace(PT_LWPINFO, tracee, &info, sizeof(info))
+ != -1);
+
+ printf("Assert that there exists a thread\n");
+ FORKEE_ASSERTX(info.pl_lwpid > 0);
+
+ printf("Assert that lwp thread %d received event "
+ "PL_EVENT_SIGNAL\n", info.pl_lwpid);
+ FORKEE_ASSERT_EQ(info.pl_event, PL_EVENT_SIGNAL);
+
+ printf("Before calling ptrace(2) with PT_LWPINFO for child\n");
+ FORKEE_ASSERT(ptrace(PT_LWPINFO, tracee, &info, sizeof(info))
+ != -1);
+
+ printf("Assert that there are no more lwp threads in child\n");
+ FORKEE_ASSERTX(info.pl_lwpid == 0);
+
+ /* Resume tracee with PT_CONTINUE */
+ FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
+
+ /* Inform parent that tracer has attached to tracee */
+ CHILD_TO_PARENT("tracer ready", parent_tracer, msg);
+ /* Wait for parent */
+ CHILD_FROM_PARENT("tracer wait", parent_tracer, msg);
+
+ /* Wait for tracee and assert that it exited */
+ FORKEE_REQUIRE_SUCCESS(
+ wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
+
+ forkee_status_exited(status, exitval_tracee);
+
+ printf("Before exiting of the tracer process\n");
+ _exit(exitval_tracer);
+ }
+
+ printf("Wait for the tracer to attach to the tracee\n");
+ PARENT_FROM_CHILD("tracer ready", parent_tracer, msg);
+
+ printf("Resume the tracee and let it exit\n");
+ PARENT_TO_CHILD("tracee exit", parent_tracee, msg);
+
+ printf("Detect that tracee is zombie\n");
+ await_zombie(tracee);
+
+ printf("Assert that there is no status about tracee - "
+ "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(
+ wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
+
+ printf("Resume the tracer and let it detect exited tracee\n");
+ PARENT_TO_CHILD("tracer wait", parent_tracer, msg);
+
+ printf("Wait for tracer to finish its job and exit - calling %s()\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
+ tracer);
+
+ validate_status_exited(status, exitval_tracer);
+
+ printf("Wait for tracee to finish its job and exit - calling %s()\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG),
+ tracee);
+
+ validate_status_exited(status, exitval_tracee);
+
+ msg_close(&parent_tracer);
+ msg_close(&parent_tracee);
+}
+#endif
+
+ATF_TC(siginfo1);
+ATF_TC_HEAD(siginfo1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify basic PT_GET_SIGINFO call for SIGTRAP from tracee");
+}
+
+ATF_TC_BODY(siginfo1, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGTRAP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ struct ptrace_siginfo info;
+ memset(&info, 0, sizeof(info));
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Signal traced to lwpid=%d\n", info.psi_lwpid);
+ printf("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
+ info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
+ info.psi_siginfo.si_errno);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(siginfo2);
+ATF_TC_HEAD(siginfo2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify basic PT_GET_SIGINFO and PT_SET_SIGINFO calls without "
+ "modification of SIGINT from tracee");
+}
+
+static int siginfo2_caught = 0;
+
+static void
+siginfo2_sighandler(int sig)
+{
+ FORKEE_ASSERT_EQ(sig, SIGINT);
+
+ ++siginfo2_caught;
+}
+
+ATF_TC_BODY(siginfo2, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGINT;
+ pid_t child, wpid;
+ struct sigaction sa;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ struct ptrace_siginfo info;
+ memset(&info, 0, sizeof(info));
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ sa.sa_handler = siginfo2_sighandler;
+ sa.sa_flags = SA_SIGINFO;
+ sigemptyset(&sa.sa_mask);
+
+ FORKEE_ASSERT(sigaction(sigval, &sa, NULL) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT_EQ(siginfo2_caught, 1);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Signal traced to lwpid=%d\n", info.psi_lwpid);
+ printf("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
+ info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
+ info.psi_siginfo.si_errno);
+
+ printf("Before calling ptrace(2) with PT_SET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_SET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigval) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(siginfo3);
+ATF_TC_HEAD(siginfo3, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify basic PT_GET_SIGINFO and PT_SET_SIGINFO calls with "
+ "setting signal to new value");
+}
+
+static int siginfo3_caught = 0;
+
+static void
+siginfo3_sigaction(int sig, siginfo_t *info, void *ctx)
+{
+ FORKEE_ASSERT_EQ(sig, SIGTRAP);
+
+ FORKEE_ASSERT_EQ(info->si_signo, SIGTRAP);
+ FORKEE_ASSERT_EQ(info->si_code, TRAP_BRKPT);
+
+ ++siginfo3_caught;
+}
+
+ATF_TC_BODY(siginfo3, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGINT;
+ const int sigfaked = SIGTRAP;
+ const int sicodefaked = TRAP_BRKPT;
+ pid_t child, wpid;
+ struct sigaction sa;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ struct ptrace_siginfo info;
+ memset(&info, 0, sizeof(info));
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ sa.sa_sigaction = siginfo3_sigaction;
+ sa.sa_flags = SA_SIGINFO;
+ sigemptyset(&sa.sa_mask);
+
+ FORKEE_ASSERT(sigaction(sigfaked, &sa, NULL) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT_EQ(siginfo3_caught, 1);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Signal traced to lwpid=%d\n", info.psi_lwpid);
+ printf("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
+ info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
+ info.psi_siginfo.si_errno);
+
+ printf("Before setting new faked signal to signo=%d si_code=%d\n",
+ sigfaked, sicodefaked);
+ info.psi_siginfo.si_signo = sigfaked;
+ info.psi_siginfo.si_code = sicodefaked;
+
+ printf("Before calling ptrace(2) with PT_SET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_SET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Before checking siginfo_t\n");
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigfaked);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_code, sicodefaked);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigfaked) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(siginfo4);
+ATF_TC_HEAD(siginfo4, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Detect SIGTRAP TRAP_EXEC from tracee");
+}
+
+ATF_TC_BODY(siginfo4, tc)
+{
+ const int sigval = SIGTRAP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+
+ struct ptrace_siginfo info;
+ memset(&info, 0, sizeof(info));
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before calling execve(2) from child\n");
+ execlp("/bin/echo", "/bin/echo", NULL);
+
+ FORKEE_ASSERT(0 && "Not reached");
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Signal traced to lwpid=%d\n", info.psi_lwpid);
+ printf("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
+ info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
+ info.psi_siginfo.si_errno);
+
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_EXEC);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+#if defined(TWAIT_HAVE_PID)
+ATF_TC(siginfo5);
+ATF_TC_HEAD(siginfo5, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that fork(2) is intercepted by ptrace(2) with EVENT_MASK "
+ "set to PTRACE_FORK and reports correct signal information");
+}
+
+ATF_TC_BODY(siginfo5, tc)
+{
+ const int exitval = 5;
+ const int exitval2 = 15;
+ const int sigval = SIGSTOP;
+ pid_t child, child2, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ ptrace_state_t state;
+ const int slen = sizeof(state);
+ ptrace_event_t event;
+ const int elen = sizeof(event);
+ struct ptrace_siginfo info;
+
+ memset(&info, 0, sizeof(info));
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT((child2 = fork()) != 1);
+
+ if (child2 == 0)
+ _exit(exitval2);
+
+ FORKEE_REQUIRE_SUCCESS
+ (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
+
+ forkee_status_exited(status, exitval2);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Before checking siginfo_t\n");
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
+
+ printf("Enable PTRACE_FORK in EVENT_MASK for the child %d\n", child);
+ event.pe_set_event = PTRACE_FORK;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child %d\n", TWAIT_FNAME, child);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Before checking siginfo_t\n");
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_CHLD);
+
+ ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
+ ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
+
+ child2 = state.pe_other_pid;
+ printf("Reported PTRACE_FORK event with forkee %d\n", child2);
+
+ printf("Before calling %s() for the forkee %d of the child %d\n",
+ TWAIT_FNAME, child2, child);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
+ child2);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Before checking siginfo_t\n");
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_CHLD);
+
+ ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
+ ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
+ ATF_REQUIRE_EQ(state.pe_other_pid, child);
+
+ printf("Before resuming the forkee process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the forkee - expected exited\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
+ child2);
+
+ validate_status_exited(status, exitval2);
+
+ printf("Before calling %s() for the forkee - expected no process\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD,
+ wpid = TWAIT_GENERIC(child2, &status, 0));
+
+ printf("Before calling %s() for the child - expected stopped "
+ "SIGCHLD\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGCHLD);
+
+ printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Before checking siginfo_t\n");
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGCHLD);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_code, CLD_EXITED);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child - expected exited\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child - expected no process\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(PT_STEP)
+ATF_TC(siginfo6);
+ATF_TC_HEAD(siginfo6, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify single PT_STEP call with signal information check");
+}
+
+ATF_TC_BODY(siginfo6, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ int happy;
+ struct ptrace_siginfo info;
+
+ memset(&info, 0, sizeof(info));
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ happy = check_happy(100);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT_EQ(happy, check_happy(100));
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Before checking siginfo_t\n");
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent (use PT_STEP)\n");
+ ATF_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
+ ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
+
+ printf("Before checking siginfo_t\n");
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
+ ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_TRACE);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+volatile lwpid_t the_lwp_id = 0;
+
+static void
+lwp_main_func(void *arg)
+{
+ the_lwp_id = _lwp_self();
+ _lwp_exit();
+}
+
+ATF_TC(lwp_create1);
+ATF_TC_HEAD(lwp_create1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that 1 LWP creation is intercepted by ptrace(2) with "
+ "EVENT_MASK set to PTRACE_LWP_CREATE");
+}
+
+ATF_TC_BODY(lwp_create1, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ ptrace_state_t state;
+ const int slen = sizeof(state);
+ ptrace_event_t event;
+ const int elen = sizeof(event);
+ ucontext_t uc;
+ lwpid_t lid;
+ static const size_t ssize = 16*1024;
+ void *stack;
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before allocating memory for stack in child\n");
+ FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
+
+ printf("Before making context for new lwp in child\n");
+ _lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
+
+ printf("Before creating new in child\n");
+ FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
+
+ printf("Before waiting for lwp %d to exit\n", lid);
+ FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
+
+ printf("Before verifying that reported %d and running lid %d "
+ "are the same\n", lid, the_lwp_id);
+ FORKEE_ASSERT_EQ(lid, the_lwp_id);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Set empty EVENT_MASK for the child %d\n", child);
+ event.pe_set_event = PTRACE_LWP_CREATE;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child - expected stopped "
+ "SIGTRAP\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
+
+ ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_CREATE);
+
+ lid = state.pe_lwp;
+ printf("Reported PTRACE_LWP_CREATE event with lid %d\n", lid);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child - expected exited\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child - expected no process\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(lwp_exit1);
+ATF_TC_HEAD(lwp_exit1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that 1 LWP creation is intercepted by ptrace(2) with "
+ "EVENT_MASK set to PTRACE_LWP_EXIT");
+}
+
+ATF_TC_BODY(lwp_exit1, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ ptrace_state_t state;
+ const int slen = sizeof(state);
+ ptrace_event_t event;
+ const int elen = sizeof(event);
+ ucontext_t uc;
+ lwpid_t lid;
+ static const size_t ssize = 16*1024;
+ void *stack;
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before allocating memory for stack in child\n");
+ FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
+
+ printf("Before making context for new lwp in child\n");
+ _lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
+
+ printf("Before creating new in child\n");
+ FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
+
+ printf("Before waiting for lwp %d to exit\n", lid);
+ FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
+
+ printf("Before verifying that reported %d and running lid %d "
+ "are the same\n", lid, the_lwp_id);
+ FORKEE_ASSERT_EQ(lid, the_lwp_id);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Set empty EVENT_MASK for the child %d\n", child);
+ event.pe_set_event = PTRACE_LWP_EXIT;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child - expected stopped "
+ "SIGTRAP\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
+
+ ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_EXIT);
+
+ lid = state.pe_lwp;
+ printf("Reported PTRACE_LWP_EXIT event with lid %d\n", lid);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child - expected exited\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child - expected no process\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(signal1);
+ATF_TC_HEAD(signal1, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that masking single unrelated signal does not stop tracer "
+ "from catching other signals");
+}
+
+ATF_TC_BODY(signal1, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ const int sigmasked = SIGTRAP;
+ const int signotmasked = SIGINT;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ sigset_t intmask;
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ sigemptyset(&intmask);
+ sigaddset(&intmask, sigmasked);
+ sigprocmask(SIG_BLOCK, &intmask, NULL);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before raising %s from child\n",
+ strsignal(signotmasked));
+ FORKEE_ASSERT(raise(signotmasked) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, signotmasked);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(signal2);
+ATF_TC_HEAD(signal2, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that masking SIGTRAP in tracee stops tracer from "
+ "catching this raised signal");
+}
+
+ATF_TC_BODY(signal2, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ const int sigmasked = SIGTRAP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ sigset_t intmask;
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ sigemptyset(&intmask);
+ sigaddset(&intmask, sigmasked);
+ sigprocmask(SIG_BLOCK, &intmask, NULL);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before raising %s breakpoint from child\n",
+ strsignal(sigmasked));
+ FORKEE_ASSERT(raise(sigmasked) == 0);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(signal3);
+ATF_TC_HEAD(signal3, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that masking SIGTRAP in tracee does not stop tracer from "
+ "catching software breakpoints");
+}
+
+ATF_TC_BODY(signal3, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ const int sigmasked = SIGTRAP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ sigset_t intmask;
+
+ atf_tc_expect_fail("PR kern/51918");
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ sigemptyset(&intmask);
+ sigaddset(&intmask, sigmasked);
+ sigprocmask(SIG_BLOCK, &intmask, NULL);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before raising software breakpoint from child\n");
+#if defined(__x86_64__)
+ __asm__ __volatile__ ("int3\n;");
+#else
+ /* port me */
+#endif
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigmasked);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+#if defined(PT_STEP)
+ATF_TC(signal4);
+ATF_TC_HEAD(signal4, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that masking SIGTRAP in tracee does not stop tracer from "
+ "catching single step trap");
+}
+
+ATF_TC_BODY(signal4, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ const int sigmasked = SIGTRAP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ sigset_t intmask;
+ int happy;
+
+ atf_tc_expect_fail("PR kern/51918");
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ happy = check_happy(100);
+
+ sigemptyset(&intmask);
+ sigaddset(&intmask, sigmasked);
+ sigprocmask(SIG_BLOCK, &intmask, NULL);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT_EQ(happy, check_happy(100));
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigmasked);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+ATF_TC(signal5);
+ATF_TC_HEAD(signal5, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that masking SIGTRAP in tracee does not stop tracer from "
+ "catching exec() breakpoint");
+}
+
+ATF_TC_BODY(signal5, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ const int sigmasked = SIGTRAP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ sigset_t intmask;
+
+ atf_tc_expect_fail("PR kern/51918");
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ sigemptyset(&intmask);
+ sigaddset(&intmask, sigmasked);
+ sigprocmask(SIG_BLOCK, &intmask, NULL);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before calling execve(2) from child\n");
+ execlp("/bin/echo", "/bin/echo", NULL);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigmasked);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+#if defined(TWAIT_HAVE_PID)
+ATF_TC(signal6);
+ATF_TC_HEAD(signal6, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that masking SIGTRAP in tracee does not stop tracer from "
+ "catching PTRACE_FORK breakpoint");
+}
+
+ATF_TC_BODY(signal6, tc)
+{
+ const int exitval = 5;
+ const int exitval2 = 15;
+ const int sigval = SIGSTOP;
+ const int sigmasked = SIGTRAP;
+ pid_t child, child2, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ sigset_t intmask;
+ ptrace_state_t state;
+ const int slen = sizeof(state);
+ ptrace_event_t event;
+ const int elen = sizeof(event);
+
+ atf_tc_expect_fail("PR kern/51918");
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ sigemptyset(&intmask);
+ sigaddset(&intmask, sigmasked);
+ sigprocmask(SIG_BLOCK, &intmask, NULL);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT((child2 = fork()) != 1);
+
+ if (child2 == 0)
+ _exit(exitval2);
+
+ FORKEE_REQUIRE_SUCCESS
+ (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
+
+ forkee_status_exited(status, exitval2);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Enable PTRACE_FORK in EVENT_MASK for the child %d\n", child);
+ event.pe_set_event = PTRACE_FORK;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigmasked);
+
+ ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
+ ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
+
+ child2 = state.pe_other_pid;
+ printf("Reported PTRACE_FORK event with forkee %d\n", child2);
+
+ printf("Before calling %s() for the child2\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
+ child2);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
+ ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
+ ATF_REQUIRE_EQ(state.pe_other_pid, child);
+
+ printf("Before resuming the forkee process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the forkee - expected exited\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
+ child2);
+
+ validate_status_exited(status, exitval2);
+
+ printf("Before calling %s() for the forkee - expected no process\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD,
+ wpid = TWAIT_GENERIC(child2, &status, 0));
+
+ printf("Before calling %s() for the child - expected stopped "
+ "SIGCHLD\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGCHLD);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child - expected exited\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child - expected no process\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+#if defined(TWAIT_HAVE_PID)
+ATF_TC(signal7);
+ATF_TC_HEAD(signal7, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that masking SIGTRAP in tracee does not stop tracer from "
+ "catching PTRACE_VFORK breakpoint");
+}
+
+ATF_TC_BODY(signal7, tc)
+{
+ const int exitval = 5;
+ const int exitval2 = 15;
+ const int sigval = SIGSTOP;
+ const int sigmasked = SIGTRAP;
+ pid_t child, child2, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ sigset_t intmask;
+ ptrace_state_t state;
+ const int slen = sizeof(state);
+ ptrace_event_t event;
+ const int elen = sizeof(event);
+
+ atf_tc_expect_fail("PR kern/51918 PR kern/51630");
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ sigemptyset(&intmask);
+ sigaddset(&intmask, sigmasked);
+ sigprocmask(SIG_BLOCK, &intmask, NULL);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT((child2 = fork()) != 1);
+
+ if (child2 == 0)
+ _exit(exitval2);
+
+ FORKEE_REQUIRE_SUCCESS
+ (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
+
+ forkee_status_exited(status, exitval2);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Enable PTRACE_VFORK in EVENT_MASK for the child %d\n", child);
+ event.pe_set_event = PTRACE_VFORK;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigmasked);
+
+ ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
+ ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK);
+
+ child2 = state.pe_other_pid;
+ printf("Reported PTRACE_VFORK event with forkee %d\n", child2);
+
+ printf("Before calling %s() for the child2\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
+ child2);
+
+ validate_status_stopped(status, SIGTRAP);
+
+ ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
+ ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK);
+ ATF_REQUIRE_EQ(state.pe_other_pid, child);
+
+ printf("Before resuming the forkee process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the forkee - expected exited\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
+ child2);
+
+ validate_status_exited(status, exitval2);
+
+ printf("Before calling %s() for the forkee - expected no process\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD,
+ wpid = TWAIT_GENERIC(child2, &status, 0));
+
+ printf("Before calling %s() for the child - expected stopped "
+ "SIGCHLD\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGCHLD);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child - expected exited\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child - expected no process\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+#endif
+
+ATF_TC(signal8);
+ATF_TC_HEAD(signal8, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that masking SIGTRAP in tracee does not stop tracer from "
+ "catching PTRACE_VFORK_DONE breakpoint");
+}
+
+ATF_TC_BODY(signal8, tc)
+{
+ const int exitval = 5;
+ const int exitval2 = 15;
+ const int sigval = SIGSTOP;
+ const int sigmasked = SIGTRAP;
+ pid_t child, child2, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ sigset_t intmask;
+ ptrace_state_t state;
+ const int slen = sizeof(state);
+ ptrace_event_t event;
+ const int elen = sizeof(event);
+
+ atf_tc_expect_fail("PR kern/51918");
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ sigemptyset(&intmask);
+ sigaddset(&intmask, sigmasked);
+ sigprocmask(SIG_BLOCK, &intmask, NULL);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ FORKEE_ASSERT((child2 = vfork()) != 1);
+
+ if (child2 == 0)
+ _exit(exitval2);
+
+ FORKEE_REQUIRE_SUCCESS
+ (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
+
+ forkee_status_exited(status, exitval2);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Enable PTRACE_VFORK_DONE in EVENT_MASK for the child %d\n",
+ child);
+ event.pe_set_event = PTRACE_VFORK_DONE;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigmasked);
+
+ ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
+ ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK_DONE);
+
+ child2 = state.pe_other_pid;
+ printf("Reported PTRACE_VFORK_DONE event with forkee %d\n", child2);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child - expected stopped "
+ "SIGCHLD\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, SIGCHLD);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child - expected exited\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child - expected no process\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(signal9);
+ATF_TC_HEAD(signal9, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that masking SIGTRAP in tracee does not stop tracer from "
+ "catching PTRACE_LWP_CREATE breakpoint");
+}
+
+ATF_TC_BODY(signal9, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ const int sigmasked = SIGTRAP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ sigset_t intmask;
+ ptrace_state_t state;
+ const int slen = sizeof(state);
+ ptrace_event_t event;
+ const int elen = sizeof(event);
+ ucontext_t uc;
+ lwpid_t lid;
+ static const size_t ssize = 16*1024;
+ void *stack;
+
+ atf_tc_expect_fail("PR kern/51918");
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ sigemptyset(&intmask);
+ sigaddset(&intmask, sigmasked);
+ sigprocmask(SIG_BLOCK, &intmask, NULL);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before allocating memory for stack in child\n");
+ FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
+
+ printf("Before making context for new lwp in child\n");
+ _lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
+
+ printf("Before creating new in child\n");
+ FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
+
+ printf("Before waiting for lwp %d to exit\n", lid);
+ FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
+
+ printf("Before verifying that reported %d and running lid %d "
+ "are the same\n", lid, the_lwp_id);
+ FORKEE_ASSERT_EQ(lid, the_lwp_id);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Set empty EVENT_MASK for the child %d\n", child);
+ event.pe_set_event = PTRACE_LWP_CREATE;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child - expected stopped "
+ "SIGTRAP\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigmasked);
+
+ ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
+
+ ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_CREATE);
+
+ lid = state.pe_lwp;
+ printf("Reported PTRACE_LWP_CREATE event with lid %d\n", lid);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child - expected exited\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child - expected no process\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TC(signal10);
+ATF_TC_HEAD(signal10, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Verify that masking SIGTRAP in tracee does not stop tracer from "
+ "catching PTRACE_LWP_EXIT breakpoint");
+}
+
+ATF_TC_BODY(signal10, tc)
+{
+ const int exitval = 5;
+ const int sigval = SIGSTOP;
+ const int sigmasked = SIGTRAP;
+ pid_t child, wpid;
+#if defined(TWAIT_HAVE_STATUS)
+ int status;
+#endif
+ sigset_t intmask;
+ ptrace_state_t state;
+ const int slen = sizeof(state);
+ ptrace_event_t event;
+ const int elen = sizeof(event);
+ ucontext_t uc;
+ lwpid_t lid;
+ static const size_t ssize = 16*1024;
+ void *stack;
+
+ atf_tc_expect_fail("PR kern/51918");
+
+ printf("Before forking process PID=%d\n", getpid());
+ ATF_REQUIRE((child = fork()) != -1);
+ if (child == 0) {
+ printf("Before calling PT_TRACE_ME from child %d\n", getpid());
+ FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
+
+ sigemptyset(&intmask);
+ sigaddset(&intmask, sigmasked);
+ sigprocmask(SIG_BLOCK, &intmask, NULL);
+
+ printf("Before raising %s from child\n", strsignal(sigval));
+ FORKEE_ASSERT(raise(sigval) == 0);
+
+ printf("Before allocating memory for stack in child\n");
+ FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
+
+ printf("Before making context for new lwp in child\n");
+ _lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
+
+ printf("Before creating new in child\n");
+ FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
+
+ printf("Before waiting for lwp %d to exit\n", lid);
+ FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
+
+ printf("Before verifying that reported %d and running lid %d "
+ "are the same\n", lid, the_lwp_id);
+ FORKEE_ASSERT_EQ(lid, the_lwp_id);
+
+ printf("Before exiting of the child process\n");
+ _exit(exitval);
+ }
+ printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
+
+ printf("Before calling %s() for the child\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigval);
+
+ printf("Set empty EVENT_MASK for the child %d\n", child);
+ event.pe_set_event = PTRACE_LWP_EXIT;
+ ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child - expected stopped "
+ "SIGTRAP\n", TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_stopped(status, sigmasked);
+
+ ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
+
+ ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_EXIT);
+
+ lid = state.pe_lwp;
+ printf("Reported PTRACE_LWP_EXIT event with lid %d\n", lid);
+
+ printf("Before resuming the child process where it left off and "
+ "without signal to be sent\n");
+ ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
+
+ printf("Before calling %s() for the child - expected exited\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
+
+ validate_status_exited(status, exitval);
+
+ printf("Before calling %s() for the child - expected no process\n",
+ TWAIT_FNAME);
+ TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ setvbuf(stdout, NULL, _IONBF, 0);
+ setvbuf(stderr, NULL, _IONBF, 0);
+ ATF_TP_ADD_TC(tp, traceme1);
+ ATF_TP_ADD_TC(tp, traceme2);
+ ATF_TP_ADD_TC(tp, traceme3);
+ ATF_TP_ADD_TC(tp, traceme4);
+
+ ATF_TP_ADD_TC_HAVE_PID(tp, attach1);
+ ATF_TP_ADD_TC_HAVE_PID(tp, attach2);
+ ATF_TP_ADD_TC(tp, attach3);
+ ATF_TP_ADD_TC(tp, attach4);
+ ATF_TP_ADD_TC_HAVE_PID(tp, attach5);
+ ATF_TP_ADD_TC_HAVE_PID(tp, attach6);
+ ATF_TP_ADD_TC_HAVE_PID(tp, attach7);
+
+ ATF_TP_ADD_TC(tp, eventmask1);
+ ATF_TP_ADD_TC(tp, eventmask2);
+ ATF_TP_ADD_TC(tp, eventmask3);
+ ATF_TP_ADD_TC(tp, eventmask4);
+ ATF_TP_ADD_TC(tp, eventmask5);
+ ATF_TP_ADD_TC(tp, eventmask6);
+
+ ATF_TP_ADD_TC_HAVE_PID(tp, fork1);
+ ATF_TP_ADD_TC(tp, fork2);
+
+ ATF_TP_ADD_TC_HAVE_PID(tp, vfork1);
+ ATF_TP_ADD_TC(tp, vfork2);
+
+ ATF_TP_ADD_TC(tp, vforkdone1);
+ ATF_TP_ADD_TC(tp, vforkdone2);
+
+ ATF_TP_ADD_TC(tp, io_read_d1);
+ ATF_TP_ADD_TC(tp, io_read_d2);
+ ATF_TP_ADD_TC(tp, io_read_d3);
+ ATF_TP_ADD_TC(tp, io_read_d4);
+
+ ATF_TP_ADD_TC(tp, io_write_d1);
+ ATF_TP_ADD_TC(tp, io_write_d2);
+ ATF_TP_ADD_TC(tp, io_write_d3);
+ ATF_TP_ADD_TC(tp, io_write_d4);
+
+ ATF_TP_ADD_TC(tp, read_d1);
+ ATF_TP_ADD_TC(tp, read_d2);
+ ATF_TP_ADD_TC(tp, read_d3);
+ ATF_TP_ADD_TC(tp, read_d4);
+
+ ATF_TP_ADD_TC(tp, write_d1);
+ ATF_TP_ADD_TC(tp, write_d2);
+ ATF_TP_ADD_TC(tp, write_d3);
+ ATF_TP_ADD_TC(tp, write_d4);
+
+ ATF_TP_ADD_TC(tp, io_read_d_write_d_handshake1);
+ ATF_TP_ADD_TC(tp, io_read_d_write_d_handshake2);
+
+ ATF_TP_ADD_TC(tp, read_d_write_d_handshake1);
+ ATF_TP_ADD_TC(tp, read_d_write_d_handshake2);
+
+ ATF_TP_ADD_TC(tp, io_read_i1);
+ ATF_TP_ADD_TC(tp, io_read_i2);
+ ATF_TP_ADD_TC(tp, io_read_i3);
+ ATF_TP_ADD_TC(tp, io_read_i4);
+
+ ATF_TP_ADD_TC(tp, read_i1);
+ ATF_TP_ADD_TC(tp, read_i2);
+ ATF_TP_ADD_TC(tp, read_i3);
+ ATF_TP_ADD_TC(tp, read_i4);
+
+ ATF_TP_ADD_TC(tp, io_read_auxv1);
+
+ ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs1);
+ ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs2);
+ ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs3);
+ ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs4);
+ ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs5);
+
+ ATF_TP_ADD_TC_HAVE_FPREGS(tp, fpregs1);
+ ATF_TP_ADD_TC_HAVE_FPREGS(tp, fpregs2);
+
+ ATF_TP_ADD_TC_PT_STEP(tp, step1);
+ ATF_TP_ADD_TC_PT_STEP(tp, step2);
+ ATF_TP_ADD_TC_PT_STEP(tp, step3);
+ ATF_TP_ADD_TC_PT_STEP(tp, step4);
+
+ ATF_TP_ADD_TC(tp, kill1);
+ ATF_TP_ADD_TC(tp, kill2);
+
+ ATF_TP_ADD_TC(tp, lwpinfo1);
+ ATF_TP_ADD_TC_HAVE_PID(tp, lwpinfo2);
+
+ ATF_TP_ADD_TC(tp, siginfo1);
+ ATF_TP_ADD_TC(tp, siginfo2);
+ ATF_TP_ADD_TC(tp, siginfo3);
+ ATF_TP_ADD_TC(tp, siginfo4);
+ ATF_TP_ADD_TC_HAVE_PID(tp, siginfo5);
+ ATF_TP_ADD_TC_PT_STEP(tp, siginfo6);
+
+ ATF_TP_ADD_TC(tp, lwp_create1);
+
+ ATF_TP_ADD_TC(tp, lwp_exit1);
+
+ ATF_TP_ADD_TC(tp, signal1);
+ ATF_TP_ADD_TC(tp, signal2);
+ ATF_TP_ADD_TC(tp, signal3);
+ ATF_TP_ADD_TC_PT_STEP(tp, signal4);
+ ATF_TP_ADD_TC(tp, signal5);
+ ATF_TP_ADD_TC_HAVE_PID(tp, signal6);
+ ATF_TP_ADD_TC_HAVE_PID(tp, signal7);
+ ATF_TP_ADD_TC(tp, signal8);
+ ATF_TP_ADD_TC(tp, signal9);
+ ATF_TP_ADD_TC(tp, signal10);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/t_ptrace_wait.h b/contrib/netbsd-tests/kernel/t_ptrace_wait.h
new file mode 100644
index 000000000000..9c6921c70683
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/t_ptrace_wait.h
@@ -0,0 +1,431 @@
+/* $NetBSD: t_ptrace_wait.h,v 1.7 2017/01/09 22:09:20 kamil Exp $ */
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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.
+ */
+
+/* Detect plain wait(2) use-case */
+#if !defined(TWAIT_WAITPID) && \
+ !defined(TWAIT_WAITID) && \
+ !defined(TWAIT_WAIT3) && \
+ !defined(TWAIT_WAIT4) && \
+ !defined(TWAIT_WAIT6)
+#define TWAIT_WAIT
+#endif
+
+/*
+ * There are two classes of wait(2)-like functions:
+ * - wait4(2)-like accepting pid_t, optional options parameter, struct rusage*
+ * - wait6(2)-like accepting idtype_t, id_t, struct wrusage, mandatory options
+ *
+ * The TWAIT_FNAME value is to be used for convenience in debug messages.
+ *
+ * The TWAIT_GENERIC() macro is designed to reuse the same unmodified
+ * code with as many wait(2)-like functions as possible.
+ *
+ * In a common use-case wait4(2) and wait6(2)-like function can work the almost
+ * the same way, however there are few important differences:
+ * wait6(2) must specify P_PID for idtype to match wpid from wait4(2).
+ * To behave like wait4(2), wait6(2) the 'options' to wait must include
+ * WEXITED|WTRUNCATED.
+ *
+ * There are two helper macros (they purpose it to mach more than one
+ * wait(2)-like function):
+ * The TWAIT_HAVE_STATUS - specifies whether a function can retrieve
+ * status (as integer value).
+ * The TWAIT_HAVE_PID - specifies whether a function can request
+ * exact process identifier
+ * The TWAIT_HAVE_RUSAGE - specifies whether a function can request
+ * the struct rusage value
+ *
+ */
+
+#if defined(TWAIT_WAIT)
+# define TWAIT_FNAME "wait"
+# define TWAIT_WAIT4TYPE(a,b,c,d) wait((b))
+# define TWAIT_GENERIC(a,b,c) wait((b))
+# define TWAIT_HAVE_STATUS 1
+#elif defined(TWAIT_WAITPID)
+# define TWAIT_FNAME "waitpid"
+# define TWAIT_WAIT4TYPE(a,b,c,d) waitpid((a),(b),(c))
+# define TWAIT_GENERIC(a,b,c) waitpid((a),(b),(c))
+# define TWAIT_HAVE_PID 1
+# define TWAIT_HAVE_STATUS 1
+#elif defined(TWAIT_WAITID)
+# define TWAIT_FNAME "waitid"
+# define TWAIT_GENERIC(a,b,c) \
+ waitid(P_PID,(a),NULL,(c)|WEXITED|WTRAPPED)
+# define TWAIT_WAIT6TYPE(a,b,c,d,e,f) waitid((a),(b),(f),(d))
+# define TWAIT_HAVE_PID 1
+#elif defined(TWAIT_WAIT3)
+# define TWAIT_FNAME "wait3"
+# define TWAIT_WAIT4TYPE(a,b,c,d) wait3((b),(c),(d))
+# define TWAIT_GENERIC(a,b,c) wait3((b),(c),NULL)
+# define TWAIT_HAVE_STATUS 1
+# define TWAIT_HAVE_RUSAGE 1
+#elif defined(TWAIT_WAIT4)
+# define TWAIT_FNAME "wait4"
+# define TWAIT_WAIT4TYPE(a,b,c,d) wait4((a),(b),(c),(d))
+# define TWAIT_GENERIC(a,b,c) wait4((a),(b),(c),NULL)
+# define TWAIT_HAVE_PID 1
+# define TWAIT_HAVE_STATUS 1
+# define TWAIT_HAVE_RUSAGE 1
+#elif defined(TWAIT_WAIT6)
+# define TWAIT_FNAME "wait6"
+# define TWAIT_WAIT6TYPE(a,b,c,d,e,f) wait6((a),(b),(c),(d),(e),(f))
+# define TWAIT_GENERIC(a,b,c) \
+ wait6(P_PID,(a),(b),(c)|WEXITED|WTRAPPED,NULL,NULL)
+# define TWAIT_HAVE_PID 1
+# define TWAIT_HAVE_STATUS 1
+#endif
+
+/*
+ * There are 3 groups of tests:
+ * - TWAIT_GENERIC() (wait, wait2, waitpid, wait3, wait4, wait6)
+ * - TWAIT_WAIT4TYPE() (wait2, waitpid, wait3, wait4)
+ * - TWAIT_WAIT6TYPE() (waitid, wait6)
+ *
+ * Tests only in the above categories are allowed. However some tests are not
+ * possible in the context requested functionality to be verified, therefore
+ * there are helper macros:
+ * - TWAIT_HAVE_PID (wait2, waitpid, waitid, wait4, wait6)
+ * - TWAIT_HAVE_STATUS (wait, wait2, waitpid, wait3, wait4, wait6)
+ * - TWAIT_HAVE_RUSAGE (wait3, wait4)
+ * - TWAIT_HAVE_RETPID (wait, wait2, waitpid, wait3, wait4, wait6)
+ *
+ * If there is an intention to test e.g. wait6(2) specific features in the
+ * ptrace(2) context, find the most matching group and with #ifdefs reduce
+ * functionality of less featured than wait6(2) interface (TWAIT_WAIT6TYPE).
+ *
+ * For clarity never use negative preprocessor checks, like:
+ * #if !defined(TWAIT_WAIT4)
+ * always refer to checks for positive values.
+ */
+
+#define TEST_REQUIRE_EQ(x, y) \
+do { \
+ uintmax_t vx = (x); \
+ uintmax_t vy = (y); \
+ int ret = vx == vy; \
+ if (!ret) \
+ ATF_REQUIRE_EQ_MSG(vx, vy, "%s(%ju) == %s(%ju)", \
+ #x, vx, #y, vy); \
+} while (/*CONSTCOND*/0)
+
+/*
+ * A child process cannot call atf functions and expect them to magically
+ * work like in the parent.
+ * The printf(3) messaging from a child will not work out of the box as well
+ * without estabilishing a communication protocol with its parent. To not
+ * overcomplicate the tests - do not log from a child and use err(3)/errx(3)
+ * wrapped with FORKEE_ASSERT()/FORKEE_ASSERTX() as that is guaranteed to work.
+ */
+#define FORKEE_ASSERT_EQ(x, y) \
+do { \
+ uintmax_t vx = (x); \
+ uintmax_t vy = (y); \
+ int ret = vx == vy; \
+ if (!ret) \
+ errx(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: " \
+ "%s(%ju) == %s(%ju)", __FILE__, __LINE__, __func__, \
+ #x, vx, #y, vy); \
+} while (/*CONSTCOND*/0)
+
+#define FORKEE_ASSERTX(x) \
+do { \
+ int ret = (x); \
+ if (!ret) \
+ errx(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: %s",\
+ __FILE__, __LINE__, __func__, #x); \
+} while (/*CONSTCOND*/0)
+
+#define FORKEE_ASSERT(x) \
+do { \
+ int ret = (x); \
+ if (!ret) \
+ err(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: %s",\
+ __FILE__, __LINE__, __func__, #x); \
+} while (/*CONSTCOND*/0)
+
+/*
+ * Simplify logic for functions using general purpose registers add HAVE_GPREGS
+ *
+ * For platforms that do not implement all needed calls for simplicity assume
+ * that they are unsupported at all.
+ */
+#if defined(PT_GETREGS) \
+ && defined(PT_SETREGS) \
+ && defined(PTRACE_REG_PC) \
+ && defined(PTRACE_REG_SET_PC) \
+ && defined(PTRACE_REG_SP) \
+ && defined(PTRACE_REG_INTRV)
+#define HAVE_GPREGS
+#endif
+
+/* Add guards for floating point registers */
+#if defined(PT_GETFPREGS) \
+ && defined(PT_SETFPREGS)
+#define HAVE_FPREGS
+#endif
+
+/* Add guards for cpu debug registers */
+#if defined(PT_GETDBREGS) \
+ && defined(PT_SETDBREGS)
+#define HAVE_DBREGS
+#endif
+
+/*
+ * If waitid(2) returns because one or more processes have a state change to
+ * report, 0 is returned. If an error is detected, a value of -1 is returned
+ * and errno is set to indicate the error. If WNOHANG is specified and there
+ * are no stopped, continued or exited children, 0 is returned.
+ */
+#if defined(TWAIT_WAITID)
+#define TWAIT_REQUIRE_SUCCESS(a,b) TEST_REQUIRE_EQ((a), 0)
+#define TWAIT_REQUIRE_FAILURE(a,b) ATF_REQUIRE_ERRNO((a),(b) == -1)
+#define FORKEE_REQUIRE_SUCCESS(a,b) FORKEE_ASSERT_EQ(a, 0)
+#define FORKEE_REQUIRE_FAILURE(a,b) \
+ FORKEE_ASSERTX(((a) == errno) && ((b) == -1))
+#else
+#define TWAIT_REQUIRE_SUCCESS(a,b) TEST_REQUIRE_EQ((a), (b))
+#define TWAIT_REQUIRE_FAILURE(a,b) ATF_REQUIRE_ERRNO((a),(b) == -1)
+#define FORKEE_REQUIRE_SUCCESS(a,b) FORKEE_ASSERT_EQ(a, b)
+#define FORKEE_REQUIRE_FAILURE(a,b) \
+ FORKEE_ASSERTX(((a) == errno) && ((b) == -1))
+#endif
+
+/*
+ * Helper tools to verify whether status reports exited value
+ */
+#if TWAIT_HAVE_STATUS
+static void __used
+validate_status_exited(int status, int expected)
+{
+ ATF_REQUIRE_MSG(WIFEXITED(status), "Reported !exited process");
+ ATF_REQUIRE_MSG(!WIFCONTINUED(status), "Reported continued process");
+ ATF_REQUIRE_MSG(!WIFSIGNALED(status), "Reported signaled process");
+ ATF_REQUIRE_MSG(!WIFSTOPPED(status), "Reported stopped process");
+
+ ATF_REQUIRE_EQ_MSG(WEXITSTATUS(status), expected,
+ "The process has exited with invalid value %d != %d",
+ WEXITSTATUS(status), expected);
+}
+
+static void __used
+forkee_status_exited(int status, int expected)
+{
+ FORKEE_ASSERTX(WIFEXITED(status));
+ FORKEE_ASSERTX(!WIFCONTINUED(status));
+ FORKEE_ASSERTX(!WIFSIGNALED(status));
+ FORKEE_ASSERTX(!WIFSTOPPED(status));
+
+ FORKEE_ASSERT_EQ(WEXITSTATUS(status), expected);
+}
+
+static void __used
+validate_status_continued(int status)
+{
+ ATF_REQUIRE_MSG(!WIFEXITED(status), "Reported exited process");
+ ATF_REQUIRE_MSG(WIFCONTINUED(status), "Reported !continued process");
+ ATF_REQUIRE_MSG(!WIFSIGNALED(status), "Reported signaled process");
+ ATF_REQUIRE_MSG(!WIFSTOPPED(status), "Reported stopped process");
+}
+
+static void __used
+forkee_status_continued(int status)
+{
+ FORKEE_ASSERTX(!WIFEXITED(status));
+ FORKEE_ASSERTX(WIFCONTINUED(status));
+ FORKEE_ASSERTX(!WIFSIGNALED(status));
+ FORKEE_ASSERTX(!WIFSTOPPED(status));
+}
+
+static void __used
+validate_status_signaled(int status, int expected_termsig, int expected_core)
+{
+ ATF_REQUIRE_MSG(!WIFEXITED(status), "Reported exited process");
+ ATF_REQUIRE_MSG(!WIFCONTINUED(status), "Reported continued process");
+ ATF_REQUIRE_MSG(WIFSIGNALED(status), "Reported !signaled process");
+ ATF_REQUIRE_MSG(!WIFSTOPPED(status), "Reported stopped process");
+
+ ATF_REQUIRE_EQ_MSG(WTERMSIG(status), expected_termsig,
+ "Unexpected signal received");
+
+ ATF_REQUIRE_EQ_MSG(WCOREDUMP(status), expected_core,
+ "Unexpectedly core file %s generated", expected_core ? "not" : "");
+}
+
+static void __used
+forkee_status_signaled(int status, int expected_termsig, int expected_core)
+{
+ FORKEE_ASSERTX(!WIFEXITED(status));
+ FORKEE_ASSERTX(!WIFCONTINUED(status));
+ FORKEE_ASSERTX(WIFSIGNALED(status));
+ FORKEE_ASSERTX(!WIFSTOPPED(status));
+
+ FORKEE_ASSERT_EQ(WTERMSIG(status), expected_termsig);
+ FORKEE_ASSERT_EQ(WCOREDUMP(status), expected_core);
+}
+
+static void __used
+validate_status_stopped(int status, int expected)
+{
+ ATF_REQUIRE_MSG(!WIFEXITED(status), "Reported exited process");
+ ATF_REQUIRE_MSG(!WIFCONTINUED(status), "Reported continued process");
+ ATF_REQUIRE_MSG(!WIFSIGNALED(status), "Reported signaled process");
+ ATF_REQUIRE_MSG(WIFSTOPPED(status), "Reported !stopped process");
+
+ char st[128], ex[128];
+ strlcpy(st, strsignal(WSTOPSIG(status)), sizeof(st));
+ strlcpy(ex, strsignal(expected), sizeof(ex));
+
+ ATF_REQUIRE_EQ_MSG(WSTOPSIG(status), expected,
+ "Unexpected stop signal received [%s] != [%s]", st, ex);
+}
+
+static void __used
+forkee_status_stopped(int status, int expected)
+{
+ FORKEE_ASSERTX(!WIFEXITED(status));
+ FORKEE_ASSERTX(!WIFCONTINUED(status));
+ FORKEE_ASSERTX(!WIFSIGNALED(status));
+ FORKEE_ASSERTX(WIFSTOPPED(status));
+
+ FORKEE_ASSERT_EQ(WSTOPSIG(status), expected);
+}
+#else
+#define validate_status_exited(a,b)
+#define forkee_status_exited(a,b)
+#define validate_status_continued(a,b)
+#define forkee_status_continued(a,b)
+#define validate_status_signaled(a,b,c)
+#define forkee_status_signaled(a,b,c)
+#define validate_status_stopped(a,b)
+#define forkee_status_stopped(a,b)
+#endif
+
+/* This function is currently designed to be run in the main/parent process */
+static void __used
+await_zombie(pid_t process)
+{
+ struct kinfo_proc2 p;
+ size_t len = sizeof(p);
+
+ const int name[] = {
+ [0] = CTL_KERN,
+ [1] = KERN_PROC2,
+ [2] = KERN_PROC_PID,
+ [3] = process,
+ [4] = sizeof(p),
+ [5] = 1
+ };
+
+ const size_t namelen = __arraycount(name);
+
+ /* Await the process becoming a zombie */
+ while(1) {
+ ATF_REQUIRE(sysctl(name, namelen, &p, &len, NULL, 0) == 0);
+
+ if (p.p_stat == LSZOMB)
+ break;
+
+ ATF_REQUIRE(usleep(1000) == 0);
+ }
+}
+
+/* Happy number sequence -- this function is used to just consume cpu cycles */
+#define HAPPY_NUMBER 1
+
+/* If n is not happy then its sequence ends in the cycle:
+ * 4, 16, 37, 58, 89, 145, 42, 20, 4, ... */
+#define SAD_NUMBER 4
+
+/* Calculate the sum of the squares of the digits of n */
+static unsigned __used
+dsum(unsigned n)
+{
+ unsigned sum, x;
+ for (sum = 0; n; n /= 10) {
+ x = n % 10;
+ sum += x * x;
+ }
+ return sum;
+}
+
+/*
+ * XXX: Disabled optimization is required to make tests for hardware assisted
+ * traps in .text functional
+ *
+ * Tested with GCC 5.4 on NetBSD 7.99.47 amd64
+ */
+static int __used
+#ifdef __clang__
+__attribute__((__optnone__))
+#else
+__attribute__((__optimize__("O0")))
+#endif
+check_happy(unsigned n)
+{
+ for (;;) {
+ unsigned total = dsum(n);
+
+ if (total == HAPPY_NUMBER)
+ return 1;
+ if (total == SAD_NUMBER)
+ return 0;
+
+ n = total;
+ }
+}
+
+#if defined(TWAIT_HAVE_PID)
+#define ATF_TP_ADD_TC_HAVE_PID(a,b) ATF_TP_ADD_TC(a,b)
+#else
+#define ATF_TP_ADD_TC_HAVE_PID(a,b)
+#endif
+
+#if defined(HAVE_GPREGS)
+#define ATF_TP_ADD_TC_HAVE_GPREGS(a,b) ATF_TP_ADD_TC(a,b)
+#else
+#define ATF_TP_ADD_TC_HAVE_GPREGS(a,b)
+#endif
+
+#if defined(HAVE_FPREGS)
+#define ATF_TP_ADD_TC_HAVE_FPREGS(a,b) ATF_TP_ADD_TC(a,b)
+#else
+#define ATF_TP_ADD_TC_HAVE_FPREGS(a,b)
+#endif
+
+#if defined(PT_STEP)
+#define ATF_TP_ADD_TC_PT_STEP(a,b) ATF_TP_ADD_TC(a,b)
+#else
+#define ATF_TP_ADD_TC_PT_STEP(a,b)
+#endif
+
+#if defined(__HAVE_PTRACE_WATCHPOINTS)
+#define ATF_TP_ADD_TC_HAVE_PTRACE_WATCHPOINTS(a,b) ATF_TP_ADD_TC(a,b)
+#else
+#define ATF_TP_ADD_TC_HAVE_PTRACE_WATCHPOINTS(a,b)
+#endif
diff --git a/contrib/netbsd-tests/kernel/t_ptrace_wait3.c b/contrib/netbsd-tests/kernel/t_ptrace_wait3.c
new file mode 100644
index 000000000000..c136431af818
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/t_ptrace_wait3.c
@@ -0,0 +1,30 @@
+/* $NetBSD: t_ptrace_wait3.c,v 1.1 2016/11/07 21:09:03 kamil Exp $ */
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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.
+ */
+
+#define TWAIT_WAIT3
+#include "t_ptrace_wait.c"
diff --git a/contrib/netbsd-tests/kernel/t_ptrace_wait4.c b/contrib/netbsd-tests/kernel/t_ptrace_wait4.c
new file mode 100644
index 000000000000..7ae771bb7075
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/t_ptrace_wait4.c
@@ -0,0 +1,30 @@
+/* $NetBSD: t_ptrace_wait4.c,v 1.1 2016/11/07 21:09:03 kamil Exp $ */
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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.
+ */
+
+#define TWAIT_WAIT4
+#include "t_ptrace_wait.c"
diff --git a/contrib/netbsd-tests/kernel/t_ptrace_wait6.c b/contrib/netbsd-tests/kernel/t_ptrace_wait6.c
new file mode 100644
index 000000000000..122cb1d9a1cc
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/t_ptrace_wait6.c
@@ -0,0 +1,30 @@
+/* $NetBSD: t_ptrace_wait6.c,v 1.1 2016/11/07 21:09:03 kamil Exp $ */
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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.
+ */
+
+#define TWAIT_WAIT6
+#include "t_ptrace_wait.c"
diff --git a/contrib/netbsd-tests/kernel/t_ptrace_waitid.c b/contrib/netbsd-tests/kernel/t_ptrace_waitid.c
new file mode 100644
index 000000000000..1c09be70d8a5
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/t_ptrace_waitid.c
@@ -0,0 +1,30 @@
+/* $NetBSD: t_ptrace_waitid.c,v 1.1 2016/11/07 21:09:03 kamil Exp $ */
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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.
+ */
+
+#define TWAIT_WAITID
+#include "t_ptrace_wait.c"
diff --git a/contrib/netbsd-tests/kernel/t_ptrace_waitpid.c b/contrib/netbsd-tests/kernel/t_ptrace_waitpid.c
new file mode 100644
index 000000000000..c8f107dbda51
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/t_ptrace_waitpid.c
@@ -0,0 +1,30 @@
+/* $NetBSD: t_ptrace_waitpid.c,v 1.1 2016/11/07 21:09:03 kamil Exp $ */
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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.
+ */
+
+#define TWAIT_WAITPID
+#include "t_ptrace_wait.c"
diff --git a/contrib/netbsd-tests/kernel/t_pty.c b/contrib/netbsd-tests/kernel/t_pty.c
new file mode 100644
index 000000000000..3b079ef616ba
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/t_pty.c
@@ -0,0 +1,351 @@
+/* $Id: t_pty.c,v 1.2 2017/01/13 21:30:41 christos Exp $ */
+
+/*
+ * Allocates a pty(4) device, and sends the specified number of packets of the
+ * specified length though it, while a child reader process reads and reports
+ * results.
+ *
+ * Written by Matthew Mondor
+ */
+
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: t_pty.c,v 1.2 2017/01/13 21:30:41 christos Exp $");
+
+#include <errno.h>
+#include <err.h>
+#include <fcntl.h>
+#include <poll.h>
+#include <stdio.h>
+#ifdef __linux__
+#define _XOPEN_SOURCE
+#define __USE_XOPEN
+#endif
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <termios.h>
+#include <unistd.h>
+
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#ifdef STANDALONE
+static __dead void usage(const char *);
+static void parse_args(int, char **);
+#else
+#include <atf-c.h>
+#include "h_macros.h"
+#endif
+
+static int pty_open(void);
+static int tty_open(const char *);
+static void fd_nonblock(int);
+static pid_t child_spawn(const char *);
+static void run(void);
+
+static size_t buffer_size = 4096;
+static size_t packets = 2;
+static uint8_t *dbuf;
+static int verbose;
+static int qsize;
+
+
+static
+void run(void)
+{
+ size_t i;
+ int pty;
+ int status;
+ pid_t child;
+ if ((dbuf = calloc(1, buffer_size)) == NULL)
+ err(EXIT_FAILURE, "malloc(%zu)", buffer_size);
+
+ if (verbose)
+ (void)printf(
+ "parent: started; opening PTY and spawning child\n");
+ pty = pty_open();
+ child = child_spawn(ptsname(pty));
+ if (verbose)
+ (void)printf("parent: sleeping to make sure child is ready\n");
+ (void)sleep(1);
+
+ for (i = 0; i < buffer_size; i++)
+ dbuf[i] = i & 0xff;
+
+ if (verbose)
+ (void)printf("parent: writing\n");
+
+ for (i = 0; i < packets; i++) {
+ ssize_t size;
+
+ if (verbose)
+ (void)printf(
+ "parent: attempting to write %zu bytes to PTY\n",
+ buffer_size);
+ if ((size = write(pty, dbuf, buffer_size)) == -1) {
+ err(EXIT_FAILURE, "parent: write()");
+ break;
+ }
+ if (verbose)
+ (void)printf("parent: wrote %zd bytes to PTY\n", size);
+ }
+
+ if (verbose)
+ (void)printf("parent: waiting for child to exit\n");
+ if (waitpid(child, &status, 0) == -1)
+ err(EXIT_FAILURE, "waitpid");
+ if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
+ errx(EXIT_FAILURE, "child failed");
+
+ if (verbose)
+ (void)printf("parent: closing PTY\n");
+ (void)close(pty);
+ if (verbose)
+ (void)printf("parent: exiting\n");
+}
+
+static void
+condition(int fd)
+{
+ struct termios tios;
+
+ if (qsize) {
+ int opt = qsize;
+ if (ioctl(fd, TIOCSQSIZE, &opt) == -1)
+ err(EXIT_FAILURE, "Couldn't set tty(4) buffer size");
+ if (ioctl(fd, TIOCGQSIZE, &opt) == -1)
+ err(EXIT_FAILURE, "Couldn't get tty(4) buffer size");
+ if (opt != qsize)
+ errx(EXIT_FAILURE, "Wrong qsize %d != %d\n",
+ qsize, opt);
+ }
+ if (tcgetattr(fd, &tios) == -1)
+ err(EXIT_FAILURE, "tcgetattr()");
+ cfmakeraw(&tios);
+ cfsetspeed(&tios, B921600);
+ if (tcsetattr(fd, TCSANOW, &tios) == -1)
+ err(EXIT_FAILURE, "tcsetattr()");
+}
+
+static int
+pty_open(void)
+{
+ int fd;
+
+ if ((fd = posix_openpt(O_RDWR)) == -1)
+ err(EXIT_FAILURE, "Couldn't pty(4) device");
+ condition(fd);
+ if (grantpt(fd) == -1)
+ err(EXIT_FAILURE,
+ "Couldn't grant permissions on tty(4) device");
+
+
+ condition(fd);
+
+ if (unlockpt(fd) == -1)
+ err(EXIT_FAILURE, "unlockpt()");
+
+ return fd;
+}
+
+static int
+tty_open(const char *ttydev)
+{
+ int fd;
+
+ if ((fd = open(ttydev, O_RDWR, 0)) == -1)
+ err(EXIT_FAILURE, "Couldn't open tty(4) device");
+
+#ifdef USE_PPP_DISCIPLINE
+ {
+ int opt = PPPDISC;
+ if (ioctl(fd, TIOCSETD, &opt) == -1)
+ err(EXIT_FAILURE,
+ "Couldn't set tty(4) discipline to PPP");
+ }
+#endif
+
+ condition(fd);
+
+ return fd;
+}
+
+static void
+fd_nonblock(int fd)
+{
+ int opt;
+
+ if ((opt = fcntl(fd, F_GETFL, NULL)) == -1)
+ err(EXIT_FAILURE, "fcntl()");
+ if (fcntl(fd, F_SETFL, opt | O_NONBLOCK) == -1)
+ err(EXIT_FAILURE, "fcntl()");
+}
+
+static pid_t
+child_spawn(const char *ttydev)
+{
+ pid_t pid;
+ int tty;
+ struct pollfd pfd;
+ size_t total = 0;
+
+ if ((pid = fork()) == -1)
+ err(EXIT_FAILURE, "fork()");
+ (void)setsid();
+ if (pid != 0)
+ return pid;
+
+ if (verbose)
+ (void)printf("child: started; open \"%s\"\n", ttydev);
+ tty = tty_open(ttydev);
+ fd_nonblock(tty);
+
+ if (verbose)
+ (void)printf("child: TTY open, starting read loop\n");
+ pfd.fd = tty;
+ pfd.events = POLLIN;
+ pfd.revents = 0;
+ for (;;) {
+ int ret;
+ ssize_t size;
+
+ if (verbose)
+ (void)printf("child: polling\n");
+ if ((ret = poll(&pfd, 1, 2000)) == -1)
+ err(EXIT_FAILURE, "child: poll()");
+ if (ret == 0)
+ break;
+ if ((pfd.revents & POLLERR) != 0)
+ break;
+ if ((pfd.revents & POLLIN) != 0) {
+ for (;;) {
+ if (verbose)
+ (void)printf(
+ "child: attempting to read %zu"
+ " bytes\n", buffer_size);
+ if ((size = read(tty, dbuf, buffer_size))
+ == -1) {
+ if (errno == EAGAIN)
+ break;
+ err(EXIT_FAILURE, "child: read()");
+ }
+ if (qsize && size < qsize &&
+ (size_t)size < buffer_size)
+ errx(EXIT_FAILURE, "read returned %zd "
+ "less than the queue size %d",
+ size, qsize);
+ if (verbose)
+ (void)printf(
+ "child: read %zd bytes from TTY\n",
+ size);
+ if (size == 0)
+ goto end;
+ total += size;
+ }
+ }
+ }
+end:
+ if (verbose)
+ (void)printf("child: closing TTY %zu\n", total);
+ (void)close(tty);
+ if (verbose)
+ (void)printf("child: exiting\n");
+ if (total != buffer_size * packets)
+ errx(EXIT_FAILURE,
+ "Lost data %zu != %zu\n", total, buffer_size * packets);
+
+ exit(EXIT_SUCCESS);
+}
+
+#ifdef STANDALONE
+static void
+usage(const char *msg)
+{
+
+ if (msg != NULL)
+ (void) fprintf(stderr, "\n%s\n\n", msg);
+
+ (void)fprintf(stderr,
+ "Usage: %s [-v] [-q <qsize>] [-s <packetsize>] [-n <packets>]\n",
+ getprogname());
+
+ exit(EXIT_FAILURE);
+}
+
+static void
+parse_args(int argc, char **argv)
+{
+ int ch;
+
+ while ((ch = getopt(argc, argv, "n:q:s:v")) != -1) {
+ switch (ch) {
+ case 'n':
+ packets = (size_t)atoi(optarg);
+ break;
+ case 'q':
+ qsize = atoi(optarg);
+ break;
+ case 's':
+ buffer_size = (size_t)atoi(optarg);
+ break;
+ case 'v':
+ verbose++;
+ break;
+ default:
+ usage(NULL);
+ break;
+ }
+ }
+ if (buffer_size < 0 || buffer_size > 65536)
+ usage("-s must be between 0 and 65536");
+ if (packets < 1 || packets > 100)
+ usage("-p must be between 1 and 100");
+}
+
+int
+main(int argc, char **argv)
+{
+
+ parse_args(argc, argv);
+ run();
+ exit(EXIT_SUCCESS);
+}
+
+#else
+ATF_TC(pty_no_queue);
+
+ATF_TC_HEAD(pty_no_queue, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Checks that writing to pty "
+ "does not lose data with the default queue size of 1024");
+}
+
+ATF_TC_BODY(pty_no_queue, tc)
+{
+ qsize = 0;
+ run();
+}
+
+ATF_TC(pty_queue);
+
+ATF_TC_HEAD(pty_queue, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Checks that writing to pty "
+ "does not lose data with the a queue size of 4096");
+}
+
+ATF_TC_BODY(pty_queue, tc)
+{
+ qsize = 4096;
+ run();
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, pty_no_queue);
+ ATF_TP_ADD_TC(tp, pty_queue);
+
+ return atf_no_error();
+}
+#endif
diff --git a/contrib/netbsd-tests/kernel/t_rnd.c b/contrib/netbsd-tests/kernel/t_rnd.c
new file mode 100644
index 000000000000..ae9ef5572023
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/t_rnd.c
@@ -0,0 +1,114 @@
+/* $NetBSD: t_rnd.c,v 1.10 2017/01/13 21:30:41 christos Exp $ */
+
+/*
+ * Copyright (c) 2009 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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_rnd.c,v 1.10 2017/01/13 21:30:41 christos Exp $");
+
+#include <sys/types.h>
+#include <sys/fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/rndio.h>
+
+#include <atf-c.h>
+
+#include <rump/rump.h>
+#include <rump/rump_syscalls.h>
+
+#include "h_macros.h"
+
+ATF_TC(RNDADDDATA);
+ATF_TC_HEAD(RNDADDDATA, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Checks ioctl(RNDADDDATA) (PR kern/42020)");
+}
+
+/* Adapted from example provided by Juho Salminen in the noted PR. */
+ATF_TC_BODY(RNDADDDATA, tc)
+{
+ rnddata_t rd;
+ int fd;
+
+ rump_init();
+ fd = rump_sys_open("/dev/random", O_RDWR, 0);
+ if (fd == -1)
+ atf_tc_fail_errno("cannot open /dev/random");
+
+ rd.entropy = 1;
+ rd.len = 1;
+ if (rump_sys_ioctl(fd, RNDADDDATA, &rd) == -1)
+ atf_tc_fail_errno("RNDADDDATA");
+}
+
+ATF_TC(RNDADDDATA2);
+ATF_TC_HEAD(RNDADDDATA2, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "checks ioctl(RNDADDDATA) deals with "
+ "garbage len field");
+}
+ATF_TC_BODY(RNDADDDATA2, tc)
+{
+ rnddata_t rd;
+ int fd;
+
+ rump_init();
+ fd = rump_sys_open("/dev/random", O_RDWR, 0);
+ if (fd == -1)
+ atf_tc_fail_errno("cannot open /dev/random");
+
+ rd.entropy = 1;
+ rd.len = -1;
+ ATF_REQUIRE_ERRNO(EINVAL, rump_sys_ioctl(fd, RNDADDDATA, &rd) == -1);
+}
+
+ATF_TC(read_random);
+ATF_TC_HEAD(read_random, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "does reading /dev/random return "
+ "within reasonable time");
+ atf_tc_set_md_var(tc, "timeout", "10");
+}
+
+ATF_TC_BODY(read_random, tc)
+{
+ char buf[128];
+ int fd;
+
+ rump_init();
+ RL(fd = rump_sys_open("/dev/random", O_RDONLY));
+ RL(rump_sys_read(fd, buf, sizeof(buf)));
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, RNDADDDATA);
+ ATF_TP_ADD_TC(tp, RNDADDDATA2);
+ ATF_TP_ADD_TC(tp, read_random);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/t_sysctl.c b/contrib/netbsd-tests/kernel/t_sysctl.c
new file mode 100644
index 000000000000..8b7c97eb3516
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/t_sysctl.c
@@ -0,0 +1,74 @@
+/* $NetBSD: t_sysctl.c,v 1.1 2014/08/09 07:04:03 gson Exp $ */
+
+/*-
+ * Copyright (c) 2014 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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>
+__COPYRIGHT("@(#) Copyright (c) 2014\
+ The NetBSD Foundation, inc. All rights reserved.");
+__RCSID("$NetBSD: t_sysctl.c,v 1.1 2014/08/09 07:04:03 gson Exp $");
+
+#include <sys/sysctl.h>
+#include <errno.h>
+#include <memory.h>
+
+#include <atf-c.h>
+
+ATF_TC(bufsize);
+ATF_TC_HEAD(bufsize, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Test sysctl integer reads with different buffer sizes");
+}
+ATF_TC_BODY(bufsize, tc)
+{
+ union {
+ int int_val;
+ unsigned char space[256];
+ } buf;
+ size_t len;
+ for (len = 0; len < sizeof(buf); len++) {
+ size_t oldlen = len;
+ int r;
+ memset(&buf, 0xFF, sizeof(buf));
+ r = sysctlbyname("kern.job_control", &buf, &oldlen, 0, (size_t) 0);
+ if (len < sizeof(int)) {
+ ATF_REQUIRE_EQ(r, -1);
+ ATF_REQUIRE_EQ(errno, ENOMEM);
+ } else {
+ ATF_REQUIRE_EQ(r, 0);
+ ATF_REQUIRE_EQ(buf.int_val, 1);
+ ATF_REQUIRE_EQ(oldlen, sizeof(int));
+ }
+ }
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, bufsize);
+
+ return atf_no_error();
+}
diff --git a/contrib/netbsd-tests/kernel/t_sysv.c b/contrib/netbsd-tests/kernel/t_sysv.c
new file mode 100644
index 000000000000..847c9d83824d
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/t_sysv.c
@@ -0,0 +1,819 @@
+/* $NetBSD: t_sysv.c,v 1.4 2014/03/02 20:13:12 jmmv Exp $ */
+
+/*-
+ * Copyright (c) 1999, 2007 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
+ * NASA Ames Research Center, and by Andrew Doran.
+ *
+ * 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.
+ */
+
+/*
+ * Test the SVID-compatible Message Queue facility.
+ */
+
+#include <atf-c.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <sys/ipc.h>
+#include <sys/mman.h>
+#include <sys/msg.h>
+#include <sys/param.h>
+#include <sys/sem.h>
+#include <sys/shm.h>
+#include <sys/wait.h>
+
+volatile int did_sigsys;
+
+void sigsys_handler(int);
+
+key_t get_ftok(int);
+
+void print_msqid_ds(struct msqid_ds *, mode_t);
+void receiver(void);
+
+void print_semid_ds(struct semid_ds *, mode_t);
+void waiter(void);
+
+void print_shmid_ds(struct shmid_ds *, mode_t);
+void sharer(void);
+
+#define MESSAGE_TEXT_LEN 256
+
+struct testmsg {
+ long mtype;
+ char mtext[MESSAGE_TEXT_LEN];
+};
+
+const char *m1_str = "California is overrated.";
+const char *m2_str = "The quick brown fox jumped over the lazy dog.";
+
+size_t pgsize;
+
+#define MTYPE_1 1
+#define MTYPE_1_ACK 2
+
+#define MTYPE_2 3
+#define MTYPE_2_ACK 4
+
+pid_t child_pid;
+
+key_t msgkey, semkey, shmkey;
+
+int maxloop = 1;
+
+union semun {
+ int val; /* value for SETVAL */
+ struct semid_ds *buf; /* buffer for IPC_{STAT,SET} */
+ u_short *array; /* array for GETALL & SETALL */
+};
+
+
+/* Writes an integer to a file. To be used from the body of the test
+ * cases below to pass any global identifiers to the cleanup routine. */
+static void
+write_int(const char *path, const int value)
+{
+ int output;
+
+ output = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
+ ATF_REQUIRE_MSG(output != -1, "Failed to create %s", path);
+ write(output, &value, sizeof(value));
+ close(output);
+}
+
+
+/* Reads an integer from a file. To be used from the cleanup routines
+ * of the test cases below. */
+static int
+read_int(const char *path)
+{
+ int input;
+
+ input = open(path, O_RDONLY);
+ if (input == -1)
+ return -1;
+ else {
+ int value;
+ ATF_REQUIRE_EQ(read(input, &value, sizeof(value)), sizeof(value));
+ close(input);
+ return value;
+ }
+}
+
+
+void
+sigsys_handler(int signo)
+{
+
+ did_sigsys = 1;
+}
+
+key_t get_ftok(int id)
+{
+ int fd;
+ char token_key[64], token_dir[64];
+ char *tmpdir;
+ key_t key;
+
+ strlcpy(token_key, "/tmp/t_sysv.XXXXXX", sizeof(token_key));
+ tmpdir = mkdtemp(token_key);
+ ATF_REQUIRE_MSG(tmpdir != NULL, "mkdtemp() failed: %d", errno);
+
+ strlcpy(token_dir, tmpdir, sizeof(token_dir));
+ strlcpy(token_key, tmpdir, sizeof(token_key));
+ strlcat(token_key, "/token_key", sizeof(token_key));
+
+ /* Create the file, since ftok() requires it to exist! */
+
+ fd = open(token_key, O_RDWR | O_CREAT | O_EXCL, 0600);
+ if (fd == -1) {
+ rmdir(tmpdir);
+ atf_tc_fail("open() of temp file failed: %d", errno);
+ return (key_t)-1;
+ } else
+ close(fd);
+
+ key = ftok(token_key, id);
+
+ ATF_REQUIRE_MSG(unlink(token_key) != -1, "unlink() failed: %d", errno);
+ ATF_REQUIRE_MSG(rmdir(token_dir) != -1, "rmdir() failed: %d", errno);
+
+ return key;
+}
+
+ATF_TC_WITH_CLEANUP(msg);
+ATF_TC_HEAD(msg, tc)
+{
+
+ atf_tc_set_md_var(tc, "timeout", "3");
+ atf_tc_set_md_var(tc, "descr", "Checks sysvmsg passing");
+}
+
+ATF_TC_BODY(msg, tc)
+{
+ struct sigaction sa;
+ struct msqid_ds m_ds;
+ struct testmsg m;
+ int sender_msqid;
+ int loop;
+ int c_status;
+ pid_t wait_result;
+
+ /*
+ * Install a SIGSYS handler so that we can exit gracefully if
+ * System V Message Queue support isn't in the kernel.
+ */
+ did_sigsys = 0;
+ sa.sa_handler = sigsys_handler;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ ATF_REQUIRE_MSG(sigaction(SIGSYS, &sa, NULL) != -1,
+ "sigaction SIGSYS: %d", errno);
+
+ msgkey = get_ftok(4160);
+ ATF_REQUIRE_MSG(msgkey != (key_t)-1, "get_ftok failed");
+
+ sender_msqid = msgget(msgkey, IPC_CREAT | 0640);
+ ATF_REQUIRE_MSG(sender_msqid != -1, "msgget: %d", errno);
+ write_int("sender_msqid", sender_msqid);
+
+ if (did_sigsys) {
+ atf_tc_skip("SYSV Message Queue not supported");
+ return;
+ }
+
+ ATF_REQUIRE_MSG(msgctl(sender_msqid, IPC_STAT, &m_ds) != -1,
+ "msgctl IPC_STAT 1: %d", errno);
+
+ print_msqid_ds(&m_ds, 0640);
+
+ m_ds.msg_perm.mode = (m_ds.msg_perm.mode & ~0777) | 0600;
+
+ ATF_REQUIRE_MSG(msgctl(sender_msqid, IPC_SET, &m_ds) != -1,
+ "msgctl IPC_SET: %d", errno);
+
+ memset(&m_ds, 0, sizeof(m_ds));
+
+ ATF_REQUIRE_MSG(msgctl(sender_msqid, IPC_STAT, &m_ds) != -1,
+ "msgctl IPC_STAT 2: %d", errno);
+
+ ATF_REQUIRE_MSG((m_ds.msg_perm.mode & 0777) == 0600,
+ "IPC_SET of mode didn't hold");
+
+ print_msqid_ds(&m_ds, 0600);
+
+ fflush(stdout);
+
+ switch ((child_pid = fork())) {
+ case -1:
+ atf_tc_fail("fork: %d", errno);
+ return;
+
+ case 0:
+ receiver();
+ break;
+
+ default:
+ break;
+ }
+
+ for (loop = 0; loop < maxloop; loop++) {
+ /*
+ * Send the first message to the receiver and wait for the ACK.
+ */
+ m.mtype = MTYPE_1;
+ strlcpy(m.mtext, m1_str, sizeof(m.mtext));
+ ATF_REQUIRE_MSG(msgsnd(sender_msqid, &m, MESSAGE_TEXT_LEN,
+ 0) != -1, "sender: msgsnd 1: %d", errno);
+
+ ATF_REQUIRE_MSG(msgrcv(sender_msqid, &m, MESSAGE_TEXT_LEN,
+ MTYPE_1_ACK, 0) == MESSAGE_TEXT_LEN,
+ "sender: msgrcv 1 ack: %d", errno);
+
+ print_msqid_ds(&m_ds, 0600);
+
+ /*
+ * Send the second message to the receiver and wait for the ACK.
+ */
+ m.mtype = MTYPE_2;
+ strlcpy(m.mtext, m2_str, sizeof(m.mtext));
+ ATF_REQUIRE_MSG(msgsnd(sender_msqid, &m, MESSAGE_TEXT_LEN, 0) != -1,
+ "sender: msgsnd 2: %d", errno);
+
+ ATF_REQUIRE_MSG(msgrcv(sender_msqid, &m, MESSAGE_TEXT_LEN,
+ MTYPE_2_ACK, 0) == MESSAGE_TEXT_LEN,
+ "sender: msgrcv 2 ack: %d", errno);
+ }
+
+ /*
+ * Wait for child to finish
+ */
+ wait_result = wait(&c_status);
+ ATF_REQUIRE_EQ_MSG(wait_result, child_pid, "wait returned %d (%s)",
+ wait_result, wait_result == -1 ? strerror(errno) : "");
+ ATF_REQUIRE_MSG(WIFEXITED(c_status), "child abnormal exit: %d (sig %d)",
+ c_status, WTERMSIG(c_status));
+ ATF_REQUIRE_EQ_MSG(WEXITSTATUS(c_status), 0, "child status: %d",
+ WEXITSTATUS(c_status));
+
+ ATF_REQUIRE_MSG(msgctl(sender_msqid, IPC_STAT, &m_ds) != -1,
+ "msgctl IPC_STAT: %d", errno);
+
+ print_msqid_ds(&m_ds, 0600);
+}
+
+ATF_TC_CLEANUP(msg, tc)
+{
+ int sender_msqid;
+
+ /*
+ * Remove the message queue if it exists.
+ */
+ sender_msqid = read_int("sender_msqid");
+ if (sender_msqid != -1)
+ if (msgctl(sender_msqid, IPC_RMID, NULL) == -1)
+ err(1, "msgctl IPC_RMID");
+}
+
+void
+print_msqid_ds(struct msqid_ds *mp, mode_t mode)
+{
+ uid_t uid = geteuid();
+ gid_t gid = getegid();
+
+ printf("PERM: uid %d, gid %d, cuid %d, cgid %d, mode 0%o\n",
+ mp->msg_perm.uid, mp->msg_perm.gid,
+ mp->msg_perm.cuid, mp->msg_perm.cgid,
+ mp->msg_perm.mode & 0777);
+
+ printf("qnum %lu, qbytes %lu, lspid %d, lrpid %d\n",
+ mp->msg_qnum, (u_long)mp->msg_qbytes, mp->msg_lspid,
+ mp->msg_lrpid);
+
+ printf("stime: %s", ctime(&mp->msg_stime));
+ printf("rtime: %s", ctime(&mp->msg_rtime));
+ printf("ctime: %s", ctime(&mp->msg_ctime));
+
+ /*
+ * Sanity check a few things.
+ */
+
+ ATF_REQUIRE_MSG(mp->msg_perm.uid == uid && mp->msg_perm.cuid == uid,
+ "uid mismatch");
+
+ ATF_REQUIRE_MSG(mp->msg_perm.gid == gid && mp->msg_perm.cgid == gid,
+ "gid mismatch");
+
+ ATF_REQUIRE_MSG((mp->msg_perm.mode & 0777) == mode, "mode mismatch");
+}
+
+void
+receiver(void)
+{
+ struct testmsg m;
+ int msqid, loop;
+
+ if ((msqid = msgget(msgkey, 0)) == -1)
+ err(1, "receiver: msgget");
+
+ for (loop = 0; loop < maxloop; loop++) {
+ /*
+ * Receive the first message, print it, and send an ACK.
+ */
+ if (msgrcv(msqid, &m, MESSAGE_TEXT_LEN, MTYPE_1, 0) != MESSAGE_TEXT_LEN)
+ err(1, "receiver: msgrcv 1");
+
+ printf("%s\n", m.mtext);
+ if (strcmp(m.mtext, m1_str) != 0)
+ errx(1, "receiver: message 1 data isn't correct");
+
+ m.mtype = MTYPE_1_ACK;
+
+ if (msgsnd(msqid, &m, MESSAGE_TEXT_LEN, 0) == -1)
+ err(1, "receiver: msgsnd ack 1");
+
+ /*
+ * Receive the second message, print it, and send an ACK.
+ */
+
+ if (msgrcv(msqid, &m, MESSAGE_TEXT_LEN, MTYPE_2, 0) != MESSAGE_TEXT_LEN)
+ err(1, "receiver: msgrcv 2");
+
+ printf("%s\n", m.mtext);
+ if (strcmp(m.mtext, m2_str) != 0)
+ errx(1, "receiver: message 2 data isn't correct");
+
+ m.mtype = MTYPE_2_ACK;
+
+ if (msgsnd(msqid, &m, MESSAGE_TEXT_LEN, 0) == -1)
+ err(1, "receiver: msgsnd ack 2");
+ }
+
+ exit(0);
+}
+
+/*
+ * Test the SVID-compatible Semaphore facility.
+ */
+
+ATF_TC_WITH_CLEANUP(sem);
+ATF_TC_HEAD(sem, tc)
+{
+
+ atf_tc_set_md_var(tc, "timeout", "3");
+ atf_tc_set_md_var(tc, "descr", "Checks sysvmsg passing");
+}
+
+ATF_TC_BODY(sem, tc)
+{
+ struct sigaction sa;
+ union semun sun;
+ struct semid_ds s_ds;
+ int sender_semid;
+ int i;
+ int c_status;
+ int child_count;
+ pid_t wait_result;
+
+ /*
+ * Install a SIGSYS handler so that we can exit gracefully if
+ * System V Semaphore support isn't in the kernel.
+ */
+ did_sigsys = 0;
+ sa.sa_handler = sigsys_handler;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ ATF_REQUIRE_MSG(sigaction(SIGSYS, &sa, NULL) != -1,
+ "sigaction SIGSYS: %d", errno);
+
+ semkey = get_ftok(4160);
+ ATF_REQUIRE_MSG(semkey != (key_t)-1, "get_ftok failed");
+
+ sender_semid = semget(semkey, 1, IPC_CREAT | 0640);
+ ATF_REQUIRE_MSG(sender_semid != -1, "semget: %d", errno);
+ write_int("sender_semid", sender_semid);
+
+ if (did_sigsys) {
+ atf_tc_skip("SYSV Semaphore not supported");
+ return;
+ }
+
+ sun.buf = &s_ds;
+ ATF_REQUIRE_MSG(semctl(sender_semid, 0, IPC_STAT, sun) != -1,
+ "semctl IPC_STAT: %d", errno);
+
+ print_semid_ds(&s_ds, 0640);
+
+ s_ds.sem_perm.mode = (s_ds.sem_perm.mode & ~0777) | 0600;
+
+ sun.buf = &s_ds;
+ ATF_REQUIRE_MSG(semctl(sender_semid, 0, IPC_SET, sun) != -1,
+ "semctl IPC_SET: %d", errno);
+
+ memset(&s_ds, 0, sizeof(s_ds));
+
+ sun.buf = &s_ds;
+ ATF_REQUIRE_MSG(semctl(sender_semid, 0, IPC_STAT, sun) != -1,
+ "semctl IPC_STAT: %d", errno);
+
+ ATF_REQUIRE_MSG((s_ds.sem_perm.mode & 0777) == 0600,
+ "IPC_SET of mode didn't hold");
+
+ print_semid_ds(&s_ds, 0600);
+
+ fflush(stdout);
+
+ for (child_count = 0; child_count < 5; child_count++) {
+ switch ((child_pid = fork())) {
+ case -1:
+ atf_tc_fail("fork: %d", errno);
+ return;
+
+ case 0:
+ waiter();
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ /*
+ * Wait for all of the waiters to be attempting to acquire the
+ * semaphore.
+ */
+ for (;;) {
+ i = semctl(sender_semid, 0, GETNCNT);
+ if (i == -1)
+ atf_tc_fail("semctl GETNCNT: %d", i);
+ if (i == 5)
+ break;
+ }
+
+ /*
+ * Now set the thundering herd in motion by initializing the
+ * semaphore to the value 1.
+ */
+ sun.val = 1;
+ ATF_REQUIRE_MSG(semctl(sender_semid, 0, SETVAL, sun) != -1,
+ "sender: semctl SETVAL to 1: %d", errno);
+
+ /*
+ * Wait for all children to finish
+ */
+ while (child_count-- > 0) {
+ wait_result = wait(&c_status);
+ ATF_REQUIRE_MSG(wait_result != -1, "wait failed: %s",
+ strerror(errno));
+ ATF_REQUIRE_MSG(WIFEXITED(c_status),
+ "child abnormal exit: %d (sig %d)",
+ c_status, WTERMSIG(c_status));
+ ATF_REQUIRE_EQ_MSG(WEXITSTATUS(c_status), 0, "child status: %d",
+ WEXITSTATUS(c_status));
+
+ sun.buf = &s_ds;
+ ATF_REQUIRE_MSG(semctl(sender_semid, 0, IPC_STAT, sun) != -1,
+ "semctl IPC_STAT: %d", errno);
+
+ print_semid_ds(&s_ds, 0600);
+ }
+}
+
+ATF_TC_CLEANUP(sem, tc)
+{
+ int sender_semid;
+
+ /*
+ * Remove the semaphore if it exists
+ */
+ sender_semid = read_int("sender_semid");
+ if (sender_semid != -1)
+ if (semctl(sender_semid, 0, IPC_RMID) == -1)
+ err(1, "semctl IPC_RMID");
+}
+
+void
+print_semid_ds(struct semid_ds *sp, mode_t mode)
+{
+ uid_t uid = geteuid();
+ gid_t gid = getegid();
+
+ printf("PERM: uid %d, gid %d, cuid %d, cgid %d, mode 0%o\n",
+ sp->sem_perm.uid, sp->sem_perm.gid,
+ sp->sem_perm.cuid, sp->sem_perm.cgid,
+ sp->sem_perm.mode & 0777);
+
+ printf("nsems %u\n", sp->sem_nsems);
+
+ printf("otime: %s", ctime(&sp->sem_otime));
+ printf("ctime: %s", ctime(&sp->sem_ctime));
+
+ /*
+ * Sanity check a few things.
+ */
+
+ ATF_REQUIRE_MSG(sp->sem_perm.uid == uid && sp->sem_perm.cuid == uid,
+ "uid mismatch");
+
+ ATF_REQUIRE_MSG(sp->sem_perm.gid == gid && sp->sem_perm.cgid == gid,
+ "gid mismatch");
+
+ ATF_REQUIRE_MSG((sp->sem_perm.mode & 0777) == mode,
+ "mode mismatch %o != %o", (sp->sem_perm.mode & 0777), mode);
+}
+
+void
+waiter(void)
+{
+ struct sembuf s;
+ int semid;
+
+ if ((semid = semget(semkey, 1, 0)) == -1)
+ err(1, "waiter: semget");
+
+ /*
+ * Attempt to acquire the semaphore.
+ */
+ s.sem_num = 0;
+ s.sem_op = -1;
+ s.sem_flg = SEM_UNDO;
+
+ if (semop(semid, &s, 1) == -1)
+ err(1, "waiter: semop -1");
+
+ printf("WOO! GOT THE SEMAPHORE!\n");
+ usleep(10000);
+
+ /*
+ * Release the semaphore and exit.
+ */
+ s.sem_num = 0;
+ s.sem_op = 1;
+ s.sem_flg = SEM_UNDO;
+
+ if (semop(semid, &s, 1) == -1)
+ err(1, "waiter: semop +1");
+
+ exit(0);
+}
+
+/*
+ * Test the SVID-compatible Shared Memory facility.
+ */
+
+ATF_TC_WITH_CLEANUP(shm);
+ATF_TC_HEAD(shm, tc)
+{
+
+ atf_tc_set_md_var(tc, "timeout", "3");
+ atf_tc_set_md_var(tc, "descr", "Checks sysv shared memory");
+}
+
+ATF_TC_BODY(shm, tc)
+{
+ struct sigaction sa;
+ struct shmid_ds s_ds;
+ char *shm_buf;
+ int sender_shmid;
+ int c_status;
+ pid_t wait_result;
+
+ /*
+ * Install a SIGSYS handler so that we can exit gracefully if
+ * System V Shared Memory support isn't in the kernel.
+ */
+ did_sigsys = 0;
+ sa.sa_handler = sigsys_handler;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ ATF_REQUIRE_MSG(sigaction(SIGSYS, &sa, NULL) != -1,
+ "sigaction SIGSYS: %d", errno);
+
+ pgsize = sysconf(_SC_PAGESIZE);
+
+ shmkey = get_ftok(4160);
+ ATF_REQUIRE_MSG(shmkey != (key_t)-1, "get_ftok failed");
+
+ ATF_REQUIRE_MSG((sender_shmid = shmget(shmkey, pgsize,
+ IPC_CREAT | 0640)) != -1,
+ "shmget: %d", errno);
+ write_int("sender_shmid", sender_shmid);
+
+ ATF_REQUIRE_MSG(shmctl(sender_shmid, IPC_STAT, &s_ds) != -1,
+ "shmctl IPC_STAT: %d", errno);
+
+ print_shmid_ds(&s_ds, 0640);
+
+ s_ds.shm_perm.mode = (s_ds.shm_perm.mode & ~0777) | 0600;
+
+ ATF_REQUIRE_MSG(shmctl(sender_shmid, IPC_SET, &s_ds) != -1,
+ "shmctl IPC_SET: %d", errno);
+
+ memset(&s_ds, 0, sizeof(s_ds));
+
+ ATF_REQUIRE_MSG(shmctl(sender_shmid, IPC_STAT, &s_ds) != -1,
+ "shmctl IPC_STAT: %d", errno);
+
+ ATF_REQUIRE_MSG((s_ds.shm_perm.mode & 0777) == 0600,
+ "IPC_SET of mode didn't hold");
+
+ print_shmid_ds(&s_ds, 0600);
+
+ shm_buf = shmat(sender_shmid, NULL, 0);
+ ATF_REQUIRE_MSG(shm_buf != (void *) -1, "sender: shmat: %d", errno);
+
+ /*
+ * Write the test pattern into the shared memory buffer.
+ */
+ strcpy(shm_buf, m2_str);
+
+ fflush(stdout);
+
+ switch ((child_pid = fork())) {
+ case -1:
+ atf_tc_fail("fork: %d", errno);
+ return;
+
+ case 0:
+ sharer();
+ break;
+
+ default:
+ break;
+ }
+
+ /*
+ * Wait for child to finish
+ */
+ wait_result = wait(&c_status);
+ ATF_REQUIRE_EQ_MSG(wait_result, child_pid, "wait returned %d (%s)",
+ wait_result, wait_result == -1 ? strerror(errno) : "");
+ ATF_REQUIRE_MSG(WIFEXITED(c_status), "child abnormal exit: %d (sig %d)",
+ c_status, WTERMSIG(c_status));
+ ATF_REQUIRE_EQ_MSG(WEXITSTATUS(c_status), 0, "child status: %d",
+ WEXITSTATUS(c_status));
+
+ ATF_REQUIRE_MSG(shmctl(sender_shmid, IPC_STAT, &s_ds) != -1,
+ "shmctl IPC_STAT: %d", errno);
+
+ print_shmid_ds(&s_ds, 0600);
+}
+
+static void
+shmid_cleanup(const char *name)
+{
+ int shmid;
+
+ /*
+ * Remove the shared memory area if it exists.
+ */
+ shmid = read_int(name);
+ if (shmid != -1) {
+ if (shmctl(shmid, IPC_RMID, NULL) == -1)
+ err(1, "shmctl IPC_RMID");
+ }
+}
+
+ATF_TC_CLEANUP(shm, tc)
+{
+
+ shmid_cleanup("sender_shmid");
+}
+
+void
+print_shmid_ds(struct shmid_ds *sp, mode_t mode)
+{
+ uid_t uid = geteuid();
+ gid_t gid = getegid();
+
+ printf("PERM: uid %d, gid %d, cuid %d, cgid %d, mode 0%o\n",
+ sp->shm_perm.uid, sp->shm_perm.gid,
+ sp->shm_perm.cuid, sp->shm_perm.cgid,
+ sp->shm_perm.mode & 0777);
+
+ printf("segsz %lu, lpid %d, cpid %d, nattch %u\n",
+ (u_long)sp->shm_segsz, sp->shm_lpid, sp->shm_cpid,
+ sp->shm_nattch);
+
+ printf("atime: %s", ctime(&sp->shm_atime));
+ printf("dtime: %s", ctime(&sp->shm_dtime));
+ printf("ctime: %s", ctime(&sp->shm_ctime));
+
+ /*
+ * Sanity check a few things.
+ */
+
+ ATF_REQUIRE_MSG(sp->shm_perm.uid == uid && sp->shm_perm.cuid == uid,
+ "uid mismatch");
+
+ ATF_REQUIRE_MSG(sp->shm_perm.gid == gid && sp->shm_perm.cgid == gid,
+ "gid mismatch");
+
+ ATF_REQUIRE_MSG((sp->shm_perm.mode & 0777) == mode, "mode mismatch");
+}
+
+void
+sharer(void)
+{
+ int shmid;
+ void *shm_buf;
+
+ shmid = shmget(shmkey, pgsize, 0);
+ if (shmid == -1)
+ err(1, "receiver: shmget");
+
+ shm_buf = shmat(shmid, NULL, 0);
+ if (shm_buf == (void *) -1)
+ err(1, "receiver: shmat");
+
+ printf("%s\n", (const char *)shm_buf);
+
+ if (strcmp((const char *)shm_buf, m2_str) != 0)
+ errx(1, "receiver: data isn't correct");
+
+ exit(0);
+}
+
+#ifdef SHM_REMAP
+ATF_TC_WITH_CLEANUP(shm_remap);
+ATF_TC_HEAD(shm_remap, tc)
+{
+
+ atf_tc_set_md_var(tc, "descr", "Checks SHM_REMAP");
+}
+
+ATF_TC_BODY(shm_remap, tc)
+{
+ char *shm_buf;
+ int shmid_remap;
+
+ pgsize = sysconf(_SC_PAGESIZE);
+
+ shmkey = get_ftok(4160);
+ ATF_REQUIRE_MSG(shmkey != (key_t)-1, "get_ftok failed");
+
+ ATF_REQUIRE_MSG((shmid_remap = shmget(shmkey, pgsize,
+ IPC_CREAT | 0640)) != -1, "shmget: %d", errno);
+ write_int("shmid_remap", shmid_remap);
+
+ ATF_REQUIRE_MSG((shm_buf = mmap(NULL, pgsize, PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_PRIVATE, -1, 0)) != MAP_FAILED, "mmap: %d", errno);
+
+ ATF_REQUIRE_MSG(shmat(shmid_remap, shm_buf, 0) == (void *)-1,
+ "shmat without MAP_REMAP succeeded");
+ ATF_REQUIRE_MSG(shmat(shmid_remap, shm_buf, SHM_REMAP) == shm_buf,
+ "shmat(SHM_REMAP): %d", errno);
+}
+
+ATF_TC_CLEANUP(shm_remap, tc)
+{
+
+ shmid_cleanup("shmid_remap");
+}
+#endif /* SHM_REMAP */
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, msg);
+ ATF_TP_ADD_TC(tp, sem);
+ ATF_TP_ADD_TC(tp, shm);
+#ifdef SHM_REMAP
+ ATF_TP_ADD_TC(tp, shm_remap);
+#endif
+
+ return atf_no_error();
+}
+
diff --git a/contrib/netbsd-tests/kernel/t_umount.sh b/contrib/netbsd-tests/kernel/t_umount.sh
new file mode 100755
index 000000000000..b62ba10245fb
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/t_umount.sh
@@ -0,0 +1,95 @@
+# $NetBSD: t_umount.sh,v 1.5 2010/11/07 17:51:19 jmmv Exp $
+#
+# Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
+# All rights reserved.
+#
+# 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.
+#
+
+TMPMP=umount-f_mount
+TMPIM=umount-f.im
+
+VND=vnd0
+BVND=/dev/${VND}
+CVND=/dev/r${VND}
+MPART=a
+
+atf_test_case umount cleanup
+umount_head()
+{
+ atf_set "descr" "Checks forced unmounting"
+ atf_set "require.user" "root"
+}
+umount_body()
+{
+ cat >disktab <<EOF
+floppy288|2.88MB 3.5in Extra High Density Floppy:\
+ :ty=floppy:se#512:nt#2:rm#300:ns#36:nc#80:\
+ :pa#5760:oa#0:ba#4096:fa#512:ta=4.2BSD:\
+ :pb#5760:ob#0:\
+ :pc#5760:oc#0:
+EOF
+
+ echo "*** Creating a dummy directory tree at" \
+ "${TMPMP} mounted on ${TMPIM}"
+
+ atf_check -o ignore -e ignore mkdir ${TMPMP}
+ atf_check -o ignore -e ignore touch ${TMPMP}/under_the_mount
+ atf_check -o ignore -e ignore dd if=/dev/zero of=${TMPIM} count=5860
+ atf_check -o ignore -e ignore vnconfig -v ${VND} ${TMPIM}
+ atf_check -o ignore -e ignore disklabel -f disktab -rw ${VND} floppy288
+ atf_check -o ignore -e ignore newfs -i 500 -b 8192 -f 1024 ${CVND}${MPART}
+ atf_check -o ignore -e ignore mount -o async ${BVND}${MPART} ${TMPMP}
+ atf_check -o ignore -e ignore touch ${TMPMP}/in_mounted_directory
+
+ echo "*** Testing forced unmount"
+ test -e "${TMPMP}/in_mounted_directory" || \
+ atf_fail "Test file not present in mounted directory!"
+
+ mydir="`pwd`"
+ cd "${TMPMP}"
+ atf_check -o ignore -e ignore umount -f "${BVND}${MPART}"
+
+ atf_check -s ne:0 -e inline:"ls: .: No such file or directory\n" ls .
+ atf_check -s ne:0 -e inline:"ls: ..: No such file or directory\n" ls ..
+
+ atf_check -s ne:0 -e ignore -o inline:"cd: can't cd to .\n" \
+ -x "cd . 2>&1"
+ atf_check -s ne:0 -e ignore -o inline:"cd: can't cd to ..\n" \
+ -x "cd .. 2>&1"
+
+ cd "${mydir}"
+
+ test -e "${TMPMP}/under_the_mount" || \
+ atf_fail "Original mount point dissapeared!"
+}
+umount_cleanup()
+{
+ echo "*** Cleaning up ${TMPMP}, ${TMPIM}."
+ umount -f "${TMPMP}"
+ vnconfig -u "${VND}"
+}
+
+atf_init_test_cases()
+{
+ atf_add_test_case umount
+}
diff --git a/contrib/netbsd-tests/kernel/t_umountstress.sh b/contrib/netbsd-tests/kernel/t_umountstress.sh
new file mode 100755
index 000000000000..fd50a1daca30
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/t_umountstress.sh
@@ -0,0 +1,209 @@
+# $NetBSD: t_umountstress.sh,v 1.5 2013/05/31 14:40:48 gson Exp $
+#
+# Copyright (c) 2013 The NetBSD Foundation, Inc.
+# All rights reserved.
+#
+# 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.
+#
+
+TMPMP=umount-stress_mount
+TMPIM=umount-stress.im
+
+VND=vnd0
+BVND=/dev/${VND}
+CVND=/dev/r${VND}
+MPART=a
+
+atf_test_case fileop cleanup
+fileop_head()
+{
+ atf_set "descr" "Checks unmounting a filesystem doing file operations"
+ atf_set "require.user" "root"
+}
+fileop_body()
+{
+ cat >disktab <<EOF
+floppy288|2.88MB 3.5in Extra High Density Floppy:\
+ :ty=floppy:se#512:nt#2:rm#300:ns#36:nc#80:\
+ :pa#5760:oa#0:ba#4096:fa#512:ta=4.2BSD:\
+ :pb#5760:ob#0:\
+ :pc#5760:oc#0:
+EOF
+
+ echo "*** Creating a dummy directory tree at" \
+ "${TMPMP} mounted on ${TMPIM}"
+
+ atf_check -o ignore -e ignore mkdir ${TMPMP}
+ atf_check -o ignore -e ignore dd if=/dev/zero of=${TMPIM} count=5860
+ atf_check -o ignore -e ignore vnconfig -v ${VND} ${TMPIM}
+ atf_check -o ignore -e ignore disklabel -f disktab -rw ${VND} floppy288
+ atf_check -o ignore -e ignore newfs -i 500 -b 8192 -f 1024 ${CVND}${MPART}
+ atf_check -o ignore -e ignore mount -o async ${BVND}${MPART} ${TMPMP}
+
+ echo "*** Testing fileops"
+
+ touch ${TMPMP}/hold
+ exec 9< ${TMPMP}/hold
+
+ (
+ for j in 0 1 2; do
+ for k in 0 1 2 3 4 5 6 7 8 9; do
+ if ! dd msgfmt=quiet if=/dev/zero \
+ count=1 of=${TMPMP}/test$i$j$k; then
+ echo 1 >result
+ exit
+ fi
+ done
+ done
+ echo 0 >result
+ ) &
+ busypid=$!
+
+ while ! test -f result; do
+ if err=$(umount ${TMPMP} 2>&1); then
+ kill $busypid
+ exec 9<&-
+ wait
+ atf_fail "Unmount succeeded while busy"
+ return
+ fi
+
+ case $err in
+ *:\ Device\ busy)
+ ;;
+ *)
+ kill $busypid
+ exec 9<&-
+ wait
+ atf_fail "Unmount failed: $err"
+ return
+ ;;
+ esac
+ done
+
+ exec 9<&-
+ wait
+
+ rc=`cat result`
+ rm -f result
+
+ case $rc in
+ 0) ;;
+ *) atf_fail "File operation failed"
+ esac
+}
+fileop_cleanup()
+{
+ echo "*** Cleaning up ${TMPMP}, ${TMPIM}."
+ umount -f "${TMPMP}"
+ vnconfig -u "${VND}"
+}
+
+atf_test_case mountlist cleanup
+mountlist_head()
+{
+ atf_set "descr" "Checks unmounting a filesystem using mountlist"
+ atf_set "require.user" "root"
+}
+mountlist_body()
+{
+ cat >disktab <<EOF
+floppy288|2.88MB 3.5in Extra High Density Floppy:\
+ :ty=floppy:se#512:nt#2:rm#300:ns#36:nc#80:\
+ :pa#5760:oa#0:ba#4096:fa#512:ta=4.2BSD:\
+ :pb#5760:ob#0:\
+ :pc#5760:oc#0:
+EOF
+
+ echo "*** Creating a dummy directory tree at" \
+ "${TMPMP} mounted on ${TMPIM}"
+
+ atf_check -o ignore -e ignore mkdir ${TMPMP}
+ atf_check -o ignore -e ignore dd if=/dev/zero of=${TMPIM} count=5860
+ atf_check -o ignore -e ignore vnconfig -v ${VND} ${TMPIM}
+ atf_check -o ignore -e ignore disklabel -f disktab -rw ${VND} floppy288
+ atf_check -o ignore -e ignore newfs -i 500 -b 8192 -f 1024 ${CVND}${MPART}
+ atf_check -o ignore -e ignore mount -o async ${BVND}${MPART} ${TMPMP}
+
+ echo "*** Testing mountlist"
+
+ (
+ for j in 0 1 2 3 4 5 6 7 8 9; do
+ for k in 0 1 2 3 4 5 6 7 8 9; do
+ if ! out=$(mount); then
+ echo 1 >result
+ exit
+ fi
+ done
+ done
+ echo 0 >result
+ ) &
+ busypid=$!
+
+ while ! test -f result; do
+ if err=$(umount ${TMPMP} 2>&1); then
+ if ! mount -o async ${BVND}${MPART} ${TMPMP}; then
+ kill $busypid
+ exec 9<&-
+ wait
+ atf_fail "Remount failed"
+ return
+ fi
+ continue
+ fi
+
+ case $err in
+ *:\ Device\ busy)
+ ;;
+ *)
+ kill $busypid
+ exec 9<&-
+ wait
+ atf_fail "Unmount failed: $err"
+ return
+ ;;
+ esac
+ done
+
+ exec 9<&-
+ wait
+
+ rc=`cat result`
+ rm -f result
+
+ case $rc in
+ 0) ;;
+ *) atf_fail "Mountlist operation failed"
+ esac
+}
+mountlist_cleanup()
+{
+ echo "*** Cleaning up ${TMPMP}, ${TMPIM}."
+ umount -f "${TMPMP}"
+ vnconfig -u "${VND}"
+}
+
+atf_init_test_cases()
+{
+ atf_add_test_case fileop
+ atf_add_test_case mountlist
+}
diff --git a/contrib/netbsd-tests/kernel/tty/t_pr.c b/contrib/netbsd-tests/kernel/tty/t_pr.c
new file mode 100644
index 000000000000..c590ea5ec608
--- /dev/null
+++ b/contrib/netbsd-tests/kernel/tty/t_pr.c
@@ -0,0 +1,186 @@
+/* $NetBSD: t_pr.c,v 1.7 2011/04/26 20:42:01 martin Exp $ */
+
+/*-
+ * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Antti Kantee <pooka@NetBSD.org> and Martin Husemann <martin@NetBSD.org>.
+ *
+ * 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/types.h>
+#include <sys/ioctl.h>
+#include <sys/tty.h>
+
+#include <atf-c.h>
+#include <fcntl.h>
+
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <rump/rump.h>
+#include <rump/rump_syscalls.h>
+
+static int
+sendsome(int from, int to)
+{
+ size_t i;
+ ssize_t cnt;
+ static const char msg[] = "hello world\n";
+ char buf[sizeof(msg)+10];
+
+ memset(buf, 0, sizeof(buf));
+ rump_sys_write(from, msg, strlen(msg));
+ cnt = rump_sys_read(to, buf, sizeof(buf));
+ if (cnt < (ssize_t)strlen(msg)) {
+ printf("short message read: %zd chars: \"%s\"\n", cnt, buf);
+ return 1;
+ }
+ for (i = 0; i < sizeof(buf); i++) {
+ if (buf[i] == '\r' || buf[i] == '\n') {
+ buf[i] = '\n';
+ buf[i+1] = '\0';
+ break;
+ }
+ }
+
+ return strcmp(buf, msg) != 0;
+}
+
+static int
+exercise_ptytty(int master, int slave)
+{
+ int error, flags;
+
+ /*
+ * send a few bytes from master to slave and read them back
+ */
+ error = sendsome(master, slave);
+ if (error)
+ return error;
+
+ flags = FREAD|FWRITE;
+ rump_sys_ioctl(master, TIOCFLUSH, &flags);
+
+ /*
+ * and the same in the other direction
+ */
+ error = sendsome(slave, master);
+ if (error)
+ return error;
+
+ flags = FREAD|FWRITE;
+ rump_sys_ioctl(master, TIOCFLUSH, &flags);
+ return 0;
+}
+
+ATF_TC(client_first);
+ATF_TC_HEAD(client_first, tc)
+{
+
+ atf_tc_set_md_var(tc, "descr",
+ "test basic tty/pty operation when opening client side first");
+}
+
+ATF_TC_BODY(client_first, tc)
+{
+ int master, slave, error, v;
+
+ rump_init();
+ slave = rump_sys_open("/dev/ttyp1", O_RDWR|O_NONBLOCK);
+ ATF_CHECK(slave != -1);
+
+ master = rump_sys_open("/dev/ptyp1", O_RDWR);
+ ATF_CHECK(master != -1);
+
+ v = 0;
+ rump_sys_ioctl(slave, FIOASYNC, &v);
+ error = exercise_ptytty(master, slave);
+ ATF_CHECK(error == 0);
+
+ rump_sys_close(master);
+ rump_sys_close(slave);
+}
+
+ATF_TC(master_first);
+ATF_TC_HEAD(master_first, tc)
+{
+
+ atf_tc_set_md_var(tc, "descr",
+ "test basic tty/pty operation when opening master side first");
+}
+
+ATF_TC_BODY(master_first, tc)
+{
+ int master, slave, error;
+
+ rump_init();
+ master = rump_sys_open("/dev/ptyp1", O_RDWR);
+ ATF_CHECK(master != -1);
+
+ slave = rump_sys_open("/dev/ttyp1", O_RDWR);
+ ATF_CHECK(slave != -1);
+
+ error = exercise_ptytty(master, slave);
+ ATF_CHECK(error == 0);
+
+ rump_sys_close(master);
+ rump_sys_close(slave);
+}
+
+ATF_TC(ptyioctl);
+ATF_TC_HEAD(ptyioctl, tc)
+{
+
+ atf_tc_set_md_var(tc, "descr",
+ "ioctl on pty with client side not open");
+}
+
+ATF_TC_BODY(ptyioctl, tc)
+{
+ struct termios tio;
+ int fd;
+
+ rump_init();
+ fd = rump_sys_open("/dev/ptyp1", O_RDWR);
+ ATF_CHECK(fd != -1);
+
+ /*
+ * This used to die with null deref under ptcwakeup()
+ * atf_tc_expect_signal(-1, "PR kern/40688");
+ */
+ rump_sys_ioctl(fd, TIOCGETA, &tio);
+
+ rump_sys_close(fd);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, ptyioctl);
+ ATF_TP_ADD_TC(tp, client_first);
+ ATF_TP_ADD_TC(tp, master_first);
+
+ return atf_no_error();
+}