xref: /freebsd/lib/libproc/proc_util.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
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/cdefs.h>
34 #include <sys/types.h>
35 #include <sys/ptrace.h>
36 #include <sys/wait.h>
37 
38 #include <err.h>
39 #include <errno.h>
40 #include <signal.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 #include "_libproc.h"
45 
46 int
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
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
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
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
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
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
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
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
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 *
182 proc_signame(int sig, char *name, size_t namesz)
183 {
184 
185 	strlcpy(name, strsignal(sig), namesz);
186 
187 	return (name);
188 }
189 
190 int
191 proc_read(struct proc_handle *phdl, void *buf, size_t size, size_t addr)
192 {
193 	struct ptrace_io_desc piod;
194 
195 	if (phdl == NULL)
196 		return (-1);
197 	piod.piod_op = PIOD_READ_D;
198 	piod.piod_len = size;
199 	piod.piod_addr = (void *)buf;
200 	piod.piod_offs = (void *)addr;
201 
202 	if (ptrace(PT_IO, proc_getpid(phdl), (caddr_t)&piod, 0) < 0)
203 		return (-1);
204 	return (piod.piod_len);
205 }
206 
207 const lwpstatus_t *
208 proc_getlwpstatus(struct proc_handle *phdl)
209 {
210 	struct ptrace_lwpinfo lwpinfo;
211 	lwpstatus_t *psp = &phdl->lwps;
212 	siginfo_t *siginfo;
213 
214 	if (phdl == NULL)
215 		return (NULL);
216 	if (ptrace(PT_LWPINFO, proc_getpid(phdl), (caddr_t)&lwpinfo,
217 	    sizeof(lwpinfo)) < 0)
218 		return (NULL);
219 	siginfo = &lwpinfo.pl_siginfo;
220 	if (lwpinfo.pl_event == PL_EVENT_SIGNAL &&
221 	    (lwpinfo.pl_flags & PL_FLAG_SI) != 0) {
222 		if (siginfo->si_signo == SIGTRAP &&
223 		    (siginfo->si_code == TRAP_BRKPT ||
224 		    siginfo->si_code == TRAP_TRACE)) {
225 			psp->pr_why = PR_FAULTED;
226 			psp->pr_what = FLTBPT;
227 		} else {
228 			psp->pr_why = PR_SIGNALLED;
229 			psp->pr_what = siginfo->si_signo;
230 		}
231 	} else if (lwpinfo.pl_flags & PL_FLAG_SCE) {
232 		psp->pr_why = PR_SYSENTRY;
233 	} else if (lwpinfo.pl_flags & PL_FLAG_SCX) {
234 		psp->pr_why = PR_SYSEXIT;
235 	}
236 
237 	return (psp);
238 }
239