Break paragraph with driver rewrite.
[netbsd-mini2440.git] / sys / miscfs / procfs / procfs_ctl.c
blobe96da03fb6febe8c08b2a1269a9ffa23d097b8a7
1 /* $NetBSD: procfs_ctl.c,v 1.45 2008/04/24 18:39:25 ad Exp $ */
3 /*
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Jan-Simon Pendry.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 * @(#)procfs_ctl.c 8.4 (Berkeley) 6/15/94
38 * Copyright (c) 1993 Jan-Simon Pendry
40 * This code is derived from software contributed to Berkeley by
41 * Jan-Simon Pendry.
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
71 * @(#)procfs_ctl.c 8.4 (Berkeley) 6/15/94
74 #include <sys/cdefs.h>
75 __KERNEL_RCSID(0, "$NetBSD: procfs_ctl.c,v 1.45 2008/04/24 18:39:25 ad Exp $");
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/time.h>
80 #include <sys/kernel.h>
81 #include <sys/proc.h>
82 #include <sys/vnode.h>
83 #include <sys/ioctl.h>
84 #include <sys/tty.h>
85 #include <sys/resource.h>
86 #include <sys/resourcevar.h>
87 #include <sys/signalvar.h>
88 #include <sys/kauth.h>
90 #include <uvm/uvm_extern.h>
92 #include <miscfs/procfs/procfs.h>
94 #define PROCFS_CTL_ATTACH 1
95 #define PROCFS_CTL_DETACH 2
96 #define PROCFS_CTL_STEP 3
97 #define PROCFS_CTL_RUN 4
98 #define PROCFS_CTL_WAIT 5
100 static const vfs_namemap_t ctlnames[] = {
101 /* special /proc commands */
102 { "attach", PROCFS_CTL_ATTACH },
103 { "detach", PROCFS_CTL_DETACH },
104 { "step", PROCFS_CTL_STEP },
105 { "run", PROCFS_CTL_RUN },
106 { "wait", PROCFS_CTL_WAIT },
107 { NULL, 0 },
110 static const vfs_namemap_t signames[] = {
111 /* regular signal names */
112 { "hup", SIGHUP }, { "int", SIGINT },
113 { "quit", SIGQUIT }, { "ill", SIGILL },
114 { "trap", SIGTRAP }, { "abrt", SIGABRT },
115 { "iot", SIGIOT }, { "emt", SIGEMT },
116 { "fpe", SIGFPE }, { "kill", SIGKILL },
117 { "bus", SIGBUS }, { "segv", SIGSEGV },
118 { "sys", SIGSYS }, { "pipe", SIGPIPE },
119 { "alrm", SIGALRM }, { "term", SIGTERM },
120 { "urg", SIGURG }, { "stop", SIGSTOP },
121 { "tstp", SIGTSTP }, { "cont", SIGCONT },
122 { "chld", SIGCHLD }, { "ttin", SIGTTIN },
123 { "ttou", SIGTTOU }, { "io", SIGIO },
124 { "xcpu", SIGXCPU }, { "xfsz", SIGXFSZ },
125 { "vtalrm", SIGVTALRM }, { "prof", SIGPROF },
126 { "winch", SIGWINCH }, { "info", SIGINFO },
127 { "usr1", SIGUSR1 }, { "usr2", SIGUSR2 },
128 { NULL, 0 },
131 static int procfs_control(struct lwp *, struct lwp *, int, int,
132 struct pfsnode *);
135 procfs_control(struct lwp *curl, struct lwp *l, int op, int sig, struct pfsnode *pfs)
137 struct proc *curp = curl->l_proc;
138 struct proc *p = l->l_proc;
139 int error = 0;
141 mutex_enter(proc_lock);
142 mutex_enter(p->p_lock);
144 switch (op) {
146 * Attach - attaches the target process for debugging
147 * by the calling process.
149 case PROCFS_CTL_ATTACH:
151 * You can't attach to a process if:
152 * (1) it's the process that's doing the attaching,
154 if (p->p_pid == curp->p_pid) {
155 error = EINVAL;
156 break;
160 * (2) it's already being traced, or
162 if (ISSET(p->p_slflag, PSL_TRACED)) {
163 error = EBUSY;
164 break;
168 * (3) the security model prevents it.
170 if ((error = kauth_authorize_process(curl->l_cred,
171 KAUTH_PROCESS_PROCFS, p, pfs,
172 KAUTH_ARG(KAUTH_REQ_PROCESS_PROCFS_CTL), NULL)) != 0)
173 break;
175 break;
178 * Target process must be stopped, owned by (curp) and
179 * be set up for tracing (PSL_TRACED flag set).
180 * Allow DETACH to take place at any time for sanity.
181 * Allow WAIT any time, of course.
183 * Note that the semantics of DETACH are different here
184 * from ptrace(2).
186 case PROCFS_CTL_DETACH:
187 case PROCFS_CTL_WAIT:
189 * You can't do what you want to the process if:
190 * (1) It's not being traced at all,
192 if (!ISSET(p->p_slflag, PSL_TRACED)) {
193 error = EPERM;
194 break;
198 * (2) it's being traced by ptrace(2) (which has
199 * different signal delivery semantics), or
201 if (!ISSET(p->p_slflag, PSL_FSTRACE)) {
202 error = EBUSY;
203 break;
207 * (3) it's not being traced by _you_.
209 if (p->p_pptr != curp)
210 error = EBUSY;
212 break;
214 default:
216 * You can't do what you want to the process if:
217 * (1) It's not being traced at all,
219 if (!ISSET(p->p_slflag, PSL_TRACED)) {
220 error = EPERM;
221 break;
225 * (2) it's being traced by ptrace(2) (which has
226 * different signal delivery semantics),
228 if (!ISSET(p->p_slflag, PSL_FSTRACE)) {
229 error = EBUSY;
230 break;
234 * (3) it's not being traced by _you_, or
236 if (p->p_pptr != curp) {
237 error = EBUSY;
238 break;
242 * (4) it's not currently stopped.
244 if (p->p_stat != SSTOP || !p->p_waited /* XXXSMP */)
245 error = EBUSY;
247 break;
250 if (error != 0) {
251 mutex_exit(p->p_lock);
252 mutex_exit(proc_lock);
253 return (error);
256 /* Do single-step fixup if needed. */
257 FIX_SSTEP(p);
259 switch (op) {
260 case PROCFS_CTL_ATTACH:
262 * Go ahead and set the trace flag.
263 * Save the old parent (it's reset in
264 * _DETACH, and also in kern_exit.c:wait4()
265 * Reparent the process so that the tracing
266 * proc gets to see all the action.
267 * Stop the target.
269 SET(p->p_slflag, PSL_TRACED|PSL_FSTRACE);
270 p->p_opptr = p->p_pptr;
271 if (p->p_pptr != curp) {
272 if (p->p_pptr->p_lock < p->p_lock) {
273 if (!mutex_tryenter(p->p_pptr->p_lock)) {
274 mutex_exit(p->p_lock);
275 mutex_enter(p->p_pptr->p_lock);
277 } else if (p->p_pptr->p_lock > p->p_lock) {
278 mutex_enter(p->p_pptr->p_lock);
280 p->p_pptr->p_slflag |= PSL_CHTRACED;
281 proc_reparent(p, curp);
282 if (p->p_pptr->p_lock != p->p_lock)
283 mutex_exit(p->p_pptr->p_lock);
285 sig = SIGSTOP;
286 goto sendsig;
288 #ifdef PT_STEP
289 case PROCFS_CTL_STEP:
291 * Step. Let the target process execute a single instruction.
293 #endif
294 case PROCFS_CTL_RUN:
295 case PROCFS_CTL_DETACH:
296 #ifdef PT_STEP
297 uvm_lwp_hold(l);
298 /* XXXAD locking? */
299 error = process_sstep(l, op == PROCFS_CTL_STEP);
300 uvm_lwp_rele(l);
301 if (error)
302 break;
303 #endif
305 if (op == PROCFS_CTL_DETACH) {
306 /* give process back to original parent */
307 if (p->p_opptr != p->p_pptr) {
308 struct proc *pp = p->p_opptr;
309 proc_reparent(p, pp ? pp : initproc);
312 /* not being traced any more */
313 p->p_opptr = NULL;
314 CLR(p->p_slflag, PSL_TRACED|PSL_FSTRACE);
315 p->p_waited = 0; /* XXXSMP */
318 sendsig:
319 /* Finally, deliver the requested signal (or none). */
320 lwp_lock(l);
321 if (l->l_stat == LSSTOP) {
322 p->p_xstat = sig;
323 /* setrunnable() will release the lock. */
324 setrunnable(l);
325 } else {
326 lwp_unlock(l);
327 if (sig != 0) {
328 mutex_exit(p->p_lock);
329 psignal(p, sig);
330 mutex_enter(p->p_lock);
333 mutex_exit(p->p_lock);
334 mutex_exit(proc_lock);
335 return (error);
337 case PROCFS_CTL_WAIT:
338 mutex_exit(p->p_lock);
339 mutex_exit(proc_lock);
342 * Wait for the target process to stop.
343 * XXXSMP WTF is this?
345 while (l->l_stat != LSSTOP && P_ZOMBIE(p)) {
346 error = tsleep(l, PWAIT|PCATCH, "procfsx", hz);
347 if (error)
348 break;
351 return (error);
353 default:
354 panic("procfs_ctl");
355 /* NOTREACHED */
358 mutex_exit(p->p_lock);
359 mutex_exit(proc_lock);
360 return (error);
364 procfs_doctl(
365 struct lwp *curl,
366 struct lwp *l,
367 struct pfsnode *pfs,
368 struct uio *uio
371 struct proc *p = l->l_proc;
372 char msg[PROCFS_CTLLEN+1];
373 const vfs_namemap_t *nm;
374 int error;
375 int xlen;
377 if (uio->uio_rw != UIO_WRITE)
378 return (EOPNOTSUPP);
380 xlen = PROCFS_CTLLEN;
381 error = vfs_getuserstr(uio, msg, &xlen);
382 if (error)
383 return (error);
386 * Map signal names into signal generation
387 * or debug control. Unknown commands and/or signals
388 * return EOPNOTSUPP.
390 * Sending a signal while the process is being debugged
391 * also has the side effect of letting the target continue
392 * to run. There is no way to single-step a signal delivery.
394 error = EOPNOTSUPP;
396 nm = vfs_findname(ctlnames, msg, xlen);
397 if (nm) {
398 error = procfs_control(curl, l, nm->nm_val, 0, pfs);
399 } else {
400 nm = vfs_findname(signames, msg, xlen);
401 if (nm) {
402 if (ISSET(p->p_slflag, PSL_TRACED) &&
403 p->p_pptr == p)
404 error = procfs_control(curl, l, PROCFS_CTL_RUN,
405 nm->nm_val, pfs);
406 else {
407 psignal(p, nm->nm_val);
408 error = 0;
413 return (error);