xref: /dragonfly/sys/vfs/procfs/procfs_ctl.c (revision 3170ffd7)
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  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)procfs_ctl.c	8.4 (Berkeley) 6/15/94
34  *
35  * From:
36  * $FreeBSD: src/sys/miscfs/procfs/procfs_ctl.c,v 1.20.2.2 2002/01/22 17:22:59 nectar Exp $
37  * $DragonFly: src/sys/vfs/procfs/procfs_ctl.c,v 1.16 2007/03/12 21:08:15 corecode Exp $
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/proc.h>
43 #include <sys/priv.h>
44 #include <sys/vnode.h>
45 #include <sys/ptrace.h>
46 #include <sys/signalvar.h>
47 #include <vfs/procfs/procfs.h>
48 
49 #include <sys/signal2.h>
50 #include <sys/thread2.h>
51 #include <sys/mplock2.h>
52 #include <sys/spinlock2.h>
53 
54 #include <vm/vm.h>
55 
56 #ifndef FIX_SSTEP
57 #define FIX_SSTEP(lp)
58 #endif
59 
60 /*
61  * True iff process (p) is in trace wait state
62  * relative to process (curp)
63  */
64 #define TRACE_WAIT_P(curp, p) \
65 	(((p)->p_stat == SSTOP) && \
66 	 (p)->p_pptr == (curp) && \
67 	 ((p)->p_flags & P_TRACED))
68 
69 #define PROCFS_CTL_ATTACH	1
70 #define PROCFS_CTL_DETACH	2
71 #define PROCFS_CTL_STEP		3
72 #define PROCFS_CTL_RUN		4
73 #define PROCFS_CTL_WAIT		5
74 
75 static vfs_namemap_t ctlnames[] = {
76 	/* special /proc commands */
77 	{ "attach",	PROCFS_CTL_ATTACH },
78 	{ "detach",	PROCFS_CTL_DETACH },
79 	{ "step",	PROCFS_CTL_STEP },
80 	{ "run",	PROCFS_CTL_RUN },
81 	{ "wait",	PROCFS_CTL_WAIT },
82 	{ 0 },
83 };
84 
85 static vfs_namemap_t signames[] = {
86 	/* regular signal names */
87 	{ "hup",	SIGHUP },	{ "int",	SIGINT },
88 	{ "quit",	SIGQUIT },	{ "ill",	SIGILL },
89 	{ "trap",	SIGTRAP },	{ "abrt",	SIGABRT },
90 	{ "iot",	SIGIOT },	{ "emt",	SIGEMT },
91 	{ "fpe",	SIGFPE },	{ "kill",	SIGKILL },
92 	{ "bus",	SIGBUS },	{ "segv",	SIGSEGV },
93 	{ "sys",	SIGSYS },	{ "pipe",	SIGPIPE },
94 	{ "alrm",	SIGALRM },	{ "term",	SIGTERM },
95 	{ "urg",	SIGURG },	{ "stop",	SIGSTOP },
96 	{ "tstp",	SIGTSTP },	{ "cont",	SIGCONT },
97 	{ "chld",	SIGCHLD },	{ "ttin",	SIGTTIN },
98 	{ "ttou",	SIGTTOU },	{ "io",		SIGIO },
99 	{ "xcpu",	SIGXCPU },	{ "xfsz",	SIGXFSZ },
100 	{ "vtalrm",	SIGVTALRM },	{ "prof",	SIGPROF },
101 	{ "winch",	SIGWINCH },	{ "info",	SIGINFO },
102 	{ "usr1",	SIGUSR1 },	{ "usr2",	SIGUSR2 },
103 	{ 0 },
104 };
105 
106 static int	procfs_control (struct proc *curp, struct lwp *lp, int op);
107 
108 static int
109 procfs_control(struct proc *curp, struct lwp *lp, int op)
110 {
111 	struct proc *p = lp->lwp_proc;
112 	int error;
113 
114 	ASSERT_LWKT_TOKEN_HELD(&p->p_token);
115 	ASSERT_LWKT_TOKEN_HELD(&proc_token);
116 
117 	/* Can't trace a process that's currently exec'ing. */
118 	if ((p->p_flags & P_INEXEC) != 0)
119 		return EAGAIN;
120 	/*
121 	 * Authorization check: rely on normal debugging protection, except
122 	 * allow processes to disengage debugging on a process onto which
123 	 * they have previously attached, but no longer have permission to
124 	 * debug.
125 	 */
126 	if (op != PROCFS_CTL_DETACH) {
127 		if (securelevel > 0 && p->p_pid == 1)
128 			return (EPERM);
129 
130 		if (!CHECKIO(curp, p) || p_trespass(curp->p_ucred, p->p_ucred))
131 			return (EPERM);
132 	}
133 
134 	/*
135 	 * Attach - attaches the target process for debugging
136 	 * by the calling process.
137 	 */
138 	if (op == PROCFS_CTL_ATTACH) {
139 		/* check whether already being traced */
140 		if (p->p_flags & P_TRACED)
141 			return (EBUSY);
142 
143 		/* can't trace yourself! */
144 		if (p->p_pid == curp->p_pid)
145 			return (EINVAL);
146 
147 		/*
148 		 * Go ahead and set the trace flag.
149 		 * Save the old parent (it's reset in
150 		 *   _DETACH, and also in kern_exit.c:wait4()
151 		 * Reparent the process so that the tracing
152 		 *   proc gets to see all the action.
153 		 * Stop the target.
154 		 */
155 		p->p_flags |= P_TRACED;
156 		faultin(p);
157 		p->p_xstat = 0;		/* XXX ? */
158 		if (p->p_pptr != curp) {
159 			p->p_oppid = p->p_pptr->p_pid;
160 			proc_reparent(p, curp);
161 		}
162 		proc_stop(p);
163 		return (0);
164 	}
165 
166 	/*
167 	 * Target process must be stopped, owned by (curp) and
168 	 * be set up for tracing (P_TRACED flag set).
169 	 * Allow DETACH to take place at any time for sanity.
170 	 * Allow WAIT any time, of course.
171 	 */
172 	switch (op) {
173 	case PROCFS_CTL_DETACH:
174 	case PROCFS_CTL_WAIT:
175 		break;
176 
177 	default:
178 		if (!TRACE_WAIT_P(curp, p))
179 			return (EBUSY);
180 	}
181 
182 
183 #ifdef FIX_SSTEP
184 	/*
185 	 * do single-step fixup if needed
186 	 */
187 	FIX_SSTEP(lp);
188 #endif
189 
190 	/*
191 	 * Don't deliver any signal by default.
192 	 * To continue with a signal, just send
193 	 * the signal name to the ctl file
194 	 */
195 	p->p_xstat = 0;
196 
197 	switch (op) {
198 	/*
199 	 * Detach.  Cleans up the target process, reparent it if possible
200 	 * and set it running once more.
201 	 */
202 	case PROCFS_CTL_DETACH:
203 		/* if not being traced, then this is a painless no-op */
204 		if ((p->p_flags & P_TRACED) == 0)
205 			return (0);
206 
207 		/* not being traced any more */
208 		p->p_flags &= ~P_TRACED;
209 
210 		/* remove pending SIGTRAP, else the process will die */
211 		spin_lock(&lp->lwp_spin);
212 		lwp_delsig(lp, SIGTRAP);
213 		spin_unlock(&lp->lwp_spin);
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 = pfs_pfind(p->p_oppid);
220 			if (pp) {
221 				proc_reparent(p, pp);
222 				pfs_pdone(pp);
223 			}
224 		}
225 
226 		p->p_oppid = 0;
227 		p->p_flags &= ~P_WAITED;	/* XXX ? */
228 		wakeup((caddr_t) curp);		/* XXX for CTL_WAIT below ? */
229 
230 		break;
231 
232 	/*
233 	 * Step.  Let the target process execute a single instruction.
234 	 */
235 	case PROCFS_CTL_STEP:
236 		LWPHOLD(lp);
237 		error = procfs_sstep(lp);
238 		LWPRELE(lp);
239 		if (error)
240 			return (error);
241 		break;
242 
243 	/*
244 	 * Run.  Let the target process continue running until a breakpoint
245 	 * or some other trap.
246 	 */
247 	case PROCFS_CTL_RUN:
248 		break;
249 
250 	/*
251 	 * Wait for the target process to stop.
252 	 * If the target is not being traced then just wait
253 	 * to enter
254 	 */
255 	case PROCFS_CTL_WAIT:
256 		error = 0;
257 		if (p->p_flags & P_TRACED) {
258 			while (error == 0 &&
259 					p->p_stat != SSTOP &&
260 					(p->p_flags & P_TRACED) &&
261 					(p->p_pptr == curp)) {
262 				error = tsleep((caddr_t) p,
263 						PCATCH, "procfsx", 0);
264 			}
265 			if (error == 0 && !TRACE_WAIT_P(curp, p))
266 				error = EBUSY;
267 		} else {
268 			while (error == 0 && p->p_stat != SSTOP) {
269 				error = tsleep((caddr_t) p,
270 						PCATCH, "procfs", 0);
271 			}
272 		}
273 		return (error);
274 
275 	default:
276 		panic("procfs_control");
277 	}
278 
279 	/*
280 	 * If the process is in a stopped state, make it runnable again.
281 	 * Do not set LWP_MP_BREAKTSLEEP - that is, do not break a tsleep
282 	 * that might be in progress.
283 	 */
284 	if (p->p_stat == SSTOP)
285 		proc_unstop(p);
286 	return (0);
287 }
288 
289 int
290 procfs_doctl(struct proc *curp, struct lwp *lp, struct pfsnode *pfs,
291 	     struct uio *uio)
292 {
293 	struct proc *p = lp->lwp_proc;
294 	int xlen;
295 	int error;
296 	char msg[PROCFS_CTLLEN+1];
297 	vfs_namemap_t *nm;
298 
299 	ASSERT_LWKT_TOKEN_HELD(&p->p_token);
300 	ASSERT_LWKT_TOKEN_HELD(&proc_token);
301 
302 	if (uio->uio_rw != UIO_WRITE)
303 		return (EOPNOTSUPP);
304 
305 	xlen = PROCFS_CTLLEN;
306 	error = vfs_getuserstr(uio, msg, &xlen);
307 	if (error)
308 		return (error);
309 
310 	/*
311 	 * Map signal names into signal generation
312 	 * or debug control.  Unknown commands and/or signals
313 	 * return EOPNOTSUPP.
314 	 *
315 	 * Sending a signal while the process is being debugged
316 	 * also has the side effect of letting the target continue
317 	 * to run.  There is no way to single-step a signal delivery.
318 	 */
319 	error = EOPNOTSUPP;
320 
321 	nm = vfs_findname(ctlnames, msg, xlen);
322 	if (nm) {
323 		error = procfs_control(curp, lp, nm->nm_val);
324 	} else {
325 		nm = vfs_findname(signames, msg, xlen);
326 		if (nm) {
327 			if (TRACE_WAIT_P(curp, p)) {
328 				p->p_xstat = nm->nm_val;
329 #ifdef FIX_SSTEP
330 				FIX_SSTEP(lp);
331 #endif
332 				/*
333 				 * Make the process runnable but do not
334 				 * break its tsleep.
335 				 */
336 				proc_unstop(p);
337 			} else {
338 				ksignal(p, nm->nm_val);
339 			}
340 			error = 0;
341 		}
342 	}
343 
344 	return (error);
345 }
346