Home | History | Annotate | Line # | Download | only in kern
kern_ktrace.c revision 1.65
      1 /*	$NetBSD: kern_ktrace.c,v 1.65 2002/12/20 05:06:25 gmcgarry 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.65 2002/12/20 05:06:25 gmcgarry 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, args)
    131 	struct proc *p;
    132 	register_t code;
    133 	register_t realcode;
    134 	register_t args[];
    135 {
    136 	struct ktr_header kth;
    137 	struct ktr_syscall *ktp;
    138 	register_t *argp;
    139 	int argsize;
    140 	size_t len;
    141 	u_int i;
    142 
    143 	argsize = p->p_emul->e_sysent[code].sy_narg * sizeof (register_t);
    144 	len = sizeof(struct ktr_syscall) + argsize;
    145 
    146 	p->p_traceflag |= KTRFAC_ACTIVE;
    147 	ktrinitheader(&kth, p, KTR_SYSCALL);
    148 	ktp = malloc(len, M_TEMP, M_WAITOK);
    149 	ktp->ktr_code = realcode;
    150 	ktp->ktr_argsize = argsize;
    151 	argp = (register_t *)((char *)ktp + sizeof(struct ktr_syscall));
    152 	for (i = 0; i < (argsize / sizeof(*argp)); i++)
    153 		*argp++ = args[i];
    154 	kth.ktr_buf = (caddr_t)ktp;
    155 	kth.ktr_len = len;
    156 	(void) ktrwrite(p, &kth);
    157 	free(ktp, M_TEMP);
    158 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    159 }
    160 
    161 void
    162 ktrsysret(p, code, error, retval)
    163 	struct proc *p;
    164 	register_t code;
    165 	int error;
    166 	register_t retval;
    167 {
    168 	struct ktr_header kth;
    169 	struct ktr_sysret ktp;
    170 
    171 	p->p_traceflag |= KTRFAC_ACTIVE;
    172 	ktrinitheader(&kth, p, KTR_SYSRET);
    173 	ktp.ktr_code = code;
    174 	ktp.ktr_eosys = 0;			/* XXX unused */
    175 	ktp.ktr_error = error;
    176 	ktp.ktr_retval = retval;		/* what about val2 ? */
    177 
    178 	kth.ktr_buf = (caddr_t)&ktp;
    179 	kth.ktr_len = sizeof(struct ktr_sysret);
    180 
    181 	(void) ktrwrite(p, &kth);
    182 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    183 }
    184 
    185 void
    186 ktrnamei(p, path)
    187 	struct proc *p;
    188 	char *path;
    189 {
    190 	struct ktr_header kth;
    191 
    192 	p->p_traceflag |= KTRFAC_ACTIVE;
    193 	ktrinitheader(&kth, p, KTR_NAMEI);
    194 	kth.ktr_len = strlen(path);
    195 	kth.ktr_buf = path;
    196 
    197 	(void) ktrwrite(p, &kth);
    198 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    199 }
    200 
    201 void
    202 ktremul(p)
    203 	struct proc *p;
    204 {
    205 	struct ktr_header kth;
    206 	const char *emul = p->p_emul->e_name;
    207 
    208 	p->p_traceflag |= KTRFAC_ACTIVE;
    209 	ktrinitheader(&kth, p, KTR_EMUL);
    210 	kth.ktr_len = strlen(emul);
    211 	kth.ktr_buf = (caddr_t)emul;
    212 
    213 	(void) ktrwrite(p, &kth);
    214 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    215 }
    216 
    217 void
    218 ktrgenio(p, fd, rw, iov, len, error)
    219 	struct proc *p;
    220 	int fd;
    221 	enum uio_rw rw;
    222 	struct iovec *iov;
    223 	int len;
    224 	int error;
    225 {
    226 	struct ktr_header kth;
    227 	struct ktr_genio *ktp;
    228 	caddr_t cp;
    229 	int resid = len, cnt;
    230 	int buflen;
    231 
    232 	if (error)
    233 		return;
    234 
    235 	p->p_traceflag |= KTRFAC_ACTIVE;
    236 
    237 	buflen = min(PAGE_SIZE, len + sizeof(struct ktr_genio));
    238 
    239 	ktrinitheader(&kth, p, KTR_GENIO);
    240 	ktp = malloc(buflen, M_TEMP, M_WAITOK);
    241 	ktp->ktr_fd = fd;
    242 	ktp->ktr_rw = rw;
    243 
    244 	kth.ktr_buf = (caddr_t)ktp;
    245 
    246 	cp = (caddr_t)((char *)ktp + sizeof(struct ktr_genio));
    247 	buflen -= sizeof(struct ktr_genio);
    248 
    249 	while (resid > 0) {
    250 		KDASSERT(p->p_cpu != NULL);
    251 		KDASSERT(p->p_cpu == curcpu());
    252 		if (p->p_cpu->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
    253 			preempt(NULL);
    254 
    255 		cnt = min(iov->iov_len, buflen);
    256 		if (cnt > resid)
    257 			cnt = resid;
    258 		if (copyin(iov->iov_base, cp, cnt))
    259 			break;
    260 
    261 		kth.ktr_len = cnt + sizeof(struct ktr_genio);
    262 
    263 		if (__predict_false(ktrwrite(p, &kth) != 0))
    264 			break;
    265 
    266 		iov->iov_base = (caddr_t)iov->iov_base + cnt;
    267 		iov->iov_len -= cnt;
    268 
    269 		if (iov->iov_len == 0)
    270 			iov++;
    271 
    272 		resid -= cnt;
    273 	}
    274 
    275 	free(ktp, M_TEMP);
    276 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    277 }
    278 
    279 void
    280 ktrpsig(p, sig, action, mask, code)
    281 	struct proc *p;
    282 	int sig;
    283 	sig_t action;
    284 	sigset_t *mask;
    285 	int code;
    286 {
    287 	struct ktr_header kth;
    288 	struct ktr_psig	kp;
    289 
    290 	p->p_traceflag |= KTRFAC_ACTIVE;
    291 	ktrinitheader(&kth, p, KTR_PSIG);
    292 	kp.signo = (char)sig;
    293 	kp.action = action;
    294 	kp.mask = *mask;
    295 	kp.code = code;
    296 	kth.ktr_buf = (caddr_t)&kp;
    297 	kth.ktr_len = sizeof(struct ktr_psig);
    298 
    299 	(void) ktrwrite(p, &kth);
    300 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    301 }
    302 
    303 void
    304 ktrcsw(p, out, user)
    305 	struct proc *p;
    306 	int out;
    307 	int user;
    308 {
    309 	struct ktr_header kth;
    310 	struct ktr_csw kc;
    311 
    312 	p->p_traceflag |= KTRFAC_ACTIVE;
    313 	ktrinitheader(&kth, p, KTR_CSW);
    314 	kc.out = out;
    315 	kc.user = user;
    316 	kth.ktr_buf = (caddr_t)&kc;
    317 	kth.ktr_len = sizeof(struct ktr_csw);
    318 
    319 	(void) ktrwrite(p, &kth);
    320 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    321 }
    322 
    323 void
    324 ktruser(p, id, addr, len, ustr)
    325 	struct proc *p;
    326 	const char *id;
    327 	void *addr;
    328 	size_t len;
    329 	int ustr;
    330 {
    331 	struct ktr_header kth;
    332 	struct ktr_user *ktp;
    333 	caddr_t user_dta;
    334 
    335 	p->p_traceflag |= KTRFAC_ACTIVE;
    336 	ktrinitheader(&kth, p, KTR_USER);
    337 	ktp = malloc(sizeof(struct ktr_user) + len, M_TEMP, M_WAITOK);
    338 	if (ustr) {
    339 		if (copyinstr(id, ktp->ktr_id, KTR_USER_MAXIDLEN, NULL) != 0)
    340 			ktp->ktr_id[0] = '\0';
    341 	} else
    342 		strncpy(ktp->ktr_id, id, KTR_USER_MAXIDLEN);
    343 	ktp->ktr_id[KTR_USER_MAXIDLEN-1] = '\0';
    344 
    345 	user_dta = (caddr_t) ((char *)ktp + sizeof(struct ktr_user));
    346 	if (copyin(addr, (void *) user_dta, len) != 0)
    347 		len = 0;
    348 
    349 	kth.ktr_buf = (void *)ktp;
    350 	kth.ktr_len = sizeof(struct ktr_user) + len;
    351 	(void) ktrwrite(p, &kth);
    352 
    353 	free(ktp, M_TEMP);
    354 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    355 
    356 }
    357 
    358 void
    359 ktrmmsg(p, msgh, size)
    360 	struct proc *p;
    361 	const void *msgh;
    362 	size_t size;
    363 {
    364 	struct ktr_header kth;
    365 	struct ktr_mmsg	*kp;
    366 
    367 	p->p_traceflag |= KTRFAC_ACTIVE;
    368 	ktrinitheader(&kth, p, KTR_MMSG);
    369 
    370 	kp = (struct ktr_mmsg *)msgh;
    371 	kth.ktr_buf = (caddr_t)kp;
    372 	kth.ktr_len = size;
    373 	(void) ktrwrite(p, &kth);
    374 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    375 }
    376 
    377 /* Interface and common routines */
    378 
    379 int
    380 ktrace_common(curp, ops, facs, pid, fp)
    381 	struct proc *curp;
    382 	int ops;
    383 	int facs;
    384 	int pid;
    385 	struct file *fp;
    386 {
    387 	int ret = 0;
    388 	int error = 0;
    389 	int one = 1;
    390 	int descend;
    391 	struct proc *p;
    392 	struct pgrp *pg;
    393 
    394 	curp->p_traceflag |= KTRFAC_ACTIVE;
    395 	descend = ops & KTRFLAG_DESCEND;
    396 	facs = facs & ~((unsigned) KTRFAC_ROOT);
    397 
    398 	/*
    399 	 * Clear all uses of the tracefile
    400 	 */
    401 	if (KTROP(ops) == KTROP_CLEARFILE) {
    402 		proclist_lock_read();
    403 		for (p = LIST_FIRST(&allproc); p != NULL;
    404 		     p = LIST_NEXT(p, p_list)) {
    405 			if (ktrsamefile(p->p_tracep, fp)) {
    406 				if (ktrcanset(curp, p))
    407 					ktrderef(p);
    408 				else
    409 					error = EPERM;
    410 			}
    411 		}
    412 		proclist_unlock_read();
    413 		goto done;
    414 	}
    415 
    416 	/*
    417 	 * Mark fp non-blocking, to avoid problems from possible deadlocks.
    418 	 */
    419 
    420 	if (fp != NULL) {
    421 		fp->f_flag |= FNONBLOCK;
    422 		(*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&one, curp);
    423 	}
    424 
    425 	/*
    426 	 * need something to (un)trace (XXX - why is this here?)
    427 	 */
    428 	if (!facs) {
    429 		error = EINVAL;
    430 		goto done;
    431 	}
    432 	/*
    433 	 * do it
    434 	 */
    435 	if (pid < 0) {
    436 		/*
    437 		 * by process group
    438 		 */
    439 		pg = pgfind(-pid);
    440 		if (pg == NULL) {
    441 			error = ESRCH;
    442 			goto done;
    443 		}
    444 		for (p = LIST_FIRST(&pg->pg_members); p != NULL;
    445 		     p = LIST_NEXT(p, p_pglist)) {
    446 			if (descend)
    447 				ret |= ktrsetchildren(curp, p, ops, facs, fp);
    448 			else
    449 				ret |= ktrops(curp, p, ops, facs, fp);
    450 		}
    451 
    452 	} else {
    453 		/*
    454 		 * by pid
    455 		 */
    456 		p = pfind(pid);
    457 		if (p == NULL) {
    458 			error = ESRCH;
    459 			goto done;
    460 		}
    461 		if (descend)
    462 			ret |= ktrsetchildren(curp, p, ops, facs, fp);
    463 		else
    464 			ret |= ktrops(curp, p, ops, facs, fp);
    465 	}
    466 	if (!ret)
    467 		error = EPERM;
    468 done:
    469 	curp->p_traceflag &= ~KTRFAC_ACTIVE;
    470 	return (error);
    471 }
    472 
    473 /*
    474  * ktrace system call
    475  */
    476 /* ARGSUSED */
    477 int
    478 sys_fktrace(curp, v, retval)
    479 	struct proc *curp;
    480 	void *v;
    481 	register_t *retval;
    482 {
    483 	struct sys_fktrace_args /* {
    484 		syscallarg(int) fd;
    485 		syscallarg(int) ops;
    486 		syscallarg(int) facs;
    487 		syscallarg(int) pid;
    488 	} */ *uap = v;
    489 	struct file *fp = NULL;
    490 	struct filedesc *fdp = curp->p_fd;
    491 
    492 	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
    493 		return (EBADF);
    494 
    495 	if ((fp->f_flag & FWRITE) == 0)
    496 		return (EBADF);
    497 
    498 	return ktrace_common(curp, SCARG(uap, ops),
    499 	    SCARG(uap, facs), SCARG(uap, pid), fp);
    500 }
    501 
    502 /*
    503  * ktrace system call
    504  */
    505 /* ARGSUSED */
    506 int
    507 sys_ktrace(curp, v, retval)
    508 	struct proc *curp;
    509 	void *v;
    510 	register_t *retval;
    511 {
    512 	struct sys_ktrace_args /* {
    513 		syscallarg(const char *) fname;
    514 		syscallarg(int) ops;
    515 		syscallarg(int) facs;
    516 		syscallarg(int) pid;
    517 	} */ *uap = v;
    518 	struct vnode *vp = NULL;
    519 	struct file *fp = NULL;
    520 	int fd;
    521 	int ops = SCARG(uap, ops);
    522 	int error = 0;
    523 	struct nameidata nd;
    524 
    525 	ops = KTROP(ops) | (ops & KTRFLAG_DESCEND);
    526 
    527 	curp->p_traceflag |= KTRFAC_ACTIVE;
    528 	if (ops != KTROP_CLEAR) {
    529 		/*
    530 		 * an operation which requires a file argument.
    531 		 */
    532 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname),
    533 		    curp);
    534 		if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
    535 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
    536 			return (error);
    537 		}
    538 		vp = nd.ni_vp;
    539 		VOP_UNLOCK(vp, 0);
    540 		if (vp->v_type != VREG) {
    541 			(void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp);
    542 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
    543 			return (EACCES);
    544 		}
    545 		/*
    546 		 * XXX This uses up a file descriptor slot in the
    547 		 * tracing process for the duration of this syscall.
    548 		 * This is not expected to be a problem.  If
    549 		 * falloc(NULL, ...) DTRT we could skip that part, but
    550 		 * that would require changing its interface to allow
    551 		 * the caller to pass in a ucred..
    552 		 *
    553 		 * This will FILE_USE the fp it returns, if any.
    554 		 * Keep it in use until we return.
    555 		 */
    556 		if ((error = falloc(curp, &fp, &fd)) != 0)
    557 			goto done;
    558 
    559 		fp->f_flag = FWRITE|FAPPEND;
    560 		fp->f_type = DTYPE_VNODE;
    561 		fp->f_ops = &vnops;
    562 		fp->f_data = (caddr_t)vp;
    563 		FILE_SET_MATURE(fp);
    564 		vp = NULL;
    565 	}
    566 	error = ktrace_common(curp, SCARG(uap, ops), SCARG(uap, facs),
    567 	    SCARG(uap, pid), fp);
    568 done:
    569 	if (vp != NULL)
    570 		(void) vn_close(vp, FWRITE, curp->p_ucred, curp);
    571 	if (fp != NULL) {
    572 		FILE_UNUSE(fp, curp);	/* release file */
    573 		fdrelease(curp, fd); 	/* release fd table slot */
    574 	}
    575 	return (error);
    576 }
    577 
    578 int
    579 ktrops(curp, p, ops, facs, fp)
    580 	struct proc *curp;
    581 	struct proc *p;
    582 	int ops;
    583 	int facs;
    584 	struct file *fp;
    585 {
    586 
    587 	if (!ktrcanset(curp, p))
    588 		return (0);
    589 	if (KTROP(ops) == KTROP_SET) {
    590 		if (p->p_tracep != fp) {
    591 			/*
    592 			 * if trace file already in use, relinquish
    593 			 */
    594 			ktrderef(p);
    595 			p->p_tracep = fp;
    596 			ktradref(p);
    597 		}
    598 		p->p_traceflag |= facs;
    599 		if (curp->p_ucred->cr_uid == 0)
    600 			p->p_traceflag |= KTRFAC_ROOT;
    601 	} else {
    602 		/* KTROP_CLEAR */
    603 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
    604 			/* no more tracing */
    605 			ktrderef(p);
    606 		}
    607 	}
    608 
    609 	/*
    610 	 * Emit an emulation record, every time there is a ktrace
    611 	 * change/attach request.
    612 	 */
    613 	if (KTRPOINT(p, KTR_EMUL))
    614 		ktremul(p);
    615 #ifdef __HAVE_SYSCALL_INTERN
    616 	(*p->p_emul->e_syscall_intern)(p);
    617 #endif
    618 
    619 	return (1);
    620 }
    621 
    622 int
    623 ktrsetchildren(curp, top, ops, facs, fp)
    624 	struct proc *curp;
    625 	struct proc *top;
    626 	int ops;
    627 	int facs;
    628 	struct file *fp;
    629 {
    630 	struct proc *p;
    631 	int ret = 0;
    632 
    633 	p = top;
    634 	for (;;) {
    635 		ret |= ktrops(curp, p, ops, facs, fp);
    636 		/*
    637 		 * If this process has children, descend to them next,
    638 		 * otherwise do any siblings, and if done with this level,
    639 		 * follow back up the tree (but not past top).
    640 		 */
    641 		if (LIST_FIRST(&p->p_children) != NULL)
    642 			p = LIST_FIRST(&p->p_children);
    643 		else for (;;) {
    644 			if (p == top)
    645 				return (ret);
    646 			if (LIST_NEXT(p, p_sibling) != NULL) {
    647 				p = LIST_NEXT(p, p_sibling);
    648 				break;
    649 			}
    650 			p = p->p_pptr;
    651 		}
    652 	}
    653 	/*NOTREACHED*/
    654 }
    655 
    656 int
    657 ktrwrite(p, kth)
    658 	struct proc *p;
    659 	struct ktr_header *kth;
    660 {
    661 	struct uio auio;
    662 	struct iovec aiov[2];
    663 	int error, tries;
    664 	struct file *fp = p->p_tracep;
    665 
    666 	if (fp == NULL)
    667 		return 0;
    668 
    669 	auio.uio_iov = &aiov[0];
    670 	auio.uio_offset = 0;
    671 	auio.uio_segflg = UIO_SYSSPACE;
    672 	auio.uio_rw = UIO_WRITE;
    673 	aiov[0].iov_base = (caddr_t)kth;
    674 	aiov[0].iov_len = sizeof(struct ktr_header);
    675 	auio.uio_resid = sizeof(struct ktr_header);
    676 	auio.uio_iovcnt = 1;
    677 	auio.uio_procp = (struct proc *)0;
    678 	if (kth->ktr_len > 0) {
    679 		auio.uio_iovcnt++;
    680 		aiov[1].iov_base = kth->ktr_buf;
    681 		aiov[1].iov_len = kth->ktr_len;
    682 		auio.uio_resid += kth->ktr_len;
    683 	}
    684 
    685 	FILE_USE(fp);
    686 
    687 	tries = 0;
    688 	do {
    689 		error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio,
    690 		    fp->f_cred, FOF_UPDATE_OFFSET);
    691 		tries++;
    692 		if (error == EWOULDBLOCK)
    693 		  	preempt(NULL);
    694 	} while ((error == EWOULDBLOCK) && (tries < 3));
    695 	FILE_UNUSE(fp, NULL);
    696 
    697 	if (__predict_true(error == 0))
    698 		return (0);
    699 	/*
    700 	 * If error encountered, give up tracing on this vnode.  Don't report
    701 	 * EPIPE as this can easily happen with fktrace()/ktruss.
    702 	 */
    703 	if (error != EPIPE)
    704 		log(LOG_NOTICE,
    705 		    "ktrace write failed, errno %d, tracing stopped\n",
    706 		    error);
    707 	proclist_lock_read();
    708 	for (p = LIST_FIRST(&allproc); p != NULL; p = LIST_NEXT(p, p_list)) {
    709 		if (ktrsamefile(p->p_tracep, fp))
    710 			ktrderef(p);
    711 	}
    712 	proclist_unlock_read();
    713 
    714 	return (error);
    715 }
    716 
    717 /*
    718  * Return true if caller has permission to set the ktracing state
    719  * of target.  Essentially, the target can't possess any
    720  * more permissions than the caller.  KTRFAC_ROOT signifies that
    721  * root previously set the tracing status on the target process, and
    722  * so, only root may further change it.
    723  *
    724  * TODO: check groups.  use caller effective gid.
    725  */
    726 int
    727 ktrcanset(callp, targetp)
    728 	struct proc *callp;
    729 	struct proc *targetp;
    730 {
    731 	struct pcred *caller = callp->p_cred;
    732 	struct pcred *target = targetp->p_cred;
    733 
    734 	if ((caller->pc_ucred->cr_uid == target->p_ruid &&
    735 	     target->p_ruid == target->p_svuid &&
    736 	     caller->p_rgid == target->p_rgid &&	/* XXX */
    737 	     target->p_rgid == target->p_svgid &&
    738 	     (targetp->p_traceflag & KTRFAC_ROOT) == 0 &&
    739 	     (targetp->p_flag & P_SUGID) == 0) ||
    740 	     caller->pc_ucred->cr_uid == 0)
    741 		return (1);
    742 
    743 	return (0);
    744 }
    745 #endif /* KTRACE */
    746 
    747 /*
    748  * Put user defined entry to ktrace records.
    749  */
    750 int
    751 sys_utrace(p, v, retval)
    752 	struct proc *p;
    753 	void *v;
    754 	register_t *retval;
    755 {
    756 #ifdef KTRACE
    757 	struct sys_utrace_args /* {
    758 		syscallarg(const char *) label;
    759 		syscallarg(void *) addr;
    760 		syscallarg(size_t) len;
    761 	} */ *uap = v;
    762 
    763 	if (!KTRPOINT(p, KTR_USER))
    764 		return (0);
    765 
    766 	if (SCARG(uap, len) > KTR_USER_MAXLEN)
    767 		return (EINVAL);
    768 
    769 	ktruser(p, SCARG(uap, label), SCARG(uap, addr), SCARG(uap, len), 1);
    770 
    771 	return (0);
    772 #else /* !KTRACE */
    773 	return ENOSYS;
    774 #endif /* KTRACE */
    775 }
    776