xref: /dragonfly/sys/vfs/procfs/procfs_ctl.c (revision 335b9e93)
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. 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  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/proc.h>
42 #include <sys/priv.h>
43 #include <sys/vnode.h>
44 #include <sys/ptrace.h>
45 #include <sys/signalvar.h>
46 #include <vfs/procfs/procfs.h>
47 
48 #include <sys/signal2.h>
49 #include <sys/spinlock2.h>
50 
51 #include <vm/vm.h>
52 
53 #ifndef FIX_SSTEP
54 #define FIX_SSTEP(lp)
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_flags & 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 (struct proc *curp, struct lwp *lp, int op);
104 
105 static int
106 procfs_control(struct proc *curp, struct lwp *lp, int op)
107 {
108 	struct proc *p = lp->lwp_proc;
109 	int error;
110 
111 	ASSERT_LWKT_TOKEN_HELD(&p->p_token);
112 
113 	/* Can't trace a process that's currently exec'ing. */
114 	if ((p->p_flags & 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_ucred, p->p_ucred))
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_flags & 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_flags |= 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 		proc_stop(p, SSTOP);
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(lp);
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_flags & P_TRACED) == 0)
201 			return (0);
202 
203 		/* not being traced any more */
204 		p->p_flags &= ~P_TRACED;
205 
206 		/* remove pending SIGTRAP, else the process will die */
207 		spin_lock(&lp->lwp_spin);
208 		lwp_delsig(lp, SIGTRAP, 1);
209 		spin_unlock(&lp->lwp_spin);
210 
211 		/* give process back to original parent */
212 		if (p->p_oppid != p->p_pptr->p_pid) {
213 			struct proc *pp;
214 
215 			pp = pfs_pfind(p->p_oppid);
216 			if (pp) {
217 				proc_reparent(p, pp);
218 				pfs_pdone(pp);
219 			}
220 		}
221 
222 		p->p_oppid = 0;
223 		p->p_flags &= ~P_WAITED;	/* XXX ? */
224 		wakeup((caddr_t) curp);		/* XXX for CTL_WAIT below ? */
225 
226 		break;
227 
228 	/*
229 	 * Step.  Let the target process execute a single instruction.
230 	 */
231 	case PROCFS_CTL_STEP:
232 		LWPHOLD(lp);
233 		error = procfs_sstep(lp);
234 		LWPRELE(lp);
235 		if (error)
236 			return (error);
237 		break;
238 
239 	/*
240 	 * Run.  Let the target process continue running until a breakpoint
241 	 * or some other trap.
242 	 */
243 	case PROCFS_CTL_RUN:
244 		break;
245 
246 	/*
247 	 * Wait for the target process to stop.
248 	 * If the target is not being traced then just wait
249 	 * to enter
250 	 */
251 	case PROCFS_CTL_WAIT:
252 		error = 0;
253 		if (p->p_flags & P_TRACED) {
254 			while (error == 0 &&
255 					p->p_stat != SSTOP &&
256 					(p->p_flags & P_TRACED) &&
257 					(p->p_pptr == curp)) {
258 				error = tsleep((caddr_t) p,
259 						PCATCH, "procfsx", 0);
260 			}
261 			if (error == 0 && !TRACE_WAIT_P(curp, p))
262 				error = EBUSY;
263 		} else {
264 			while (error == 0 && p->p_stat != SSTOP) {
265 				error = tsleep((caddr_t) p,
266 						PCATCH, "procfs", 0);
267 			}
268 		}
269 		return (error);
270 
271 	default:
272 		panic("procfs_control");
273 	}
274 
275 	/*
276 	 * If the process is in a stopped state, make it runnable again.
277 	 * Do not set LWP_MP_BREAKTSLEEP - that is, do not break a tsleep
278 	 * that might be in progress.
279 	 */
280 	if (p->p_stat == SSTOP)
281 		proc_unstop(p, SSTOP);
282 	return (0);
283 }
284 
285 int
286 procfs_doctl(struct proc *curp, struct lwp *lp, struct pfsnode *pfs,
287 	     struct uio *uio)
288 {
289 	struct proc *p = lp->lwp_proc;
290 	int xlen;
291 	int error;
292 	char msg[PROCFS_CTLLEN+1];
293 	vfs_namemap_t *nm;
294 
295 	ASSERT_LWKT_TOKEN_HELD(&p->p_token);
296 
297 	if (uio->uio_rw != UIO_WRITE)
298 		return (EOPNOTSUPP);
299 
300 	xlen = PROCFS_CTLLEN;
301 	error = vfs_getuserstr(uio, msg, &xlen);
302 	if (error)
303 		return (error);
304 
305 	/*
306 	 * Map signal names into signal generation
307 	 * or debug control.  Unknown commands and/or signals
308 	 * return EOPNOTSUPP.
309 	 *
310 	 * Sending a signal while the process is being debugged
311 	 * also has the side effect of letting the target continue
312 	 * to run.  There is no way to single-step a signal delivery.
313 	 */
314 	error = EOPNOTSUPP;
315 
316 	nm = vfs_findname(ctlnames, msg, xlen);
317 	if (nm) {
318 		error = procfs_control(curp, lp, nm->nm_val);
319 	} else {
320 		nm = vfs_findname(signames, msg, xlen);
321 		if (nm) {
322 			if (TRACE_WAIT_P(curp, p)) {
323 				p->p_xstat = nm->nm_val;
324 #ifdef FIX_SSTEP
325 				FIX_SSTEP(lp);
326 #endif
327 				/*
328 				 * Make the process runnable but do not
329 				 * break its tsleep.
330 				 */
331 				proc_unstop(p, SSTOP);
332 			} else {
333 				ksignal(p, nm->nm_val);
334 			}
335 			error = 0;
336 		}
337 	}
338 
339 	return (error);
340 }
341