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