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