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