Home | History | Annotate | Line # | Download | only in kern
kern_ktrace.c revision 1.53.2.5
      1 /*	$NetBSD: kern_ktrace.c,v 1.53.2.5 2002/06/20 03:47:14 nathanw 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.53.2.5 2002/06/20 03:47:14 nathanw Exp $");
     40 
     41 #include "opt_ktrace.h"
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/lwp.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/sa.h>
     58 #include <sys/syscallargs.h>
     59 
     60 #ifdef KTRACE
     61 
     62 int	ktrace_common(struct proc *, int, int, int, struct file *);
     63 void	ktrinitheader(struct ktr_header *, struct proc *, int);
     64 int	ktrops(struct proc *, struct proc *, int, int, struct file *);
     65 int	ktrsetchildren(struct proc *, struct proc *, int, int,
     66 	    struct file *);
     67 int	ktrwrite(struct proc *, struct ktr_header *);
     68 int	ktrcanset(struct proc *, struct proc *);
     69 int	ktrsamefile(struct file *, struct file *);
     70 
     71 /*
     72  * "deep" compare of two files for the purposes of clearing a trace.
     73  * Returns true if they're the same open file, or if they point at the
     74  * same underlying vnode/socket.
     75  */
     76 
     77 int
     78 ktrsamefile(struct file *f1, struct file *f2)
     79 {
     80 	return ((f1 == f2) ||
     81 	    ((f1 != NULL) && (f2 != NULL) &&
     82 		(f1->f_type == f2->f_type) &&
     83 		(f1->f_data == f2->f_data)));
     84 }
     85 
     86 void
     87 ktrderef(struct proc *p)
     88 {
     89 	struct file *fp = p->p_tracep;
     90 	p->p_traceflag = 0;
     91 	if (fp == NULL)
     92 		return;
     93 	FILE_USE(fp);
     94 	closef(fp, NULL);
     95 
     96 	p->p_tracep = NULL;
     97 }
     98 
     99 void
    100 ktradref(struct proc *p)
    101 {
    102 	struct file *fp = p->p_tracep;
    103 
    104 	fp->f_count++;
    105 }
    106 
    107 void
    108 ktrinitheader(struct ktr_header *kth, struct proc *p, int type)
    109 {
    110 
    111 	memset(kth, 0, sizeof(*kth));
    112 	kth->ktr_type = type;
    113 	microtime(&kth->ktr_time);
    114 	kth->ktr_pid = p->p_pid;
    115 	memcpy(kth->ktr_comm, p->p_comm, MAXCOMLEN);
    116 }
    117 
    118 void
    119 ktrsyscall(struct proc *p, register_t code, register_t args[])
    120 {
    121 	struct ktr_header kth;
    122 	struct ktr_syscall *ktp;
    123 	register_t *argp;
    124 	int argsize;
    125 	size_t len;
    126 	int i;
    127 
    128 	argsize = p->p_emul->e_sysent[code].sy_narg * sizeof (register_t);
    129 	len = sizeof(struct ktr_syscall) + argsize;
    130 
    131 	p->p_traceflag |= KTRFAC_ACTIVE;
    132 	ktrinitheader(&kth, p, KTR_SYSCALL);
    133 	ktp = malloc(len, M_TEMP, M_WAITOK);
    134 	ktp->ktr_code = code;
    135 	ktp->ktr_argsize = argsize;
    136 	argp = (register_t *)((char *)ktp + sizeof(struct ktr_syscall));
    137 	for (i = 0; i < (argsize / sizeof(*argp)); i++)
    138 		*argp++ = args[i];
    139 	kth.ktr_buf = (caddr_t)ktp;
    140 	kth.ktr_len = len;
    141 	(void) ktrwrite(p, &kth);
    142 	free(ktp, M_TEMP);
    143 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    144 }
    145 
    146 void
    147 ktrsysret(struct proc *p, register_t code, int error, register_t retval)
    148 {
    149 	struct ktr_header kth;
    150 	struct ktr_sysret ktp;
    151 
    152 	p->p_traceflag |= KTRFAC_ACTIVE;
    153 	ktrinitheader(&kth, p, KTR_SYSRET);
    154 	ktp.ktr_code = code;
    155 	ktp.ktr_eosys = 0;			/* XXX unused */
    156 	ktp.ktr_error = error;
    157 	ktp.ktr_retval = retval;		/* what about val2 ? */
    158 
    159 	kth.ktr_buf = (caddr_t)&ktp;
    160 	kth.ktr_len = sizeof(struct ktr_sysret);
    161 
    162 	(void) ktrwrite(p, &kth);
    163 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    164 }
    165 
    166 void
    167 ktrnamei(struct proc *p, char *path)
    168 {
    169 	struct ktr_header kth;
    170 
    171 	p->p_traceflag |= KTRFAC_ACTIVE;
    172 	ktrinitheader(&kth, p, KTR_NAMEI);
    173 	kth.ktr_len = strlen(path);
    174 	kth.ktr_buf = path;
    175 
    176 	(void) ktrwrite(p, &kth);
    177 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    178 }
    179 
    180 void
    181 ktremul(struct proc *p)
    182 {
    183 	struct ktr_header kth;
    184 	const char *emul = p->p_emul->e_name;
    185 
    186 	p->p_traceflag |= KTRFAC_ACTIVE;
    187 	ktrinitheader(&kth, p, KTR_EMUL);
    188 	kth.ktr_len = strlen(emul);
    189 	kth.ktr_buf = (caddr_t)emul;
    190 
    191 	(void) ktrwrite(p, &kth);
    192 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    193 }
    194 
    195 void
    196 ktrgenio(struct proc *p, int fd, enum uio_rw rw, struct iovec *iov,
    197     int len, int error)
    198 {
    199 	struct ktr_header kth;
    200 	struct ktr_genio *ktp;
    201 	caddr_t cp;
    202 	int resid = len, cnt;
    203 	int buflen;
    204 
    205 	if (error)
    206 		return;
    207 
    208 	p->p_traceflag |= KTRFAC_ACTIVE;
    209 
    210 	buflen = min(PAGE_SIZE, len + sizeof(struct ktr_genio));
    211 
    212 	ktrinitheader(&kth, p, KTR_GENIO);
    213 	ktp = malloc(buflen, M_TEMP, M_WAITOK);
    214 	ktp->ktr_fd = fd;
    215 	ktp->ktr_rw = rw;
    216 
    217 	kth.ktr_buf = (caddr_t)ktp;
    218 
    219 	cp = (caddr_t)((char *)ktp + sizeof(struct ktr_genio));
    220 	buflen -= sizeof(struct ktr_genio);
    221 
    222 	while (resid > 0) {
    223 #if 0 /* XXX NJWLWP */
    224 		KDASSERT(p->p_cpu != NULL);
    225 		KDASSERT(p->p_cpu == curcpu());
    226 #endif
    227 		/* XXX NJWLWP */
    228 		if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
    229 			preempt(NULL);
    230 
    231 		cnt = min(iov->iov_len, buflen);
    232 		if (cnt > resid)
    233 			cnt = resid;
    234 		if (copyin(iov->iov_base, cp, cnt))
    235 			break;
    236 
    237 		kth.ktr_len = cnt + sizeof(struct ktr_genio);
    238 
    239 		if (__predict_false(ktrwrite(p, &kth) != 0))
    240 			break;
    241 
    242 		iov->iov_base = (caddr_t)iov->iov_base + cnt;
    243 		iov->iov_len -= cnt;
    244 
    245 		if (iov->iov_len == 0)
    246 			iov++;
    247 
    248 		resid -= cnt;
    249 	}
    250 
    251 	free(ktp, M_TEMP);
    252 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    253 }
    254 
    255 void
    256 ktrpsig(struct proc *p, int sig, sig_t action, sigset_t *mask, int code)
    257 {
    258 	struct ktr_header kth;
    259 	struct ktr_psig	kp;
    260 
    261 	p->p_traceflag |= KTRFAC_ACTIVE;
    262 	ktrinitheader(&kth, p, KTR_PSIG);
    263 	kp.signo = (char)sig;
    264 	kp.action = action;
    265 	kp.mask = *mask;
    266 	kp.code = code;
    267 	kth.ktr_buf = (caddr_t)&kp;
    268 	kth.ktr_len = sizeof(struct ktr_psig);
    269 
    270 	(void) ktrwrite(p, &kth);
    271 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    272 }
    273 
    274 void
    275 ktrcsw(struct proc *p, int out, int user)
    276 {
    277 	struct ktr_header kth;
    278 	struct ktr_csw kc;
    279 
    280 	p->p_traceflag |= KTRFAC_ACTIVE;
    281 	ktrinitheader(&kth, p, KTR_CSW);
    282 	kc.out = out;
    283 	kc.user = user;
    284 	kth.ktr_buf = (caddr_t)&kc;
    285 	kth.ktr_len = sizeof(struct ktr_csw);
    286 
    287 	(void) ktrwrite(p, &kth);
    288 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    289 }
    290 
    291 void
    292 ktruser(p, id, addr, len, ustr)
    293 	struct proc *p;
    294 	const char *id;
    295 	void *addr;
    296 	size_t len;
    297 	int ustr;
    298 {
    299 	struct ktr_header kth;
    300 	struct ktr_user *ktp;
    301 	caddr_t user_dta;
    302 
    303 	p->p_traceflag |= KTRFAC_ACTIVE;
    304 	ktrinitheader(&kth, p, KTR_USER);
    305 	ktp = malloc(sizeof(struct ktr_user) + len, M_TEMP, M_WAITOK);
    306 	if (ustr) {
    307 		if (copyinstr(id, ktp->ktr_id, KTR_USER_MAXIDLEN, NULL) != 0)
    308 			ktp->ktr_id[0] = '\0';
    309 	} else
    310 		strncpy(ktp->ktr_id, id, KTR_USER_MAXIDLEN);
    311 	ktp->ktr_id[KTR_USER_MAXIDLEN-1] = '\0';
    312 
    313 	user_dta = (caddr_t) ((char *)ktp + sizeof(struct ktr_user));
    314 	if (copyin(addr, (void *) user_dta, len) != 0)
    315 		len = 0;
    316 
    317 	kth.ktr_buf = (void *)ktp;
    318 	kth.ktr_len = sizeof(struct ktr_user) + len;
    319 	(void) ktrwrite(p, &kth);
    320 
    321 	free(ktp, M_TEMP);
    322 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    323 
    324 }
    325 
    326 /* Interface and common routines */
    327 
    328 int
    329 ktrace_common(struct proc *curp, int ops, int facs, int pid, struct file *fp)
    330 {
    331 	int ret = 0;
    332 	int error = 0;
    333 	int one = 1;
    334 	int descend;
    335 	struct proc *p;
    336 	struct pgrp *pg;
    337 
    338 	curp->p_traceflag |= KTRFAC_ACTIVE;
    339 	descend = ops & KTRFLAG_DESCEND;
    340 	facs = facs & ~((unsigned) KTRFAC_ROOT);
    341 
    342 	/*
    343 	 * Clear all uses of the tracefile
    344 	 */
    345 	if (KTROP(ops) == KTROP_CLEARFILE) {
    346 		proclist_lock_read();
    347 		for (p = LIST_FIRST(&allproc); p != NULL;
    348 		     p = LIST_NEXT(p, p_list)) {
    349 			if (ktrsamefile(p->p_tracep, fp)) {
    350 				if (ktrcanset(curp, p))
    351 					ktrderef(p);
    352 				else
    353 					error = EPERM;
    354 			}
    355 		}
    356 		proclist_unlock_read();
    357 		goto done;
    358 	}
    359 
    360 	/*
    361 	 * Mark fp non-blocking, to avoid problems from possible deadlocks.
    362 	 */
    363 
    364 	if (fp != NULL) {
    365 		fp->f_flag |= FNONBLOCK;
    366 		(*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&one, curp);
    367 	}
    368 
    369 	/*
    370 	 * need something to (un)trace (XXX - why is this here?)
    371 	 */
    372 	if (!facs) {
    373 		error = EINVAL;
    374 		goto done;
    375 	}
    376 	/*
    377 	 * do it
    378 	 */
    379 	if (pid < 0) {
    380 		/*
    381 		 * by process group
    382 		 */
    383 		pg = pgfind(-pid);
    384 		if (pg == NULL) {
    385 			error = ESRCH;
    386 			goto done;
    387 		}
    388 		for (p = LIST_FIRST(&pg->pg_members); p != NULL;
    389 		     p = LIST_NEXT(p, p_pglist)) {
    390 			if (descend)
    391 				ret |= ktrsetchildren(curp, p, ops, facs, fp);
    392 			else
    393 				ret |= ktrops(curp, p, ops, facs, fp);
    394 		}
    395 
    396 	} else {
    397 		/*
    398 		 * by pid
    399 		 */
    400 		p = pfind(pid);
    401 		if (p == NULL) {
    402 			error = ESRCH;
    403 			goto done;
    404 		}
    405 		if (descend)
    406 			ret |= ktrsetchildren(curp, p, ops, facs, fp);
    407 		else
    408 			ret |= ktrops(curp, p, ops, facs, fp);
    409 	}
    410 	if (!ret)
    411 		error = EPERM;
    412 done:
    413 	curp->p_traceflag &= ~KTRFAC_ACTIVE;
    414 	return (error);
    415 }
    416 
    417 /*
    418  * ktrace system call
    419  */
    420 /* ARGSUSED */
    421 int
    422 sys_fktrace(struct lwp *l, void *v, register_t *retval)
    423 {
    424 	struct sys_fktrace_args /* {
    425 		syscallarg(int) fd;
    426 		syscallarg(int) ops;
    427 		syscallarg(int) facs;
    428 		syscallarg(int) pid;
    429 	} */ *uap = v;
    430 	struct proc *curp = l->l_proc;
    431 	struct file *fp = NULL;
    432 	struct filedesc *fdp = curp->p_fd;
    433 
    434 	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
    435 		return (EBADF);
    436 
    437 	if ((fp->f_flag & FWRITE) == 0)
    438 		return (EBADF);
    439 
    440 	return ktrace_common(curp, SCARG(uap, ops),
    441 	    SCARG(uap, facs), SCARG(uap, pid), fp);
    442 }
    443 
    444 /*
    445  * ktrace system call
    446  */
    447 /* ARGSUSED */
    448 int
    449 sys_ktrace(struct lwp *l, void *v, register_t *retval)
    450 {
    451 	struct sys_ktrace_args /* {
    452 		syscallarg(const char *) fname;
    453 		syscallarg(int) ops;
    454 		syscallarg(int) facs;
    455 		syscallarg(int) pid;
    456 	} */ *uap = v;
    457 	struct proc *curp = l->l_proc;
    458 	struct vnode *vp = NULL;
    459 	struct file *fp = NULL;
    460 	int fd;
    461 	int ops = SCARG(uap, ops);
    462 	int error = 0;
    463 	struct nameidata nd;
    464 
    465 	ops = KTROP(ops) | (ops & KTRFLAG_DESCEND);
    466 
    467 	curp->p_traceflag |= KTRFAC_ACTIVE;
    468 	if (ops != KTROP_CLEAR) {
    469 		/*
    470 		 * an operation which requires a file argument.
    471 		 */
    472 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname),
    473 		    curp);
    474 		if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
    475 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
    476 			return (error);
    477 		}
    478 		vp = nd.ni_vp;
    479 		VOP_UNLOCK(vp, 0);
    480 		if (vp->v_type != VREG) {
    481 			(void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp);
    482 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
    483 			return (EACCES);
    484 		}
    485 		/*
    486 		 * XXX This uses up a file descriptor slot in the
    487 		 * tracing process for the duration of this syscall.
    488 		 * This is not expected to be a problem.  If
    489 		 * falloc(NULL, ...) DTRT we could skip that part, but
    490 		 * that would require changing its interface to allow
    491 		 * the caller to pass in a ucred..
    492 		 *
    493 		 * This will FILE_USE the fp it returns, if any.
    494 		 * Keep it in use until we return.
    495 		 */
    496 		if ((error = falloc(curp, &fp, &fd)) != 0)
    497 			goto done;
    498 
    499 		fp->f_flag = FWRITE|FAPPEND;
    500 		fp->f_type = DTYPE_VNODE;
    501 		fp->f_ops = &vnops;
    502 		fp->f_data = (caddr_t)vp;
    503 		FILE_SET_MATURE(fp);
    504 		vp = NULL;
    505 	}
    506 	error = ktrace_common(curp, SCARG(uap, ops), SCARG(uap, facs),
    507 	    SCARG(uap, pid), fp);
    508 done:
    509 	if (vp != NULL)
    510 		(void) vn_close(vp, FWRITE, curp->p_ucred, curp);
    511 	if (fp != NULL) {
    512 		FILE_UNUSE(fp, curp);	/* release file */
    513 		fdrelease(curp, fd); 	/* release fd table slot */
    514 	}
    515 	return (error);
    516 }
    517 
    518 int
    519 ktrops(struct proc *curp, struct proc *p, int ops, int facs, struct file *fp)
    520 {
    521 
    522 	if (!ktrcanset(curp, p))
    523 		return (0);
    524 	if (KTROP(ops) == KTROP_SET) {
    525 		if (p->p_tracep != fp) {
    526 			/*
    527 			 * if trace file already in use, relinquish
    528 			 */
    529 			ktrderef(p);
    530 			p->p_tracep = fp;
    531 			ktradref(p);
    532 		}
    533 		p->p_traceflag |= facs;
    534 		if (curp->p_ucred->cr_uid == 0)
    535 			p->p_traceflag |= KTRFAC_ROOT;
    536 	} else {
    537 		/* KTROP_CLEAR */
    538 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
    539 			/* no more tracing */
    540 			ktrderef(p);
    541 		}
    542 	}
    543 
    544 	/*
    545 	 * Emit an emulation record, every time there is a ktrace
    546 	 * change/attach request.
    547 	 */
    548 	if (KTRPOINT(p, KTR_EMUL))
    549 		ktremul(p);
    550 #ifdef __HAVE_SYSCALL_INTERN
    551 	(*p->p_emul->e_syscall_intern)(p);
    552 #endif
    553 
    554 	return (1);
    555 }
    556 
    557 int
    558 ktrsetchildren(struct proc *curp, struct proc *top, int ops, int facs,
    559     struct file *fp)
    560 {
    561 	struct proc *p;
    562 	int ret = 0;
    563 
    564 	p = top;
    565 	for (;;) {
    566 		ret |= ktrops(curp, p, ops, facs, fp);
    567 		/*
    568 		 * If this process has children, descend to them next,
    569 		 * otherwise do any siblings, and if done with this level,
    570 		 * follow back up the tree (but not past top).
    571 		 */
    572 		if (LIST_FIRST(&p->p_children) != NULL)
    573 			p = LIST_FIRST(&p->p_children);
    574 		else for (;;) {
    575 			if (p == top)
    576 				return (ret);
    577 			if (LIST_NEXT(p, p_sibling) != NULL) {
    578 				p = LIST_NEXT(p, p_sibling);
    579 				break;
    580 			}
    581 			p = p->p_pptr;
    582 		}
    583 	}
    584 	/*NOTREACHED*/
    585 }
    586 
    587 int
    588 ktrwrite(struct proc *p, struct ktr_header *kth)
    589 {
    590 	struct uio auio;
    591 	struct iovec aiov[2];
    592 	int error, tries;
    593 	struct file *fp = p->p_tracep;
    594 
    595 	if (fp == NULL)
    596 		return 0;
    597 
    598 	auio.uio_iov = &aiov[0];
    599 	auio.uio_offset = 0;
    600 	auio.uio_segflg = UIO_SYSSPACE;
    601 	auio.uio_rw = UIO_WRITE;
    602 	aiov[0].iov_base = (caddr_t)kth;
    603 	aiov[0].iov_len = sizeof(struct ktr_header);
    604 	auio.uio_resid = sizeof(struct ktr_header);
    605 	auio.uio_iovcnt = 1;
    606 	auio.uio_procp = (struct proc *)0;
    607 	if (kth->ktr_len > 0) {
    608 		auio.uio_iovcnt++;
    609 		aiov[1].iov_base = kth->ktr_buf;
    610 		aiov[1].iov_len = kth->ktr_len;
    611 		auio.uio_resid += kth->ktr_len;
    612 	}
    613 
    614 	FILE_USE(fp);
    615 
    616 	tries = 0;
    617 	do {
    618 		error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio,
    619 		    fp->f_cred, FOF_UPDATE_OFFSET);
    620 		tries++;
    621 		if (error == EWOULDBLOCK)
    622 		  	yield();
    623 	} while ((error == EWOULDBLOCK) && (tries < 3));
    624 	FILE_UNUSE(fp, NULL);
    625 
    626 	if (__predict_true(error == 0))
    627 		return (0);
    628 	/*
    629 	 * If error encountered, give up tracing on this vnode.  Don't report
    630 	 * EPIPE as this can easily happen with fktrace()/ktruss.
    631 	 */
    632 	if (error != EPIPE)
    633 		log(LOG_NOTICE,
    634 		    "ktrace write failed, errno %d, tracing stopped\n",
    635 		    error);
    636 	proclist_lock_read();
    637 	for (p = LIST_FIRST(&allproc); p != NULL; p = LIST_NEXT(p, p_list)) {
    638 		if (ktrsamefile(p->p_tracep, fp))
    639 			ktrderef(p);
    640 	}
    641 	proclist_unlock_read();
    642 
    643 	return (error);
    644 }
    645 
    646 /*
    647  * Return true if caller has permission to set the ktracing state
    648  * of target.  Essentially, the target can't possess any
    649  * more permissions than the caller.  KTRFAC_ROOT signifies that
    650  * root previously set the tracing status on the target process, and
    651  * so, only root may further change it.
    652  *
    653  * TODO: check groups.  use caller effective gid.
    654  */
    655 int
    656 ktrcanset(struct proc *callp, struct proc *targetp)
    657 {
    658 	struct pcred *caller = callp->p_cred;
    659 	struct pcred *target = targetp->p_cred;
    660 
    661 	if ((caller->pc_ucred->cr_uid == target->p_ruid &&
    662 	     target->p_ruid == target->p_svuid &&
    663 	     caller->p_rgid == target->p_rgid &&	/* XXX */
    664 	     target->p_rgid == target->p_svgid &&
    665 	     (targetp->p_traceflag & KTRFAC_ROOT) == 0) ||
    666 	     caller->pc_ucred->cr_uid == 0)
    667 		return (1);
    668 
    669 	return (0);
    670 }
    671 #endif /* KTRACE */
    672 
    673 /*
    674  * Put user defined entry to ktrace records.
    675  */
    676 int
    677 sys_utrace(l, v, retval)
    678 	struct lwp *l;
    679 	void *v;
    680 	register_t *retval;
    681 {
    682 #ifdef KTRACE
    683 	struct sys_utrace_args /* {
    684 		syscallarg(const char *) label;
    685 		syscallarg(void *) addr;
    686 		syscallarg(size_t) len;
    687 	} */ *uap = v;
    688 	struct proc *p = l->l_proc;
    689 	if (!KTRPOINT(p, KTR_USER))
    690 		return (0);
    691 
    692 	if (SCARG(uap, len) > KTR_USER_MAXLEN)
    693 		return (EINVAL);
    694 
    695 	ktruser(p, SCARG(uap, label), SCARG(uap, addr), SCARG(uap, len), 1);
    696 
    697 	return (0);
    698 #else /* !KTRACE */
    699 	return ENOSYS;
    700 #endif /* KTRACE */
    701 }
    702