t_ptrace_kill.c revision 1.1 1 /* $NetBSD: t_ptrace_kill.c,v 1.1 2024/12/17 17:52:23 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2024 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: t_ptrace_kill.c,v 1.1 2024/12/17 17:52:23 christos Exp $");
34
35 #include <sys/types.h>
36 #include <sys/ptrace.h>
37 #include <sys/wait.h>
38 #include <pthread.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <signal.h>
43 #include <unistd.h>
44 #include <errno.h>
45 #include <err.h>
46 #include <atf-c.h>
47
48 #define SYSCALL(a, b) ATF_REQUIRE_EQ_MSG(a, b, "%s got %s", #a, strerror(errno))
49
50 ATF_TC(pt_kill);
51 ATF_TC_HEAD(pt_kill, tc)
52 {
53 atf_tc_set_md_var(tc, "descr", "Test PT_KILL of a PT_STOP'ed process");
54 }
55
56 static void
57 child(int *fdto, int *fdfrom)
58 {
59 char p = '2', q;
60 printf("%d: born\n", getpid());
61 write(fdfrom[1], &p, 1);
62 read(fdto[0], &q, 1);
63 printf("%d: seppuku %c\n", getpid(), q);
64 write(fdfrom[1], &p, 1);
65 read(fdto[0], &q, 1);
66 // *(int *)1 = 0;
67 // kill(getpid(), SIGSEGV);
68 // kill(getpid(), SIGSTOP);
69 for (;;)
70 sleep(1);
71
72 }
73
74 static void *
75 waitthread(void *pidp)
76 {
77 int status = 0;
78 pid_t rpid, pid;
79
80 pid = *(pid_t *)pidp;
81 printf("waiting for %d\n", pid);
82 while ((rpid = waitpid(pid, &status, 0)) != pid) {
83 printf("waitpid %d = %d status = %#x", pid, rpid, status);
84 }
85 printf("done waitpid %d = %d status = %#x", pid, rpid, status);
86 return NULL;
87 }
88
89 ATF_TC_BODY(pt_kill, tc)
90 {
91 pid_t pid;
92 int fdto[2], fdfrom[2];
93 char p = '1', q;
94 int status;
95 pthread_t thread;
96
97 SYSCALL(pipe(fdto), 0);
98 SYSCALL(pipe(fdfrom), 0);
99 switch (pid = fork()) {
100 case 0:
101 child(fdto, fdfrom);
102 break;
103 case -1:
104 err(EXIT_FAILURE, "fork failed");
105 default:
106 SYSCALL(pthread_create(&thread, NULL, waitthread, &pid), 0);
107 sleep(1); // XXX: too lazy to sync properly
108 SYSCALL(read(fdfrom[0], &q, 1), 1);
109 printf("%d: read %c\n", pid, q);
110 SYSCALL(ptrace(PT_ATTACH, pid, NULL, 0), 0);
111 printf("%d: attached\n", pid);
112 SYSCALL(write(fdto[1], &p, 1), 1);
113 waitpid(pid, NULL, WNOHANG);
114 printf("%d: sent\n", pid);
115 SYSCALL(ptrace(PT_CONTINUE, pid, (void *)1, 0), 0);
116 SYSCALL(read(fdfrom[0], &p, 1), 1);
117 printf("%d: received\n", pid);
118 SYSCALL(ptrace(PT_STOP, pid, NULL, 0), 0);
119 SYSCALL(ptrace(PT_KILL, pid, NULL, 0), 0);
120 SYSCALL(waitpid(pid, &status, 0), pid);
121 ATF_REQUIRE(status == 9);
122 break;
123 }
124 }
125
126 ATF_TP_ADD_TCS(tp)
127 {
128 ATF_TP_ADD_TC(tp, pt_kill);
129
130 return atf_no_error();
131 }
132