kernel - Add per-process token, adjust signal code to use it.
[dragonfly.git] / sys / vfs / procfs / procfs_ctl.c
CommitLineData
984263bc
MD
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 $
f33e8653 41 * $DragonFly: src/sys/vfs/procfs/procfs_ctl.c,v 1.16 2007/03/12 21:08:15 corecode Exp $
984263bc
MD
42 */
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/proc.h>
75bda2d9 47#include <sys/priv.h>
984263bc
MD
48#include <sys/vnode.h>
49#include <sys/ptrace.h>
50#include <sys/signalvar.h>
1f2de5d4 51#include <vfs/procfs/procfs.h>
984263bc 52
b5d16701
MD
53#include <sys/signal2.h>
54#include <sys/thread2.h>
55#include <sys/mplock2.h>
56
984263bc
MD
57#include <vm/vm.h>
58
59#ifndef FIX_SSTEP
c7e98b2f 60#define FIX_SSTEP(lp)
984263bc
MD
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) \
164b8401 68 (((p)->p_stat == SSTOP) && \
984263bc
MD
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
78static 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
88static 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
c7e98b2f 109static int procfs_control (struct proc *curp, struct lwp *lp, int op);
984263bc
MD
110
111static int
c7e98b2f 112procfs_control(struct proc *curp, struct lwp *lp, int op)
984263bc 113{
c7e98b2f 114 struct proc *p = lp->lwp_proc;
984263bc
MD
115 int error;
116
5686ec5a 117 ASSERT_LWKT_TOKEN_HELD(&p->p_token);
98c2b8ac
MD
118 ASSERT_LWKT_TOKEN_HELD(&proc_token);
119
984263bc
MD
120 /* Can't trace a process that's currently exec'ing. */
121 if ((p->p_flag & P_INEXEC) != 0)
122 return EAGAIN;
123 /*
124 * Authorization check: rely on normal debugging protection, except
125 * allow processes to disengage debugging on a process onto which
126 * they have previously attached, but no longer have permission to
127 * debug.
128 */
129 if (op != PROCFS_CTL_DETACH) {
130 if (securelevel > 0 && p->p_pid == 1)
131 return (EPERM);
132
41c20dac 133 if (!CHECKIO(curp, p) || p_trespass(curp->p_ucred, p->p_ucred))
984263bc
MD
134 return (EPERM);
135 }
136
137 /*
138 * Attach - attaches the target process for debugging
139 * by the calling process.
140 */
141 if (op == PROCFS_CTL_ATTACH) {
142 /* check whether already being traced */
143 if (p->p_flag & P_TRACED)
144 return (EBUSY);
145
146 /* can't trace yourself! */
147 if (p->p_pid == curp->p_pid)
148 return (EINVAL);
149
150 /*
151 * Go ahead and set the trace flag.
152 * Save the old parent (it's reset in
153 * _DETACH, and also in kern_exit.c:wait4()
154 * Reparent the process so that the tracing
155 * proc gets to see all the action.
156 * Stop the target.
157 */
158 p->p_flag |= P_TRACED;
159 faultin(p);
160 p->p_xstat = 0; /* XXX ? */
161 if (p->p_pptr != curp) {
162 p->p_oppid = p->p_pptr->p_pid;
163 proc_reparent(p, curp);
164 }
f33e8653 165 proc_stop(p);
984263bc
MD
166 return (0);
167 }
168
169 /*
170 * Target process must be stopped, owned by (curp) and
171 * be set up for tracing (P_TRACED flag set).
172 * Allow DETACH to take place at any time for sanity.
173 * Allow WAIT any time, of course.
174 */
175 switch (op) {
176 case PROCFS_CTL_DETACH:
177 case PROCFS_CTL_WAIT:
178 break;
179
180 default:
181 if (!TRACE_WAIT_P(curp, p))
182 return (EBUSY);
183 }
184
185
186#ifdef FIX_SSTEP
187 /*
188 * do single-step fixup if needed
189 */
c7e98b2f 190 FIX_SSTEP(lp);
984263bc
MD
191#endif
192
193 /*
194 * Don't deliver any signal by default.
195 * To continue with a signal, just send
196 * the signal name to the ctl file
197 */
198 p->p_xstat = 0;
199
200 switch (op) {
201 /*
202 * Detach. Cleans up the target process, reparent it if possible
203 * and set it running once more.
204 */
205 case PROCFS_CTL_DETACH:
206 /* if not being traced, then this is a painless no-op */
207 if ((p->p_flag & P_TRACED) == 0)
208 return (0);
209
210 /* not being traced any more */
211 p->p_flag &= ~P_TRACED;
212
213 /* remove pending SIGTRAP, else the process will die */
aa6c3de6 214 lwp_delsig(lp, SIGTRAP);
984263bc
MD
215
216 /* give process back to original parent */
217 if (p->p_oppid != p->p_pptr->p_pid) {
218 struct proc *pp;
219
220 pp = pfind(p->p_oppid);
221 if (pp)
222 proc_reparent(p, pp);
223 }
224
225 p->p_oppid = 0;
226 p->p_flag &= ~P_WAITED; /* XXX ? */
227 wakeup((caddr_t) curp); /* XXX for CTL_WAIT below ? */
228
229 break;
230
231 /*
232 * Step. Let the target process execute a single instruction.
233 */
234 case PROCFS_CTL_STEP:
c7e98b2f
SS
235 LWPHOLD(lp);
236 error = procfs_sstep(lp);
237 LWPRELE(lp);
984263bc
MD
238 if (error)
239 return (error);
240 break;
241
242 /*
243 * Run. Let the target process continue running until a breakpoint
244 * or some other trap.
245 */
246 case PROCFS_CTL_RUN:
247 break;
248
249 /*
250 * Wait for the target process to stop.
251 * If the target is not being traced then just wait
252 * to enter
253 */
254 case PROCFS_CTL_WAIT:
255 error = 0;
256 if (p->p_flag & P_TRACED) {
257 while (error == 0 &&
164b8401 258 p->p_stat != SSTOP &&
984263bc
MD
259 (p->p_flag & P_TRACED) &&
260 (p->p_pptr == curp)) {
261 error = tsleep((caddr_t) p,
377d4740 262 PCATCH, "procfsx", 0);
984263bc
MD
263 }
264 if (error == 0 && !TRACE_WAIT_P(curp, p))
265 error = EBUSY;
266 } else {
164b8401 267 while (error == 0 && p->p_stat != SSTOP) {
984263bc 268 error = tsleep((caddr_t) p,
377d4740 269 PCATCH, "procfs", 0);
984263bc
MD
270 }
271 }
272 return (error);
273
274 default:
275 panic("procfs_control");
276 }
277
344ad853
MD
278 /*
279 * If the process is in a stopped state, make it runnable again.
9a379a4a 280 * Do not set LWP_BREAKTSLEEP - that is, do not break a tsleep that
344ad853
MD
281 * might be in progress.
282 */
164b8401 283 if (p->p_stat == SSTOP)
9a379a4a 284 proc_unstop(p);
984263bc
MD
285 return (0);
286}
287
288int
c7e98b2f 289procfs_doctl(struct proc *curp, struct lwp *lp, struct pfsnode *pfs,
ac424f9b 290 struct uio *uio)
984263bc 291{
c7e98b2f 292 struct proc *p = lp->lwp_proc;
984263bc
MD
293 int xlen;
294 int error;
295 char msg[PROCFS_CTLLEN+1];
296 vfs_namemap_t *nm;
297
5686ec5a 298 ASSERT_LWKT_TOKEN_HELD(&p->p_token);
98c2b8ac
MD
299 ASSERT_LWKT_TOKEN_HELD(&proc_token);
300
984263bc
MD
301 if (uio->uio_rw != UIO_WRITE)
302 return (EOPNOTSUPP);
303
304 xlen = PROCFS_CTLLEN;
305 error = vfs_getuserstr(uio, msg, &xlen);
306 if (error)
307 return (error);
308
309 /*
310 * Map signal names into signal generation
311 * or debug control. Unknown commands and/or signals
312 * return EOPNOTSUPP.
313 *
314 * Sending a signal while the process is being debugged
315 * also has the side effect of letting the target continue
316 * to run. There is no way to single-step a signal delivery.
317 */
318 error = EOPNOTSUPP;
319
320 nm = vfs_findname(ctlnames, msg, xlen);
321 if (nm) {
c7e98b2f 322 error = procfs_control(curp, lp, nm->nm_val);
984263bc
MD
323 } else {
324 nm = vfs_findname(signames, msg, xlen);
325 if (nm) {
326 if (TRACE_WAIT_P(curp, p)) {
327 p->p_xstat = nm->nm_val;
328#ifdef FIX_SSTEP
c7e98b2f 329 FIX_SSTEP(lp);
984263bc 330#endif
344ad853
MD
331 /*
332 * Make the process runnable but do not
333 * break its tsleep.
334 */
9a379a4a 335 proc_unstop(p);
984263bc 336 } else {
84204577 337 ksignal(p, nm->nm_val);
984263bc
MD
338 }
339 error = 0;
340 }
341 }
342
343 return (error);
344}