Initial import from FreeBSD RELENG_4:
[games.git] / sys / vfs / procfs / procfs_ctl.c
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  */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/proc.h>
46 #include <sys/vnode.h>
47 #include <sys/ptrace.h>
48 #include <sys/signalvar.h>
49 #include <miscfs/procfs/procfs.h>
50
51 #include <vm/vm.h>
52
53 #ifndef FIX_SSTEP
54 #define FIX_SSTEP(p)
55 #endif
56
57 /*
58  * True iff process (p) is in trace wait state
59  * relative to process (curp)
60  */
61 #define TRACE_WAIT_P(curp, p) \
62         ((p)->p_stat == SSTOP && \
63          (p)->p_pptr == (curp) && \
64          ((p)->p_flag & P_TRACED))
65
66 #define PROCFS_CTL_ATTACH       1
67 #define PROCFS_CTL_DETACH       2
68 #define PROCFS_CTL_STEP         3
69 #define PROCFS_CTL_RUN          4
70 #define PROCFS_CTL_WAIT         5
71
72 static vfs_namemap_t ctlnames[] = {
73         /* special /proc commands */
74         { "attach",     PROCFS_CTL_ATTACH },
75         { "detach",     PROCFS_CTL_DETACH },
76         { "step",       PROCFS_CTL_STEP },
77         { "run",        PROCFS_CTL_RUN },
78         { "wait",       PROCFS_CTL_WAIT },
79         { 0 },
80 };
81
82 static vfs_namemap_t signames[] = {
83         /* regular signal names */
84         { "hup",        SIGHUP },       { "int",        SIGINT },
85         { "quit",       SIGQUIT },      { "ill",        SIGILL },
86         { "trap",       SIGTRAP },      { "abrt",       SIGABRT },
87         { "iot",        SIGIOT },       { "emt",        SIGEMT },
88         { "fpe",        SIGFPE },       { "kill",       SIGKILL },
89         { "bus",        SIGBUS },       { "segv",       SIGSEGV },
90         { "sys",        SIGSYS },       { "pipe",       SIGPIPE },
91         { "alrm",       SIGALRM },      { "term",       SIGTERM },
92         { "urg",        SIGURG },       { "stop",       SIGSTOP },
93         { "tstp",       SIGTSTP },      { "cont",       SIGCONT },
94         { "chld",       SIGCHLD },      { "ttin",       SIGTTIN },
95         { "ttou",       SIGTTOU },      { "io",         SIGIO },
96         { "xcpu",       SIGXCPU },      { "xfsz",       SIGXFSZ },
97         { "vtalrm",     SIGVTALRM },    { "prof",       SIGPROF },
98         { "winch",      SIGWINCH },     { "info",       SIGINFO },
99         { "usr1",       SIGUSR1 },      { "usr2",       SIGUSR2 },
100         { 0 },
101 };
102
103 static int      procfs_control __P((struct proc *curp, struct proc *p, int op));
104
105 static int
106 procfs_control(curp, p, op)
107         struct proc *curp;
108         struct proc *p;
109         int op;
110 {
111         int error;
112
113         /* Can't trace a process that's currently exec'ing. */ 
114         if ((p->p_flag & P_INEXEC) != 0)
115                 return EAGAIN;
116         /*
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.
121          */
122         if (op != PROCFS_CTL_DETACH) {
123                 if (securelevel > 0 && p->p_pid == 1)
124                         return (EPERM);
125
126                 if (!CHECKIO(curp, p) || p_trespass(curp, p))
127                         return (EPERM);
128         }
129
130         /*
131          * Attach - attaches the target process for debugging
132          * by the calling process.
133          */
134         if (op == PROCFS_CTL_ATTACH) {
135                 /* check whether already being traced */
136                 if (p->p_flag & P_TRACED)
137                         return (EBUSY);
138
139                 /* can't trace yourself! */
140                 if (p->p_pid == curp->p_pid)
141                         return (EINVAL);
142
143                 /*
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.
150                  */
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);
157                 }
158                 psignal(p, SIGSTOP);
159                 return (0);
160         }
161
162         /*
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.
167          */
168         switch (op) {
169         case PROCFS_CTL_DETACH:
170         case PROCFS_CTL_WAIT:
171                 break;
172
173         default:
174                 if (!TRACE_WAIT_P(curp, p))
175                         return (EBUSY);
176         }
177
178
179 #ifdef FIX_SSTEP
180         /*
181          * do single-step fixup if needed
182          */
183         FIX_SSTEP(p);
184 #endif
185
186         /*
187          * Don't deliver any signal by default.
188          * To continue with a signal, just send
189          * the signal name to the ctl file
190          */
191         p->p_xstat = 0;
192
193         switch (op) {
194         /*
195          * Detach.  Cleans up the target process, reparent it if possible
196          * and set it running once more.
197          */
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);
202
203                 /* not being traced any more */
204                 p->p_flag &= ~P_TRACED;
205
206                 /* remove pending SIGTRAP, else the process will die */
207                 SIGDELSET(p->p_siglist, SIGTRAP);
208
209                 /* give process back to original parent */
210                 if (p->p_oppid != p->p_pptr->p_pid) {
211                         struct proc *pp;
212
213                         pp = pfind(p->p_oppid);
214                         if (pp)
215                                 proc_reparent(p, pp);
216                 }
217
218                 p->p_oppid = 0;
219                 p->p_flag &= ~P_WAITED; /* XXX ? */
220                 wakeup((caddr_t) curp); /* XXX for CTL_WAIT below ? */
221
222                 break;
223
224         /*
225          * Step.  Let the target process execute a single instruction.
226          */
227         case PROCFS_CTL_STEP:
228                 PHOLD(p);
229                 error = procfs_sstep(p);
230                 PRELE(p);
231                 if (error)
232                         return (error);
233                 break;
234
235         /*
236          * Run.  Let the target process continue running until a breakpoint
237          * or some other trap.
238          */
239         case PROCFS_CTL_RUN:
240                 break;
241
242         /*
243          * Wait for the target process to stop.
244          * If the target is not being traced then just wait
245          * to enter
246          */
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                                                 PWAIT|PCATCH, "procfsx", 0);
256                         }
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                                                 PWAIT|PCATCH, "procfs", 0);
263                         }
264                 }
265                 return (error);
266
267         default:
268                 panic("procfs_control");
269         }
270
271         if (p->p_stat == SSTOP)
272                 setrunnable(p);
273         return (0);
274 }
275
276 int
277 procfs_doctl(curp, p, pfs, uio)
278         struct proc *curp;
279         struct pfsnode *pfs;
280         struct uio *uio;
281         struct proc *p;
282 {
283         int xlen;
284         int error;
285         char msg[PROCFS_CTLLEN+1];
286         vfs_namemap_t *nm;
287
288         if (uio->uio_rw != UIO_WRITE)
289                 return (EOPNOTSUPP);
290
291         xlen = PROCFS_CTLLEN;
292         error = vfs_getuserstr(uio, msg, &xlen);
293         if (error)
294                 return (error);
295
296         /*
297          * Map signal names into signal generation
298          * or debug control.  Unknown commands and/or signals
299          * return EOPNOTSUPP.
300          *
301          * Sending a signal while the process is being debugged
302          * also has the side effect of letting the target continue
303          * to run.  There is no way to single-step a signal delivery.
304          */
305         error = EOPNOTSUPP;
306
307         nm = vfs_findname(ctlnames, msg, xlen);
308         if (nm) {
309                 error = procfs_control(curp, p, nm->nm_val);
310         } else {
311                 nm = vfs_findname(signames, msg, xlen);
312                 if (nm) {
313                         if (TRACE_WAIT_P(curp, p)) {
314                                 p->p_xstat = nm->nm_val;
315 #ifdef FIX_SSTEP
316                                 FIX_SSTEP(p);
317 #endif
318                                 setrunnable(p);
319                         } else {
320                                 psignal(p, nm->nm_val);
321                         }
322                         error = 0;
323                 }
324         }
325
326         return (error);
327 }