1c209e3e2SJohn Baldwin /*- 2c209e3e2SJohn Baldwin * Copyright (c) 2015 John Baldwin <jhb@FreeBSD.org> 3c209e3e2SJohn Baldwin * All rights reserved. 4c209e3e2SJohn Baldwin * 5c209e3e2SJohn Baldwin * Redistribution and use in source and binary forms, with or without 6c209e3e2SJohn Baldwin * modification, are permitted provided that the following conditions 7c209e3e2SJohn Baldwin * are met: 8c209e3e2SJohn Baldwin * 1. Redistributions of source code must retain the above copyright 9c209e3e2SJohn Baldwin * notice, this list of conditions and the following disclaimer. 10c209e3e2SJohn Baldwin * 2. Redistributions in binary form must reproduce the above copyright 11c209e3e2SJohn Baldwin * notice, this list of conditions and the following disclaimer in the 12c209e3e2SJohn Baldwin * documentation and/or other materials provided with the distribution. 13c209e3e2SJohn Baldwin * 14c209e3e2SJohn Baldwin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15c209e3e2SJohn Baldwin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16c209e3e2SJohn Baldwin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17c209e3e2SJohn Baldwin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18c209e3e2SJohn Baldwin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19c209e3e2SJohn Baldwin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20c209e3e2SJohn Baldwin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21c209e3e2SJohn Baldwin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22c209e3e2SJohn Baldwin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23c209e3e2SJohn Baldwin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24c209e3e2SJohn Baldwin * SUCH DAMAGE. 25c209e3e2SJohn Baldwin */ 26c209e3e2SJohn Baldwin 27c209e3e2SJohn Baldwin #include <sys/cdefs.h> 28c209e3e2SJohn Baldwin __FBSDID("$FreeBSD$"); 29c209e3e2SJohn Baldwin 30c209e3e2SJohn Baldwin #include <sys/types.h> 3182a4538fSEric Badger #include <sys/cpuset.h> 3282a4538fSEric Badger #include <sys/event.h> 33*d74da94cSMark Johnston #include <sys/file.h> 3482a4538fSEric Badger #include <sys/time.h> 35b38bd91fSEric Badger #include <sys/procctl.h> 36c209e3e2SJohn Baldwin #include <sys/ptrace.h> 37bc2be1d3SEric Badger #include <sys/queue.h> 38bc2be1d3SEric Badger #include <sys/runq.h> 39189ac973SJohn Baldwin #include <sys/syscall.h> 4057c74f5bSJohn Baldwin #include <sys/sysctl.h> 4157c74f5bSJohn Baldwin #include <sys/user.h> 42c209e3e2SJohn Baldwin #include <sys/wait.h> 43c209e3e2SJohn Baldwin #include <errno.h> 449e0d1159SEric Badger #include <machine/cpufunc.h> 45189ac973SJohn Baldwin #include <pthread.h> 46bc2be1d3SEric Badger #include <sched.h> 4782a4538fSEric Badger #include <semaphore.h> 48c209e3e2SJohn Baldwin #include <signal.h> 49dfa8ba12SJohn Baldwin #include <stdio.h> 50c209e3e2SJohn Baldwin #include <stdlib.h> 51c209e3e2SJohn Baldwin #include <unistd.h> 52c209e3e2SJohn Baldwin #include <atf-c.h> 53c209e3e2SJohn Baldwin 54c209e3e2SJohn Baldwin /* 55dfa8ba12SJohn Baldwin * A variant of ATF_REQUIRE that is suitable for use in child 56dfa8ba12SJohn Baldwin * processes. This only works if the parent process is tripped up by 57dfa8ba12SJohn Baldwin * the early exit and fails some requirement itself. 58dfa8ba12SJohn Baldwin */ 59dfa8ba12SJohn Baldwin #define CHILD_REQUIRE(exp) do { \ 60dfa8ba12SJohn Baldwin if (!(exp)) \ 61dfa8ba12SJohn Baldwin child_fail_require(__FILE__, __LINE__, \ 62dfa8ba12SJohn Baldwin #exp " not met"); \ 63dfa8ba12SJohn Baldwin } while (0) 64dfa8ba12SJohn Baldwin 6598685dc8SJohn Baldwin static __dead2 void 66dfa8ba12SJohn Baldwin child_fail_require(const char *file, int line, const char *str) 67dfa8ba12SJohn Baldwin { 68dfa8ba12SJohn Baldwin char buf[128]; 69dfa8ba12SJohn Baldwin 70dfa8ba12SJohn Baldwin snprintf(buf, sizeof(buf), "%s:%d: %s\n", file, line, str); 71dfa8ba12SJohn Baldwin write(2, buf, strlen(buf)); 72dfa8ba12SJohn Baldwin _exit(32); 73dfa8ba12SJohn Baldwin } 74dfa8ba12SJohn Baldwin 7598685dc8SJohn Baldwin static void 7698685dc8SJohn Baldwin trace_me(void) 7798685dc8SJohn Baldwin { 7898685dc8SJohn Baldwin 7998685dc8SJohn Baldwin /* Attach the parent process as a tracer of this process. */ 8098685dc8SJohn Baldwin CHILD_REQUIRE(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1); 8198685dc8SJohn Baldwin 8298685dc8SJohn Baldwin /* Trigger a stop. */ 8398685dc8SJohn Baldwin raise(SIGSTOP); 8498685dc8SJohn Baldwin } 8598685dc8SJohn Baldwin 8698685dc8SJohn Baldwin static void 8798685dc8SJohn Baldwin attach_child(pid_t pid) 8898685dc8SJohn Baldwin { 8998685dc8SJohn Baldwin pid_t wpid; 9098685dc8SJohn Baldwin int status; 9198685dc8SJohn Baldwin 9298685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_ATTACH, pid, NULL, 0) == 0); 9398685dc8SJohn Baldwin 9498685dc8SJohn Baldwin wpid = waitpid(pid, &status, 0); 9598685dc8SJohn Baldwin ATF_REQUIRE(wpid == pid); 9698685dc8SJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 9798685dc8SJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 9898685dc8SJohn Baldwin } 9998685dc8SJohn Baldwin 10098685dc8SJohn Baldwin static void 10198685dc8SJohn Baldwin wait_for_zombie(pid_t pid) 10298685dc8SJohn Baldwin { 10398685dc8SJohn Baldwin 10498685dc8SJohn Baldwin /* 10598685dc8SJohn Baldwin * Wait for a process to exit. This is kind of gross, but 10698685dc8SJohn Baldwin * there is not a better way. 10798685dc8SJohn Baldwin */ 10898685dc8SJohn Baldwin for (;;) { 10998685dc8SJohn Baldwin struct kinfo_proc kp; 11098685dc8SJohn Baldwin size_t len; 11198685dc8SJohn Baldwin int mib[4]; 11298685dc8SJohn Baldwin 11398685dc8SJohn Baldwin mib[0] = CTL_KERN; 11498685dc8SJohn Baldwin mib[1] = KERN_PROC; 11598685dc8SJohn Baldwin mib[2] = KERN_PROC_PID; 11698685dc8SJohn Baldwin mib[3] = pid; 11798685dc8SJohn Baldwin len = sizeof(kp); 11898685dc8SJohn Baldwin if (sysctl(mib, nitems(mib), &kp, &len, NULL, 0) == -1) { 11998685dc8SJohn Baldwin /* The KERN_PROC_PID sysctl fails for zombies. */ 12098685dc8SJohn Baldwin ATF_REQUIRE(errno == ESRCH); 12198685dc8SJohn Baldwin break; 12298685dc8SJohn Baldwin } 12398685dc8SJohn Baldwin usleep(5000); 12498685dc8SJohn Baldwin } 12598685dc8SJohn Baldwin } 12698685dc8SJohn Baldwin 127dfa8ba12SJohn Baldwin /* 128c209e3e2SJohn Baldwin * Verify that a parent debugger process "sees" the exit of a debugged 129c209e3e2SJohn Baldwin * process exactly once when attached via PT_TRACE_ME. 130c209e3e2SJohn Baldwin */ 131c209e3e2SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_trace_me); 132c209e3e2SJohn Baldwin ATF_TC_BODY(ptrace__parent_wait_after_trace_me, tc) 133c209e3e2SJohn Baldwin { 134c209e3e2SJohn Baldwin pid_t child, wpid; 135c209e3e2SJohn Baldwin int status; 136c209e3e2SJohn Baldwin 137c209e3e2SJohn Baldwin ATF_REQUIRE((child = fork()) != -1); 138c209e3e2SJohn Baldwin if (child == 0) { 139c209e3e2SJohn Baldwin /* Child process. */ 14098685dc8SJohn Baldwin trace_me(); 141c209e3e2SJohn Baldwin 142b98cb919SJohn Baldwin _exit(1); 143c209e3e2SJohn Baldwin } 144c209e3e2SJohn Baldwin 145c209e3e2SJohn Baldwin /* Parent process. */ 146c209e3e2SJohn Baldwin 147c209e3e2SJohn Baldwin /* The first wait() should report the stop from SIGSTOP. */ 148c209e3e2SJohn Baldwin wpid = waitpid(child, &status, 0); 149c209e3e2SJohn Baldwin ATF_REQUIRE(wpid == child); 150c209e3e2SJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 151c209e3e2SJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 152c209e3e2SJohn Baldwin 153c209e3e2SJohn Baldwin /* Continue the child ignoring the SIGSTOP. */ 154c209e3e2SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1); 155c209e3e2SJohn Baldwin 156c209e3e2SJohn Baldwin /* The second wait() should report the exit status. */ 157c209e3e2SJohn Baldwin wpid = waitpid(child, &status, 0); 158c209e3e2SJohn Baldwin ATF_REQUIRE(wpid == child); 159c209e3e2SJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 160c209e3e2SJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 1); 161c209e3e2SJohn Baldwin 162c209e3e2SJohn Baldwin /* The child should no longer exist. */ 163c209e3e2SJohn Baldwin wpid = waitpid(child, &status, 0); 164c209e3e2SJohn Baldwin ATF_REQUIRE(wpid == -1); 165c209e3e2SJohn Baldwin ATF_REQUIRE(errno == ECHILD); 166c209e3e2SJohn Baldwin } 167c209e3e2SJohn Baldwin 168c209e3e2SJohn Baldwin /* 169c209e3e2SJohn Baldwin * Verify that a parent debugger process "sees" the exit of a debugged 170c209e3e2SJohn Baldwin * process exactly once when attached via PT_ATTACH. 171c209e3e2SJohn Baldwin */ 172c209e3e2SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_attach); 173c209e3e2SJohn Baldwin ATF_TC_BODY(ptrace__parent_wait_after_attach, tc) 174c209e3e2SJohn Baldwin { 175c209e3e2SJohn Baldwin pid_t child, wpid; 176c209e3e2SJohn Baldwin int cpipe[2], status; 177c209e3e2SJohn Baldwin char c; 178c209e3e2SJohn Baldwin 179c209e3e2SJohn Baldwin ATF_REQUIRE(pipe(cpipe) == 0); 180c209e3e2SJohn Baldwin ATF_REQUIRE((child = fork()) != -1); 181c209e3e2SJohn Baldwin if (child == 0) { 182c209e3e2SJohn Baldwin /* Child process. */ 183c209e3e2SJohn Baldwin close(cpipe[0]); 184c209e3e2SJohn Baldwin 185c209e3e2SJohn Baldwin /* Wait for the parent to attach. */ 186dfa8ba12SJohn Baldwin CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == 0); 187c209e3e2SJohn Baldwin 188b98cb919SJohn Baldwin _exit(1); 189c209e3e2SJohn Baldwin } 190c209e3e2SJohn Baldwin close(cpipe[1]); 191c209e3e2SJohn Baldwin 192c209e3e2SJohn Baldwin /* Parent process. */ 193c209e3e2SJohn Baldwin 194c209e3e2SJohn Baldwin /* Attach to the child process. */ 19598685dc8SJohn Baldwin attach_child(child); 196c209e3e2SJohn Baldwin 197c209e3e2SJohn Baldwin /* Continue the child ignoring the SIGSTOP. */ 198c209e3e2SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1); 199c209e3e2SJohn Baldwin 200c209e3e2SJohn Baldwin /* Signal the child to exit. */ 201c209e3e2SJohn Baldwin close(cpipe[0]); 202c209e3e2SJohn Baldwin 203c209e3e2SJohn Baldwin /* The second wait() should report the exit status. */ 204c209e3e2SJohn Baldwin wpid = waitpid(child, &status, 0); 205c209e3e2SJohn Baldwin ATF_REQUIRE(wpid == child); 206c209e3e2SJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 207c209e3e2SJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 1); 208c209e3e2SJohn Baldwin 209c209e3e2SJohn Baldwin /* The child should no longer exist. */ 210c209e3e2SJohn Baldwin wpid = waitpid(child, &status, 0); 211c209e3e2SJohn Baldwin ATF_REQUIRE(wpid == -1); 212c209e3e2SJohn Baldwin ATF_REQUIRE(errno == ECHILD); 213c209e3e2SJohn Baldwin } 214c209e3e2SJohn Baldwin 21557c74f5bSJohn Baldwin /* 21657c74f5bSJohn Baldwin * Verify that a parent process "sees" the exit of a debugged process only 21757c74f5bSJohn Baldwin * after the debugger has seen it. 21857c74f5bSJohn Baldwin */ 21957c74f5bSJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_child_debugger); 22057c74f5bSJohn Baldwin ATF_TC_BODY(ptrace__parent_sees_exit_after_child_debugger, tc) 22157c74f5bSJohn Baldwin { 22257c74f5bSJohn Baldwin pid_t child, debugger, wpid; 22357c74f5bSJohn Baldwin int cpipe[2], dpipe[2], status; 22457c74f5bSJohn Baldwin char c; 22557c74f5bSJohn Baldwin 22657c74f5bSJohn Baldwin ATF_REQUIRE(pipe(cpipe) == 0); 22757c74f5bSJohn Baldwin ATF_REQUIRE((child = fork()) != -1); 22857c74f5bSJohn Baldwin 22957c74f5bSJohn Baldwin if (child == 0) { 23057c74f5bSJohn Baldwin /* Child process. */ 23157c74f5bSJohn Baldwin close(cpipe[0]); 23257c74f5bSJohn Baldwin 23357c74f5bSJohn Baldwin /* Wait for parent to be ready. */ 234dfa8ba12SJohn Baldwin CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c)); 23557c74f5bSJohn Baldwin 236b98cb919SJohn Baldwin _exit(1); 23757c74f5bSJohn Baldwin } 23857c74f5bSJohn Baldwin close(cpipe[1]); 23957c74f5bSJohn Baldwin 24057c74f5bSJohn Baldwin ATF_REQUIRE(pipe(dpipe) == 0); 24157c74f5bSJohn Baldwin ATF_REQUIRE((debugger = fork()) != -1); 24257c74f5bSJohn Baldwin 24357c74f5bSJohn Baldwin if (debugger == 0) { 24457c74f5bSJohn Baldwin /* Debugger process. */ 24557c74f5bSJohn Baldwin close(dpipe[0]); 24657c74f5bSJohn Baldwin 247dfa8ba12SJohn Baldwin CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1); 24857c74f5bSJohn Baldwin 24957c74f5bSJohn Baldwin wpid = waitpid(child, &status, 0); 250dfa8ba12SJohn Baldwin CHILD_REQUIRE(wpid == child); 251dfa8ba12SJohn Baldwin CHILD_REQUIRE(WIFSTOPPED(status)); 252dfa8ba12SJohn Baldwin CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP); 25357c74f5bSJohn Baldwin 254dfa8ba12SJohn Baldwin CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1); 25557c74f5bSJohn Baldwin 25657c74f5bSJohn Baldwin /* Signal parent that debugger is attached. */ 257dfa8ba12SJohn Baldwin CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c)); 25857c74f5bSJohn Baldwin 25957c74f5bSJohn Baldwin /* Wait for parent's failed wait. */ 260dfa8ba12SJohn Baldwin CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == 0); 26157c74f5bSJohn Baldwin 26257c74f5bSJohn Baldwin wpid = waitpid(child, &status, 0); 263dfa8ba12SJohn Baldwin CHILD_REQUIRE(wpid == child); 264dfa8ba12SJohn Baldwin CHILD_REQUIRE(WIFEXITED(status)); 265dfa8ba12SJohn Baldwin CHILD_REQUIRE(WEXITSTATUS(status) == 1); 26657c74f5bSJohn Baldwin 267b98cb919SJohn Baldwin _exit(0); 26857c74f5bSJohn Baldwin } 26957c74f5bSJohn Baldwin close(dpipe[1]); 27057c74f5bSJohn Baldwin 27157c74f5bSJohn Baldwin /* Parent process. */ 27257c74f5bSJohn Baldwin 27357c74f5bSJohn Baldwin /* Wait for the debugger to attach to the child. */ 27457c74f5bSJohn Baldwin ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c)); 27557c74f5bSJohn Baldwin 27657c74f5bSJohn Baldwin /* Release the child. */ 27757c74f5bSJohn Baldwin ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c)); 27857c74f5bSJohn Baldwin ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0); 27957c74f5bSJohn Baldwin close(cpipe[0]); 28057c74f5bSJohn Baldwin 28198685dc8SJohn Baldwin wait_for_zombie(child); 28257c74f5bSJohn Baldwin 28357c74f5bSJohn Baldwin /* 2842f021998SJohn Baldwin * This wait should return a pid of 0 to indicate no status to 2852f021998SJohn Baldwin * report. The parent should see the child as non-exited 2862f021998SJohn Baldwin * until the debugger sees the exit. 28757c74f5bSJohn Baldwin */ 28857c74f5bSJohn Baldwin wpid = waitpid(child, &status, WNOHANG); 28957c74f5bSJohn Baldwin ATF_REQUIRE(wpid == 0); 29057c74f5bSJohn Baldwin 29157c74f5bSJohn Baldwin /* Signal the debugger to wait for the child. */ 29257c74f5bSJohn Baldwin close(dpipe[0]); 29357c74f5bSJohn Baldwin 29457c74f5bSJohn Baldwin /* Wait for the debugger. */ 29557c74f5bSJohn Baldwin wpid = waitpid(debugger, &status, 0); 29657c74f5bSJohn Baldwin ATF_REQUIRE(wpid == debugger); 29757c74f5bSJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 29857c74f5bSJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 0); 29957c74f5bSJohn Baldwin 30057c74f5bSJohn Baldwin /* The child process should now be ready. */ 30157c74f5bSJohn Baldwin wpid = waitpid(child, &status, WNOHANG); 30257c74f5bSJohn Baldwin ATF_REQUIRE(wpid == child); 30357c74f5bSJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 30457c74f5bSJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 1); 30557c74f5bSJohn Baldwin } 30657c74f5bSJohn Baldwin 30757c74f5bSJohn Baldwin /* 30857c74f5bSJohn Baldwin * Verify that a parent process "sees" the exit of a debugged process 30957c74f5bSJohn Baldwin * only after a non-direct-child debugger has seen it. In particular, 31057c74f5bSJohn Baldwin * various wait() calls in the parent must avoid failing with ESRCH by 31157c74f5bSJohn Baldwin * checking the parent's orphan list for the debugee. 31257c74f5bSJohn Baldwin */ 31357c74f5bSJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_unrelated_debugger); 31457c74f5bSJohn Baldwin ATF_TC_BODY(ptrace__parent_sees_exit_after_unrelated_debugger, tc) 31557c74f5bSJohn Baldwin { 31657c74f5bSJohn Baldwin pid_t child, debugger, fpid, wpid; 31757c74f5bSJohn Baldwin int cpipe[2], dpipe[2], status; 31857c74f5bSJohn Baldwin char c; 31957c74f5bSJohn Baldwin 32057c74f5bSJohn Baldwin ATF_REQUIRE(pipe(cpipe) == 0); 32157c74f5bSJohn Baldwin ATF_REQUIRE((child = fork()) != -1); 32257c74f5bSJohn Baldwin 32357c74f5bSJohn Baldwin if (child == 0) { 32457c74f5bSJohn Baldwin /* Child process. */ 32557c74f5bSJohn Baldwin close(cpipe[0]); 32657c74f5bSJohn Baldwin 32757c74f5bSJohn Baldwin /* Wait for parent to be ready. */ 328dfa8ba12SJohn Baldwin CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c)); 32957c74f5bSJohn Baldwin 330b98cb919SJohn Baldwin _exit(1); 33157c74f5bSJohn Baldwin } 33257c74f5bSJohn Baldwin close(cpipe[1]); 33357c74f5bSJohn Baldwin 33457c74f5bSJohn Baldwin ATF_REQUIRE(pipe(dpipe) == 0); 33557c74f5bSJohn Baldwin ATF_REQUIRE((debugger = fork()) != -1); 33657c74f5bSJohn Baldwin 33757c74f5bSJohn Baldwin if (debugger == 0) { 33857c74f5bSJohn Baldwin /* Debugger parent. */ 33957c74f5bSJohn Baldwin 34057c74f5bSJohn Baldwin /* 34157c74f5bSJohn Baldwin * Fork again and drop the debugger parent so that the 34257c74f5bSJohn Baldwin * debugger is not a child of the main parent. 34357c74f5bSJohn Baldwin */ 344dfa8ba12SJohn Baldwin CHILD_REQUIRE((fpid = fork()) != -1); 34557c74f5bSJohn Baldwin if (fpid != 0) 346b98cb919SJohn Baldwin _exit(2); 34757c74f5bSJohn Baldwin 34857c74f5bSJohn Baldwin /* Debugger process. */ 34957c74f5bSJohn Baldwin close(dpipe[0]); 35057c74f5bSJohn Baldwin 351dfa8ba12SJohn Baldwin CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1); 35257c74f5bSJohn Baldwin 35357c74f5bSJohn Baldwin wpid = waitpid(child, &status, 0); 354dfa8ba12SJohn Baldwin CHILD_REQUIRE(wpid == child); 355dfa8ba12SJohn Baldwin CHILD_REQUIRE(WIFSTOPPED(status)); 356dfa8ba12SJohn Baldwin CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP); 35757c74f5bSJohn Baldwin 358dfa8ba12SJohn Baldwin CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1); 35957c74f5bSJohn Baldwin 36057c74f5bSJohn Baldwin /* Signal parent that debugger is attached. */ 361dfa8ba12SJohn Baldwin CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c)); 36257c74f5bSJohn Baldwin 36357c74f5bSJohn Baldwin /* Wait for parent's failed wait. */ 364dfa8ba12SJohn Baldwin CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == sizeof(c)); 36557c74f5bSJohn Baldwin 36657c74f5bSJohn Baldwin wpid = waitpid(child, &status, 0); 367dfa8ba12SJohn Baldwin CHILD_REQUIRE(wpid == child); 368dfa8ba12SJohn Baldwin CHILD_REQUIRE(WIFEXITED(status)); 369dfa8ba12SJohn Baldwin CHILD_REQUIRE(WEXITSTATUS(status) == 1); 37057c74f5bSJohn Baldwin 371b98cb919SJohn Baldwin _exit(0); 37257c74f5bSJohn Baldwin } 373eddb85c6SJohn Baldwin close(dpipe[1]); 37457c74f5bSJohn Baldwin 37557c74f5bSJohn Baldwin /* Parent process. */ 37657c74f5bSJohn Baldwin 37757c74f5bSJohn Baldwin /* Wait for the debugger parent process to exit. */ 37857c74f5bSJohn Baldwin wpid = waitpid(debugger, &status, 0); 37957c74f5bSJohn Baldwin ATF_REQUIRE(wpid == debugger); 38057c74f5bSJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 38157c74f5bSJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 2); 38257c74f5bSJohn Baldwin 38357c74f5bSJohn Baldwin /* A WNOHANG wait here should see the non-exited child. */ 38457c74f5bSJohn Baldwin wpid = waitpid(child, &status, WNOHANG); 38557c74f5bSJohn Baldwin ATF_REQUIRE(wpid == 0); 38657c74f5bSJohn Baldwin 38757c74f5bSJohn Baldwin /* Wait for the debugger to attach to the child. */ 38857c74f5bSJohn Baldwin ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c)); 38957c74f5bSJohn Baldwin 39057c74f5bSJohn Baldwin /* Release the child. */ 39157c74f5bSJohn Baldwin ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c)); 39257c74f5bSJohn Baldwin ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0); 39357c74f5bSJohn Baldwin close(cpipe[0]); 39457c74f5bSJohn Baldwin 39598685dc8SJohn Baldwin wait_for_zombie(child); 39657c74f5bSJohn Baldwin 39757c74f5bSJohn Baldwin /* 3982f021998SJohn Baldwin * This wait should return a pid of 0 to indicate no status to 3992f021998SJohn Baldwin * report. The parent should see the child as non-exited 4002f021998SJohn Baldwin * until the debugger sees the exit. 40157c74f5bSJohn Baldwin */ 40257c74f5bSJohn Baldwin wpid = waitpid(child, &status, WNOHANG); 40357c74f5bSJohn Baldwin ATF_REQUIRE(wpid == 0); 40457c74f5bSJohn Baldwin 40557c74f5bSJohn Baldwin /* Signal the debugger to wait for the child. */ 406eddb85c6SJohn Baldwin ATF_REQUIRE(write(dpipe[0], &c, sizeof(c)) == sizeof(c)); 40757c74f5bSJohn Baldwin 40857c74f5bSJohn Baldwin /* Wait for the debugger. */ 409eddb85c6SJohn Baldwin ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == 0); 410eddb85c6SJohn Baldwin close(dpipe[0]); 41157c74f5bSJohn Baldwin 41257c74f5bSJohn Baldwin /* The child process should now be ready. */ 41357c74f5bSJohn Baldwin wpid = waitpid(child, &status, WNOHANG); 41457c74f5bSJohn Baldwin ATF_REQUIRE(wpid == child); 41557c74f5bSJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 41657c74f5bSJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 1); 41757c74f5bSJohn Baldwin } 41857c74f5bSJohn Baldwin 41998685dc8SJohn Baldwin /* 42098685dc8SJohn Baldwin * The parent process should always act the same regardless of how the 42198685dc8SJohn Baldwin * debugger is attached to it. 42298685dc8SJohn Baldwin */ 42398685dc8SJohn Baldwin static __dead2 void 424189ac973SJohn Baldwin follow_fork_parent(bool use_vfork) 42598685dc8SJohn Baldwin { 42698685dc8SJohn Baldwin pid_t fpid, wpid; 42798685dc8SJohn Baldwin int status; 42898685dc8SJohn Baldwin 429189ac973SJohn Baldwin if (use_vfork) 430189ac973SJohn Baldwin CHILD_REQUIRE((fpid = vfork()) != -1); 431189ac973SJohn Baldwin else 43298685dc8SJohn Baldwin CHILD_REQUIRE((fpid = fork()) != -1); 43398685dc8SJohn Baldwin 43498685dc8SJohn Baldwin if (fpid == 0) 43598685dc8SJohn Baldwin /* Child */ 436b98cb919SJohn Baldwin _exit(2); 43798685dc8SJohn Baldwin 43898685dc8SJohn Baldwin wpid = waitpid(fpid, &status, 0); 43998685dc8SJohn Baldwin CHILD_REQUIRE(wpid == fpid); 44098685dc8SJohn Baldwin CHILD_REQUIRE(WIFEXITED(status)); 44198685dc8SJohn Baldwin CHILD_REQUIRE(WEXITSTATUS(status) == 2); 44298685dc8SJohn Baldwin 443b98cb919SJohn Baldwin _exit(1); 44498685dc8SJohn Baldwin } 44598685dc8SJohn Baldwin 44698685dc8SJohn Baldwin /* 44798685dc8SJohn Baldwin * Helper routine for follow fork tests. This waits for two stops 44898685dc8SJohn Baldwin * that report both "sides" of a fork. It returns the pid of the new 44998685dc8SJohn Baldwin * child process. 45098685dc8SJohn Baldwin */ 45198685dc8SJohn Baldwin static pid_t 452189ac973SJohn Baldwin handle_fork_events(pid_t parent, struct ptrace_lwpinfo *ppl) 45398685dc8SJohn Baldwin { 45498685dc8SJohn Baldwin struct ptrace_lwpinfo pl; 45598685dc8SJohn Baldwin bool fork_reported[2]; 45698685dc8SJohn Baldwin pid_t child, wpid; 45798685dc8SJohn Baldwin int i, status; 45898685dc8SJohn Baldwin 45998685dc8SJohn Baldwin fork_reported[0] = false; 46098685dc8SJohn Baldwin fork_reported[1] = false; 46198685dc8SJohn Baldwin child = -1; 46298685dc8SJohn Baldwin 46398685dc8SJohn Baldwin /* 46498685dc8SJohn Baldwin * Each process should report a fork event. The parent should 46598685dc8SJohn Baldwin * report a PL_FLAG_FORKED event, and the child should report 46698685dc8SJohn Baldwin * a PL_FLAG_CHILD event. 46798685dc8SJohn Baldwin */ 46898685dc8SJohn Baldwin for (i = 0; i < 2; i++) { 46998685dc8SJohn Baldwin wpid = wait(&status); 47098685dc8SJohn Baldwin ATF_REQUIRE(wpid > 0); 47198685dc8SJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 47298685dc8SJohn Baldwin 47398685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, 47498685dc8SJohn Baldwin sizeof(pl)) != -1); 47598685dc8SJohn Baldwin ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) != 47698685dc8SJohn Baldwin 0); 47798685dc8SJohn Baldwin ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) != 47898685dc8SJohn Baldwin (PL_FLAG_FORKED | PL_FLAG_CHILD)); 47998685dc8SJohn Baldwin if (pl.pl_flags & PL_FLAG_CHILD) { 48098685dc8SJohn Baldwin ATF_REQUIRE(wpid != parent); 48198685dc8SJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 48298685dc8SJohn Baldwin ATF_REQUIRE(!fork_reported[1]); 48398685dc8SJohn Baldwin if (child == -1) 48498685dc8SJohn Baldwin child = wpid; 48598685dc8SJohn Baldwin else 48698685dc8SJohn Baldwin ATF_REQUIRE(child == wpid); 487189ac973SJohn Baldwin if (ppl != NULL) 488189ac973SJohn Baldwin ppl[1] = pl; 48998685dc8SJohn Baldwin fork_reported[1] = true; 49098685dc8SJohn Baldwin } else { 49198685dc8SJohn Baldwin ATF_REQUIRE(wpid == parent); 49298685dc8SJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP); 49398685dc8SJohn Baldwin ATF_REQUIRE(!fork_reported[0]); 49498685dc8SJohn Baldwin if (child == -1) 49598685dc8SJohn Baldwin child = pl.pl_child_pid; 49698685dc8SJohn Baldwin else 49798685dc8SJohn Baldwin ATF_REQUIRE(child == pl.pl_child_pid); 498189ac973SJohn Baldwin if (ppl != NULL) 499189ac973SJohn Baldwin ppl[0] = pl; 50098685dc8SJohn Baldwin fork_reported[0] = true; 50198685dc8SJohn Baldwin } 50298685dc8SJohn Baldwin } 50398685dc8SJohn Baldwin 50498685dc8SJohn Baldwin return (child); 50598685dc8SJohn Baldwin } 50698685dc8SJohn Baldwin 50798685dc8SJohn Baldwin /* 50898685dc8SJohn Baldwin * Verify that a new child process is stopped after a followed fork and 50998685dc8SJohn Baldwin * that the traced parent sees the exit of the child after the debugger 51098685dc8SJohn Baldwin * when both processes remain attached to the debugger. 51198685dc8SJohn Baldwin */ 51298685dc8SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached); 51398685dc8SJohn Baldwin ATF_TC_BODY(ptrace__follow_fork_both_attached, tc) 51498685dc8SJohn Baldwin { 515479b610dSJohn Baldwin pid_t children[2], fpid, wpid; 51698685dc8SJohn Baldwin int status; 51798685dc8SJohn Baldwin 51898685dc8SJohn Baldwin ATF_REQUIRE((fpid = fork()) != -1); 51998685dc8SJohn Baldwin if (fpid == 0) { 52098685dc8SJohn Baldwin trace_me(); 521189ac973SJohn Baldwin follow_fork_parent(false); 52298685dc8SJohn Baldwin } 52398685dc8SJohn Baldwin 52498685dc8SJohn Baldwin /* Parent process. */ 52598685dc8SJohn Baldwin children[0] = fpid; 52698685dc8SJohn Baldwin 52798685dc8SJohn Baldwin /* The first wait() should report the stop from SIGSTOP. */ 52898685dc8SJohn Baldwin wpid = waitpid(children[0], &status, 0); 52998685dc8SJohn Baldwin ATF_REQUIRE(wpid == children[0]); 53098685dc8SJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 53198685dc8SJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 53298685dc8SJohn Baldwin 53398685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1); 53498685dc8SJohn Baldwin 53598685dc8SJohn Baldwin /* Continue the child ignoring the SIGSTOP. */ 53698685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1); 53798685dc8SJohn Baldwin 538189ac973SJohn Baldwin children[1] = handle_fork_events(children[0], NULL); 53998685dc8SJohn Baldwin ATF_REQUIRE(children[1] > 0); 54098685dc8SJohn Baldwin 54198685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1); 54298685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1); 54398685dc8SJohn Baldwin 54498685dc8SJohn Baldwin /* 54598685dc8SJohn Baldwin * The child can't exit until the grandchild reports status, so the 54698685dc8SJohn Baldwin * grandchild should report its exit first to the debugger. 54798685dc8SJohn Baldwin */ 54898685dc8SJohn Baldwin wpid = wait(&status); 54998685dc8SJohn Baldwin ATF_REQUIRE(wpid == children[1]); 55098685dc8SJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 55198685dc8SJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 2); 55298685dc8SJohn Baldwin 55398685dc8SJohn Baldwin wpid = wait(&status); 55498685dc8SJohn Baldwin ATF_REQUIRE(wpid == children[0]); 55598685dc8SJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 55698685dc8SJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 1); 55798685dc8SJohn Baldwin 55898685dc8SJohn Baldwin wpid = wait(&status); 55998685dc8SJohn Baldwin ATF_REQUIRE(wpid == -1); 56098685dc8SJohn Baldwin ATF_REQUIRE(errno == ECHILD); 56198685dc8SJohn Baldwin } 56298685dc8SJohn Baldwin 56398685dc8SJohn Baldwin /* 56498685dc8SJohn Baldwin * Verify that a new child process is stopped after a followed fork 56598685dc8SJohn Baldwin * and that the traced parent sees the exit of the child when the new 56698685dc8SJohn Baldwin * child process is detached after it reports its fork. 56798685dc8SJohn Baldwin */ 56898685dc8SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached); 56998685dc8SJohn Baldwin ATF_TC_BODY(ptrace__follow_fork_child_detached, tc) 57098685dc8SJohn Baldwin { 571479b610dSJohn Baldwin pid_t children[2], fpid, wpid; 57298685dc8SJohn Baldwin int status; 57398685dc8SJohn Baldwin 57498685dc8SJohn Baldwin ATF_REQUIRE((fpid = fork()) != -1); 57598685dc8SJohn Baldwin if (fpid == 0) { 57698685dc8SJohn Baldwin trace_me(); 577189ac973SJohn Baldwin follow_fork_parent(false); 57898685dc8SJohn Baldwin } 57998685dc8SJohn Baldwin 58098685dc8SJohn Baldwin /* Parent process. */ 58198685dc8SJohn Baldwin children[0] = fpid; 58298685dc8SJohn Baldwin 58398685dc8SJohn Baldwin /* The first wait() should report the stop from SIGSTOP. */ 58498685dc8SJohn Baldwin wpid = waitpid(children[0], &status, 0); 58598685dc8SJohn Baldwin ATF_REQUIRE(wpid == children[0]); 58698685dc8SJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 58798685dc8SJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 58898685dc8SJohn Baldwin 58998685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1); 59098685dc8SJohn Baldwin 59198685dc8SJohn Baldwin /* Continue the child ignoring the SIGSTOP. */ 59298685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1); 59398685dc8SJohn Baldwin 594189ac973SJohn Baldwin children[1] = handle_fork_events(children[0], NULL); 59598685dc8SJohn Baldwin ATF_REQUIRE(children[1] > 0); 59698685dc8SJohn Baldwin 59798685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1); 59898685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1); 59998685dc8SJohn Baldwin 60098685dc8SJohn Baldwin /* 60198685dc8SJohn Baldwin * Should not see any status from the grandchild now, only the 60298685dc8SJohn Baldwin * child. 60398685dc8SJohn Baldwin */ 60498685dc8SJohn Baldwin wpid = wait(&status); 60598685dc8SJohn Baldwin ATF_REQUIRE(wpid == children[0]); 60698685dc8SJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 60798685dc8SJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 1); 60898685dc8SJohn Baldwin 60998685dc8SJohn Baldwin wpid = wait(&status); 61098685dc8SJohn Baldwin ATF_REQUIRE(wpid == -1); 61198685dc8SJohn Baldwin ATF_REQUIRE(errno == ECHILD); 61298685dc8SJohn Baldwin } 61398685dc8SJohn Baldwin 61498685dc8SJohn Baldwin /* 61598685dc8SJohn Baldwin * Verify that a new child process is stopped after a followed fork 61698685dc8SJohn Baldwin * and that the traced parent sees the exit of the child when the 61798685dc8SJohn Baldwin * traced parent is detached after the fork. 61898685dc8SJohn Baldwin */ 61998685dc8SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached); 62098685dc8SJohn Baldwin ATF_TC_BODY(ptrace__follow_fork_parent_detached, tc) 62198685dc8SJohn Baldwin { 622479b610dSJohn Baldwin pid_t children[2], fpid, wpid; 62398685dc8SJohn Baldwin int status; 62498685dc8SJohn Baldwin 62598685dc8SJohn Baldwin ATF_REQUIRE((fpid = fork()) != -1); 62698685dc8SJohn Baldwin if (fpid == 0) { 62798685dc8SJohn Baldwin trace_me(); 628189ac973SJohn Baldwin follow_fork_parent(false); 62998685dc8SJohn Baldwin } 63098685dc8SJohn Baldwin 63198685dc8SJohn Baldwin /* Parent process. */ 63298685dc8SJohn Baldwin children[0] = fpid; 63398685dc8SJohn Baldwin 63498685dc8SJohn Baldwin /* The first wait() should report the stop from SIGSTOP. */ 63598685dc8SJohn Baldwin wpid = waitpid(children[0], &status, 0); 63698685dc8SJohn Baldwin ATF_REQUIRE(wpid == children[0]); 63798685dc8SJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 63898685dc8SJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 63998685dc8SJohn Baldwin 64098685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1); 64198685dc8SJohn Baldwin 64298685dc8SJohn Baldwin /* Continue the child ignoring the SIGSTOP. */ 64398685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1); 64498685dc8SJohn Baldwin 645189ac973SJohn Baldwin children[1] = handle_fork_events(children[0], NULL); 64698685dc8SJohn Baldwin ATF_REQUIRE(children[1] > 0); 64798685dc8SJohn Baldwin 64898685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1); 64998685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1); 65098685dc8SJohn Baldwin 65198685dc8SJohn Baldwin /* 65298685dc8SJohn Baldwin * The child can't exit until the grandchild reports status, so the 65398685dc8SJohn Baldwin * grandchild should report its exit first to the debugger. 65498685dc8SJohn Baldwin * 65598685dc8SJohn Baldwin * Even though the child process is detached, it is still a 65698685dc8SJohn Baldwin * child of the debugger, so it will still report it's exit 65798685dc8SJohn Baldwin * after the grandchild. 65898685dc8SJohn Baldwin */ 65998685dc8SJohn Baldwin wpid = wait(&status); 66098685dc8SJohn Baldwin ATF_REQUIRE(wpid == children[1]); 66198685dc8SJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 66298685dc8SJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 2); 66398685dc8SJohn Baldwin 66498685dc8SJohn Baldwin wpid = wait(&status); 66598685dc8SJohn Baldwin ATF_REQUIRE(wpid == children[0]); 66698685dc8SJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 66798685dc8SJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 1); 66898685dc8SJohn Baldwin 66998685dc8SJohn Baldwin wpid = wait(&status); 67098685dc8SJohn Baldwin ATF_REQUIRE(wpid == -1); 67198685dc8SJohn Baldwin ATF_REQUIRE(errno == ECHILD); 67298685dc8SJohn Baldwin } 67398685dc8SJohn Baldwin 67498685dc8SJohn Baldwin static void 67598685dc8SJohn Baldwin attach_fork_parent(int cpipe[2]) 67698685dc8SJohn Baldwin { 67798685dc8SJohn Baldwin pid_t fpid; 67898685dc8SJohn Baldwin 67998685dc8SJohn Baldwin close(cpipe[0]); 68098685dc8SJohn Baldwin 68198685dc8SJohn Baldwin /* Double-fork to disassociate from the debugger. */ 68298685dc8SJohn Baldwin CHILD_REQUIRE((fpid = fork()) != -1); 68398685dc8SJohn Baldwin if (fpid != 0) 684b98cb919SJohn Baldwin _exit(3); 68598685dc8SJohn Baldwin 68698685dc8SJohn Baldwin /* Send the pid of the disassociated child to the debugger. */ 68798685dc8SJohn Baldwin fpid = getpid(); 68898685dc8SJohn Baldwin CHILD_REQUIRE(write(cpipe[1], &fpid, sizeof(fpid)) == sizeof(fpid)); 68998685dc8SJohn Baldwin 69098685dc8SJohn Baldwin /* Wait for the debugger to attach. */ 69198685dc8SJohn Baldwin CHILD_REQUIRE(read(cpipe[1], &fpid, sizeof(fpid)) == 0); 69298685dc8SJohn Baldwin } 69398685dc8SJohn Baldwin 69498685dc8SJohn Baldwin /* 69598685dc8SJohn Baldwin * Verify that a new child process is stopped after a followed fork and 69698685dc8SJohn Baldwin * that the traced parent sees the exit of the child after the debugger 69798685dc8SJohn Baldwin * when both processes remain attached to the debugger. In this test 69898685dc8SJohn Baldwin * the parent that forks is not a direct child of the debugger. 69998685dc8SJohn Baldwin */ 70098685dc8SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached_unrelated_debugger); 70198685dc8SJohn Baldwin ATF_TC_BODY(ptrace__follow_fork_both_attached_unrelated_debugger, tc) 70298685dc8SJohn Baldwin { 703479b610dSJohn Baldwin pid_t children[2], fpid, wpid; 70498685dc8SJohn Baldwin int cpipe[2], status; 70598685dc8SJohn Baldwin 70698685dc8SJohn Baldwin ATF_REQUIRE(pipe(cpipe) == 0); 70798685dc8SJohn Baldwin ATF_REQUIRE((fpid = fork()) != -1); 70898685dc8SJohn Baldwin if (fpid == 0) { 70998685dc8SJohn Baldwin attach_fork_parent(cpipe); 710189ac973SJohn Baldwin follow_fork_parent(false); 71198685dc8SJohn Baldwin } 71298685dc8SJohn Baldwin 71398685dc8SJohn Baldwin /* Parent process. */ 71498685dc8SJohn Baldwin close(cpipe[1]); 71598685dc8SJohn Baldwin 71698685dc8SJohn Baldwin /* Wait for the direct child to exit. */ 71798685dc8SJohn Baldwin wpid = waitpid(fpid, &status, 0); 71898685dc8SJohn Baldwin ATF_REQUIRE(wpid == fpid); 71998685dc8SJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 72098685dc8SJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 3); 72198685dc8SJohn Baldwin 72298685dc8SJohn Baldwin /* Read the pid of the fork parent. */ 72398685dc8SJohn Baldwin ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) == 72498685dc8SJohn Baldwin sizeof(children[0])); 72598685dc8SJohn Baldwin 72698685dc8SJohn Baldwin /* Attach to the fork parent. */ 72798685dc8SJohn Baldwin attach_child(children[0]); 72898685dc8SJohn Baldwin 72998685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1); 73098685dc8SJohn Baldwin 73198685dc8SJohn Baldwin /* Continue the fork parent ignoring the SIGSTOP. */ 73298685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1); 73398685dc8SJohn Baldwin 73498685dc8SJohn Baldwin /* Signal the fork parent to continue. */ 73598685dc8SJohn Baldwin close(cpipe[0]); 73698685dc8SJohn Baldwin 737189ac973SJohn Baldwin children[1] = handle_fork_events(children[0], NULL); 73898685dc8SJohn Baldwin ATF_REQUIRE(children[1] > 0); 73998685dc8SJohn Baldwin 74098685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1); 74198685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1); 74298685dc8SJohn Baldwin 74398685dc8SJohn Baldwin /* 74498685dc8SJohn Baldwin * The fork parent can't exit until the child reports status, 74598685dc8SJohn Baldwin * so the child should report its exit first to the debugger. 74698685dc8SJohn Baldwin */ 74798685dc8SJohn Baldwin wpid = wait(&status); 74898685dc8SJohn Baldwin ATF_REQUIRE(wpid == children[1]); 74998685dc8SJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 75098685dc8SJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 2); 75198685dc8SJohn Baldwin 75298685dc8SJohn Baldwin wpid = wait(&status); 75398685dc8SJohn Baldwin ATF_REQUIRE(wpid == children[0]); 75498685dc8SJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 75598685dc8SJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 1); 75698685dc8SJohn Baldwin 75798685dc8SJohn Baldwin wpid = wait(&status); 75898685dc8SJohn Baldwin ATF_REQUIRE(wpid == -1); 75998685dc8SJohn Baldwin ATF_REQUIRE(errno == ECHILD); 76098685dc8SJohn Baldwin } 76198685dc8SJohn Baldwin 76298685dc8SJohn Baldwin /* 76398685dc8SJohn Baldwin * Verify that a new child process is stopped after a followed fork 76498685dc8SJohn Baldwin * and that the traced parent sees the exit of the child when the new 76598685dc8SJohn Baldwin * child process is detached after it reports its fork. In this test 76698685dc8SJohn Baldwin * the parent that forks is not a direct child of the debugger. 76798685dc8SJohn Baldwin */ 76898685dc8SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached_unrelated_debugger); 76998685dc8SJohn Baldwin ATF_TC_BODY(ptrace__follow_fork_child_detached_unrelated_debugger, tc) 77098685dc8SJohn Baldwin { 771479b610dSJohn Baldwin pid_t children[2], fpid, wpid; 77298685dc8SJohn Baldwin int cpipe[2], status; 77398685dc8SJohn Baldwin 77498685dc8SJohn Baldwin ATF_REQUIRE(pipe(cpipe) == 0); 77598685dc8SJohn Baldwin ATF_REQUIRE((fpid = fork()) != -1); 77698685dc8SJohn Baldwin if (fpid == 0) { 77798685dc8SJohn Baldwin attach_fork_parent(cpipe); 778189ac973SJohn Baldwin follow_fork_parent(false); 77998685dc8SJohn Baldwin } 78098685dc8SJohn Baldwin 78198685dc8SJohn Baldwin /* Parent process. */ 78298685dc8SJohn Baldwin close(cpipe[1]); 78398685dc8SJohn Baldwin 78498685dc8SJohn Baldwin /* Wait for the direct child to exit. */ 78598685dc8SJohn Baldwin wpid = waitpid(fpid, &status, 0); 78698685dc8SJohn Baldwin ATF_REQUIRE(wpid == fpid); 78798685dc8SJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 78898685dc8SJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 3); 78998685dc8SJohn Baldwin 79098685dc8SJohn Baldwin /* Read the pid of the fork parent. */ 79198685dc8SJohn Baldwin ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) == 79298685dc8SJohn Baldwin sizeof(children[0])); 79398685dc8SJohn Baldwin 79498685dc8SJohn Baldwin /* Attach to the fork parent. */ 79598685dc8SJohn Baldwin attach_child(children[0]); 79698685dc8SJohn Baldwin 79798685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1); 79898685dc8SJohn Baldwin 79998685dc8SJohn Baldwin /* Continue the fork parent ignoring the SIGSTOP. */ 80098685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1); 80198685dc8SJohn Baldwin 80298685dc8SJohn Baldwin /* Signal the fork parent to continue. */ 80398685dc8SJohn Baldwin close(cpipe[0]); 80498685dc8SJohn Baldwin 805189ac973SJohn Baldwin children[1] = handle_fork_events(children[0], NULL); 80698685dc8SJohn Baldwin ATF_REQUIRE(children[1] > 0); 80798685dc8SJohn Baldwin 80898685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1); 80998685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1); 81098685dc8SJohn Baldwin 81198685dc8SJohn Baldwin /* 81298685dc8SJohn Baldwin * Should not see any status from the child now, only the fork 81398685dc8SJohn Baldwin * parent. 81498685dc8SJohn Baldwin */ 81598685dc8SJohn Baldwin wpid = wait(&status); 81698685dc8SJohn Baldwin ATF_REQUIRE(wpid == children[0]); 81798685dc8SJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 81898685dc8SJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 1); 81998685dc8SJohn Baldwin 82098685dc8SJohn Baldwin wpid = wait(&status); 82198685dc8SJohn Baldwin ATF_REQUIRE(wpid == -1); 82298685dc8SJohn Baldwin ATF_REQUIRE(errno == ECHILD); 82398685dc8SJohn Baldwin } 82498685dc8SJohn Baldwin 82598685dc8SJohn Baldwin /* 82698685dc8SJohn Baldwin * Verify that a new child process is stopped after a followed fork 82798685dc8SJohn Baldwin * and that the traced parent sees the exit of the child when the 82898685dc8SJohn Baldwin * traced parent is detached after the fork. In this test the parent 82998685dc8SJohn Baldwin * that forks is not a direct child of the debugger. 83098685dc8SJohn Baldwin */ 83198685dc8SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached_unrelated_debugger); 83298685dc8SJohn Baldwin ATF_TC_BODY(ptrace__follow_fork_parent_detached_unrelated_debugger, tc) 83398685dc8SJohn Baldwin { 834479b610dSJohn Baldwin pid_t children[2], fpid, wpid; 83598685dc8SJohn Baldwin int cpipe[2], status; 83698685dc8SJohn Baldwin 83798685dc8SJohn Baldwin ATF_REQUIRE(pipe(cpipe) == 0); 83898685dc8SJohn Baldwin ATF_REQUIRE((fpid = fork()) != -1); 83998685dc8SJohn Baldwin if (fpid == 0) { 84098685dc8SJohn Baldwin attach_fork_parent(cpipe); 841189ac973SJohn Baldwin follow_fork_parent(false); 84298685dc8SJohn Baldwin } 84398685dc8SJohn Baldwin 84498685dc8SJohn Baldwin /* Parent process. */ 84598685dc8SJohn Baldwin close(cpipe[1]); 84698685dc8SJohn Baldwin 84798685dc8SJohn Baldwin /* Wait for the direct child to exit. */ 84898685dc8SJohn Baldwin wpid = waitpid(fpid, &status, 0); 84998685dc8SJohn Baldwin ATF_REQUIRE(wpid == fpid); 85098685dc8SJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 85198685dc8SJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 3); 85298685dc8SJohn Baldwin 85398685dc8SJohn Baldwin /* Read the pid of the fork parent. */ 85498685dc8SJohn Baldwin ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) == 85598685dc8SJohn Baldwin sizeof(children[0])); 85698685dc8SJohn Baldwin 85798685dc8SJohn Baldwin /* Attach to the fork parent. */ 85898685dc8SJohn Baldwin attach_child(children[0]); 85998685dc8SJohn Baldwin 86098685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1); 86198685dc8SJohn Baldwin 86298685dc8SJohn Baldwin /* Continue the fork parent ignoring the SIGSTOP. */ 86398685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1); 86498685dc8SJohn Baldwin 86598685dc8SJohn Baldwin /* Signal the fork parent to continue. */ 86698685dc8SJohn Baldwin close(cpipe[0]); 86798685dc8SJohn Baldwin 868189ac973SJohn Baldwin children[1] = handle_fork_events(children[0], NULL); 86998685dc8SJohn Baldwin ATF_REQUIRE(children[1] > 0); 87098685dc8SJohn Baldwin 87198685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1); 87298685dc8SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1); 87398685dc8SJohn Baldwin 87498685dc8SJohn Baldwin /* 87598685dc8SJohn Baldwin * Should not see any status from the fork parent now, only 87698685dc8SJohn Baldwin * the child. 87798685dc8SJohn Baldwin */ 87898685dc8SJohn Baldwin wpid = wait(&status); 87998685dc8SJohn Baldwin ATF_REQUIRE(wpid == children[1]); 88098685dc8SJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 88198685dc8SJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 2); 88298685dc8SJohn Baldwin 88398685dc8SJohn Baldwin wpid = wait(&status); 88498685dc8SJohn Baldwin ATF_REQUIRE(wpid == -1); 88598685dc8SJohn Baldwin ATF_REQUIRE(errno == ECHILD); 88698685dc8SJohn Baldwin } 88798685dc8SJohn Baldwin 888368b2b1cSJohn Baldwin /* 889368b2b1cSJohn Baldwin * Verify that a child process does not see an unrelated debugger as its 890368b2b1cSJohn Baldwin * parent but sees its original parent process. 891368b2b1cSJohn Baldwin */ 892368b2b1cSJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__getppid); 893368b2b1cSJohn Baldwin ATF_TC_BODY(ptrace__getppid, tc) 894368b2b1cSJohn Baldwin { 895368b2b1cSJohn Baldwin pid_t child, debugger, ppid, wpid; 896368b2b1cSJohn Baldwin int cpipe[2], dpipe[2], status; 897368b2b1cSJohn Baldwin char c; 898368b2b1cSJohn Baldwin 899368b2b1cSJohn Baldwin ATF_REQUIRE(pipe(cpipe) == 0); 900368b2b1cSJohn Baldwin ATF_REQUIRE((child = fork()) != -1); 901368b2b1cSJohn Baldwin 902368b2b1cSJohn Baldwin if (child == 0) { 903368b2b1cSJohn Baldwin /* Child process. */ 904368b2b1cSJohn Baldwin close(cpipe[0]); 905368b2b1cSJohn Baldwin 906368b2b1cSJohn Baldwin /* Wait for parent to be ready. */ 907368b2b1cSJohn Baldwin CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c)); 908368b2b1cSJohn Baldwin 909368b2b1cSJohn Baldwin /* Report the parent PID to the parent. */ 910368b2b1cSJohn Baldwin ppid = getppid(); 911368b2b1cSJohn Baldwin CHILD_REQUIRE(write(cpipe[1], &ppid, sizeof(ppid)) == 912368b2b1cSJohn Baldwin sizeof(ppid)); 913368b2b1cSJohn Baldwin 914368b2b1cSJohn Baldwin _exit(1); 915368b2b1cSJohn Baldwin } 916368b2b1cSJohn Baldwin close(cpipe[1]); 917368b2b1cSJohn Baldwin 918368b2b1cSJohn Baldwin ATF_REQUIRE(pipe(dpipe) == 0); 919368b2b1cSJohn Baldwin ATF_REQUIRE((debugger = fork()) != -1); 920368b2b1cSJohn Baldwin 921368b2b1cSJohn Baldwin if (debugger == 0) { 922368b2b1cSJohn Baldwin /* Debugger process. */ 923368b2b1cSJohn Baldwin close(dpipe[0]); 924368b2b1cSJohn Baldwin 925368b2b1cSJohn Baldwin CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1); 926368b2b1cSJohn Baldwin 927368b2b1cSJohn Baldwin wpid = waitpid(child, &status, 0); 928368b2b1cSJohn Baldwin CHILD_REQUIRE(wpid == child); 929368b2b1cSJohn Baldwin CHILD_REQUIRE(WIFSTOPPED(status)); 930368b2b1cSJohn Baldwin CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP); 931368b2b1cSJohn Baldwin 932368b2b1cSJohn Baldwin CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1); 933368b2b1cSJohn Baldwin 934368b2b1cSJohn Baldwin /* Signal parent that debugger is attached. */ 935368b2b1cSJohn Baldwin CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c)); 936368b2b1cSJohn Baldwin 937368b2b1cSJohn Baldwin /* Wait for traced child to exit. */ 938368b2b1cSJohn Baldwin wpid = waitpid(child, &status, 0); 939368b2b1cSJohn Baldwin CHILD_REQUIRE(wpid == child); 940368b2b1cSJohn Baldwin CHILD_REQUIRE(WIFEXITED(status)); 941368b2b1cSJohn Baldwin CHILD_REQUIRE(WEXITSTATUS(status) == 1); 942368b2b1cSJohn Baldwin 943368b2b1cSJohn Baldwin _exit(0); 944368b2b1cSJohn Baldwin } 945368b2b1cSJohn Baldwin close(dpipe[1]); 946368b2b1cSJohn Baldwin 947368b2b1cSJohn Baldwin /* Parent process. */ 948368b2b1cSJohn Baldwin 949368b2b1cSJohn Baldwin /* Wait for the debugger to attach to the child. */ 950368b2b1cSJohn Baldwin ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c)); 951368b2b1cSJohn Baldwin 952368b2b1cSJohn Baldwin /* Release the child. */ 953368b2b1cSJohn Baldwin ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c)); 954368b2b1cSJohn Baldwin 955368b2b1cSJohn Baldwin /* Read the parent PID from the child. */ 956368b2b1cSJohn Baldwin ATF_REQUIRE(read(cpipe[0], &ppid, sizeof(ppid)) == sizeof(ppid)); 957368b2b1cSJohn Baldwin close(cpipe[0]); 958368b2b1cSJohn Baldwin 959368b2b1cSJohn Baldwin ATF_REQUIRE(ppid == getpid()); 960368b2b1cSJohn Baldwin 961368b2b1cSJohn Baldwin /* Wait for the debugger. */ 962368b2b1cSJohn Baldwin wpid = waitpid(debugger, &status, 0); 963368b2b1cSJohn Baldwin ATF_REQUIRE(wpid == debugger); 964368b2b1cSJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 965368b2b1cSJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 0); 966368b2b1cSJohn Baldwin 967368b2b1cSJohn Baldwin /* The child process should now be ready. */ 968368b2b1cSJohn Baldwin wpid = waitpid(child, &status, WNOHANG); 969368b2b1cSJohn Baldwin ATF_REQUIRE(wpid == child); 970368b2b1cSJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 971368b2b1cSJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 1); 972368b2b1cSJohn Baldwin } 973368b2b1cSJohn Baldwin 974189ac973SJohn Baldwin /* 975189ac973SJohn Baldwin * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new 976189ac973SJohn Baldwin * child process created via fork() reports the correct value. 977189ac973SJohn Baldwin */ 978189ac973SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_fork); 979189ac973SJohn Baldwin ATF_TC_BODY(ptrace__new_child_pl_syscall_code_fork, tc) 980189ac973SJohn Baldwin { 981189ac973SJohn Baldwin struct ptrace_lwpinfo pl[2]; 982189ac973SJohn Baldwin pid_t children[2], fpid, wpid; 983189ac973SJohn Baldwin int status; 984189ac973SJohn Baldwin 985189ac973SJohn Baldwin ATF_REQUIRE((fpid = fork()) != -1); 986189ac973SJohn Baldwin if (fpid == 0) { 987189ac973SJohn Baldwin trace_me(); 988189ac973SJohn Baldwin follow_fork_parent(false); 989189ac973SJohn Baldwin } 990189ac973SJohn Baldwin 991189ac973SJohn Baldwin /* Parent process. */ 992189ac973SJohn Baldwin children[0] = fpid; 993189ac973SJohn Baldwin 994189ac973SJohn Baldwin /* The first wait() should report the stop from SIGSTOP. */ 995189ac973SJohn Baldwin wpid = waitpid(children[0], &status, 0); 996189ac973SJohn Baldwin ATF_REQUIRE(wpid == children[0]); 997189ac973SJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 998189ac973SJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 999189ac973SJohn Baldwin 1000189ac973SJohn Baldwin ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1); 1001189ac973SJohn Baldwin 1002189ac973SJohn Baldwin /* Continue the child ignoring the SIGSTOP. */ 1003189ac973SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1); 1004189ac973SJohn Baldwin 1005189ac973SJohn Baldwin /* Wait for both halves of the fork event to get reported. */ 1006189ac973SJohn Baldwin children[1] = handle_fork_events(children[0], pl); 1007189ac973SJohn Baldwin ATF_REQUIRE(children[1] > 0); 1008189ac973SJohn Baldwin 1009189ac973SJohn Baldwin ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0); 1010189ac973SJohn Baldwin ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0); 1011189ac973SJohn Baldwin ATF_REQUIRE(pl[0].pl_syscall_code == SYS_fork); 1012189ac973SJohn Baldwin ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code); 1013189ac973SJohn Baldwin ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg); 1014189ac973SJohn Baldwin 1015189ac973SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1); 1016189ac973SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1); 1017189ac973SJohn Baldwin 1018189ac973SJohn Baldwin /* 1019189ac973SJohn Baldwin * The child can't exit until the grandchild reports status, so the 1020189ac973SJohn Baldwin * grandchild should report its exit first to the debugger. 1021189ac973SJohn Baldwin */ 1022189ac973SJohn Baldwin wpid = wait(&status); 1023189ac973SJohn Baldwin ATF_REQUIRE(wpid == children[1]); 1024189ac973SJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 1025189ac973SJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 2); 1026189ac973SJohn Baldwin 1027189ac973SJohn Baldwin wpid = wait(&status); 1028189ac973SJohn Baldwin ATF_REQUIRE(wpid == children[0]); 1029189ac973SJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 1030189ac973SJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 1); 1031189ac973SJohn Baldwin 1032189ac973SJohn Baldwin wpid = wait(&status); 1033189ac973SJohn Baldwin ATF_REQUIRE(wpid == -1); 1034189ac973SJohn Baldwin ATF_REQUIRE(errno == ECHILD); 1035189ac973SJohn Baldwin } 1036189ac973SJohn Baldwin 1037189ac973SJohn Baldwin /* 1038189ac973SJohn Baldwin * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new 1039189ac973SJohn Baldwin * child process created via vfork() reports the correct value. 1040189ac973SJohn Baldwin */ 1041189ac973SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_vfork); 1042189ac973SJohn Baldwin ATF_TC_BODY(ptrace__new_child_pl_syscall_code_vfork, tc) 1043189ac973SJohn Baldwin { 1044189ac973SJohn Baldwin struct ptrace_lwpinfo pl[2]; 1045189ac973SJohn Baldwin pid_t children[2], fpid, wpid; 1046189ac973SJohn Baldwin int status; 1047189ac973SJohn Baldwin 1048189ac973SJohn Baldwin ATF_REQUIRE((fpid = fork()) != -1); 1049189ac973SJohn Baldwin if (fpid == 0) { 1050189ac973SJohn Baldwin trace_me(); 1051189ac973SJohn Baldwin follow_fork_parent(true); 1052189ac973SJohn Baldwin } 1053189ac973SJohn Baldwin 1054189ac973SJohn Baldwin /* Parent process. */ 1055189ac973SJohn Baldwin children[0] = fpid; 1056189ac973SJohn Baldwin 1057189ac973SJohn Baldwin /* The first wait() should report the stop from SIGSTOP. */ 1058189ac973SJohn Baldwin wpid = waitpid(children[0], &status, 0); 1059189ac973SJohn Baldwin ATF_REQUIRE(wpid == children[0]); 1060189ac973SJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 1061189ac973SJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 1062189ac973SJohn Baldwin 1063189ac973SJohn Baldwin ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1); 1064189ac973SJohn Baldwin 1065189ac973SJohn Baldwin /* Continue the child ignoring the SIGSTOP. */ 1066189ac973SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1); 1067189ac973SJohn Baldwin 1068189ac973SJohn Baldwin /* Wait for both halves of the fork event to get reported. */ 1069189ac973SJohn Baldwin children[1] = handle_fork_events(children[0], pl); 1070189ac973SJohn Baldwin ATF_REQUIRE(children[1] > 0); 1071189ac973SJohn Baldwin 1072189ac973SJohn Baldwin ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0); 1073189ac973SJohn Baldwin ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0); 1074189ac973SJohn Baldwin ATF_REQUIRE(pl[0].pl_syscall_code == SYS_vfork); 1075189ac973SJohn Baldwin ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code); 1076189ac973SJohn Baldwin ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg); 1077189ac973SJohn Baldwin 1078189ac973SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1); 1079189ac973SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1); 1080189ac973SJohn Baldwin 1081189ac973SJohn Baldwin /* 1082189ac973SJohn Baldwin * The child can't exit until the grandchild reports status, so the 1083189ac973SJohn Baldwin * grandchild should report its exit first to the debugger. 1084189ac973SJohn Baldwin */ 1085189ac973SJohn Baldwin wpid = wait(&status); 1086189ac973SJohn Baldwin ATF_REQUIRE(wpid == children[1]); 1087189ac973SJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 1088189ac973SJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 2); 1089189ac973SJohn Baldwin 1090189ac973SJohn Baldwin wpid = wait(&status); 1091189ac973SJohn Baldwin ATF_REQUIRE(wpid == children[0]); 1092189ac973SJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 1093189ac973SJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 1); 1094189ac973SJohn Baldwin 1095189ac973SJohn Baldwin wpid = wait(&status); 1096189ac973SJohn Baldwin ATF_REQUIRE(wpid == -1); 1097189ac973SJohn Baldwin ATF_REQUIRE(errno == ECHILD); 1098189ac973SJohn Baldwin } 1099189ac973SJohn Baldwin 1100189ac973SJohn Baldwin static void * 1101189ac973SJohn Baldwin simple_thread(void *arg __unused) 1102189ac973SJohn Baldwin { 1103189ac973SJohn Baldwin 1104189ac973SJohn Baldwin pthread_exit(NULL); 1105189ac973SJohn Baldwin } 1106189ac973SJohn Baldwin 11075fcfab6eSJohn Baldwin static __dead2 void 11085fcfab6eSJohn Baldwin simple_thread_main(void) 11095fcfab6eSJohn Baldwin { 11105fcfab6eSJohn Baldwin pthread_t thread; 11115fcfab6eSJohn Baldwin 11125fcfab6eSJohn Baldwin CHILD_REQUIRE(pthread_create(&thread, NULL, simple_thread, NULL) == 0); 11135fcfab6eSJohn Baldwin CHILD_REQUIRE(pthread_join(thread, NULL) == 0); 11145fcfab6eSJohn Baldwin exit(1); 11155fcfab6eSJohn Baldwin } 11165fcfab6eSJohn Baldwin 1117189ac973SJohn Baldwin /* 1118189ac973SJohn Baldwin * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new 1119189ac973SJohn Baldwin * thread reports the correct value. 1120189ac973SJohn Baldwin */ 1121189ac973SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_thread); 1122189ac973SJohn Baldwin ATF_TC_BODY(ptrace__new_child_pl_syscall_code_thread, tc) 1123189ac973SJohn Baldwin { 1124189ac973SJohn Baldwin struct ptrace_lwpinfo pl; 1125189ac973SJohn Baldwin pid_t fpid, wpid; 1126e72879e5SJohn Baldwin lwpid_t mainlwp; 1127189ac973SJohn Baldwin int status; 1128189ac973SJohn Baldwin 1129189ac973SJohn Baldwin ATF_REQUIRE((fpid = fork()) != -1); 1130189ac973SJohn Baldwin if (fpid == 0) { 1131189ac973SJohn Baldwin trace_me(); 11325fcfab6eSJohn Baldwin simple_thread_main(); 1133189ac973SJohn Baldwin } 1134189ac973SJohn Baldwin 1135189ac973SJohn Baldwin /* The first wait() should report the stop from SIGSTOP. */ 1136189ac973SJohn Baldwin wpid = waitpid(fpid, &status, 0); 1137189ac973SJohn Baldwin ATF_REQUIRE(wpid == fpid); 1138189ac973SJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 1139189ac973SJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 1140189ac973SJohn Baldwin 1141189ac973SJohn Baldwin ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, 1142189ac973SJohn Baldwin sizeof(pl)) != -1); 1143e72879e5SJohn Baldwin mainlwp = pl.pl_lwpid; 1144189ac973SJohn Baldwin 1145189ac973SJohn Baldwin /* 1146189ac973SJohn Baldwin * Continue the child ignoring the SIGSTOP and tracing all 1147189ac973SJohn Baldwin * system call exits. 1148189ac973SJohn Baldwin */ 1149189ac973SJohn Baldwin ATF_REQUIRE(ptrace(PT_TO_SCX, fpid, (caddr_t)1, 0) != -1); 1150189ac973SJohn Baldwin 1151189ac973SJohn Baldwin /* 1152189ac973SJohn Baldwin * Wait for the new thread to arrive. pthread_create() might 1153189ac973SJohn Baldwin * invoke any number of system calls. For now we just wait 1154189ac973SJohn Baldwin * for the new thread to arrive and make sure it reports a 1155189ac973SJohn Baldwin * valid system call code. If ptrace grows thread event 1156189ac973SJohn Baldwin * reporting then this test can be made more precise. 1157189ac973SJohn Baldwin */ 1158189ac973SJohn Baldwin for (;;) { 1159189ac973SJohn Baldwin wpid = waitpid(fpid, &status, 0); 1160189ac973SJohn Baldwin ATF_REQUIRE(wpid == fpid); 1161189ac973SJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 1162189ac973SJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP); 1163189ac973SJohn Baldwin 1164189ac973SJohn Baldwin ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, 1165189ac973SJohn Baldwin sizeof(pl)) != -1); 1166189ac973SJohn Baldwin ATF_REQUIRE((pl.pl_flags & PL_FLAG_SCX) != 0); 1167189ac973SJohn Baldwin ATF_REQUIRE(pl.pl_syscall_code != 0); 1168e72879e5SJohn Baldwin if (pl.pl_lwpid != mainlwp) 1169189ac973SJohn Baldwin /* New thread seen. */ 1170189ac973SJohn Baldwin break; 1171189ac973SJohn Baldwin 1172189ac973SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 1173189ac973SJohn Baldwin } 1174189ac973SJohn Baldwin 1175189ac973SJohn Baldwin /* Wait for the child to exit. */ 1176189ac973SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 1177189ac973SJohn Baldwin for (;;) { 1178189ac973SJohn Baldwin wpid = waitpid(fpid, &status, 0); 1179189ac973SJohn Baldwin ATF_REQUIRE(wpid == fpid); 1180189ac973SJohn Baldwin if (WIFEXITED(status)) 1181189ac973SJohn Baldwin break; 1182189ac973SJohn Baldwin 1183189ac973SJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 1184189ac973SJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP); 1185189ac973SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 1186189ac973SJohn Baldwin } 1187189ac973SJohn Baldwin 1188189ac973SJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 1); 1189189ac973SJohn Baldwin 1190189ac973SJohn Baldwin wpid = wait(&status); 1191189ac973SJohn Baldwin ATF_REQUIRE(wpid == -1); 1192189ac973SJohn Baldwin ATF_REQUIRE(errno == ECHILD); 1193189ac973SJohn Baldwin } 1194189ac973SJohn Baldwin 11955fcfab6eSJohn Baldwin /* 11965fcfab6eSJohn Baldwin * Verify that the expected LWP events are reported for a child thread. 11975fcfab6eSJohn Baldwin */ 11985fcfab6eSJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__lwp_events); 11995fcfab6eSJohn Baldwin ATF_TC_BODY(ptrace__lwp_events, tc) 12005fcfab6eSJohn Baldwin { 12015fcfab6eSJohn Baldwin struct ptrace_lwpinfo pl; 12025fcfab6eSJohn Baldwin pid_t fpid, wpid; 12035fcfab6eSJohn Baldwin lwpid_t lwps[2]; 12045fcfab6eSJohn Baldwin int status; 12055fcfab6eSJohn Baldwin 12065fcfab6eSJohn Baldwin ATF_REQUIRE((fpid = fork()) != -1); 12075fcfab6eSJohn Baldwin if (fpid == 0) { 12085fcfab6eSJohn Baldwin trace_me(); 12095fcfab6eSJohn Baldwin simple_thread_main(); 12105fcfab6eSJohn Baldwin } 12115fcfab6eSJohn Baldwin 12125fcfab6eSJohn Baldwin /* The first wait() should report the stop from SIGSTOP. */ 12135fcfab6eSJohn Baldwin wpid = waitpid(fpid, &status, 0); 12145fcfab6eSJohn Baldwin ATF_REQUIRE(wpid == fpid); 12155fcfab6eSJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 12165fcfab6eSJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 12175fcfab6eSJohn Baldwin 12185fcfab6eSJohn Baldwin ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, 12195fcfab6eSJohn Baldwin sizeof(pl)) != -1); 12205fcfab6eSJohn Baldwin lwps[0] = pl.pl_lwpid; 12215fcfab6eSJohn Baldwin 12225fcfab6eSJohn Baldwin ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0); 12235fcfab6eSJohn Baldwin 12245fcfab6eSJohn Baldwin /* Continue the child ignoring the SIGSTOP. */ 12255fcfab6eSJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 12265fcfab6eSJohn Baldwin 12275fcfab6eSJohn Baldwin /* The first event should be for the child thread's birth. */ 12285fcfab6eSJohn Baldwin wpid = waitpid(fpid, &status, 0); 12295fcfab6eSJohn Baldwin ATF_REQUIRE(wpid == fpid); 12305fcfab6eSJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 12315fcfab6eSJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP); 12325fcfab6eSJohn Baldwin 12335fcfab6eSJohn Baldwin ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1); 12345fcfab6eSJohn Baldwin ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) == 12355fcfab6eSJohn Baldwin (PL_FLAG_BORN | PL_FLAG_SCX)); 12365fcfab6eSJohn Baldwin ATF_REQUIRE(pl.pl_lwpid != lwps[0]); 12375fcfab6eSJohn Baldwin lwps[1] = pl.pl_lwpid; 12385fcfab6eSJohn Baldwin 12395fcfab6eSJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 12405fcfab6eSJohn Baldwin 12415fcfab6eSJohn Baldwin /* The next event should be for the child thread's death. */ 12425fcfab6eSJohn Baldwin wpid = waitpid(fpid, &status, 0); 12435fcfab6eSJohn Baldwin ATF_REQUIRE(wpid == fpid); 12445fcfab6eSJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 12455fcfab6eSJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP); 12465fcfab6eSJohn Baldwin 12475fcfab6eSJohn Baldwin ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1); 12485fcfab6eSJohn Baldwin ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) == 12495fcfab6eSJohn Baldwin (PL_FLAG_EXITED | PL_FLAG_SCE)); 12505fcfab6eSJohn Baldwin ATF_REQUIRE(pl.pl_lwpid == lwps[1]); 12515fcfab6eSJohn Baldwin 12525fcfab6eSJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 12535fcfab6eSJohn Baldwin 12545fcfab6eSJohn Baldwin /* The last event should be for the child process's exit. */ 12555fcfab6eSJohn Baldwin wpid = waitpid(fpid, &status, 0); 12565fcfab6eSJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 12575fcfab6eSJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 1); 12585fcfab6eSJohn Baldwin 12595fcfab6eSJohn Baldwin wpid = wait(&status); 12605fcfab6eSJohn Baldwin ATF_REQUIRE(wpid == -1); 12615fcfab6eSJohn Baldwin ATF_REQUIRE(errno == ECHILD); 12625fcfab6eSJohn Baldwin } 12635fcfab6eSJohn Baldwin 12645fcfab6eSJohn Baldwin static void * 12655fcfab6eSJohn Baldwin exec_thread(void *arg __unused) 12665fcfab6eSJohn Baldwin { 12675fcfab6eSJohn Baldwin 12685fcfab6eSJohn Baldwin execl("/usr/bin/true", "true", NULL); 12695fcfab6eSJohn Baldwin exit(127); 12705fcfab6eSJohn Baldwin } 12715fcfab6eSJohn Baldwin 12725fcfab6eSJohn Baldwin static __dead2 void 12735fcfab6eSJohn Baldwin exec_thread_main(void) 12745fcfab6eSJohn Baldwin { 12755fcfab6eSJohn Baldwin pthread_t thread; 12765fcfab6eSJohn Baldwin 12775fcfab6eSJohn Baldwin CHILD_REQUIRE(pthread_create(&thread, NULL, exec_thread, NULL) == 0); 12785fcfab6eSJohn Baldwin for (;;) 12795fcfab6eSJohn Baldwin sleep(60); 12805fcfab6eSJohn Baldwin exit(1); 12815fcfab6eSJohn Baldwin } 12825fcfab6eSJohn Baldwin 12835fcfab6eSJohn Baldwin /* 12845fcfab6eSJohn Baldwin * Verify that the expected LWP events are reported for a multithreaded 12855fcfab6eSJohn Baldwin * process that calls execve(2). 12865fcfab6eSJohn Baldwin */ 12875fcfab6eSJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__lwp_events_exec); 12885fcfab6eSJohn Baldwin ATF_TC_BODY(ptrace__lwp_events_exec, tc) 12895fcfab6eSJohn Baldwin { 12905fcfab6eSJohn Baldwin struct ptrace_lwpinfo pl; 12915fcfab6eSJohn Baldwin pid_t fpid, wpid; 12925fcfab6eSJohn Baldwin lwpid_t lwps[2]; 12935fcfab6eSJohn Baldwin int status; 12945fcfab6eSJohn Baldwin 12955fcfab6eSJohn Baldwin ATF_REQUIRE((fpid = fork()) != -1); 12965fcfab6eSJohn Baldwin if (fpid == 0) { 12975fcfab6eSJohn Baldwin trace_me(); 12985fcfab6eSJohn Baldwin exec_thread_main(); 12995fcfab6eSJohn Baldwin } 13005fcfab6eSJohn Baldwin 13015fcfab6eSJohn Baldwin /* The first wait() should report the stop from SIGSTOP. */ 13025fcfab6eSJohn Baldwin wpid = waitpid(fpid, &status, 0); 13035fcfab6eSJohn Baldwin ATF_REQUIRE(wpid == fpid); 13045fcfab6eSJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 13055fcfab6eSJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 13065fcfab6eSJohn Baldwin 13075fcfab6eSJohn Baldwin ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, 13085fcfab6eSJohn Baldwin sizeof(pl)) != -1); 13095fcfab6eSJohn Baldwin lwps[0] = pl.pl_lwpid; 13105fcfab6eSJohn Baldwin 13115fcfab6eSJohn Baldwin ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0); 13125fcfab6eSJohn Baldwin 13135fcfab6eSJohn Baldwin /* Continue the child ignoring the SIGSTOP. */ 13145fcfab6eSJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 13155fcfab6eSJohn Baldwin 13165fcfab6eSJohn Baldwin /* The first event should be for the child thread's birth. */ 13175fcfab6eSJohn Baldwin wpid = waitpid(fpid, &status, 0); 13185fcfab6eSJohn Baldwin ATF_REQUIRE(wpid == fpid); 13195fcfab6eSJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 13205fcfab6eSJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP); 13215fcfab6eSJohn Baldwin 13225fcfab6eSJohn Baldwin ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1); 13235fcfab6eSJohn Baldwin ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) == 13245fcfab6eSJohn Baldwin (PL_FLAG_BORN | PL_FLAG_SCX)); 13255fcfab6eSJohn Baldwin ATF_REQUIRE(pl.pl_lwpid != lwps[0]); 13265fcfab6eSJohn Baldwin lwps[1] = pl.pl_lwpid; 13275fcfab6eSJohn Baldwin 13285fcfab6eSJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 13295fcfab6eSJohn Baldwin 13305fcfab6eSJohn Baldwin /* 13315fcfab6eSJohn Baldwin * The next event should be for the main thread's death due to 13325fcfab6eSJohn Baldwin * single threading from execve(). 13335fcfab6eSJohn Baldwin */ 13345fcfab6eSJohn Baldwin wpid = waitpid(fpid, &status, 0); 13355fcfab6eSJohn Baldwin ATF_REQUIRE(wpid == fpid); 13365fcfab6eSJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 13375fcfab6eSJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP); 13385fcfab6eSJohn Baldwin 13395fcfab6eSJohn Baldwin ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1); 13405fcfab6eSJohn Baldwin ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) == 13415fcfab6eSJohn Baldwin (PL_FLAG_EXITED)); 13425fcfab6eSJohn Baldwin ATF_REQUIRE(pl.pl_lwpid == lwps[0]); 13435fcfab6eSJohn Baldwin 13445fcfab6eSJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 13455fcfab6eSJohn Baldwin 13465fcfab6eSJohn Baldwin /* The next event should be for the child process's exec. */ 13475fcfab6eSJohn Baldwin wpid = waitpid(fpid, &status, 0); 13485fcfab6eSJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 13495fcfab6eSJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP); 13505fcfab6eSJohn Baldwin 13515fcfab6eSJohn Baldwin ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1); 13525fcfab6eSJohn Baldwin ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) == 13535fcfab6eSJohn Baldwin (PL_FLAG_EXEC | PL_FLAG_SCX)); 13545fcfab6eSJohn Baldwin ATF_REQUIRE(pl.pl_lwpid == lwps[1]); 13555fcfab6eSJohn Baldwin 13565fcfab6eSJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 13575fcfab6eSJohn Baldwin 13585fcfab6eSJohn Baldwin /* The last event should be for the child process's exit. */ 13595fcfab6eSJohn Baldwin wpid = waitpid(fpid, &status, 0); 13605fcfab6eSJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 13615fcfab6eSJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 0); 13625fcfab6eSJohn Baldwin 13635fcfab6eSJohn Baldwin wpid = wait(&status); 13645fcfab6eSJohn Baldwin ATF_REQUIRE(wpid == -1); 13655fcfab6eSJohn Baldwin ATF_REQUIRE(errno == ECHILD); 13665fcfab6eSJohn Baldwin } 13675fcfab6eSJohn Baldwin 13683340c45bSJohn Baldwin static void 13693340c45bSJohn Baldwin handler(int sig __unused) 13703340c45bSJohn Baldwin { 13713340c45bSJohn Baldwin } 13723340c45bSJohn Baldwin 13733340c45bSJohn Baldwin static void 13743340c45bSJohn Baldwin signal_main(void) 13753340c45bSJohn Baldwin { 13763340c45bSJohn Baldwin 13773340c45bSJohn Baldwin signal(SIGINFO, handler); 13783340c45bSJohn Baldwin raise(SIGINFO); 13793340c45bSJohn Baldwin exit(0); 13803340c45bSJohn Baldwin } 13813340c45bSJohn Baldwin 13823340c45bSJohn Baldwin /* 13833340c45bSJohn Baldwin * Verify that the expected ptrace event is reported for a signal. 13843340c45bSJohn Baldwin */ 13853340c45bSJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__siginfo); 13863340c45bSJohn Baldwin ATF_TC_BODY(ptrace__siginfo, tc) 13873340c45bSJohn Baldwin { 13883340c45bSJohn Baldwin struct ptrace_lwpinfo pl; 13893340c45bSJohn Baldwin pid_t fpid, wpid; 13903340c45bSJohn Baldwin int status; 13913340c45bSJohn Baldwin 13923340c45bSJohn Baldwin ATF_REQUIRE((fpid = fork()) != -1); 13933340c45bSJohn Baldwin if (fpid == 0) { 13943340c45bSJohn Baldwin trace_me(); 13953340c45bSJohn Baldwin signal_main(); 13963340c45bSJohn Baldwin } 13973340c45bSJohn Baldwin 13983340c45bSJohn Baldwin /* The first wait() should report the stop from SIGSTOP. */ 13993340c45bSJohn Baldwin wpid = waitpid(fpid, &status, 0); 14003340c45bSJohn Baldwin ATF_REQUIRE(wpid == fpid); 14013340c45bSJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 14023340c45bSJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 14033340c45bSJohn Baldwin 14043340c45bSJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 14053340c45bSJohn Baldwin 14063340c45bSJohn Baldwin /* The next event should be for the SIGINFO. */ 14073340c45bSJohn Baldwin wpid = waitpid(fpid, &status, 0); 14083340c45bSJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 14093340c45bSJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGINFO); 14103340c45bSJohn Baldwin 14113340c45bSJohn Baldwin ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1); 14123340c45bSJohn Baldwin ATF_REQUIRE(pl.pl_event == PL_EVENT_SIGNAL); 14133340c45bSJohn Baldwin ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI); 14143340c45bSJohn Baldwin ATF_REQUIRE(pl.pl_siginfo.si_code == SI_LWP); 14153340c45bSJohn Baldwin ATF_REQUIRE(pl.pl_siginfo.si_pid == wpid); 14163340c45bSJohn Baldwin 14173340c45bSJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 14183340c45bSJohn Baldwin 14193340c45bSJohn Baldwin /* The last event should be for the child process's exit. */ 14203340c45bSJohn Baldwin wpid = waitpid(fpid, &status, 0); 14213340c45bSJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 14223340c45bSJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 0); 14233340c45bSJohn Baldwin 14243340c45bSJohn Baldwin wpid = wait(&status); 14253340c45bSJohn Baldwin ATF_REQUIRE(wpid == -1); 14263340c45bSJohn Baldwin ATF_REQUIRE(errno == ECHILD); 14273340c45bSJohn Baldwin } 14283340c45bSJohn Baldwin 14298d570f64SJohn Baldwin /* 14308d570f64SJohn Baldwin * Verify that the expected ptrace events are reported for PTRACE_EXEC. 14318d570f64SJohn Baldwin */ 14328d570f64SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_disable); 14338d570f64SJohn Baldwin ATF_TC_BODY(ptrace__ptrace_exec_disable, tc) 14348d570f64SJohn Baldwin { 14358d570f64SJohn Baldwin pid_t fpid, wpid; 14368d570f64SJohn Baldwin int events, status; 14378d570f64SJohn Baldwin 14388d570f64SJohn Baldwin ATF_REQUIRE((fpid = fork()) != -1); 14398d570f64SJohn Baldwin if (fpid == 0) { 14408d570f64SJohn Baldwin trace_me(); 14418d570f64SJohn Baldwin exec_thread(NULL); 14428d570f64SJohn Baldwin } 14438d570f64SJohn Baldwin 14448d570f64SJohn Baldwin /* The first wait() should report the stop from SIGSTOP. */ 14458d570f64SJohn Baldwin wpid = waitpid(fpid, &status, 0); 14468d570f64SJohn Baldwin ATF_REQUIRE(wpid == fpid); 14478d570f64SJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 14488d570f64SJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 14498d570f64SJohn Baldwin 14508d570f64SJohn Baldwin events = 0; 14518d570f64SJohn Baldwin ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events, 14528d570f64SJohn Baldwin sizeof(events)) == 0); 14538d570f64SJohn Baldwin 14548d570f64SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 14558d570f64SJohn Baldwin 14568d570f64SJohn Baldwin /* Should get one event at exit. */ 14578d570f64SJohn Baldwin wpid = waitpid(fpid, &status, 0); 14588d570f64SJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 14598d570f64SJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 0); 14608d570f64SJohn Baldwin 14618d570f64SJohn Baldwin wpid = wait(&status); 14628d570f64SJohn Baldwin ATF_REQUIRE(wpid == -1); 14638d570f64SJohn Baldwin ATF_REQUIRE(errno == ECHILD); 14648d570f64SJohn Baldwin } 14658d570f64SJohn Baldwin 14668d570f64SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_enable); 14678d570f64SJohn Baldwin ATF_TC_BODY(ptrace__ptrace_exec_enable, tc) 14688d570f64SJohn Baldwin { 14698d570f64SJohn Baldwin struct ptrace_lwpinfo pl; 14708d570f64SJohn Baldwin pid_t fpid, wpid; 14718d570f64SJohn Baldwin int events, status; 14728d570f64SJohn Baldwin 14738d570f64SJohn Baldwin ATF_REQUIRE((fpid = fork()) != -1); 14748d570f64SJohn Baldwin if (fpid == 0) { 14758d570f64SJohn Baldwin trace_me(); 14768d570f64SJohn Baldwin exec_thread(NULL); 14778d570f64SJohn Baldwin } 14788d570f64SJohn Baldwin 14798d570f64SJohn Baldwin /* The first wait() should report the stop from SIGSTOP. */ 14808d570f64SJohn Baldwin wpid = waitpid(fpid, &status, 0); 14818d570f64SJohn Baldwin ATF_REQUIRE(wpid == fpid); 14828d570f64SJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 14838d570f64SJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 14848d570f64SJohn Baldwin 14858d570f64SJohn Baldwin events = PTRACE_EXEC; 14868d570f64SJohn Baldwin ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events, 14878d570f64SJohn Baldwin sizeof(events)) == 0); 14888d570f64SJohn Baldwin 14898d570f64SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 14908d570f64SJohn Baldwin 14918d570f64SJohn Baldwin /* The next event should be for the child process's exec. */ 14928d570f64SJohn Baldwin wpid = waitpid(fpid, &status, 0); 14938d570f64SJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 14948d570f64SJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP); 14958d570f64SJohn Baldwin 14968d570f64SJohn Baldwin ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1); 14978d570f64SJohn Baldwin ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) == 14988d570f64SJohn Baldwin (PL_FLAG_EXEC | PL_FLAG_SCX)); 14998d570f64SJohn Baldwin 15008d570f64SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 15018d570f64SJohn Baldwin 15028d570f64SJohn Baldwin /* The last event should be for the child process's exit. */ 15038d570f64SJohn Baldwin wpid = waitpid(fpid, &status, 0); 15048d570f64SJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 15058d570f64SJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 0); 15068d570f64SJohn Baldwin 15078d570f64SJohn Baldwin wpid = wait(&status); 15088d570f64SJohn Baldwin ATF_REQUIRE(wpid == -1); 15098d570f64SJohn Baldwin ATF_REQUIRE(errno == ECHILD); 15108d570f64SJohn Baldwin } 15118d570f64SJohn Baldwin 15128d570f64SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__event_mask); 15138d570f64SJohn Baldwin ATF_TC_BODY(ptrace__event_mask, tc) 15148d570f64SJohn Baldwin { 15158d570f64SJohn Baldwin pid_t fpid, wpid; 15168d570f64SJohn Baldwin int events, status; 15178d570f64SJohn Baldwin 15188d570f64SJohn Baldwin ATF_REQUIRE((fpid = fork()) != -1); 15198d570f64SJohn Baldwin if (fpid == 0) { 15208d570f64SJohn Baldwin trace_me(); 15218d570f64SJohn Baldwin exit(0); 15228d570f64SJohn Baldwin } 15238d570f64SJohn Baldwin 15248d570f64SJohn Baldwin /* The first wait() should report the stop from SIGSTOP. */ 15258d570f64SJohn Baldwin wpid = waitpid(fpid, &status, 0); 15268d570f64SJohn Baldwin ATF_REQUIRE(wpid == fpid); 15278d570f64SJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 15288d570f64SJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 15298d570f64SJohn Baldwin 15308d570f64SJohn Baldwin /* PT_FOLLOW_FORK should toggle the state of PTRACE_FORK. */ 15318d570f64SJohn Baldwin ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 1) != -1); 15328d570f64SJohn Baldwin ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events, 15338d570f64SJohn Baldwin sizeof(events)) == 0); 15348d570f64SJohn Baldwin ATF_REQUIRE(events & PTRACE_FORK); 15358d570f64SJohn Baldwin ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 0) != -1); 15368d570f64SJohn Baldwin ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events, 15378d570f64SJohn Baldwin sizeof(events)) == 0); 15388d570f64SJohn Baldwin ATF_REQUIRE(!(events & PTRACE_FORK)); 15398d570f64SJohn Baldwin 15408d570f64SJohn Baldwin /* PT_LWP_EVENTS should toggle the state of PTRACE_LWP. */ 15418d570f64SJohn Baldwin ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 1) != -1); 15428d570f64SJohn Baldwin ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events, 15438d570f64SJohn Baldwin sizeof(events)) == 0); 15448d570f64SJohn Baldwin ATF_REQUIRE(events & PTRACE_LWP); 15458d570f64SJohn Baldwin ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 0) != -1); 15468d570f64SJohn Baldwin ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events, 15478d570f64SJohn Baldwin sizeof(events)) == 0); 15488d570f64SJohn Baldwin ATF_REQUIRE(!(events & PTRACE_LWP)); 15498d570f64SJohn Baldwin 15508d570f64SJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 15518d570f64SJohn Baldwin 15528d570f64SJohn Baldwin /* Should get one event at exit. */ 15538d570f64SJohn Baldwin wpid = waitpid(fpid, &status, 0); 15548d570f64SJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 15558d570f64SJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 0); 15568d570f64SJohn Baldwin 15578d570f64SJohn Baldwin wpid = wait(&status); 15588d570f64SJohn Baldwin ATF_REQUIRE(wpid == -1); 15598d570f64SJohn Baldwin ATF_REQUIRE(errno == ECHILD); 15608d570f64SJohn Baldwin } 15618d570f64SJohn Baldwin 1562fc4f075aSJohn Baldwin /* 1563fc4f075aSJohn Baldwin * Verify that the expected ptrace events are reported for PTRACE_VFORK. 1564fc4f075aSJohn Baldwin */ 1565fc4f075aSJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork); 1566fc4f075aSJohn Baldwin ATF_TC_BODY(ptrace__ptrace_vfork, tc) 1567fc4f075aSJohn Baldwin { 1568fc4f075aSJohn Baldwin struct ptrace_lwpinfo pl; 1569fc4f075aSJohn Baldwin pid_t fpid, wpid; 1570fc4f075aSJohn Baldwin int events, status; 1571fc4f075aSJohn Baldwin 1572fc4f075aSJohn Baldwin ATF_REQUIRE((fpid = fork()) != -1); 1573fc4f075aSJohn Baldwin if (fpid == 0) { 1574fc4f075aSJohn Baldwin trace_me(); 1575fc4f075aSJohn Baldwin follow_fork_parent(true); 1576fc4f075aSJohn Baldwin } 1577fc4f075aSJohn Baldwin 1578fc4f075aSJohn Baldwin /* The first wait() should report the stop from SIGSTOP. */ 1579fc4f075aSJohn Baldwin wpid = waitpid(fpid, &status, 0); 1580fc4f075aSJohn Baldwin ATF_REQUIRE(wpid == fpid); 1581fc4f075aSJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 1582fc4f075aSJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 1583fc4f075aSJohn Baldwin 1584fc4f075aSJohn Baldwin ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events, 1585fc4f075aSJohn Baldwin sizeof(events)) == 0); 1586fc4f075aSJohn Baldwin events |= PTRACE_VFORK; 1587fc4f075aSJohn Baldwin ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events, 1588fc4f075aSJohn Baldwin sizeof(events)) == 0); 1589fc4f075aSJohn Baldwin 1590fc4f075aSJohn Baldwin /* Continue the child ignoring the SIGSTOP. */ 1591fc4f075aSJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1); 1592fc4f075aSJohn Baldwin 1593fc4f075aSJohn Baldwin /* The next event should report the end of the vfork. */ 1594fc4f075aSJohn Baldwin wpid = wait(&status); 1595fc4f075aSJohn Baldwin ATF_REQUIRE(wpid == fpid); 1596fc4f075aSJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 1597fc4f075aSJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP); 1598fc4f075aSJohn Baldwin ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1); 1599fc4f075aSJohn Baldwin ATF_REQUIRE((pl.pl_flags & PL_FLAG_VFORK_DONE) != 0); 1600fc4f075aSJohn Baldwin 1601fc4f075aSJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1); 1602fc4f075aSJohn Baldwin 1603fc4f075aSJohn Baldwin wpid = wait(&status); 1604fc4f075aSJohn Baldwin ATF_REQUIRE(wpid == fpid); 1605fc4f075aSJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 1606fc4f075aSJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 1); 1607fc4f075aSJohn Baldwin 1608fc4f075aSJohn Baldwin wpid = wait(&status); 1609fc4f075aSJohn Baldwin ATF_REQUIRE(wpid == -1); 1610fc4f075aSJohn Baldwin ATF_REQUIRE(errno == ECHILD); 1611fc4f075aSJohn Baldwin } 1612fc4f075aSJohn Baldwin 1613fc4f075aSJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork_follow); 1614fc4f075aSJohn Baldwin ATF_TC_BODY(ptrace__ptrace_vfork_follow, tc) 1615fc4f075aSJohn Baldwin { 1616fc4f075aSJohn Baldwin struct ptrace_lwpinfo pl[2]; 1617fc4f075aSJohn Baldwin pid_t children[2], fpid, wpid; 1618fc4f075aSJohn Baldwin int events, status; 1619fc4f075aSJohn Baldwin 1620fc4f075aSJohn Baldwin ATF_REQUIRE((fpid = fork()) != -1); 1621fc4f075aSJohn Baldwin if (fpid == 0) { 1622fc4f075aSJohn Baldwin trace_me(); 1623fc4f075aSJohn Baldwin follow_fork_parent(true); 1624fc4f075aSJohn Baldwin } 1625fc4f075aSJohn Baldwin 1626fc4f075aSJohn Baldwin /* Parent process. */ 1627fc4f075aSJohn Baldwin children[0] = fpid; 1628fc4f075aSJohn Baldwin 1629fc4f075aSJohn Baldwin /* The first wait() should report the stop from SIGSTOP. */ 1630fc4f075aSJohn Baldwin wpid = waitpid(children[0], &status, 0); 1631fc4f075aSJohn Baldwin ATF_REQUIRE(wpid == children[0]); 1632fc4f075aSJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 1633fc4f075aSJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 1634fc4f075aSJohn Baldwin 1635fc4f075aSJohn Baldwin ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, children[0], (caddr_t)&events, 1636fc4f075aSJohn Baldwin sizeof(events)) == 0); 1637fc4f075aSJohn Baldwin events |= PTRACE_FORK | PTRACE_VFORK; 1638fc4f075aSJohn Baldwin ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, children[0], (caddr_t)&events, 1639fc4f075aSJohn Baldwin sizeof(events)) == 0); 1640fc4f075aSJohn Baldwin 1641fc4f075aSJohn Baldwin /* Continue the child ignoring the SIGSTOP. */ 1642fc4f075aSJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1); 1643fc4f075aSJohn Baldwin 1644fc4f075aSJohn Baldwin /* Wait for both halves of the fork event to get reported. */ 1645fc4f075aSJohn Baldwin children[1] = handle_fork_events(children[0], pl); 1646fc4f075aSJohn Baldwin ATF_REQUIRE(children[1] > 0); 1647fc4f075aSJohn Baldwin 1648fc4f075aSJohn Baldwin ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORKED) != 0); 1649fc4f075aSJohn Baldwin 1650fc4f075aSJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1); 1651fc4f075aSJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1); 1652fc4f075aSJohn Baldwin 1653fc4f075aSJohn Baldwin /* 1654fc4f075aSJohn Baldwin * The child can't exit until the grandchild reports status, so the 1655fc4f075aSJohn Baldwin * grandchild should report its exit first to the debugger. 1656fc4f075aSJohn Baldwin */ 1657fc4f075aSJohn Baldwin wpid = waitpid(children[1], &status, 0); 1658fc4f075aSJohn Baldwin ATF_REQUIRE(wpid == children[1]); 1659fc4f075aSJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 1660fc4f075aSJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 2); 1661fc4f075aSJohn Baldwin 1662fc4f075aSJohn Baldwin /* 1663fc4f075aSJohn Baldwin * The child should report it's vfork() completion before it 1664fc4f075aSJohn Baldwin * exits. 1665fc4f075aSJohn Baldwin */ 1666fc4f075aSJohn Baldwin wpid = wait(&status); 1667fc4f075aSJohn Baldwin ATF_REQUIRE(wpid == children[0]); 1668fc4f075aSJohn Baldwin ATF_REQUIRE(WIFSTOPPED(status)); 1669fc4f075aSJohn Baldwin ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP); 1670fc4f075aSJohn Baldwin ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl[0], sizeof(pl[0])) != 1671fc4f075aSJohn Baldwin -1); 1672fc4f075aSJohn Baldwin ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORK_DONE) != 0); 1673fc4f075aSJohn Baldwin 1674fc4f075aSJohn Baldwin ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1); 1675fc4f075aSJohn Baldwin 1676fc4f075aSJohn Baldwin wpid = wait(&status); 1677fc4f075aSJohn Baldwin ATF_REQUIRE(wpid == children[0]); 1678fc4f075aSJohn Baldwin ATF_REQUIRE(WIFEXITED(status)); 1679fc4f075aSJohn Baldwin ATF_REQUIRE(WEXITSTATUS(status) == 1); 1680fc4f075aSJohn Baldwin 1681fc4f075aSJohn Baldwin wpid = wait(&status); 1682fc4f075aSJohn Baldwin ATF_REQUIRE(wpid == -1); 1683fc4f075aSJohn Baldwin ATF_REQUIRE(errno == ECHILD); 1684fc4f075aSJohn Baldwin } 1685fc4f075aSJohn Baldwin 168682a4538fSEric Badger /* 1687e2ebfbbfSEric Badger * XXX: There's nothing inherently platform specific about this test, however a 1688e2ebfbbfSEric Badger * userspace visible breakpoint() is a prerequisite. 1689e2ebfbbfSEric Badger */ 1690e2ebfbbfSEric Badger #if defined(__amd64__) || defined(__i386__) || defined(__sparc64__) 1691e2ebfbbfSEric Badger /* 169282a4538fSEric Badger * Verify that no more events are reported after PT_KILL except for the 169382a4538fSEric Badger * process exit when stopped due to a breakpoint trap. 169482a4538fSEric Badger */ 169582a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_breakpoint); 169682a4538fSEric Badger ATF_TC_BODY(ptrace__PT_KILL_breakpoint, tc) 169782a4538fSEric Badger { 169882a4538fSEric Badger pid_t fpid, wpid; 169982a4538fSEric Badger int status; 170082a4538fSEric Badger 170182a4538fSEric Badger ATF_REQUIRE((fpid = fork()) != -1); 170282a4538fSEric Badger if (fpid == 0) { 170382a4538fSEric Badger trace_me(); 17049e0d1159SEric Badger breakpoint(); 170582a4538fSEric Badger exit(1); 170682a4538fSEric Badger } 170782a4538fSEric Badger 170882a4538fSEric Badger /* The first wait() should report the stop from SIGSTOP. */ 170982a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 171082a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 171182a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 171282a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 171382a4538fSEric Badger 171482a4538fSEric Badger /* Continue the child ignoring the SIGSTOP. */ 171582a4538fSEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 171682a4538fSEric Badger 171782a4538fSEric Badger /* The second wait() should report hitting the breakpoint. */ 171882a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 171982a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 172082a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 172182a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP); 172282a4538fSEric Badger 172382a4538fSEric Badger /* Kill the child process. */ 172482a4538fSEric Badger ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0); 172582a4538fSEric Badger 172682a4538fSEric Badger /* The last wait() should report the SIGKILL. */ 172782a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 172882a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 172982a4538fSEric Badger ATF_REQUIRE(WIFSIGNALED(status)); 173082a4538fSEric Badger ATF_REQUIRE(WTERMSIG(status) == SIGKILL); 173182a4538fSEric Badger 173282a4538fSEric Badger wpid = wait(&status); 173382a4538fSEric Badger ATF_REQUIRE(wpid == -1); 173482a4538fSEric Badger ATF_REQUIRE(errno == ECHILD); 173582a4538fSEric Badger } 1736e2ebfbbfSEric Badger #endif /* defined(__amd64__) || defined(__i386__) || defined(__sparc64__) */ 173782a4538fSEric Badger 173882a4538fSEric Badger /* 173982a4538fSEric Badger * Verify that no more events are reported after PT_KILL except for the 174082a4538fSEric Badger * process exit when stopped inside of a system call. 174182a4538fSEric Badger */ 174282a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_system_call); 174382a4538fSEric Badger ATF_TC_BODY(ptrace__PT_KILL_system_call, tc) 174482a4538fSEric Badger { 174582a4538fSEric Badger struct ptrace_lwpinfo pl; 174682a4538fSEric Badger pid_t fpid, wpid; 174782a4538fSEric Badger int status; 174882a4538fSEric Badger 174982a4538fSEric Badger ATF_REQUIRE((fpid = fork()) != -1); 175082a4538fSEric Badger if (fpid == 0) { 175182a4538fSEric Badger trace_me(); 175282a4538fSEric Badger getpid(); 175382a4538fSEric Badger exit(1); 175482a4538fSEric Badger } 175582a4538fSEric Badger 175682a4538fSEric Badger /* The first wait() should report the stop from SIGSTOP. */ 175782a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 175882a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 175982a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 176082a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 176182a4538fSEric Badger 176282a4538fSEric Badger /* Continue the child ignoring the SIGSTOP and tracing system calls. */ 176382a4538fSEric Badger ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0); 176482a4538fSEric Badger 176582a4538fSEric Badger /* The second wait() should report a system call entry for getpid(). */ 176682a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 176782a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 176882a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 176982a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP); 177082a4538fSEric Badger 177182a4538fSEric Badger ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1); 177282a4538fSEric Badger ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE); 177382a4538fSEric Badger 177482a4538fSEric Badger /* Kill the child process. */ 177582a4538fSEric Badger ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0); 177682a4538fSEric Badger 177782a4538fSEric Badger /* The last wait() should report the SIGKILL. */ 177882a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 177982a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 178082a4538fSEric Badger ATF_REQUIRE(WIFSIGNALED(status)); 178182a4538fSEric Badger ATF_REQUIRE(WTERMSIG(status) == SIGKILL); 178282a4538fSEric Badger 178382a4538fSEric Badger wpid = wait(&status); 178482a4538fSEric Badger ATF_REQUIRE(wpid == -1); 178582a4538fSEric Badger ATF_REQUIRE(errno == ECHILD); 178682a4538fSEric Badger } 178782a4538fSEric Badger 178882a4538fSEric Badger /* 178982a4538fSEric Badger * Verify that no more events are reported after PT_KILL except for the 179082a4538fSEric Badger * process exit when killing a multithreaded process. 179182a4538fSEric Badger */ 179282a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_threads); 179382a4538fSEric Badger ATF_TC_BODY(ptrace__PT_KILL_threads, tc) 179482a4538fSEric Badger { 179582a4538fSEric Badger struct ptrace_lwpinfo pl; 179682a4538fSEric Badger pid_t fpid, wpid; 179782a4538fSEric Badger lwpid_t main_lwp; 179882a4538fSEric Badger int status; 179982a4538fSEric Badger 180082a4538fSEric Badger ATF_REQUIRE((fpid = fork()) != -1); 180182a4538fSEric Badger if (fpid == 0) { 180282a4538fSEric Badger trace_me(); 180382a4538fSEric Badger simple_thread_main(); 180482a4538fSEric Badger } 180582a4538fSEric Badger 180682a4538fSEric Badger /* The first wait() should report the stop from SIGSTOP. */ 180782a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 180882a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 180982a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 181082a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 181182a4538fSEric Badger 181282a4538fSEric Badger ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, 181382a4538fSEric Badger sizeof(pl)) != -1); 181482a4538fSEric Badger main_lwp = pl.pl_lwpid; 181582a4538fSEric Badger 181682a4538fSEric Badger ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0); 181782a4538fSEric Badger 181882a4538fSEric Badger /* Continue the child ignoring the SIGSTOP. */ 181982a4538fSEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 182082a4538fSEric Badger 182182a4538fSEric Badger /* The first event should be for the child thread's birth. */ 182282a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 182382a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 182482a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 182582a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP); 182682a4538fSEric Badger 182782a4538fSEric Badger ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1); 182882a4538fSEric Badger ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) == 182982a4538fSEric Badger (PL_FLAG_BORN | PL_FLAG_SCX)); 183082a4538fSEric Badger ATF_REQUIRE(pl.pl_lwpid != main_lwp); 183182a4538fSEric Badger 183282a4538fSEric Badger /* Kill the child process. */ 183382a4538fSEric Badger ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0); 183482a4538fSEric Badger 183582a4538fSEric Badger /* The last wait() should report the SIGKILL. */ 183682a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 183782a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 183882a4538fSEric Badger ATF_REQUIRE(WIFSIGNALED(status)); 183982a4538fSEric Badger ATF_REQUIRE(WTERMSIG(status) == SIGKILL); 184082a4538fSEric Badger 184182a4538fSEric Badger wpid = wait(&status); 184282a4538fSEric Badger ATF_REQUIRE(wpid == -1); 184382a4538fSEric Badger ATF_REQUIRE(errno == ECHILD); 184482a4538fSEric Badger } 184582a4538fSEric Badger 184682a4538fSEric Badger static void * 184782a4538fSEric Badger mask_usr1_thread(void *arg) 184882a4538fSEric Badger { 184982a4538fSEric Badger pthread_barrier_t *pbarrier; 185082a4538fSEric Badger sigset_t sigmask; 185182a4538fSEric Badger 185282a4538fSEric Badger pbarrier = (pthread_barrier_t*)arg; 185382a4538fSEric Badger 185482a4538fSEric Badger sigemptyset(&sigmask); 185582a4538fSEric Badger sigaddset(&sigmask, SIGUSR1); 185682a4538fSEric Badger CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0); 185782a4538fSEric Badger 185882a4538fSEric Badger /* Sync up with other thread after sigmask updated. */ 185982a4538fSEric Badger pthread_barrier_wait(pbarrier); 186082a4538fSEric Badger 186182a4538fSEric Badger for (;;) 186282a4538fSEric Badger sleep(60); 186382a4538fSEric Badger 186482a4538fSEric Badger return (NULL); 186582a4538fSEric Badger } 186682a4538fSEric Badger 186782a4538fSEric Badger /* 186882a4538fSEric Badger * Verify that the SIGKILL from PT_KILL takes priority over other signals 186982a4538fSEric Badger * and prevents spurious stops due to those other signals. 187082a4538fSEric Badger */ 187182a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_competing_signal); 187282a4538fSEric Badger ATF_TC_BODY(ptrace__PT_KILL_competing_signal, tc) 187382a4538fSEric Badger { 187482a4538fSEric Badger pid_t fpid, wpid; 187582a4538fSEric Badger int status; 187682a4538fSEric Badger cpuset_t setmask; 187782a4538fSEric Badger pthread_t t; 187882a4538fSEric Badger pthread_barrier_t barrier; 1879bc2be1d3SEric Badger struct sched_param sched_param; 188082a4538fSEric Badger 188182a4538fSEric Badger ATF_REQUIRE((fpid = fork()) != -1); 188282a4538fSEric Badger if (fpid == 0) { 1883bc2be1d3SEric Badger /* Bind to one CPU so only one thread at a time will run. */ 188482a4538fSEric Badger CPU_ZERO(&setmask); 188582a4538fSEric Badger CPU_SET(0, &setmask); 188682a4538fSEric Badger cpusetid_t setid; 188782a4538fSEric Badger CHILD_REQUIRE(cpuset(&setid) == 0); 188882a4538fSEric Badger CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET, 188982a4538fSEric Badger CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0); 189082a4538fSEric Badger 189182a4538fSEric Badger CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0); 189282a4538fSEric Badger 189382a4538fSEric Badger CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread, 189482a4538fSEric Badger (void*)&barrier) == 0); 189582a4538fSEric Badger 1896bc2be1d3SEric Badger /* 1897bc2be1d3SEric Badger * Give the main thread higher priority. The test always 1898bc2be1d3SEric Badger * assumes that, if both threads are able to run, the main 1899bc2be1d3SEric Badger * thread runs first. 1900bc2be1d3SEric Badger */ 1901bc2be1d3SEric Badger sched_param.sched_priority = 1902bc2be1d3SEric Badger (sched_get_priority_max(SCHED_FIFO) + 1903bc2be1d3SEric Badger sched_get_priority_min(SCHED_FIFO)) / 2; 1904bc2be1d3SEric Badger CHILD_REQUIRE(pthread_setschedparam(pthread_self(), 1905bc2be1d3SEric Badger SCHED_FIFO, &sched_param) == 0); 1906bc2be1d3SEric Badger sched_param.sched_priority -= RQ_PPQ; 1907bc2be1d3SEric Badger CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO, 1908bc2be1d3SEric Badger &sched_param) == 0); 1909bc2be1d3SEric Badger 191082a4538fSEric Badger sigset_t sigmask; 191182a4538fSEric Badger sigemptyset(&sigmask); 191282a4538fSEric Badger sigaddset(&sigmask, SIGUSR2); 191382a4538fSEric Badger CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0); 191482a4538fSEric Badger 191582a4538fSEric Badger /* Sync up with other thread after sigmask updated. */ 191682a4538fSEric Badger pthread_barrier_wait(&barrier); 191782a4538fSEric Badger 191882a4538fSEric Badger trace_me(); 191982a4538fSEric Badger 192082a4538fSEric Badger for (;;) 192182a4538fSEric Badger sleep(60); 192282a4538fSEric Badger 192382a4538fSEric Badger exit(1); 192482a4538fSEric Badger } 192582a4538fSEric Badger 192682a4538fSEric Badger /* The first wait() should report the stop from SIGSTOP. */ 192782a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 192882a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 192982a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 193082a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 193182a4538fSEric Badger 193282a4538fSEric Badger /* Continue the child ignoring the SIGSTOP. */ 193382a4538fSEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 193482a4538fSEric Badger 193582a4538fSEric Badger /* Send a signal that only the second thread can handle. */ 193682a4538fSEric Badger ATF_REQUIRE(kill(fpid, SIGUSR2) == 0); 193782a4538fSEric Badger 193882a4538fSEric Badger /* The second wait() should report the SIGUSR2. */ 193982a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 194082a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 194182a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 194282a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2); 194382a4538fSEric Badger 194482a4538fSEric Badger /* Send a signal that only the first thread can handle. */ 194582a4538fSEric Badger ATF_REQUIRE(kill(fpid, SIGUSR1) == 0); 194682a4538fSEric Badger 194782a4538fSEric Badger /* Replace the SIGUSR2 with a kill. */ 194882a4538fSEric Badger ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0); 194982a4538fSEric Badger 195082a4538fSEric Badger /* The last wait() should report the SIGKILL (not the SIGUSR signal). */ 195182a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 195282a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 195382a4538fSEric Badger ATF_REQUIRE(WIFSIGNALED(status)); 195482a4538fSEric Badger ATF_REQUIRE(WTERMSIG(status) == SIGKILL); 195582a4538fSEric Badger 195682a4538fSEric Badger wpid = wait(&status); 195782a4538fSEric Badger ATF_REQUIRE(wpid == -1); 195882a4538fSEric Badger ATF_REQUIRE(errno == ECHILD); 195982a4538fSEric Badger } 196082a4538fSEric Badger 196182a4538fSEric Badger /* 196282a4538fSEric Badger * Verify that the SIGKILL from PT_KILL takes priority over other stop events 196382a4538fSEric Badger * and prevents spurious stops caused by those events. 196482a4538fSEric Badger */ 196582a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_competing_stop); 196682a4538fSEric Badger ATF_TC_BODY(ptrace__PT_KILL_competing_stop, tc) 196782a4538fSEric Badger { 196882a4538fSEric Badger pid_t fpid, wpid; 1969bc2be1d3SEric Badger int status; 197082a4538fSEric Badger cpuset_t setmask; 197182a4538fSEric Badger pthread_t t; 197282a4538fSEric Badger pthread_barrier_t barrier; 197382a4538fSEric Badger lwpid_t main_lwp; 197482a4538fSEric Badger struct ptrace_lwpinfo pl; 1975bc2be1d3SEric Badger struct sched_param sched_param; 197682a4538fSEric Badger 197782a4538fSEric Badger ATF_REQUIRE((fpid = fork()) != -1); 197882a4538fSEric Badger if (fpid == 0) { 197982a4538fSEric Badger trace_me(); 198082a4538fSEric Badger 1981bc2be1d3SEric Badger /* Bind to one CPU so only one thread at a time will run. */ 198282a4538fSEric Badger CPU_ZERO(&setmask); 198382a4538fSEric Badger CPU_SET(0, &setmask); 198482a4538fSEric Badger cpusetid_t setid; 198582a4538fSEric Badger CHILD_REQUIRE(cpuset(&setid) == 0); 198682a4538fSEric Badger CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET, 198782a4538fSEric Badger CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0); 198882a4538fSEric Badger 198982a4538fSEric Badger CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0); 199082a4538fSEric Badger 199182a4538fSEric Badger CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread, 199282a4538fSEric Badger (void*)&barrier) == 0); 199382a4538fSEric Badger 1994bc2be1d3SEric Badger /* 1995bc2be1d3SEric Badger * Give the main thread higher priority. The test always 1996bc2be1d3SEric Badger * assumes that, if both threads are able to run, the main 1997bc2be1d3SEric Badger * thread runs first. 1998bc2be1d3SEric Badger */ 1999bc2be1d3SEric Badger sched_param.sched_priority = 2000bc2be1d3SEric Badger (sched_get_priority_max(SCHED_FIFO) + 2001bc2be1d3SEric Badger sched_get_priority_min(SCHED_FIFO)) / 2; 2002bc2be1d3SEric Badger CHILD_REQUIRE(pthread_setschedparam(pthread_self(), 2003bc2be1d3SEric Badger SCHED_FIFO, &sched_param) == 0); 2004bc2be1d3SEric Badger sched_param.sched_priority -= RQ_PPQ; 2005bc2be1d3SEric Badger CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO, 2006bc2be1d3SEric Badger &sched_param) == 0); 2007bc2be1d3SEric Badger 200882a4538fSEric Badger sigset_t sigmask; 200982a4538fSEric Badger sigemptyset(&sigmask); 201082a4538fSEric Badger sigaddset(&sigmask, SIGUSR2); 201182a4538fSEric Badger CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0); 201282a4538fSEric Badger 201382a4538fSEric Badger /* Sync up with other thread after sigmask updated. */ 201482a4538fSEric Badger pthread_barrier_wait(&barrier); 201582a4538fSEric Badger 201682a4538fSEric Badger /* Sync up with the test before doing the getpid(). */ 201782a4538fSEric Badger raise(SIGSTOP); 201882a4538fSEric Badger 201982a4538fSEric Badger getpid(); 202082a4538fSEric Badger exit(1); 202182a4538fSEric Badger } 202282a4538fSEric Badger 202382a4538fSEric Badger /* The first wait() should report the stop from SIGSTOP. */ 202482a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 202582a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 202682a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 202782a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 202882a4538fSEric Badger 202982a4538fSEric Badger ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1); 203082a4538fSEric Badger main_lwp = pl.pl_lwpid; 203182a4538fSEric Badger 203282a4538fSEric Badger /* Continue the child ignoring the SIGSTOP and tracing system calls. */ 203382a4538fSEric Badger ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0); 203482a4538fSEric Badger 203582a4538fSEric Badger /* 203682a4538fSEric Badger * Continue until child is done with setup, which is indicated with 203782a4538fSEric Badger * SIGSTOP. Ignore system calls in the meantime. 203882a4538fSEric Badger */ 203982a4538fSEric Badger for (;;) { 204082a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 204182a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 204282a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 204382a4538fSEric Badger if (WSTOPSIG(status) == SIGTRAP) { 204482a4538fSEric Badger ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, 204582a4538fSEric Badger sizeof(pl)) != -1); 204682a4538fSEric Badger ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX)); 204782a4538fSEric Badger } else { 204882a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 204982a4538fSEric Badger break; 205082a4538fSEric Badger } 205182a4538fSEric Badger ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0); 205282a4538fSEric Badger } 205382a4538fSEric Badger 2054bc2be1d3SEric Badger /* Proceed, allowing main thread to hit syscall entry for getpid(). */ 205582a4538fSEric Badger ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0); 205682a4538fSEric Badger 205782a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 205882a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 205982a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 206082a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP); 206182a4538fSEric Badger 206282a4538fSEric Badger ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, 206382a4538fSEric Badger sizeof(pl)) != -1); 2064bc2be1d3SEric Badger ATF_REQUIRE(pl.pl_lwpid == main_lwp); 206582a4538fSEric Badger ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE); 2066bc2be1d3SEric Badger /* Prevent the main thread from hitting its syscall exit for now. */ 206782a4538fSEric Badger ATF_REQUIRE(ptrace(PT_SUSPEND, main_lwp, 0, 0) == 0); 206882a4538fSEric Badger 2069bc2be1d3SEric Badger /* 2070bc2be1d3SEric Badger * Proceed, allowing second thread to hit syscall exit for 2071bc2be1d3SEric Badger * pthread_barrier_wait(). 2072bc2be1d3SEric Badger */ 2073bc2be1d3SEric Badger ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0); 2074bc2be1d3SEric Badger 2075bc2be1d3SEric Badger wpid = waitpid(fpid, &status, 0); 2076bc2be1d3SEric Badger ATF_REQUIRE(wpid == fpid); 2077bc2be1d3SEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 2078bc2be1d3SEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP); 2079bc2be1d3SEric Badger 2080bc2be1d3SEric Badger ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, 2081bc2be1d3SEric Badger sizeof(pl)) != -1); 2082bc2be1d3SEric Badger ATF_REQUIRE(pl.pl_lwpid != main_lwp); 2083bc2be1d3SEric Badger ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX); 208482a4538fSEric Badger 208582a4538fSEric Badger /* Send a signal that only the second thread can handle. */ 208682a4538fSEric Badger ATF_REQUIRE(kill(fpid, SIGUSR2) == 0); 208782a4538fSEric Badger 208882a4538fSEric Badger ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0); 208982a4538fSEric Badger 2090bc2be1d3SEric Badger /* The next wait() should report the SIGUSR2. */ 209182a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 209282a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 209382a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 209482a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2); 209582a4538fSEric Badger 209682a4538fSEric Badger /* Allow the main thread to try to finish its system call. */ 209782a4538fSEric Badger ATF_REQUIRE(ptrace(PT_RESUME, main_lwp, 0, 0) == 0); 209882a4538fSEric Badger 209982a4538fSEric Badger /* 210082a4538fSEric Badger * At this point, the main thread is in the middle of a system call and 2101bc2be1d3SEric Badger * has been resumed. The second thread has taken a SIGUSR2 which will 2102bc2be1d3SEric Badger * be replaced with a SIGKILL below. The main thread will get to run 2103bc2be1d3SEric Badger * first. It should notice the kill request (even though the signal 2104bc2be1d3SEric Badger * replacement occurred in the other thread) and exit accordingly. It 2105bc2be1d3SEric Badger * should not stop for the system call exit event. 210682a4538fSEric Badger */ 210782a4538fSEric Badger 210882a4538fSEric Badger /* Replace the SIGUSR2 with a kill. */ 210982a4538fSEric Badger ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0); 211082a4538fSEric Badger 211182a4538fSEric Badger /* The last wait() should report the SIGKILL (not a syscall exit). */ 211282a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 211382a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 211482a4538fSEric Badger ATF_REQUIRE(WIFSIGNALED(status)); 211582a4538fSEric Badger ATF_REQUIRE(WTERMSIG(status) == SIGKILL); 211682a4538fSEric Badger 211782a4538fSEric Badger wpid = wait(&status); 211882a4538fSEric Badger ATF_REQUIRE(wpid == -1); 211982a4538fSEric Badger ATF_REQUIRE(errno == ECHILD); 212082a4538fSEric Badger } 212182a4538fSEric Badger 212282a4538fSEric Badger static void 212382a4538fSEric Badger sigusr1_handler(int sig) 212482a4538fSEric Badger { 212582a4538fSEric Badger 212682a4538fSEric Badger CHILD_REQUIRE(sig == SIGUSR1); 212782a4538fSEric Badger _exit(2); 212882a4538fSEric Badger } 212982a4538fSEric Badger 213082a4538fSEric Badger /* 213182a4538fSEric Badger * Verify that even if the signal queue is full for a child process, 213282a4538fSEric Badger * a PT_KILL will kill the process. 213382a4538fSEric Badger */ 213482a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_with_signal_full_sigqueue); 213582a4538fSEric Badger ATF_TC_BODY(ptrace__PT_KILL_with_signal_full_sigqueue, tc) 213682a4538fSEric Badger { 213782a4538fSEric Badger pid_t fpid, wpid; 213882a4538fSEric Badger int status; 213982a4538fSEric Badger int max_pending_per_proc; 214082a4538fSEric Badger size_t len; 214182a4538fSEric Badger int i; 214282a4538fSEric Badger 214382a4538fSEric Badger ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR); 214482a4538fSEric Badger 214582a4538fSEric Badger ATF_REQUIRE((fpid = fork()) != -1); 214682a4538fSEric Badger if (fpid == 0) { 214782a4538fSEric Badger trace_me(); 214882a4538fSEric Badger exit(1); 214982a4538fSEric Badger } 215082a4538fSEric Badger 215182a4538fSEric Badger /* The first wait() should report the stop from SIGSTOP. */ 215282a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 215382a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 215482a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 215582a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 215682a4538fSEric Badger 215782a4538fSEric Badger len = sizeof(max_pending_per_proc); 215882a4538fSEric Badger ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc", 215982a4538fSEric Badger &max_pending_per_proc, &len, NULL, 0) == 0); 216082a4538fSEric Badger 216182a4538fSEric Badger /* Fill the signal queue. */ 216282a4538fSEric Badger for (i = 0; i < max_pending_per_proc; ++i) 216382a4538fSEric Badger ATF_REQUIRE(kill(fpid, SIGUSR1) == 0); 216482a4538fSEric Badger 216582a4538fSEric Badger /* Kill the child process. */ 216682a4538fSEric Badger ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0); 216782a4538fSEric Badger 216882a4538fSEric Badger /* The last wait() should report the SIGKILL. */ 216982a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 217082a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 217182a4538fSEric Badger ATF_REQUIRE(WIFSIGNALED(status)); 217282a4538fSEric Badger ATF_REQUIRE(WTERMSIG(status) == SIGKILL); 217382a4538fSEric Badger 217482a4538fSEric Badger wpid = wait(&status); 217582a4538fSEric Badger ATF_REQUIRE(wpid == -1); 217682a4538fSEric Badger ATF_REQUIRE(errno == ECHILD); 217782a4538fSEric Badger } 217882a4538fSEric Badger 217982a4538fSEric Badger /* 218082a4538fSEric Badger * Verify that when stopped at a system call entry, a signal can be 218182a4538fSEric Badger * requested with PT_CONTINUE which will be delivered once the system 218282a4538fSEric Badger * call is complete. 218382a4538fSEric Badger */ 218482a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry); 218582a4538fSEric Badger ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry, tc) 218682a4538fSEric Badger { 218782a4538fSEric Badger struct ptrace_lwpinfo pl; 218882a4538fSEric Badger pid_t fpid, wpid; 218982a4538fSEric Badger int status; 219082a4538fSEric Badger 219182a4538fSEric Badger ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR); 219282a4538fSEric Badger 219382a4538fSEric Badger ATF_REQUIRE((fpid = fork()) != -1); 219482a4538fSEric Badger if (fpid == 0) { 219582a4538fSEric Badger trace_me(); 219682a4538fSEric Badger getpid(); 219782a4538fSEric Badger exit(1); 219882a4538fSEric Badger } 219982a4538fSEric Badger 220082a4538fSEric Badger /* The first wait() should report the stop from SIGSTOP. */ 220182a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 220282a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 220382a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 220482a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 220582a4538fSEric Badger 220682a4538fSEric Badger /* Continue the child ignoring the SIGSTOP and tracing system calls. */ 220782a4538fSEric Badger ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0); 220882a4538fSEric Badger 220982a4538fSEric Badger /* The second wait() should report a system call entry for getpid(). */ 221082a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 221182a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 221282a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 221382a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP); 221482a4538fSEric Badger 221582a4538fSEric Badger ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1); 221682a4538fSEric Badger ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE); 221782a4538fSEric Badger 221882a4538fSEric Badger /* Continue the child process with a signal. */ 221982a4538fSEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0); 222082a4538fSEric Badger 222182a4538fSEric Badger for (;;) { 222282a4538fSEric Badger /* 222382a4538fSEric Badger * The last wait() should report exit 2, i.e., a normal _exit 222482a4538fSEric Badger * from the signal handler. In the meantime, catch and proceed 222582a4538fSEric Badger * past any syscall stops. 222682a4538fSEric Badger */ 222782a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 222882a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 222982a4538fSEric Badger if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) { 223082a4538fSEric Badger ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1); 223182a4538fSEric Badger ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX)); 223282a4538fSEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 223382a4538fSEric Badger } else { 223482a4538fSEric Badger ATF_REQUIRE(WIFEXITED(status)); 223582a4538fSEric Badger ATF_REQUIRE(WEXITSTATUS(status) == 2); 223682a4538fSEric Badger break; 223782a4538fSEric Badger } 223882a4538fSEric Badger } 223982a4538fSEric Badger 224082a4538fSEric Badger wpid = wait(&status); 224182a4538fSEric Badger ATF_REQUIRE(wpid == -1); 224282a4538fSEric Badger ATF_REQUIRE(errno == ECHILD); 224382a4538fSEric Badger } 224482a4538fSEric Badger 224582a4538fSEric Badger static void 224682a4538fSEric Badger sigusr1_counting_handler(int sig) 224782a4538fSEric Badger { 224882a4538fSEric Badger static int counter = 0; 224982a4538fSEric Badger 225082a4538fSEric Badger CHILD_REQUIRE(sig == SIGUSR1); 225182a4538fSEric Badger counter++; 225282a4538fSEric Badger if (counter == 2) 225382a4538fSEric Badger _exit(2); 225482a4538fSEric Badger } 225582a4538fSEric Badger 225682a4538fSEric Badger /* 225782a4538fSEric Badger * Verify that, when continuing from a stop at system call entry and exit, 225882a4538fSEric Badger * a signal can be requested from both stops, and both will be delivered when 225982a4538fSEric Badger * the system call is complete. 226082a4538fSEric Badger */ 226182a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit); 226282a4538fSEric Badger ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit, tc) 226382a4538fSEric Badger { 226482a4538fSEric Badger struct ptrace_lwpinfo pl; 226582a4538fSEric Badger pid_t fpid, wpid; 226682a4538fSEric Badger int status; 226782a4538fSEric Badger 226882a4538fSEric Badger ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR); 226982a4538fSEric Badger 227082a4538fSEric Badger ATF_REQUIRE((fpid = fork()) != -1); 227182a4538fSEric Badger if (fpid == 0) { 227282a4538fSEric Badger trace_me(); 227382a4538fSEric Badger getpid(); 227482a4538fSEric Badger exit(1); 227582a4538fSEric Badger } 227682a4538fSEric Badger 227782a4538fSEric Badger /* The first wait() should report the stop from SIGSTOP. */ 227882a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 227982a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 228082a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 228182a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 228282a4538fSEric Badger 228382a4538fSEric Badger /* Continue the child ignoring the SIGSTOP and tracing system calls. */ 228482a4538fSEric Badger ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0); 228582a4538fSEric Badger 228682a4538fSEric Badger /* The second wait() should report a system call entry for getpid(). */ 228782a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 228882a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 228982a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 229082a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP); 229182a4538fSEric Badger 229282a4538fSEric Badger ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1); 229382a4538fSEric Badger ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE); 229482a4538fSEric Badger 229582a4538fSEric Badger /* Continue the child process with a signal. */ 229682a4538fSEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0); 229782a4538fSEric Badger 229882a4538fSEric Badger /* The third wait() should report a system call exit for getpid(). */ 229982a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 230082a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 230182a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 230282a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP); 230382a4538fSEric Badger 230482a4538fSEric Badger ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1); 230582a4538fSEric Badger ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX); 230682a4538fSEric Badger 230782a4538fSEric Badger /* Continue the child process with a signal. */ 230882a4538fSEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0); 230982a4538fSEric Badger 231082a4538fSEric Badger for (;;) { 231182a4538fSEric Badger /* 231282a4538fSEric Badger * The last wait() should report exit 2, i.e., a normal _exit 231382a4538fSEric Badger * from the signal handler. In the meantime, catch and proceed 231482a4538fSEric Badger * past any syscall stops. 231582a4538fSEric Badger */ 231682a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 231782a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 231882a4538fSEric Badger if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) { 231982a4538fSEric Badger ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1); 232082a4538fSEric Badger ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX)); 232182a4538fSEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 232282a4538fSEric Badger } else { 232382a4538fSEric Badger ATF_REQUIRE(WIFEXITED(status)); 232482a4538fSEric Badger ATF_REQUIRE(WEXITSTATUS(status) == 2); 232582a4538fSEric Badger break; 232682a4538fSEric Badger } 232782a4538fSEric Badger } 232882a4538fSEric Badger 232982a4538fSEric Badger wpid = wait(&status); 233082a4538fSEric Badger ATF_REQUIRE(wpid == -1); 233182a4538fSEric Badger ATF_REQUIRE(errno == ECHILD); 233282a4538fSEric Badger } 233382a4538fSEric Badger 233482a4538fSEric Badger /* 233582a4538fSEric Badger * Verify that even if the signal queue is full for a child process, 233682a4538fSEric Badger * a PT_CONTINUE with a signal will not result in loss of that signal. 233782a4538fSEric Badger */ 233882a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_full_sigqueue); 233982a4538fSEric Badger ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_full_sigqueue, tc) 234082a4538fSEric Badger { 234182a4538fSEric Badger pid_t fpid, wpid; 234282a4538fSEric Badger int status; 234382a4538fSEric Badger int max_pending_per_proc; 234482a4538fSEric Badger size_t len; 234582a4538fSEric Badger int i; 234682a4538fSEric Badger 234782a4538fSEric Badger ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR); 234882a4538fSEric Badger ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR); 234982a4538fSEric Badger 235082a4538fSEric Badger ATF_REQUIRE((fpid = fork()) != -1); 235182a4538fSEric Badger if (fpid == 0) { 235282a4538fSEric Badger trace_me(); 235382a4538fSEric Badger exit(1); 235482a4538fSEric Badger } 235582a4538fSEric Badger 235682a4538fSEric Badger /* The first wait() should report the stop from SIGSTOP. */ 235782a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 235882a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 235982a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 236082a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 236182a4538fSEric Badger 236282a4538fSEric Badger len = sizeof(max_pending_per_proc); 236382a4538fSEric Badger ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc", 236482a4538fSEric Badger &max_pending_per_proc, &len, NULL, 0) == 0); 236582a4538fSEric Badger 236682a4538fSEric Badger /* Fill the signal queue. */ 236782a4538fSEric Badger for (i = 0; i < max_pending_per_proc; ++i) 236882a4538fSEric Badger ATF_REQUIRE(kill(fpid, SIGUSR2) == 0); 236982a4538fSEric Badger 237082a4538fSEric Badger /* Continue with signal. */ 237182a4538fSEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0); 237282a4538fSEric Badger 237382a4538fSEric Badger for (;;) { 237482a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 237582a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 237682a4538fSEric Badger if (WIFSTOPPED(status)) { 237782a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2); 237882a4538fSEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 237982a4538fSEric Badger } else { 238082a4538fSEric Badger /* 238182a4538fSEric Badger * The last wait() should report normal _exit from the 238282a4538fSEric Badger * SIGUSR1 handler. 238382a4538fSEric Badger */ 238482a4538fSEric Badger ATF_REQUIRE(WIFEXITED(status)); 238582a4538fSEric Badger ATF_REQUIRE(WEXITSTATUS(status) == 2); 238682a4538fSEric Badger break; 238782a4538fSEric Badger } 238882a4538fSEric Badger } 238982a4538fSEric Badger 239082a4538fSEric Badger wpid = wait(&status); 239182a4538fSEric Badger ATF_REQUIRE(wpid == -1); 239282a4538fSEric Badger ATF_REQUIRE(errno == ECHILD); 239382a4538fSEric Badger } 239482a4538fSEric Badger 239582a4538fSEric Badger /* 239682a4538fSEric Badger * Verify that, after stopping due to a signal, that signal can be 239782a4538fSEric Badger * replaced with another signal. 239882a4538fSEric Badger */ 239982a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_change_sig); 240082a4538fSEric Badger ATF_TC_BODY(ptrace__PT_CONTINUE_change_sig, tc) 240182a4538fSEric Badger { 240282a4538fSEric Badger struct ptrace_lwpinfo pl; 240382a4538fSEric Badger pid_t fpid, wpid; 240482a4538fSEric Badger int status; 240582a4538fSEric Badger 240682a4538fSEric Badger ATF_REQUIRE((fpid = fork()) != -1); 240782a4538fSEric Badger if (fpid == 0) { 240882a4538fSEric Badger trace_me(); 240982a4538fSEric Badger sleep(20); 241082a4538fSEric Badger exit(1); 241182a4538fSEric Badger } 241282a4538fSEric Badger 241382a4538fSEric Badger /* The first wait() should report the stop from SIGSTOP. */ 241482a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 241582a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 241682a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 241782a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 241882a4538fSEric Badger 241982a4538fSEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 242082a4538fSEric Badger 242182a4538fSEric Badger /* Send a signal without ptrace. */ 242282a4538fSEric Badger ATF_REQUIRE(kill(fpid, SIGINT) == 0); 242382a4538fSEric Badger 242482a4538fSEric Badger /* The second wait() should report a SIGINT was received. */ 242582a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 242682a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 242782a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 242882a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGINT); 242982a4538fSEric Badger 243082a4538fSEric Badger ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1); 243182a4538fSEric Badger ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI); 243282a4538fSEric Badger ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGINT); 243382a4538fSEric Badger 243482a4538fSEric Badger /* Continue the child process with a different signal. */ 243582a4538fSEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTERM) == 0); 243682a4538fSEric Badger 243782a4538fSEric Badger /* 243882a4538fSEric Badger * The last wait() should report having died due to the new 243982a4538fSEric Badger * signal, SIGTERM. 244082a4538fSEric Badger */ 244182a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 244282a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 244382a4538fSEric Badger ATF_REQUIRE(WIFSIGNALED(status)); 244482a4538fSEric Badger ATF_REQUIRE(WTERMSIG(status) == SIGTERM); 244582a4538fSEric Badger 244682a4538fSEric Badger wpid = wait(&status); 244782a4538fSEric Badger ATF_REQUIRE(wpid == -1); 244882a4538fSEric Badger ATF_REQUIRE(errno == ECHILD); 244982a4538fSEric Badger } 245082a4538fSEric Badger 245182a4538fSEric Badger /* 245282a4538fSEric Badger * Verify that a signal can be passed through to the child even when there 245382a4538fSEric Badger * was no true signal originally. Such cases arise when a SIGTRAP is 245482a4538fSEric Badger * invented for e.g, system call stops. 245582a4538fSEric Badger */ 245682a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry); 245782a4538fSEric Badger ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry, tc) 245882a4538fSEric Badger { 245982a4538fSEric Badger struct ptrace_lwpinfo pl; 246082a4538fSEric Badger pid_t fpid, wpid; 246182a4538fSEric Badger int status; 246282a4538fSEric Badger 246382a4538fSEric Badger ATF_REQUIRE((fpid = fork()) != -1); 246482a4538fSEric Badger if (fpid == 0) { 246582a4538fSEric Badger trace_me(); 246682a4538fSEric Badger getpid(); 246782a4538fSEric Badger exit(1); 246882a4538fSEric Badger } 246982a4538fSEric Badger 247082a4538fSEric Badger /* The first wait() should report the stop from SIGSTOP. */ 247182a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 247282a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 247382a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 247482a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 247582a4538fSEric Badger 247682a4538fSEric Badger /* Continue the child ignoring the SIGSTOP and tracing system calls. */ 247782a4538fSEric Badger ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0); 247882a4538fSEric Badger 247982a4538fSEric Badger /* The second wait() should report a system call entry for getpid(). */ 248082a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 248182a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 248282a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 248382a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP); 248482a4538fSEric Badger 248582a4538fSEric Badger ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1); 248682a4538fSEric Badger ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE); 248782a4538fSEric Badger 248882a4538fSEric Badger /* Continue the child process with a SIGTRAP. */ 248982a4538fSEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTRAP) == 0); 249082a4538fSEric Badger 249182a4538fSEric Badger for (;;) { 249282a4538fSEric Badger /* 249382a4538fSEric Badger * The last wait() should report exit due to SIGTRAP. In the 249482a4538fSEric Badger * meantime, catch and proceed past any syscall stops. 249582a4538fSEric Badger */ 249682a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 249782a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 249882a4538fSEric Badger if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) { 249982a4538fSEric Badger ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1); 250082a4538fSEric Badger ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX)); 250182a4538fSEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 250282a4538fSEric Badger } else { 250382a4538fSEric Badger ATF_REQUIRE(WIFSIGNALED(status)); 250482a4538fSEric Badger ATF_REQUIRE(WTERMSIG(status) == SIGTRAP); 250582a4538fSEric Badger break; 250682a4538fSEric Badger } 250782a4538fSEric Badger } 250882a4538fSEric Badger 250982a4538fSEric Badger wpid = wait(&status); 251082a4538fSEric Badger ATF_REQUIRE(wpid == -1); 251182a4538fSEric Badger ATF_REQUIRE(errno == ECHILD); 251282a4538fSEric Badger 251382a4538fSEric Badger } 251482a4538fSEric Badger 251582a4538fSEric Badger /* 251682a4538fSEric Badger * A mixed bag PT_CONTINUE with signal test. 251782a4538fSEric Badger */ 251882a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_mix); 251982a4538fSEric Badger ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_mix, tc) 252082a4538fSEric Badger { 252182a4538fSEric Badger struct ptrace_lwpinfo pl; 252282a4538fSEric Badger pid_t fpid, wpid; 252382a4538fSEric Badger int status; 252482a4538fSEric Badger 252582a4538fSEric Badger ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR); 252682a4538fSEric Badger 252782a4538fSEric Badger ATF_REQUIRE((fpid = fork()) != -1); 252882a4538fSEric Badger if (fpid == 0) { 252982a4538fSEric Badger trace_me(); 253082a4538fSEric Badger getpid(); 253182a4538fSEric Badger exit(1); 253282a4538fSEric Badger } 253382a4538fSEric Badger 253482a4538fSEric Badger /* The first wait() should report the stop from SIGSTOP. */ 253582a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 253682a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 253782a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 253882a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 253982a4538fSEric Badger 254082a4538fSEric Badger /* Continue the child ignoring the SIGSTOP and tracing system calls. */ 254182a4538fSEric Badger ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0); 254282a4538fSEric Badger 254382a4538fSEric Badger /* The second wait() should report a system call entry for getpid(). */ 254482a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 254582a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 254682a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 254782a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP); 254882a4538fSEric Badger 254982a4538fSEric Badger ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1); 255082a4538fSEric Badger ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE); 255182a4538fSEric Badger 255282a4538fSEric Badger /* Continue with the first SIGUSR1. */ 255382a4538fSEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0); 255482a4538fSEric Badger 255582a4538fSEric Badger /* The next wait() should report a system call exit for getpid(). */ 255682a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 255782a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 255882a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 255982a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP); 256082a4538fSEric Badger 256182a4538fSEric Badger ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1); 256282a4538fSEric Badger ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX); 256382a4538fSEric Badger 256482a4538fSEric Badger /* Send an ABRT without ptrace. */ 256582a4538fSEric Badger ATF_REQUIRE(kill(fpid, SIGABRT) == 0); 256682a4538fSEric Badger 256782a4538fSEric Badger /* Continue normally. */ 256882a4538fSEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 256982a4538fSEric Badger 257082a4538fSEric Badger /* The next wait() should report the SIGABRT. */ 257182a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 257282a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 257382a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 257482a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGABRT); 257582a4538fSEric Badger 257682a4538fSEric Badger ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1); 257782a4538fSEric Badger ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI); 257882a4538fSEric Badger ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT); 257982a4538fSEric Badger 258082a4538fSEric Badger /* Continue, replacing the SIGABRT with another SIGUSR1. */ 258182a4538fSEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0); 258282a4538fSEric Badger 258382a4538fSEric Badger for (;;) { 258482a4538fSEric Badger /* 258582a4538fSEric Badger * The last wait() should report exit 2, i.e., a normal _exit 258682a4538fSEric Badger * from the signal handler. In the meantime, catch and proceed 258782a4538fSEric Badger * past any syscall stops. 258882a4538fSEric Badger */ 258982a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 259082a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 259182a4538fSEric Badger if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) { 259282a4538fSEric Badger ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1); 259382a4538fSEric Badger ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX)); 259482a4538fSEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 259582a4538fSEric Badger } else { 259682a4538fSEric Badger ATF_REQUIRE(WIFEXITED(status)); 259782a4538fSEric Badger ATF_REQUIRE(WEXITSTATUS(status) == 2); 259882a4538fSEric Badger break; 259982a4538fSEric Badger } 260082a4538fSEric Badger } 260182a4538fSEric Badger 260282a4538fSEric Badger wpid = wait(&status); 260382a4538fSEric Badger ATF_REQUIRE(wpid == -1); 260482a4538fSEric Badger ATF_REQUIRE(errno == ECHILD); 260582a4538fSEric Badger 260682a4538fSEric Badger } 260782a4538fSEric Badger 260882a4538fSEric Badger /* 260982a4538fSEric Badger * Verify a signal delivered by ptrace is noticed by kevent(2). 261082a4538fSEric Badger */ 261182a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_kqueue); 261282a4538fSEric Badger ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_kqueue, tc) 261382a4538fSEric Badger { 261482a4538fSEric Badger pid_t fpid, wpid; 261582a4538fSEric Badger int status, kq, nevents; 261682a4538fSEric Badger struct kevent kev; 261782a4538fSEric Badger 261882a4538fSEric Badger ATF_REQUIRE(signal(SIGUSR1, SIG_IGN) != SIG_ERR); 261982a4538fSEric Badger 262082a4538fSEric Badger ATF_REQUIRE((fpid = fork()) != -1); 262182a4538fSEric Badger if (fpid == 0) { 262282a4538fSEric Badger CHILD_REQUIRE((kq = kqueue()) > 0); 262382a4538fSEric Badger EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, 0); 262482a4538fSEric Badger CHILD_REQUIRE(kevent(kq, &kev, 1, NULL, 0, NULL) == 0); 262582a4538fSEric Badger 262682a4538fSEric Badger trace_me(); 262782a4538fSEric Badger 262882a4538fSEric Badger for (;;) { 262982a4538fSEric Badger nevents = kevent(kq, NULL, 0, &kev, 1, NULL); 263082a4538fSEric Badger if (nevents == -1 && errno == EINTR) 263182a4538fSEric Badger continue; 263282a4538fSEric Badger CHILD_REQUIRE(nevents > 0); 263382a4538fSEric Badger CHILD_REQUIRE(kev.filter == EVFILT_SIGNAL); 263482a4538fSEric Badger CHILD_REQUIRE(kev.ident == SIGUSR1); 263582a4538fSEric Badger break; 263682a4538fSEric Badger } 263782a4538fSEric Badger 263882a4538fSEric Badger exit(1); 263982a4538fSEric Badger } 264082a4538fSEric Badger 264182a4538fSEric Badger /* The first wait() should report the stop from SIGSTOP. */ 264282a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 264382a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 264482a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 264582a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 264682a4538fSEric Badger 264782a4538fSEric Badger /* Continue with the SIGUSR1. */ 264882a4538fSEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0); 264982a4538fSEric Badger 265082a4538fSEric Badger /* 265182a4538fSEric Badger * The last wait() should report normal exit with code 1. 265282a4538fSEric Badger */ 265382a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 265482a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 265582a4538fSEric Badger ATF_REQUIRE(WIFEXITED(status)); 265682a4538fSEric Badger ATF_REQUIRE(WEXITSTATUS(status) == 1); 265782a4538fSEric Badger 265882a4538fSEric Badger wpid = wait(&status); 265982a4538fSEric Badger ATF_REQUIRE(wpid == -1); 266082a4538fSEric Badger ATF_REQUIRE(errno == ECHILD); 266182a4538fSEric Badger } 266282a4538fSEric Badger 266382a4538fSEric Badger static sem_t sigusr1_sem; 266482a4538fSEric Badger 266582a4538fSEric Badger static void 266682a4538fSEric Badger sigusr1_sempost_handler(int sig __unused) 266782a4538fSEric Badger { 266882a4538fSEric Badger 266982a4538fSEric Badger CHILD_REQUIRE(sem_post(&sigusr1_sem) == 0); 267082a4538fSEric Badger } 267182a4538fSEric Badger 267282a4538fSEric Badger static void * 267382a4538fSEric Badger signal_thread(void *arg) 267482a4538fSEric Badger { 267582a4538fSEric Badger int err; 267682a4538fSEric Badger sigset_t sigmask; 267782a4538fSEric Badger 267882a4538fSEric Badger pthread_barrier_t *pbarrier = (pthread_barrier_t*)arg; 267982a4538fSEric Badger 268082a4538fSEric Badger /* Wait for this thread to receive a SIGUSR1. */ 268182a4538fSEric Badger do { 268282a4538fSEric Badger err = sem_wait(&sigusr1_sem); 268382a4538fSEric Badger CHILD_REQUIRE(err == 0 || errno == EINTR); 268482a4538fSEric Badger } while (err != 0 && errno == EINTR); 268582a4538fSEric Badger 268682a4538fSEric Badger /* Free our companion thread from the barrier. */ 268782a4538fSEric Badger pthread_barrier_wait(pbarrier); 268882a4538fSEric Badger 268982a4538fSEric Badger /* 269082a4538fSEric Badger * Swap ignore duties; the next SIGUSR1 should go to the 269182a4538fSEric Badger * other thread. 269282a4538fSEric Badger */ 269382a4538fSEric Badger CHILD_REQUIRE(sigemptyset(&sigmask) == 0); 269482a4538fSEric Badger CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0); 269582a4538fSEric Badger CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0); 269682a4538fSEric Badger 269782a4538fSEric Badger /* Sync up threads after swapping signal masks. */ 269882a4538fSEric Badger pthread_barrier_wait(pbarrier); 269982a4538fSEric Badger 270082a4538fSEric Badger /* Wait until our companion has received its SIGUSR1. */ 270182a4538fSEric Badger pthread_barrier_wait(pbarrier); 270282a4538fSEric Badger 270382a4538fSEric Badger return (NULL); 270482a4538fSEric Badger } 270582a4538fSEric Badger 270682a4538fSEric Badger /* 270782a4538fSEric Badger * Verify that if ptrace stops due to a signal but continues with 270882a4538fSEric Badger * a different signal that the new signal is routed to a thread 270982a4538fSEric Badger * that can accept it, and that that thread is awakened by the signal 271082a4538fSEric Badger * in a timely manner. 271182a4538fSEric Badger */ 271282a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_thread_sigmask); 271382a4538fSEric Badger ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_thread_sigmask, tc) 271482a4538fSEric Badger { 271582a4538fSEric Badger pid_t fpid, wpid; 271682a4538fSEric Badger int status, err; 271782a4538fSEric Badger pthread_t t; 271882a4538fSEric Badger sigset_t sigmask; 271982a4538fSEric Badger pthread_barrier_t barrier; 272082a4538fSEric Badger 272182a4538fSEric Badger ATF_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0); 272282a4538fSEric Badger ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0); 272382a4538fSEric Badger ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR); 272482a4538fSEric Badger 272582a4538fSEric Badger ATF_REQUIRE((fpid = fork()) != -1); 272682a4538fSEric Badger if (fpid == 0) { 272782a4538fSEric Badger CHILD_REQUIRE(pthread_create(&t, NULL, signal_thread, (void*)&barrier) == 0); 272882a4538fSEric Badger 272982a4538fSEric Badger /* The other thread should receive the first SIGUSR1. */ 273082a4538fSEric Badger CHILD_REQUIRE(sigemptyset(&sigmask) == 0); 273182a4538fSEric Badger CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0); 273282a4538fSEric Badger CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0); 273382a4538fSEric Badger 273482a4538fSEric Badger trace_me(); 273582a4538fSEric Badger 273682a4538fSEric Badger /* Wait until other thread has received its SIGUSR1. */ 273782a4538fSEric Badger pthread_barrier_wait(&barrier); 273882a4538fSEric Badger 273982a4538fSEric Badger /* 274082a4538fSEric Badger * Swap ignore duties; the next SIGUSR1 should go to this 274182a4538fSEric Badger * thread. 274282a4538fSEric Badger */ 274382a4538fSEric Badger CHILD_REQUIRE(pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL) == 0); 274482a4538fSEric Badger 274582a4538fSEric Badger /* Sync up threads after swapping signal masks. */ 274682a4538fSEric Badger pthread_barrier_wait(&barrier); 274782a4538fSEric Badger 274882a4538fSEric Badger /* 274982a4538fSEric Badger * Sync up with test code; we're ready for the next SIGUSR1 275082a4538fSEric Badger * now. 275182a4538fSEric Badger */ 275282a4538fSEric Badger raise(SIGSTOP); 275382a4538fSEric Badger 275482a4538fSEric Badger /* Wait for this thread to receive a SIGUSR1. */ 275582a4538fSEric Badger do { 275682a4538fSEric Badger err = sem_wait(&sigusr1_sem); 275782a4538fSEric Badger CHILD_REQUIRE(err == 0 || errno == EINTR); 275882a4538fSEric Badger } while (err != 0 && errno == EINTR); 275982a4538fSEric Badger 276082a4538fSEric Badger /* Free the other thread from the barrier. */ 276182a4538fSEric Badger pthread_barrier_wait(&barrier); 276282a4538fSEric Badger 276382a4538fSEric Badger CHILD_REQUIRE(pthread_join(t, NULL) == 0); 276482a4538fSEric Badger 276582a4538fSEric Badger exit(1); 276682a4538fSEric Badger } 276782a4538fSEric Badger 276882a4538fSEric Badger /* The first wait() should report the stop from SIGSTOP. */ 276982a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 277082a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 277182a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 277282a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 277382a4538fSEric Badger 277482a4538fSEric Badger /* Continue the child ignoring the SIGSTOP. */ 277582a4538fSEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 277682a4538fSEric Badger 277782a4538fSEric Badger /* 277882a4538fSEric Badger * Send a signal without ptrace that either thread will accept (USR2, 277982a4538fSEric Badger * in this case). 278082a4538fSEric Badger */ 278182a4538fSEric Badger ATF_REQUIRE(kill(fpid, SIGUSR2) == 0); 278282a4538fSEric Badger 278382a4538fSEric Badger /* The second wait() should report a SIGUSR2 was received. */ 278482a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 278582a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 278682a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 278782a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2); 278882a4538fSEric Badger 278982a4538fSEric Badger /* Continue the child, changing the signal to USR1. */ 279082a4538fSEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0); 279182a4538fSEric Badger 279282a4538fSEric Badger /* The next wait() should report the stop from SIGSTOP. */ 279382a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 279482a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 279582a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 279682a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 279782a4538fSEric Badger 279882a4538fSEric Badger /* Continue the child ignoring the SIGSTOP. */ 279982a4538fSEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 280082a4538fSEric Badger 280182a4538fSEric Badger ATF_REQUIRE(kill(fpid, SIGUSR2) == 0); 280282a4538fSEric Badger 280382a4538fSEric Badger /* The next wait() should report a SIGUSR2 was received. */ 280482a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 280582a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 280682a4538fSEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 280782a4538fSEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2); 280882a4538fSEric Badger 280982a4538fSEric Badger /* Continue the child, changing the signal to USR1. */ 281082a4538fSEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0); 281182a4538fSEric Badger 281282a4538fSEric Badger /* The last wait() should report normal exit with code 1. */ 281382a4538fSEric Badger wpid = waitpid(fpid, &status, 0); 281482a4538fSEric Badger ATF_REQUIRE(wpid == fpid); 281582a4538fSEric Badger ATF_REQUIRE(WIFEXITED(status)); 281682a4538fSEric Badger ATF_REQUIRE(WEXITSTATUS(status) == 1); 281782a4538fSEric Badger 281882a4538fSEric Badger wpid = wait(&status); 281982a4538fSEric Badger ATF_REQUIRE(wpid == -1); 282082a4538fSEric Badger ATF_REQUIRE(errno == ECHILD); 282182a4538fSEric Badger } 282282a4538fSEric Badger 2823b38bd91fSEric Badger static void * 2824b38bd91fSEric Badger raise_sigstop_thread(void *arg __unused) 2825b38bd91fSEric Badger { 2826b38bd91fSEric Badger 2827b38bd91fSEric Badger raise(SIGSTOP); 2828b38bd91fSEric Badger return NULL; 2829b38bd91fSEric Badger } 2830b38bd91fSEric Badger 2831b38bd91fSEric Badger static void * 2832b38bd91fSEric Badger sleep_thread(void *arg __unused) 2833b38bd91fSEric Badger { 2834b38bd91fSEric Badger 2835b38bd91fSEric Badger sleep(60); 2836b38bd91fSEric Badger return NULL; 2837b38bd91fSEric Badger } 2838b38bd91fSEric Badger 2839b38bd91fSEric Badger static void 2840b38bd91fSEric Badger terminate_with_pending_sigstop(bool sigstop_from_main_thread) 2841b38bd91fSEric Badger { 2842b38bd91fSEric Badger pid_t fpid, wpid; 2843b38bd91fSEric Badger int status, i; 2844b38bd91fSEric Badger cpuset_t setmask; 2845b38bd91fSEric Badger cpusetid_t setid; 2846b38bd91fSEric Badger pthread_t t; 2847b38bd91fSEric Badger 2848b38bd91fSEric Badger /* 2849b38bd91fSEric Badger * Become the reaper for this process tree. We need to be able to check 2850b38bd91fSEric Badger * that both child and grandchild have died. 2851b38bd91fSEric Badger */ 2852b38bd91fSEric Badger ATF_REQUIRE(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == 0); 2853b38bd91fSEric Badger 2854b38bd91fSEric Badger fpid = fork(); 2855b38bd91fSEric Badger ATF_REQUIRE(fpid >= 0); 2856b38bd91fSEric Badger if (fpid == 0) { 2857b38bd91fSEric Badger fpid = fork(); 2858b38bd91fSEric Badger CHILD_REQUIRE(fpid >= 0); 2859b38bd91fSEric Badger if (fpid == 0) { 2860b38bd91fSEric Badger trace_me(); 2861b38bd91fSEric Badger 2862b38bd91fSEric Badger /* Pin to CPU 0 to serialize thread execution. */ 2863b38bd91fSEric Badger CPU_ZERO(&setmask); 2864b38bd91fSEric Badger CPU_SET(0, &setmask); 2865b38bd91fSEric Badger CHILD_REQUIRE(cpuset(&setid) == 0); 2866b38bd91fSEric Badger CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET, 2867b38bd91fSEric Badger CPU_WHICH_CPUSET, setid, 2868b38bd91fSEric Badger sizeof(setmask), &setmask) == 0); 2869b38bd91fSEric Badger 2870b38bd91fSEric Badger if (sigstop_from_main_thread) { 2871b38bd91fSEric Badger /* 2872b38bd91fSEric Badger * We expect the SIGKILL sent when our parent 2873b38bd91fSEric Badger * dies to be delivered to the new thread. 2874b38bd91fSEric Badger * Raise the SIGSTOP in this thread so the 2875b38bd91fSEric Badger * threads compete. 2876b38bd91fSEric Badger */ 2877b38bd91fSEric Badger CHILD_REQUIRE(pthread_create(&t, NULL, 2878b38bd91fSEric Badger sleep_thread, NULL) == 0); 2879b38bd91fSEric Badger raise(SIGSTOP); 2880b38bd91fSEric Badger } else { 2881b38bd91fSEric Badger /* 2882b38bd91fSEric Badger * We expect the SIGKILL to be delivered to 2883b38bd91fSEric Badger * this thread. After creating the new thread, 2884b38bd91fSEric Badger * just get off the CPU so the other thread can 2885b38bd91fSEric Badger * raise the SIGSTOP. 2886b38bd91fSEric Badger */ 2887b38bd91fSEric Badger CHILD_REQUIRE(pthread_create(&t, NULL, 2888b38bd91fSEric Badger raise_sigstop_thread, NULL) == 0); 2889b38bd91fSEric Badger sleep(60); 2890b38bd91fSEric Badger } 2891b38bd91fSEric Badger 2892b38bd91fSEric Badger exit(0); 2893b38bd91fSEric Badger } 2894b38bd91fSEric Badger /* First stop is trace_me() immediately after fork. */ 2895b38bd91fSEric Badger wpid = waitpid(fpid, &status, 0); 2896b38bd91fSEric Badger CHILD_REQUIRE(wpid == fpid); 2897b38bd91fSEric Badger CHILD_REQUIRE(WIFSTOPPED(status)); 2898b38bd91fSEric Badger CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP); 2899b38bd91fSEric Badger 2900b38bd91fSEric Badger CHILD_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 2901b38bd91fSEric Badger 2902b38bd91fSEric Badger /* Second stop is from the raise(SIGSTOP). */ 2903b38bd91fSEric Badger wpid = waitpid(fpid, &status, 0); 2904b38bd91fSEric Badger CHILD_REQUIRE(wpid == fpid); 2905b38bd91fSEric Badger CHILD_REQUIRE(WIFSTOPPED(status)); 2906b38bd91fSEric Badger CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP); 2907b38bd91fSEric Badger 2908b38bd91fSEric Badger /* 2909b38bd91fSEric Badger * Terminate tracing process without detaching. Our child 2910b38bd91fSEric Badger * should be killed. 2911b38bd91fSEric Badger */ 2912b38bd91fSEric Badger exit(0); 2913b38bd91fSEric Badger } 2914b38bd91fSEric Badger 2915b38bd91fSEric Badger /* 2916b38bd91fSEric Badger * We should get a normal exit from our immediate child and a SIGKILL 2917b38bd91fSEric Badger * exit from our grandchild. The latter case is the interesting one. 2918b38bd91fSEric Badger * Our grandchild should not have stopped due to the SIGSTOP that was 2919b38bd91fSEric Badger * left dangling when its parent died. 2920b38bd91fSEric Badger */ 2921b38bd91fSEric Badger for (i = 0; i < 2; ++i) { 2922b38bd91fSEric Badger wpid = wait(&status); 2923b38bd91fSEric Badger if (wpid == fpid) 2924b38bd91fSEric Badger ATF_REQUIRE(WIFEXITED(status) && 2925b38bd91fSEric Badger WEXITSTATUS(status) == 0); 2926b38bd91fSEric Badger else 2927b38bd91fSEric Badger ATF_REQUIRE(WIFSIGNALED(status) && 2928b38bd91fSEric Badger WTERMSIG(status) == SIGKILL); 2929b38bd91fSEric Badger } 2930b38bd91fSEric Badger } 2931b38bd91fSEric Badger 2932b38bd91fSEric Badger /* 2933b38bd91fSEric Badger * These two tests ensure that if the tracing process exits without detaching 2934b38bd91fSEric Badger * just after the child received a SIGSTOP, the child is cleanly killed and 2935b38bd91fSEric Badger * doesn't go to sleep due to the SIGSTOP. The parent's death will send a 2936b38bd91fSEric Badger * SIGKILL to the child. If the SIGKILL and the SIGSTOP are handled by 2937b38bd91fSEric Badger * different threads, the SIGKILL must win. There are two variants of this 2938b38bd91fSEric Badger * test, designed to catch the case where the SIGKILL is delivered to the 2939b38bd91fSEric Badger * younger thread (the first test) and the case where the SIGKILL is delivered 2940b38bd91fSEric Badger * to the older thread (the second test). This behavior has changed in the 2941b38bd91fSEric Badger * past, so make no assumption. 2942b38bd91fSEric Badger */ 2943b38bd91fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__parent_terminate_with_pending_sigstop1); 2944b38bd91fSEric Badger ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop1, tc) 2945b38bd91fSEric Badger { 2946b38bd91fSEric Badger 2947b38bd91fSEric Badger terminate_with_pending_sigstop(true); 2948b38bd91fSEric Badger } 2949b38bd91fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__parent_terminate_with_pending_sigstop2); 2950b38bd91fSEric Badger ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop2, tc) 2951b38bd91fSEric Badger { 2952b38bd91fSEric Badger 2953b38bd91fSEric Badger terminate_with_pending_sigstop(false); 2954b38bd91fSEric Badger } 2955b38bd91fSEric Badger 2956b4d33259SEric Badger /* 2957b4d33259SEric Badger * Verify that after ptrace() discards a SIGKILL signal, the event mask 2958b4d33259SEric Badger * is not modified. 2959b4d33259SEric Badger */ 2960b4d33259SEric Badger ATF_TC_WITHOUT_HEAD(ptrace__event_mask_sigkill_discard); 2961b4d33259SEric Badger ATF_TC_BODY(ptrace__event_mask_sigkill_discard, tc) 2962b4d33259SEric Badger { 2963b4d33259SEric Badger struct ptrace_lwpinfo pl; 2964b4d33259SEric Badger pid_t fpid, wpid; 2965b4d33259SEric Badger int status, event_mask, new_event_mask; 2966b4d33259SEric Badger 2967b4d33259SEric Badger ATF_REQUIRE((fpid = fork()) != -1); 2968b4d33259SEric Badger if (fpid == 0) { 2969b4d33259SEric Badger trace_me(); 2970b4d33259SEric Badger raise(SIGSTOP); 2971b4d33259SEric Badger exit(0); 2972b4d33259SEric Badger } 2973b4d33259SEric Badger 2974b4d33259SEric Badger /* The first wait() should report the stop from trace_me(). */ 2975b4d33259SEric Badger wpid = waitpid(fpid, &status, 0); 2976b4d33259SEric Badger ATF_REQUIRE(wpid == fpid); 2977b4d33259SEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 2978b4d33259SEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 2979b4d33259SEric Badger 2980b4d33259SEric Badger /* Set several unobtrusive event bits. */ 2981b4d33259SEric Badger event_mask = PTRACE_EXEC | PTRACE_FORK | PTRACE_LWP; 2982b4d33259SEric Badger ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, wpid, (caddr_t)&event_mask, 2983b4d33259SEric Badger sizeof(event_mask)) == 0); 2984b4d33259SEric Badger 2985b4d33259SEric Badger /* Send a SIGKILL without using ptrace. */ 2986b4d33259SEric Badger ATF_REQUIRE(kill(fpid, SIGKILL) == 0); 2987b4d33259SEric Badger 2988b4d33259SEric Badger /* Continue the child ignoring the SIGSTOP. */ 2989b4d33259SEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 2990b4d33259SEric Badger 2991b4d33259SEric Badger /* The next stop should be due to the SIGKILL. */ 2992b4d33259SEric Badger wpid = waitpid(fpid, &status, 0); 2993b4d33259SEric Badger ATF_REQUIRE(wpid == fpid); 2994b4d33259SEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 2995b4d33259SEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGKILL); 2996b4d33259SEric Badger 2997b4d33259SEric Badger ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1); 2998b4d33259SEric Badger ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI); 2999b4d33259SEric Badger ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGKILL); 3000b4d33259SEric Badger 3001b4d33259SEric Badger /* Continue the child ignoring the SIGKILL. */ 3002b4d33259SEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 3003b4d33259SEric Badger 3004b4d33259SEric Badger /* The next wait() should report the stop from SIGSTOP. */ 3005b4d33259SEric Badger wpid = waitpid(fpid, &status, 0); 3006b4d33259SEric Badger ATF_REQUIRE(wpid == fpid); 3007b4d33259SEric Badger ATF_REQUIRE(WIFSTOPPED(status)); 3008b4d33259SEric Badger ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 3009b4d33259SEric Badger 3010b4d33259SEric Badger /* Check the current event mask. It should not have changed. */ 3011b4d33259SEric Badger new_event_mask = 0; 3012b4d33259SEric Badger ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, wpid, (caddr_t)&new_event_mask, 3013b4d33259SEric Badger sizeof(new_event_mask)) == 0); 3014b4d33259SEric Badger ATF_REQUIRE(event_mask == new_event_mask); 3015b4d33259SEric Badger 3016b4d33259SEric Badger /* Continue the child to let it exit. */ 3017b4d33259SEric Badger ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0); 3018b4d33259SEric Badger 3019b4d33259SEric Badger /* The last event should be for the child process's exit. */ 3020b4d33259SEric Badger wpid = waitpid(fpid, &status, 0); 3021b4d33259SEric Badger ATF_REQUIRE(WIFEXITED(status)); 3022b4d33259SEric Badger ATF_REQUIRE(WEXITSTATUS(status) == 0); 3023b4d33259SEric Badger 3024b4d33259SEric Badger wpid = wait(&status); 3025b4d33259SEric Badger ATF_REQUIRE(wpid == -1); 3026b4d33259SEric Badger ATF_REQUIRE(errno == ECHILD); 3027b4d33259SEric Badger } 3028b4d33259SEric Badger 3029*d74da94cSMark Johnston static void * 3030*d74da94cSMark Johnston flock_thread(void *arg) 3031*d74da94cSMark Johnston { 3032*d74da94cSMark Johnston int fd; 3033*d74da94cSMark Johnston 3034*d74da94cSMark Johnston fd = *(int *)arg; 3035*d74da94cSMark Johnston (void)flock(fd, LOCK_EX); 3036*d74da94cSMark Johnston (void)flock(fd, LOCK_UN); 3037*d74da94cSMark Johnston return (NULL); 3038*d74da94cSMark Johnston } 3039*d74da94cSMark Johnston 3040*d74da94cSMark Johnston /* 3041*d74da94cSMark Johnston * Verify that PT_ATTACH will suspend threads sleeping in an SBDRY section. 3042*d74da94cSMark Johnston * We rely on the fact that the lockf implementation sets SBDRY before blocking 3043*d74da94cSMark Johnston * on a lock. This is a regression test for r318191. 3044*d74da94cSMark Johnston */ 3045*d74da94cSMark Johnston ATF_TC_WITHOUT_HEAD(ptrace__PT_ATTACH_with_SBDRY_thread); 3046*d74da94cSMark Johnston ATF_TC_BODY(ptrace__PT_ATTACH_with_SBDRY_thread, tc) 3047*d74da94cSMark Johnston { 3048*d74da94cSMark Johnston pthread_barrier_t barrier; 3049*d74da94cSMark Johnston pthread_barrierattr_t battr; 3050*d74da94cSMark Johnston char tmpfile[64]; 3051*d74da94cSMark Johnston pid_t child, wpid; 3052*d74da94cSMark Johnston int error, fd, i, status; 3053*d74da94cSMark Johnston 3054*d74da94cSMark Johnston ATF_REQUIRE(pthread_barrierattr_init(&battr) == 0); 3055*d74da94cSMark Johnston ATF_REQUIRE(pthread_barrierattr_setpshared(&battr, 3056*d74da94cSMark Johnston PTHREAD_PROCESS_SHARED) == 0); 3057*d74da94cSMark Johnston ATF_REQUIRE(pthread_barrier_init(&barrier, &battr, 2) == 0); 3058*d74da94cSMark Johnston 3059*d74da94cSMark Johnston (void)snprintf(tmpfile, sizeof(tmpfile), "./ptrace.XXXXXX"); 3060*d74da94cSMark Johnston fd = mkstemp(tmpfile); 3061*d74da94cSMark Johnston ATF_REQUIRE(fd >= 0); 3062*d74da94cSMark Johnston 3063*d74da94cSMark Johnston ATF_REQUIRE((child = fork()) != -1); 3064*d74da94cSMark Johnston if (child == 0) { 3065*d74da94cSMark Johnston pthread_t t[2]; 3066*d74da94cSMark Johnston int error, cfd; 3067*d74da94cSMark Johnston 3068*d74da94cSMark Johnston error = pthread_barrier_wait(&barrier); 3069*d74da94cSMark Johnston if (error != 0 && error != PTHREAD_BARRIER_SERIAL_THREAD) 3070*d74da94cSMark Johnston _exit(1); 3071*d74da94cSMark Johnston 3072*d74da94cSMark Johnston cfd = open(tmpfile, O_RDONLY); 3073*d74da94cSMark Johnston if (cfd < 0) 3074*d74da94cSMark Johnston _exit(1); 3075*d74da94cSMark Johnston 3076*d74da94cSMark Johnston /* 3077*d74da94cSMark Johnston * We want at least two threads blocked on the file lock since 3078*d74da94cSMark Johnston * the SIGSTOP from PT_ATTACH may kick one of them out of 3079*d74da94cSMark Johnston * sleep. 3080*d74da94cSMark Johnston */ 3081*d74da94cSMark Johnston if (pthread_create(&t[0], NULL, flock_thread, &cfd) != 0) 3082*d74da94cSMark Johnston _exit(1); 3083*d74da94cSMark Johnston if (pthread_create(&t[1], NULL, flock_thread, &cfd) != 0) 3084*d74da94cSMark Johnston _exit(1); 3085*d74da94cSMark Johnston if (pthread_join(t[0], NULL) != 0) 3086*d74da94cSMark Johnston _exit(1); 3087*d74da94cSMark Johnston if (pthread_join(t[1], NULL) != 0) 3088*d74da94cSMark Johnston _exit(1); 3089*d74da94cSMark Johnston _exit(0); 3090*d74da94cSMark Johnston } 3091*d74da94cSMark Johnston 3092*d74da94cSMark Johnston ATF_REQUIRE(flock(fd, LOCK_EX) == 0); 3093*d74da94cSMark Johnston 3094*d74da94cSMark Johnston error = pthread_barrier_wait(&barrier); 3095*d74da94cSMark Johnston ATF_REQUIRE(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD); 3096*d74da94cSMark Johnston 3097*d74da94cSMark Johnston /* 3098*d74da94cSMark Johnston * Give the child some time to block. Is there a better way to do this? 3099*d74da94cSMark Johnston */ 3100*d74da94cSMark Johnston sleep(1); 3101*d74da94cSMark Johnston 3102*d74da94cSMark Johnston /* 3103*d74da94cSMark Johnston * Attach and give the child 3 seconds to stop. 3104*d74da94cSMark Johnston */ 3105*d74da94cSMark Johnston ATF_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) == 0); 3106*d74da94cSMark Johnston for (i = 0; i < 3; i++) { 3107*d74da94cSMark Johnston wpid = waitpid(child, &status, WNOHANG); 3108*d74da94cSMark Johnston if (wpid == child && WIFSTOPPED(status) && 3109*d74da94cSMark Johnston WSTOPSIG(status) == SIGSTOP) 3110*d74da94cSMark Johnston break; 3111*d74da94cSMark Johnston sleep(1); 3112*d74da94cSMark Johnston } 3113*d74da94cSMark Johnston ATF_REQUIRE_MSG(i < 3, "failed to stop child process after PT_ATTACH"); 3114*d74da94cSMark Johnston 3115*d74da94cSMark Johnston ATF_REQUIRE(ptrace(PT_DETACH, child, NULL, 0) == 0); 3116*d74da94cSMark Johnston 3117*d74da94cSMark Johnston ATF_REQUIRE(flock(fd, LOCK_UN) == 0); 3118*d74da94cSMark Johnston ATF_REQUIRE(unlink(tmpfile) == 0); 3119*d74da94cSMark Johnston ATF_REQUIRE(close(fd) == 0); 3120*d74da94cSMark Johnston } 3121*d74da94cSMark Johnston 3122c209e3e2SJohn Baldwin ATF_TP_ADD_TCS(tp) 3123c209e3e2SJohn Baldwin { 3124c209e3e2SJohn Baldwin 3125c209e3e2SJohn Baldwin ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_trace_me); 3126c209e3e2SJohn Baldwin ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_attach); 312757c74f5bSJohn Baldwin ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_child_debugger); 312857c74f5bSJohn Baldwin ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_unrelated_debugger); 312998685dc8SJohn Baldwin ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached); 313098685dc8SJohn Baldwin ATF_TP_ADD_TC(tp, ptrace__follow_fork_child_detached); 313198685dc8SJohn Baldwin ATF_TP_ADD_TC(tp, ptrace__follow_fork_parent_detached); 313298685dc8SJohn Baldwin ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached_unrelated_debugger); 313398685dc8SJohn Baldwin ATF_TP_ADD_TC(tp, 313498685dc8SJohn Baldwin ptrace__follow_fork_child_detached_unrelated_debugger); 313598685dc8SJohn Baldwin ATF_TP_ADD_TC(tp, 313698685dc8SJohn Baldwin ptrace__follow_fork_parent_detached_unrelated_debugger); 3137368b2b1cSJohn Baldwin ATF_TP_ADD_TC(tp, ptrace__getppid); 3138189ac973SJohn Baldwin ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_fork); 3139189ac973SJohn Baldwin ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_vfork); 3140189ac973SJohn Baldwin ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_thread); 31415fcfab6eSJohn Baldwin ATF_TP_ADD_TC(tp, ptrace__lwp_events); 31425fcfab6eSJohn Baldwin ATF_TP_ADD_TC(tp, ptrace__lwp_events_exec); 31433340c45bSJohn Baldwin ATF_TP_ADD_TC(tp, ptrace__siginfo); 31448d570f64SJohn Baldwin ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_disable); 31458d570f64SJohn Baldwin ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_enable); 31468d570f64SJohn Baldwin ATF_TP_ADD_TC(tp, ptrace__event_mask); 3147fc4f075aSJohn Baldwin ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork); 3148fc4f075aSJohn Baldwin ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork_follow); 3149e2ebfbbfSEric Badger #if defined(__amd64__) || defined(__i386__) || defined(__sparc64__) 315082a4538fSEric Badger ATF_TP_ADD_TC(tp, ptrace__PT_KILL_breakpoint); 3151e2ebfbbfSEric Badger #endif 315282a4538fSEric Badger ATF_TP_ADD_TC(tp, ptrace__PT_KILL_system_call); 315382a4538fSEric Badger ATF_TP_ADD_TC(tp, ptrace__PT_KILL_threads); 315482a4538fSEric Badger ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_signal); 315582a4538fSEric Badger ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_stop); 315682a4538fSEric Badger ATF_TP_ADD_TC(tp, ptrace__PT_KILL_with_signal_full_sigqueue); 315782a4538fSEric Badger ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_system_call_entry); 315882a4538fSEric Badger ATF_TP_ADD_TC(tp, 315982a4538fSEric Badger ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit); 316082a4538fSEric Badger ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_full_sigqueue); 316182a4538fSEric Badger ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_change_sig); 316282a4538fSEric Badger ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigtrap_system_call_entry); 316382a4538fSEric Badger ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_mix); 316482a4538fSEric Badger ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_kqueue); 316582a4538fSEric Badger ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_thread_sigmask); 3166b38bd91fSEric Badger ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop1); 3167b38bd91fSEric Badger ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop2); 3168b4d33259SEric Badger ATF_TP_ADD_TC(tp, ptrace__event_mask_sigkill_discard); 3169*d74da94cSMark Johnston ATF_TP_ADD_TC(tp, ptrace__PT_ATTACH_with_SBDRY_thread); 3170c209e3e2SJohn Baldwin 3171c209e3e2SJohn Baldwin return (atf_no_error()); 3172c209e3e2SJohn Baldwin } 3173