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