Home | History | Annotate | Line # | Download | only in kern
kern_ktrace.c revision 1.38.2.2
      1  1.38.2.2    bouyer /*	$NetBSD: kern_ktrace.c,v 1.38.2.2 2000/12/13 15:50:20 bouyer 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.38.2.1    bouyer #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.38.2.1    bouyer int	ktrace_common(struct proc *, int, int, int, struct file *);
     58  1.38.2.1    bouyer void	ktrinitheader(struct ktr_header *, struct proc *, int);
     59  1.38.2.1    bouyer int	ktrops(struct proc *, struct proc *, int, int, struct file *);
     60  1.38.2.1    bouyer int	ktrsetchildren(struct proc *, struct proc *, int, int,
     61  1.38.2.1    bouyer 	    struct file *);
     62  1.38.2.1    bouyer int	ktrwrite(struct proc *, struct ktr_header *);
     63  1.38.2.1    bouyer int	ktrcanset(struct proc *, struct proc *);
     64  1.38.2.1    bouyer int	ktrsamefile(struct file *, struct file *);
     65  1.38.2.1    bouyer 
     66  1.38.2.1    bouyer /*
     67  1.38.2.1    bouyer  * "deep" compare of two files for the purposes of clearing a trace.
     68  1.38.2.1    bouyer  * Returns true if they're the same open file, or if they point at the
     69  1.38.2.1    bouyer  * same underlying vnode/socket.
     70  1.38.2.1    bouyer  */
     71  1.38.2.1    bouyer 
     72  1.38.2.1    bouyer int
     73  1.38.2.1    bouyer ktrsamefile(struct file *f1, struct file *f2)
     74  1.38.2.1    bouyer {
     75  1.38.2.1    bouyer 	return ((f1 == f2) ||
     76  1.38.2.1    bouyer 	    ((f1 != NULL) && (f2 != NULL) &&
     77  1.38.2.1    bouyer 		(f1->f_type == f2->f_type) &&
     78  1.38.2.1    bouyer 		(f1->f_data == f2->f_data)));
     79  1.38.2.1    bouyer }
     80      1.22  christos 
     81      1.28  christos void
     82  1.38.2.1    bouyer ktrderef(struct proc *p)
     83      1.28  christos {
     84  1.38.2.1    bouyer 	struct file *fp = p->p_tracep;
     85  1.38.2.1    bouyer 	p->p_traceflag = 0;
     86  1.38.2.1    bouyer 	if (fp == NULL)
     87      1.28  christos 		return;
     88  1.38.2.1    bouyer 	FILE_USE(fp);
     89  1.38.2.1    bouyer 	closef(fp, NULL);
     90      1.28  christos 
     91      1.28  christos 	p->p_tracep = NULL;
     92      1.28  christos }
     93      1.28  christos 
     94      1.28  christos void
     95  1.38.2.1    bouyer ktradref(struct proc *p)
     96      1.28  christos {
     97  1.38.2.1    bouyer 	struct file *fp = p->p_tracep;
     98      1.28  christos 
     99  1.38.2.1    bouyer 	fp->f_count++;
    100      1.28  christos }
    101      1.28  christos 
    102  1.38.2.1    bouyer void
    103  1.38.2.1    bouyer ktrinitheader(struct ktr_header *kth, struct proc *p, int type)
    104       1.1       cgd {
    105       1.1       cgd 
    106  1.38.2.1    bouyer 	memset(kth, 0, sizeof(*kth));
    107       1.1       cgd 	kth->ktr_type = type;
    108       1.1       cgd 	microtime(&kth->ktr_time);
    109       1.1       cgd 	kth->ktr_pid = p->p_pid;
    110      1.32     perry 	memcpy(kth->ktr_comm, p->p_comm, MAXCOMLEN);
    111       1.1       cgd }
    112       1.1       cgd 
    113      1.17       cgd void
    114  1.38.2.1    bouyer ktrsyscall(struct proc *p, register_t code, size_t argsize, register_t args[])
    115  1.38.2.1    bouyer {
    116  1.38.2.1    bouyer 	struct ktr_header kth;
    117  1.38.2.1    bouyer 	struct ktr_syscall *ktp;
    118      1.17       cgd 	register_t *argp;
    119  1.38.2.1    bouyer 	size_t len = sizeof(struct ktr_syscall) + argsize;
    120      1.17       cgd 	int i;
    121       1.1       cgd 
    122       1.9       cgd 	p->p_traceflag |= KTRFAC_ACTIVE;
    123  1.38.2.1    bouyer 	ktrinitheader(&kth, p, KTR_SYSCALL);
    124  1.38.2.1    bouyer 	ktp = malloc(len, M_TEMP, M_WAITOK);
    125       1.1       cgd 	ktp->ktr_code = code;
    126      1.17       cgd 	ktp->ktr_argsize = argsize;
    127      1.17       cgd 	argp = (register_t *)((char *)ktp + sizeof(struct ktr_syscall));
    128      1.31     perry 	for (i = 0; i < (argsize / sizeof(*argp)); i++)
    129       1.1       cgd 		*argp++ = args[i];
    130  1.38.2.1    bouyer 	kth.ktr_buf = (caddr_t)ktp;
    131  1.38.2.1    bouyer 	kth.ktr_len = len;
    132  1.38.2.1    bouyer 	(void) ktrwrite(p, &kth);
    133  1.38.2.1    bouyer 	free(ktp, M_TEMP);
    134       1.9       cgd 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    135       1.1       cgd }
    136       1.1       cgd 
    137      1.17       cgd void
    138  1.38.2.1    bouyer ktrsysret(struct proc *p, register_t code, int error, register_t retval)
    139       1.1       cgd {
    140  1.38.2.1    bouyer 	struct ktr_header kth;
    141       1.1       cgd 	struct ktr_sysret ktp;
    142       1.1       cgd 
    143       1.9       cgd 	p->p_traceflag |= KTRFAC_ACTIVE;
    144  1.38.2.1    bouyer 	ktrinitheader(&kth, p, KTR_SYSRET);
    145       1.1       cgd 	ktp.ktr_code = code;
    146      1.34    kleink 	ktp.ktr_eosys = 0;			/* XXX unused */
    147       1.1       cgd 	ktp.ktr_error = error;
    148       1.1       cgd 	ktp.ktr_retval = retval;		/* what about val2 ? */
    149       1.1       cgd 
    150  1.38.2.1    bouyer 	kth.ktr_buf = (caddr_t)&ktp;
    151  1.38.2.1    bouyer 	kth.ktr_len = sizeof(struct ktr_sysret);
    152       1.1       cgd 
    153  1.38.2.1    bouyer 	(void) ktrwrite(p, &kth);
    154       1.9       cgd 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    155       1.1       cgd }
    156       1.1       cgd 
    157      1.17       cgd void
    158  1.38.2.1    bouyer ktrnamei(struct proc *p, char *path)
    159       1.1       cgd {
    160  1.38.2.1    bouyer 	struct ktr_header kth;
    161       1.1       cgd 
    162       1.9       cgd 	p->p_traceflag |= KTRFAC_ACTIVE;
    163  1.38.2.1    bouyer 	ktrinitheader(&kth, p, KTR_NAMEI);
    164  1.38.2.1    bouyer 	kth.ktr_len = strlen(path);
    165  1.38.2.1    bouyer 	kth.ktr_buf = path;
    166      1.18  christos 
    167  1.38.2.1    bouyer 	(void) ktrwrite(p, &kth);
    168      1.18  christos 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    169      1.18  christos }
    170      1.18  christos 
    171      1.18  christos void
    172  1.38.2.1    bouyer ktremul(struct proc *p)
    173      1.18  christos {
    174  1.38.2.1    bouyer 	struct ktr_header kth;
    175  1.38.2.1    bouyer 	char *emul = p->p_emul->e_name;
    176      1.18  christos 
    177      1.18  christos 	p->p_traceflag |= KTRFAC_ACTIVE;
    178  1.38.2.1    bouyer 	ktrinitheader(&kth, p, KTR_EMUL);
    179  1.38.2.1    bouyer 	kth.ktr_len = strlen(emul);
    180  1.38.2.1    bouyer 	kth.ktr_buf = emul;
    181       1.1       cgd 
    182  1.38.2.1    bouyer 	(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.38.2.1    bouyer ktrgenio(struct proc *p, int fd, enum uio_rw rw, struct iovec *iov,
    188  1.38.2.1    bouyer     int len, int error)
    189       1.1       cgd {
    190  1.38.2.1    bouyer 	struct ktr_header kth;
    191      1.28  christos 	struct ktr_genio *ktp;
    192      1.28  christos 	caddr_t cp;
    193      1.28  christos 	int resid = len, cnt;
    194  1.38.2.1    bouyer 	int buflen;
    195  1.38.2.1    bouyer 
    196       1.1       cgd 	if (error)
    197       1.1       cgd 		return;
    198  1.38.2.1    bouyer 
    199       1.9       cgd 	p->p_traceflag |= KTRFAC_ACTIVE;
    200  1.38.2.1    bouyer 
    201  1.38.2.1    bouyer 	buflen = min(PAGE_SIZE, len + sizeof(struct ktr_genio));
    202  1.38.2.1    bouyer 
    203  1.38.2.1    bouyer 	ktrinitheader(&kth, p, KTR_GENIO);
    204  1.38.2.1    bouyer 	ktp = malloc(buflen, M_TEMP, M_WAITOK);
    205       1.1       cgd 	ktp->ktr_fd = fd;
    206       1.1       cgd 	ktp->ktr_rw = rw;
    207  1.38.2.1    bouyer 
    208  1.38.2.1    bouyer 	kth.ktr_buf = (caddr_t)ktp;
    209  1.38.2.1    bouyer 
    210      1.31     perry 	cp = (caddr_t)((char *)ktp + sizeof(struct ktr_genio));
    211  1.38.2.1    bouyer 	buflen -= sizeof(struct ktr_genio);
    212  1.38.2.1    bouyer 
    213       1.1       cgd 	while (resid > 0) {
    214  1.38.2.1    bouyer 		KDASSERT(p->p_cpu != NULL);
    215  1.38.2.1    bouyer 		KDASSERT(p->p_cpu == curcpu());
    216  1.38.2.1    bouyer 		if (p->p_cpu->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
    217  1.38.2.1    bouyer 			preempt(NULL);
    218  1.38.2.1    bouyer 
    219  1.38.2.1    bouyer 		cnt = min(iov->iov_len, buflen);
    220  1.38.2.1    bouyer 		if (cnt > resid)
    221       1.1       cgd 			cnt = resid;
    222  1.38.2.1    bouyer 		if (copyin(iov->iov_base, cp, cnt))
    223  1.38.2.1    bouyer 			break;
    224  1.38.2.1    bouyer 
    225  1.38.2.1    bouyer 		kth.ktr_len = cnt + sizeof(struct ktr_genio);
    226  1.38.2.1    bouyer 
    227  1.38.2.1    bouyer 		if (__predict_false(ktrwrite(p, &kth) != 0))
    228  1.38.2.1    bouyer 			break;
    229  1.38.2.1    bouyer 
    230  1.38.2.1    bouyer 		iov->iov_base = (caddr_t)iov->iov_base + cnt;
    231  1.38.2.1    bouyer 		iov->iov_len -= cnt;
    232  1.38.2.1    bouyer 
    233  1.38.2.1    bouyer 		if (iov->iov_len == 0)
    234  1.38.2.1    bouyer 			iov++;
    235  1.38.2.1    bouyer 
    236       1.1       cgd 		resid -= cnt;
    237       1.1       cgd 	}
    238       1.1       cgd 
    239  1.38.2.1    bouyer 	free(ktp, M_TEMP);
    240       1.9       cgd 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    241       1.1       cgd }
    242       1.1       cgd 
    243      1.17       cgd void
    244  1.38.2.1    bouyer ktrpsig(struct proc *p, int sig, sig_t action, sigset_t *mask, int code)
    245       1.1       cgd {
    246  1.38.2.1    bouyer 	struct ktr_header kth;
    247       1.1       cgd 	struct ktr_psig	kp;
    248       1.1       cgd 
    249       1.9       cgd 	p->p_traceflag |= KTRFAC_ACTIVE;
    250  1.38.2.1    bouyer 	ktrinitheader(&kth, p, KTR_PSIG);
    251       1.1       cgd 	kp.signo = (char)sig;
    252       1.1       cgd 	kp.action = action;
    253      1.33   mycroft 	kp.mask = *mask;
    254       1.1       cgd 	kp.code = code;
    255  1.38.2.1    bouyer 	kth.ktr_buf = (caddr_t)&kp;
    256  1.38.2.1    bouyer 	kth.ktr_len = sizeof(struct ktr_psig);
    257       1.1       cgd 
    258  1.38.2.1    bouyer 	(void) ktrwrite(p, &kth);
    259       1.9       cgd 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    260       1.9       cgd }
    261       1.9       cgd 
    262      1.17       cgd void
    263  1.38.2.1    bouyer ktrcsw(struct proc *p, int out, int user)
    264  1.38.2.1    bouyer {
    265  1.38.2.1    bouyer 	struct ktr_header kth;
    266  1.38.2.1    bouyer 	struct ktr_csw kc;
    267       1.9       cgd 
    268       1.9       cgd 	p->p_traceflag |= KTRFAC_ACTIVE;
    269  1.38.2.1    bouyer 	ktrinitheader(&kth, p, KTR_CSW);
    270       1.9       cgd 	kc.out = out;
    271       1.9       cgd 	kc.user = user;
    272  1.38.2.1    bouyer 	kth.ktr_buf = (caddr_t)&kc;
    273  1.38.2.1    bouyer 	kth.ktr_len = sizeof(struct ktr_csw);
    274       1.9       cgd 
    275  1.38.2.1    bouyer 	(void) ktrwrite(p, &kth);
    276       1.9       cgd 	p->p_traceflag &= ~KTRFAC_ACTIVE;
    277       1.1       cgd }
    278       1.1       cgd 
    279       1.1       cgd /* Interface and common routines */
    280       1.1       cgd 
    281      1.17       cgd int
    282  1.38.2.1    bouyer ktrace_common(struct proc *curp, int ops, int facs, int pid, struct file *fp)
    283      1.28  christos {
    284      1.28  christos 	int ret = 0;
    285      1.28  christos 	int error = 0;
    286  1.38.2.1    bouyer 	int one = 1;
    287  1.38.2.1    bouyer 	int descend;
    288  1.38.2.1    bouyer 	struct proc *p;
    289  1.38.2.1    bouyer 	struct pgrp *pg;
    290      1.28  christos 
    291      1.28  christos 	curp->p_traceflag |= KTRFAC_ACTIVE;
    292  1.38.2.1    bouyer 	descend = ops & KTRFLAG_DESCEND;
    293  1.38.2.1    bouyer 	facs = facs & ~((unsigned) KTRFAC_ROOT);
    294      1.28  christos 
    295      1.28  christos 	/*
    296      1.28  christos 	 * Clear all uses of the tracefile
    297      1.28  christos 	 */
    298      1.28  christos 	if (KTROP(ops) == KTROP_CLEARFILE) {
    299      1.37   thorpej 		proclist_lock_read();
    300  1.38.2.1    bouyer 		for (p = LIST_FIRST(&allproc); p != NULL;
    301  1.38.2.1    bouyer 		     p = LIST_NEXT(p, p_list)) {
    302  1.38.2.1    bouyer 			if (ktrsamefile(p->p_tracep, fp)) {
    303      1.28  christos 				if (ktrcanset(curp, p))
    304      1.28  christos 					ktrderef(p);
    305      1.28  christos 				else
    306      1.28  christos 					error = EPERM;
    307      1.28  christos 			}
    308      1.28  christos 		}
    309      1.36   thorpej 		proclist_unlock_read();
    310      1.28  christos 		goto done;
    311      1.28  christos 	}
    312  1.38.2.1    bouyer 
    313  1.38.2.1    bouyer 	/*
    314  1.38.2.1    bouyer 	 * Mark fp non-blocking, to avoid problems from possible deadlocks.
    315  1.38.2.1    bouyer 	 */
    316  1.38.2.1    bouyer 
    317  1.38.2.1    bouyer 	if (fp != NULL) {
    318  1.38.2.1    bouyer 		fp->f_flag |= FNONBLOCK;
    319  1.38.2.1    bouyer 		(*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&one, curp);
    320  1.38.2.1    bouyer 	}
    321  1.38.2.1    bouyer 
    322      1.28  christos 	/*
    323      1.28  christos 	 * need something to (un)trace (XXX - why is this here?)
    324      1.28  christos 	 */
    325      1.28  christos 	if (!facs) {
    326      1.28  christos 		error = EINVAL;
    327      1.28  christos 		goto done;
    328      1.28  christos 	}
    329      1.28  christos 	/*
    330      1.28  christos 	 * do it
    331      1.28  christos 	 */
    332  1.38.2.1    bouyer 	if (pid < 0) {
    333      1.28  christos 		/*
    334      1.28  christos 		 * by process group
    335      1.28  christos 		 */
    336  1.38.2.1    bouyer 		pg = pgfind(-pid);
    337      1.28  christos 		if (pg == NULL) {
    338      1.28  christos 			error = ESRCH;
    339      1.28  christos 			goto done;
    340      1.28  christos 		}
    341  1.38.2.1    bouyer 		for (p = LIST_FIRST(&pg->pg_members); p != NULL;
    342  1.38.2.1    bouyer 		     p = LIST_NEXT(p, p_pglist)) {
    343      1.28  christos 			if (descend)
    344      1.28  christos 				ret |= ktrsetchildren(curp, p, ops, facs, fp);
    345      1.28  christos 			else
    346      1.28  christos 				ret |= ktrops(curp, p, ops, facs, fp);
    347  1.38.2.1    bouyer 		}
    348      1.28  christos 
    349      1.28  christos 	} else {
    350      1.28  christos 		/*
    351      1.28  christos 		 * by pid
    352      1.28  christos 		 */
    353  1.38.2.1    bouyer 		p = pfind(pid);
    354      1.28  christos 		if (p == NULL) {
    355      1.28  christos 			error = ESRCH;
    356      1.28  christos 			goto done;
    357      1.28  christos 		}
    358      1.28  christos 		if (descend)
    359      1.28  christos 			ret |= ktrsetchildren(curp, p, ops, facs, fp);
    360      1.28  christos 		else
    361      1.28  christos 			ret |= ktrops(curp, p, ops, facs, fp);
    362      1.28  christos 	}
    363      1.28  christos 	if (!ret)
    364      1.28  christos 		error = EPERM;
    365      1.28  christos done:
    366      1.28  christos 	curp->p_traceflag &= ~KTRFAC_ACTIVE;
    367      1.28  christos 	return (error);
    368      1.28  christos }
    369      1.28  christos 
    370      1.28  christos /*
    371      1.28  christos  * ktrace system call
    372      1.28  christos  */
    373      1.28  christos /* ARGSUSED */
    374      1.28  christos int
    375  1.38.2.1    bouyer sys_fktrace(struct proc *curp, void *v, register_t *retval)
    376  1.38.2.1    bouyer {
    377  1.38.2.1    bouyer 	struct sys_fktrace_args /* {
    378  1.38.2.1    bouyer 		syscallarg(int) fd;
    379  1.38.2.1    bouyer 		syscallarg(int) ops;
    380  1.38.2.1    bouyer 		syscallarg(int) facs;
    381  1.38.2.1    bouyer 		syscallarg(int) pid;
    382  1.38.2.1    bouyer 	} */ *uap = v;
    383  1.38.2.1    bouyer 	struct file *fp = NULL;
    384  1.38.2.1    bouyer 	struct filedesc *fdp = curp->p_fd;
    385  1.38.2.1    bouyer 
    386  1.38.2.1    bouyer 	if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
    387  1.38.2.1    bouyer 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
    388  1.38.2.1    bouyer 	    (fp->f_flag & FWRITE) == 0)
    389  1.38.2.1    bouyer 		return (EBADF);
    390  1.38.2.1    bouyer 
    391  1.38.2.1    bouyer 	return ktrace_common(curp, SCARG(uap, ops),
    392  1.38.2.1    bouyer 	    SCARG(uap, facs), SCARG(uap, pid), fp);
    393  1.38.2.1    bouyer }
    394  1.38.2.1    bouyer 
    395  1.38.2.1    bouyer /*
    396  1.38.2.1    bouyer  * ktrace system call
    397  1.38.2.1    bouyer  */
    398  1.38.2.1    bouyer /* ARGSUSED */
    399  1.38.2.1    bouyer int
    400  1.38.2.1    bouyer sys_ktrace(struct proc *curp, void *v, register_t *retval)
    401      1.19   thorpej {
    402      1.28  christos 	struct sys_ktrace_args /* {
    403      1.24   mycroft 		syscallarg(const char *) fname;
    404      1.13       cgd 		syscallarg(int) ops;
    405      1.13       cgd 		syscallarg(int) facs;
    406      1.13       cgd 		syscallarg(int) pid;
    407      1.19   thorpej 	} */ *uap = v;
    408      1.28  christos 	struct vnode *vp = NULL;
    409  1.38.2.1    bouyer 	struct file *fp = NULL;
    410  1.38.2.1    bouyer 	int fd;
    411  1.38.2.1    bouyer 	int ops = SCARG(uap, ops);
    412       1.1       cgd 	int error = 0;
    413       1.1       cgd 	struct nameidata nd;
    414       1.1       cgd 
    415  1.38.2.1    bouyer 	ops = KTROP(ops) | (ops & KTRFLAG_DESCEND);
    416  1.38.2.1    bouyer 
    417       1.9       cgd 	curp->p_traceflag |= KTRFAC_ACTIVE;
    418       1.1       cgd 	if (ops != KTROP_CLEAR) {
    419       1.1       cgd 		/*
    420       1.1       cgd 		 * an operation which requires a file argument.
    421       1.1       cgd 		 */
    422      1.13       cgd 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, fname),
    423      1.13       cgd 		    curp);
    424      1.22  christos 		if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
    425       1.9       cgd 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
    426       1.1       cgd 			return (error);
    427       1.9       cgd 		}
    428       1.1       cgd 		vp = nd.ni_vp;
    429      1.25      fvdl 		VOP_UNLOCK(vp, 0);
    430       1.1       cgd 		if (vp->v_type != VREG) {
    431       1.1       cgd 			(void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp);
    432       1.9       cgd 			curp->p_traceflag &= ~KTRFAC_ACTIVE;
    433       1.1       cgd 			return (EACCES);
    434       1.1       cgd 		}
    435       1.1       cgd 		/*
    436  1.38.2.1    bouyer 		 * XXX This uses up a file descriptor slot in the
    437  1.38.2.1    bouyer 		 * tracing process for the duration of this syscall.
    438  1.38.2.1    bouyer 		 * This is not expected to be a problem.  If
    439  1.38.2.1    bouyer 		 * falloc(NULL, ...) DTRT we could skip that part, but
    440  1.38.2.1    bouyer 		 * that would require changing its interface to allow
    441  1.38.2.1    bouyer 		 * the caller to pass in a ucred..
    442  1.38.2.1    bouyer 		 *
    443  1.38.2.1    bouyer 		 * This will FILE_USE the fp it returns, if any.
    444  1.38.2.1    bouyer 		 * Keep it in use until we return.
    445       1.1       cgd 		 */
    446  1.38.2.1    bouyer 		if ((error = falloc(curp, &fp, &fd)) != 0)
    447       1.1       cgd 			goto done;
    448  1.38.2.1    bouyer 
    449  1.38.2.1    bouyer 		fp->f_flag = FWRITE|FAPPEND;
    450  1.38.2.1    bouyer 		fp->f_type = DTYPE_VNODE;
    451  1.38.2.1    bouyer 		fp->f_ops = &vnops;
    452  1.38.2.1    bouyer 		fp->f_data = (caddr_t)vp;
    453  1.38.2.1    bouyer 		vp = NULL;
    454  1.38.2.1    bouyer 	}
    455  1.38.2.1    bouyer 	error = ktrace_common(curp, SCARG(uap, ops), SCARG(uap, facs),
    456  1.38.2.1    bouyer 	    SCARG(uap, pid), fp);
    457  1.38.2.1    bouyer done:
    458       1.1       cgd 	if (vp != NULL)
    459       1.1       cgd 		(void) vn_close(vp, FWRITE, curp->p_ucred, curp);
    460  1.38.2.1    bouyer 	if (fp != NULL) {
    461  1.38.2.1    bouyer 		FILE_UNUSE(fp, curp);	/* release file */
    462  1.38.2.1    bouyer 		fdrelease(curp, fd); 	/* release fd table slot */
    463  1.38.2.1    bouyer 	}
    464       1.1       cgd 	return (error);
    465       1.1       cgd }
    466       1.1       cgd 
    467       1.4    andrew int
    468  1.38.2.1    bouyer ktrops(struct proc *curp, struct proc *p, int ops, int facs, struct file *fp)
    469       1.1       cgd {
    470       1.1       cgd 
    471       1.1       cgd 	if (!ktrcanset(curp, p))
    472       1.1       cgd 		return (0);
    473      1.28  christos 	if (KTROP(ops) == KTROP_SET) {
    474  1.38.2.1    bouyer 		if (p->p_tracep != fp) {
    475       1.1       cgd 			/*
    476       1.1       cgd 			 * if trace file already in use, relinquish
    477       1.1       cgd 			 */
    478      1.28  christos 			ktrderef(p);
    479  1.38.2.1    bouyer 			p->p_tracep = fp;
    480      1.28  christos 			ktradref(p);
    481       1.1       cgd 		}
    482       1.1       cgd 		p->p_traceflag |= facs;
    483       1.1       cgd 		if (curp->p_ucred->cr_uid == 0)
    484       1.1       cgd 			p->p_traceflag |= KTRFAC_ROOT;
    485       1.1       cgd 	} else {
    486       1.1       cgd 		/* KTROP_CLEAR */
    487       1.1       cgd 		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
    488       1.1       cgd 			/* no more tracing */
    489      1.28  christos 			ktrderef(p);
    490       1.1       cgd 		}
    491       1.1       cgd 	}
    492      1.21  christos 
    493      1.21  christos 	/*
    494      1.21  christos 	 * Emit an emulation record, every time there is a ktrace
    495      1.21  christos 	 * change/attach request.
    496      1.21  christos 	 */
    497      1.21  christos 	if (KTRPOINT(p, KTR_EMUL))
    498  1.38.2.1    bouyer 		ktremul(p);
    499  1.38.2.2    bouyer #ifdef __HAVE_SYSCALL_INTERN
    500  1.38.2.2    bouyer 	(*p->p_emul->e_syscall_intern)(p);
    501  1.38.2.2    bouyer #endif
    502       1.1       cgd 
    503       1.1       cgd 	return (1);
    504       1.1       cgd }
    505       1.1       cgd 
    506      1.22  christos int
    507  1.38.2.1    bouyer ktrsetchildren(struct proc *curp, struct proc *top, int ops, int facs,
    508  1.38.2.1    bouyer     struct file *fp)
    509       1.1       cgd {
    510      1.28  christos 	struct proc *p;
    511      1.28  christos 	int ret = 0;
    512       1.1       cgd 
    513       1.1       cgd 	p = top;
    514       1.1       cgd 	for (;;) {
    515  1.38.2.1    bouyer 		ret |= ktrops(curp, p, ops, facs, fp);
    516       1.1       cgd 		/*
    517       1.1       cgd 		 * If this process has children, descend to them next,
    518       1.1       cgd 		 * otherwise do any siblings, and if done with this level,
    519       1.1       cgd 		 * follow back up the tree (but not past top).
    520       1.1       cgd 		 */
    521  1.38.2.1    bouyer 		if (LIST_FIRST(&p->p_children) != NULL)
    522  1.38.2.1    bouyer 			p = LIST_FIRST(&p->p_children);
    523       1.1       cgd 		else for (;;) {
    524       1.1       cgd 			if (p == top)
    525       1.1       cgd 				return (ret);
    526  1.38.2.1    bouyer 			if (LIST_NEXT(p, p_sibling) != NULL) {
    527  1.38.2.1    bouyer 				p = LIST_NEXT(p, p_sibling);
    528       1.1       cgd 				break;
    529       1.1       cgd 			}
    530      1.12   mycroft 			p = p->p_pptr;
    531       1.1       cgd 		}
    532       1.1       cgd 	}
    533       1.1       cgd 	/*NOTREACHED*/
    534       1.1       cgd }
    535       1.1       cgd 
    536  1.38.2.1    bouyer int
    537  1.38.2.1    bouyer ktrwrite(struct proc *p, struct ktr_header *kth)
    538       1.1       cgd {
    539       1.1       cgd 	struct uio auio;
    540       1.1       cgd 	struct iovec aiov[2];
    541  1.38.2.1    bouyer 	int error, tries;
    542  1.38.2.1    bouyer 	struct file *fp = p->p_tracep;
    543       1.1       cgd 
    544  1.38.2.1    bouyer 	if (fp == NULL)
    545  1.38.2.1    bouyer 		return 0;
    546  1.38.2.1    bouyer 
    547       1.1       cgd 	auio.uio_iov = &aiov[0];
    548       1.1       cgd 	auio.uio_offset = 0;
    549       1.1       cgd 	auio.uio_segflg = UIO_SYSSPACE;
    550       1.1       cgd 	auio.uio_rw = UIO_WRITE;
    551       1.1       cgd 	aiov[0].iov_base = (caddr_t)kth;
    552       1.1       cgd 	aiov[0].iov_len = sizeof(struct ktr_header);
    553       1.1       cgd 	auio.uio_resid = sizeof(struct ktr_header);
    554       1.1       cgd 	auio.uio_iovcnt = 1;
    555       1.1       cgd 	auio.uio_procp = (struct proc *)0;
    556       1.1       cgd 	if (kth->ktr_len > 0) {
    557       1.1       cgd 		auio.uio_iovcnt++;
    558       1.1       cgd 		aiov[1].iov_base = kth->ktr_buf;
    559       1.1       cgd 		aiov[1].iov_len = kth->ktr_len;
    560       1.1       cgd 		auio.uio_resid += kth->ktr_len;
    561       1.1       cgd 	}
    562      1.28  christos 
    563  1.38.2.1    bouyer 	FILE_USE(fp);
    564  1.38.2.1    bouyer 
    565  1.38.2.1    bouyer 	tries = 0;
    566  1.38.2.1    bouyer 	do {
    567      1.30   thorpej 		error = (*fp->f_ops->fo_write)(fp, &fp->f_offset, &auio,
    568      1.30   thorpej 		    fp->f_cred, FOF_UPDATE_OFFSET);
    569  1.38.2.1    bouyer 		tries++;
    570  1.38.2.1    bouyer 		if (error == EWOULDBLOCK)
    571  1.38.2.1    bouyer 		  	yield();
    572  1.38.2.1    bouyer 	} while ((error == EWOULDBLOCK) && (tries < 3));
    573  1.38.2.1    bouyer 	FILE_UNUSE(fp, NULL);
    574      1.28  christos 
    575  1.38.2.1    bouyer 	if (__predict_true(error == 0))
    576  1.38.2.1    bouyer 		return (0);
    577       1.1       cgd 	/*
    578      1.38   darrenr 	 * If error encountered, give up tracing on this vnode.  Don't report
    579      1.38   darrenr 	 * EPIPE as this can easily happen with fktrace()/ktruss.
    580       1.1       cgd 	 */
    581      1.38   darrenr 	if (error != EPIPE)
    582      1.38   darrenr 		log(LOG_NOTICE,
    583      1.38   darrenr 		    "ktrace write failed, errno %d, tracing stopped\n",
    584      1.38   darrenr 		    error);
    585      1.37   thorpej 	proclist_lock_read();
    586  1.38.2.1    bouyer 	for (p = LIST_FIRST(&allproc); p != NULL; p = LIST_NEXT(p, p_list)) {
    587  1.38.2.1    bouyer 		if (ktrsamefile(p->p_tracep, fp))
    588      1.28  christos 			ktrderef(p);
    589       1.1       cgd 	}
    590      1.36   thorpej 	proclist_unlock_read();
    591  1.38.2.1    bouyer 
    592  1.38.2.1    bouyer 	return (error);
    593       1.1       cgd }
    594       1.1       cgd 
    595       1.1       cgd /*
    596       1.1       cgd  * Return true if caller has permission to set the ktracing state
    597       1.1       cgd  * of target.  Essentially, the target can't possess any
    598       1.1       cgd  * more permissions than the caller.  KTRFAC_ROOT signifies that
    599       1.1       cgd  * root previously set the tracing status on the target process, and
    600       1.1       cgd  * so, only root may further change it.
    601       1.1       cgd  *
    602       1.1       cgd  * TODO: check groups.  use caller effective gid.
    603       1.1       cgd  */
    604      1.22  christos int
    605  1.38.2.1    bouyer ktrcanset(struct proc *callp, struct proc *targetp)
    606       1.1       cgd {
    607      1.28  christos 	struct pcred *caller = callp->p_cred;
    608      1.28  christos 	struct pcred *target = targetp->p_cred;
    609       1.1       cgd 
    610       1.1       cgd 	if ((caller->pc_ucred->cr_uid == target->p_ruid &&
    611       1.1       cgd 	     target->p_ruid == target->p_svuid &&
    612       1.1       cgd 	     caller->p_rgid == target->p_rgid &&	/* XXX */
    613       1.1       cgd 	     target->p_rgid == target->p_svgid &&
    614       1.1       cgd 	     (targetp->p_traceflag & KTRFAC_ROOT) == 0) ||
    615       1.1       cgd 	     caller->pc_ucred->cr_uid == 0)
    616       1.1       cgd 		return (1);
    617       1.1       cgd 
    618       1.1       cgd 	return (0);
    619       1.1       cgd }
    620  1.38.2.1    bouyer #endif /* KTRACE */
    621