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