1 /* 2 * Copyright (c) 1993 Jan-Simon Pendry 3 * Copyright (c) 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * Jan-Simon Pendry. 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 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)procfs_ctl.c 8.4 (Berkeley) 6/15/94 38 * 39 * From: 40 * $FreeBSD: src/sys/miscfs/procfs/procfs_ctl.c,v 1.20.2.2 2002/01/22 17:22:59 nectar Exp $ 41 * $DragonFly: src/sys/vfs/procfs/procfs_ctl.c,v 1.16 2007/03/12 21:08:15 corecode Exp $ 42 */ 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/proc.h> 47 #include <sys/priv.h> 48 #include <sys/vnode.h> 49 #include <sys/ptrace.h> 50 #include <sys/signalvar.h> 51 #include <sys/signal2.h> 52 #include <vfs/procfs/procfs.h> 53 54 #include <vm/vm.h> 55 56 #ifndef FIX_SSTEP 57 #define FIX_SSTEP(lp) 58 #endif 59 60 /* 61 * True iff process (p) is in trace wait state 62 * relative to process (curp) 63 */ 64 #define TRACE_WAIT_P(curp, p) \ 65 (((p)->p_stat == SSTOP) && \ 66 (p)->p_pptr == (curp) && \ 67 ((p)->p_flag & P_TRACED)) 68 69 #define PROCFS_CTL_ATTACH 1 70 #define PROCFS_CTL_DETACH 2 71 #define PROCFS_CTL_STEP 3 72 #define PROCFS_CTL_RUN 4 73 #define PROCFS_CTL_WAIT 5 74 75 static vfs_namemap_t ctlnames[] = { 76 /* special /proc commands */ 77 { "attach", PROCFS_CTL_ATTACH }, 78 { "detach", PROCFS_CTL_DETACH }, 79 { "step", PROCFS_CTL_STEP }, 80 { "run", PROCFS_CTL_RUN }, 81 { "wait", PROCFS_CTL_WAIT }, 82 { 0 }, 83 }; 84 85 static vfs_namemap_t signames[] = { 86 /* regular signal names */ 87 { "hup", SIGHUP }, { "int", SIGINT }, 88 { "quit", SIGQUIT }, { "ill", SIGILL }, 89 { "trap", SIGTRAP }, { "abrt", SIGABRT }, 90 { "iot", SIGIOT }, { "emt", SIGEMT }, 91 { "fpe", SIGFPE }, { "kill", SIGKILL }, 92 { "bus", SIGBUS }, { "segv", SIGSEGV }, 93 { "sys", SIGSYS }, { "pipe", SIGPIPE }, 94 { "alrm", SIGALRM }, { "term", SIGTERM }, 95 { "urg", SIGURG }, { "stop", SIGSTOP }, 96 { "tstp", SIGTSTP }, { "cont", SIGCONT }, 97 { "chld", SIGCHLD }, { "ttin", SIGTTIN }, 98 { "ttou", SIGTTOU }, { "io", SIGIO }, 99 { "xcpu", SIGXCPU }, { "xfsz", SIGXFSZ }, 100 { "vtalrm", SIGVTALRM }, { "prof", SIGPROF }, 101 { "winch", SIGWINCH }, { "info", SIGINFO }, 102 { "usr1", SIGUSR1 }, { "usr2", SIGUSR2 }, 103 { 0 }, 104 }; 105 106 static int procfs_control (struct proc *curp, struct lwp *lp, int op); 107 108 static int 109 procfs_control(struct proc *curp, struct lwp *lp, int op) 110 { 111 struct proc *p = lp->lwp_proc; 112 int error; 113 114 /* Can't trace a process that's currently exec'ing. */ 115 if ((p->p_flag & P_INEXEC) != 0) 116 return EAGAIN; 117 /* 118 * Authorization check: rely on normal debugging protection, except 119 * allow processes to disengage debugging on a process onto which 120 * they have previously attached, but no longer have permission to 121 * debug. 122 */ 123 if (op != PROCFS_CTL_DETACH) { 124 if (securelevel > 0 && p->p_pid == 1) 125 return (EPERM); 126 127 if (!CHECKIO(curp, p) || p_trespass(curp->p_ucred, p->p_ucred)) 128 return (EPERM); 129 } 130 131 /* 132 * Attach - attaches the target process for debugging 133 * by the calling process. 134 */ 135 if (op == PROCFS_CTL_ATTACH) { 136 /* check whether already being traced */ 137 if (p->p_flag & P_TRACED) 138 return (EBUSY); 139 140 /* can't trace yourself! */ 141 if (p->p_pid == curp->p_pid) 142 return (EINVAL); 143 144 /* 145 * Go ahead and set the trace flag. 146 * Save the old parent (it's reset in 147 * _DETACH, and also in kern_exit.c:wait4() 148 * Reparent the process so that the tracing 149 * proc gets to see all the action. 150 * Stop the target. 151 */ 152 p->p_flag |= P_TRACED; 153 faultin(p); 154 p->p_xstat = 0; /* XXX ? */ 155 if (p->p_pptr != curp) { 156 p->p_oppid = p->p_pptr->p_pid; 157 proc_reparent(p, curp); 158 } 159 proc_stop(p); 160 return (0); 161 } 162 163 /* 164 * Target process must be stopped, owned by (curp) and 165 * be set up for tracing (P_TRACED flag set). 166 * Allow DETACH to take place at any time for sanity. 167 * Allow WAIT any time, of course. 168 */ 169 switch (op) { 170 case PROCFS_CTL_DETACH: 171 case PROCFS_CTL_WAIT: 172 break; 173 174 default: 175 if (!TRACE_WAIT_P(curp, p)) 176 return (EBUSY); 177 } 178 179 180 #ifdef FIX_SSTEP 181 /* 182 * do single-step fixup if needed 183 */ 184 FIX_SSTEP(lp); 185 #endif 186 187 /* 188 * Don't deliver any signal by default. 189 * To continue with a signal, just send 190 * the signal name to the ctl file 191 */ 192 p->p_xstat = 0; 193 194 switch (op) { 195 /* 196 * Detach. Cleans up the target process, reparent it if possible 197 * and set it running once more. 198 */ 199 case PROCFS_CTL_DETACH: 200 /* if not being traced, then this is a painless no-op */ 201 if ((p->p_flag & P_TRACED) == 0) 202 return (0); 203 204 /* not being traced any more */ 205 p->p_flag &= ~P_TRACED; 206 207 /* remove pending SIGTRAP, else the process will die */ 208 lwp_delsig(lp, SIGTRAP); 209 210 /* give process back to original parent */ 211 if (p->p_oppid != p->p_pptr->p_pid) { 212 struct proc *pp; 213 214 pp = pfind(p->p_oppid); 215 if (pp) 216 proc_reparent(p, pp); 217 } 218 219 p->p_oppid = 0; 220 p->p_flag &= ~P_WAITED; /* XXX ? */ 221 wakeup((caddr_t) curp); /* XXX for CTL_WAIT below ? */ 222 223 break; 224 225 /* 226 * Step. Let the target process execute a single instruction. 227 */ 228 case PROCFS_CTL_STEP: 229 LWPHOLD(lp); 230 error = procfs_sstep(lp); 231 LWPRELE(lp); 232 if (error) 233 return (error); 234 break; 235 236 /* 237 * Run. Let the target process continue running until a breakpoint 238 * or some other trap. 239 */ 240 case PROCFS_CTL_RUN: 241 break; 242 243 /* 244 * Wait for the target process to stop. 245 * If the target is not being traced then just wait 246 * to enter 247 */ 248 case PROCFS_CTL_WAIT: 249 error = 0; 250 if (p->p_flag & P_TRACED) { 251 while (error == 0 && 252 p->p_stat != SSTOP && 253 (p->p_flag & P_TRACED) && 254 (p->p_pptr == curp)) { 255 error = tsleep((caddr_t) p, 256 PCATCH, "procfsx", 0); 257 } 258 if (error == 0 && !TRACE_WAIT_P(curp, p)) 259 error = EBUSY; 260 } else { 261 while (error == 0 && p->p_stat != SSTOP) { 262 error = tsleep((caddr_t) p, 263 PCATCH, "procfs", 0); 264 } 265 } 266 return (error); 267 268 default: 269 panic("procfs_control"); 270 } 271 272 /* 273 * If the process is in a stopped state, make it runnable again. 274 * Do not set LWP_BREAKTSLEEP - that is, do not break a tsleep that 275 * might be in progress. 276 */ 277 if (p->p_stat == SSTOP) 278 proc_unstop(p); 279 return (0); 280 } 281 282 int 283 procfs_doctl(struct proc *curp, struct lwp *lp, struct pfsnode *pfs, 284 struct uio *uio) 285 { 286 struct proc *p = lp->lwp_proc; 287 int xlen; 288 int error; 289 char msg[PROCFS_CTLLEN+1]; 290 vfs_namemap_t *nm; 291 292 if (uio->uio_rw != UIO_WRITE) 293 return (EOPNOTSUPP); 294 295 xlen = PROCFS_CTLLEN; 296 error = vfs_getuserstr(uio, msg, &xlen); 297 if (error) 298 return (error); 299 300 /* 301 * Map signal names into signal generation 302 * or debug control. Unknown commands and/or signals 303 * return EOPNOTSUPP. 304 * 305 * Sending a signal while the process is being debugged 306 * also has the side effect of letting the target continue 307 * to run. There is no way to single-step a signal delivery. 308 */ 309 error = EOPNOTSUPP; 310 311 nm = vfs_findname(ctlnames, msg, xlen); 312 if (nm) { 313 error = procfs_control(curp, lp, nm->nm_val); 314 } else { 315 nm = vfs_findname(signames, msg, xlen); 316 if (nm) { 317 if (TRACE_WAIT_P(curp, p)) { 318 p->p_xstat = nm->nm_val; 319 #ifdef FIX_SSTEP 320 FIX_SSTEP(lp); 321 #endif 322 /* 323 * Make the process runnable but do not 324 * break its tsleep. 325 */ 326 proc_unstop(p); 327 } else { 328 ksignal(p, nm->nm_val); 329 } 330 error = 0; 331 } 332 } 333 334 return (error); 335 } 336