Home | History | Annotate | Line # | Download | only in kern
kern_ktrace.c revision 1.81
      1 /*	$NetBSD: kern_ktrace.c,v 1.81 2003/11/02 12:01:40 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. 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.81 2003/11/02 12:01:40 jdolecek 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 /* Interface and common routines */
    407 
    408 int
    409 ktrace_common(curp, ops, facs, pid, fp)
    410 	struct proc *curp;
    411 	int ops;
    412 	int facs;
    413 	int pid;
    414 	struct file *fp;
    415 {
    416 	int ret = 0;
    417 	int error = 0;
    418 	int one = 1;
    419 	int descend;
    420 	struct proc *p;
    421 	struct pgrp *pg;
    422 
    423 	curp->p_traceflag |= KTRFAC_ACTIVE;
    424 	descend = ops & KTRFLAG_DESCEND;
    425 	facs = facs & ~((unsigned) KTRFAC_ROOT);
    426 
    427 	/*
    428 	 * Clear all uses of the tracefile
    429 	 */
    430 	if (KTROP(ops) == KTROP_CLEARFILE) {
    431 		proclist_lock_read();
    432 		LIST_FOREACH(p, &allproc, p_list) {
    433 			if (ktrsamefile(p->p_tracep, fp)) {
    434 				if (ktrcanset(curp, p))
    435 					ktrderef(p);
    436 				else
    437 					error = EPERM;
    438 			}
    439 		}
    440 		proclist_unlock_read();
    441 		goto done;
    442 	}
    443 
    444 	/*
    445 	 * Mark fp non-blocking, to avoid problems from possible deadlocks.
    446 	 */
    447 
    448 	if (fp != NULL) {
    449 		fp->f_flag |= FNONBLOCK;
    450 		(*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&one, curp);
    451 	}
    452 
    453 	/*
    454 	 * need something to (un)trace (XXX - why is this here?)
    455 	 */
    456 	if (!facs) {
    457 		error = EINVAL;
    458 		goto done;
    459 	}
    460 	/*
    461 	 * do it
    462 	 */
    463 	if (pid < 0) {
    464 		/*
    465 		 * by process group
    466 		 */
    467 		pg = pgfind(-pid);
    468 		if (pg == NULL) {
    469 			error = ESRCH;
    470 			goto done;
    471 		}
    472 		LIST_FOREACH(p, &pg->pg_members, p_pglist) {
    473 			if (descend)
    474 				ret |= ktrsetchildren(curp, p, ops, facs, fp);
    475 			else
    476 				ret |= ktrops(curp, p, ops, facs, fp);
    477 		}
    478 
    479 	} else {
    480 		/*
    481 		 * by pid
    482 		 */
    483 		p = pfind(pid);
    484 		if (p == NULL) {
    485 			error = ESRCH;
    486 			goto done;
    487 		}
    488 		if (descend)
    489 			ret |= ktrsetchildren(curp, p, ops, facs, fp);
    490 		else
    491 			ret |= ktrops(curp, p, ops, facs, fp);
    492 	}
    493 	if (!ret)
    494 		error = EPERM;
    495 done:
    496 	curp->p_traceflag &= ~KTRFAC_ACTIVE;
    497 	return (error);
    498 }
    499 
    500 /*
    501  * ktrace system call
    502  */
    503 /* ARGSUSED */
    504 int
    505 sys_fktrace(l, v, retval)
    506 	struct lwp *l;
    507 	void *v;
    508 	register_t *retval;
    509 {
    510 	struct sys_fktrace_args /* {
    511 		syscallarg(int) fd;
    512 		syscallarg(int) ops;
    513 		syscallarg(int) facs;
    514 		syscallarg(int) pid;
    515 	} */ *uap = v;
    516 	struct proc *curp = l->l_proc;
    517 	struct file *fp = NULL;
    518 	struct filedesc *fdp = curp->p_fd;
    519 	int error;
    520 
    521 	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
    522 		return (EBADF);
    523 
    524 	FILE_USE(fp);
    525 
    526 	if ((fp->f_flag & FWRITE) == 0)
    527 		error = EBADF;
    528 	else
    529 		error = ktrace_common(curp, SCARG(uap, ops),
    530 		    SCARG(uap, facs), SCARG(uap, pid), fp);
    531 
    532 	FILE_UNUSE(fp, curp);
    533 
    534 	return error;
    535 }
    536 
    537 /*
    538  * ktrace system call
    539  */
    540 /* ARGSUSED */
    541 int
    542 sys_ktrace(l, v, retval)
    543 	struct lwp *l;
    544 	void *v;
    545 	register_t *retval;
    546 {
    547 	struct sys_ktrace_args /* {
    548 		syscallarg(const char *) fname;
    549 		syscallarg(int) ops;
    550 		syscallarg(int) facs;
    551 		syscallarg(int) pid;
    552 	} */ *uap = v;
    553 	struct proc *curp = l->l_proc;
    554 	struct vnode *vp = NULL;
    555 	struct file *fp = NULL;
    556 	int fd;
    557 	int ops = SCARG(uap, ops);
    558 	int error = 0;
    559 	struct nameidata nd;
    560 
    561 	ops = KTROP(ops) | (ops & KTRFLAG_DESCEND);
    562 
    563 	curp->p_traceflag |= KTRFAC_ACTIVE;
    564 	if ((ops & KTROP_CLEAR) == 0) {
    565 		/*
    566 		 * an operation which requires a file argument.
    567 		 */
    568 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname),
    569 		    curp);
    570 		if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
    571 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
    572 			return (error);
    573 		}
    574 		vp = nd.ni_vp;
    575 		VOP_UNLOCK(vp, 0);
    576 		if (vp->v_type != VREG) {
    577 			(void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp);
    578 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
    579 			return (EACCES);
    580 		}
    581 		/*
    582 		 * XXX This uses up a file descriptor slot in the
    583 		 * tracing process for the duration of this syscall.
    584 		 * This is not expected to be a problem.  If
    585 		 * falloc(NULL, ...) DTRT we could skip that part, but
    586 		 * that would require changing its interface to allow
    587 		 * the caller to pass in a ucred..
    588 		 *
    589 		 * This will FILE_USE the fp it returns, if any.
    590 		 * Keep it in use until we return.
    591 		 */
    592 		if ((error = falloc(curp, &fp, &fd)) != 0)
    593 			goto done;
    594 
    595 		fp->f_flag = FWRITE|FAPPEND;
    596 		fp->f_type = DTYPE_VNODE;
    597 		fp->f_ops = &vnops;
    598 		fp->f_data = (caddr_t)vp;
    599 		FILE_SET_MATURE(fp);
    600 		vp = NULL;
    601 	}
    602 	error = ktrace_common(curp, SCARG(uap, ops), SCARG(uap, facs),
    603 	    SCARG(uap, pid), fp);
    604 done:
    605 	if (vp != NULL)
    606 		(void) vn_close(vp, FWRITE, curp->p_ucred, curp);
    607 	if (fp != NULL) {
    608 		FILE_UNUSE(fp, curp);	/* release file */
    609 		fdrelease(curp, fd); 	/* release fd table slot */
    610 	}
    611 	return (error);
    612 }
    613 
    614 int
    615 ktrops(curp, p, ops, facs, fp)
    616 	struct proc *curp;
    617 	struct proc *p;
    618 	int ops;
    619 	int facs;
    620 	struct file *fp;
    621 {
    622 
    623 	if (!ktrcanset(curp, p))
    624 		return (0);
    625 	if (KTROP(ops) == KTROP_SET) {
    626 		if (p->p_tracep != fp) {
    627 			/*
    628 			 * if trace file already in use, relinquish
    629 			 */
    630 			ktrderef(p);
    631 			p->p_tracep = fp;
    632 			ktradref(p);
    633 		}
    634 		p->p_traceflag |= facs;
    635 		if (curp->p_ucred->cr_uid == 0)
    636 			p->p_traceflag |= KTRFAC_ROOT;
    637 	} else {
    638 		/* KTROP_CLEAR */
    639 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
    640 			/* no more tracing */
    641 			ktrderef(p);
    642 		}
    643 	}
    644 
    645 	/*
    646 	 * Emit an emulation record, every time there is a ktrace
    647 	 * change/attach request.
    648 	 */
    649 	if (KTRPOINT(p, KTR_EMUL))
    650 		ktremul(p);
    651 #ifdef __HAVE_SYSCALL_INTERN
    652 	(*p->p_emul->e_syscall_intern)(p);
    653 #endif
    654 
    655 	return (1);
    656 }
    657 
    658 int
    659 ktrsetchildren(curp, top, ops, facs, fp)
    660 	struct proc *curp;
    661 	struct proc *top;
    662 	int ops;
    663 	int facs;
    664 	struct file *fp;
    665 {
    666 	struct proc *p;
    667 	int ret = 0;
    668 
    669 	p = top;
    670 	for (;;) {
    671 		ret |= ktrops(curp, p, ops, facs, fp);
    672 		/*
    673 		 * If this process has children, descend to them next,
    674 		 * otherwise do any siblings, and if done with this level,
    675 		 * follow back up the tree (but not past top).
    676 		 */
    677 		if (LIST_FIRST(&p->p_children) != NULL)
    678 			p = LIST_FIRST(&p->p_children);
    679 		else for (;;) {
    680 			if (p == top)
    681 				return (ret);
    682 			if (LIST_NEXT(p, p_sibling) != NULL) {
    683 				p = LIST_NEXT(p, p_sibling);
    684 				break;
    685 			}
    686 			p = p->p_pptr;
    687 		}
    688 	}
    689 	/*NOTREACHED*/
    690 }
    691 
    692 int
    693 ktrwrite(p, kth)
    694 	struct proc *p;
    695 	struct ktr_header *kth;
    696 {
    697 	struct uio auio;
    698 	struct iovec aiov[2];
    699 	int error, tries;
    700 	struct file *fp = p->p_tracep;
    701 
    702 	if (fp == NULL)
    703 		return 0;
    704 
    705 	auio.uio_iov = &aiov[0];
    706 	auio.uio_offset = 0;
    707 	auio.uio_segflg = UIO_SYSSPACE;
    708 	auio.uio_rw = UIO_WRITE;
    709 	aiov[0].iov_base = (caddr_t)kth;
    710 	aiov[0].iov_len = sizeof(struct ktr_header);
    711 	auio.uio_resid = sizeof(struct ktr_header);
    712 	auio.uio_iovcnt = 1;
    713 	auio.uio_procp = (struct proc *)0;
    714 	if (kth->ktr_len > 0) {
    715 		auio.uio_iovcnt++;
    716 		aiov[1].iov_base = (void *)kth->ktr_buf;
    717 		aiov[1].iov_len = kth->ktr_len;
    718 		auio.uio_resid += kth->ktr_len;
    719 	}
    720 
    721 	simple_lock(&fp->f_slock);
    722 	FILE_USE(fp);
    723 
    724 	tries = 0;
    725 	do {
    726 		error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio,
    727 		    fp->f_cred, FOF_UPDATE_OFFSET);
    728 		tries++;
    729 		if (error == EWOULDBLOCK)
    730 		  	preempt(1);
    731 	} while ((error == EWOULDBLOCK) && (tries < 3));
    732 	FILE_UNUSE(fp, NULL);
    733 
    734 	if (__predict_true(error == 0))
    735 		return (0);
    736 	/*
    737 	 * If error encountered, give up tracing on this vnode.  Don't report
    738 	 * EPIPE as this can easily happen with fktrace()/ktruss.
    739 	 */
    740 	if (error != EPIPE)
    741 		log(LOG_NOTICE,
    742 		    "ktrace write failed, errno %d, tracing stopped\n",
    743 		    error);
    744 	proclist_lock_read();
    745 	LIST_FOREACH(p, &allproc, p_list) {
    746 		if (ktrsamefile(p->p_tracep, fp))
    747 			ktrderef(p);
    748 	}
    749 	proclist_unlock_read();
    750 
    751 	return (error);
    752 }
    753 
    754 /*
    755  * Return true if caller has permission to set the ktracing state
    756  * of target.  Essentially, the target can't possess any
    757  * more permissions than the caller.  KTRFAC_ROOT signifies that
    758  * root previously set the tracing status on the target process, and
    759  * so, only root may further change it.
    760  *
    761  * TODO: check groups.  use caller effective gid.
    762  */
    763 int
    764 ktrcanset(callp, targetp)
    765 	struct proc *callp;
    766 	struct proc *targetp;
    767 {
    768 	struct pcred *caller = callp->p_cred;
    769 	struct pcred *target = targetp->p_cred;
    770 
    771 	if ((caller->pc_ucred->cr_uid == target->p_ruid &&
    772 	     target->p_ruid == target->p_svuid &&
    773 	     caller->p_rgid == target->p_rgid &&	/* XXX */
    774 	     target->p_rgid == target->p_svgid &&
    775 	     (targetp->p_traceflag & KTRFAC_ROOT) == 0 &&
    776 	     (targetp->p_flag & P_SUGID) == 0) ||
    777 	     caller->pc_ucred->cr_uid == 0)
    778 		return (1);
    779 
    780 	return (0);
    781 }
    782 #endif /* KTRACE */
    783 
    784 /*
    785  * Put user defined entry to ktrace records.
    786  */
    787 int
    788 sys_utrace(l, v, retval)
    789 	struct lwp *l;
    790 	void *v;
    791 	register_t *retval;
    792 {
    793 #ifdef KTRACE
    794 	struct sys_utrace_args /* {
    795 		syscallarg(const char *) label;
    796 		syscallarg(void *) addr;
    797 		syscallarg(size_t) len;
    798 	} */ *uap = v;
    799 	struct proc *p = l->l_proc;
    800 	if (!KTRPOINT(p, KTR_USER))
    801 		return (0);
    802 
    803 	if (SCARG(uap, len) > KTR_USER_MAXLEN)
    804 		return (EINVAL);
    805 
    806 	ktruser(p, SCARG(uap, label), SCARG(uap, addr), SCARG(uap, len), 1);
    807 
    808 	return (0);
    809 #else /* !KTRACE */
    810 	return ENOSYS;
    811 #endif /* KTRACE */
    812 }
    813