xref: /freebsd/tests/sys/kern/ptrace_test.c (revision e2ebfbbf385bb12d172a21926d093a91a48f409d)
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>
3382a4538fSEric Badger #include <sys/time.h>
34c209e3e2SJohn Baldwin #include <sys/ptrace.h>
35189ac973SJohn Baldwin #include <sys/syscall.h>
3657c74f5bSJohn Baldwin #include <sys/sysctl.h>
3757c74f5bSJohn Baldwin #include <sys/user.h>
38c209e3e2SJohn Baldwin #include <sys/wait.h>
39c209e3e2SJohn Baldwin #include <errno.h>
409e0d1159SEric Badger #include <machine/cpufunc.h>
41189ac973SJohn Baldwin #include <pthread.h>
4282a4538fSEric Badger #include <semaphore.h>
43c209e3e2SJohn Baldwin #include <signal.h>
44dfa8ba12SJohn Baldwin #include <stdio.h>
45c209e3e2SJohn Baldwin #include <stdlib.h>
46c209e3e2SJohn Baldwin #include <unistd.h>
47c209e3e2SJohn Baldwin #include <atf-c.h>
48c209e3e2SJohn Baldwin 
49c209e3e2SJohn Baldwin /*
50dfa8ba12SJohn Baldwin  * A variant of ATF_REQUIRE that is suitable for use in child
51dfa8ba12SJohn Baldwin  * processes.  This only works if the parent process is tripped up by
52dfa8ba12SJohn Baldwin  * the early exit and fails some requirement itself.
53dfa8ba12SJohn Baldwin  */
54dfa8ba12SJohn Baldwin #define	CHILD_REQUIRE(exp) do {						\
55dfa8ba12SJohn Baldwin 		if (!(exp))						\
56dfa8ba12SJohn Baldwin 			child_fail_require(__FILE__, __LINE__,		\
57dfa8ba12SJohn Baldwin 			    #exp " not met");				\
58dfa8ba12SJohn Baldwin 	} while (0)
59dfa8ba12SJohn Baldwin 
6098685dc8SJohn Baldwin static __dead2 void
61dfa8ba12SJohn Baldwin child_fail_require(const char *file, int line, const char *str)
62dfa8ba12SJohn Baldwin {
63dfa8ba12SJohn Baldwin 	char buf[128];
64dfa8ba12SJohn Baldwin 
65dfa8ba12SJohn Baldwin 	snprintf(buf, sizeof(buf), "%s:%d: %s\n", file, line, str);
66dfa8ba12SJohn Baldwin 	write(2, buf, strlen(buf));
67dfa8ba12SJohn Baldwin 	_exit(32);
68dfa8ba12SJohn Baldwin }
69dfa8ba12SJohn Baldwin 
7098685dc8SJohn Baldwin static void
7198685dc8SJohn Baldwin trace_me(void)
7298685dc8SJohn Baldwin {
7398685dc8SJohn Baldwin 
7498685dc8SJohn Baldwin 	/* Attach the parent process as a tracer of this process. */
7598685dc8SJohn Baldwin 	CHILD_REQUIRE(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
7698685dc8SJohn Baldwin 
7798685dc8SJohn Baldwin 	/* Trigger a stop. */
7898685dc8SJohn Baldwin 	raise(SIGSTOP);
7998685dc8SJohn Baldwin }
8098685dc8SJohn Baldwin 
8198685dc8SJohn Baldwin static void
8298685dc8SJohn Baldwin attach_child(pid_t pid)
8398685dc8SJohn Baldwin {
8498685dc8SJohn Baldwin 	pid_t wpid;
8598685dc8SJohn Baldwin 	int status;
8698685dc8SJohn Baldwin 
8798685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_ATTACH, pid, NULL, 0) == 0);
8898685dc8SJohn Baldwin 
8998685dc8SJohn Baldwin 	wpid = waitpid(pid, &status, 0);
9098685dc8SJohn Baldwin 	ATF_REQUIRE(wpid == pid);
9198685dc8SJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
9298685dc8SJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
9398685dc8SJohn Baldwin }
9498685dc8SJohn Baldwin 
9598685dc8SJohn Baldwin static void
9698685dc8SJohn Baldwin wait_for_zombie(pid_t pid)
9798685dc8SJohn Baldwin {
9898685dc8SJohn Baldwin 
9998685dc8SJohn Baldwin 	/*
10098685dc8SJohn Baldwin 	 * Wait for a process to exit.  This is kind of gross, but
10198685dc8SJohn Baldwin 	 * there is not a better way.
10298685dc8SJohn Baldwin 	 */
10398685dc8SJohn Baldwin 	for (;;) {
10498685dc8SJohn Baldwin 		struct kinfo_proc kp;
10598685dc8SJohn Baldwin 		size_t len;
10698685dc8SJohn Baldwin 		int mib[4];
10798685dc8SJohn Baldwin 
10898685dc8SJohn Baldwin 		mib[0] = CTL_KERN;
10998685dc8SJohn Baldwin 		mib[1] = KERN_PROC;
11098685dc8SJohn Baldwin 		mib[2] = KERN_PROC_PID;
11198685dc8SJohn Baldwin 		mib[3] = pid;
11298685dc8SJohn Baldwin 		len = sizeof(kp);
11398685dc8SJohn Baldwin 		if (sysctl(mib, nitems(mib), &kp, &len, NULL, 0) == -1) {
11498685dc8SJohn Baldwin 			/* The KERN_PROC_PID sysctl fails for zombies. */
11598685dc8SJohn Baldwin 			ATF_REQUIRE(errno == ESRCH);
11698685dc8SJohn Baldwin 			break;
11798685dc8SJohn Baldwin 		}
11898685dc8SJohn Baldwin 		usleep(5000);
11998685dc8SJohn Baldwin 	}
12098685dc8SJohn Baldwin }
12198685dc8SJohn Baldwin 
122dfa8ba12SJohn Baldwin /*
123c209e3e2SJohn Baldwin  * Verify that a parent debugger process "sees" the exit of a debugged
124c209e3e2SJohn Baldwin  * process exactly once when attached via PT_TRACE_ME.
125c209e3e2SJohn Baldwin  */
126c209e3e2SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_trace_me);
127c209e3e2SJohn Baldwin ATF_TC_BODY(ptrace__parent_wait_after_trace_me, tc)
128c209e3e2SJohn Baldwin {
129c209e3e2SJohn Baldwin 	pid_t child, wpid;
130c209e3e2SJohn Baldwin 	int status;
131c209e3e2SJohn Baldwin 
132c209e3e2SJohn Baldwin 	ATF_REQUIRE((child = fork()) != -1);
133c209e3e2SJohn Baldwin 	if (child == 0) {
134c209e3e2SJohn Baldwin 		/* Child process. */
13598685dc8SJohn Baldwin 		trace_me();
136c209e3e2SJohn Baldwin 
137b98cb919SJohn Baldwin 		_exit(1);
138c209e3e2SJohn Baldwin 	}
139c209e3e2SJohn Baldwin 
140c209e3e2SJohn Baldwin 	/* Parent process. */
141c209e3e2SJohn Baldwin 
142c209e3e2SJohn Baldwin 	/* The first wait() should report the stop from SIGSTOP. */
143c209e3e2SJohn Baldwin 	wpid = waitpid(child, &status, 0);
144c209e3e2SJohn Baldwin 	ATF_REQUIRE(wpid == child);
145c209e3e2SJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
146c209e3e2SJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
147c209e3e2SJohn Baldwin 
148c209e3e2SJohn Baldwin 	/* Continue the child ignoring the SIGSTOP. */
149c209e3e2SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
150c209e3e2SJohn Baldwin 
151c209e3e2SJohn Baldwin 	/* The second wait() should report the exit status. */
152c209e3e2SJohn Baldwin 	wpid = waitpid(child, &status, 0);
153c209e3e2SJohn Baldwin 	ATF_REQUIRE(wpid == child);
154c209e3e2SJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
155c209e3e2SJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
156c209e3e2SJohn Baldwin 
157c209e3e2SJohn Baldwin 	/* The child should no longer exist. */
158c209e3e2SJohn Baldwin 	wpid = waitpid(child, &status, 0);
159c209e3e2SJohn Baldwin 	ATF_REQUIRE(wpid == -1);
160c209e3e2SJohn Baldwin 	ATF_REQUIRE(errno == ECHILD);
161c209e3e2SJohn Baldwin }
162c209e3e2SJohn Baldwin 
163c209e3e2SJohn Baldwin /*
164c209e3e2SJohn Baldwin  * Verify that a parent debugger process "sees" the exit of a debugged
165c209e3e2SJohn Baldwin  * process exactly once when attached via PT_ATTACH.
166c209e3e2SJohn Baldwin  */
167c209e3e2SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_attach);
168c209e3e2SJohn Baldwin ATF_TC_BODY(ptrace__parent_wait_after_attach, tc)
169c209e3e2SJohn Baldwin {
170c209e3e2SJohn Baldwin 	pid_t child, wpid;
171c209e3e2SJohn Baldwin 	int cpipe[2], status;
172c209e3e2SJohn Baldwin 	char c;
173c209e3e2SJohn Baldwin 
174c209e3e2SJohn Baldwin 	ATF_REQUIRE(pipe(cpipe) == 0);
175c209e3e2SJohn Baldwin 	ATF_REQUIRE((child = fork()) != -1);
176c209e3e2SJohn Baldwin 	if (child == 0) {
177c209e3e2SJohn Baldwin 		/* Child process. */
178c209e3e2SJohn Baldwin 		close(cpipe[0]);
179c209e3e2SJohn Baldwin 
180c209e3e2SJohn Baldwin 		/* Wait for the parent to attach. */
181dfa8ba12SJohn Baldwin 		CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == 0);
182c209e3e2SJohn Baldwin 
183b98cb919SJohn Baldwin 		_exit(1);
184c209e3e2SJohn Baldwin 	}
185c209e3e2SJohn Baldwin 	close(cpipe[1]);
186c209e3e2SJohn Baldwin 
187c209e3e2SJohn Baldwin 	/* Parent process. */
188c209e3e2SJohn Baldwin 
189c209e3e2SJohn Baldwin 	/* Attach to the child process. */
19098685dc8SJohn Baldwin 	attach_child(child);
191c209e3e2SJohn Baldwin 
192c209e3e2SJohn Baldwin 	/* Continue the child ignoring the SIGSTOP. */
193c209e3e2SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
194c209e3e2SJohn Baldwin 
195c209e3e2SJohn Baldwin 	/* Signal the child to exit. */
196c209e3e2SJohn Baldwin 	close(cpipe[0]);
197c209e3e2SJohn Baldwin 
198c209e3e2SJohn Baldwin 	/* The second wait() should report the exit status. */
199c209e3e2SJohn Baldwin 	wpid = waitpid(child, &status, 0);
200c209e3e2SJohn Baldwin 	ATF_REQUIRE(wpid == child);
201c209e3e2SJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
202c209e3e2SJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
203c209e3e2SJohn Baldwin 
204c209e3e2SJohn Baldwin 	/* The child should no longer exist. */
205c209e3e2SJohn Baldwin 	wpid = waitpid(child, &status, 0);
206c209e3e2SJohn Baldwin 	ATF_REQUIRE(wpid == -1);
207c209e3e2SJohn Baldwin 	ATF_REQUIRE(errno == ECHILD);
208c209e3e2SJohn Baldwin }
209c209e3e2SJohn Baldwin 
21057c74f5bSJohn Baldwin /*
21157c74f5bSJohn Baldwin  * Verify that a parent process "sees" the exit of a debugged process only
21257c74f5bSJohn Baldwin  * after the debugger has seen it.
21357c74f5bSJohn Baldwin  */
21457c74f5bSJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_child_debugger);
21557c74f5bSJohn Baldwin ATF_TC_BODY(ptrace__parent_sees_exit_after_child_debugger, tc)
21657c74f5bSJohn Baldwin {
21757c74f5bSJohn Baldwin 	pid_t child, debugger, wpid;
21857c74f5bSJohn Baldwin 	int cpipe[2], dpipe[2], status;
21957c74f5bSJohn Baldwin 	char c;
22057c74f5bSJohn Baldwin 
22157c74f5bSJohn Baldwin 	ATF_REQUIRE(pipe(cpipe) == 0);
22257c74f5bSJohn Baldwin 	ATF_REQUIRE((child = fork()) != -1);
22357c74f5bSJohn Baldwin 
22457c74f5bSJohn Baldwin 	if (child == 0) {
22557c74f5bSJohn Baldwin 		/* Child process. */
22657c74f5bSJohn Baldwin 		close(cpipe[0]);
22757c74f5bSJohn Baldwin 
22857c74f5bSJohn Baldwin 		/* Wait for parent to be ready. */
229dfa8ba12SJohn Baldwin 		CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
23057c74f5bSJohn Baldwin 
231b98cb919SJohn Baldwin 		_exit(1);
23257c74f5bSJohn Baldwin 	}
23357c74f5bSJohn Baldwin 	close(cpipe[1]);
23457c74f5bSJohn Baldwin 
23557c74f5bSJohn Baldwin 	ATF_REQUIRE(pipe(dpipe) == 0);
23657c74f5bSJohn Baldwin 	ATF_REQUIRE((debugger = fork()) != -1);
23757c74f5bSJohn Baldwin 
23857c74f5bSJohn Baldwin 	if (debugger == 0) {
23957c74f5bSJohn Baldwin 		/* Debugger process. */
24057c74f5bSJohn Baldwin 		close(dpipe[0]);
24157c74f5bSJohn Baldwin 
242dfa8ba12SJohn Baldwin 		CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
24357c74f5bSJohn Baldwin 
24457c74f5bSJohn Baldwin 		wpid = waitpid(child, &status, 0);
245dfa8ba12SJohn Baldwin 		CHILD_REQUIRE(wpid == child);
246dfa8ba12SJohn Baldwin 		CHILD_REQUIRE(WIFSTOPPED(status));
247dfa8ba12SJohn Baldwin 		CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
24857c74f5bSJohn Baldwin 
249dfa8ba12SJohn Baldwin 		CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
25057c74f5bSJohn Baldwin 
25157c74f5bSJohn Baldwin 		/* Signal parent that debugger is attached. */
252dfa8ba12SJohn Baldwin 		CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
25357c74f5bSJohn Baldwin 
25457c74f5bSJohn Baldwin 		/* Wait for parent's failed wait. */
255dfa8ba12SJohn Baldwin 		CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == 0);
25657c74f5bSJohn Baldwin 
25757c74f5bSJohn Baldwin 		wpid = waitpid(child, &status, 0);
258dfa8ba12SJohn Baldwin 		CHILD_REQUIRE(wpid == child);
259dfa8ba12SJohn Baldwin 		CHILD_REQUIRE(WIFEXITED(status));
260dfa8ba12SJohn Baldwin 		CHILD_REQUIRE(WEXITSTATUS(status) == 1);
26157c74f5bSJohn Baldwin 
262b98cb919SJohn Baldwin 		_exit(0);
26357c74f5bSJohn Baldwin 	}
26457c74f5bSJohn Baldwin 	close(dpipe[1]);
26557c74f5bSJohn Baldwin 
26657c74f5bSJohn Baldwin 	/* Parent process. */
26757c74f5bSJohn Baldwin 
26857c74f5bSJohn Baldwin 	/* Wait for the debugger to attach to the child. */
26957c74f5bSJohn Baldwin 	ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
27057c74f5bSJohn Baldwin 
27157c74f5bSJohn Baldwin 	/* Release the child. */
27257c74f5bSJohn Baldwin 	ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
27357c74f5bSJohn Baldwin 	ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
27457c74f5bSJohn Baldwin 	close(cpipe[0]);
27557c74f5bSJohn Baldwin 
27698685dc8SJohn Baldwin 	wait_for_zombie(child);
27757c74f5bSJohn Baldwin 
27857c74f5bSJohn Baldwin 	/*
2792f021998SJohn Baldwin 	 * This wait should return a pid of 0 to indicate no status to
2802f021998SJohn Baldwin 	 * report.  The parent should see the child as non-exited
2812f021998SJohn Baldwin 	 * until the debugger sees the exit.
28257c74f5bSJohn Baldwin 	 */
28357c74f5bSJohn Baldwin 	wpid = waitpid(child, &status, WNOHANG);
28457c74f5bSJohn Baldwin 	ATF_REQUIRE(wpid == 0);
28557c74f5bSJohn Baldwin 
28657c74f5bSJohn Baldwin 	/* Signal the debugger to wait for the child. */
28757c74f5bSJohn Baldwin 	close(dpipe[0]);
28857c74f5bSJohn Baldwin 
28957c74f5bSJohn Baldwin 	/* Wait for the debugger. */
29057c74f5bSJohn Baldwin 	wpid = waitpid(debugger, &status, 0);
29157c74f5bSJohn Baldwin 	ATF_REQUIRE(wpid == debugger);
29257c74f5bSJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
29357c74f5bSJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 0);
29457c74f5bSJohn Baldwin 
29557c74f5bSJohn Baldwin 	/* The child process should now be ready. */
29657c74f5bSJohn Baldwin 	wpid = waitpid(child, &status, WNOHANG);
29757c74f5bSJohn Baldwin 	ATF_REQUIRE(wpid == child);
29857c74f5bSJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
29957c74f5bSJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
30057c74f5bSJohn Baldwin }
30157c74f5bSJohn Baldwin 
30257c74f5bSJohn Baldwin /*
30357c74f5bSJohn Baldwin  * Verify that a parent process "sees" the exit of a debugged process
30457c74f5bSJohn Baldwin  * only after a non-direct-child debugger has seen it.  In particular,
30557c74f5bSJohn Baldwin  * various wait() calls in the parent must avoid failing with ESRCH by
30657c74f5bSJohn Baldwin  * checking the parent's orphan list for the debugee.
30757c74f5bSJohn Baldwin  */
30857c74f5bSJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_unrelated_debugger);
30957c74f5bSJohn Baldwin ATF_TC_BODY(ptrace__parent_sees_exit_after_unrelated_debugger, tc)
31057c74f5bSJohn Baldwin {
31157c74f5bSJohn Baldwin 	pid_t child, debugger, fpid, wpid;
31257c74f5bSJohn Baldwin 	int cpipe[2], dpipe[2], status;
31357c74f5bSJohn Baldwin 	char c;
31457c74f5bSJohn Baldwin 
31557c74f5bSJohn Baldwin 	ATF_REQUIRE(pipe(cpipe) == 0);
31657c74f5bSJohn Baldwin 	ATF_REQUIRE((child = fork()) != -1);
31757c74f5bSJohn Baldwin 
31857c74f5bSJohn Baldwin 	if (child == 0) {
31957c74f5bSJohn Baldwin 		/* Child process. */
32057c74f5bSJohn Baldwin 		close(cpipe[0]);
32157c74f5bSJohn Baldwin 
32257c74f5bSJohn Baldwin 		/* Wait for parent to be ready. */
323dfa8ba12SJohn Baldwin 		CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
32457c74f5bSJohn Baldwin 
325b98cb919SJohn Baldwin 		_exit(1);
32657c74f5bSJohn Baldwin 	}
32757c74f5bSJohn Baldwin 	close(cpipe[1]);
32857c74f5bSJohn Baldwin 
32957c74f5bSJohn Baldwin 	ATF_REQUIRE(pipe(dpipe) == 0);
33057c74f5bSJohn Baldwin 	ATF_REQUIRE((debugger = fork()) != -1);
33157c74f5bSJohn Baldwin 
33257c74f5bSJohn Baldwin 	if (debugger == 0) {
33357c74f5bSJohn Baldwin 		/* Debugger parent. */
33457c74f5bSJohn Baldwin 
33557c74f5bSJohn Baldwin 		/*
33657c74f5bSJohn Baldwin 		 * Fork again and drop the debugger parent so that the
33757c74f5bSJohn Baldwin 		 * debugger is not a child of the main parent.
33857c74f5bSJohn Baldwin 		 */
339dfa8ba12SJohn Baldwin 		CHILD_REQUIRE((fpid = fork()) != -1);
34057c74f5bSJohn Baldwin 		if (fpid != 0)
341b98cb919SJohn Baldwin 			_exit(2);
34257c74f5bSJohn Baldwin 
34357c74f5bSJohn Baldwin 		/* Debugger process. */
34457c74f5bSJohn Baldwin 		close(dpipe[0]);
34557c74f5bSJohn Baldwin 
346dfa8ba12SJohn Baldwin 		CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
34757c74f5bSJohn Baldwin 
34857c74f5bSJohn Baldwin 		wpid = waitpid(child, &status, 0);
349dfa8ba12SJohn Baldwin 		CHILD_REQUIRE(wpid == child);
350dfa8ba12SJohn Baldwin 		CHILD_REQUIRE(WIFSTOPPED(status));
351dfa8ba12SJohn Baldwin 		CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
35257c74f5bSJohn Baldwin 
353dfa8ba12SJohn Baldwin 		CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
35457c74f5bSJohn Baldwin 
35557c74f5bSJohn Baldwin 		/* Signal parent that debugger is attached. */
356dfa8ba12SJohn Baldwin 		CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
35757c74f5bSJohn Baldwin 
35857c74f5bSJohn Baldwin 		/* Wait for parent's failed wait. */
359dfa8ba12SJohn Baldwin 		CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == sizeof(c));
36057c74f5bSJohn Baldwin 
36157c74f5bSJohn Baldwin 		wpid = waitpid(child, &status, 0);
362dfa8ba12SJohn Baldwin 		CHILD_REQUIRE(wpid == child);
363dfa8ba12SJohn Baldwin 		CHILD_REQUIRE(WIFEXITED(status));
364dfa8ba12SJohn Baldwin 		CHILD_REQUIRE(WEXITSTATUS(status) == 1);
36557c74f5bSJohn Baldwin 
366b98cb919SJohn Baldwin 		_exit(0);
36757c74f5bSJohn Baldwin 	}
368eddb85c6SJohn Baldwin 	close(dpipe[1]);
36957c74f5bSJohn Baldwin 
37057c74f5bSJohn Baldwin 	/* Parent process. */
37157c74f5bSJohn Baldwin 
37257c74f5bSJohn Baldwin 	/* Wait for the debugger parent process to exit. */
37357c74f5bSJohn Baldwin 	wpid = waitpid(debugger, &status, 0);
37457c74f5bSJohn Baldwin 	ATF_REQUIRE(wpid == debugger);
37557c74f5bSJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
37657c74f5bSJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 2);
37757c74f5bSJohn Baldwin 
37857c74f5bSJohn Baldwin 	/* A WNOHANG wait here should see the non-exited child. */
37957c74f5bSJohn Baldwin 	wpid = waitpid(child, &status, WNOHANG);
38057c74f5bSJohn Baldwin 	ATF_REQUIRE(wpid == 0);
38157c74f5bSJohn Baldwin 
38257c74f5bSJohn Baldwin 	/* Wait for the debugger to attach to the child. */
38357c74f5bSJohn Baldwin 	ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
38457c74f5bSJohn Baldwin 
38557c74f5bSJohn Baldwin 	/* Release the child. */
38657c74f5bSJohn Baldwin 	ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
38757c74f5bSJohn Baldwin 	ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
38857c74f5bSJohn Baldwin 	close(cpipe[0]);
38957c74f5bSJohn Baldwin 
39098685dc8SJohn Baldwin 	wait_for_zombie(child);
39157c74f5bSJohn Baldwin 
39257c74f5bSJohn Baldwin 	/*
3932f021998SJohn Baldwin 	 * This wait should return a pid of 0 to indicate no status to
3942f021998SJohn Baldwin 	 * report.  The parent should see the child as non-exited
3952f021998SJohn Baldwin 	 * until the debugger sees the exit.
39657c74f5bSJohn Baldwin 	 */
39757c74f5bSJohn Baldwin 	wpid = waitpid(child, &status, WNOHANG);
39857c74f5bSJohn Baldwin 	ATF_REQUIRE(wpid == 0);
39957c74f5bSJohn Baldwin 
40057c74f5bSJohn Baldwin 	/* Signal the debugger to wait for the child. */
401eddb85c6SJohn Baldwin 	ATF_REQUIRE(write(dpipe[0], &c, sizeof(c)) == sizeof(c));
40257c74f5bSJohn Baldwin 
40357c74f5bSJohn Baldwin 	/* Wait for the debugger. */
404eddb85c6SJohn Baldwin 	ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == 0);
405eddb85c6SJohn Baldwin 	close(dpipe[0]);
40657c74f5bSJohn Baldwin 
40757c74f5bSJohn Baldwin 	/* The child process should now be ready. */
40857c74f5bSJohn Baldwin 	wpid = waitpid(child, &status, WNOHANG);
40957c74f5bSJohn Baldwin 	ATF_REQUIRE(wpid == child);
41057c74f5bSJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
41157c74f5bSJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
41257c74f5bSJohn Baldwin }
41357c74f5bSJohn Baldwin 
41498685dc8SJohn Baldwin /*
41598685dc8SJohn Baldwin  * The parent process should always act the same regardless of how the
41698685dc8SJohn Baldwin  * debugger is attached to it.
41798685dc8SJohn Baldwin  */
41898685dc8SJohn Baldwin static __dead2 void
419189ac973SJohn Baldwin follow_fork_parent(bool use_vfork)
42098685dc8SJohn Baldwin {
42198685dc8SJohn Baldwin 	pid_t fpid, wpid;
42298685dc8SJohn Baldwin 	int status;
42398685dc8SJohn Baldwin 
424189ac973SJohn Baldwin 	if (use_vfork)
425189ac973SJohn Baldwin 		CHILD_REQUIRE((fpid = vfork()) != -1);
426189ac973SJohn Baldwin 	else
42798685dc8SJohn Baldwin 		CHILD_REQUIRE((fpid = fork()) != -1);
42898685dc8SJohn Baldwin 
42998685dc8SJohn Baldwin 	if (fpid == 0)
43098685dc8SJohn Baldwin 		/* Child */
431b98cb919SJohn Baldwin 		_exit(2);
43298685dc8SJohn Baldwin 
43398685dc8SJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
43498685dc8SJohn Baldwin 	CHILD_REQUIRE(wpid == fpid);
43598685dc8SJohn Baldwin 	CHILD_REQUIRE(WIFEXITED(status));
43698685dc8SJohn Baldwin 	CHILD_REQUIRE(WEXITSTATUS(status) == 2);
43798685dc8SJohn Baldwin 
438b98cb919SJohn Baldwin 	_exit(1);
43998685dc8SJohn Baldwin }
44098685dc8SJohn Baldwin 
44198685dc8SJohn Baldwin /*
44298685dc8SJohn Baldwin  * Helper routine for follow fork tests.  This waits for two stops
44398685dc8SJohn Baldwin  * that report both "sides" of a fork.  It returns the pid of the new
44498685dc8SJohn Baldwin  * child process.
44598685dc8SJohn Baldwin  */
44698685dc8SJohn Baldwin static pid_t
447189ac973SJohn Baldwin handle_fork_events(pid_t parent, struct ptrace_lwpinfo *ppl)
44898685dc8SJohn Baldwin {
44998685dc8SJohn Baldwin 	struct ptrace_lwpinfo pl;
45098685dc8SJohn Baldwin 	bool fork_reported[2];
45198685dc8SJohn Baldwin 	pid_t child, wpid;
45298685dc8SJohn Baldwin 	int i, status;
45398685dc8SJohn Baldwin 
45498685dc8SJohn Baldwin 	fork_reported[0] = false;
45598685dc8SJohn Baldwin 	fork_reported[1] = false;
45698685dc8SJohn Baldwin 	child = -1;
45798685dc8SJohn Baldwin 
45898685dc8SJohn Baldwin 	/*
45998685dc8SJohn Baldwin 	 * Each process should report a fork event.  The parent should
46098685dc8SJohn Baldwin 	 * report a PL_FLAG_FORKED event, and the child should report
46198685dc8SJohn Baldwin 	 * a PL_FLAG_CHILD event.
46298685dc8SJohn Baldwin 	 */
46398685dc8SJohn Baldwin 	for (i = 0; i < 2; i++) {
46498685dc8SJohn Baldwin 		wpid = wait(&status);
46598685dc8SJohn Baldwin 		ATF_REQUIRE(wpid > 0);
46698685dc8SJohn Baldwin 		ATF_REQUIRE(WIFSTOPPED(status));
46798685dc8SJohn Baldwin 
46898685dc8SJohn Baldwin 		ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
46998685dc8SJohn Baldwin 		    sizeof(pl)) != -1);
47098685dc8SJohn Baldwin 		ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
47198685dc8SJohn Baldwin 		    0);
47298685dc8SJohn Baldwin 		ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
47398685dc8SJohn Baldwin 		    (PL_FLAG_FORKED | PL_FLAG_CHILD));
47498685dc8SJohn Baldwin 		if (pl.pl_flags & PL_FLAG_CHILD) {
47598685dc8SJohn Baldwin 			ATF_REQUIRE(wpid != parent);
47698685dc8SJohn Baldwin 			ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
47798685dc8SJohn Baldwin 			ATF_REQUIRE(!fork_reported[1]);
47898685dc8SJohn Baldwin 			if (child == -1)
47998685dc8SJohn Baldwin 				child = wpid;
48098685dc8SJohn Baldwin 			else
48198685dc8SJohn Baldwin 				ATF_REQUIRE(child == wpid);
482189ac973SJohn Baldwin 			if (ppl != NULL)
483189ac973SJohn Baldwin 				ppl[1] = pl;
48498685dc8SJohn Baldwin 			fork_reported[1] = true;
48598685dc8SJohn Baldwin 		} else {
48698685dc8SJohn Baldwin 			ATF_REQUIRE(wpid == parent);
48798685dc8SJohn Baldwin 			ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
48898685dc8SJohn Baldwin 			ATF_REQUIRE(!fork_reported[0]);
48998685dc8SJohn Baldwin 			if (child == -1)
49098685dc8SJohn Baldwin 				child = pl.pl_child_pid;
49198685dc8SJohn Baldwin 			else
49298685dc8SJohn Baldwin 				ATF_REQUIRE(child == pl.pl_child_pid);
493189ac973SJohn Baldwin 			if (ppl != NULL)
494189ac973SJohn Baldwin 				ppl[0] = pl;
49598685dc8SJohn Baldwin 			fork_reported[0] = true;
49698685dc8SJohn Baldwin 		}
49798685dc8SJohn Baldwin 	}
49898685dc8SJohn Baldwin 
49998685dc8SJohn Baldwin 	return (child);
50098685dc8SJohn Baldwin }
50198685dc8SJohn Baldwin 
50298685dc8SJohn Baldwin /*
50398685dc8SJohn Baldwin  * Verify that a new child process is stopped after a followed fork and
50498685dc8SJohn Baldwin  * that the traced parent sees the exit of the child after the debugger
50598685dc8SJohn Baldwin  * when both processes remain attached to the debugger.
50698685dc8SJohn Baldwin  */
50798685dc8SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached);
50898685dc8SJohn Baldwin ATF_TC_BODY(ptrace__follow_fork_both_attached, tc)
50998685dc8SJohn Baldwin {
510479b610dSJohn Baldwin 	pid_t children[2], fpid, wpid;
51198685dc8SJohn Baldwin 	int status;
51298685dc8SJohn Baldwin 
51398685dc8SJohn Baldwin 	ATF_REQUIRE((fpid = fork()) != -1);
51498685dc8SJohn Baldwin 	if (fpid == 0) {
51598685dc8SJohn Baldwin 		trace_me();
516189ac973SJohn Baldwin 		follow_fork_parent(false);
51798685dc8SJohn Baldwin 	}
51898685dc8SJohn Baldwin 
51998685dc8SJohn Baldwin 	/* Parent process. */
52098685dc8SJohn Baldwin 	children[0] = fpid;
52198685dc8SJohn Baldwin 
52298685dc8SJohn Baldwin 	/* The first wait() should report the stop from SIGSTOP. */
52398685dc8SJohn Baldwin 	wpid = waitpid(children[0], &status, 0);
52498685dc8SJohn Baldwin 	ATF_REQUIRE(wpid == children[0]);
52598685dc8SJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
52698685dc8SJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
52798685dc8SJohn Baldwin 
52898685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
52998685dc8SJohn Baldwin 
53098685dc8SJohn Baldwin 	/* Continue the child ignoring the SIGSTOP. */
53198685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
53298685dc8SJohn Baldwin 
533189ac973SJohn Baldwin 	children[1] = handle_fork_events(children[0], NULL);
53498685dc8SJohn Baldwin 	ATF_REQUIRE(children[1] > 0);
53598685dc8SJohn Baldwin 
53698685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
53798685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
53898685dc8SJohn Baldwin 
53998685dc8SJohn Baldwin 	/*
54098685dc8SJohn Baldwin 	 * The child can't exit until the grandchild reports status, so the
54198685dc8SJohn Baldwin 	 * grandchild should report its exit first to the debugger.
54298685dc8SJohn Baldwin 	 */
54398685dc8SJohn Baldwin 	wpid = wait(&status);
54498685dc8SJohn Baldwin 	ATF_REQUIRE(wpid == children[1]);
54598685dc8SJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
54698685dc8SJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 2);
54798685dc8SJohn Baldwin 
54898685dc8SJohn Baldwin 	wpid = wait(&status);
54998685dc8SJohn Baldwin 	ATF_REQUIRE(wpid == children[0]);
55098685dc8SJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
55198685dc8SJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
55298685dc8SJohn Baldwin 
55398685dc8SJohn Baldwin 	wpid = wait(&status);
55498685dc8SJohn Baldwin 	ATF_REQUIRE(wpid == -1);
55598685dc8SJohn Baldwin 	ATF_REQUIRE(errno == ECHILD);
55698685dc8SJohn Baldwin }
55798685dc8SJohn Baldwin 
55898685dc8SJohn Baldwin /*
55998685dc8SJohn Baldwin  * Verify that a new child process is stopped after a followed fork
56098685dc8SJohn Baldwin  * and that the traced parent sees the exit of the child when the new
56198685dc8SJohn Baldwin  * child process is detached after it reports its fork.
56298685dc8SJohn Baldwin  */
56398685dc8SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached);
56498685dc8SJohn Baldwin ATF_TC_BODY(ptrace__follow_fork_child_detached, tc)
56598685dc8SJohn Baldwin {
566479b610dSJohn Baldwin 	pid_t children[2], fpid, wpid;
56798685dc8SJohn Baldwin 	int status;
56898685dc8SJohn Baldwin 
56998685dc8SJohn Baldwin 	ATF_REQUIRE((fpid = fork()) != -1);
57098685dc8SJohn Baldwin 	if (fpid == 0) {
57198685dc8SJohn Baldwin 		trace_me();
572189ac973SJohn Baldwin 		follow_fork_parent(false);
57398685dc8SJohn Baldwin 	}
57498685dc8SJohn Baldwin 
57598685dc8SJohn Baldwin 	/* Parent process. */
57698685dc8SJohn Baldwin 	children[0] = fpid;
57798685dc8SJohn Baldwin 
57898685dc8SJohn Baldwin 	/* The first wait() should report the stop from SIGSTOP. */
57998685dc8SJohn Baldwin 	wpid = waitpid(children[0], &status, 0);
58098685dc8SJohn Baldwin 	ATF_REQUIRE(wpid == children[0]);
58198685dc8SJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
58298685dc8SJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
58398685dc8SJohn Baldwin 
58498685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
58598685dc8SJohn Baldwin 
58698685dc8SJohn Baldwin 	/* Continue the child ignoring the SIGSTOP. */
58798685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
58898685dc8SJohn Baldwin 
589189ac973SJohn Baldwin 	children[1] = handle_fork_events(children[0], NULL);
59098685dc8SJohn Baldwin 	ATF_REQUIRE(children[1] > 0);
59198685dc8SJohn Baldwin 
59298685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
59398685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
59498685dc8SJohn Baldwin 
59598685dc8SJohn Baldwin 	/*
59698685dc8SJohn Baldwin 	 * Should not see any status from the grandchild now, only the
59798685dc8SJohn Baldwin 	 * child.
59898685dc8SJohn Baldwin 	 */
59998685dc8SJohn Baldwin 	wpid = wait(&status);
60098685dc8SJohn Baldwin 	ATF_REQUIRE(wpid == children[0]);
60198685dc8SJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
60298685dc8SJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
60398685dc8SJohn Baldwin 
60498685dc8SJohn Baldwin 	wpid = wait(&status);
60598685dc8SJohn Baldwin 	ATF_REQUIRE(wpid == -1);
60698685dc8SJohn Baldwin 	ATF_REQUIRE(errno == ECHILD);
60798685dc8SJohn Baldwin }
60898685dc8SJohn Baldwin 
60998685dc8SJohn Baldwin /*
61098685dc8SJohn Baldwin  * Verify that a new child process is stopped after a followed fork
61198685dc8SJohn Baldwin  * and that the traced parent sees the exit of the child when the
61298685dc8SJohn Baldwin  * traced parent is detached after the fork.
61398685dc8SJohn Baldwin  */
61498685dc8SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached);
61598685dc8SJohn Baldwin ATF_TC_BODY(ptrace__follow_fork_parent_detached, tc)
61698685dc8SJohn Baldwin {
617479b610dSJohn Baldwin 	pid_t children[2], fpid, wpid;
61898685dc8SJohn Baldwin 	int status;
61998685dc8SJohn Baldwin 
62098685dc8SJohn Baldwin 	ATF_REQUIRE((fpid = fork()) != -1);
62198685dc8SJohn Baldwin 	if (fpid == 0) {
62298685dc8SJohn Baldwin 		trace_me();
623189ac973SJohn Baldwin 		follow_fork_parent(false);
62498685dc8SJohn Baldwin 	}
62598685dc8SJohn Baldwin 
62698685dc8SJohn Baldwin 	/* Parent process. */
62798685dc8SJohn Baldwin 	children[0] = fpid;
62898685dc8SJohn Baldwin 
62998685dc8SJohn Baldwin 	/* The first wait() should report the stop from SIGSTOP. */
63098685dc8SJohn Baldwin 	wpid = waitpid(children[0], &status, 0);
63198685dc8SJohn Baldwin 	ATF_REQUIRE(wpid == children[0]);
63298685dc8SJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
63398685dc8SJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
63498685dc8SJohn Baldwin 
63598685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
63698685dc8SJohn Baldwin 
63798685dc8SJohn Baldwin 	/* Continue the child ignoring the SIGSTOP. */
63898685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
63998685dc8SJohn Baldwin 
640189ac973SJohn Baldwin 	children[1] = handle_fork_events(children[0], NULL);
64198685dc8SJohn Baldwin 	ATF_REQUIRE(children[1] > 0);
64298685dc8SJohn Baldwin 
64398685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
64498685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
64598685dc8SJohn Baldwin 
64698685dc8SJohn Baldwin 	/*
64798685dc8SJohn Baldwin 	 * The child can't exit until the grandchild reports status, so the
64898685dc8SJohn Baldwin 	 * grandchild should report its exit first to the debugger.
64998685dc8SJohn Baldwin 	 *
65098685dc8SJohn Baldwin 	 * Even though the child process is detached, it is still a
65198685dc8SJohn Baldwin 	 * child of the debugger, so it will still report it's exit
65298685dc8SJohn Baldwin 	 * after the grandchild.
65398685dc8SJohn Baldwin 	 */
65498685dc8SJohn Baldwin 	wpid = wait(&status);
65598685dc8SJohn Baldwin 	ATF_REQUIRE(wpid == children[1]);
65698685dc8SJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
65798685dc8SJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 2);
65898685dc8SJohn Baldwin 
65998685dc8SJohn Baldwin 	wpid = wait(&status);
66098685dc8SJohn Baldwin 	ATF_REQUIRE(wpid == children[0]);
66198685dc8SJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
66298685dc8SJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
66398685dc8SJohn Baldwin 
66498685dc8SJohn Baldwin 	wpid = wait(&status);
66598685dc8SJohn Baldwin 	ATF_REQUIRE(wpid == -1);
66698685dc8SJohn Baldwin 	ATF_REQUIRE(errno == ECHILD);
66798685dc8SJohn Baldwin }
66898685dc8SJohn Baldwin 
66998685dc8SJohn Baldwin static void
67098685dc8SJohn Baldwin attach_fork_parent(int cpipe[2])
67198685dc8SJohn Baldwin {
67298685dc8SJohn Baldwin 	pid_t fpid;
67398685dc8SJohn Baldwin 
67498685dc8SJohn Baldwin 	close(cpipe[0]);
67598685dc8SJohn Baldwin 
67698685dc8SJohn Baldwin 	/* Double-fork to disassociate from the debugger. */
67798685dc8SJohn Baldwin 	CHILD_REQUIRE((fpid = fork()) != -1);
67898685dc8SJohn Baldwin 	if (fpid != 0)
679b98cb919SJohn Baldwin 		_exit(3);
68098685dc8SJohn Baldwin 
68198685dc8SJohn Baldwin 	/* Send the pid of the disassociated child to the debugger. */
68298685dc8SJohn Baldwin 	fpid = getpid();
68398685dc8SJohn Baldwin 	CHILD_REQUIRE(write(cpipe[1], &fpid, sizeof(fpid)) == sizeof(fpid));
68498685dc8SJohn Baldwin 
68598685dc8SJohn Baldwin 	/* Wait for the debugger to attach. */
68698685dc8SJohn Baldwin 	CHILD_REQUIRE(read(cpipe[1], &fpid, sizeof(fpid)) == 0);
68798685dc8SJohn Baldwin }
68898685dc8SJohn Baldwin 
68998685dc8SJohn Baldwin /*
69098685dc8SJohn Baldwin  * Verify that a new child process is stopped after a followed fork and
69198685dc8SJohn Baldwin  * that the traced parent sees the exit of the child after the debugger
69298685dc8SJohn Baldwin  * when both processes remain attached to the debugger.  In this test
69398685dc8SJohn Baldwin  * the parent that forks is not a direct child of the debugger.
69498685dc8SJohn Baldwin  */
69598685dc8SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached_unrelated_debugger);
69698685dc8SJohn Baldwin ATF_TC_BODY(ptrace__follow_fork_both_attached_unrelated_debugger, tc)
69798685dc8SJohn Baldwin {
698479b610dSJohn Baldwin 	pid_t children[2], fpid, wpid;
69998685dc8SJohn Baldwin 	int cpipe[2], status;
70098685dc8SJohn Baldwin 
70198685dc8SJohn Baldwin 	ATF_REQUIRE(pipe(cpipe) == 0);
70298685dc8SJohn Baldwin 	ATF_REQUIRE((fpid = fork()) != -1);
70398685dc8SJohn Baldwin 	if (fpid == 0) {
70498685dc8SJohn Baldwin 		attach_fork_parent(cpipe);
705189ac973SJohn Baldwin 		follow_fork_parent(false);
70698685dc8SJohn Baldwin 	}
70798685dc8SJohn Baldwin 
70898685dc8SJohn Baldwin 	/* Parent process. */
70998685dc8SJohn Baldwin 	close(cpipe[1]);
71098685dc8SJohn Baldwin 
71198685dc8SJohn Baldwin 	/* Wait for the direct child to exit. */
71298685dc8SJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
71398685dc8SJohn Baldwin 	ATF_REQUIRE(wpid == fpid);
71498685dc8SJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
71598685dc8SJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 3);
71698685dc8SJohn Baldwin 
71798685dc8SJohn Baldwin 	/* Read the pid of the fork parent. */
71898685dc8SJohn Baldwin 	ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
71998685dc8SJohn Baldwin 	    sizeof(children[0]));
72098685dc8SJohn Baldwin 
72198685dc8SJohn Baldwin 	/* Attach to the fork parent. */
72298685dc8SJohn Baldwin 	attach_child(children[0]);
72398685dc8SJohn Baldwin 
72498685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
72598685dc8SJohn Baldwin 
72698685dc8SJohn Baldwin 	/* Continue the fork parent ignoring the SIGSTOP. */
72798685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
72898685dc8SJohn Baldwin 
72998685dc8SJohn Baldwin 	/* Signal the fork parent to continue. */
73098685dc8SJohn Baldwin 	close(cpipe[0]);
73198685dc8SJohn Baldwin 
732189ac973SJohn Baldwin 	children[1] = handle_fork_events(children[0], NULL);
73398685dc8SJohn Baldwin 	ATF_REQUIRE(children[1] > 0);
73498685dc8SJohn Baldwin 
73598685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
73698685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
73798685dc8SJohn Baldwin 
73898685dc8SJohn Baldwin 	/*
73998685dc8SJohn Baldwin 	 * The fork parent can't exit until the child reports status,
74098685dc8SJohn Baldwin 	 * so the child should report its exit first to the debugger.
74198685dc8SJohn Baldwin 	 */
74298685dc8SJohn Baldwin 	wpid = wait(&status);
74398685dc8SJohn Baldwin 	ATF_REQUIRE(wpid == children[1]);
74498685dc8SJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
74598685dc8SJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 2);
74698685dc8SJohn Baldwin 
74798685dc8SJohn Baldwin 	wpid = wait(&status);
74898685dc8SJohn Baldwin 	ATF_REQUIRE(wpid == children[0]);
74998685dc8SJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
75098685dc8SJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
75198685dc8SJohn Baldwin 
75298685dc8SJohn Baldwin 	wpid = wait(&status);
75398685dc8SJohn Baldwin 	ATF_REQUIRE(wpid == -1);
75498685dc8SJohn Baldwin 	ATF_REQUIRE(errno == ECHILD);
75598685dc8SJohn Baldwin }
75698685dc8SJohn Baldwin 
75798685dc8SJohn Baldwin /*
75898685dc8SJohn Baldwin  * Verify that a new child process is stopped after a followed fork
75998685dc8SJohn Baldwin  * and that the traced parent sees the exit of the child when the new
76098685dc8SJohn Baldwin  * child process is detached after it reports its fork.  In this test
76198685dc8SJohn Baldwin  * the parent that forks is not a direct child of the debugger.
76298685dc8SJohn Baldwin  */
76398685dc8SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached_unrelated_debugger);
76498685dc8SJohn Baldwin ATF_TC_BODY(ptrace__follow_fork_child_detached_unrelated_debugger, tc)
76598685dc8SJohn Baldwin {
766479b610dSJohn Baldwin 	pid_t children[2], fpid, wpid;
76798685dc8SJohn Baldwin 	int cpipe[2], status;
76898685dc8SJohn Baldwin 
76998685dc8SJohn Baldwin 	ATF_REQUIRE(pipe(cpipe) == 0);
77098685dc8SJohn Baldwin 	ATF_REQUIRE((fpid = fork()) != -1);
77198685dc8SJohn Baldwin 	if (fpid == 0) {
77298685dc8SJohn Baldwin 		attach_fork_parent(cpipe);
773189ac973SJohn Baldwin 		follow_fork_parent(false);
77498685dc8SJohn Baldwin 	}
77598685dc8SJohn Baldwin 
77698685dc8SJohn Baldwin 	/* Parent process. */
77798685dc8SJohn Baldwin 	close(cpipe[1]);
77898685dc8SJohn Baldwin 
77998685dc8SJohn Baldwin 	/* Wait for the direct child to exit. */
78098685dc8SJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
78198685dc8SJohn Baldwin 	ATF_REQUIRE(wpid == fpid);
78298685dc8SJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
78398685dc8SJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 3);
78498685dc8SJohn Baldwin 
78598685dc8SJohn Baldwin 	/* Read the pid of the fork parent. */
78698685dc8SJohn Baldwin 	ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
78798685dc8SJohn Baldwin 	    sizeof(children[0]));
78898685dc8SJohn Baldwin 
78998685dc8SJohn Baldwin 	/* Attach to the fork parent. */
79098685dc8SJohn Baldwin 	attach_child(children[0]);
79198685dc8SJohn Baldwin 
79298685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
79398685dc8SJohn Baldwin 
79498685dc8SJohn Baldwin 	/* Continue the fork parent ignoring the SIGSTOP. */
79598685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
79698685dc8SJohn Baldwin 
79798685dc8SJohn Baldwin 	/* Signal the fork parent to continue. */
79898685dc8SJohn Baldwin 	close(cpipe[0]);
79998685dc8SJohn Baldwin 
800189ac973SJohn Baldwin 	children[1] = handle_fork_events(children[0], NULL);
80198685dc8SJohn Baldwin 	ATF_REQUIRE(children[1] > 0);
80298685dc8SJohn Baldwin 
80398685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
80498685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
80598685dc8SJohn Baldwin 
80698685dc8SJohn Baldwin 	/*
80798685dc8SJohn Baldwin 	 * Should not see any status from the child now, only the fork
80898685dc8SJohn Baldwin 	 * parent.
80998685dc8SJohn Baldwin 	 */
81098685dc8SJohn Baldwin 	wpid = wait(&status);
81198685dc8SJohn Baldwin 	ATF_REQUIRE(wpid == children[0]);
81298685dc8SJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
81398685dc8SJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
81498685dc8SJohn Baldwin 
81598685dc8SJohn Baldwin 	wpid = wait(&status);
81698685dc8SJohn Baldwin 	ATF_REQUIRE(wpid == -1);
81798685dc8SJohn Baldwin 	ATF_REQUIRE(errno == ECHILD);
81898685dc8SJohn Baldwin }
81998685dc8SJohn Baldwin 
82098685dc8SJohn Baldwin /*
82198685dc8SJohn Baldwin  * Verify that a new child process is stopped after a followed fork
82298685dc8SJohn Baldwin  * and that the traced parent sees the exit of the child when the
82398685dc8SJohn Baldwin  * traced parent is detached after the fork.  In this test the parent
82498685dc8SJohn Baldwin  * that forks is not a direct child of the debugger.
82598685dc8SJohn Baldwin  */
82698685dc8SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached_unrelated_debugger);
82798685dc8SJohn Baldwin ATF_TC_BODY(ptrace__follow_fork_parent_detached_unrelated_debugger, tc)
82898685dc8SJohn Baldwin {
829479b610dSJohn Baldwin 	pid_t children[2], fpid, wpid;
83098685dc8SJohn Baldwin 	int cpipe[2], status;
83198685dc8SJohn Baldwin 
83298685dc8SJohn Baldwin 	ATF_REQUIRE(pipe(cpipe) == 0);
83398685dc8SJohn Baldwin 	ATF_REQUIRE((fpid = fork()) != -1);
83498685dc8SJohn Baldwin 	if (fpid == 0) {
83598685dc8SJohn Baldwin 		attach_fork_parent(cpipe);
836189ac973SJohn Baldwin 		follow_fork_parent(false);
83798685dc8SJohn Baldwin 	}
83898685dc8SJohn Baldwin 
83998685dc8SJohn Baldwin 	/* Parent process. */
84098685dc8SJohn Baldwin 	close(cpipe[1]);
84198685dc8SJohn Baldwin 
84298685dc8SJohn Baldwin 	/* Wait for the direct child to exit. */
84398685dc8SJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
84498685dc8SJohn Baldwin 	ATF_REQUIRE(wpid == fpid);
84598685dc8SJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
84698685dc8SJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 3);
84798685dc8SJohn Baldwin 
84898685dc8SJohn Baldwin 	/* Read the pid of the fork parent. */
84998685dc8SJohn Baldwin 	ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
85098685dc8SJohn Baldwin 	    sizeof(children[0]));
85198685dc8SJohn Baldwin 
85298685dc8SJohn Baldwin 	/* Attach to the fork parent. */
85398685dc8SJohn Baldwin 	attach_child(children[0]);
85498685dc8SJohn Baldwin 
85598685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
85698685dc8SJohn Baldwin 
85798685dc8SJohn Baldwin 	/* Continue the fork parent ignoring the SIGSTOP. */
85898685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
85998685dc8SJohn Baldwin 
86098685dc8SJohn Baldwin 	/* Signal the fork parent to continue. */
86198685dc8SJohn Baldwin 	close(cpipe[0]);
86298685dc8SJohn Baldwin 
863189ac973SJohn Baldwin 	children[1] = handle_fork_events(children[0], NULL);
86498685dc8SJohn Baldwin 	ATF_REQUIRE(children[1] > 0);
86598685dc8SJohn Baldwin 
86698685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
86798685dc8SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
86898685dc8SJohn Baldwin 
86998685dc8SJohn Baldwin 	/*
87098685dc8SJohn Baldwin 	 * Should not see any status from the fork parent now, only
87198685dc8SJohn Baldwin 	 * the child.
87298685dc8SJohn Baldwin 	 */
87398685dc8SJohn Baldwin 	wpid = wait(&status);
87498685dc8SJohn Baldwin 	ATF_REQUIRE(wpid == children[1]);
87598685dc8SJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
87698685dc8SJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 2);
87798685dc8SJohn Baldwin 
87898685dc8SJohn Baldwin 	wpid = wait(&status);
87998685dc8SJohn Baldwin 	ATF_REQUIRE(wpid == -1);
88098685dc8SJohn Baldwin 	ATF_REQUIRE(errno == ECHILD);
88198685dc8SJohn Baldwin }
88298685dc8SJohn Baldwin 
883368b2b1cSJohn Baldwin /*
884368b2b1cSJohn Baldwin  * Verify that a child process does not see an unrelated debugger as its
885368b2b1cSJohn Baldwin  * parent but sees its original parent process.
886368b2b1cSJohn Baldwin  */
887368b2b1cSJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__getppid);
888368b2b1cSJohn Baldwin ATF_TC_BODY(ptrace__getppid, tc)
889368b2b1cSJohn Baldwin {
890368b2b1cSJohn Baldwin 	pid_t child, debugger, ppid, wpid;
891368b2b1cSJohn Baldwin 	int cpipe[2], dpipe[2], status;
892368b2b1cSJohn Baldwin 	char c;
893368b2b1cSJohn Baldwin 
894368b2b1cSJohn Baldwin 	ATF_REQUIRE(pipe(cpipe) == 0);
895368b2b1cSJohn Baldwin 	ATF_REQUIRE((child = fork()) != -1);
896368b2b1cSJohn Baldwin 
897368b2b1cSJohn Baldwin 	if (child == 0) {
898368b2b1cSJohn Baldwin 		/* Child process. */
899368b2b1cSJohn Baldwin 		close(cpipe[0]);
900368b2b1cSJohn Baldwin 
901368b2b1cSJohn Baldwin 		/* Wait for parent to be ready. */
902368b2b1cSJohn Baldwin 		CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
903368b2b1cSJohn Baldwin 
904368b2b1cSJohn Baldwin 		/* Report the parent PID to the parent. */
905368b2b1cSJohn Baldwin 		ppid = getppid();
906368b2b1cSJohn Baldwin 		CHILD_REQUIRE(write(cpipe[1], &ppid, sizeof(ppid)) ==
907368b2b1cSJohn Baldwin 		    sizeof(ppid));
908368b2b1cSJohn Baldwin 
909368b2b1cSJohn Baldwin 		_exit(1);
910368b2b1cSJohn Baldwin 	}
911368b2b1cSJohn Baldwin 	close(cpipe[1]);
912368b2b1cSJohn Baldwin 
913368b2b1cSJohn Baldwin 	ATF_REQUIRE(pipe(dpipe) == 0);
914368b2b1cSJohn Baldwin 	ATF_REQUIRE((debugger = fork()) != -1);
915368b2b1cSJohn Baldwin 
916368b2b1cSJohn Baldwin 	if (debugger == 0) {
917368b2b1cSJohn Baldwin 		/* Debugger process. */
918368b2b1cSJohn Baldwin 		close(dpipe[0]);
919368b2b1cSJohn Baldwin 
920368b2b1cSJohn Baldwin 		CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
921368b2b1cSJohn Baldwin 
922368b2b1cSJohn Baldwin 		wpid = waitpid(child, &status, 0);
923368b2b1cSJohn Baldwin 		CHILD_REQUIRE(wpid == child);
924368b2b1cSJohn Baldwin 		CHILD_REQUIRE(WIFSTOPPED(status));
925368b2b1cSJohn Baldwin 		CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
926368b2b1cSJohn Baldwin 
927368b2b1cSJohn Baldwin 		CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
928368b2b1cSJohn Baldwin 
929368b2b1cSJohn Baldwin 		/* Signal parent that debugger is attached. */
930368b2b1cSJohn Baldwin 		CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
931368b2b1cSJohn Baldwin 
932368b2b1cSJohn Baldwin 		/* Wait for traced child to exit. */
933368b2b1cSJohn Baldwin 		wpid = waitpid(child, &status, 0);
934368b2b1cSJohn Baldwin 		CHILD_REQUIRE(wpid == child);
935368b2b1cSJohn Baldwin 		CHILD_REQUIRE(WIFEXITED(status));
936368b2b1cSJohn Baldwin 		CHILD_REQUIRE(WEXITSTATUS(status) == 1);
937368b2b1cSJohn Baldwin 
938368b2b1cSJohn Baldwin 		_exit(0);
939368b2b1cSJohn Baldwin 	}
940368b2b1cSJohn Baldwin 	close(dpipe[1]);
941368b2b1cSJohn Baldwin 
942368b2b1cSJohn Baldwin 	/* Parent process. */
943368b2b1cSJohn Baldwin 
944368b2b1cSJohn Baldwin 	/* Wait for the debugger to attach to the child. */
945368b2b1cSJohn Baldwin 	ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
946368b2b1cSJohn Baldwin 
947368b2b1cSJohn Baldwin 	/* Release the child. */
948368b2b1cSJohn Baldwin 	ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
949368b2b1cSJohn Baldwin 
950368b2b1cSJohn Baldwin 	/* Read the parent PID from the child. */
951368b2b1cSJohn Baldwin 	ATF_REQUIRE(read(cpipe[0], &ppid, sizeof(ppid)) == sizeof(ppid));
952368b2b1cSJohn Baldwin 	close(cpipe[0]);
953368b2b1cSJohn Baldwin 
954368b2b1cSJohn Baldwin 	ATF_REQUIRE(ppid == getpid());
955368b2b1cSJohn Baldwin 
956368b2b1cSJohn Baldwin 	/* Wait for the debugger. */
957368b2b1cSJohn Baldwin 	wpid = waitpid(debugger, &status, 0);
958368b2b1cSJohn Baldwin 	ATF_REQUIRE(wpid == debugger);
959368b2b1cSJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
960368b2b1cSJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 0);
961368b2b1cSJohn Baldwin 
962368b2b1cSJohn Baldwin 	/* The child process should now be ready. */
963368b2b1cSJohn Baldwin 	wpid = waitpid(child, &status, WNOHANG);
964368b2b1cSJohn Baldwin 	ATF_REQUIRE(wpid == child);
965368b2b1cSJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
966368b2b1cSJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
967368b2b1cSJohn Baldwin }
968368b2b1cSJohn Baldwin 
969189ac973SJohn Baldwin /*
970189ac973SJohn Baldwin  * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
971189ac973SJohn Baldwin  * child process created via fork() reports the correct value.
972189ac973SJohn Baldwin  */
973189ac973SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_fork);
974189ac973SJohn Baldwin ATF_TC_BODY(ptrace__new_child_pl_syscall_code_fork, tc)
975189ac973SJohn Baldwin {
976189ac973SJohn Baldwin 	struct ptrace_lwpinfo pl[2];
977189ac973SJohn Baldwin 	pid_t children[2], fpid, wpid;
978189ac973SJohn Baldwin 	int status;
979189ac973SJohn Baldwin 
980189ac973SJohn Baldwin 	ATF_REQUIRE((fpid = fork()) != -1);
981189ac973SJohn Baldwin 	if (fpid == 0) {
982189ac973SJohn Baldwin 		trace_me();
983189ac973SJohn Baldwin 		follow_fork_parent(false);
984189ac973SJohn Baldwin 	}
985189ac973SJohn Baldwin 
986189ac973SJohn Baldwin 	/* Parent process. */
987189ac973SJohn Baldwin 	children[0] = fpid;
988189ac973SJohn Baldwin 
989189ac973SJohn Baldwin 	/* The first wait() should report the stop from SIGSTOP. */
990189ac973SJohn Baldwin 	wpid = waitpid(children[0], &status, 0);
991189ac973SJohn Baldwin 	ATF_REQUIRE(wpid == children[0]);
992189ac973SJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
993189ac973SJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
994189ac973SJohn Baldwin 
995189ac973SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
996189ac973SJohn Baldwin 
997189ac973SJohn Baldwin 	/* Continue the child ignoring the SIGSTOP. */
998189ac973SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
999189ac973SJohn Baldwin 
1000189ac973SJohn Baldwin 	/* Wait for both halves of the fork event to get reported. */
1001189ac973SJohn Baldwin 	children[1] = handle_fork_events(children[0], pl);
1002189ac973SJohn Baldwin 	ATF_REQUIRE(children[1] > 0);
1003189ac973SJohn Baldwin 
1004189ac973SJohn Baldwin 	ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1005189ac973SJohn Baldwin 	ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1006189ac973SJohn Baldwin 	ATF_REQUIRE(pl[0].pl_syscall_code == SYS_fork);
1007189ac973SJohn Baldwin 	ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1008189ac973SJohn Baldwin 	ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1009189ac973SJohn Baldwin 
1010189ac973SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1011189ac973SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1012189ac973SJohn Baldwin 
1013189ac973SJohn Baldwin 	/*
1014189ac973SJohn Baldwin 	 * The child can't exit until the grandchild reports status, so the
1015189ac973SJohn Baldwin 	 * grandchild should report its exit first to the debugger.
1016189ac973SJohn Baldwin 	 */
1017189ac973SJohn Baldwin 	wpid = wait(&status);
1018189ac973SJohn Baldwin 	ATF_REQUIRE(wpid == children[1]);
1019189ac973SJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
1020189ac973SJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 2);
1021189ac973SJohn Baldwin 
1022189ac973SJohn Baldwin 	wpid = wait(&status);
1023189ac973SJohn Baldwin 	ATF_REQUIRE(wpid == children[0]);
1024189ac973SJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
1025189ac973SJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1026189ac973SJohn Baldwin 
1027189ac973SJohn Baldwin 	wpid = wait(&status);
1028189ac973SJohn Baldwin 	ATF_REQUIRE(wpid == -1);
1029189ac973SJohn Baldwin 	ATF_REQUIRE(errno == ECHILD);
1030189ac973SJohn Baldwin }
1031189ac973SJohn Baldwin 
1032189ac973SJohn Baldwin /*
1033189ac973SJohn Baldwin  * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1034189ac973SJohn Baldwin  * child process created via vfork() reports the correct value.
1035189ac973SJohn Baldwin  */
1036189ac973SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_vfork);
1037189ac973SJohn Baldwin ATF_TC_BODY(ptrace__new_child_pl_syscall_code_vfork, tc)
1038189ac973SJohn Baldwin {
1039189ac973SJohn Baldwin 	struct ptrace_lwpinfo pl[2];
1040189ac973SJohn Baldwin 	pid_t children[2], fpid, wpid;
1041189ac973SJohn Baldwin 	int status;
1042189ac973SJohn Baldwin 
1043189ac973SJohn Baldwin 	ATF_REQUIRE((fpid = fork()) != -1);
1044189ac973SJohn Baldwin 	if (fpid == 0) {
1045189ac973SJohn Baldwin 		trace_me();
1046189ac973SJohn Baldwin 		follow_fork_parent(true);
1047189ac973SJohn Baldwin 	}
1048189ac973SJohn Baldwin 
1049189ac973SJohn Baldwin 	/* Parent process. */
1050189ac973SJohn Baldwin 	children[0] = fpid;
1051189ac973SJohn Baldwin 
1052189ac973SJohn Baldwin 	/* The first wait() should report the stop from SIGSTOP. */
1053189ac973SJohn Baldwin 	wpid = waitpid(children[0], &status, 0);
1054189ac973SJohn Baldwin 	ATF_REQUIRE(wpid == children[0]);
1055189ac973SJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
1056189ac973SJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1057189ac973SJohn Baldwin 
1058189ac973SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1059189ac973SJohn Baldwin 
1060189ac973SJohn Baldwin 	/* Continue the child ignoring the SIGSTOP. */
1061189ac973SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1062189ac973SJohn Baldwin 
1063189ac973SJohn Baldwin 	/* Wait for both halves of the fork event to get reported. */
1064189ac973SJohn Baldwin 	children[1] = handle_fork_events(children[0], pl);
1065189ac973SJohn Baldwin 	ATF_REQUIRE(children[1] > 0);
1066189ac973SJohn Baldwin 
1067189ac973SJohn Baldwin 	ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1068189ac973SJohn Baldwin 	ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1069189ac973SJohn Baldwin 	ATF_REQUIRE(pl[0].pl_syscall_code == SYS_vfork);
1070189ac973SJohn Baldwin 	ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1071189ac973SJohn Baldwin 	ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1072189ac973SJohn Baldwin 
1073189ac973SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1074189ac973SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1075189ac973SJohn Baldwin 
1076189ac973SJohn Baldwin 	/*
1077189ac973SJohn Baldwin 	 * The child can't exit until the grandchild reports status, so the
1078189ac973SJohn Baldwin 	 * grandchild should report its exit first to the debugger.
1079189ac973SJohn Baldwin 	 */
1080189ac973SJohn Baldwin 	wpid = wait(&status);
1081189ac973SJohn Baldwin 	ATF_REQUIRE(wpid == children[1]);
1082189ac973SJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
1083189ac973SJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 2);
1084189ac973SJohn Baldwin 
1085189ac973SJohn Baldwin 	wpid = wait(&status);
1086189ac973SJohn Baldwin 	ATF_REQUIRE(wpid == children[0]);
1087189ac973SJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
1088189ac973SJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1089189ac973SJohn Baldwin 
1090189ac973SJohn Baldwin 	wpid = wait(&status);
1091189ac973SJohn Baldwin 	ATF_REQUIRE(wpid == -1);
1092189ac973SJohn Baldwin 	ATF_REQUIRE(errno == ECHILD);
1093189ac973SJohn Baldwin }
1094189ac973SJohn Baldwin 
1095189ac973SJohn Baldwin static void *
1096189ac973SJohn Baldwin simple_thread(void *arg __unused)
1097189ac973SJohn Baldwin {
1098189ac973SJohn Baldwin 
1099189ac973SJohn Baldwin 	pthread_exit(NULL);
1100189ac973SJohn Baldwin }
1101189ac973SJohn Baldwin 
11025fcfab6eSJohn Baldwin static __dead2 void
11035fcfab6eSJohn Baldwin simple_thread_main(void)
11045fcfab6eSJohn Baldwin {
11055fcfab6eSJohn Baldwin 	pthread_t thread;
11065fcfab6eSJohn Baldwin 
11075fcfab6eSJohn Baldwin 	CHILD_REQUIRE(pthread_create(&thread, NULL, simple_thread, NULL) == 0);
11085fcfab6eSJohn Baldwin 	CHILD_REQUIRE(pthread_join(thread, NULL) == 0);
11095fcfab6eSJohn Baldwin 	exit(1);
11105fcfab6eSJohn Baldwin }
11115fcfab6eSJohn Baldwin 
1112189ac973SJohn Baldwin /*
1113189ac973SJohn Baldwin  * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1114189ac973SJohn Baldwin  * thread reports the correct value.
1115189ac973SJohn Baldwin  */
1116189ac973SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_thread);
1117189ac973SJohn Baldwin ATF_TC_BODY(ptrace__new_child_pl_syscall_code_thread, tc)
1118189ac973SJohn Baldwin {
1119189ac973SJohn Baldwin 	struct ptrace_lwpinfo pl;
1120189ac973SJohn Baldwin 	pid_t fpid, wpid;
1121e72879e5SJohn Baldwin 	lwpid_t mainlwp;
1122189ac973SJohn Baldwin 	int status;
1123189ac973SJohn Baldwin 
1124189ac973SJohn Baldwin 	ATF_REQUIRE((fpid = fork()) != -1);
1125189ac973SJohn Baldwin 	if (fpid == 0) {
1126189ac973SJohn Baldwin 		trace_me();
11275fcfab6eSJohn Baldwin 		simple_thread_main();
1128189ac973SJohn Baldwin 	}
1129189ac973SJohn Baldwin 
1130189ac973SJohn Baldwin 	/* The first wait() should report the stop from SIGSTOP. */
1131189ac973SJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
1132189ac973SJohn Baldwin 	ATF_REQUIRE(wpid == fpid);
1133189ac973SJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
1134189ac973SJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1135189ac973SJohn Baldwin 
1136189ac973SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1137189ac973SJohn Baldwin 	    sizeof(pl)) != -1);
1138e72879e5SJohn Baldwin 	mainlwp = pl.pl_lwpid;
1139189ac973SJohn Baldwin 
1140189ac973SJohn Baldwin 	/*
1141189ac973SJohn Baldwin 	 * Continue the child ignoring the SIGSTOP and tracing all
1142189ac973SJohn Baldwin 	 * system call exits.
1143189ac973SJohn Baldwin 	 */
1144189ac973SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_TO_SCX, fpid, (caddr_t)1, 0) != -1);
1145189ac973SJohn Baldwin 
1146189ac973SJohn Baldwin 	/*
1147189ac973SJohn Baldwin 	 * Wait for the new thread to arrive.  pthread_create() might
1148189ac973SJohn Baldwin 	 * invoke any number of system calls.  For now we just wait
1149189ac973SJohn Baldwin 	 * for the new thread to arrive and make sure it reports a
1150189ac973SJohn Baldwin 	 * valid system call code.  If ptrace grows thread event
1151189ac973SJohn Baldwin 	 * reporting then this test can be made more precise.
1152189ac973SJohn Baldwin 	 */
1153189ac973SJohn Baldwin 	for (;;) {
1154189ac973SJohn Baldwin 		wpid = waitpid(fpid, &status, 0);
1155189ac973SJohn Baldwin 		ATF_REQUIRE(wpid == fpid);
1156189ac973SJohn Baldwin 		ATF_REQUIRE(WIFSTOPPED(status));
1157189ac973SJohn Baldwin 		ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1158189ac973SJohn Baldwin 
1159189ac973SJohn Baldwin 		ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1160189ac973SJohn Baldwin 		    sizeof(pl)) != -1);
1161189ac973SJohn Baldwin 		ATF_REQUIRE((pl.pl_flags & PL_FLAG_SCX) != 0);
1162189ac973SJohn Baldwin 		ATF_REQUIRE(pl.pl_syscall_code != 0);
1163e72879e5SJohn Baldwin 		if (pl.pl_lwpid != mainlwp)
1164189ac973SJohn Baldwin 			/* New thread seen. */
1165189ac973SJohn Baldwin 			break;
1166189ac973SJohn Baldwin 
1167189ac973SJohn Baldwin 		ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1168189ac973SJohn Baldwin 	}
1169189ac973SJohn Baldwin 
1170189ac973SJohn Baldwin 	/* Wait for the child to exit. */
1171189ac973SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1172189ac973SJohn Baldwin 	for (;;) {
1173189ac973SJohn Baldwin 		wpid = waitpid(fpid, &status, 0);
1174189ac973SJohn Baldwin 		ATF_REQUIRE(wpid == fpid);
1175189ac973SJohn Baldwin 		if (WIFEXITED(status))
1176189ac973SJohn Baldwin 			break;
1177189ac973SJohn Baldwin 
1178189ac973SJohn Baldwin 		ATF_REQUIRE(WIFSTOPPED(status));
1179189ac973SJohn Baldwin 		ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1180189ac973SJohn Baldwin 		ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1181189ac973SJohn Baldwin 	}
1182189ac973SJohn Baldwin 
1183189ac973SJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1184189ac973SJohn Baldwin 
1185189ac973SJohn Baldwin 	wpid = wait(&status);
1186189ac973SJohn Baldwin 	ATF_REQUIRE(wpid == -1);
1187189ac973SJohn Baldwin 	ATF_REQUIRE(errno == ECHILD);
1188189ac973SJohn Baldwin }
1189189ac973SJohn Baldwin 
11905fcfab6eSJohn Baldwin /*
11915fcfab6eSJohn Baldwin  * Verify that the expected LWP events are reported for a child thread.
11925fcfab6eSJohn Baldwin  */
11935fcfab6eSJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__lwp_events);
11945fcfab6eSJohn Baldwin ATF_TC_BODY(ptrace__lwp_events, tc)
11955fcfab6eSJohn Baldwin {
11965fcfab6eSJohn Baldwin 	struct ptrace_lwpinfo pl;
11975fcfab6eSJohn Baldwin 	pid_t fpid, wpid;
11985fcfab6eSJohn Baldwin 	lwpid_t lwps[2];
11995fcfab6eSJohn Baldwin 	int status;
12005fcfab6eSJohn Baldwin 
12015fcfab6eSJohn Baldwin 	ATF_REQUIRE((fpid = fork()) != -1);
12025fcfab6eSJohn Baldwin 	if (fpid == 0) {
12035fcfab6eSJohn Baldwin 		trace_me();
12045fcfab6eSJohn Baldwin 		simple_thread_main();
12055fcfab6eSJohn Baldwin 	}
12065fcfab6eSJohn Baldwin 
12075fcfab6eSJohn Baldwin 	/* The first wait() should report the stop from SIGSTOP. */
12085fcfab6eSJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
12095fcfab6eSJohn Baldwin 	ATF_REQUIRE(wpid == fpid);
12105fcfab6eSJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
12115fcfab6eSJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
12125fcfab6eSJohn Baldwin 
12135fcfab6eSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
12145fcfab6eSJohn Baldwin 	    sizeof(pl)) != -1);
12155fcfab6eSJohn Baldwin 	lwps[0] = pl.pl_lwpid;
12165fcfab6eSJohn Baldwin 
12175fcfab6eSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
12185fcfab6eSJohn Baldwin 
12195fcfab6eSJohn Baldwin 	/* Continue the child ignoring the SIGSTOP. */
12205fcfab6eSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
12215fcfab6eSJohn Baldwin 
12225fcfab6eSJohn Baldwin 	/* The first event should be for the child thread's birth. */
12235fcfab6eSJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
12245fcfab6eSJohn Baldwin 	ATF_REQUIRE(wpid == fpid);
12255fcfab6eSJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
12265fcfab6eSJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
12275fcfab6eSJohn Baldwin 
12285fcfab6eSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
12295fcfab6eSJohn Baldwin 	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
12305fcfab6eSJohn Baldwin 	    (PL_FLAG_BORN | PL_FLAG_SCX));
12315fcfab6eSJohn Baldwin 	ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
12325fcfab6eSJohn Baldwin 	lwps[1] = pl.pl_lwpid;
12335fcfab6eSJohn Baldwin 
12345fcfab6eSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
12355fcfab6eSJohn Baldwin 
12365fcfab6eSJohn Baldwin 	/* The next event should be for the child thread's death. */
12375fcfab6eSJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
12385fcfab6eSJohn Baldwin 	ATF_REQUIRE(wpid == fpid);
12395fcfab6eSJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
12405fcfab6eSJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
12415fcfab6eSJohn Baldwin 
12425fcfab6eSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
12435fcfab6eSJohn Baldwin 	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
12445fcfab6eSJohn Baldwin 	    (PL_FLAG_EXITED | PL_FLAG_SCE));
12455fcfab6eSJohn Baldwin 	ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
12465fcfab6eSJohn Baldwin 
12475fcfab6eSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
12485fcfab6eSJohn Baldwin 
12495fcfab6eSJohn Baldwin 	/* The last event should be for the child process's exit. */
12505fcfab6eSJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
12515fcfab6eSJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
12525fcfab6eSJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
12535fcfab6eSJohn Baldwin 
12545fcfab6eSJohn Baldwin 	wpid = wait(&status);
12555fcfab6eSJohn Baldwin 	ATF_REQUIRE(wpid == -1);
12565fcfab6eSJohn Baldwin 	ATF_REQUIRE(errno == ECHILD);
12575fcfab6eSJohn Baldwin }
12585fcfab6eSJohn Baldwin 
12595fcfab6eSJohn Baldwin static void *
12605fcfab6eSJohn Baldwin exec_thread(void *arg __unused)
12615fcfab6eSJohn Baldwin {
12625fcfab6eSJohn Baldwin 
12635fcfab6eSJohn Baldwin 	execl("/usr/bin/true", "true", NULL);
12645fcfab6eSJohn Baldwin 	exit(127);
12655fcfab6eSJohn Baldwin }
12665fcfab6eSJohn Baldwin 
12675fcfab6eSJohn Baldwin static __dead2 void
12685fcfab6eSJohn Baldwin exec_thread_main(void)
12695fcfab6eSJohn Baldwin {
12705fcfab6eSJohn Baldwin 	pthread_t thread;
12715fcfab6eSJohn Baldwin 
12725fcfab6eSJohn Baldwin 	CHILD_REQUIRE(pthread_create(&thread, NULL, exec_thread, NULL) == 0);
12735fcfab6eSJohn Baldwin 	for (;;)
12745fcfab6eSJohn Baldwin 		sleep(60);
12755fcfab6eSJohn Baldwin 	exit(1);
12765fcfab6eSJohn Baldwin }
12775fcfab6eSJohn Baldwin 
12785fcfab6eSJohn Baldwin /*
12795fcfab6eSJohn Baldwin  * Verify that the expected LWP events are reported for a multithreaded
12805fcfab6eSJohn Baldwin  * process that calls execve(2).
12815fcfab6eSJohn Baldwin  */
12825fcfab6eSJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__lwp_events_exec);
12835fcfab6eSJohn Baldwin ATF_TC_BODY(ptrace__lwp_events_exec, tc)
12845fcfab6eSJohn Baldwin {
12855fcfab6eSJohn Baldwin 	struct ptrace_lwpinfo pl;
12865fcfab6eSJohn Baldwin 	pid_t fpid, wpid;
12875fcfab6eSJohn Baldwin 	lwpid_t lwps[2];
12885fcfab6eSJohn Baldwin 	int status;
12895fcfab6eSJohn Baldwin 
12905fcfab6eSJohn Baldwin 	ATF_REQUIRE((fpid = fork()) != -1);
12915fcfab6eSJohn Baldwin 	if (fpid == 0) {
12925fcfab6eSJohn Baldwin 		trace_me();
12935fcfab6eSJohn Baldwin 		exec_thread_main();
12945fcfab6eSJohn Baldwin 	}
12955fcfab6eSJohn Baldwin 
12965fcfab6eSJohn Baldwin 	/* The first wait() should report the stop from SIGSTOP. */
12975fcfab6eSJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
12985fcfab6eSJohn Baldwin 	ATF_REQUIRE(wpid == fpid);
12995fcfab6eSJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
13005fcfab6eSJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
13015fcfab6eSJohn Baldwin 
13025fcfab6eSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
13035fcfab6eSJohn Baldwin 	    sizeof(pl)) != -1);
13045fcfab6eSJohn Baldwin 	lwps[0] = pl.pl_lwpid;
13055fcfab6eSJohn Baldwin 
13065fcfab6eSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
13075fcfab6eSJohn Baldwin 
13085fcfab6eSJohn Baldwin 	/* Continue the child ignoring the SIGSTOP. */
13095fcfab6eSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
13105fcfab6eSJohn Baldwin 
13115fcfab6eSJohn Baldwin 	/* The first event should be for the child thread's birth. */
13125fcfab6eSJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
13135fcfab6eSJohn Baldwin 	ATF_REQUIRE(wpid == fpid);
13145fcfab6eSJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
13155fcfab6eSJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
13165fcfab6eSJohn Baldwin 
13175fcfab6eSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
13185fcfab6eSJohn Baldwin 	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
13195fcfab6eSJohn Baldwin 	    (PL_FLAG_BORN | PL_FLAG_SCX));
13205fcfab6eSJohn Baldwin 	ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
13215fcfab6eSJohn Baldwin 	lwps[1] = pl.pl_lwpid;
13225fcfab6eSJohn Baldwin 
13235fcfab6eSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
13245fcfab6eSJohn Baldwin 
13255fcfab6eSJohn Baldwin 	/*
13265fcfab6eSJohn Baldwin 	 * The next event should be for the main thread's death due to
13275fcfab6eSJohn Baldwin 	 * single threading from execve().
13285fcfab6eSJohn Baldwin 	 */
13295fcfab6eSJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
13305fcfab6eSJohn Baldwin 	ATF_REQUIRE(wpid == fpid);
13315fcfab6eSJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
13325fcfab6eSJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
13335fcfab6eSJohn Baldwin 
13345fcfab6eSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
13355fcfab6eSJohn Baldwin 	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
13365fcfab6eSJohn Baldwin 	    (PL_FLAG_EXITED));
13375fcfab6eSJohn Baldwin 	ATF_REQUIRE(pl.pl_lwpid == lwps[0]);
13385fcfab6eSJohn Baldwin 
13395fcfab6eSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
13405fcfab6eSJohn Baldwin 
13415fcfab6eSJohn Baldwin 	/* The next event should be for the child process's exec. */
13425fcfab6eSJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
13435fcfab6eSJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
13445fcfab6eSJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
13455fcfab6eSJohn Baldwin 
13465fcfab6eSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
13475fcfab6eSJohn Baldwin 	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
13485fcfab6eSJohn Baldwin 	    (PL_FLAG_EXEC | PL_FLAG_SCX));
13495fcfab6eSJohn Baldwin 	ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
13505fcfab6eSJohn Baldwin 
13515fcfab6eSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
13525fcfab6eSJohn Baldwin 
13535fcfab6eSJohn Baldwin 	/* The last event should be for the child process's exit. */
13545fcfab6eSJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
13555fcfab6eSJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
13565fcfab6eSJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 0);
13575fcfab6eSJohn Baldwin 
13585fcfab6eSJohn Baldwin 	wpid = wait(&status);
13595fcfab6eSJohn Baldwin 	ATF_REQUIRE(wpid == -1);
13605fcfab6eSJohn Baldwin 	ATF_REQUIRE(errno == ECHILD);
13615fcfab6eSJohn Baldwin }
13625fcfab6eSJohn Baldwin 
13633340c45bSJohn Baldwin static void
13643340c45bSJohn Baldwin handler(int sig __unused)
13653340c45bSJohn Baldwin {
13663340c45bSJohn Baldwin }
13673340c45bSJohn Baldwin 
13683340c45bSJohn Baldwin static void
13693340c45bSJohn Baldwin signal_main(void)
13703340c45bSJohn Baldwin {
13713340c45bSJohn Baldwin 
13723340c45bSJohn Baldwin 	signal(SIGINFO, handler);
13733340c45bSJohn Baldwin 	raise(SIGINFO);
13743340c45bSJohn Baldwin 	exit(0);
13753340c45bSJohn Baldwin }
13763340c45bSJohn Baldwin 
13773340c45bSJohn Baldwin /*
13783340c45bSJohn Baldwin  * Verify that the expected ptrace event is reported for a signal.
13793340c45bSJohn Baldwin  */
13803340c45bSJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__siginfo);
13813340c45bSJohn Baldwin ATF_TC_BODY(ptrace__siginfo, tc)
13823340c45bSJohn Baldwin {
13833340c45bSJohn Baldwin 	struct ptrace_lwpinfo pl;
13843340c45bSJohn Baldwin 	pid_t fpid, wpid;
13853340c45bSJohn Baldwin 	int status;
13863340c45bSJohn Baldwin 
13873340c45bSJohn Baldwin 	ATF_REQUIRE((fpid = fork()) != -1);
13883340c45bSJohn Baldwin 	if (fpid == 0) {
13893340c45bSJohn Baldwin 		trace_me();
13903340c45bSJohn Baldwin 		signal_main();
13913340c45bSJohn Baldwin 	}
13923340c45bSJohn Baldwin 
13933340c45bSJohn Baldwin 	/* The first wait() should report the stop from SIGSTOP. */
13943340c45bSJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
13953340c45bSJohn Baldwin 	ATF_REQUIRE(wpid == fpid);
13963340c45bSJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
13973340c45bSJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
13983340c45bSJohn Baldwin 
13993340c45bSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
14003340c45bSJohn Baldwin 
14013340c45bSJohn Baldwin 	/* The next event should be for the SIGINFO. */
14023340c45bSJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
14033340c45bSJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
14043340c45bSJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGINFO);
14053340c45bSJohn Baldwin 
14063340c45bSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
14073340c45bSJohn Baldwin 	ATF_REQUIRE(pl.pl_event == PL_EVENT_SIGNAL);
14083340c45bSJohn Baldwin 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
14093340c45bSJohn Baldwin 	ATF_REQUIRE(pl.pl_siginfo.si_code == SI_LWP);
14103340c45bSJohn Baldwin 	ATF_REQUIRE(pl.pl_siginfo.si_pid == wpid);
14113340c45bSJohn Baldwin 
14123340c45bSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
14133340c45bSJohn Baldwin 
14143340c45bSJohn Baldwin 	/* The last event should be for the child process's exit. */
14153340c45bSJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
14163340c45bSJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
14173340c45bSJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 0);
14183340c45bSJohn Baldwin 
14193340c45bSJohn Baldwin 	wpid = wait(&status);
14203340c45bSJohn Baldwin 	ATF_REQUIRE(wpid == -1);
14213340c45bSJohn Baldwin 	ATF_REQUIRE(errno == ECHILD);
14223340c45bSJohn Baldwin }
14233340c45bSJohn Baldwin 
14248d570f64SJohn Baldwin /*
14258d570f64SJohn Baldwin  * Verify that the expected ptrace events are reported for PTRACE_EXEC.
14268d570f64SJohn Baldwin  */
14278d570f64SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_disable);
14288d570f64SJohn Baldwin ATF_TC_BODY(ptrace__ptrace_exec_disable, tc)
14298d570f64SJohn Baldwin {
14308d570f64SJohn Baldwin 	pid_t fpid, wpid;
14318d570f64SJohn Baldwin 	int events, status;
14328d570f64SJohn Baldwin 
14338d570f64SJohn Baldwin 	ATF_REQUIRE((fpid = fork()) != -1);
14348d570f64SJohn Baldwin 	if (fpid == 0) {
14358d570f64SJohn Baldwin 		trace_me();
14368d570f64SJohn Baldwin 		exec_thread(NULL);
14378d570f64SJohn Baldwin 	}
14388d570f64SJohn Baldwin 
14398d570f64SJohn Baldwin 	/* The first wait() should report the stop from SIGSTOP. */
14408d570f64SJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
14418d570f64SJohn Baldwin 	ATF_REQUIRE(wpid == fpid);
14428d570f64SJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
14438d570f64SJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
14448d570f64SJohn Baldwin 
14458d570f64SJohn Baldwin 	events = 0;
14468d570f64SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
14478d570f64SJohn Baldwin 	    sizeof(events)) == 0);
14488d570f64SJohn Baldwin 
14498d570f64SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
14508d570f64SJohn Baldwin 
14518d570f64SJohn Baldwin 	/* Should get one event at exit. */
14528d570f64SJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
14538d570f64SJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
14548d570f64SJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 0);
14558d570f64SJohn Baldwin 
14568d570f64SJohn Baldwin 	wpid = wait(&status);
14578d570f64SJohn Baldwin 	ATF_REQUIRE(wpid == -1);
14588d570f64SJohn Baldwin 	ATF_REQUIRE(errno == ECHILD);
14598d570f64SJohn Baldwin }
14608d570f64SJohn Baldwin 
14618d570f64SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_enable);
14628d570f64SJohn Baldwin ATF_TC_BODY(ptrace__ptrace_exec_enable, tc)
14638d570f64SJohn Baldwin {
14648d570f64SJohn Baldwin 	struct ptrace_lwpinfo pl;
14658d570f64SJohn Baldwin 	pid_t fpid, wpid;
14668d570f64SJohn Baldwin 	int events, status;
14678d570f64SJohn Baldwin 
14688d570f64SJohn Baldwin 	ATF_REQUIRE((fpid = fork()) != -1);
14698d570f64SJohn Baldwin 	if (fpid == 0) {
14708d570f64SJohn Baldwin 		trace_me();
14718d570f64SJohn Baldwin 		exec_thread(NULL);
14728d570f64SJohn Baldwin 	}
14738d570f64SJohn Baldwin 
14748d570f64SJohn Baldwin 	/* The first wait() should report the stop from SIGSTOP. */
14758d570f64SJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
14768d570f64SJohn Baldwin 	ATF_REQUIRE(wpid == fpid);
14778d570f64SJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
14788d570f64SJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
14798d570f64SJohn Baldwin 
14808d570f64SJohn Baldwin 	events = PTRACE_EXEC;
14818d570f64SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
14828d570f64SJohn Baldwin 	    sizeof(events)) == 0);
14838d570f64SJohn Baldwin 
14848d570f64SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
14858d570f64SJohn Baldwin 
14868d570f64SJohn Baldwin 	/* The next event should be for the child process's exec. */
14878d570f64SJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
14888d570f64SJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
14898d570f64SJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
14908d570f64SJohn Baldwin 
14918d570f64SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
14928d570f64SJohn Baldwin 	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
14938d570f64SJohn Baldwin 	    (PL_FLAG_EXEC | PL_FLAG_SCX));
14948d570f64SJohn Baldwin 
14958d570f64SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
14968d570f64SJohn Baldwin 
14978d570f64SJohn Baldwin 	/* The last event should be for the child process's exit. */
14988d570f64SJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
14998d570f64SJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
15008d570f64SJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 0);
15018d570f64SJohn Baldwin 
15028d570f64SJohn Baldwin 	wpid = wait(&status);
15038d570f64SJohn Baldwin 	ATF_REQUIRE(wpid == -1);
15048d570f64SJohn Baldwin 	ATF_REQUIRE(errno == ECHILD);
15058d570f64SJohn Baldwin }
15068d570f64SJohn Baldwin 
15078d570f64SJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__event_mask);
15088d570f64SJohn Baldwin ATF_TC_BODY(ptrace__event_mask, tc)
15098d570f64SJohn Baldwin {
15108d570f64SJohn Baldwin 	pid_t fpid, wpid;
15118d570f64SJohn Baldwin 	int events, status;
15128d570f64SJohn Baldwin 
15138d570f64SJohn Baldwin 	ATF_REQUIRE((fpid = fork()) != -1);
15148d570f64SJohn Baldwin 	if (fpid == 0) {
15158d570f64SJohn Baldwin 		trace_me();
15168d570f64SJohn Baldwin 		exit(0);
15178d570f64SJohn Baldwin 	}
15188d570f64SJohn Baldwin 
15198d570f64SJohn Baldwin 	/* The first wait() should report the stop from SIGSTOP. */
15208d570f64SJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
15218d570f64SJohn Baldwin 	ATF_REQUIRE(wpid == fpid);
15228d570f64SJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
15238d570f64SJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
15248d570f64SJohn Baldwin 
15258d570f64SJohn Baldwin 	/* PT_FOLLOW_FORK should toggle the state of PTRACE_FORK. */
15268d570f64SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 1) != -1);
15278d570f64SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
15288d570f64SJohn Baldwin 	    sizeof(events)) == 0);
15298d570f64SJohn Baldwin 	ATF_REQUIRE(events & PTRACE_FORK);
15308d570f64SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 0) != -1);
15318d570f64SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
15328d570f64SJohn Baldwin 	    sizeof(events)) == 0);
15338d570f64SJohn Baldwin 	ATF_REQUIRE(!(events & PTRACE_FORK));
15348d570f64SJohn Baldwin 
15358d570f64SJohn Baldwin 	/* PT_LWP_EVENTS should toggle the state of PTRACE_LWP. */
15368d570f64SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 1) != -1);
15378d570f64SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
15388d570f64SJohn Baldwin 	    sizeof(events)) == 0);
15398d570f64SJohn Baldwin 	ATF_REQUIRE(events & PTRACE_LWP);
15408d570f64SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 0) != -1);
15418d570f64SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
15428d570f64SJohn Baldwin 	    sizeof(events)) == 0);
15438d570f64SJohn Baldwin 	ATF_REQUIRE(!(events & PTRACE_LWP));
15448d570f64SJohn Baldwin 
15458d570f64SJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
15468d570f64SJohn Baldwin 
15478d570f64SJohn Baldwin 	/* Should get one event at exit. */
15488d570f64SJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
15498d570f64SJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
15508d570f64SJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 0);
15518d570f64SJohn Baldwin 
15528d570f64SJohn Baldwin 	wpid = wait(&status);
15538d570f64SJohn Baldwin 	ATF_REQUIRE(wpid == -1);
15548d570f64SJohn Baldwin 	ATF_REQUIRE(errno == ECHILD);
15558d570f64SJohn Baldwin }
15568d570f64SJohn Baldwin 
1557fc4f075aSJohn Baldwin /*
1558fc4f075aSJohn Baldwin  * Verify that the expected ptrace events are reported for PTRACE_VFORK.
1559fc4f075aSJohn Baldwin  */
1560fc4f075aSJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork);
1561fc4f075aSJohn Baldwin ATF_TC_BODY(ptrace__ptrace_vfork, tc)
1562fc4f075aSJohn Baldwin {
1563fc4f075aSJohn Baldwin 	struct ptrace_lwpinfo pl;
1564fc4f075aSJohn Baldwin 	pid_t fpid, wpid;
1565fc4f075aSJohn Baldwin 	int events, status;
1566fc4f075aSJohn Baldwin 
1567fc4f075aSJohn Baldwin 	ATF_REQUIRE((fpid = fork()) != -1);
1568fc4f075aSJohn Baldwin 	if (fpid == 0) {
1569fc4f075aSJohn Baldwin 		trace_me();
1570fc4f075aSJohn Baldwin 		follow_fork_parent(true);
1571fc4f075aSJohn Baldwin 	}
1572fc4f075aSJohn Baldwin 
1573fc4f075aSJohn Baldwin 	/* The first wait() should report the stop from SIGSTOP. */
1574fc4f075aSJohn Baldwin 	wpid = waitpid(fpid, &status, 0);
1575fc4f075aSJohn Baldwin 	ATF_REQUIRE(wpid == fpid);
1576fc4f075aSJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
1577fc4f075aSJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1578fc4f075aSJohn Baldwin 
1579fc4f075aSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1580fc4f075aSJohn Baldwin 	    sizeof(events)) == 0);
1581fc4f075aSJohn Baldwin 	events |= PTRACE_VFORK;
1582fc4f075aSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1583fc4f075aSJohn Baldwin 	    sizeof(events)) == 0);
1584fc4f075aSJohn Baldwin 
1585fc4f075aSJohn Baldwin 	/* Continue the child ignoring the SIGSTOP. */
1586fc4f075aSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1587fc4f075aSJohn Baldwin 
1588fc4f075aSJohn Baldwin 	/* The next event should report the end of the vfork. */
1589fc4f075aSJohn Baldwin 	wpid = wait(&status);
1590fc4f075aSJohn Baldwin 	ATF_REQUIRE(wpid == fpid);
1591fc4f075aSJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
1592fc4f075aSJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1593fc4f075aSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1594fc4f075aSJohn Baldwin 	ATF_REQUIRE((pl.pl_flags & PL_FLAG_VFORK_DONE) != 0);
1595fc4f075aSJohn Baldwin 
1596fc4f075aSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1597fc4f075aSJohn Baldwin 
1598fc4f075aSJohn Baldwin 	wpid = wait(&status);
1599fc4f075aSJohn Baldwin 	ATF_REQUIRE(wpid == fpid);
1600fc4f075aSJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
1601fc4f075aSJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1602fc4f075aSJohn Baldwin 
1603fc4f075aSJohn Baldwin 	wpid = wait(&status);
1604fc4f075aSJohn Baldwin 	ATF_REQUIRE(wpid == -1);
1605fc4f075aSJohn Baldwin 	ATF_REQUIRE(errno == ECHILD);
1606fc4f075aSJohn Baldwin }
1607fc4f075aSJohn Baldwin 
1608fc4f075aSJohn Baldwin ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork_follow);
1609fc4f075aSJohn Baldwin ATF_TC_BODY(ptrace__ptrace_vfork_follow, tc)
1610fc4f075aSJohn Baldwin {
1611fc4f075aSJohn Baldwin 	struct ptrace_lwpinfo pl[2];
1612fc4f075aSJohn Baldwin 	pid_t children[2], fpid, wpid;
1613fc4f075aSJohn Baldwin 	int events, status;
1614fc4f075aSJohn Baldwin 
1615fc4f075aSJohn Baldwin 	ATF_REQUIRE((fpid = fork()) != -1);
1616fc4f075aSJohn Baldwin 	if (fpid == 0) {
1617fc4f075aSJohn Baldwin 		trace_me();
1618fc4f075aSJohn Baldwin 		follow_fork_parent(true);
1619fc4f075aSJohn Baldwin 	}
1620fc4f075aSJohn Baldwin 
1621fc4f075aSJohn Baldwin 	/* Parent process. */
1622fc4f075aSJohn Baldwin 	children[0] = fpid;
1623fc4f075aSJohn Baldwin 
1624fc4f075aSJohn Baldwin 	/* The first wait() should report the stop from SIGSTOP. */
1625fc4f075aSJohn Baldwin 	wpid = waitpid(children[0], &status, 0);
1626fc4f075aSJohn Baldwin 	ATF_REQUIRE(wpid == children[0]);
1627fc4f075aSJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
1628fc4f075aSJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1629fc4f075aSJohn Baldwin 
1630fc4f075aSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, children[0], (caddr_t)&events,
1631fc4f075aSJohn Baldwin 	    sizeof(events)) == 0);
1632fc4f075aSJohn Baldwin 	events |= PTRACE_FORK | PTRACE_VFORK;
1633fc4f075aSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, children[0], (caddr_t)&events,
1634fc4f075aSJohn Baldwin 	    sizeof(events)) == 0);
1635fc4f075aSJohn Baldwin 
1636fc4f075aSJohn Baldwin 	/* Continue the child ignoring the SIGSTOP. */
1637fc4f075aSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1638fc4f075aSJohn Baldwin 
1639fc4f075aSJohn Baldwin 	/* Wait for both halves of the fork event to get reported. */
1640fc4f075aSJohn Baldwin 	children[1] = handle_fork_events(children[0], pl);
1641fc4f075aSJohn Baldwin 	ATF_REQUIRE(children[1] > 0);
1642fc4f075aSJohn Baldwin 
1643fc4f075aSJohn Baldwin 	ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORKED) != 0);
1644fc4f075aSJohn Baldwin 
1645fc4f075aSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1646fc4f075aSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1647fc4f075aSJohn Baldwin 
1648fc4f075aSJohn Baldwin 	/*
1649fc4f075aSJohn Baldwin 	 * The child can't exit until the grandchild reports status, so the
1650fc4f075aSJohn Baldwin 	 * grandchild should report its exit first to the debugger.
1651fc4f075aSJohn Baldwin 	 */
1652fc4f075aSJohn Baldwin 	wpid = waitpid(children[1], &status, 0);
1653fc4f075aSJohn Baldwin 	ATF_REQUIRE(wpid == children[1]);
1654fc4f075aSJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
1655fc4f075aSJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 2);
1656fc4f075aSJohn Baldwin 
1657fc4f075aSJohn Baldwin 	/*
1658fc4f075aSJohn Baldwin 	 * The child should report it's vfork() completion before it
1659fc4f075aSJohn Baldwin 	 * exits.
1660fc4f075aSJohn Baldwin 	 */
1661fc4f075aSJohn Baldwin 	wpid = wait(&status);
1662fc4f075aSJohn Baldwin 	ATF_REQUIRE(wpid == children[0]);
1663fc4f075aSJohn Baldwin 	ATF_REQUIRE(WIFSTOPPED(status));
1664fc4f075aSJohn Baldwin 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1665fc4f075aSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl[0], sizeof(pl[0])) !=
1666fc4f075aSJohn Baldwin 	    -1);
1667fc4f075aSJohn Baldwin 	ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORK_DONE) != 0);
1668fc4f075aSJohn Baldwin 
1669fc4f075aSJohn Baldwin 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1670fc4f075aSJohn Baldwin 
1671fc4f075aSJohn Baldwin 	wpid = wait(&status);
1672fc4f075aSJohn Baldwin 	ATF_REQUIRE(wpid == children[0]);
1673fc4f075aSJohn Baldwin 	ATF_REQUIRE(WIFEXITED(status));
1674fc4f075aSJohn Baldwin 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1675fc4f075aSJohn Baldwin 
1676fc4f075aSJohn Baldwin 	wpid = wait(&status);
1677fc4f075aSJohn Baldwin 	ATF_REQUIRE(wpid == -1);
1678fc4f075aSJohn Baldwin 	ATF_REQUIRE(errno == ECHILD);
1679fc4f075aSJohn Baldwin }
1680fc4f075aSJohn Baldwin 
168182a4538fSEric Badger /*
1682*e2ebfbbfSEric Badger  * XXX: There's nothing inherently platform specific about this test, however a
1683*e2ebfbbfSEric Badger  * userspace visible breakpoint() is a prerequisite.
1684*e2ebfbbfSEric Badger  */
1685*e2ebfbbfSEric Badger  #if defined(__amd64__) || defined(__i386__) || defined(__sparc64__)
1686*e2ebfbbfSEric Badger /*
168782a4538fSEric Badger  * Verify that no more events are reported after PT_KILL except for the
168882a4538fSEric Badger  * process exit when stopped due to a breakpoint trap.
168982a4538fSEric Badger  */
169082a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_breakpoint);
169182a4538fSEric Badger ATF_TC_BODY(ptrace__PT_KILL_breakpoint, tc)
169282a4538fSEric Badger {
169382a4538fSEric Badger 	pid_t fpid, wpid;
169482a4538fSEric Badger 	int status;
169582a4538fSEric Badger 
169682a4538fSEric Badger 	ATF_REQUIRE((fpid = fork()) != -1);
169782a4538fSEric Badger 	if (fpid == 0) {
169882a4538fSEric Badger 		trace_me();
16999e0d1159SEric Badger 		breakpoint();
170082a4538fSEric Badger 		exit(1);
170182a4538fSEric Badger 	}
170282a4538fSEric Badger 
170382a4538fSEric Badger 	/* The first wait() should report the stop from SIGSTOP. */
170482a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
170582a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
170682a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
170782a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
170882a4538fSEric Badger 
170982a4538fSEric Badger 	/* Continue the child ignoring the SIGSTOP. */
171082a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
171182a4538fSEric Badger 
171282a4538fSEric Badger 	/* The second wait() should report hitting the breakpoint. */
171382a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
171482a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
171582a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
171682a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
171782a4538fSEric Badger 
171882a4538fSEric Badger 	/* Kill the child process. */
171982a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
172082a4538fSEric Badger 
172182a4538fSEric Badger 	/* The last wait() should report the SIGKILL. */
172282a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
172382a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
172482a4538fSEric Badger 	ATF_REQUIRE(WIFSIGNALED(status));
172582a4538fSEric Badger 	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
172682a4538fSEric Badger 
172782a4538fSEric Badger 	wpid = wait(&status);
172882a4538fSEric Badger 	ATF_REQUIRE(wpid == -1);
172982a4538fSEric Badger 	ATF_REQUIRE(errno == ECHILD);
173082a4538fSEric Badger }
1731*e2ebfbbfSEric Badger #endif /* defined(__amd64__) || defined(__i386__) || defined(__sparc64__) */
173282a4538fSEric Badger 
173382a4538fSEric Badger /*
173482a4538fSEric Badger  * Verify that no more events are reported after PT_KILL except for the
173582a4538fSEric Badger  * process exit when stopped inside of a system call.
173682a4538fSEric Badger  */
173782a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_system_call);
173882a4538fSEric Badger ATF_TC_BODY(ptrace__PT_KILL_system_call, tc)
173982a4538fSEric Badger {
174082a4538fSEric Badger 	struct ptrace_lwpinfo pl;
174182a4538fSEric Badger 	pid_t fpid, wpid;
174282a4538fSEric Badger 	int status;
174382a4538fSEric Badger 
174482a4538fSEric Badger 	ATF_REQUIRE((fpid = fork()) != -1);
174582a4538fSEric Badger 	if (fpid == 0) {
174682a4538fSEric Badger 		trace_me();
174782a4538fSEric Badger 		getpid();
174882a4538fSEric Badger 		exit(1);
174982a4538fSEric Badger 	}
175082a4538fSEric Badger 
175182a4538fSEric Badger 	/* The first wait() should report the stop from SIGSTOP. */
175282a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
175382a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
175482a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
175582a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
175682a4538fSEric Badger 
175782a4538fSEric Badger 	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
175882a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
175982a4538fSEric Badger 
176082a4538fSEric Badger 	/* The second wait() should report a system call entry for getpid(). */
176182a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
176282a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
176382a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
176482a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
176582a4538fSEric Badger 
176682a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
176782a4538fSEric Badger 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
176882a4538fSEric Badger 
176982a4538fSEric Badger 	/* Kill the child process. */
177082a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
177182a4538fSEric Badger 
177282a4538fSEric Badger 	/* The last wait() should report the SIGKILL. */
177382a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
177482a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
177582a4538fSEric Badger 	ATF_REQUIRE(WIFSIGNALED(status));
177682a4538fSEric Badger 	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
177782a4538fSEric Badger 
177882a4538fSEric Badger 	wpid = wait(&status);
177982a4538fSEric Badger 	ATF_REQUIRE(wpid == -1);
178082a4538fSEric Badger 	ATF_REQUIRE(errno == ECHILD);
178182a4538fSEric Badger }
178282a4538fSEric Badger 
178382a4538fSEric Badger /*
178482a4538fSEric Badger  * Verify that no more events are reported after PT_KILL except for the
178582a4538fSEric Badger  * process exit when killing a multithreaded process.
178682a4538fSEric Badger  */
178782a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_threads);
178882a4538fSEric Badger ATF_TC_BODY(ptrace__PT_KILL_threads, tc)
178982a4538fSEric Badger {
179082a4538fSEric Badger 	struct ptrace_lwpinfo pl;
179182a4538fSEric Badger 	pid_t fpid, wpid;
179282a4538fSEric Badger 	lwpid_t main_lwp;
179382a4538fSEric Badger 	int status;
179482a4538fSEric Badger 
179582a4538fSEric Badger 	ATF_REQUIRE((fpid = fork()) != -1);
179682a4538fSEric Badger 	if (fpid == 0) {
179782a4538fSEric Badger 		trace_me();
179882a4538fSEric Badger 		simple_thread_main();
179982a4538fSEric Badger 	}
180082a4538fSEric Badger 
180182a4538fSEric Badger 	/* The first wait() should report the stop from SIGSTOP. */
180282a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
180382a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
180482a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
180582a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
180682a4538fSEric Badger 
180782a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
180882a4538fSEric Badger 	    sizeof(pl)) != -1);
180982a4538fSEric Badger 	main_lwp = pl.pl_lwpid;
181082a4538fSEric Badger 
181182a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
181282a4538fSEric Badger 
181382a4538fSEric Badger 	/* Continue the child ignoring the SIGSTOP. */
181482a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
181582a4538fSEric Badger 
181682a4538fSEric Badger 	/* The first event should be for the child thread's birth. */
181782a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
181882a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
181982a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
182082a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
182182a4538fSEric Badger 
182282a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
182382a4538fSEric Badger 	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
182482a4538fSEric Badger 	    (PL_FLAG_BORN | PL_FLAG_SCX));
182582a4538fSEric Badger 	ATF_REQUIRE(pl.pl_lwpid != main_lwp);
182682a4538fSEric Badger 
182782a4538fSEric Badger 	/* Kill the child process. */
182882a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
182982a4538fSEric Badger 
183082a4538fSEric Badger 	/* The last wait() should report the SIGKILL. */
183182a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
183282a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
183382a4538fSEric Badger 	ATF_REQUIRE(WIFSIGNALED(status));
183482a4538fSEric Badger 	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
183582a4538fSEric Badger 
183682a4538fSEric Badger 	wpid = wait(&status);
183782a4538fSEric Badger 	ATF_REQUIRE(wpid == -1);
183882a4538fSEric Badger 	ATF_REQUIRE(errno == ECHILD);
183982a4538fSEric Badger }
184082a4538fSEric Badger 
184182a4538fSEric Badger static void *
184282a4538fSEric Badger mask_usr1_thread(void *arg)
184382a4538fSEric Badger {
184482a4538fSEric Badger 	pthread_barrier_t *pbarrier;
184582a4538fSEric Badger 	sigset_t sigmask;
184682a4538fSEric Badger 
184782a4538fSEric Badger 	pbarrier = (pthread_barrier_t*)arg;
184882a4538fSEric Badger 
184982a4538fSEric Badger 	sigemptyset(&sigmask);
185082a4538fSEric Badger 	sigaddset(&sigmask, SIGUSR1);
185182a4538fSEric Badger 	CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
185282a4538fSEric Badger 
185382a4538fSEric Badger 	/* Sync up with other thread after sigmask updated. */
185482a4538fSEric Badger 	pthread_barrier_wait(pbarrier);
185582a4538fSEric Badger 
185682a4538fSEric Badger 	for (;;)
185782a4538fSEric Badger 		sleep(60);
185882a4538fSEric Badger 
185982a4538fSEric Badger 	return (NULL);
186082a4538fSEric Badger }
186182a4538fSEric Badger 
186282a4538fSEric Badger /*
186382a4538fSEric Badger  * Verify that the SIGKILL from PT_KILL takes priority over other signals
186482a4538fSEric Badger  * and prevents spurious stops due to those other signals.
186582a4538fSEric Badger  */
186682a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_competing_signal);
186782a4538fSEric Badger ATF_TC_BODY(ptrace__PT_KILL_competing_signal, tc)
186882a4538fSEric Badger {
186982a4538fSEric Badger 	pid_t fpid, wpid;
187082a4538fSEric Badger 	int status;
187182a4538fSEric Badger 	cpuset_t setmask;
187282a4538fSEric Badger 	pthread_t t;
187382a4538fSEric Badger 	pthread_barrier_t barrier;
187482a4538fSEric Badger 
187582a4538fSEric Badger 	ATF_REQUIRE((fpid = fork()) != -1);
187682a4538fSEric Badger 	if (fpid == 0) {
187782a4538fSEric Badger 		/*
187882a4538fSEric Badger 		 * Bind to one CPU so only one thread at a time will run. This
187982a4538fSEric Badger 		 * test expects that the first thread created (the main thread)
188082a4538fSEric Badger 		 * will be unsuspended first and will block the second thread
188182a4538fSEric Badger 		 * from running.
188282a4538fSEric Badger 		 */
188382a4538fSEric Badger 		CPU_ZERO(&setmask);
188482a4538fSEric Badger 		CPU_SET(0, &setmask);
188582a4538fSEric Badger 		cpusetid_t setid;
188682a4538fSEric Badger 		CHILD_REQUIRE(cpuset(&setid) == 0);
188782a4538fSEric Badger 		CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
188882a4538fSEric Badger 		    CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
188982a4538fSEric Badger 
189082a4538fSEric Badger 		CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
189182a4538fSEric Badger 
189282a4538fSEric Badger 		CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
189382a4538fSEric Badger 		    (void*)&barrier) == 0);
189482a4538fSEric Badger 
189582a4538fSEric Badger 		sigset_t sigmask;
189682a4538fSEric Badger 		sigemptyset(&sigmask);
189782a4538fSEric Badger 		sigaddset(&sigmask, SIGUSR2);
189882a4538fSEric Badger 		CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
189982a4538fSEric Badger 
190082a4538fSEric Badger 		/* Sync up with other thread after sigmask updated. */
190182a4538fSEric Badger 		pthread_barrier_wait(&barrier);
190282a4538fSEric Badger 
190382a4538fSEric Badger 		trace_me();
190482a4538fSEric Badger 
190582a4538fSEric Badger 		for (;;)
190682a4538fSEric Badger 			sleep(60);
190782a4538fSEric Badger 
190882a4538fSEric Badger 		exit(1);
190982a4538fSEric Badger 	}
191082a4538fSEric Badger 
191182a4538fSEric Badger 	/* The first wait() should report the stop from SIGSTOP. */
191282a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
191382a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
191482a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
191582a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
191682a4538fSEric Badger 
191782a4538fSEric Badger 	/* Continue the child ignoring the SIGSTOP. */
191882a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
191982a4538fSEric Badger 
192082a4538fSEric Badger 	/* Send a signal that only the second thread can handle. */
192182a4538fSEric Badger 	ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
192282a4538fSEric Badger 
192382a4538fSEric Badger 	/* The second wait() should report the SIGUSR2. */
192482a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
192582a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
192682a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
192782a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
192882a4538fSEric Badger 
192982a4538fSEric Badger 	/* Send a signal that only the first thread can handle. */
193082a4538fSEric Badger 	ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
193182a4538fSEric Badger 
193282a4538fSEric Badger 	/* Replace the SIGUSR2 with a kill. */
193382a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
193482a4538fSEric Badger 
193582a4538fSEric Badger 	/* The last wait() should report the SIGKILL (not the SIGUSR signal). */
193682a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
193782a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
193882a4538fSEric Badger 	ATF_REQUIRE(WIFSIGNALED(status));
193982a4538fSEric Badger 	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
194082a4538fSEric Badger 
194182a4538fSEric Badger 	wpid = wait(&status);
194282a4538fSEric Badger 	ATF_REQUIRE(wpid == -1);
194382a4538fSEric Badger 	ATF_REQUIRE(errno == ECHILD);
194482a4538fSEric Badger }
194582a4538fSEric Badger 
194682a4538fSEric Badger /*
194782a4538fSEric Badger  * Verify that the SIGKILL from PT_KILL takes priority over other stop events
194882a4538fSEric Badger  * and prevents spurious stops caused by those events.
194982a4538fSEric Badger  */
195082a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_competing_stop);
195182a4538fSEric Badger ATF_TC_BODY(ptrace__PT_KILL_competing_stop, tc)
195282a4538fSEric Badger {
195382a4538fSEric Badger 	pid_t fpid, wpid;
195482a4538fSEric Badger 	int status, i;
195582a4538fSEric Badger 	cpuset_t setmask;
195682a4538fSEric Badger 	pthread_t t;
195782a4538fSEric Badger 	pthread_barrier_t barrier;
195882a4538fSEric Badger 	lwpid_t main_lwp;
195982a4538fSEric Badger 	struct ptrace_lwpinfo pl;
196082a4538fSEric Badger 
196182a4538fSEric Badger 	ATF_REQUIRE((fpid = fork()) != -1);
196282a4538fSEric Badger 	if (fpid == 0) {
196382a4538fSEric Badger 		trace_me();
196482a4538fSEric Badger 
196582a4538fSEric Badger 		/*
196682a4538fSEric Badger 		 * Bind to one CPU so only one thread at a time will run. This
196782a4538fSEric Badger 		 * test expects that the first thread created (the main thread)
196882a4538fSEric Badger 		 * will be unsuspended first and will block the second thread
196982a4538fSEric Badger 		 * from running.
197082a4538fSEric Badger 		 */
197182a4538fSEric Badger 		CPU_ZERO(&setmask);
197282a4538fSEric Badger 		CPU_SET(0, &setmask);
197382a4538fSEric Badger 		cpusetid_t setid;
197482a4538fSEric Badger 		CHILD_REQUIRE(cpuset(&setid) == 0);
197582a4538fSEric Badger 		CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
197682a4538fSEric Badger 		    CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
197782a4538fSEric Badger 
197882a4538fSEric Badger 		CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
197982a4538fSEric Badger 
198082a4538fSEric Badger 		CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
198182a4538fSEric Badger 		    (void*)&barrier) == 0);
198282a4538fSEric Badger 
198382a4538fSEric Badger 		sigset_t sigmask;
198482a4538fSEric Badger 		sigemptyset(&sigmask);
198582a4538fSEric Badger 		sigaddset(&sigmask, SIGUSR2);
198682a4538fSEric Badger 		CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
198782a4538fSEric Badger 
198882a4538fSEric Badger 		/* Sync up with other thread after sigmask updated. */
198982a4538fSEric Badger 		pthread_barrier_wait(&barrier);
199082a4538fSEric Badger 
199182a4538fSEric Badger 		/* Sync up with the test before doing the getpid(). */
199282a4538fSEric Badger 		raise(SIGSTOP);
199382a4538fSEric Badger 
199482a4538fSEric Badger 		getpid();
199582a4538fSEric Badger 		exit(1);
199682a4538fSEric Badger 	}
199782a4538fSEric Badger 
199882a4538fSEric Badger 	/* The first wait() should report the stop from SIGSTOP. */
199982a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
200082a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
200182a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
200282a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
200382a4538fSEric Badger 
200482a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
200582a4538fSEric Badger 	main_lwp = pl.pl_lwpid;
200682a4538fSEric Badger 
200782a4538fSEric Badger 	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
200882a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
200982a4538fSEric Badger 
201082a4538fSEric Badger 	/*
201182a4538fSEric Badger 	 * Continue until child is done with setup, which is indicated with
201282a4538fSEric Badger 	 * SIGSTOP. Ignore system calls in the meantime.
201382a4538fSEric Badger 	 */
201482a4538fSEric Badger 	for (;;) {
201582a4538fSEric Badger 		wpid = waitpid(fpid, &status, 0);
201682a4538fSEric Badger 		ATF_REQUIRE(wpid == fpid);
201782a4538fSEric Badger 		ATF_REQUIRE(WIFSTOPPED(status));
201882a4538fSEric Badger 		if (WSTOPSIG(status) == SIGTRAP) {
201982a4538fSEric Badger 			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
202082a4538fSEric Badger 			    sizeof(pl)) != -1);
202182a4538fSEric Badger 			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
202282a4538fSEric Badger 		} else {
202382a4538fSEric Badger 			ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
202482a4538fSEric Badger 			break;
202582a4538fSEric Badger 		}
202682a4538fSEric Badger 		ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
202782a4538fSEric Badger 	}
202882a4538fSEric Badger 
202982a4538fSEric Badger 	/* Let both threads hit their syscall entries. */
203082a4538fSEric Badger 	for (i = 0; i < 2; ++i) {
203182a4538fSEric Badger 		ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
203282a4538fSEric Badger 
203382a4538fSEric Badger 		wpid = waitpid(fpid, &status, 0);
203482a4538fSEric Badger 		ATF_REQUIRE(wpid == fpid);
203582a4538fSEric Badger 		ATF_REQUIRE(WIFSTOPPED(status));
203682a4538fSEric Badger 		ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
203782a4538fSEric Badger 
203882a4538fSEric Badger 		ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
203982a4538fSEric Badger 		    sizeof(pl)) != -1);
204082a4538fSEric Badger 		ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
204182a4538fSEric Badger 
204282a4538fSEric Badger 		/*
204382a4538fSEric Badger 		 * Prevent the main thread from hitting its syscall exit for
204482a4538fSEric Badger 		 * now.
204582a4538fSEric Badger 		 */
204682a4538fSEric Badger 		if (pl.pl_lwpid == main_lwp)
204782a4538fSEric Badger 			ATF_REQUIRE(ptrace(PT_SUSPEND, main_lwp, 0, 0) == 0);
204882a4538fSEric Badger 
204982a4538fSEric Badger 	}
205082a4538fSEric Badger 
205182a4538fSEric Badger 	/* Send a signal that only the second thread can handle. */
205282a4538fSEric Badger 	ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
205382a4538fSEric Badger 
205482a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
205582a4538fSEric Badger 
205682a4538fSEric Badger 	/* The second wait() should report the SIGUSR2. */
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) == SIGUSR2);
206182a4538fSEric Badger 
206282a4538fSEric Badger 	/* Allow the main thread to try to finish its system call. */
206382a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_RESUME, main_lwp, 0, 0) == 0);
206482a4538fSEric Badger 
206582a4538fSEric Badger 	/*
206682a4538fSEric Badger 	 * At this point, the main thread is in the middle of a system call and
206782a4538fSEric Badger 	 * has been resumed. The second thread has taken a signal which will be
206882a4538fSEric Badger 	 * replaced with a SIGKILL. We expect the main thread will get to run
206982a4538fSEric Badger 	 * first. It should notice the kill request and exit accordingly and
207082a4538fSEric Badger 	 * not stop for the system call exit event.
207182a4538fSEric Badger 	 */
207282a4538fSEric Badger 
207382a4538fSEric Badger 	/* Replace the SIGUSR2 with a kill. */
207482a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
207582a4538fSEric Badger 
207682a4538fSEric Badger 	/* The last wait() should report the SIGKILL (not a syscall exit). */
207782a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
207882a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
207982a4538fSEric Badger 	ATF_REQUIRE(WIFSIGNALED(status));
208082a4538fSEric Badger 	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
208182a4538fSEric Badger 
208282a4538fSEric Badger 	wpid = wait(&status);
208382a4538fSEric Badger 	ATF_REQUIRE(wpid == -1);
208482a4538fSEric Badger 	ATF_REQUIRE(errno == ECHILD);
208582a4538fSEric Badger }
208682a4538fSEric Badger 
208782a4538fSEric Badger static void
208882a4538fSEric Badger sigusr1_handler(int sig)
208982a4538fSEric Badger {
209082a4538fSEric Badger 
209182a4538fSEric Badger 	CHILD_REQUIRE(sig == SIGUSR1);
209282a4538fSEric Badger 	_exit(2);
209382a4538fSEric Badger }
209482a4538fSEric Badger 
209582a4538fSEric Badger /*
209682a4538fSEric Badger  * Verify that even if the signal queue is full for a child process,
209782a4538fSEric Badger  * a PT_KILL will kill the process.
209882a4538fSEric Badger  */
209982a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_with_signal_full_sigqueue);
210082a4538fSEric Badger ATF_TC_BODY(ptrace__PT_KILL_with_signal_full_sigqueue, tc)
210182a4538fSEric Badger {
210282a4538fSEric Badger 	pid_t fpid, wpid;
210382a4538fSEric Badger 	int status;
210482a4538fSEric Badger 	int max_pending_per_proc;
210582a4538fSEric Badger 	size_t len;
210682a4538fSEric Badger 	int i;
210782a4538fSEric Badger 
210882a4538fSEric Badger 	ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
210982a4538fSEric Badger 
211082a4538fSEric Badger 	ATF_REQUIRE((fpid = fork()) != -1);
211182a4538fSEric Badger 	if (fpid == 0) {
211282a4538fSEric Badger 		trace_me();
211382a4538fSEric Badger 		exit(1);
211482a4538fSEric Badger 	}
211582a4538fSEric Badger 
211682a4538fSEric Badger 	/* The first wait() should report the stop from SIGSTOP. */
211782a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
211882a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
211982a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
212082a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
212182a4538fSEric Badger 
212282a4538fSEric Badger 	len = sizeof(max_pending_per_proc);
212382a4538fSEric Badger 	ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
212482a4538fSEric Badger 	    &max_pending_per_proc, &len, NULL, 0) == 0);
212582a4538fSEric Badger 
212682a4538fSEric Badger 	/* Fill the signal queue. */
212782a4538fSEric Badger 	for (i = 0; i < max_pending_per_proc; ++i)
212882a4538fSEric Badger 		ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
212982a4538fSEric Badger 
213082a4538fSEric Badger 	/* Kill the child process. */
213182a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
213282a4538fSEric Badger 
213382a4538fSEric Badger 	/* The last wait() should report the SIGKILL. */
213482a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
213582a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
213682a4538fSEric Badger 	ATF_REQUIRE(WIFSIGNALED(status));
213782a4538fSEric Badger 	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
213882a4538fSEric Badger 
213982a4538fSEric Badger 	wpid = wait(&status);
214082a4538fSEric Badger 	ATF_REQUIRE(wpid == -1);
214182a4538fSEric Badger 	ATF_REQUIRE(errno == ECHILD);
214282a4538fSEric Badger }
214382a4538fSEric Badger 
214482a4538fSEric Badger /*
214582a4538fSEric Badger  * Verify that when stopped at a system call entry, a signal can be
214682a4538fSEric Badger  * requested with PT_CONTINUE which will be delivered once the system
214782a4538fSEric Badger  * call is complete.
214882a4538fSEric Badger  */
214982a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry);
215082a4538fSEric Badger ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry, tc)
215182a4538fSEric Badger {
215282a4538fSEric Badger 	struct ptrace_lwpinfo pl;
215382a4538fSEric Badger 	pid_t fpid, wpid;
215482a4538fSEric Badger 	int status;
215582a4538fSEric Badger 
215682a4538fSEric Badger 	ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
215782a4538fSEric Badger 
215882a4538fSEric Badger 	ATF_REQUIRE((fpid = fork()) != -1);
215982a4538fSEric Badger 	if (fpid == 0) {
216082a4538fSEric Badger 		trace_me();
216182a4538fSEric Badger 		getpid();
216282a4538fSEric Badger 		exit(1);
216382a4538fSEric Badger 	}
216482a4538fSEric Badger 
216582a4538fSEric Badger 	/* The first wait() should report the stop from SIGSTOP. */
216682a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
216782a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
216882a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
216982a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
217082a4538fSEric Badger 
217182a4538fSEric Badger 	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
217282a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
217382a4538fSEric Badger 
217482a4538fSEric Badger 	/* The second wait() should report a system call entry for getpid(). */
217582a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
217682a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
217782a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
217882a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
217982a4538fSEric Badger 
218082a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
218182a4538fSEric Badger 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
218282a4538fSEric Badger 
218382a4538fSEric Badger 	/* Continue the child process with a signal. */
218482a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
218582a4538fSEric Badger 
218682a4538fSEric Badger 	for (;;) {
218782a4538fSEric Badger 		/*
218882a4538fSEric Badger 		 * The last wait() should report exit 2, i.e., a normal _exit
218982a4538fSEric Badger 		 * from the signal handler. In the meantime, catch and proceed
219082a4538fSEric Badger 		 * past any syscall stops.
219182a4538fSEric Badger 		 */
219282a4538fSEric Badger 		wpid = waitpid(fpid, &status, 0);
219382a4538fSEric Badger 		ATF_REQUIRE(wpid == fpid);
219482a4538fSEric Badger 		if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
219582a4538fSEric Badger 			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
219682a4538fSEric Badger 			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
219782a4538fSEric Badger 			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
219882a4538fSEric Badger 		} else {
219982a4538fSEric Badger 			ATF_REQUIRE(WIFEXITED(status));
220082a4538fSEric Badger 			ATF_REQUIRE(WEXITSTATUS(status) == 2);
220182a4538fSEric Badger 			break;
220282a4538fSEric Badger 		}
220382a4538fSEric Badger 	}
220482a4538fSEric Badger 
220582a4538fSEric Badger 	wpid = wait(&status);
220682a4538fSEric Badger 	ATF_REQUIRE(wpid == -1);
220782a4538fSEric Badger 	ATF_REQUIRE(errno == ECHILD);
220882a4538fSEric Badger }
220982a4538fSEric Badger 
221082a4538fSEric Badger static void
221182a4538fSEric Badger sigusr1_counting_handler(int sig)
221282a4538fSEric Badger {
221382a4538fSEric Badger 	static int counter = 0;
221482a4538fSEric Badger 
221582a4538fSEric Badger 	CHILD_REQUIRE(sig == SIGUSR1);
221682a4538fSEric Badger 	counter++;
221782a4538fSEric Badger 	if (counter == 2)
221882a4538fSEric Badger 		_exit(2);
221982a4538fSEric Badger }
222082a4538fSEric Badger 
222182a4538fSEric Badger /*
222282a4538fSEric Badger  * Verify that, when continuing from a stop at system call entry and exit,
222382a4538fSEric Badger  * a signal can be requested from both stops, and both will be delivered when
222482a4538fSEric Badger  * the system call is complete.
222582a4538fSEric Badger  */
222682a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
222782a4538fSEric Badger ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit, tc)
222882a4538fSEric Badger {
222982a4538fSEric Badger 	struct ptrace_lwpinfo pl;
223082a4538fSEric Badger 	pid_t fpid, wpid;
223182a4538fSEric Badger 	int status;
223282a4538fSEric Badger 
223382a4538fSEric Badger 	ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
223482a4538fSEric Badger 
223582a4538fSEric Badger 	ATF_REQUIRE((fpid = fork()) != -1);
223682a4538fSEric Badger 	if (fpid == 0) {
223782a4538fSEric Badger 		trace_me();
223882a4538fSEric Badger 		getpid();
223982a4538fSEric Badger 		exit(1);
224082a4538fSEric Badger 	}
224182a4538fSEric Badger 
224282a4538fSEric Badger 	/* The first wait() should report the stop from SIGSTOP. */
224382a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
224482a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
224582a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
224682a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
224782a4538fSEric Badger 
224882a4538fSEric Badger 	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
224982a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
225082a4538fSEric Badger 
225182a4538fSEric Badger 	/* The second wait() should report a system call entry for getpid(). */
225282a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
225382a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
225482a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
225582a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
225682a4538fSEric Badger 
225782a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
225882a4538fSEric Badger 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
225982a4538fSEric Badger 
226082a4538fSEric Badger 	/* Continue the child process with a signal. */
226182a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
226282a4538fSEric Badger 
226382a4538fSEric Badger 	/* The third wait() should report a system call exit for getpid(). */
226482a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
226582a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
226682a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
226782a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
226882a4538fSEric Badger 
226982a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
227082a4538fSEric Badger 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
227182a4538fSEric Badger 
227282a4538fSEric Badger 	/* Continue the child process with a signal. */
227382a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
227482a4538fSEric Badger 
227582a4538fSEric Badger 	for (;;) {
227682a4538fSEric Badger 		/*
227782a4538fSEric Badger 		 * The last wait() should report exit 2, i.e., a normal _exit
227882a4538fSEric Badger 		 * from the signal handler. In the meantime, catch and proceed
227982a4538fSEric Badger 		 * past any syscall stops.
228082a4538fSEric Badger 		 */
228182a4538fSEric Badger 		wpid = waitpid(fpid, &status, 0);
228282a4538fSEric Badger 		ATF_REQUIRE(wpid == fpid);
228382a4538fSEric Badger 		if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
228482a4538fSEric Badger 			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
228582a4538fSEric Badger 			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
228682a4538fSEric Badger 			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
228782a4538fSEric Badger 		} else {
228882a4538fSEric Badger 			ATF_REQUIRE(WIFEXITED(status));
228982a4538fSEric Badger 			ATF_REQUIRE(WEXITSTATUS(status) == 2);
229082a4538fSEric Badger 			break;
229182a4538fSEric Badger 		}
229282a4538fSEric Badger 	}
229382a4538fSEric Badger 
229482a4538fSEric Badger 	wpid = wait(&status);
229582a4538fSEric Badger 	ATF_REQUIRE(wpid == -1);
229682a4538fSEric Badger 	ATF_REQUIRE(errno == ECHILD);
229782a4538fSEric Badger }
229882a4538fSEric Badger 
229982a4538fSEric Badger /*
230082a4538fSEric Badger  * Verify that even if the signal queue is full for a child process,
230182a4538fSEric Badger  * a PT_CONTINUE with a signal will not result in loss of that signal.
230282a4538fSEric Badger  */
230382a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_full_sigqueue);
230482a4538fSEric Badger ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_full_sigqueue, tc)
230582a4538fSEric Badger {
230682a4538fSEric Badger 	pid_t fpid, wpid;
230782a4538fSEric Badger 	int status;
230882a4538fSEric Badger 	int max_pending_per_proc;
230982a4538fSEric Badger 	size_t len;
231082a4538fSEric Badger 	int i;
231182a4538fSEric Badger 
231282a4538fSEric Badger 	ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
231382a4538fSEric Badger 	ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
231482a4538fSEric Badger 
231582a4538fSEric Badger 	ATF_REQUIRE((fpid = fork()) != -1);
231682a4538fSEric Badger 	if (fpid == 0) {
231782a4538fSEric Badger 		trace_me();
231882a4538fSEric Badger 		exit(1);
231982a4538fSEric Badger 	}
232082a4538fSEric Badger 
232182a4538fSEric Badger 	/* The first wait() should report the stop from SIGSTOP. */
232282a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
232382a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
232482a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
232582a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
232682a4538fSEric Badger 
232782a4538fSEric Badger 	len = sizeof(max_pending_per_proc);
232882a4538fSEric Badger 	ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
232982a4538fSEric Badger 	    &max_pending_per_proc, &len, NULL, 0) == 0);
233082a4538fSEric Badger 
233182a4538fSEric Badger 	/* Fill the signal queue. */
233282a4538fSEric Badger 	for (i = 0; i < max_pending_per_proc; ++i)
233382a4538fSEric Badger 		ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
233482a4538fSEric Badger 
233582a4538fSEric Badger 	/* Continue with signal. */
233682a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
233782a4538fSEric Badger 
233882a4538fSEric Badger 	for (;;) {
233982a4538fSEric Badger 		wpid = waitpid(fpid, &status, 0);
234082a4538fSEric Badger 		ATF_REQUIRE(wpid == fpid);
234182a4538fSEric Badger 		if (WIFSTOPPED(status)) {
234282a4538fSEric Badger 			ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
234382a4538fSEric Badger 			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
234482a4538fSEric Badger 		} else {
234582a4538fSEric Badger 			/*
234682a4538fSEric Badger 			 * The last wait() should report normal _exit from the
234782a4538fSEric Badger 			 * SIGUSR1 handler.
234882a4538fSEric Badger 			 */
234982a4538fSEric Badger 			ATF_REQUIRE(WIFEXITED(status));
235082a4538fSEric Badger 			ATF_REQUIRE(WEXITSTATUS(status) == 2);
235182a4538fSEric Badger 			break;
235282a4538fSEric Badger 		}
235382a4538fSEric Badger 	}
235482a4538fSEric Badger 
235582a4538fSEric Badger 	wpid = wait(&status);
235682a4538fSEric Badger 	ATF_REQUIRE(wpid == -1);
235782a4538fSEric Badger 	ATF_REQUIRE(errno == ECHILD);
235882a4538fSEric Badger }
235982a4538fSEric Badger 
236082a4538fSEric Badger /*
236182a4538fSEric Badger  * Verify that, after stopping due to a signal, that signal can be
236282a4538fSEric Badger  * replaced with another signal.
236382a4538fSEric Badger  */
236482a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_change_sig);
236582a4538fSEric Badger ATF_TC_BODY(ptrace__PT_CONTINUE_change_sig, tc)
236682a4538fSEric Badger {
236782a4538fSEric Badger 	struct ptrace_lwpinfo pl;
236882a4538fSEric Badger 	pid_t fpid, wpid;
236982a4538fSEric Badger 	int status;
237082a4538fSEric Badger 
237182a4538fSEric Badger 	ATF_REQUIRE((fpid = fork()) != -1);
237282a4538fSEric Badger 	if (fpid == 0) {
237382a4538fSEric Badger 		trace_me();
237482a4538fSEric Badger 		sleep(20);
237582a4538fSEric Badger 		exit(1);
237682a4538fSEric Badger 	}
237782a4538fSEric Badger 
237882a4538fSEric Badger 	/* The first wait() should report the stop from SIGSTOP. */
237982a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
238082a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
238182a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
238282a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
238382a4538fSEric Badger 
238482a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
238582a4538fSEric Badger 
238682a4538fSEric Badger 	/* Send a signal without ptrace. */
238782a4538fSEric Badger 	ATF_REQUIRE(kill(fpid, SIGINT) == 0);
238882a4538fSEric Badger 
238982a4538fSEric Badger 	/* The second wait() should report a SIGINT was received. */
239082a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
239182a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
239282a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
239382a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGINT);
239482a4538fSEric Badger 
239582a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
239682a4538fSEric Badger 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
239782a4538fSEric Badger 	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGINT);
239882a4538fSEric Badger 
239982a4538fSEric Badger 	/* Continue the child process with a different signal. */
240082a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTERM) == 0);
240182a4538fSEric Badger 
240282a4538fSEric Badger 	/*
240382a4538fSEric Badger 	 * The last wait() should report having died due to the new
240482a4538fSEric Badger 	 * signal, SIGTERM.
240582a4538fSEric Badger 	 */
240682a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
240782a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
240882a4538fSEric Badger 	ATF_REQUIRE(WIFSIGNALED(status));
240982a4538fSEric Badger 	ATF_REQUIRE(WTERMSIG(status) == SIGTERM);
241082a4538fSEric Badger 
241182a4538fSEric Badger 	wpid = wait(&status);
241282a4538fSEric Badger 	ATF_REQUIRE(wpid == -1);
241382a4538fSEric Badger 	ATF_REQUIRE(errno == ECHILD);
241482a4538fSEric Badger }
241582a4538fSEric Badger 
241682a4538fSEric Badger /*
241782a4538fSEric Badger  * Verify that a signal can be passed through to the child even when there
241882a4538fSEric Badger  * was no true signal originally. Such cases arise when a SIGTRAP is
241982a4538fSEric Badger  * invented for e.g, system call stops.
242082a4538fSEric Badger  */
242182a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
242282a4538fSEric Badger ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry, tc)
242382a4538fSEric Badger {
242482a4538fSEric Badger 	struct ptrace_lwpinfo pl;
242582a4538fSEric Badger 	pid_t fpid, wpid;
242682a4538fSEric Badger 	int status;
242782a4538fSEric Badger 
242882a4538fSEric Badger 	ATF_REQUIRE((fpid = fork()) != -1);
242982a4538fSEric Badger 	if (fpid == 0) {
243082a4538fSEric Badger 		trace_me();
243182a4538fSEric Badger 		getpid();
243282a4538fSEric Badger 		exit(1);
243382a4538fSEric Badger 	}
243482a4538fSEric Badger 
243582a4538fSEric Badger 	/* The first wait() should report the stop from SIGSTOP. */
243682a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
243782a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
243882a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
243982a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
244082a4538fSEric Badger 
244182a4538fSEric Badger 	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
244282a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
244382a4538fSEric Badger 
244482a4538fSEric Badger 	/* The second wait() should report a system call entry for getpid(). */
244582a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
244682a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
244782a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
244882a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
244982a4538fSEric Badger 
245082a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
245182a4538fSEric Badger 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
245282a4538fSEric Badger 
245382a4538fSEric Badger 	/* Continue the child process with a SIGTRAP. */
245482a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTRAP) == 0);
245582a4538fSEric Badger 
245682a4538fSEric Badger 	for (;;) {
245782a4538fSEric Badger 		/*
245882a4538fSEric Badger 		 * The last wait() should report exit due to SIGTRAP.  In the
245982a4538fSEric Badger 		 * meantime, catch and proceed past any syscall stops.
246082a4538fSEric Badger 		 */
246182a4538fSEric Badger 		wpid = waitpid(fpid, &status, 0);
246282a4538fSEric Badger 		ATF_REQUIRE(wpid == fpid);
246382a4538fSEric Badger 		if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
246482a4538fSEric Badger 			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
246582a4538fSEric Badger 			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
246682a4538fSEric Badger 			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
246782a4538fSEric Badger 		} else {
246882a4538fSEric Badger 			ATF_REQUIRE(WIFSIGNALED(status));
246982a4538fSEric Badger 			ATF_REQUIRE(WTERMSIG(status) == SIGTRAP);
247082a4538fSEric Badger 			break;
247182a4538fSEric Badger 		}
247282a4538fSEric Badger 	}
247382a4538fSEric Badger 
247482a4538fSEric Badger 	wpid = wait(&status);
247582a4538fSEric Badger 	ATF_REQUIRE(wpid == -1);
247682a4538fSEric Badger 	ATF_REQUIRE(errno == ECHILD);
247782a4538fSEric Badger 
247882a4538fSEric Badger }
247982a4538fSEric Badger 
248082a4538fSEric Badger /*
248182a4538fSEric Badger  * A mixed bag PT_CONTINUE with signal test.
248282a4538fSEric Badger  */
248382a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_mix);
248482a4538fSEric Badger ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_mix, tc)
248582a4538fSEric Badger {
248682a4538fSEric Badger 	struct ptrace_lwpinfo pl;
248782a4538fSEric Badger 	pid_t fpid, wpid;
248882a4538fSEric Badger 	int status;
248982a4538fSEric Badger 
249082a4538fSEric Badger 	ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
249182a4538fSEric Badger 
249282a4538fSEric Badger 	ATF_REQUIRE((fpid = fork()) != -1);
249382a4538fSEric Badger 	if (fpid == 0) {
249482a4538fSEric Badger 		trace_me();
249582a4538fSEric Badger 		getpid();
249682a4538fSEric Badger 		exit(1);
249782a4538fSEric Badger 	}
249882a4538fSEric Badger 
249982a4538fSEric Badger 	/* The first wait() should report the stop from SIGSTOP. */
250082a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
250182a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
250282a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
250382a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
250482a4538fSEric Badger 
250582a4538fSEric Badger 	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
250682a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
250782a4538fSEric Badger 
250882a4538fSEric Badger 	/* The second wait() should report a system call entry for getpid(). */
250982a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
251082a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
251182a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
251282a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
251382a4538fSEric Badger 
251482a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
251582a4538fSEric Badger 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
251682a4538fSEric Badger 
251782a4538fSEric Badger 	/* Continue with the first SIGUSR1. */
251882a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
251982a4538fSEric Badger 
252082a4538fSEric Badger 	/* The next wait() should report a system call exit for getpid(). */
252182a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
252282a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
252382a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
252482a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
252582a4538fSEric Badger 
252682a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
252782a4538fSEric Badger 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
252882a4538fSEric Badger 
252982a4538fSEric Badger 	/* Send an ABRT without ptrace. */
253082a4538fSEric Badger 	ATF_REQUIRE(kill(fpid, SIGABRT) == 0);
253182a4538fSEric Badger 
253282a4538fSEric Badger 	/* Continue normally. */
253382a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
253482a4538fSEric Badger 
253582a4538fSEric Badger 	/* The next wait() should report the SIGABRT. */
253682a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
253782a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
253882a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
253982a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
254082a4538fSEric Badger 
254182a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
254282a4538fSEric Badger 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
254382a4538fSEric Badger 	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
254482a4538fSEric Badger 
254582a4538fSEric Badger 	/* Continue, replacing the SIGABRT with another SIGUSR1. */
254682a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
254782a4538fSEric Badger 
254882a4538fSEric Badger 	for (;;) {
254982a4538fSEric Badger 		/*
255082a4538fSEric Badger 		 * The last wait() should report exit 2, i.e., a normal _exit
255182a4538fSEric Badger 		 * from the signal handler. In the meantime, catch and proceed
255282a4538fSEric Badger 		 * past any syscall stops.
255382a4538fSEric Badger 		 */
255482a4538fSEric Badger 		wpid = waitpid(fpid, &status, 0);
255582a4538fSEric Badger 		ATF_REQUIRE(wpid == fpid);
255682a4538fSEric Badger 		if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
255782a4538fSEric Badger 			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
255882a4538fSEric Badger 			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
255982a4538fSEric Badger 			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
256082a4538fSEric Badger 		} else {
256182a4538fSEric Badger 			ATF_REQUIRE(WIFEXITED(status));
256282a4538fSEric Badger 			ATF_REQUIRE(WEXITSTATUS(status) == 2);
256382a4538fSEric Badger 			break;
256482a4538fSEric Badger 		}
256582a4538fSEric Badger 	}
256682a4538fSEric Badger 
256782a4538fSEric Badger 	wpid = wait(&status);
256882a4538fSEric Badger 	ATF_REQUIRE(wpid == -1);
256982a4538fSEric Badger 	ATF_REQUIRE(errno == ECHILD);
257082a4538fSEric Badger 
257182a4538fSEric Badger }
257282a4538fSEric Badger 
257382a4538fSEric Badger /*
257482a4538fSEric Badger  * Verify a signal delivered by ptrace is noticed by kevent(2).
257582a4538fSEric Badger  */
257682a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_kqueue);
257782a4538fSEric Badger ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_kqueue, tc)
257882a4538fSEric Badger {
257982a4538fSEric Badger 	pid_t fpid, wpid;
258082a4538fSEric Badger 	int status, kq, nevents;
258182a4538fSEric Badger 	struct kevent kev;
258282a4538fSEric Badger 
258382a4538fSEric Badger 	ATF_REQUIRE(signal(SIGUSR1, SIG_IGN) != SIG_ERR);
258482a4538fSEric Badger 
258582a4538fSEric Badger 	ATF_REQUIRE((fpid = fork()) != -1);
258682a4538fSEric Badger 	if (fpid == 0) {
258782a4538fSEric Badger 		CHILD_REQUIRE((kq = kqueue()) > 0);
258882a4538fSEric Badger 		EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
258982a4538fSEric Badger 		CHILD_REQUIRE(kevent(kq, &kev, 1, NULL, 0, NULL) == 0);
259082a4538fSEric Badger 
259182a4538fSEric Badger 		trace_me();
259282a4538fSEric Badger 
259382a4538fSEric Badger 		for (;;) {
259482a4538fSEric Badger 			nevents = kevent(kq, NULL, 0, &kev, 1, NULL);
259582a4538fSEric Badger 			if (nevents == -1 && errno == EINTR)
259682a4538fSEric Badger 				continue;
259782a4538fSEric Badger 			CHILD_REQUIRE(nevents > 0);
259882a4538fSEric Badger 			CHILD_REQUIRE(kev.filter == EVFILT_SIGNAL);
259982a4538fSEric Badger 			CHILD_REQUIRE(kev.ident == SIGUSR1);
260082a4538fSEric Badger 			break;
260182a4538fSEric Badger 		}
260282a4538fSEric Badger 
260382a4538fSEric Badger 		exit(1);
260482a4538fSEric Badger 	}
260582a4538fSEric Badger 
260682a4538fSEric Badger 	/* The first wait() should report the stop from SIGSTOP. */
260782a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
260882a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
260982a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
261082a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
261182a4538fSEric Badger 
261282a4538fSEric Badger 	/* Continue with the SIGUSR1. */
261382a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
261482a4538fSEric Badger 
261582a4538fSEric Badger 	/*
261682a4538fSEric Badger 	 * The last wait() should report normal exit with code 1.
261782a4538fSEric Badger 	 */
261882a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
261982a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
262082a4538fSEric Badger 	ATF_REQUIRE(WIFEXITED(status));
262182a4538fSEric Badger 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
262282a4538fSEric Badger 
262382a4538fSEric Badger 	wpid = wait(&status);
262482a4538fSEric Badger 	ATF_REQUIRE(wpid == -1);
262582a4538fSEric Badger 	ATF_REQUIRE(errno == ECHILD);
262682a4538fSEric Badger }
262782a4538fSEric Badger 
262882a4538fSEric Badger static sem_t sigusr1_sem;
262982a4538fSEric Badger 
263082a4538fSEric Badger static void
263182a4538fSEric Badger sigusr1_sempost_handler(int sig __unused)
263282a4538fSEric Badger {
263382a4538fSEric Badger 
263482a4538fSEric Badger 	CHILD_REQUIRE(sem_post(&sigusr1_sem) == 0);
263582a4538fSEric Badger }
263682a4538fSEric Badger 
263782a4538fSEric Badger static void *
263882a4538fSEric Badger signal_thread(void *arg)
263982a4538fSEric Badger {
264082a4538fSEric Badger 	int err;
264182a4538fSEric Badger 	sigset_t sigmask;
264282a4538fSEric Badger 
264382a4538fSEric Badger 	pthread_barrier_t *pbarrier = (pthread_barrier_t*)arg;
264482a4538fSEric Badger 
264582a4538fSEric Badger 	/* Wait for this thread to receive a SIGUSR1. */
264682a4538fSEric Badger 	do {
264782a4538fSEric Badger 		err = sem_wait(&sigusr1_sem);
264882a4538fSEric Badger 		CHILD_REQUIRE(err == 0 || errno == EINTR);
264982a4538fSEric Badger 	} while (err != 0 && errno == EINTR);
265082a4538fSEric Badger 
265182a4538fSEric Badger 	/* Free our companion thread from the barrier. */
265282a4538fSEric Badger 	pthread_barrier_wait(pbarrier);
265382a4538fSEric Badger 
265482a4538fSEric Badger 	/*
265582a4538fSEric Badger 	 * Swap ignore duties; the next SIGUSR1 should go to the
265682a4538fSEric Badger 	 * other thread.
265782a4538fSEric Badger 	 */
265882a4538fSEric Badger 	CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
265982a4538fSEric Badger 	CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
266082a4538fSEric Badger 	CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
266182a4538fSEric Badger 
266282a4538fSEric Badger 	/* Sync up threads after swapping signal masks. */
266382a4538fSEric Badger 	pthread_barrier_wait(pbarrier);
266482a4538fSEric Badger 
266582a4538fSEric Badger 	/* Wait until our companion has received its SIGUSR1. */
266682a4538fSEric Badger 	pthread_barrier_wait(pbarrier);
266782a4538fSEric Badger 
266882a4538fSEric Badger 	return (NULL);
266982a4538fSEric Badger }
267082a4538fSEric Badger 
267182a4538fSEric Badger /*
267282a4538fSEric Badger  * Verify that if ptrace stops due to a signal but continues with
267382a4538fSEric Badger  * a different signal that the new signal is routed to a thread
267482a4538fSEric Badger  * that can accept it, and that that thread is awakened by the signal
267582a4538fSEric Badger  * in a timely manner.
267682a4538fSEric Badger  */
267782a4538fSEric Badger ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_thread_sigmask);
267882a4538fSEric Badger ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_thread_sigmask, tc)
267982a4538fSEric Badger {
268082a4538fSEric Badger 	pid_t fpid, wpid;
268182a4538fSEric Badger 	int status, err;
268282a4538fSEric Badger 	pthread_t t;
268382a4538fSEric Badger 	sigset_t sigmask;
268482a4538fSEric Badger 	pthread_barrier_t barrier;
268582a4538fSEric Badger 
268682a4538fSEric Badger 	ATF_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
268782a4538fSEric Badger 	ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
268882a4538fSEric Badger 	ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
268982a4538fSEric Badger 
269082a4538fSEric Badger 	ATF_REQUIRE((fpid = fork()) != -1);
269182a4538fSEric Badger 	if (fpid == 0) {
269282a4538fSEric Badger 		CHILD_REQUIRE(pthread_create(&t, NULL, signal_thread, (void*)&barrier) == 0);
269382a4538fSEric Badger 
269482a4538fSEric Badger 		/* The other thread should receive the first SIGUSR1. */
269582a4538fSEric Badger 		CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
269682a4538fSEric Badger 		CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
269782a4538fSEric Badger 		CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
269882a4538fSEric Badger 
269982a4538fSEric Badger 		trace_me();
270082a4538fSEric Badger 
270182a4538fSEric Badger 		/* Wait until other thread has received its SIGUSR1. */
270282a4538fSEric Badger 		pthread_barrier_wait(&barrier);
270382a4538fSEric Badger 
270482a4538fSEric Badger 		/*
270582a4538fSEric Badger 		 * Swap ignore duties; the next SIGUSR1 should go to this
270682a4538fSEric Badger 		 * thread.
270782a4538fSEric Badger 		 */
270882a4538fSEric Badger 		CHILD_REQUIRE(pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
270982a4538fSEric Badger 
271082a4538fSEric Badger 		/* Sync up threads after swapping signal masks. */
271182a4538fSEric Badger 		pthread_barrier_wait(&barrier);
271282a4538fSEric Badger 
271382a4538fSEric Badger 		/*
271482a4538fSEric Badger 		 * Sync up with test code; we're ready for the next SIGUSR1
271582a4538fSEric Badger 		 * now.
271682a4538fSEric Badger 		 */
271782a4538fSEric Badger 		raise(SIGSTOP);
271882a4538fSEric Badger 
271982a4538fSEric Badger 		/* Wait for this thread to receive a SIGUSR1. */
272082a4538fSEric Badger 		do {
272182a4538fSEric Badger 			err = sem_wait(&sigusr1_sem);
272282a4538fSEric Badger 			CHILD_REQUIRE(err == 0 || errno == EINTR);
272382a4538fSEric Badger 		} while (err != 0 && errno == EINTR);
272482a4538fSEric Badger 
272582a4538fSEric Badger 		/* Free the other thread from the barrier. */
272682a4538fSEric Badger 		pthread_barrier_wait(&barrier);
272782a4538fSEric Badger 
272882a4538fSEric Badger 		CHILD_REQUIRE(pthread_join(t, NULL) == 0);
272982a4538fSEric Badger 
273082a4538fSEric Badger 		exit(1);
273182a4538fSEric Badger 	}
273282a4538fSEric Badger 
273382a4538fSEric Badger 	/* The first wait() should report the stop from SIGSTOP. */
273482a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
273582a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
273682a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
273782a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
273882a4538fSEric Badger 
273982a4538fSEric Badger 	/* Continue the child ignoring the SIGSTOP. */
274082a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
274182a4538fSEric Badger 
274282a4538fSEric Badger 	/*
274382a4538fSEric Badger 	 * Send a signal without ptrace that either thread will accept (USR2,
274482a4538fSEric Badger 	 * in this case).
274582a4538fSEric Badger 	 */
274682a4538fSEric Badger 	ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
274782a4538fSEric Badger 
274882a4538fSEric Badger 	/* The second wait() should report a SIGUSR2 was received. */
274982a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
275082a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
275182a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
275282a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
275382a4538fSEric Badger 
275482a4538fSEric Badger 	/* Continue the child, changing the signal to USR1. */
275582a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
275682a4538fSEric Badger 
275782a4538fSEric Badger 	/* The next wait() should report the stop from SIGSTOP. */
275882a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
275982a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
276082a4538fSEric Badger 	ATF_REQUIRE(WIFSTOPPED(status));
276182a4538fSEric Badger 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
276282a4538fSEric Badger 
276382a4538fSEric Badger 	/* Continue the child ignoring the SIGSTOP. */
276482a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
276582a4538fSEric Badger 
276682a4538fSEric Badger 	ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
276782a4538fSEric Badger 
276882a4538fSEric Badger 	/* The next wait() should report a SIGUSR2 was received. */
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) == SIGUSR2);
277382a4538fSEric Badger 
277482a4538fSEric Badger 	/* Continue the child, changing the signal to USR1. */
277582a4538fSEric Badger 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
277682a4538fSEric Badger 
277782a4538fSEric Badger 	/* The last wait() should report normal exit with code 1. */
277882a4538fSEric Badger 	wpid = waitpid(fpid, &status, 0);
277982a4538fSEric Badger 	ATF_REQUIRE(wpid == fpid);
278082a4538fSEric Badger 	ATF_REQUIRE(WIFEXITED(status));
278182a4538fSEric Badger 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
278282a4538fSEric Badger 
278382a4538fSEric Badger 	wpid = wait(&status);
278482a4538fSEric Badger 	ATF_REQUIRE(wpid == -1);
278582a4538fSEric Badger 	ATF_REQUIRE(errno == ECHILD);
278682a4538fSEric Badger }
278782a4538fSEric Badger 
2788c209e3e2SJohn Baldwin ATF_TP_ADD_TCS(tp)
2789c209e3e2SJohn Baldwin {
2790c209e3e2SJohn Baldwin 
2791c209e3e2SJohn Baldwin 	ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_trace_me);
2792c209e3e2SJohn Baldwin 	ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_attach);
279357c74f5bSJohn Baldwin 	ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_child_debugger);
279457c74f5bSJohn Baldwin 	ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_unrelated_debugger);
279598685dc8SJohn Baldwin 	ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached);
279698685dc8SJohn Baldwin 	ATF_TP_ADD_TC(tp, ptrace__follow_fork_child_detached);
279798685dc8SJohn Baldwin 	ATF_TP_ADD_TC(tp, ptrace__follow_fork_parent_detached);
279898685dc8SJohn Baldwin 	ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached_unrelated_debugger);
279998685dc8SJohn Baldwin 	ATF_TP_ADD_TC(tp,
280098685dc8SJohn Baldwin 	    ptrace__follow_fork_child_detached_unrelated_debugger);
280198685dc8SJohn Baldwin 	ATF_TP_ADD_TC(tp,
280298685dc8SJohn Baldwin 	    ptrace__follow_fork_parent_detached_unrelated_debugger);
2803368b2b1cSJohn Baldwin 	ATF_TP_ADD_TC(tp, ptrace__getppid);
2804189ac973SJohn Baldwin 	ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_fork);
2805189ac973SJohn Baldwin 	ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_vfork);
2806189ac973SJohn Baldwin 	ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_thread);
28075fcfab6eSJohn Baldwin 	ATF_TP_ADD_TC(tp, ptrace__lwp_events);
28085fcfab6eSJohn Baldwin 	ATF_TP_ADD_TC(tp, ptrace__lwp_events_exec);
28093340c45bSJohn Baldwin 	ATF_TP_ADD_TC(tp, ptrace__siginfo);
28108d570f64SJohn Baldwin 	ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_disable);
28118d570f64SJohn Baldwin 	ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_enable);
28128d570f64SJohn Baldwin 	ATF_TP_ADD_TC(tp, ptrace__event_mask);
2813fc4f075aSJohn Baldwin 	ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork);
2814fc4f075aSJohn Baldwin 	ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork_follow);
2815*e2ebfbbfSEric Badger #if defined(__amd64__) || defined(__i386__) || defined(__sparc64__)
281682a4538fSEric Badger 	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_breakpoint);
2817*e2ebfbbfSEric Badger #endif
281882a4538fSEric Badger 	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_system_call);
281982a4538fSEric Badger 	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_threads);
282082a4538fSEric Badger 	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_signal);
282182a4538fSEric Badger 	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_stop);
282282a4538fSEric Badger 	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_with_signal_full_sigqueue);
282382a4538fSEric Badger 	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_system_call_entry);
282482a4538fSEric Badger 	ATF_TP_ADD_TC(tp,
282582a4538fSEric Badger 	    ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
282682a4538fSEric Badger 	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_full_sigqueue);
282782a4538fSEric Badger 	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_change_sig);
282882a4538fSEric Badger 	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
282982a4538fSEric Badger 	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_mix);
283082a4538fSEric Badger 	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_kqueue);
283182a4538fSEric Badger 	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_thread_sigmask);
2832c209e3e2SJohn Baldwin 
2833c209e3e2SJohn Baldwin 	return (atf_no_error());
2834c209e3e2SJohn Baldwin }
2835