1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2010 The FreeBSD Foundation
5 * Copyright (c) 2008 John Birrell (jb@freebsd.org)
6 * All rights reserved.
7 *
8 * Portions of this software were developed by Rui Paulo under sponsorship
9 * from the FreeBSD Foundation.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/types.h>
34 #include <sys/ptrace.h>
35 #include <sys/wait.h>
36
37 #include <err.h>
38 #include <errno.h>
39 #include <signal.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <unistd.h>
43
44 #include "_libproc.h"
45
46 int
proc_clearflags(struct proc_handle * phdl,int mask)47 proc_clearflags(struct proc_handle *phdl, int mask)
48 {
49
50 if (phdl == NULL)
51 return (EINVAL);
52
53 phdl->flags &= ~mask;
54
55 return (0);
56 }
57
58 /*
59 * NB: we return -1 as the Solaris libproc Psetrun() function.
60 */
61 int
proc_continue(struct proc_handle * phdl)62 proc_continue(struct proc_handle *phdl)
63 {
64 int pending;
65
66 if (phdl == NULL)
67 return (-1);
68
69 if (phdl->status == PS_STOP && WSTOPSIG(phdl->wstat) != SIGTRAP)
70 pending = WSTOPSIG(phdl->wstat);
71 else
72 pending = 0;
73 if (ptrace(PT_CONTINUE, proc_getpid(phdl), (caddr_t)(uintptr_t)1,
74 pending) != 0)
75 return (-1);
76
77 phdl->status = PS_RUN;
78
79 return (0);
80 }
81
82 int
proc_detach(struct proc_handle * phdl,int reason)83 proc_detach(struct proc_handle *phdl, int reason)
84 {
85 int status;
86 int request;
87 pid_t pid;
88
89 if (phdl == NULL)
90 return (EINVAL);
91 if (reason == PRELEASE_HANG)
92 return (EINVAL);
93 if ((phdl->flags & PATTACH_RDONLY) != 0)
94 goto free;
95 request = (reason == PRELEASE_KILL) ? PT_KILL : PT_DETACH;
96 pid = proc_getpid(phdl);
97 if (ptrace(request, pid, 0, 0) != 0 && errno == EBUSY) {
98 kill(pid, SIGSTOP);
99 waitpid(pid, &status, WUNTRACED);
100 ptrace(request, pid, 0, 0);
101 kill(pid, SIGCONT);
102 }
103 free:
104 proc_free(phdl);
105 return (0);
106 }
107
108 int
proc_getflags(struct proc_handle * phdl)109 proc_getflags(struct proc_handle *phdl)
110 {
111
112 if (phdl == NULL)
113 return (-1);
114
115 return (phdl->flags);
116 }
117
118 int
proc_setflags(struct proc_handle * phdl,int mask)119 proc_setflags(struct proc_handle *phdl, int mask)
120 {
121
122 if (phdl == NULL)
123 return (EINVAL);
124
125 phdl->flags |= mask;
126
127 return (0);
128 }
129
130 int
proc_state(struct proc_handle * phdl)131 proc_state(struct proc_handle *phdl)
132 {
133
134 if (phdl == NULL)
135 return (-1);
136
137 return (phdl->status);
138 }
139
140 int
proc_getmodel(struct proc_handle * phdl)141 proc_getmodel(struct proc_handle *phdl)
142 {
143
144 if (phdl == NULL)
145 return (-1);
146
147 return (phdl->model);
148 }
149
150 int
proc_wstatus(struct proc_handle * phdl)151 proc_wstatus(struct proc_handle *phdl)
152 {
153 int status;
154
155 if (phdl == NULL)
156 return (-1);
157 if (waitpid(proc_getpid(phdl), &status, WUNTRACED) < 0) {
158 if (errno != EINTR)
159 DPRINTF("waitpid");
160 return (-1);
161 }
162 if (WIFSTOPPED(status))
163 phdl->status = PS_STOP;
164 if (WIFEXITED(status) || WIFSIGNALED(status))
165 phdl->status = PS_UNDEAD;
166 phdl->wstat = status;
167
168 return (phdl->status);
169 }
170
171 int
proc_getwstat(struct proc_handle * phdl)172 proc_getwstat(struct proc_handle *phdl)
173 {
174
175 if (phdl == NULL)
176 return (-1);
177
178 return (phdl->wstat);
179 }
180
181 char *
proc_signame(int sig,char * name,size_t namesz)182 proc_signame(int sig, char *name, size_t namesz)
183 {
184 char buf[SIG2STR_MAX];
185
186 if (sig2str(sig, buf) == 0)
187 (void)snprintf(name, namesz, "SIG%s", buf);
188 else
189 (void)snprintf(name, namesz, "SIG#%d", sig);
190
191 return (name);
192 }
193
194 int
proc_read(struct proc_handle * phdl,void * buf,size_t size,size_t addr)195 proc_read(struct proc_handle *phdl, void *buf, size_t size, size_t addr)
196 {
197 struct ptrace_io_desc piod;
198
199 if (phdl == NULL)
200 return (-1);
201 piod.piod_op = PIOD_READ_D;
202 piod.piod_len = size;
203 piod.piod_addr = (void *)buf;
204 piod.piod_offs = (void *)addr;
205
206 if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0)
207 return (-1);
208 return (piod.piod_len);
209 }
210
211 const lwpstatus_t *
proc_getlwpstatus(struct proc_handle * phdl)212 proc_getlwpstatus(struct proc_handle *phdl)
213 {
214 struct ptrace_lwpinfo lwpinfo;
215 lwpstatus_t *psp = &phdl->lwps;
216 siginfo_t *siginfo;
217
218 if (phdl == NULL)
219 return (NULL);
220 if (ptrace(PT_LWPINFO, proc_getpid(phdl), (caddr_t)&lwpinfo,
221 sizeof(lwpinfo)) < 0)
222 return (NULL);
223 siginfo = &lwpinfo.pl_siginfo;
224 if (lwpinfo.pl_event == PL_EVENT_SIGNAL &&
225 (lwpinfo.pl_flags & PL_FLAG_SI) != 0) {
226 if (siginfo->si_signo == SIGTRAP &&
227 (siginfo->si_code == TRAP_BRKPT ||
228 siginfo->si_code == TRAP_TRACE)) {
229 psp->pr_why = PR_FAULTED;
230 psp->pr_what = FLTBPT;
231 } else {
232 psp->pr_why = PR_SIGNALLED;
233 psp->pr_what = siginfo->si_signo;
234 }
235 } else if (lwpinfo.pl_flags & PL_FLAG_SCE) {
236 psp->pr_why = PR_SYSENTRY;
237 } else if (lwpinfo.pl_flags & PL_FLAG_SCX) {
238 psp->pr_why = PR_SYSEXIT;
239 }
240
241 return (psp);
242 }
243