Home | History | Annotate | Line # | Download | only in kern
kern_ktrace.c revision 1.62
      1 /*	$NetBSD: kern_ktrace.c,v 1.62 2002/12/09 21:29:21 manu Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)kern_ktrace.c	8.5 (Berkeley) 5/14/95
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: kern_ktrace.c,v 1.62 2002/12/09 21:29:21 manu Exp $");
     40 
     41 #include "opt_ktrace.h"
     42 #include "opt_compat_mach.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/proc.h>
     47 #include <sys/file.h>
     48 #include <sys/namei.h>
     49 #include <sys/vnode.h>
     50 #include <sys/ktrace.h>
     51 #include <sys/malloc.h>
     52 #include <sys/syslog.h>
     53 #include <sys/filedesc.h>
     54 #include <sys/ioctl.h>
     55 
     56 #include <sys/mount.h>
     57 #include <sys/syscallargs.h>
     58 
     59 #ifdef COMPAT_MACH
     60 #include <compat/mach/mach_types.h>
     61 #include <compat/mach/mach_message.h>
     62 #endif
     63 
     64 #ifdef KTRACE
     65 
     66 int	ktrace_common(struct proc *, int, int, int, struct file *);
     67 void	ktrinitheader(struct ktr_header *, struct proc *, int);
     68 int	ktrops(struct proc *, struct proc *, int, int, struct file *);
     69 int	ktrsetchildren(struct proc *, struct proc *, int, int,
     70 	    struct file *);
     71 int	ktrwrite(struct proc *, struct ktr_header *);
     72 int	ktrcanset(struct proc *, struct proc *);
     73 int	ktrsamefile(struct file *, struct file *);
     74 
     75 /*
     76  * "deep" compare of two files for the purposes of clearing a trace.
     77  * Returns true if they're the same open file, or if they point at the
     78  * same underlying vnode/socket.
     79  */
     80 
     81 int
     82 ktrsamefile(f1, f2)
     83 	struct file *f1;
     84 	struct file *f2;
     85 {
     86 	return ((f1 == f2) ||
     87 	    ((f1 != NULL) && (f2 != NULL) &&
     88 		(f1->f_type == f2->f_type) &&
     89 		(f1->f_data == f2->f_data)));
     90 }
     91 
     92 void
     93 ktrderef(p)
     94 	struct proc *p;
     95 {
     96 	struct file *fp = p->p_tracep;
     97 	p->p_traceflag = 0;
     98 	if (fp == NULL)
     99 		return;
    100 	FILE_USE(fp);
    101 
    102 	/*
    103 	 * ktrace file descriptor can't be watched (are not visible to
    104 	 * userspace), so no kqueue stuff here
    105 	 */
    106 	closef(fp, NULL);
    107 
    108 	p->p_tracep = NULL;
    109 }
    110 
    111 void
    112 ktradref(p)
    113 	struct proc *p;
    114 {
    115 	struct file *fp = p->p_tracep;
    116 
    117 	fp->f_count++;
    118 }
    119 
    120 void
    121 ktrinitheader(kth, p, type)
    122 	struct ktr_header *kth;
    123 	struct proc *p;
    124 	int type;
    125 {
    126 
    127 	memset(kth, 0, sizeof(*kth));
    128 	kth->ktr_type = type;
    129 	microtime(&kth->ktr_time);
    130 	kth->ktr_pid = p->p_pid;
    131 	memcpy(kth->ktr_comm, p->p_comm, MAXCOMLEN);
    132 }
    133 
    134 void
    135 ktrsyscall(p, code, realcode, args)
    136 	struct proc *p;
    137 	register_t code;
    138 	register_t realcode;
    139 	register_t args[];
    140 {
    141 	struct ktr_header kth;
    142 	struct ktr_syscall *ktp;
    143 	register_t *argp;
    144 	int argsize;
    145 	size_t len;
    146 	u_int i;
    147 
    148 	argsize = p->p_emul->e_sysent[code].sy_narg * sizeof (register_t);
    149 	len = sizeof(struct ktr_syscall) + argsize;
    150 
    151 	p->p_traceflag |= KTRFAC_ACTIVE;
    152 	ktrinitheader(&kth, p, KTR_SYSCALL);
    153 	ktp = malloc(len, M_TEMP, M_WAITOK);
    154 	ktp->ktr_code = realcode;
    155 	ktp->ktr_argsize = argsize;
    156 	argp = (register_t *)((char *)ktp + sizeof(struct ktr_syscall));
    157 	for (i = 0; i < (argsize / sizeof(*argp)); i++)
    158 		*argp++ = args[i];
    159 	kth.ktr_buf = (caddr_t)ktp;
    160 	kth.ktr_len = len;
    161 	(void) ktrwrite(p, &kth);
    162 	free(ktp, M_TEMP);
    163 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    164 }
    165 
    166 void
    167 ktrsysret(p, code, error, retval)
    168 	struct proc *p;
    169 	register_t code;
    170 	int error;
    171 	register_t retval;
    172 {
    173 	struct ktr_header kth;
    174 	struct ktr_sysret ktp;
    175 
    176 	p->p_traceflag |= KTRFAC_ACTIVE;
    177 	ktrinitheader(&kth, p, KTR_SYSRET);
    178 	ktp.ktr_code = code;
    179 	ktp.ktr_eosys = 0;			/* XXX unused */
    180 	ktp.ktr_error = error;
    181 	ktp.ktr_retval = retval;		/* what about val2 ? */
    182 
    183 	kth.ktr_buf = (caddr_t)&ktp;
    184 	kth.ktr_len = sizeof(struct ktr_sysret);
    185 
    186 	(void) ktrwrite(p, &kth);
    187 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    188 }
    189 
    190 void
    191 ktrnamei(p, path)
    192 	struct proc *p;
    193 	char *path;
    194 {
    195 	struct ktr_header kth;
    196 
    197 	p->p_traceflag |= KTRFAC_ACTIVE;
    198 	ktrinitheader(&kth, p, KTR_NAMEI);
    199 	kth.ktr_len = strlen(path);
    200 	kth.ktr_buf = path;
    201 
    202 	(void) ktrwrite(p, &kth);
    203 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    204 }
    205 
    206 void
    207 ktremul(p)
    208 	struct proc *p;
    209 {
    210 	struct ktr_header kth;
    211 	const char *emul = p->p_emul->e_name;
    212 
    213 	p->p_traceflag |= KTRFAC_ACTIVE;
    214 	ktrinitheader(&kth, p, KTR_EMUL);
    215 	kth.ktr_len = strlen(emul);
    216 	kth.ktr_buf = (caddr_t)emul;
    217 
    218 	(void) ktrwrite(p, &kth);
    219 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    220 }
    221 
    222 void
    223 ktrgenio(p, fd, rw, iov, len, error)
    224 	struct proc *p;
    225 	int fd;
    226 	enum uio_rw rw;
    227 	struct iovec *iov;
    228 	int len;
    229 	int error;
    230 {
    231 	struct ktr_header kth;
    232 	struct ktr_genio *ktp;
    233 	caddr_t cp;
    234 	int resid = len, cnt;
    235 	int buflen;
    236 
    237 	if (error)
    238 		return;
    239 
    240 	p->p_traceflag |= KTRFAC_ACTIVE;
    241 
    242 	buflen = min(PAGE_SIZE, len + sizeof(struct ktr_genio));
    243 
    244 	ktrinitheader(&kth, p, KTR_GENIO);
    245 	ktp = malloc(buflen, M_TEMP, M_WAITOK);
    246 	ktp->ktr_fd = fd;
    247 	ktp->ktr_rw = rw;
    248 
    249 	kth.ktr_buf = (caddr_t)ktp;
    250 
    251 	cp = (caddr_t)((char *)ktp + sizeof(struct ktr_genio));
    252 	buflen -= sizeof(struct ktr_genio);
    253 
    254 	while (resid > 0) {
    255 		KDASSERT(p->p_cpu != NULL);
    256 		KDASSERT(p->p_cpu == curcpu());
    257 		if (p->p_cpu->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
    258 			preempt(NULL);
    259 
    260 		cnt = min(iov->iov_len, buflen);
    261 		if (cnt > resid)
    262 			cnt = resid;
    263 		if (copyin(iov->iov_base, cp, cnt))
    264 			break;
    265 
    266 		kth.ktr_len = cnt + sizeof(struct ktr_genio);
    267 
    268 		if (__predict_false(ktrwrite(p, &kth) != 0))
    269 			break;
    270 
    271 		iov->iov_base = (caddr_t)iov->iov_base + cnt;
    272 		iov->iov_len -= cnt;
    273 
    274 		if (iov->iov_len == 0)
    275 			iov++;
    276 
    277 		resid -= cnt;
    278 	}
    279 
    280 	free(ktp, M_TEMP);
    281 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    282 }
    283 
    284 void
    285 ktrpsig(p, sig, action, mask, code)
    286 	struct proc *p;
    287 	int sig;
    288 	sig_t action;
    289 	sigset_t *mask;
    290 	int code;
    291 {
    292 	struct ktr_header kth;
    293 	struct ktr_psig	kp;
    294 
    295 	p->p_traceflag |= KTRFAC_ACTIVE;
    296 	ktrinitheader(&kth, p, KTR_PSIG);
    297 	kp.signo = (char)sig;
    298 	kp.action = action;
    299 	kp.mask = *mask;
    300 	kp.code = code;
    301 	kth.ktr_buf = (caddr_t)&kp;
    302 	kth.ktr_len = sizeof(struct ktr_psig);
    303 
    304 	(void) ktrwrite(p, &kth);
    305 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    306 }
    307 
    308 void
    309 ktrcsw(p, out, user)
    310 	struct proc *p;
    311 	int out;
    312 	int user;
    313 {
    314 	struct ktr_header kth;
    315 	struct ktr_csw kc;
    316 
    317 	p->p_traceflag |= KTRFAC_ACTIVE;
    318 	ktrinitheader(&kth, p, KTR_CSW);
    319 	kc.out = out;
    320 	kc.user = user;
    321 	kth.ktr_buf = (caddr_t)&kc;
    322 	kth.ktr_len = sizeof(struct ktr_csw);
    323 
    324 	(void) ktrwrite(p, &kth);
    325 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    326 }
    327 
    328 void
    329 ktruser(p, id, addr, len, ustr)
    330 	struct proc *p;
    331 	const char *id;
    332 	void *addr;
    333 	size_t len;
    334 	int ustr;
    335 {
    336 	struct ktr_header kth;
    337 	struct ktr_user *ktp;
    338 	caddr_t user_dta;
    339 
    340 	p->p_traceflag |= KTRFAC_ACTIVE;
    341 	ktrinitheader(&kth, p, KTR_USER);
    342 	ktp = malloc(sizeof(struct ktr_user) + len, M_TEMP, M_WAITOK);
    343 	if (ustr) {
    344 		if (copyinstr(id, ktp->ktr_id, KTR_USER_MAXIDLEN, NULL) != 0)
    345 			ktp->ktr_id[0] = '\0';
    346 	} else
    347 		strncpy(ktp->ktr_id, id, KTR_USER_MAXIDLEN);
    348 	ktp->ktr_id[KTR_USER_MAXIDLEN-1] = '\0';
    349 
    350 	user_dta = (caddr_t) ((char *)ktp + sizeof(struct ktr_user));
    351 	if (copyin(addr, (void *) user_dta, len) != 0)
    352 		len = 0;
    353 
    354 	kth.ktr_buf = (void *)ktp;
    355 	kth.ktr_len = sizeof(struct ktr_user) + len;
    356 	(void) ktrwrite(p, &kth);
    357 
    358 	free(ktp, M_TEMP);
    359 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    360 
    361 }
    362 
    363 #ifdef COMPAT_MACH
    364 void
    365 ktrmmsg(p, msgh, size)
    366 	struct proc *p;
    367 	const char *msgh;
    368 	size_t size;
    369 {
    370 	struct ktr_header kth;
    371 	struct ktr_mmsg	*kp;
    372 	int error;
    373 
    374 	p->p_traceflag |= KTRFAC_ACTIVE;
    375 	ktrinitheader(&kth, p, KTR_MMSG);
    376 
    377 	kp = (struct ktr_mmsg *)malloc(size, M_TEMP, M_WAITOK);
    378 	if ((error = copyin(msgh, kp, size)) != 0)
    379 		size = 0; /* Still log a message, but empty */
    380 
    381 	kth.ktr_buf = (caddr_t)kp;
    382 	kth.ktr_len = size;
    383 	(void) ktrwrite(p, &kth);
    384 	free(kp, M_TEMP);
    385 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    386 }
    387 #endif /* COMPAT_MACH */
    388 
    389 /* Interface and common routines */
    390 
    391 int
    392 ktrace_common(curp, ops, facs, pid, fp)
    393 	struct proc *curp;
    394 	int ops;
    395 	int facs;
    396 	int pid;
    397 	struct file *fp;
    398 {
    399 	int ret = 0;
    400 	int error = 0;
    401 	int one = 1;
    402 	int descend;
    403 	struct proc *p;
    404 	struct pgrp *pg;
    405 
    406 	curp->p_traceflag |= KTRFAC_ACTIVE;
    407 	descend = ops & KTRFLAG_DESCEND;
    408 	facs = facs & ~((unsigned) KTRFAC_ROOT);
    409 
    410 	/*
    411 	 * Clear all uses of the tracefile
    412 	 */
    413 	if (KTROP(ops) == KTROP_CLEARFILE) {
    414 		proclist_lock_read();
    415 		for (p = LIST_FIRST(&allproc); p != NULL;
    416 		     p = LIST_NEXT(p, p_list)) {
    417 			if (ktrsamefile(p->p_tracep, fp)) {
    418 				if (ktrcanset(curp, p))
    419 					ktrderef(p);
    420 				else
    421 					error = EPERM;
    422 			}
    423 		}
    424 		proclist_unlock_read();
    425 		goto done;
    426 	}
    427 
    428 	/*
    429 	 * Mark fp non-blocking, to avoid problems from possible deadlocks.
    430 	 */
    431 
    432 	if (fp != NULL) {
    433 		fp->f_flag |= FNONBLOCK;
    434 		(*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&one, curp);
    435 	}
    436 
    437 	/*
    438 	 * need something to (un)trace (XXX - why is this here?)
    439 	 */
    440 	if (!facs) {
    441 		error = EINVAL;
    442 		goto done;
    443 	}
    444 	/*
    445 	 * do it
    446 	 */
    447 	if (pid < 0) {
    448 		/*
    449 		 * by process group
    450 		 */
    451 		pg = pgfind(-pid);
    452 		if (pg == NULL) {
    453 			error = ESRCH;
    454 			goto done;
    455 		}
    456 		for (p = LIST_FIRST(&pg->pg_members); p != NULL;
    457 		     p = LIST_NEXT(p, p_pglist)) {
    458 			if (descend)
    459 				ret |= ktrsetchildren(curp, p, ops, facs, fp);
    460 			else
    461 				ret |= ktrops(curp, p, ops, facs, fp);
    462 		}
    463 
    464 	} else {
    465 		/*
    466 		 * by pid
    467 		 */
    468 		p = pfind(pid);
    469 		if (p == NULL) {
    470 			error = ESRCH;
    471 			goto done;
    472 		}
    473 		if (descend)
    474 			ret |= ktrsetchildren(curp, p, ops, facs, fp);
    475 		else
    476 			ret |= ktrops(curp, p, ops, facs, fp);
    477 	}
    478 	if (!ret)
    479 		error = EPERM;
    480 done:
    481 	curp->p_traceflag &= ~KTRFAC_ACTIVE;
    482 	return (error);
    483 }
    484 
    485 /*
    486  * ktrace system call
    487  */
    488 /* ARGSUSED */
    489 int
    490 sys_fktrace(curp, v, retval)
    491 	struct proc *curp;
    492 	void *v;
    493 	register_t *retval;
    494 {
    495 	struct sys_fktrace_args /* {
    496 		syscallarg(int) fd;
    497 		syscallarg(int) ops;
    498 		syscallarg(int) facs;
    499 		syscallarg(int) pid;
    500 	} */ *uap = v;
    501 	struct file *fp = NULL;
    502 	struct filedesc *fdp = curp->p_fd;
    503 
    504 	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
    505 		return (EBADF);
    506 
    507 	if ((fp->f_flag & FWRITE) == 0)
    508 		return (EBADF);
    509 
    510 	return ktrace_common(curp, SCARG(uap, ops),
    511 	    SCARG(uap, facs), SCARG(uap, pid), fp);
    512 }
    513 
    514 /*
    515  * ktrace system call
    516  */
    517 /* ARGSUSED */
    518 int
    519 sys_ktrace(curp, v, retval)
    520 	struct proc *curp;
    521 	void *v;
    522 	register_t *retval;
    523 {
    524 	struct sys_ktrace_args /* {
    525 		syscallarg(const char *) fname;
    526 		syscallarg(int) ops;
    527 		syscallarg(int) facs;
    528 		syscallarg(int) pid;
    529 	} */ *uap = v;
    530 	struct vnode *vp = NULL;
    531 	struct file *fp = NULL;
    532 	int fd;
    533 	int ops = SCARG(uap, ops);
    534 	int error = 0;
    535 	struct nameidata nd;
    536 
    537 	ops = KTROP(ops) | (ops & KTRFLAG_DESCEND);
    538 
    539 	curp->p_traceflag |= KTRFAC_ACTIVE;
    540 	if (ops != KTROP_CLEAR) {
    541 		/*
    542 		 * an operation which requires a file argument.
    543 		 */
    544 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname),
    545 		    curp);
    546 		if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
    547 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
    548 			return (error);
    549 		}
    550 		vp = nd.ni_vp;
    551 		VOP_UNLOCK(vp, 0);
    552 		if (vp->v_type != VREG) {
    553 			(void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp);
    554 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
    555 			return (EACCES);
    556 		}
    557 		/*
    558 		 * XXX This uses up a file descriptor slot in the
    559 		 * tracing process for the duration of this syscall.
    560 		 * This is not expected to be a problem.  If
    561 		 * falloc(NULL, ...) DTRT we could skip that part, but
    562 		 * that would require changing its interface to allow
    563 		 * the caller to pass in a ucred..
    564 		 *
    565 		 * This will FILE_USE the fp it returns, if any.
    566 		 * Keep it in use until we return.
    567 		 */
    568 		if ((error = falloc(curp, &fp, &fd)) != 0)
    569 			goto done;
    570 
    571 		fp->f_flag = FWRITE|FAPPEND;
    572 		fp->f_type = DTYPE_VNODE;
    573 		fp->f_ops = &vnops;
    574 		fp->f_data = (caddr_t)vp;
    575 		FILE_SET_MATURE(fp);
    576 		vp = NULL;
    577 	}
    578 	error = ktrace_common(curp, SCARG(uap, ops), SCARG(uap, facs),
    579 	    SCARG(uap, pid), fp);
    580 done:
    581 	if (vp != NULL)
    582 		(void) vn_close(vp, FWRITE, curp->p_ucred, curp);
    583 	if (fp != NULL) {
    584 		FILE_UNUSE(fp, curp);	/* release file */
    585 		fdrelease(curp, fd); 	/* release fd table slot */
    586 	}
    587 	return (error);
    588 }
    589 
    590 int
    591 ktrops(curp, p, ops, facs, fp)
    592 	struct proc *curp;
    593 	struct proc *p;
    594 	int ops;
    595 	int facs;
    596 	struct file *fp;
    597 {
    598 
    599 	if (!ktrcanset(curp, p))
    600 		return (0);
    601 	if (KTROP(ops) == KTROP_SET) {
    602 		if (p->p_tracep != fp) {
    603 			/*
    604 			 * if trace file already in use, relinquish
    605 			 */
    606 			ktrderef(p);
    607 			p->p_tracep = fp;
    608 			ktradref(p);
    609 		}
    610 		p->p_traceflag |= facs;
    611 		if (curp->p_ucred->cr_uid == 0)
    612 			p->p_traceflag |= KTRFAC_ROOT;
    613 	} else {
    614 		/* KTROP_CLEAR */
    615 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
    616 			/* no more tracing */
    617 			ktrderef(p);
    618 		}
    619 	}
    620 
    621 	/*
    622 	 * Emit an emulation record, every time there is a ktrace
    623 	 * change/attach request.
    624 	 */
    625 	if (KTRPOINT(p, KTR_EMUL))
    626 		ktremul(p);
    627 #ifdef __HAVE_SYSCALL_INTERN
    628 	(*p->p_emul->e_syscall_intern)(p);
    629 #endif
    630 
    631 	return (1);
    632 }
    633 
    634 int
    635 ktrsetchildren(curp, top, ops, facs, fp)
    636 	struct proc *curp;
    637 	struct proc *top;
    638 	int ops;
    639 	int facs;
    640 	struct file *fp;
    641 {
    642 	struct proc *p;
    643 	int ret = 0;
    644 
    645 	p = top;
    646 	for (;;) {
    647 		ret |= ktrops(curp, p, ops, facs, fp);
    648 		/*
    649 		 * If this process has children, descend to them next,
    650 		 * otherwise do any siblings, and if done with this level,
    651 		 * follow back up the tree (but not past top).
    652 		 */
    653 		if (LIST_FIRST(&p->p_children) != NULL)
    654 			p = LIST_FIRST(&p->p_children);
    655 		else for (;;) {
    656 			if (p == top)
    657 				return (ret);
    658 			if (LIST_NEXT(p, p_sibling) != NULL) {
    659 				p = LIST_NEXT(p, p_sibling);
    660 				break;
    661 			}
    662 			p = p->p_pptr;
    663 		}
    664 	}
    665 	/*NOTREACHED*/
    666 }
    667 
    668 int
    669 ktrwrite(p, kth)
    670 	struct proc *p;
    671 	struct ktr_header *kth;
    672 {
    673 	struct uio auio;
    674 	struct iovec aiov[2];
    675 	int error, tries;
    676 	struct file *fp = p->p_tracep;
    677 
    678 	if (fp == NULL)
    679 		return 0;
    680 
    681 	auio.uio_iov = &aiov[0];
    682 	auio.uio_offset = 0;
    683 	auio.uio_segflg = UIO_SYSSPACE;
    684 	auio.uio_rw = UIO_WRITE;
    685 	aiov[0].iov_base = (caddr_t)kth;
    686 	aiov[0].iov_len = sizeof(struct ktr_header);
    687 	auio.uio_resid = sizeof(struct ktr_header);
    688 	auio.uio_iovcnt = 1;
    689 	auio.uio_procp = (struct proc *)0;
    690 	if (kth->ktr_len > 0) {
    691 		auio.uio_iovcnt++;
    692 		aiov[1].iov_base = kth->ktr_buf;
    693 		aiov[1].iov_len = kth->ktr_len;
    694 		auio.uio_resid += kth->ktr_len;
    695 	}
    696 
    697 	FILE_USE(fp);
    698 
    699 	tries = 0;
    700 	do {
    701 		error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio,
    702 		    fp->f_cred, FOF_UPDATE_OFFSET);
    703 		tries++;
    704 		if (error == EWOULDBLOCK)
    705 		  	yield();
    706 	} while ((error == EWOULDBLOCK) && (tries < 3));
    707 	FILE_UNUSE(fp, NULL);
    708 
    709 	if (__predict_true(error == 0))
    710 		return (0);
    711 	/*
    712 	 * If error encountered, give up tracing on this vnode.  Don't report
    713 	 * EPIPE as this can easily happen with fktrace()/ktruss.
    714 	 */
    715 	if (error != EPIPE)
    716 		log(LOG_NOTICE,
    717 		    "ktrace write failed, errno %d, tracing stopped\n",
    718 		    error);
    719 	proclist_lock_read();
    720 	for (p = LIST_FIRST(&allproc); p != NULL; p = LIST_NEXT(p, p_list)) {
    721 		if (ktrsamefile(p->p_tracep, fp))
    722 			ktrderef(p);
    723 	}
    724 	proclist_unlock_read();
    725 
    726 	return (error);
    727 }
    728 
    729 /*
    730  * Return true if caller has permission to set the ktracing state
    731  * of target.  Essentially, the target can't possess any
    732  * more permissions than the caller.  KTRFAC_ROOT signifies that
    733  * root previously set the tracing status on the target process, and
    734  * so, only root may further change it.
    735  *
    736  * TODO: check groups.  use caller effective gid.
    737  */
    738 int
    739 ktrcanset(callp, targetp)
    740 	struct proc *callp;
    741 	struct proc *targetp;
    742 {
    743 	struct pcred *caller = callp->p_cred;
    744 	struct pcred *target = targetp->p_cred;
    745 
    746 	if ((caller->pc_ucred->cr_uid == target->p_ruid &&
    747 	     target->p_ruid == target->p_svuid &&
    748 	     caller->p_rgid == target->p_rgid &&	/* XXX */
    749 	     target->p_rgid == target->p_svgid &&
    750 	     (targetp->p_traceflag & KTRFAC_ROOT) == 0 &&
    751 	     (targetp->p_flag & P_SUGID) == 0) ||
    752 	     caller->pc_ucred->cr_uid == 0)
    753 		return (1);
    754 
    755 	return (0);
    756 }
    757 #endif /* KTRACE */
    758 
    759 /*
    760  * Put user defined entry to ktrace records.
    761  */
    762 int
    763 sys_utrace(p, v, retval)
    764 	struct proc *p;
    765 	void *v;
    766 	register_t *retval;
    767 {
    768 #ifdef KTRACE
    769 	struct sys_utrace_args /* {
    770 		syscallarg(const char *) label;
    771 		syscallarg(void *) addr;
    772 		syscallarg(size_t) len;
    773 	} */ *uap = v;
    774 
    775 	if (!KTRPOINT(p, KTR_USER))
    776 		return (0);
    777 
    778 	if (SCARG(uap, len) > KTR_USER_MAXLEN)
    779 		return (EINVAL);
    780 
    781 	ktruser(p, SCARG(uap, label), SCARG(uap, addr), SCARG(uap, len), 1);
    782 
    783 	return (0);
    784 #else /* !KTRACE */
    785 	return ENOSYS;
    786 #endif /* KTRACE */
    787 }
    788