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