Home | History | Annotate | Line # | Download | only in kern
kern_ktrace.c revision 1.53.2.10
      1 /*	$NetBSD: kern_ktrace.c,v 1.53.2.10 2002/12/19 00:50:42 thorpej 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.53.2.10 2002/12/19 00:50:42 thorpej 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/sa.h>
     58 #include <sys/syscallargs.h>
     59 
     60 #ifdef KTRACE
     61 
     62 int	ktrace_common(struct proc *, int, int, int, struct file *);
     63 void	ktrinitheader(struct ktr_header *, struct proc *, int);
     64 int	ktrops(struct proc *, struct proc *, int, int, struct file *);
     65 int	ktrsetchildren(struct proc *, struct proc *, int, int,
     66 	    struct file *);
     67 int	ktrwrite(struct proc *, struct ktr_header *);
     68 int	ktrcanset(struct proc *, struct proc *);
     69 int	ktrsamefile(struct file *, struct file *);
     70 
     71 /*
     72  * "deep" compare of two files for the purposes of clearing a trace.
     73  * Returns true if they're the same open file, or if they point at the
     74  * same underlying vnode/socket.
     75  */
     76 
     77 int
     78 ktrsamefile(f1, f2)
     79 	struct file *f1;
     80 	struct file *f2;
     81 {
     82 	return ((f1 == f2) ||
     83 	    ((f1 != NULL) && (f2 != NULL) &&
     84 		(f1->f_type == f2->f_type) &&
     85 		(f1->f_data == f2->f_data)));
     86 }
     87 
     88 void
     89 ktrderef(p)
     90 	struct proc *p;
     91 {
     92 	struct file *fp = p->p_tracep;
     93 	p->p_traceflag = 0;
     94 	if (fp == NULL)
     95 		return;
     96 	FILE_USE(fp);
     97 
     98 	/*
     99 	 * ktrace file descriptor can't be watched (are not visible to
    100 	 * userspace), so no kqueue stuff here
    101 	 */
    102 	closef(fp, NULL);
    103 
    104 	p->p_tracep = NULL;
    105 }
    106 
    107 void
    108 ktradref(p)
    109 	struct proc *p;
    110 {
    111 	struct file *fp = p->p_tracep;
    112 
    113 	fp->f_count++;
    114 }
    115 
    116 void
    117 ktrinitheader(kth, p, type)
    118 	struct ktr_header *kth;
    119 	struct proc *p;
    120 	int type;
    121 {
    122 
    123 	memset(kth, 0, sizeof(*kth));
    124 	kth->ktr_type = type;
    125 	microtime(&kth->ktr_time);
    126 	kth->ktr_pid = p->p_pid;
    127 	memcpy(kth->ktr_comm, p->p_comm, MAXCOMLEN);
    128 }
    129 
    130 void
    131 ktrsyscall(p, code, realcode, args)
    132 	struct proc *p;
    133 	register_t code;
    134 	register_t realcode;
    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 	argsize = p->p_emul->e_sysent[code].sy_narg * sizeof (register_t);
    145 	len = sizeof(struct ktr_syscall) + argsize;
    146 
    147 	p->p_traceflag |= KTRFAC_ACTIVE;
    148 	ktrinitheader(&kth, p, KTR_SYSCALL);
    149 	ktp = malloc(len, M_TEMP, M_WAITOK);
    150 	ktp->ktr_code = realcode;
    151 	ktp->ktr_argsize = argsize;
    152 	argp = (register_t *)((char *)ktp + sizeof(struct ktr_syscall));
    153 	for (i = 0; i < (argsize / sizeof(*argp)); i++)
    154 		*argp++ = args[i];
    155 	kth.ktr_buf = (caddr_t)ktp;
    156 	kth.ktr_len = len;
    157 	(void) ktrwrite(p, &kth);
    158 	free(ktp, M_TEMP);
    159 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    160 }
    161 
    162 void
    163 ktrsysret(p, code, error, retval)
    164 	struct proc *p;
    165 	register_t code;
    166 	int error;
    167 	register_t retval;
    168 {
    169 	struct ktr_header kth;
    170 	struct ktr_sysret ktp;
    171 
    172 	p->p_traceflag |= KTRFAC_ACTIVE;
    173 	ktrinitheader(&kth, p, KTR_SYSRET);
    174 	ktp.ktr_code = code;
    175 	ktp.ktr_eosys = 0;			/* XXX unused */
    176 	ktp.ktr_error = error;
    177 	ktp.ktr_retval = retval;		/* what about val2 ? */
    178 
    179 	kth.ktr_buf = (caddr_t)&ktp;
    180 	kth.ktr_len = sizeof(struct ktr_sysret);
    181 
    182 	(void) ktrwrite(p, &kth);
    183 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    184 }
    185 
    186 void
    187 ktrnamei(p, path)
    188 	struct proc *p;
    189 	char *path;
    190 {
    191 	struct ktr_header kth;
    192 
    193 	p->p_traceflag |= KTRFAC_ACTIVE;
    194 	ktrinitheader(&kth, p, KTR_NAMEI);
    195 	kth.ktr_len = strlen(path);
    196 	kth.ktr_buf = path;
    197 
    198 	(void) ktrwrite(p, &kth);
    199 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    200 }
    201 
    202 void
    203 ktremul(p)
    204 	struct proc *p;
    205 {
    206 	struct ktr_header kth;
    207 	const char *emul = p->p_emul->e_name;
    208 
    209 	p->p_traceflag |= KTRFAC_ACTIVE;
    210 	ktrinitheader(&kth, p, KTR_EMUL);
    211 	kth.ktr_len = strlen(emul);
    212 	kth.ktr_buf = (caddr_t)emul;
    213 
    214 	(void) ktrwrite(p, &kth);
    215 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    216 }
    217 
    218 void
    219 ktrgenio(p, fd, rw, iov, len, error)
    220 	struct proc *p;
    221 	int fd;
    222 	enum uio_rw rw;
    223 	struct iovec *iov;
    224 	int len;
    225 	int error;
    226 {
    227 	struct ktr_header kth;
    228 	struct ktr_genio *ktp;
    229 	caddr_t cp;
    230 	int resid = len, cnt;
    231 	int buflen;
    232 
    233 	if (error)
    234 		return;
    235 
    236 	p->p_traceflag |= KTRFAC_ACTIVE;
    237 
    238 	buflen = min(PAGE_SIZE, len + sizeof(struct ktr_genio));
    239 
    240 	ktrinitheader(&kth, p, KTR_GENIO);
    241 	ktp = malloc(buflen, M_TEMP, M_WAITOK);
    242 	ktp->ktr_fd = fd;
    243 	ktp->ktr_rw = rw;
    244 
    245 	kth.ktr_buf = (caddr_t)ktp;
    246 
    247 	cp = (caddr_t)((char *)ktp + sizeof(struct ktr_genio));
    248 	buflen -= sizeof(struct ktr_genio);
    249 
    250 	while (resid > 0) {
    251 #if 0 /* XXX NJWLWP */
    252 		KDASSERT(p->p_cpu != NULL);
    253 		KDASSERT(p->p_cpu == curcpu());
    254 #endif
    255 		/* XXX NJWLWP */
    256 		if (curcpu()->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(l, v, retval)
    483 	struct lwp *l;
    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 proc *curp = l->l_proc;
    494 	struct file *fp = NULL;
    495 	struct filedesc *fdp = curp->p_fd;
    496 
    497 	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
    498 		return (EBADF);
    499 
    500 	if ((fp->f_flag & FWRITE) == 0)
    501 		return (EBADF);
    502 
    503 	return ktrace_common(curp, SCARG(uap, ops),
    504 	    SCARG(uap, facs), SCARG(uap, pid), fp);
    505 }
    506 
    507 /*
    508  * ktrace system call
    509  */
    510 /* ARGSUSED */
    511 int
    512 sys_ktrace(l, v, retval)
    513 	struct lwp *l;
    514 	void *v;
    515 	register_t *retval;
    516 {
    517 	struct sys_ktrace_args /* {
    518 		syscallarg(const char *) fname;
    519 		syscallarg(int) ops;
    520 		syscallarg(int) facs;
    521 		syscallarg(int) pid;
    522 	} */ *uap = v;
    523 	struct proc *curp = l->l_proc;
    524 	struct vnode *vp = NULL;
    525 	struct file *fp = NULL;
    526 	int fd;
    527 	int ops = SCARG(uap, ops);
    528 	int error = 0;
    529 	struct nameidata nd;
    530 
    531 	ops = KTROP(ops) | (ops & KTRFLAG_DESCEND);
    532 
    533 	curp->p_traceflag |= KTRFAC_ACTIVE;
    534 	if (ops != KTROP_CLEAR) {
    535 		/*
    536 		 * an operation which requires a file argument.
    537 		 */
    538 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname),
    539 		    curp);
    540 		if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
    541 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
    542 			return (error);
    543 		}
    544 		vp = nd.ni_vp;
    545 		VOP_UNLOCK(vp, 0);
    546 		if (vp->v_type != VREG) {
    547 			(void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp);
    548 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
    549 			return (EACCES);
    550 		}
    551 		/*
    552 		 * XXX This uses up a file descriptor slot in the
    553 		 * tracing process for the duration of this syscall.
    554 		 * This is not expected to be a problem.  If
    555 		 * falloc(NULL, ...) DTRT we could skip that part, but
    556 		 * that would require changing its interface to allow
    557 		 * the caller to pass in a ucred..
    558 		 *
    559 		 * This will FILE_USE the fp it returns, if any.
    560 		 * Keep it in use until we return.
    561 		 */
    562 		if ((error = falloc(curp, &fp, &fd)) != 0)
    563 			goto done;
    564 
    565 		fp->f_flag = FWRITE|FAPPEND;
    566 		fp->f_type = DTYPE_VNODE;
    567 		fp->f_ops = &vnops;
    568 		fp->f_data = (caddr_t)vp;
    569 		FILE_SET_MATURE(fp);
    570 		vp = NULL;
    571 	}
    572 	error = ktrace_common(curp, SCARG(uap, ops), SCARG(uap, facs),
    573 	    SCARG(uap, pid), fp);
    574 done:
    575 	if (vp != NULL)
    576 		(void) vn_close(vp, FWRITE, curp->p_ucred, curp);
    577 	if (fp != NULL) {
    578 		FILE_UNUSE(fp, curp);	/* release file */
    579 		fdrelease(curp, fd); 	/* release fd table slot */
    580 	}
    581 	return (error);
    582 }
    583 
    584 int
    585 ktrops(curp, p, ops, facs, fp)
    586 	struct proc *curp;
    587 	struct proc *p;
    588 	int ops;
    589 	int facs;
    590 	struct file *fp;
    591 {
    592 
    593 	if (!ktrcanset(curp, p))
    594 		return (0);
    595 	if (KTROP(ops) == KTROP_SET) {
    596 		if (p->p_tracep != fp) {
    597 			/*
    598 			 * if trace file already in use, relinquish
    599 			 */
    600 			ktrderef(p);
    601 			p->p_tracep = fp;
    602 			ktradref(p);
    603 		}
    604 		p->p_traceflag |= facs;
    605 		if (curp->p_ucred->cr_uid == 0)
    606 			p->p_traceflag |= KTRFAC_ROOT;
    607 	} else {
    608 		/* KTROP_CLEAR */
    609 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
    610 			/* no more tracing */
    611 			ktrderef(p);
    612 		}
    613 	}
    614 
    615 	/*
    616 	 * Emit an emulation record, every time there is a ktrace
    617 	 * change/attach request.
    618 	 */
    619 	if (KTRPOINT(p, KTR_EMUL))
    620 		ktremul(p);
    621 #ifdef __HAVE_SYSCALL_INTERN
    622 	(*p->p_emul->e_syscall_intern)(p);
    623 #endif
    624 
    625 	return (1);
    626 }
    627 
    628 int
    629 ktrsetchildren(curp, top, ops, facs, fp)
    630 	struct proc *curp;
    631 	struct proc *top;
    632 	int ops;
    633 	int facs;
    634 	struct file *fp;
    635 {
    636 	struct proc *p;
    637 	int ret = 0;
    638 
    639 	p = top;
    640 	for (;;) {
    641 		ret |= ktrops(curp, p, ops, facs, fp);
    642 		/*
    643 		 * If this process has children, descend to them next,
    644 		 * otherwise do any siblings, and if done with this level,
    645 		 * follow back up the tree (but not past top).
    646 		 */
    647 		if (LIST_FIRST(&p->p_children) != NULL)
    648 			p = LIST_FIRST(&p->p_children);
    649 		else for (;;) {
    650 			if (p == top)
    651 				return (ret);
    652 			if (LIST_NEXT(p, p_sibling) != NULL) {
    653 				p = LIST_NEXT(p, p_sibling);
    654 				break;
    655 			}
    656 			p = p->p_pptr;
    657 		}
    658 	}
    659 	/*NOTREACHED*/
    660 }
    661 
    662 int
    663 ktrwrite(p, kth)
    664 	struct proc *p;
    665 	struct ktr_header *kth;
    666 {
    667 	struct uio auio;
    668 	struct iovec aiov[2];
    669 	int error, tries;
    670 	struct file *fp = p->p_tracep;
    671 
    672 	if (fp == NULL)
    673 		return 0;
    674 
    675 	auio.uio_iov = &aiov[0];
    676 	auio.uio_offset = 0;
    677 	auio.uio_segflg = UIO_SYSSPACE;
    678 	auio.uio_rw = UIO_WRITE;
    679 	aiov[0].iov_base = (caddr_t)kth;
    680 	aiov[0].iov_len = sizeof(struct ktr_header);
    681 	auio.uio_resid = sizeof(struct ktr_header);
    682 	auio.uio_iovcnt = 1;
    683 	auio.uio_procp = (struct proc *)0;
    684 	if (kth->ktr_len > 0) {
    685 		auio.uio_iovcnt++;
    686 		aiov[1].iov_base = kth->ktr_buf;
    687 		aiov[1].iov_len = kth->ktr_len;
    688 		auio.uio_resid += kth->ktr_len;
    689 	}
    690 
    691 	FILE_USE(fp);
    692 
    693 	tries = 0;
    694 	do {
    695 		error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio,
    696 		    fp->f_cred, FOF_UPDATE_OFFSET);
    697 		tries++;
    698 		if (error == EWOULDBLOCK)
    699 		  	yield();
    700 	} while ((error == EWOULDBLOCK) && (tries < 3));
    701 	FILE_UNUSE(fp, NULL);
    702 
    703 	if (__predict_true(error == 0))
    704 		return (0);
    705 	/*
    706 	 * If error encountered, give up tracing on this vnode.  Don't report
    707 	 * EPIPE as this can easily happen with fktrace()/ktruss.
    708 	 */
    709 	if (error != EPIPE)
    710 		log(LOG_NOTICE,
    711 		    "ktrace write failed, errno %d, tracing stopped\n",
    712 		    error);
    713 	proclist_lock_read();
    714 	for (p = LIST_FIRST(&allproc); p != NULL; p = LIST_NEXT(p, p_list)) {
    715 		if (ktrsamefile(p->p_tracep, fp))
    716 			ktrderef(p);
    717 	}
    718 	proclist_unlock_read();
    719 
    720 	return (error);
    721 }
    722 
    723 /*
    724  * Return true if caller has permission to set the ktracing state
    725  * of target.  Essentially, the target can't possess any
    726  * more permissions than the caller.  KTRFAC_ROOT signifies that
    727  * root previously set the tracing status on the target process, and
    728  * so, only root may further change it.
    729  *
    730  * TODO: check groups.  use caller effective gid.
    731  */
    732 int
    733 ktrcanset(callp, targetp)
    734 	struct proc *callp;
    735 	struct proc *targetp;
    736 {
    737 	struct pcred *caller = callp->p_cred;
    738 	struct pcred *target = targetp->p_cred;
    739 
    740 	if ((caller->pc_ucred->cr_uid == target->p_ruid &&
    741 	     target->p_ruid == target->p_svuid &&
    742 	     caller->p_rgid == target->p_rgid &&	/* XXX */
    743 	     target->p_rgid == target->p_svgid &&
    744 	     (targetp->p_traceflag & KTRFAC_ROOT) == 0 &&
    745 	     (targetp->p_flag & P_SUGID) == 0) ||
    746 	     caller->pc_ucred->cr_uid == 0)
    747 		return (1);
    748 
    749 	return (0);
    750 }
    751 #endif /* KTRACE */
    752 
    753 /*
    754  * Put user defined entry to ktrace records.
    755  */
    756 int
    757 sys_utrace(l, v, retval)
    758 	struct lwp *l;
    759 	void *v;
    760 	register_t *retval;
    761 {
    762 #ifdef KTRACE
    763 	struct sys_utrace_args /* {
    764 		syscallarg(const char *) label;
    765 		syscallarg(void *) addr;
    766 		syscallarg(size_t) len;
    767 	} */ *uap = v;
    768 	struct proc *p = l->l_proc;
    769 	if (!KTRPOINT(p, KTR_USER))
    770 		return (0);
    771 
    772 	if (SCARG(uap, len) > KTR_USER_MAXLEN)
    773 		return (EINVAL);
    774 
    775 	ktruser(p, SCARG(uap, label), SCARG(uap, addr), SCARG(uap, len), 1);
    776 
    777 	return (0);
    778 #else /* !KTRACE */
    779 	return ENOSYS;
    780 #endif /* KTRACE */
    781 }
    782