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