xref: /freebsd/lib/libproc/proc_util.c (revision b0b1dbdd)
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 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
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 	pid_t pid;
87 
88 	if (phdl == NULL)
89 		return (EINVAL);
90 	if (reason == PRELEASE_HANG)
91 		return (EINVAL);
92 	if (reason == PRELEASE_KILL) {
93 		kill(proc_getpid(phdl), SIGKILL);
94 		goto free;
95 	}
96 	if ((phdl->flags & PATTACH_RDONLY) != 0)
97 		goto free;
98 	pid = proc_getpid(phdl);
99 	if (ptrace(PT_DETACH, pid, 0, 0) != 0 && errno == ESRCH)
100 		goto free;
101 	if (errno == EBUSY) {
102 		kill(pid, SIGSTOP);
103 		waitpid(pid, &status, WUNTRACED);
104 		ptrace(PT_DETACH, pid, 0, 0);
105 		kill(pid, SIGCONT);
106 	}
107 free:
108 	proc_free(phdl);
109 	return (0);
110 }
111 
112 int
113 proc_getflags(struct proc_handle *phdl)
114 {
115 
116 	if (phdl == NULL)
117 		return (-1);
118 
119 	return (phdl->flags);
120 }
121 
122 int
123 proc_setflags(struct proc_handle *phdl, int mask)
124 {
125 
126 	if (phdl == NULL)
127 		return (EINVAL);
128 
129 	phdl->flags |= mask;
130 
131 	return (0);
132 }
133 
134 int
135 proc_state(struct proc_handle *phdl)
136 {
137 
138 	if (phdl == NULL)
139 		return (-1);
140 
141 	return (phdl->status);
142 }
143 
144 int
145 proc_getmodel(struct proc_handle *phdl)
146 {
147 
148 	if (phdl == NULL)
149 		return (-1);
150 
151 	return (phdl->model);
152 }
153 
154 int
155 proc_wstatus(struct proc_handle *phdl)
156 {
157 	int status;
158 
159 	if (phdl == NULL)
160 		return (-1);
161 	if (waitpid(proc_getpid(phdl), &status, WUNTRACED) < 0) {
162 		if (errno != EINTR)
163 			DPRINTF("waitpid");
164 		return (-1);
165 	}
166 	if (WIFSTOPPED(status))
167 		phdl->status = PS_STOP;
168 	if (WIFEXITED(status) || WIFSIGNALED(status))
169 		phdl->status = PS_UNDEAD;
170 	phdl->wstat = status;
171 
172 	return (phdl->status);
173 }
174 
175 int
176 proc_getwstat(struct proc_handle *phdl)
177 {
178 
179 	if (phdl == NULL)
180 		return (-1);
181 
182 	return (phdl->wstat);
183 }
184 
185 char *
186 proc_signame(int sig, char *name, size_t namesz)
187 {
188 
189 	strlcpy(name, strsignal(sig), namesz);
190 
191 	return (name);
192 }
193 
194 int
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 *
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