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