Rework stopping of procs.
[dragonfly.git] / sys / vfs / procfs / procfs_ctl.c
blobf5b260ea0183b0594874220bacfa92b8e4487e17
1 /*
2 * Copyright (c) 1993 Jan-Simon Pendry
3 * Copyright (c) 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Jan-Simon Pendry.
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.
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.
37 * @(#)procfs_ctl.c 8.4 (Berkeley) 6/15/94
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 $
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/proc.h>
47 #include <sys/vnode.h>
48 #include <sys/ptrace.h>
49 #include <sys/signalvar.h>
50 #include <sys/signal2.h>
51 #include <vfs/procfs/procfs.h>
53 #include <vm/vm.h>
55 #ifndef FIX_SSTEP
56 #define FIX_SSTEP(lp)
57 #endif
60 * True iff process (p) is in trace wait state
61 * relative to process (curp)
63 #define TRACE_WAIT_P(curp, p) \
64 (((p)->p_stat == SSTOP) && \
65 (p)->p_pptr == (curp) && \
66 ((p)->p_flag & P_TRACED))
68 #define PROCFS_CTL_ATTACH 1
69 #define PROCFS_CTL_DETACH 2
70 #define PROCFS_CTL_STEP 3
71 #define PROCFS_CTL_RUN 4
72 #define PROCFS_CTL_WAIT 5
74 static vfs_namemap_t ctlnames[] = {
75 /* special /proc commands */
76 { "attach", PROCFS_CTL_ATTACH },
77 { "detach", PROCFS_CTL_DETACH },
78 { "step", PROCFS_CTL_STEP },
79 { "run", PROCFS_CTL_RUN },
80 { "wait", PROCFS_CTL_WAIT },
81 { 0 },
84 static vfs_namemap_t signames[] = {
85 /* regular signal names */
86 { "hup", SIGHUP }, { "int", SIGINT },
87 { "quit", SIGQUIT }, { "ill", SIGILL },
88 { "trap", SIGTRAP }, { "abrt", SIGABRT },
89 { "iot", SIGIOT }, { "emt", SIGEMT },
90 { "fpe", SIGFPE }, { "kill", SIGKILL },
91 { "bus", SIGBUS }, { "segv", SIGSEGV },
92 { "sys", SIGSYS }, { "pipe", SIGPIPE },
93 { "alrm", SIGALRM }, { "term", SIGTERM },
94 { "urg", SIGURG }, { "stop", SIGSTOP },
95 { "tstp", SIGTSTP }, { "cont", SIGCONT },
96 { "chld", SIGCHLD }, { "ttin", SIGTTIN },
97 { "ttou", SIGTTOU }, { "io", SIGIO },
98 { "xcpu", SIGXCPU }, { "xfsz", SIGXFSZ },
99 { "vtalrm", SIGVTALRM }, { "prof", SIGPROF },
100 { "winch", SIGWINCH }, { "info", SIGINFO },
101 { "usr1", SIGUSR1 }, { "usr2", SIGUSR2 },
102 { 0 },
105 static int procfs_control (struct proc *curp, struct lwp *lp, int op);
107 static int
108 procfs_control(struct proc *curp, struct lwp *lp, int op)
110 struct proc *p = lp->lwp_proc;
111 int error;
113 /* Can't trace a process that's currently exec'ing. */
114 if ((p->p_flag & P_INEXEC) != 0)
115 return EAGAIN;
117 * Authorization check: rely on normal debugging protection, except
118 * allow processes to disengage debugging on a process onto which
119 * they have previously attached, but no longer have permission to
120 * debug.
122 if (op != PROCFS_CTL_DETACH) {
123 if (securelevel > 0 && p->p_pid == 1)
124 return (EPERM);
126 if (!CHECKIO(curp, p) || p_trespass(curp->p_ucred, p->p_ucred))
127 return (EPERM);
131 * Attach - attaches the target process for debugging
132 * by the calling process.
134 if (op == PROCFS_CTL_ATTACH) {
135 /* check whether already being traced */
136 if (p->p_flag & P_TRACED)
137 return (EBUSY);
139 /* can't trace yourself! */
140 if (p->p_pid == curp->p_pid)
141 return (EINVAL);
144 * Go ahead and set the trace flag.
145 * Save the old parent (it's reset in
146 * _DETACH, and also in kern_exit.c:wait4()
147 * Reparent the process so that the tracing
148 * proc gets to see all the action.
149 * Stop the target.
151 p->p_flag |= P_TRACED;
152 faultin(p);
153 p->p_xstat = 0; /* XXX ? */
154 if (p->p_pptr != curp) {
155 p->p_oppid = p->p_pptr->p_pid;
156 proc_reparent(p, curp);
158 proc_stop(p);
159 return (0);
163 * Target process must be stopped, owned by (curp) and
164 * be set up for tracing (P_TRACED flag set).
165 * Allow DETACH to take place at any time for sanity.
166 * Allow WAIT any time, of course.
168 switch (op) {
169 case PROCFS_CTL_DETACH:
170 case PROCFS_CTL_WAIT:
171 break;
173 default:
174 if (!TRACE_WAIT_P(curp, p))
175 return (EBUSY);
179 #ifdef FIX_SSTEP
181 * do single-step fixup if needed
183 FIX_SSTEP(lp);
184 #endif
187 * Don't deliver any signal by default.
188 * To continue with a signal, just send
189 * the signal name to the ctl file
191 p->p_xstat = 0;
193 switch (op) {
195 * Detach. Cleans up the target process, reparent it if possible
196 * and set it running once more.
198 case PROCFS_CTL_DETACH:
199 /* if not being traced, then this is a painless no-op */
200 if ((p->p_flag & P_TRACED) == 0)
201 return (0);
203 /* not being traced any more */
204 p->p_flag &= ~P_TRACED;
206 /* remove pending SIGTRAP, else the process will die */
207 lwp_delsig(lp, SIGTRAP);
209 /* give process back to original parent */
210 if (p->p_oppid != p->p_pptr->p_pid) {
211 struct proc *pp;
213 pp = pfind(p->p_oppid);
214 if (pp)
215 proc_reparent(p, pp);
218 p->p_oppid = 0;
219 p->p_flag &= ~P_WAITED; /* XXX ? */
220 wakeup((caddr_t) curp); /* XXX for CTL_WAIT below ? */
222 break;
225 * Step. Let the target process execute a single instruction.
227 case PROCFS_CTL_STEP:
228 LWPHOLD(lp);
229 error = procfs_sstep(lp);
230 LWPRELE(lp);
231 if (error)
232 return (error);
233 break;
236 * Run. Let the target process continue running until a breakpoint
237 * or some other trap.
239 case PROCFS_CTL_RUN:
240 break;
243 * Wait for the target process to stop.
244 * If the target is not being traced then just wait
245 * to enter
247 case PROCFS_CTL_WAIT:
248 error = 0;
249 if (p->p_flag & P_TRACED) {
250 while (error == 0 &&
251 p->p_stat != SSTOP &&
252 (p->p_flag & P_TRACED) &&
253 (p->p_pptr == curp)) {
254 error = tsleep((caddr_t) p,
255 PCATCH, "procfsx", 0);
257 if (error == 0 && !TRACE_WAIT_P(curp, p))
258 error = EBUSY;
259 } else {
260 while (error == 0 && p->p_stat != SSTOP) {
261 error = tsleep((caddr_t) p,
262 PCATCH, "procfs", 0);
265 return (error);
267 default:
268 panic("procfs_control");
272 * If the process is in a stopped state, make it runnable again.
273 * Do not set LWP_BREAKTSLEEP - that is, do not break a tsleep that
274 * might be in progress.
276 if (p->p_stat == SSTOP)
277 proc_unstop(p);
278 return (0);
282 procfs_doctl(struct proc *curp, struct lwp *lp, struct pfsnode *pfs,
283 struct uio *uio)
285 struct proc *p = lp->lwp_proc;
286 int xlen;
287 int error;
288 char msg[PROCFS_CTLLEN+1];
289 vfs_namemap_t *nm;
291 if (uio->uio_rw != UIO_WRITE)
292 return (EOPNOTSUPP);
294 xlen = PROCFS_CTLLEN;
295 error = vfs_getuserstr(uio, msg, &xlen);
296 if (error)
297 return (error);
300 * Map signal names into signal generation
301 * or debug control. Unknown commands and/or signals
302 * return EOPNOTSUPP.
304 * Sending a signal while the process is being debugged
305 * also has the side effect of letting the target continue
306 * to run. There is no way to single-step a signal delivery.
308 error = EOPNOTSUPP;
310 nm = vfs_findname(ctlnames, msg, xlen);
311 if (nm) {
312 error = procfs_control(curp, lp, nm->nm_val);
313 } else {
314 nm = vfs_findname(signames, msg, xlen);
315 if (nm) {
316 if (TRACE_WAIT_P(curp, p)) {
317 p->p_xstat = nm->nm_val;
318 #ifdef FIX_SSTEP
319 FIX_SSTEP(lp);
320 #endif
322 * Make the process runnable but do not
323 * break its tsleep.
325 proc_unstop(p);
326 } else {
327 ksignal(p, nm->nm_val);
329 error = 0;
333 return (error);