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