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