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