1/*	$NetBSD: t_ptrace_kill_wait.h,v 1.1 2020/05/04 21:55:12 kamil Exp $	*/
2
3/*-
4 * Copyright (c) 2016, 2017, 2018, 2019, 2020 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29static void
30ptrace_kill(const char *type)
31{
32	const int sigval = SIGSTOP;
33	pid_t child, wpid;
34#if defined(TWAIT_HAVE_STATUS)
35	int status;
36#endif
37
38	DPRINTF("Before forking process PID=%d\n", getpid());
39	SYSCALL_REQUIRE((child = fork()) != -1);
40	if (child == 0) {
41		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
42		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
43
44		DPRINTF("Before raising %s from child\n", strsignal(sigval));
45		FORKEE_ASSERT(raise(sigval) == 0);
46
47		/* NOTREACHED */
48		FORKEE_ASSERTX(0 &&
49		    "Child should be terminated by a signal from its parent");
50	}
51	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
52
53	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
54	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
55
56	validate_status_stopped(status, sigval);
57
58	DPRINTF("Before killing the child process with %s\n", type);
59	if (strcmp(type, "ptrace(PT_KILL)") == 0) {
60		SYSCALL_REQUIRE(ptrace(PT_KILL, child, (void*)1, 0) != -1);
61	} else if (strcmp(type, "kill(SIGKILL)") == 0) {
62		kill(child, SIGKILL);
63	} else if (strcmp(type, "killpg(SIGKILL)") == 0) {
64		setpgid(child, 0);
65		killpg(getpgid(child), SIGKILL);
66	}
67
68	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
69	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
70
71	validate_status_signaled(status, SIGKILL, 0);
72
73	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
74	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
75}
76
77#define PTRACE_KILL(test, type)						\
78ATF_TC(test);								\
79ATF_TC_HEAD(test, tc)							\
80{									\
81        atf_tc_set_md_var(tc, "descr",					\
82            "Verify killing the child with " type);			\
83}									\
84									\
85ATF_TC_BODY(test, tc)							\
86{									\
87									\
88        ptrace_kill(type);						\
89}
90
91// PT_CONTINUE with SIGKILL is covered by traceme_sendsignal_simple1
92PTRACE_KILL(kill1, "ptrace(PT_KILL)")
93PTRACE_KILL(kill2, "kill(SIGKILL)")
94PTRACE_KILL(kill3, "killpg(SIGKILL)")
95
96#define ATF_TP_ADD_TCS_PTRACE_WAIT_KILL() \
97	ATF_TP_ADD_TC(tp, kill1); \
98	ATF_TP_ADD_TC(tp, kill2); \
99	ATF_TP_ADD_TC(tp, kill3);
100