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 <string.h> 40 #include <unistd.h> 41 #include "_libproc.h" 42 43 int 44 proc_clearflags(struct proc_handle *phdl, int mask) 45 { 46 47 if (phdl == NULL) 48 return (EINVAL); 49 50 phdl->flags &= ~mask; 51 52 return (0); 53 } 54 55 /* 56 * NB: we return -1 as the Solaris libproc Psetrun() function. 57 */ 58 int 59 proc_continue(struct proc_handle *phdl) 60 { 61 int pending = 0; 62 63 if (phdl == NULL) 64 return (-1); 65 66 if (phdl->status == PS_STOP && WSTOPSIG(phdl->wstat) != SIGTRAP) 67 pending = WSTOPSIG(phdl->wstat); 68 if (ptrace(PT_CONTINUE, phdl->pid, (void *)(uintptr_t)1, pending) != 0) 69 return (-1); 70 71 phdl->status = PS_RUN; 72 73 return (0); 74 } 75 76 int 77 proc_detach(struct proc_handle *phdl, int reason) 78 { 79 int status; 80 81 if (phdl == NULL) 82 return EINVAL; 83 if (reason == PRELEASE_KILL) { 84 ptrace(PT_DETACH, phdl->pid, (void *)(uintptr_t)1, 0); 85 kill(phdl->pid, SIGKILL); 86 return 0; 87 } 88 if (ptrace(PT_DETACH, phdl->pid, (void *)(uintptr_t)1, 0) == 0) 89 return 0; 90 91 switch (errno) { 92 case ESRCH: 93 return 0; 94 case EBUSY: 95 break; 96 default: 97 return -1; 98 } 99 100 if (kill(phdl->pid, SIGSTOP) == -1) 101 return -1; 102 103 waitpid(phdl->pid, &status, WUNTRACED); 104 105 if (ptrace(PT_DETACH, phdl->pid, (void *)(uintptr_t)1, 0) == -1) 106 return -1; 107 108 if (kill(phdl->pid, SIGCONT) == -1) 109 return -1; 110 111 return 0; 112 } 113 114 int 115 proc_getflags(struct proc_handle *phdl) 116 { 117 118 if (phdl == NULL) 119 return (-1); 120 121 return(phdl->flags); 122 } 123 124 int 125 proc_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 136 int 137 proc_state(struct proc_handle *phdl) 138 { 139 140 if (phdl == NULL) 141 return (-1); 142 143 return (phdl->status); 144 } 145 146 pid_t 147 proc_getpid(struct proc_handle *phdl) 148 { 149 150 if (phdl == NULL) 151 return (-1); 152 153 return (phdl->pid); 154 } 155 156 int 157 proc_wstatus(struct proc_handle *phdl) 158 { 159 int status; 160 161 if (phdl == NULL) 162 return (-1); 163 if (waitpid(phdl->pid, &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 177 int 178 proc_getwstat(struct proc_handle *phdl) 179 { 180 181 if (phdl == NULL) 182 return (-1); 183 184 return (phdl->wstat); 185 } 186 187 char * 188 proc_signame(int sig, char *name, size_t namesz) 189 { 190 191 strlcpy(name, strsignal(sig), namesz); 192 193 return (name); 194 } 195 196 int 197 proc_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, phdl->pid, (void *)&piod, 0) < 0) 209 return (-1); 210 return (piod.piod_len); 211 } 212 213 const lwpstatus_t * 214 proc_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, phdl->pid, (void *)&lwpinfo, 223 sizeof(lwpinfo)) < 0) 224 return (NULL); 225 #ifdef PL_FLAG_SI 226 siginfo = &lwpinfo.pl_siginfo; 227 if (lwpinfo.pl_event == PL_EVENT_SIGNAL && 228 (lwpinfo.pl_flags & PL_FLAG_SI) != 0) { 229 if (siginfo->si_signo == SIGTRAP && 230 (siginfo->si_code == TRAP_BRKPT || 231 siginfo->si_code == TRAP_TRACE)) { 232 psp->pr_why = PR_FAULTED; 233 psp->pr_what = FLTBPT; 234 } else { 235 psp->pr_why = PR_SIGNALLED; 236 psp->pr_what = siginfo->si_signo; 237 } 238 } else if (lwpinfo.pl_flags & PL_FLAG_SCE) { 239 psp->pr_why = PR_SYSENTRY; 240 } else if (lwpinfo.pl_flags & PL_FLAG_SCX) { 241 psp->pr_why = PR_SYSEXIT; 242 } 243 #endif 244 245 return (psp); 246 } 247