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