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