1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1989, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 33df8bae1dSRodney W. Grimes * @(#)kern_ktrace.c 8.2 (Berkeley) 9/23/93 3487b6de2bSPoul-Henning Kamp * $Id: kern_ktrace.c,v 1.8 1995/12/02 18:58:47 bde Exp $ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37df8bae1dSRodney W. Grimes #ifdef KTRACE 38df8bae1dSRodney W. Grimes 39df8bae1dSRodney W. Grimes #include <sys/param.h> 40f23b4c91SGarrett Wollman #include <sys/systm.h> 41d2d3e875SBruce Evans #include <sys/sysproto.h> 42df8bae1dSRodney W. Grimes #include <sys/proc.h> 43df8bae1dSRodney W. Grimes #include <sys/file.h> 44df8bae1dSRodney W. Grimes #include <sys/namei.h> 45df8bae1dSRodney W. Grimes #include <sys/vnode.h> 46df8bae1dSRodney W. Grimes #include <sys/ktrace.h> 47df8bae1dSRodney W. Grimes #include <sys/malloc.h> 48df8bae1dSRodney W. Grimes #include <sys/syslog.h> 49df8bae1dSRodney W. Grimes 5087b6de2bSPoul-Henning Kamp static struct ktr_header *ktrgetheader __P((int type)); 5187b6de2bSPoul-Henning Kamp static void ktrwrite __P((struct vnode *, struct ktr_header *)); 5287b6de2bSPoul-Henning Kamp static int ktrcanset __P((struct proc *,struct proc *)); 5387b6de2bSPoul-Henning Kamp static int ktrsetchildren __P((struct proc *,struct proc *,int,int,struct vnode *)); 5487b6de2bSPoul-Henning Kamp static int ktrops __P((struct proc *,struct proc *,int,int,struct vnode *)); 5598d93822SBruce Evans 5687b6de2bSPoul-Henning Kamp 5787b6de2bSPoul-Henning Kamp static struct ktr_header * 58df8bae1dSRodney W. Grimes ktrgetheader(type) 59df8bae1dSRodney W. Grimes int type; 60df8bae1dSRodney W. Grimes { 61df8bae1dSRodney W. Grimes register struct ktr_header *kth; 62df8bae1dSRodney W. Grimes struct proc *p = curproc; /* XXX */ 63df8bae1dSRodney W. Grimes 64df8bae1dSRodney W. Grimes MALLOC(kth, struct ktr_header *, sizeof (struct ktr_header), 65df8bae1dSRodney W. Grimes M_TEMP, M_WAITOK); 66df8bae1dSRodney W. Grimes kth->ktr_type = type; 67df8bae1dSRodney W. Grimes microtime(&kth->ktr_time); 68df8bae1dSRodney W. Grimes kth->ktr_pid = p->p_pid; 69df8bae1dSRodney W. Grimes bcopy(p->p_comm, kth->ktr_comm, MAXCOMLEN); 70df8bae1dSRodney W. Grimes return (kth); 71df8bae1dSRodney W. Grimes } 72df8bae1dSRodney W. Grimes 7326f9a767SRodney W. Grimes void 74df8bae1dSRodney W. Grimes ktrsyscall(vp, code, narg, args) 75df8bae1dSRodney W. Grimes struct vnode *vp; 76df8bae1dSRodney W. Grimes int code, narg, args[]; 77df8bae1dSRodney W. Grimes { 78df8bae1dSRodney W. Grimes struct ktr_header *kth; 79df8bae1dSRodney W. Grimes struct ktr_syscall *ktp; 80df8bae1dSRodney W. Grimes register len = sizeof(struct ktr_syscall) + (narg * sizeof(int)); 81df8bae1dSRodney W. Grimes struct proc *p = curproc; /* XXX */ 82df8bae1dSRodney W. Grimes int *argp, i; 83df8bae1dSRodney W. Grimes 84df8bae1dSRodney W. Grimes p->p_traceflag |= KTRFAC_ACTIVE; 85df8bae1dSRodney W. Grimes kth = ktrgetheader(KTR_SYSCALL); 86df8bae1dSRodney W. Grimes MALLOC(ktp, struct ktr_syscall *, len, M_TEMP, M_WAITOK); 87df8bae1dSRodney W. Grimes ktp->ktr_code = code; 88df8bae1dSRodney W. Grimes ktp->ktr_narg = narg; 89df8bae1dSRodney W. Grimes argp = (int *)((char *)ktp + sizeof(struct ktr_syscall)); 90df8bae1dSRodney W. Grimes for (i = 0; i < narg; i++) 91df8bae1dSRodney W. Grimes *argp++ = args[i]; 92df8bae1dSRodney W. Grimes kth->ktr_buf = (caddr_t)ktp; 93df8bae1dSRodney W. Grimes kth->ktr_len = len; 94df8bae1dSRodney W. Grimes ktrwrite(vp, kth); 95df8bae1dSRodney W. Grimes FREE(ktp, M_TEMP); 96df8bae1dSRodney W. Grimes FREE(kth, M_TEMP); 97df8bae1dSRodney W. Grimes p->p_traceflag &= ~KTRFAC_ACTIVE; 98df8bae1dSRodney W. Grimes } 99df8bae1dSRodney W. Grimes 10026f9a767SRodney W. Grimes void 101df8bae1dSRodney W. Grimes ktrsysret(vp, code, error, retval) 102df8bae1dSRodney W. Grimes struct vnode *vp; 103df8bae1dSRodney W. Grimes int code, error, retval; 104df8bae1dSRodney W. Grimes { 105df8bae1dSRodney W. Grimes struct ktr_header *kth; 106df8bae1dSRodney W. Grimes struct ktr_sysret ktp; 107df8bae1dSRodney W. Grimes struct proc *p = curproc; /* XXX */ 108df8bae1dSRodney W. Grimes 109df8bae1dSRodney W. Grimes p->p_traceflag |= KTRFAC_ACTIVE; 110df8bae1dSRodney W. Grimes kth = ktrgetheader(KTR_SYSRET); 111df8bae1dSRodney W. Grimes ktp.ktr_code = code; 112df8bae1dSRodney W. Grimes ktp.ktr_error = error; 113df8bae1dSRodney W. Grimes ktp.ktr_retval = retval; /* what about val2 ? */ 114df8bae1dSRodney W. Grimes 115df8bae1dSRodney W. Grimes kth->ktr_buf = (caddr_t)&ktp; 116df8bae1dSRodney W. Grimes kth->ktr_len = sizeof(struct ktr_sysret); 117df8bae1dSRodney W. Grimes 118df8bae1dSRodney W. Grimes ktrwrite(vp, kth); 119df8bae1dSRodney W. Grimes FREE(kth, M_TEMP); 120df8bae1dSRodney W. Grimes p->p_traceflag &= ~KTRFAC_ACTIVE; 121df8bae1dSRodney W. Grimes } 122df8bae1dSRodney W. Grimes 12326f9a767SRodney W. Grimes void 124df8bae1dSRodney W. Grimes ktrnamei(vp, path) 125df8bae1dSRodney W. Grimes struct vnode *vp; 126df8bae1dSRodney W. Grimes char *path; 127df8bae1dSRodney W. Grimes { 128df8bae1dSRodney W. Grimes struct ktr_header *kth; 129df8bae1dSRodney W. Grimes struct proc *p = curproc; /* XXX */ 130df8bae1dSRodney W. Grimes 131df8bae1dSRodney W. Grimes p->p_traceflag |= KTRFAC_ACTIVE; 132df8bae1dSRodney W. Grimes kth = ktrgetheader(KTR_NAMEI); 133df8bae1dSRodney W. Grimes kth->ktr_len = strlen(path); 134df8bae1dSRodney W. Grimes kth->ktr_buf = path; 135df8bae1dSRodney W. Grimes 136df8bae1dSRodney W. Grimes ktrwrite(vp, kth); 137df8bae1dSRodney W. Grimes FREE(kth, M_TEMP); 138df8bae1dSRodney W. Grimes p->p_traceflag &= ~KTRFAC_ACTIVE; 139df8bae1dSRodney W. Grimes } 140df8bae1dSRodney W. Grimes 14126f9a767SRodney W. Grimes void 142df8bae1dSRodney W. Grimes ktrgenio(vp, fd, rw, iov, len, error) 143df8bae1dSRodney W. Grimes struct vnode *vp; 144df8bae1dSRodney W. Grimes int fd; 145df8bae1dSRodney W. Grimes enum uio_rw rw; 146df8bae1dSRodney W. Grimes register struct iovec *iov; 147df8bae1dSRodney W. Grimes int len, error; 148df8bae1dSRodney W. Grimes { 149df8bae1dSRodney W. Grimes struct ktr_header *kth; 150df8bae1dSRodney W. Grimes register struct ktr_genio *ktp; 151df8bae1dSRodney W. Grimes register caddr_t cp; 152df8bae1dSRodney W. Grimes register int resid = len, cnt; 153df8bae1dSRodney W. Grimes struct proc *p = curproc; /* XXX */ 154df8bae1dSRodney W. Grimes 155df8bae1dSRodney W. Grimes if (error) 156df8bae1dSRodney W. Grimes return; 157df8bae1dSRodney W. Grimes p->p_traceflag |= KTRFAC_ACTIVE; 158df8bae1dSRodney W. Grimes kth = ktrgetheader(KTR_GENIO); 159df8bae1dSRodney W. Grimes MALLOC(ktp, struct ktr_genio *, sizeof(struct ktr_genio) + len, 160df8bae1dSRodney W. Grimes M_TEMP, M_WAITOK); 161df8bae1dSRodney W. Grimes ktp->ktr_fd = fd; 162df8bae1dSRodney W. Grimes ktp->ktr_rw = rw; 163df8bae1dSRodney W. Grimes cp = (caddr_t)((char *)ktp + sizeof (struct ktr_genio)); 164df8bae1dSRodney W. Grimes while (resid > 0) { 165df8bae1dSRodney W. Grimes if ((cnt = iov->iov_len) > resid) 166df8bae1dSRodney W. Grimes cnt = resid; 167df8bae1dSRodney W. Grimes if (copyin(iov->iov_base, cp, (unsigned)cnt)) 168df8bae1dSRodney W. Grimes goto done; 169df8bae1dSRodney W. Grimes cp += cnt; 170df8bae1dSRodney W. Grimes resid -= cnt; 171df8bae1dSRodney W. Grimes iov++; 172df8bae1dSRodney W. Grimes } 173df8bae1dSRodney W. Grimes kth->ktr_buf = (caddr_t)ktp; 174df8bae1dSRodney W. Grimes kth->ktr_len = sizeof (struct ktr_genio) + len; 175df8bae1dSRodney W. Grimes 176df8bae1dSRodney W. Grimes ktrwrite(vp, kth); 177df8bae1dSRodney W. Grimes done: 178df8bae1dSRodney W. Grimes FREE(kth, M_TEMP); 179df8bae1dSRodney W. Grimes FREE(ktp, M_TEMP); 180df8bae1dSRodney W. Grimes p->p_traceflag &= ~KTRFAC_ACTIVE; 181df8bae1dSRodney W. Grimes } 182df8bae1dSRodney W. Grimes 18326f9a767SRodney W. Grimes void 184df8bae1dSRodney W. Grimes ktrpsig(vp, sig, action, mask, code) 185df8bae1dSRodney W. Grimes struct vnode *vp; 186df8bae1dSRodney W. Grimes int sig; 187df8bae1dSRodney W. Grimes sig_t action; 188df8bae1dSRodney W. Grimes int mask, code; 189df8bae1dSRodney W. Grimes { 190df8bae1dSRodney W. Grimes struct ktr_header *kth; 191df8bae1dSRodney W. Grimes struct ktr_psig kp; 192df8bae1dSRodney W. Grimes struct proc *p = curproc; /* XXX */ 193df8bae1dSRodney W. Grimes 194df8bae1dSRodney W. Grimes p->p_traceflag |= KTRFAC_ACTIVE; 195df8bae1dSRodney W. Grimes kth = ktrgetheader(KTR_PSIG); 196df8bae1dSRodney W. Grimes kp.signo = (char)sig; 197df8bae1dSRodney W. Grimes kp.action = action; 198df8bae1dSRodney W. Grimes kp.mask = mask; 199df8bae1dSRodney W. Grimes kp.code = code; 200df8bae1dSRodney W. Grimes kth->ktr_buf = (caddr_t)&kp; 201df8bae1dSRodney W. Grimes kth->ktr_len = sizeof (struct ktr_psig); 202df8bae1dSRodney W. Grimes 203df8bae1dSRodney W. Grimes ktrwrite(vp, kth); 204df8bae1dSRodney W. Grimes FREE(kth, M_TEMP); 205df8bae1dSRodney W. Grimes p->p_traceflag &= ~KTRFAC_ACTIVE; 206df8bae1dSRodney W. Grimes } 207df8bae1dSRodney W. Grimes 20826f9a767SRodney W. Grimes void 209df8bae1dSRodney W. Grimes ktrcsw(vp, out, user) 210df8bae1dSRodney W. Grimes struct vnode *vp; 211df8bae1dSRodney W. Grimes int out, user; 212df8bae1dSRodney W. Grimes { 213df8bae1dSRodney W. Grimes struct ktr_header *kth; 214df8bae1dSRodney W. Grimes struct ktr_csw kc; 215df8bae1dSRodney W. Grimes struct proc *p = curproc; /* XXX */ 216df8bae1dSRodney W. Grimes 217df8bae1dSRodney W. Grimes p->p_traceflag |= KTRFAC_ACTIVE; 218df8bae1dSRodney W. Grimes kth = ktrgetheader(KTR_CSW); 219df8bae1dSRodney W. Grimes kc.out = out; 220df8bae1dSRodney W. Grimes kc.user = user; 221df8bae1dSRodney W. Grimes kth->ktr_buf = (caddr_t)&kc; 222df8bae1dSRodney W. Grimes kth->ktr_len = sizeof (struct ktr_csw); 223df8bae1dSRodney W. Grimes 224df8bae1dSRodney W. Grimes ktrwrite(vp, kth); 225df8bae1dSRodney W. Grimes FREE(kth, M_TEMP); 226df8bae1dSRodney W. Grimes p->p_traceflag &= ~KTRFAC_ACTIVE; 227df8bae1dSRodney W. Grimes } 228df8bae1dSRodney W. Grimes 229df8bae1dSRodney W. Grimes /* Interface and common routines */ 230df8bae1dSRodney W. Grimes 231df8bae1dSRodney W. Grimes /* 232df8bae1dSRodney W. Grimes * ktrace system call 233df8bae1dSRodney W. Grimes */ 234d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 235df8bae1dSRodney W. Grimes struct ktrace_args { 236df8bae1dSRodney W. Grimes char *fname; 237df8bae1dSRodney W. Grimes int ops; 238df8bae1dSRodney W. Grimes int facs; 239df8bae1dSRodney W. Grimes int pid; 240df8bae1dSRodney W. Grimes }; 241d2d3e875SBruce Evans #endif 242df8bae1dSRodney W. Grimes /* ARGSUSED */ 24326f9a767SRodney W. Grimes int 244df8bae1dSRodney W. Grimes ktrace(curp, uap, retval) 245df8bae1dSRodney W. Grimes struct proc *curp; 246df8bae1dSRodney W. Grimes register struct ktrace_args *uap; 247df8bae1dSRodney W. Grimes int *retval; 248df8bae1dSRodney W. Grimes { 249df8bae1dSRodney W. Grimes register struct vnode *vp = NULL; 250df8bae1dSRodney W. Grimes register struct proc *p; 251df8bae1dSRodney W. Grimes struct pgrp *pg; 252df8bae1dSRodney W. Grimes int facs = uap->facs & ~KTRFAC_ROOT; 253df8bae1dSRodney W. Grimes int ops = KTROP(uap->ops); 254df8bae1dSRodney W. Grimes int descend = uap->ops & KTRFLAG_DESCEND; 255df8bae1dSRodney W. Grimes int ret = 0; 256df8bae1dSRodney W. Grimes int error = 0; 257df8bae1dSRodney W. Grimes struct nameidata nd; 258df8bae1dSRodney W. Grimes 259df8bae1dSRodney W. Grimes curp->p_traceflag |= KTRFAC_ACTIVE; 260df8bae1dSRodney W. Grimes if (ops != KTROP_CLEAR) { 261df8bae1dSRodney W. Grimes /* 262df8bae1dSRodney W. Grimes * an operation which requires a file argument. 263df8bae1dSRodney W. Grimes */ 264df8bae1dSRodney W. Grimes NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->fname, curp); 265797f2d22SPoul-Henning Kamp error = vn_open(&nd, FREAD|FWRITE, 0); 266797f2d22SPoul-Henning Kamp if (error) { 267df8bae1dSRodney W. Grimes curp->p_traceflag &= ~KTRFAC_ACTIVE; 268df8bae1dSRodney W. Grimes return (error); 269df8bae1dSRodney W. Grimes } 270df8bae1dSRodney W. Grimes vp = nd.ni_vp; 271df8bae1dSRodney W. Grimes VOP_UNLOCK(vp); 272df8bae1dSRodney W. Grimes if (vp->v_type != VREG) { 273df8bae1dSRodney W. Grimes (void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp); 274df8bae1dSRodney W. Grimes curp->p_traceflag &= ~KTRFAC_ACTIVE; 275df8bae1dSRodney W. Grimes return (EACCES); 276df8bae1dSRodney W. Grimes } 277df8bae1dSRodney W. Grimes } 278df8bae1dSRodney W. Grimes /* 279df8bae1dSRodney W. Grimes * Clear all uses of the tracefile 280df8bae1dSRodney W. Grimes */ 281df8bae1dSRodney W. Grimes if (ops == KTROP_CLEARFILE) { 282df8bae1dSRodney W. Grimes for (p = (struct proc *)allproc; p != NULL; p = p->p_next) { 283df8bae1dSRodney W. Grimes if (p->p_tracep == vp) { 284df8bae1dSRodney W. Grimes if (ktrcanset(curp, p)) { 285df8bae1dSRodney W. Grimes p->p_tracep = NULL; 286df8bae1dSRodney W. Grimes p->p_traceflag = 0; 287df8bae1dSRodney W. Grimes (void) vn_close(vp, FREAD|FWRITE, 288df8bae1dSRodney W. Grimes p->p_ucred, p); 289df8bae1dSRodney W. Grimes } else 290df8bae1dSRodney W. Grimes error = EPERM; 291df8bae1dSRodney W. Grimes } 292df8bae1dSRodney W. Grimes } 293df8bae1dSRodney W. Grimes goto done; 294df8bae1dSRodney W. Grimes } 295df8bae1dSRodney W. Grimes /* 296df8bae1dSRodney W. Grimes * need something to (un)trace (XXX - why is this here?) 297df8bae1dSRodney W. Grimes */ 298df8bae1dSRodney W. Grimes if (!facs) { 299df8bae1dSRodney W. Grimes error = EINVAL; 300df8bae1dSRodney W. Grimes goto done; 301df8bae1dSRodney W. Grimes } 302df8bae1dSRodney W. Grimes /* 303df8bae1dSRodney W. Grimes * do it 304df8bae1dSRodney W. Grimes */ 305df8bae1dSRodney W. Grimes if (uap->pid < 0) { 306df8bae1dSRodney W. Grimes /* 307df8bae1dSRodney W. Grimes * by process group 308df8bae1dSRodney W. Grimes */ 309df8bae1dSRodney W. Grimes pg = pgfind(-uap->pid); 310df8bae1dSRodney W. Grimes if (pg == NULL) { 311df8bae1dSRodney W. Grimes error = ESRCH; 312df8bae1dSRodney W. Grimes goto done; 313df8bae1dSRodney W. Grimes } 314df8bae1dSRodney W. Grimes for (p = pg->pg_mem; p != NULL; p = p->p_pgrpnxt) 315df8bae1dSRodney W. Grimes if (descend) 316df8bae1dSRodney W. Grimes ret |= ktrsetchildren(curp, p, ops, facs, vp); 317df8bae1dSRodney W. Grimes else 318df8bae1dSRodney W. Grimes ret |= ktrops(curp, p, ops, facs, vp); 319df8bae1dSRodney W. Grimes 320df8bae1dSRodney W. Grimes } else { 321df8bae1dSRodney W. Grimes /* 322df8bae1dSRodney W. Grimes * by pid 323df8bae1dSRodney W. Grimes */ 324df8bae1dSRodney W. Grimes p = pfind(uap->pid); 325df8bae1dSRodney W. Grimes if (p == NULL) { 326df8bae1dSRodney W. Grimes error = ESRCH; 327df8bae1dSRodney W. Grimes goto done; 328df8bae1dSRodney W. Grimes } 329df8bae1dSRodney W. Grimes if (descend) 330df8bae1dSRodney W. Grimes ret |= ktrsetchildren(curp, p, ops, facs, vp); 331df8bae1dSRodney W. Grimes else 332df8bae1dSRodney W. Grimes ret |= ktrops(curp, p, ops, facs, vp); 333df8bae1dSRodney W. Grimes } 334df8bae1dSRodney W. Grimes if (!ret) 335df8bae1dSRodney W. Grimes error = EPERM; 336df8bae1dSRodney W. Grimes done: 337df8bae1dSRodney W. Grimes if (vp != NULL) 338df8bae1dSRodney W. Grimes (void) vn_close(vp, FWRITE, curp->p_ucred, curp); 339df8bae1dSRodney W. Grimes curp->p_traceflag &= ~KTRFAC_ACTIVE; 340df8bae1dSRodney W. Grimes return (error); 341df8bae1dSRodney W. Grimes } 342df8bae1dSRodney W. Grimes 34387b6de2bSPoul-Henning Kamp static int 344df8bae1dSRodney W. Grimes ktrops(curp, p, ops, facs, vp) 345df8bae1dSRodney W. Grimes struct proc *p, *curp; 346df8bae1dSRodney W. Grimes int ops, facs; 347df8bae1dSRodney W. Grimes struct vnode *vp; 348df8bae1dSRodney W. Grimes { 349df8bae1dSRodney W. Grimes 350df8bae1dSRodney W. Grimes if (!ktrcanset(curp, p)) 351df8bae1dSRodney W. Grimes return (0); 352df8bae1dSRodney W. Grimes if (ops == KTROP_SET) { 353df8bae1dSRodney W. Grimes if (p->p_tracep != vp) { 354df8bae1dSRodney W. Grimes /* 355df8bae1dSRodney W. Grimes * if trace file already in use, relinquish 356df8bae1dSRodney W. Grimes */ 357df8bae1dSRodney W. Grimes if (p->p_tracep != NULL) 358df8bae1dSRodney W. Grimes vrele(p->p_tracep); 359df8bae1dSRodney W. Grimes VREF(vp); 360df8bae1dSRodney W. Grimes p->p_tracep = vp; 361df8bae1dSRodney W. Grimes } 362df8bae1dSRodney W. Grimes p->p_traceflag |= facs; 363df8bae1dSRodney W. Grimes if (curp->p_ucred->cr_uid == 0) 364df8bae1dSRodney W. Grimes p->p_traceflag |= KTRFAC_ROOT; 365df8bae1dSRodney W. Grimes } else { 366df8bae1dSRodney W. Grimes /* KTROP_CLEAR */ 367df8bae1dSRodney W. Grimes if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) { 368df8bae1dSRodney W. Grimes /* no more tracing */ 369df8bae1dSRodney W. Grimes p->p_traceflag = 0; 370df8bae1dSRodney W. Grimes if (p->p_tracep != NULL) { 371df8bae1dSRodney W. Grimes vrele(p->p_tracep); 372df8bae1dSRodney W. Grimes p->p_tracep = NULL; 373df8bae1dSRodney W. Grimes } 374df8bae1dSRodney W. Grimes } 375df8bae1dSRodney W. Grimes } 376df8bae1dSRodney W. Grimes 377df8bae1dSRodney W. Grimes return (1); 378df8bae1dSRodney W. Grimes } 379df8bae1dSRodney W. Grimes 38087b6de2bSPoul-Henning Kamp static int 381df8bae1dSRodney W. Grimes ktrsetchildren(curp, top, ops, facs, vp) 382df8bae1dSRodney W. Grimes struct proc *curp, *top; 383df8bae1dSRodney W. Grimes int ops, facs; 384df8bae1dSRodney W. Grimes struct vnode *vp; 385df8bae1dSRodney W. Grimes { 386df8bae1dSRodney W. Grimes register struct proc *p; 387df8bae1dSRodney W. Grimes register int ret = 0; 388df8bae1dSRodney W. Grimes 389df8bae1dSRodney W. Grimes p = top; 390df8bae1dSRodney W. Grimes for (;;) { 391df8bae1dSRodney W. Grimes ret |= ktrops(curp, p, ops, facs, vp); 392df8bae1dSRodney W. Grimes /* 393df8bae1dSRodney W. Grimes * If this process has children, descend to them next, 394df8bae1dSRodney W. Grimes * otherwise do any siblings, and if done with this level, 395df8bae1dSRodney W. Grimes * follow back up the tree (but not past top). 396df8bae1dSRodney W. Grimes */ 397df8bae1dSRodney W. Grimes if (p->p_cptr) 398df8bae1dSRodney W. Grimes p = p->p_cptr; 399df8bae1dSRodney W. Grimes else if (p == top) 400df8bae1dSRodney W. Grimes return (ret); 401df8bae1dSRodney W. Grimes else if (p->p_osptr) 402df8bae1dSRodney W. Grimes p = p->p_osptr; 403df8bae1dSRodney W. Grimes else for (;;) { 404df8bae1dSRodney W. Grimes p = p->p_pptr; 405df8bae1dSRodney W. Grimes if (p == top) 406df8bae1dSRodney W. Grimes return (ret); 407df8bae1dSRodney W. Grimes if (p->p_osptr) { 408df8bae1dSRodney W. Grimes p = p->p_osptr; 409df8bae1dSRodney W. Grimes break; 410df8bae1dSRodney W. Grimes } 411df8bae1dSRodney W. Grimes } 412df8bae1dSRodney W. Grimes } 413df8bae1dSRodney W. Grimes /*NOTREACHED*/ 414df8bae1dSRodney W. Grimes } 415df8bae1dSRodney W. Grimes 41687b6de2bSPoul-Henning Kamp static void 417df8bae1dSRodney W. Grimes ktrwrite(vp, kth) 418df8bae1dSRodney W. Grimes struct vnode *vp; 419df8bae1dSRodney W. Grimes register struct ktr_header *kth; 420df8bae1dSRodney W. Grimes { 421df8bae1dSRodney W. Grimes struct uio auio; 422df8bae1dSRodney W. Grimes struct iovec aiov[2]; 423df8bae1dSRodney W. Grimes register struct proc *p = curproc; /* XXX */ 424df8bae1dSRodney W. Grimes int error; 425df8bae1dSRodney W. Grimes 426df8bae1dSRodney W. Grimes if (vp == NULL) 427df8bae1dSRodney W. Grimes return; 428df8bae1dSRodney W. Grimes auio.uio_iov = &aiov[0]; 429df8bae1dSRodney W. Grimes auio.uio_offset = 0; 430df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_SYSSPACE; 431df8bae1dSRodney W. Grimes auio.uio_rw = UIO_WRITE; 432df8bae1dSRodney W. Grimes aiov[0].iov_base = (caddr_t)kth; 433df8bae1dSRodney W. Grimes aiov[0].iov_len = sizeof(struct ktr_header); 434df8bae1dSRodney W. Grimes auio.uio_resid = sizeof(struct ktr_header); 435df8bae1dSRodney W. Grimes auio.uio_iovcnt = 1; 436df8bae1dSRodney W. Grimes auio.uio_procp = (struct proc *)0; 437df8bae1dSRodney W. Grimes if (kth->ktr_len > 0) { 438df8bae1dSRodney W. Grimes auio.uio_iovcnt++; 439df8bae1dSRodney W. Grimes aiov[1].iov_base = kth->ktr_buf; 440df8bae1dSRodney W. Grimes aiov[1].iov_len = kth->ktr_len; 441df8bae1dSRodney W. Grimes auio.uio_resid += kth->ktr_len; 442df8bae1dSRodney W. Grimes } 443df8bae1dSRodney W. Grimes VOP_LOCK(vp); 444df8bae1dSRodney W. Grimes error = VOP_WRITE(vp, &auio, IO_UNIT|IO_APPEND, p->p_ucred); 445df8bae1dSRodney W. Grimes VOP_UNLOCK(vp); 446df8bae1dSRodney W. Grimes if (!error) 447df8bae1dSRodney W. Grimes return; 448df8bae1dSRodney W. Grimes /* 449df8bae1dSRodney W. Grimes * If error encountered, give up tracing on this vnode. 450df8bae1dSRodney W. Grimes */ 451df8bae1dSRodney W. Grimes log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n", 452df8bae1dSRodney W. Grimes error); 453df8bae1dSRodney W. Grimes for (p = (struct proc *)allproc; p != NULL; p = p->p_next) { 454df8bae1dSRodney W. Grimes if (p->p_tracep == vp) { 455df8bae1dSRodney W. Grimes p->p_tracep = NULL; 456df8bae1dSRodney W. Grimes p->p_traceflag = 0; 457df8bae1dSRodney W. Grimes vrele(vp); 458df8bae1dSRodney W. Grimes } 459df8bae1dSRodney W. Grimes } 460df8bae1dSRodney W. Grimes } 461df8bae1dSRodney W. Grimes 462df8bae1dSRodney W. Grimes /* 463df8bae1dSRodney W. Grimes * Return true if caller has permission to set the ktracing state 464df8bae1dSRodney W. Grimes * of target. Essentially, the target can't possess any 465df8bae1dSRodney W. Grimes * more permissions than the caller. KTRFAC_ROOT signifies that 466df8bae1dSRodney W. Grimes * root previously set the tracing status on the target process, and 467df8bae1dSRodney W. Grimes * so, only root may further change it. 468df8bae1dSRodney W. Grimes * 469df8bae1dSRodney W. Grimes * TODO: check groups. use caller effective gid. 470df8bae1dSRodney W. Grimes */ 47187b6de2bSPoul-Henning Kamp static int 472df8bae1dSRodney W. Grimes ktrcanset(callp, targetp) 473df8bae1dSRodney W. Grimes struct proc *callp, *targetp; 474df8bae1dSRodney W. Grimes { 475df8bae1dSRodney W. Grimes register struct pcred *caller = callp->p_cred; 476df8bae1dSRodney W. Grimes register struct pcred *target = targetp->p_cred; 477df8bae1dSRodney W. Grimes 478df8bae1dSRodney W. Grimes if ((caller->pc_ucred->cr_uid == target->p_ruid && 479df8bae1dSRodney W. Grimes target->p_ruid == target->p_svuid && 480df8bae1dSRodney W. Grimes caller->p_rgid == target->p_rgid && /* XXX */ 481df8bae1dSRodney W. Grimes target->p_rgid == target->p_svgid && 482df8bae1dSRodney W. Grimes (targetp->p_traceflag & KTRFAC_ROOT) == 0) || 483df8bae1dSRodney W. Grimes caller->pc_ucred->cr_uid == 0) 484df8bae1dSRodney W. Grimes return (1); 485df8bae1dSRodney W. Grimes 486df8bae1dSRodney W. Grimes return (0); 487df8bae1dSRodney W. Grimes } 488df8bae1dSRodney W. Grimes 489df8bae1dSRodney W. Grimes #endif 490