kern_ktrace.c revision 112199
1/*
2 * Copyright (c) 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
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 *	@(#)kern_ktrace.c	8.2 (Berkeley) 9/23/93
34 * $FreeBSD: head/sys/kern/kern_ktrace.c 112199 2003-03-13 18:31:15Z jhb $
35 */
36
37#include "opt_ktrace.h"
38#include "opt_mac.h"
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/fcntl.h>
43#include <sys/jail.h>
44#include <sys/kernel.h>
45#include <sys/kthread.h>
46#include <sys/lock.h>
47#include <sys/mutex.h>
48#include <sys/mac.h>
49#include <sys/malloc.h>
50#include <sys/namei.h>
51#include <sys/proc.h>
52#include <sys/unistd.h>
53#include <sys/vnode.h>
54#include <sys/ktrace.h>
55#include <sys/sema.h>
56#include <sys/sx.h>
57#include <sys/sysctl.h>
58#include <sys/syslog.h>
59#include <sys/sysproto.h>
60
61static MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
62
63#ifdef KTRACE
64
65#ifndef KTRACE_REQUEST_POOL
66#define	KTRACE_REQUEST_POOL	100
67#endif
68
69struct ktr_request {
70	struct	ktr_header ktr_header;
71	struct	ucred *ktr_cred;
72	struct	vnode *ktr_vp;
73	union {
74		struct	ktr_syscall ktr_syscall;
75		struct	ktr_sysret ktr_sysret;
76		struct	ktr_genio ktr_genio;
77		struct	ktr_psig ktr_psig;
78		struct	ktr_csw ktr_csw;
79	} ktr_data;
80	STAILQ_ENTRY(ktr_request) ktr_list;
81};
82
83static int data_lengths[] = {
84	0,					/* none */
85	offsetof(struct ktr_syscall, ktr_args),	/* KTR_SYSCALL */
86	sizeof(struct ktr_sysret),		/* KTR_SYSRET */
87	0,					/* KTR_NAMEI */
88	sizeof(struct ktr_genio),		/* KTR_GENIO */
89	sizeof(struct ktr_psig),		/* KTR_PSIG */
90	sizeof(struct ktr_csw),			/* KTR_CSW */
91	0					/* KTR_USER */
92};
93
94static STAILQ_HEAD(, ktr_request) ktr_todo;
95static STAILQ_HEAD(, ktr_request) ktr_free;
96
97SYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD, 0, "KTRACE options");
98
99static uint ktr_requestpool = KTRACE_REQUEST_POOL;
100TUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
101
102static uint ktr_geniosize = PAGE_SIZE;
103TUNABLE_INT("kern.ktrace.genio_size", &ktr_geniosize);
104SYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RW, &ktr_geniosize,
105    0, "Maximum size of genio event payload");
106
107static int print_message = 1;
108struct mtx ktrace_mtx;
109static struct sema ktrace_sema;
110
111static void ktrace_init(void *dummy);
112static int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
113static uint ktrace_resize_pool(uint newsize);
114static struct ktr_request *ktr_getrequest(int type);
115static void ktr_submitrequest(struct ktr_request *req);
116static void ktr_freerequest(struct ktr_request *req);
117static void ktr_loop(void *dummy);
118static void ktr_writerequest(struct ktr_request *req);
119static int ktrcanset(struct thread *,struct proc *);
120static int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
121static int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
122
123static void
124ktrace_init(void *dummy)
125{
126	struct ktr_request *req;
127	int i;
128
129	mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
130	sema_init(&ktrace_sema, 0, "ktrace");
131	STAILQ_INIT(&ktr_todo);
132	STAILQ_INIT(&ktr_free);
133	for (i = 0; i < ktr_requestpool; i++) {
134		req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
135		STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
136	}
137	kthread_create(ktr_loop, NULL, NULL, RFHIGHPID, 0, "ktrace");
138}
139SYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
140
141static int
142sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
143{
144	struct thread *td;
145	uint newsize, oldsize, wantsize;
146	int error;
147
148	/* Handle easy read-only case first to avoid warnings from GCC. */
149	if (!req->newptr) {
150		mtx_lock(&ktrace_mtx);
151		oldsize = ktr_requestpool;
152		mtx_unlock(&ktrace_mtx);
153		return (SYSCTL_OUT(req, &oldsize, sizeof(uint)));
154	}
155
156	error = SYSCTL_IN(req, &wantsize, sizeof(uint));
157	if (error)
158		return (error);
159	td = curthread;
160	td->td_inktrace = 1;
161	mtx_lock(&ktrace_mtx);
162	oldsize = ktr_requestpool;
163	newsize = ktrace_resize_pool(wantsize);
164	mtx_unlock(&ktrace_mtx);
165	td->td_inktrace = 0;
166	error = SYSCTL_OUT(req, &oldsize, sizeof(uint));
167	if (error)
168		return (error);
169	if (newsize != wantsize)
170		return (ENOSPC);
171	return (0);
172}
173SYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW,
174    &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU", "");
175
176static uint
177ktrace_resize_pool(uint newsize)
178{
179	struct ktr_request *req;
180
181	mtx_assert(&ktrace_mtx, MA_OWNED);
182	print_message = 1;
183	if (newsize == ktr_requestpool)
184		return (newsize);
185	if (newsize < ktr_requestpool)
186		/* Shrink pool down to newsize if possible. */
187		while (ktr_requestpool > newsize) {
188			req = STAILQ_FIRST(&ktr_free);
189			if (req == NULL)
190				return (ktr_requestpool);
191			STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
192			ktr_requestpool--;
193			mtx_unlock(&ktrace_mtx);
194			free(req, M_KTRACE);
195			mtx_lock(&ktrace_mtx);
196		}
197	else
198		/* Grow pool up to newsize. */
199		while (ktr_requestpool < newsize) {
200			mtx_unlock(&ktrace_mtx);
201			req = malloc(sizeof(struct ktr_request), M_KTRACE,
202			    M_WAITOK);
203			mtx_lock(&ktrace_mtx);
204			STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
205			ktr_requestpool++;
206		}
207	return (ktr_requestpool);
208}
209
210static struct ktr_request *
211ktr_getrequest(int type)
212{
213	struct ktr_request *req;
214	struct thread *td = curthread;
215	struct proc *p = td->td_proc;
216	int pm;
217
218	td->td_inktrace = 1;
219	mtx_lock(&ktrace_mtx);
220	if (!KTRCHECK(td, type)) {
221		mtx_unlock(&ktrace_mtx);
222		td->td_inktrace = 0;
223		return (NULL);
224	}
225	req = STAILQ_FIRST(&ktr_free);
226	if (req != NULL) {
227		STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
228		req->ktr_header.ktr_type = type;
229		if (p->p_traceflag & KTRFAC_DROP) {
230			req->ktr_header.ktr_type |= KTR_DROP;
231			p->p_traceflag &= ~KTRFAC_DROP;
232		}
233		KASSERT(p->p_tracevp != NULL, ("ktrace: no trace vnode"));
234		KASSERT(p->p_tracecred != NULL, ("ktrace: no trace cred"));
235		req->ktr_vp = p->p_tracevp;
236		VREF(p->p_tracevp);
237		req->ktr_cred = crhold(p->p_tracecred);
238		mtx_unlock(&ktrace_mtx);
239		microtime(&req->ktr_header.ktr_time);
240		req->ktr_header.ktr_pid = p->p_pid;
241		bcopy(p->p_comm, req->ktr_header.ktr_comm, MAXCOMLEN + 1);
242		req->ktr_header.ktr_buffer = NULL;
243		req->ktr_header.ktr_len = 0;
244	} else {
245		p->p_traceflag |= KTRFAC_DROP;
246		pm = print_message;
247		print_message = 0;
248		mtx_unlock(&ktrace_mtx);
249		if (pm)
250			printf("Out of ktrace request objects.\n");
251		td->td_inktrace = 0;
252	}
253	return (req);
254}
255
256static void
257ktr_submitrequest(struct ktr_request *req)
258{
259
260	mtx_lock(&ktrace_mtx);
261	STAILQ_INSERT_TAIL(&ktr_todo, req, ktr_list);
262	sema_post(&ktrace_sema);
263	mtx_unlock(&ktrace_mtx);
264	curthread->td_inktrace = 0;
265}
266
267static void
268ktr_freerequest(struct ktr_request *req)
269{
270
271	crfree(req->ktr_cred);
272	if (req->ktr_vp != NULL) {
273		mtx_lock(&Giant);
274		vrele(req->ktr_vp);
275		mtx_unlock(&Giant);
276	}
277	if (req->ktr_header.ktr_buffer != NULL)
278		free(req->ktr_header.ktr_buffer, M_KTRACE);
279	mtx_lock(&ktrace_mtx);
280	STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
281	mtx_unlock(&ktrace_mtx);
282}
283
284static void
285ktr_loop(void *dummy)
286{
287	struct ktr_request *req;
288	struct thread *td;
289	struct ucred *cred;
290
291	/* Only cache these values once. */
292	td = curthread;
293	cred = td->td_ucred;
294	for (;;) {
295		sema_wait(&ktrace_sema);
296		mtx_lock(&ktrace_mtx);
297		req = STAILQ_FIRST(&ktr_todo);
298		STAILQ_REMOVE_HEAD(&ktr_todo, ktr_list);
299		KASSERT(req != NULL, ("got a NULL request"));
300		mtx_unlock(&ktrace_mtx);
301		/*
302		 * It is not enough just to pass the cached cred
303		 * to the VOP's in ktr_writerequest().  Some VFS
304		 * operations use curthread->td_ucred, so we need
305		 * to modify our thread's credentials as well.
306		 * Evil.
307		 */
308		td->td_ucred = req->ktr_cred;
309		ktr_writerequest(req);
310		td->td_ucred = cred;
311		ktr_freerequest(req);
312	}
313}
314
315/*
316 * MPSAFE
317 */
318void
319ktrsyscall(code, narg, args)
320	int code, narg;
321	register_t args[];
322{
323	struct ktr_request *req;
324	struct ktr_syscall *ktp;
325	size_t buflen;
326	char *buf = NULL;
327
328	buflen = sizeof(register_t) * narg;
329	if (buflen > 0) {
330		buf = malloc(buflen, M_KTRACE, M_WAITOK);
331		bcopy(args, buf, buflen);
332	}
333	req = ktr_getrequest(KTR_SYSCALL);
334	if (req == NULL) {
335		if (buf != NULL)
336			free(buf, M_KTRACE);
337		return;
338	}
339	ktp = &req->ktr_data.ktr_syscall;
340	ktp->ktr_code = code;
341	ktp->ktr_narg = narg;
342	if (buflen > 0) {
343		req->ktr_header.ktr_len = buflen;
344		req->ktr_header.ktr_buffer = buf;
345	}
346	ktr_submitrequest(req);
347}
348
349/*
350 * MPSAFE
351 */
352void
353ktrsysret(code, error, retval)
354	int code, error;
355	register_t retval;
356{
357	struct ktr_request *req;
358	struct ktr_sysret *ktp;
359
360	req = ktr_getrequest(KTR_SYSRET);
361	if (req == NULL)
362		return;
363	ktp = &req->ktr_data.ktr_sysret;
364	ktp->ktr_code = code;
365	ktp->ktr_error = error;
366	ktp->ktr_retval = retval;		/* what about val2 ? */
367	ktr_submitrequest(req);
368}
369
370void
371ktrnamei(path)
372	char *path;
373{
374	struct ktr_request *req;
375	int namelen;
376	char *buf = NULL;
377
378	namelen = strlen(path);
379	if (namelen > 0) {
380		buf = malloc(namelen, M_KTRACE, M_WAITOK);
381		bcopy(path, buf, namelen);
382	}
383	req = ktr_getrequest(KTR_NAMEI);
384	if (req == NULL) {
385		if (buf != NULL)
386			free(buf, M_KTRACE);
387		return;
388	}
389	if (namelen > 0) {
390		req->ktr_header.ktr_len = namelen;
391		req->ktr_header.ktr_buffer = buf;
392	}
393	ktr_submitrequest(req);
394}
395
396/*
397 * Since the uio may not stay valid, we can not hand off this request to
398 * the thread and need to process it synchronously.  However, we wish to
399 * keep the relative order of records in a trace file correct, so we
400 * do put this request on the queue (if it isn't empty) and then block.
401 * The ktrace thread waks us back up when it is time for this event to
402 * be posted and blocks until we have completed writing out the event
403 * and woken it back up.
404 */
405void
406ktrgenio(fd, rw, uio, error)
407	int fd;
408	enum uio_rw rw;
409	struct uio *uio;
410	int error;
411{
412	struct ktr_request *req;
413	struct ktr_genio *ktg;
414	int datalen;
415	char *buf;
416
417	if (error)
418		return;
419	uio->uio_offset = 0;
420	uio->uio_rw = UIO_WRITE;
421	datalen = imin(uio->uio_resid, ktr_geniosize);
422	buf = malloc(datalen, M_KTRACE, M_WAITOK);
423	if (uiomove(buf, datalen, uio)) {
424		free(buf, M_KTRACE);
425		return;
426	}
427	req = ktr_getrequest(KTR_GENIO);
428	if (req == NULL) {
429		free(buf, M_KTRACE);
430		return;
431	}
432	ktg = &req->ktr_data.ktr_genio;
433	ktg->ktr_fd = fd;
434	ktg->ktr_rw = rw;
435	req->ktr_header.ktr_len = datalen;
436	req->ktr_header.ktr_buffer = buf;
437	ktr_submitrequest(req);
438}
439
440void
441ktrpsig(sig, action, mask, code)
442	int sig;
443	sig_t action;
444	sigset_t *mask;
445	int code;
446{
447	struct ktr_request *req;
448	struct ktr_psig	*kp;
449
450	req = ktr_getrequest(KTR_PSIG);
451	if (req == NULL)
452		return;
453	kp = &req->ktr_data.ktr_psig;
454	kp->signo = (char)sig;
455	kp->action = action;
456	kp->mask = *mask;
457	kp->code = code;
458	ktr_submitrequest(req);
459}
460
461void
462ktrcsw(out, user)
463	int out, user;
464{
465	struct ktr_request *req;
466	struct ktr_csw *kc;
467
468	req = ktr_getrequest(KTR_CSW);
469	if (req == NULL)
470		return;
471	kc = &req->ktr_data.ktr_csw;
472	kc->out = out;
473	kc->user = user;
474	ktr_submitrequest(req);
475}
476#endif
477
478/* Interface and common routines */
479
480/*
481 * ktrace system call
482 */
483#ifndef _SYS_SYSPROTO_H_
484struct ktrace_args {
485	char	*fname;
486	int	ops;
487	int	facs;
488	int	pid;
489};
490#endif
491/* ARGSUSED */
492int
493ktrace(td, uap)
494	struct thread *td;
495	register struct ktrace_args *uap;
496{
497#ifdef KTRACE
498	register struct vnode *vp = NULL;
499	register struct proc *p;
500	struct pgrp *pg;
501	int facs = uap->facs & ~KTRFAC_ROOT;
502	int ops = KTROP(uap->ops);
503	int descend = uap->ops & KTRFLAG_DESCEND;
504	int ret = 0;
505	int flags, error = 0;
506	struct nameidata nd;
507	struct ucred *cred;
508
509	td->td_inktrace = 1;
510	if (ops != KTROP_CLEAR) {
511		/*
512		 * an operation which requires a file argument.
513		 */
514		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, td);
515		flags = FREAD | FWRITE | O_NOFOLLOW;
516		error = vn_open(&nd, &flags, 0);
517		if (error) {
518			td->td_inktrace = 0;
519			return (error);
520		}
521		NDFREE(&nd, NDF_ONLY_PNBUF);
522		vp = nd.ni_vp;
523		VOP_UNLOCK(vp, 0, td);
524		if (vp->v_type != VREG) {
525			(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
526			td->td_inktrace = 0;
527			return (EACCES);
528		}
529	}
530	/*
531	 * Clear all uses of the tracefile.
532	 */
533	if (ops == KTROP_CLEARFILE) {
534		sx_slock(&allproc_lock);
535		LIST_FOREACH(p, &allproc, p_list) {
536			PROC_LOCK(p);
537			if (p->p_tracevp == vp) {
538				if (ktrcanset(td, p)) {
539					mtx_lock(&ktrace_mtx);
540					cred = p->p_tracecred;
541					p->p_tracecred = NULL;
542					p->p_tracevp = NULL;
543					p->p_traceflag = 0;
544					mtx_unlock(&ktrace_mtx);
545					PROC_UNLOCK(p);
546					(void) vn_close(vp, FREAD|FWRITE,
547						cred, td);
548					crfree(cred);
549				} else {
550					PROC_UNLOCK(p);
551					error = EPERM;
552				}
553			} else
554				PROC_UNLOCK(p);
555		}
556		sx_sunlock(&allproc_lock);
557		goto done;
558	}
559	/*
560	 * need something to (un)trace (XXX - why is this here?)
561	 */
562	if (!facs) {
563		error = EINVAL;
564		goto done;
565	}
566	/*
567	 * do it
568	 */
569	if (uap->pid < 0) {
570		/*
571		 * by process group
572		 */
573		sx_slock(&proctree_lock);
574		pg = pgfind(-uap->pid);
575		if (pg == NULL) {
576			sx_sunlock(&proctree_lock);
577			error = ESRCH;
578			goto done;
579		}
580		/*
581		 * ktrops() may call vrele(). Lock pg_members
582		 * by the proctree_lock rather than pg_mtx.
583		 */
584		PGRP_UNLOCK(pg);
585		LIST_FOREACH(p, &pg->pg_members, p_pglist)
586			if (descend)
587				ret |= ktrsetchildren(td, p, ops, facs, vp);
588			else
589				ret |= ktrops(td, p, ops, facs, vp);
590		sx_sunlock(&proctree_lock);
591	} else {
592		/*
593		 * by pid
594		 */
595		p = pfind(uap->pid);
596		if (p == NULL) {
597			error = ESRCH;
598			goto done;
599		}
600		PROC_UNLOCK(p);
601		/* XXX: UNLOCK above has a race */
602		if (descend)
603			ret |= ktrsetchildren(td, p, ops, facs, vp);
604		else
605			ret |= ktrops(td, p, ops, facs, vp);
606	}
607	if (!ret)
608		error = EPERM;
609done:
610	if (vp != NULL)
611		(void) vn_close(vp, FWRITE, td->td_ucred, td);
612	td->td_inktrace = 0;
613	return (error);
614#else
615	return ENOSYS;
616#endif
617}
618
619/*
620 * utrace system call
621 */
622/* ARGSUSED */
623int
624utrace(td, uap)
625	struct thread *td;
626	register struct utrace_args *uap;
627{
628
629#ifdef KTRACE
630	struct ktr_request *req;
631	void *cp;
632	int error;
633
634	if (!KTRPOINT(td, KTR_USER))
635		return (0);
636	if (uap->len > KTR_USER_MAXLEN)
637		return (EINVAL);
638	cp = malloc(uap->len, M_KTRACE, M_WAITOK);
639	error = copyin(uap->addr, cp, uap->len);
640	if (error) {
641		free(cp, M_KTRACE);
642		return (error);
643	}
644	req = ktr_getrequest(KTR_USER);
645	if (req == NULL) {
646		free(cp, M_KTRACE);
647		return (0);
648	}
649	req->ktr_header.ktr_buffer = cp;
650	req->ktr_header.ktr_len = uap->len;
651	ktr_submitrequest(req);
652	return (0);
653#else
654	return (ENOSYS);
655#endif
656}
657
658#ifdef KTRACE
659static int
660ktrops(td, p, ops, facs, vp)
661	struct thread *td;
662	struct proc *p;
663	int ops, facs;
664	struct vnode *vp;
665{
666	struct vnode *tracevp = NULL;
667	struct ucred *tracecred = NULL;
668
669	PROC_LOCK(p);
670	if (!ktrcanset(td, p)) {
671		PROC_UNLOCK(p);
672		return (0);
673	}
674	mtx_lock(&ktrace_mtx);
675	if (ops == KTROP_SET) {
676		if (p->p_tracevp != vp) {
677			/*
678			 * if trace file already in use, relinquish below
679			 */
680			tracevp = p->p_tracevp;
681			VREF(vp);
682			p->p_tracevp = vp;
683		}
684		if (p->p_tracecred != td->td_ucred) {
685			tracecred = p->p_tracecred;
686			p->p_tracecred = crhold(td->td_ucred);
687		}
688		p->p_traceflag |= facs;
689		if (td->td_ucred->cr_uid == 0)
690			p->p_traceflag |= KTRFAC_ROOT;
691	} else {
692		/* KTROP_CLEAR */
693		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
694			/* no more tracing */
695			p->p_traceflag = 0;
696			tracevp = p->p_tracevp;
697			p->p_tracevp = NULL;
698			tracecred = p->p_tracecred;
699			p->p_tracecred = NULL;
700		}
701	}
702	mtx_unlock(&ktrace_mtx);
703	PROC_UNLOCK(p);
704	if (tracevp != NULL)
705		vrele(tracevp);
706	if (tracecred != NULL)
707		crfree(tracecred);
708
709	return (1);
710}
711
712static int
713ktrsetchildren(td, top, ops, facs, vp)
714	struct thread *td;
715	struct proc *top;
716	int ops, facs;
717	struct vnode *vp;
718{
719	register struct proc *p;
720	register int ret = 0;
721
722	p = top;
723	sx_slock(&proctree_lock);
724	for (;;) {
725		ret |= ktrops(td, p, ops, facs, vp);
726		/*
727		 * If this process has children, descend to them next,
728		 * otherwise do any siblings, and if done with this level,
729		 * follow back up the tree (but not past top).
730		 */
731		if (!LIST_EMPTY(&p->p_children))
732			p = LIST_FIRST(&p->p_children);
733		else for (;;) {
734			if (p == top) {
735				sx_sunlock(&proctree_lock);
736				return (ret);
737			}
738			if (LIST_NEXT(p, p_sibling)) {
739				p = LIST_NEXT(p, p_sibling);
740				break;
741			}
742			p = p->p_pptr;
743		}
744	}
745	/*NOTREACHED*/
746}
747
748static void
749ktr_writerequest(struct ktr_request *req)
750{
751	struct ktr_header *kth;
752	struct vnode *vp;
753	struct proc *p;
754	struct thread *td;
755	struct ucred *cred;
756	struct uio auio;
757	struct iovec aiov[3];
758	struct mount *mp;
759	int datalen, buflen, vrele_count;
760	int error;
761
762	vp = req->ktr_vp;
763	/*
764	 * If vp is NULL, the vp has been cleared out from under this
765	 * request, so just drop it.
766	 */
767	if (vp == NULL)
768		return;
769	kth = &req->ktr_header;
770	datalen = data_lengths[(ushort)kth->ktr_type & ~KTR_DROP];
771	buflen = kth->ktr_len;
772	cred = req->ktr_cred;
773	td = curthread;
774	auio.uio_iov = &aiov[0];
775	auio.uio_offset = 0;
776	auio.uio_segflg = UIO_SYSSPACE;
777	auio.uio_rw = UIO_WRITE;
778	aiov[0].iov_base = (caddr_t)kth;
779	aiov[0].iov_len = sizeof(struct ktr_header);
780	auio.uio_resid = sizeof(struct ktr_header);
781	auio.uio_iovcnt = 1;
782	auio.uio_td = td;
783	if (datalen != 0) {
784		aiov[1].iov_base = (caddr_t)&req->ktr_data;
785		aiov[1].iov_len = datalen;
786		auio.uio_resid += datalen;
787		auio.uio_iovcnt++;
788		kth->ktr_len += datalen;
789	}
790	if (buflen != 0) {
791		KASSERT(kth->ktr_buffer != NULL, ("ktrace: nothing to write"));
792		aiov[auio.uio_iovcnt].iov_base = kth->ktr_buffer;
793		aiov[auio.uio_iovcnt].iov_len = buflen;
794		auio.uio_resid += buflen;
795		auio.uio_iovcnt++;
796	}
797	mtx_lock(&Giant);
798	vn_start_write(vp, &mp, V_WAIT);
799	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
800	(void)VOP_LEASE(vp, td, cred, LEASE_WRITE);
801#ifdef MAC
802	error = mac_check_vnode_write(cred, NOCRED, vp);
803	if (error == 0)
804#endif
805		error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
806	VOP_UNLOCK(vp, 0, td);
807	vn_finished_write(mp);
808	mtx_unlock(&Giant);
809	if (!error)
810		return;
811	/*
812	 * If error encountered, give up tracing on this vnode.  We defer
813	 * all the vrele()'s on the vnode until after we are finished walking
814	 * the various lists to avoid needlessly holding locks.
815	 */
816	log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
817	    error);
818	vrele_count = 0;
819	/*
820	 * First, clear this vnode from being used by any processes in the
821	 * system.
822	 * XXX - If one process gets an EPERM writing to the vnode, should
823	 * we really do this?  Other processes might have suitable
824	 * credentials for the operation.
825	 */
826	cred = NULL;
827	sx_slock(&allproc_lock);
828	LIST_FOREACH(p, &allproc, p_list) {
829		PROC_LOCK(p);
830		if (p->p_tracevp == vp) {
831			mtx_lock(&ktrace_mtx);
832			p->p_tracevp = NULL;
833			p->p_traceflag = 0;
834			cred = p->p_tracecred;
835			p->p_tracecred = NULL;
836			mtx_unlock(&ktrace_mtx);
837			vrele_count++;
838		}
839		PROC_UNLOCK(p);
840		if (cred != NULL) {
841			crfree(cred);
842			cred = NULL;
843		}
844	}
845	sx_sunlock(&allproc_lock);
846	/*
847	 * Second, clear this vnode from any pending requests.
848	 */
849	mtx_lock(&ktrace_mtx);
850	STAILQ_FOREACH(req, &ktr_todo, ktr_list) {
851		if (req->ktr_vp == vp) {
852			req->ktr_vp = NULL;
853			vrele_count++;
854		}
855	}
856	mtx_unlock(&ktrace_mtx);
857	mtx_lock(&Giant);
858	while (vrele_count-- > 0)
859		vrele(vp);
860	mtx_unlock(&Giant);
861}
862
863/*
864 * Return true if caller has permission to set the ktracing state
865 * of target.  Essentially, the target can't possess any
866 * more permissions than the caller.  KTRFAC_ROOT signifies that
867 * root previously set the tracing status on the target process, and
868 * so, only root may further change it.
869 */
870static int
871ktrcanset(td, targetp)
872	struct thread *td;
873	struct proc *targetp;
874{
875
876	PROC_LOCK_ASSERT(targetp, MA_OWNED);
877	if (targetp->p_traceflag & KTRFAC_ROOT &&
878	    suser_cred(td->td_ucred, PRISON_ROOT))
879		return (0);
880
881	if (p_candebug(td, targetp) != 0)
882		return (0);
883
884	return (1);
885}
886
887#endif /* KTRACE */
888