1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
34__FBSDID("$FreeBSD$");
35
36#include <sys/types.h>
37#include <sys/ptrace.h>
38#include <sys/wait.h>
39
40#include <err.h>
41#include <errno.h>
42#include <signal.h>
43#include <string.h>
44#include <unistd.h>
45
46#include "_libproc.h"
47
48int
49proc_clearflags(struct proc_handle *phdl, int mask)
50{
51
52	if (phdl == NULL)
53		return (EINVAL);
54
55	phdl->flags &= ~mask;
56
57	return (0);
58}
59
60/*
61 * NB: we return -1 as the Solaris libproc Psetrun() function.
62 */
63int
64proc_continue(struct proc_handle *phdl)
65{
66	int pending;
67
68	if (phdl == NULL)
69		return (-1);
70
71	if (phdl->status == PS_STOP && WSTOPSIG(phdl->wstat) != SIGTRAP)
72		pending = WSTOPSIG(phdl->wstat);
73	else
74		pending = 0;
75	if (ptrace(PT_CONTINUE, proc_getpid(phdl), (caddr_t)(uintptr_t)1,
76	    pending) != 0)
77		return (-1);
78
79	phdl->status = PS_RUN;
80
81	return (0);
82}
83
84int
85proc_detach(struct proc_handle *phdl, int reason)
86{
87	int status;
88	pid_t pid;
89
90	if (phdl == NULL)
91		return (EINVAL);
92	if (reason == PRELEASE_HANG)
93		return (EINVAL);
94	if (reason == PRELEASE_KILL) {
95		kill(proc_getpid(phdl), SIGKILL);
96		goto free;
97	}
98	if ((phdl->flags & PATTACH_RDONLY) != 0)
99		goto free;
100	pid = proc_getpid(phdl);
101	if (ptrace(PT_DETACH, pid, 0, 0) != 0 && errno == ESRCH)
102		goto free;
103	if (errno == EBUSY) {
104		kill(pid, SIGSTOP);
105		waitpid(pid, &status, WUNTRACED);
106		ptrace(PT_DETACH, pid, 0, 0);
107		kill(pid, SIGCONT);
108	}
109free:
110	proc_free(phdl);
111	return (0);
112}
113
114int
115proc_getflags(struct proc_handle *phdl)
116{
117
118	if (phdl == NULL)
119		return (-1);
120
121	return (phdl->flags);
122}
123
124int
125proc_setflags(struct proc_handle *phdl, int mask)
126{
127
128	if (phdl == NULL)
129		return (EINVAL);
130
131	phdl->flags |= mask;
132
133	return (0);
134}
135
136int
137proc_state(struct proc_handle *phdl)
138{
139
140	if (phdl == NULL)
141		return (-1);
142
143	return (phdl->status);
144}
145
146int
147proc_getmodel(struct proc_handle *phdl)
148{
149
150	if (phdl == NULL)
151		return (-1);
152
153	return (phdl->model);
154}
155
156int
157proc_wstatus(struct proc_handle *phdl)
158{
159	int status;
160
161	if (phdl == NULL)
162		return (-1);
163	if (waitpid(proc_getpid(phdl), &status, WUNTRACED) < 0) {
164		if (errno != EINTR)
165			DPRINTF("waitpid");
166		return (-1);
167	}
168	if (WIFSTOPPED(status))
169		phdl->status = PS_STOP;
170	if (WIFEXITED(status) || WIFSIGNALED(status))
171		phdl->status = PS_UNDEAD;
172	phdl->wstat = status;
173
174	return (phdl->status);
175}
176
177int
178proc_getwstat(struct proc_handle *phdl)
179{
180
181	if (phdl == NULL)
182		return (-1);
183
184	return (phdl->wstat);
185}
186
187char *
188proc_signame(int sig, char *name, size_t namesz)
189{
190
191	strlcpy(name, strsignal(sig), namesz);
192
193	return (name);
194}
195
196int
197proc_read(struct proc_handle *phdl, void *buf, size_t size, size_t addr)
198{
199	struct ptrace_io_desc piod;
200
201	if (phdl == NULL)
202		return (-1);
203	piod.piod_op = PIOD_READ_D;
204	piod.piod_len = size;
205	piod.piod_addr = (void *)buf;
206	piod.piod_offs = (void *)addr;
207
208	if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0)
209		return (-1);
210	return (piod.piod_len);
211}
212
213const lwpstatus_t *
214proc_getlwpstatus(struct proc_handle *phdl)
215{
216	struct ptrace_lwpinfo lwpinfo;
217	lwpstatus_t *psp = &phdl->lwps;
218	siginfo_t *siginfo;
219
220	if (phdl == NULL)
221		return (NULL);
222	if (ptrace(PT_LWPINFO, proc_getpid(phdl), (caddr_t)&lwpinfo,
223	    sizeof(lwpinfo)) < 0)
224		return (NULL);
225	siginfo = &lwpinfo.pl_siginfo;
226	if (lwpinfo.pl_event == PL_EVENT_SIGNAL &&
227	    (lwpinfo.pl_flags & PL_FLAG_SI) != 0) {
228		if (siginfo->si_signo == SIGTRAP &&
229		    (siginfo->si_code == TRAP_BRKPT ||
230		    siginfo->si_code == TRAP_TRACE)) {
231			psp->pr_why = PR_FAULTED;
232			psp->pr_what = FLTBPT;
233		} else {
234			psp->pr_why = PR_SIGNALLED;
235			psp->pr_what = siginfo->si_signo;
236		}
237	} else if (lwpinfo.pl_flags & PL_FLAG_SCE) {
238		psp->pr_why = PR_SYSENTRY;
239	} else if (lwpinfo.pl_flags & PL_FLAG_SCX) {
240		psp->pr_why = PR_SYSEXIT;
241	}
242
243	return (psp);
244}
245