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