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 34e6c4b9baSPoul-Henning Kamp * $Id: kern_ktrace.c,v 1.12 1996/08/04 20:13:07 phk Exp $ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37db6a20e2SGarrett Wollman #include "opt_ktrace.h" 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 50db6a20e2SGarrett Wollman #ifdef KTRACE 5187b6de2bSPoul-Henning Kamp static struct ktr_header *ktrgetheader __P((int type)); 5287b6de2bSPoul-Henning Kamp static void ktrwrite __P((struct vnode *, struct ktr_header *)); 5387b6de2bSPoul-Henning Kamp static int ktrcanset __P((struct proc *,struct proc *)); 5487b6de2bSPoul-Henning Kamp static int ktrsetchildren __P((struct proc *,struct proc *,int,int,struct vnode *)); 5587b6de2bSPoul-Henning Kamp static int ktrops __P((struct proc *,struct proc *,int,int,struct vnode *)); 5698d93822SBruce Evans 5787b6de2bSPoul-Henning Kamp 5887b6de2bSPoul-Henning Kamp static struct ktr_header * 59df8bae1dSRodney W. Grimes ktrgetheader(type) 60df8bae1dSRodney W. Grimes int type; 61df8bae1dSRodney W. Grimes { 62df8bae1dSRodney W. Grimes register struct ktr_header *kth; 63df8bae1dSRodney W. Grimes struct proc *p = curproc; /* XXX */ 64df8bae1dSRodney W. Grimes 65df8bae1dSRodney W. Grimes MALLOC(kth, struct ktr_header *, sizeof (struct ktr_header), 66d1c4c866SPoul-Henning Kamp M_KTRACE, M_WAITOK); 67df8bae1dSRodney W. Grimes kth->ktr_type = type; 68df8bae1dSRodney W. Grimes microtime(&kth->ktr_time); 69df8bae1dSRodney W. Grimes kth->ktr_pid = p->p_pid; 70df8bae1dSRodney W. Grimes bcopy(p->p_comm, kth->ktr_comm, MAXCOMLEN); 71df8bae1dSRodney W. Grimes return (kth); 72df8bae1dSRodney W. Grimes } 73df8bae1dSRodney W. Grimes 7426f9a767SRodney W. Grimes void 75df8bae1dSRodney W. Grimes ktrsyscall(vp, code, narg, args) 76df8bae1dSRodney W. Grimes struct vnode *vp; 77df8bae1dSRodney W. Grimes int code, narg, args[]; 78df8bae1dSRodney W. Grimes { 79df8bae1dSRodney W. Grimes struct ktr_header *kth; 80df8bae1dSRodney W. Grimes struct ktr_syscall *ktp; 81df8bae1dSRodney W. Grimes register len = sizeof(struct ktr_syscall) + (narg * sizeof(int)); 82df8bae1dSRodney W. Grimes struct proc *p = curproc; /* XXX */ 83df8bae1dSRodney W. Grimes int *argp, i; 84df8bae1dSRodney W. Grimes 85df8bae1dSRodney W. Grimes p->p_traceflag |= KTRFAC_ACTIVE; 86df8bae1dSRodney W. Grimes kth = ktrgetheader(KTR_SYSCALL); 87d1c4c866SPoul-Henning Kamp MALLOC(ktp, struct ktr_syscall *, len, M_KTRACE, M_WAITOK); 88df8bae1dSRodney W. Grimes ktp->ktr_code = code; 89df8bae1dSRodney W. Grimes ktp->ktr_narg = narg; 90df8bae1dSRodney W. Grimes argp = (int *)((char *)ktp + sizeof(struct ktr_syscall)); 91df8bae1dSRodney W. Grimes for (i = 0; i < narg; i++) 92df8bae1dSRodney W. Grimes *argp++ = args[i]; 93df8bae1dSRodney W. Grimes kth->ktr_buf = (caddr_t)ktp; 94df8bae1dSRodney W. Grimes kth->ktr_len = len; 95df8bae1dSRodney W. Grimes ktrwrite(vp, kth); 96d1c4c866SPoul-Henning Kamp FREE(ktp, M_KTRACE); 97d1c4c866SPoul-Henning Kamp FREE(kth, M_KTRACE); 98df8bae1dSRodney W. Grimes p->p_traceflag &= ~KTRFAC_ACTIVE; 99df8bae1dSRodney W. Grimes } 100df8bae1dSRodney W. Grimes 10126f9a767SRodney W. Grimes void 102df8bae1dSRodney W. Grimes ktrsysret(vp, code, error, retval) 103df8bae1dSRodney W. Grimes struct vnode *vp; 104df8bae1dSRodney W. Grimes int code, error, retval; 105df8bae1dSRodney W. Grimes { 106df8bae1dSRodney W. Grimes struct ktr_header *kth; 107df8bae1dSRodney W. Grimes struct ktr_sysret ktp; 108df8bae1dSRodney W. Grimes struct proc *p = curproc; /* XXX */ 109df8bae1dSRodney W. Grimes 110df8bae1dSRodney W. Grimes p->p_traceflag |= KTRFAC_ACTIVE; 111df8bae1dSRodney W. Grimes kth = ktrgetheader(KTR_SYSRET); 112df8bae1dSRodney W. Grimes ktp.ktr_code = code; 113df8bae1dSRodney W. Grimes ktp.ktr_error = error; 114df8bae1dSRodney W. Grimes ktp.ktr_retval = retval; /* what about val2 ? */ 115df8bae1dSRodney W. Grimes 116df8bae1dSRodney W. Grimes kth->ktr_buf = (caddr_t)&ktp; 117df8bae1dSRodney W. Grimes kth->ktr_len = sizeof(struct ktr_sysret); 118df8bae1dSRodney W. Grimes 119df8bae1dSRodney W. Grimes ktrwrite(vp, kth); 120d1c4c866SPoul-Henning Kamp FREE(kth, M_KTRACE); 121df8bae1dSRodney W. Grimes p->p_traceflag &= ~KTRFAC_ACTIVE; 122df8bae1dSRodney W. Grimes } 123df8bae1dSRodney W. Grimes 12426f9a767SRodney W. Grimes void 125df8bae1dSRodney W. Grimes ktrnamei(vp, path) 126df8bae1dSRodney W. Grimes struct vnode *vp; 127df8bae1dSRodney W. Grimes char *path; 128df8bae1dSRodney W. Grimes { 129df8bae1dSRodney W. Grimes struct ktr_header *kth; 130df8bae1dSRodney W. Grimes struct proc *p = curproc; /* XXX */ 131df8bae1dSRodney W. Grimes 132df8bae1dSRodney W. Grimes p->p_traceflag |= KTRFAC_ACTIVE; 133df8bae1dSRodney W. Grimes kth = ktrgetheader(KTR_NAMEI); 134df8bae1dSRodney W. Grimes kth->ktr_len = strlen(path); 135df8bae1dSRodney W. Grimes kth->ktr_buf = path; 136df8bae1dSRodney W. Grimes 137df8bae1dSRodney W. Grimes ktrwrite(vp, kth); 138d1c4c866SPoul-Henning Kamp FREE(kth, M_KTRACE); 139df8bae1dSRodney W. Grimes p->p_traceflag &= ~KTRFAC_ACTIVE; 140df8bae1dSRodney W. Grimes } 141df8bae1dSRodney W. Grimes 14226f9a767SRodney W. Grimes void 143df8bae1dSRodney W. Grimes ktrgenio(vp, fd, rw, iov, len, error) 144df8bae1dSRodney W. Grimes struct vnode *vp; 145df8bae1dSRodney W. Grimes int fd; 146df8bae1dSRodney W. Grimes enum uio_rw rw; 147df8bae1dSRodney W. Grimes register struct iovec *iov; 148df8bae1dSRodney W. Grimes int len, error; 149df8bae1dSRodney W. Grimes { 150df8bae1dSRodney W. Grimes struct ktr_header *kth; 151df8bae1dSRodney W. Grimes register struct ktr_genio *ktp; 152df8bae1dSRodney W. Grimes register caddr_t cp; 153df8bae1dSRodney W. Grimes register int resid = len, cnt; 154df8bae1dSRodney W. Grimes struct proc *p = curproc; /* XXX */ 155df8bae1dSRodney W. Grimes 156df8bae1dSRodney W. Grimes if (error) 157df8bae1dSRodney W. Grimes return; 158df8bae1dSRodney W. Grimes p->p_traceflag |= KTRFAC_ACTIVE; 159df8bae1dSRodney W. Grimes kth = ktrgetheader(KTR_GENIO); 160df8bae1dSRodney W. Grimes MALLOC(ktp, struct ktr_genio *, sizeof(struct ktr_genio) + len, 161d1c4c866SPoul-Henning Kamp M_KTRACE, M_WAITOK); 162df8bae1dSRodney W. Grimes ktp->ktr_fd = fd; 163df8bae1dSRodney W. Grimes ktp->ktr_rw = rw; 164df8bae1dSRodney W. Grimes cp = (caddr_t)((char *)ktp + sizeof (struct ktr_genio)); 165df8bae1dSRodney W. Grimes while (resid > 0) { 166df8bae1dSRodney W. Grimes if ((cnt = iov->iov_len) > resid) 167df8bae1dSRodney W. Grimes cnt = resid; 168df8bae1dSRodney W. Grimes if (copyin(iov->iov_base, cp, (unsigned)cnt)) 169df8bae1dSRodney W. Grimes goto done; 170df8bae1dSRodney W. Grimes cp += cnt; 171df8bae1dSRodney W. Grimes resid -= cnt; 172df8bae1dSRodney W. Grimes iov++; 173df8bae1dSRodney W. Grimes } 174df8bae1dSRodney W. Grimes kth->ktr_buf = (caddr_t)ktp; 175df8bae1dSRodney W. Grimes kth->ktr_len = sizeof (struct ktr_genio) + len; 176df8bae1dSRodney W. Grimes 177df8bae1dSRodney W. Grimes ktrwrite(vp, kth); 178df8bae1dSRodney W. Grimes done: 179d1c4c866SPoul-Henning Kamp FREE(kth, M_KTRACE); 180d1c4c866SPoul-Henning Kamp FREE(ktp, M_KTRACE); 181df8bae1dSRodney W. Grimes p->p_traceflag &= ~KTRFAC_ACTIVE; 182df8bae1dSRodney W. Grimes } 183df8bae1dSRodney W. Grimes 18426f9a767SRodney W. Grimes void 185df8bae1dSRodney W. Grimes ktrpsig(vp, sig, action, mask, code) 186df8bae1dSRodney W. Grimes struct vnode *vp; 187df8bae1dSRodney W. Grimes int sig; 188df8bae1dSRodney W. Grimes sig_t action; 189df8bae1dSRodney W. Grimes int mask, code; 190df8bae1dSRodney W. Grimes { 191df8bae1dSRodney W. Grimes struct ktr_header *kth; 192df8bae1dSRodney W. Grimes struct ktr_psig kp; 193df8bae1dSRodney W. Grimes struct proc *p = curproc; /* XXX */ 194df8bae1dSRodney W. Grimes 195df8bae1dSRodney W. Grimes p->p_traceflag |= KTRFAC_ACTIVE; 196df8bae1dSRodney W. Grimes kth = ktrgetheader(KTR_PSIG); 197df8bae1dSRodney W. Grimes kp.signo = (char)sig; 198df8bae1dSRodney W. Grimes kp.action = action; 199df8bae1dSRodney W. Grimes kp.mask = mask; 200df8bae1dSRodney W. Grimes kp.code = code; 201df8bae1dSRodney W. Grimes kth->ktr_buf = (caddr_t)&kp; 202df8bae1dSRodney W. Grimes kth->ktr_len = sizeof (struct ktr_psig); 203df8bae1dSRodney W. Grimes 204df8bae1dSRodney W. Grimes ktrwrite(vp, kth); 205d1c4c866SPoul-Henning Kamp FREE(kth, M_KTRACE); 206df8bae1dSRodney W. Grimes p->p_traceflag &= ~KTRFAC_ACTIVE; 207df8bae1dSRodney W. Grimes } 208df8bae1dSRodney W. Grimes 20926f9a767SRodney W. Grimes void 210df8bae1dSRodney W. Grimes ktrcsw(vp, out, user) 211df8bae1dSRodney W. Grimes struct vnode *vp; 212df8bae1dSRodney W. Grimes int out, user; 213df8bae1dSRodney W. Grimes { 214df8bae1dSRodney W. Grimes struct ktr_header *kth; 215df8bae1dSRodney W. Grimes struct ktr_csw kc; 216df8bae1dSRodney W. Grimes struct proc *p = curproc; /* XXX */ 217df8bae1dSRodney W. Grimes 218df8bae1dSRodney W. Grimes p->p_traceflag |= KTRFAC_ACTIVE; 219df8bae1dSRodney W. Grimes kth = ktrgetheader(KTR_CSW); 220df8bae1dSRodney W. Grimes kc.out = out; 221df8bae1dSRodney W. Grimes kc.user = user; 222df8bae1dSRodney W. Grimes kth->ktr_buf = (caddr_t)&kc; 223df8bae1dSRodney W. Grimes kth->ktr_len = sizeof (struct ktr_csw); 224df8bae1dSRodney W. Grimes 225df8bae1dSRodney W. Grimes ktrwrite(vp, kth); 226d1c4c866SPoul-Henning Kamp FREE(kth, M_KTRACE); 227df8bae1dSRodney W. Grimes p->p_traceflag &= ~KTRFAC_ACTIVE; 228df8bae1dSRodney W. Grimes } 229db6a20e2SGarrett Wollman #endif 230df8bae1dSRodney W. Grimes 231df8bae1dSRodney W. Grimes /* Interface and common routines */ 232df8bae1dSRodney W. Grimes 233df8bae1dSRodney W. Grimes /* 234df8bae1dSRodney W. Grimes * ktrace system call 235df8bae1dSRodney W. Grimes */ 236d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 237df8bae1dSRodney W. Grimes struct ktrace_args { 238df8bae1dSRodney W. Grimes char *fname; 239df8bae1dSRodney W. Grimes int ops; 240df8bae1dSRodney W. Grimes int facs; 241df8bae1dSRodney W. Grimes int pid; 242df8bae1dSRodney W. Grimes }; 243d2d3e875SBruce Evans #endif 244df8bae1dSRodney W. Grimes /* ARGSUSED */ 24526f9a767SRodney W. Grimes int 246df8bae1dSRodney W. Grimes ktrace(curp, uap, retval) 247df8bae1dSRodney W. Grimes struct proc *curp; 248df8bae1dSRodney W. Grimes register struct ktrace_args *uap; 249df8bae1dSRodney W. Grimes int *retval; 250df8bae1dSRodney W. Grimes { 251db6a20e2SGarrett Wollman #ifdef KTRACE 252df8bae1dSRodney W. Grimes register struct vnode *vp = NULL; 253df8bae1dSRodney W. Grimes register struct proc *p; 254df8bae1dSRodney W. Grimes struct pgrp *pg; 255df8bae1dSRodney W. Grimes int facs = uap->facs & ~KTRFAC_ROOT; 256df8bae1dSRodney W. Grimes int ops = KTROP(uap->ops); 257df8bae1dSRodney W. Grimes int descend = uap->ops & KTRFLAG_DESCEND; 258df8bae1dSRodney W. Grimes int ret = 0; 259df8bae1dSRodney W. Grimes int error = 0; 260df8bae1dSRodney W. Grimes struct nameidata nd; 261df8bae1dSRodney W. Grimes 262df8bae1dSRodney W. Grimes curp->p_traceflag |= KTRFAC_ACTIVE; 263df8bae1dSRodney W. Grimes if (ops != KTROP_CLEAR) { 264df8bae1dSRodney W. Grimes /* 265df8bae1dSRodney W. Grimes * an operation which requires a file argument. 266df8bae1dSRodney W. Grimes */ 267df8bae1dSRodney W. Grimes NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->fname, curp); 268797f2d22SPoul-Henning Kamp error = vn_open(&nd, FREAD|FWRITE, 0); 269797f2d22SPoul-Henning Kamp if (error) { 270df8bae1dSRodney W. Grimes curp->p_traceflag &= ~KTRFAC_ACTIVE; 271df8bae1dSRodney W. Grimes return (error); 272df8bae1dSRodney W. Grimes } 273df8bae1dSRodney W. Grimes vp = nd.ni_vp; 274df8bae1dSRodney W. Grimes VOP_UNLOCK(vp); 275df8bae1dSRodney W. Grimes if (vp->v_type != VREG) { 276df8bae1dSRodney W. Grimes (void) vn_close(vp, FREAD|FWRITE, curp->p_ucred, curp); 277df8bae1dSRodney W. Grimes curp->p_traceflag &= ~KTRFAC_ACTIVE; 278df8bae1dSRodney W. Grimes return (EACCES); 279df8bae1dSRodney W. Grimes } 280df8bae1dSRodney W. Grimes } 281df8bae1dSRodney W. Grimes /* 282df8bae1dSRodney W. Grimes * Clear all uses of the tracefile 283df8bae1dSRodney W. Grimes */ 284df8bae1dSRodney W. Grimes if (ops == KTROP_CLEARFILE) { 285b75356e1SJeffrey Hsu for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 286df8bae1dSRodney W. Grimes if (p->p_tracep == vp) { 287df8bae1dSRodney W. Grimes if (ktrcanset(curp, p)) { 288df8bae1dSRodney W. Grimes p->p_tracep = NULL; 289df8bae1dSRodney W. Grimes p->p_traceflag = 0; 290df8bae1dSRodney W. Grimes (void) vn_close(vp, FREAD|FWRITE, 291df8bae1dSRodney W. Grimes p->p_ucred, p); 292df8bae1dSRodney W. Grimes } else 293df8bae1dSRodney W. Grimes error = EPERM; 294df8bae1dSRodney W. Grimes } 295df8bae1dSRodney W. Grimes } 296df8bae1dSRodney W. Grimes goto done; 297df8bae1dSRodney W. Grimes } 298df8bae1dSRodney W. Grimes /* 299df8bae1dSRodney W. Grimes * need something to (un)trace (XXX - why is this here?) 300df8bae1dSRodney W. Grimes */ 301df8bae1dSRodney W. Grimes if (!facs) { 302df8bae1dSRodney W. Grimes error = EINVAL; 303df8bae1dSRodney W. Grimes goto done; 304df8bae1dSRodney W. Grimes } 305df8bae1dSRodney W. Grimes /* 306df8bae1dSRodney W. Grimes * do it 307df8bae1dSRodney W. Grimes */ 308df8bae1dSRodney W. Grimes if (uap->pid < 0) { 309df8bae1dSRodney W. Grimes /* 310df8bae1dSRodney W. Grimes * by process group 311df8bae1dSRodney W. Grimes */ 312df8bae1dSRodney W. Grimes pg = pgfind(-uap->pid); 313df8bae1dSRodney W. Grimes if (pg == NULL) { 314df8bae1dSRodney W. Grimes error = ESRCH; 315df8bae1dSRodney W. Grimes goto done; 316df8bae1dSRodney W. Grimes } 317b75356e1SJeffrey Hsu for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) 318df8bae1dSRodney W. Grimes if (descend) 319df8bae1dSRodney W. Grimes ret |= ktrsetchildren(curp, p, ops, facs, vp); 320df8bae1dSRodney W. Grimes else 321df8bae1dSRodney W. Grimes ret |= ktrops(curp, p, ops, facs, vp); 322df8bae1dSRodney W. Grimes 323df8bae1dSRodney W. Grimes } else { 324df8bae1dSRodney W. Grimes /* 325df8bae1dSRodney W. Grimes * by pid 326df8bae1dSRodney W. Grimes */ 327df8bae1dSRodney W. Grimes p = pfind(uap->pid); 328df8bae1dSRodney W. Grimes if (p == NULL) { 329df8bae1dSRodney W. Grimes error = ESRCH; 330df8bae1dSRodney W. Grimes goto done; 331df8bae1dSRodney W. Grimes } 332df8bae1dSRodney W. Grimes if (descend) 333df8bae1dSRodney W. Grimes ret |= ktrsetchildren(curp, p, ops, facs, vp); 334df8bae1dSRodney W. Grimes else 335df8bae1dSRodney W. Grimes ret |= ktrops(curp, p, ops, facs, vp); 336df8bae1dSRodney W. Grimes } 337df8bae1dSRodney W. Grimes if (!ret) 338df8bae1dSRodney W. Grimes error = EPERM; 339df8bae1dSRodney W. Grimes done: 340df8bae1dSRodney W. Grimes if (vp != NULL) 341df8bae1dSRodney W. Grimes (void) vn_close(vp, FWRITE, curp->p_ucred, curp); 342df8bae1dSRodney W. Grimes curp->p_traceflag &= ~KTRFAC_ACTIVE; 343df8bae1dSRodney W. Grimes return (error); 344db6a20e2SGarrett Wollman #else 345db6a20e2SGarrett Wollman return ENOSYS; 346db6a20e2SGarrett Wollman #endif 347df8bae1dSRodney W. Grimes } 348df8bae1dSRodney W. Grimes 349e6c4b9baSPoul-Henning Kamp /* 350e6c4b9baSPoul-Henning Kamp * utrace system call 351e6c4b9baSPoul-Henning Kamp */ 352e6c4b9baSPoul-Henning Kamp /* ARGSUSED */ 353e6c4b9baSPoul-Henning Kamp int 354e6c4b9baSPoul-Henning Kamp utrace(curp, uap, retval) 355e6c4b9baSPoul-Henning Kamp struct proc *curp; 356e6c4b9baSPoul-Henning Kamp register struct utrace_args *uap; 357e6c4b9baSPoul-Henning Kamp int *retval; 358e6c4b9baSPoul-Henning Kamp { 359e6c4b9baSPoul-Henning Kamp #ifdef KTRACE 360e6c4b9baSPoul-Henning Kamp register struct ktr_user *ktp; 361e6c4b9baSPoul-Henning Kamp struct ktr_header *kth; 362e6c4b9baSPoul-Henning Kamp struct proc *p = curproc; /* XXX */ 363e6c4b9baSPoul-Henning Kamp register caddr_t cp; 364e6c4b9baSPoul-Henning Kamp 365e6c4b9baSPoul-Henning Kamp if (!KTRPOINT(p, KTR_USER)) 366e6c4b9baSPoul-Henning Kamp return (0); 367e6c4b9baSPoul-Henning Kamp p->p_traceflag |= KTRFAC_ACTIVE; 368e6c4b9baSPoul-Henning Kamp kth = ktrgetheader(KTR_USER); 369e6c4b9baSPoul-Henning Kamp MALLOC(ktp, struct ktr_user *, sizeof(struct ktr_user) + uap->len, 370e6c4b9baSPoul-Henning Kamp M_KTRACE, M_WAITOK); 371e6c4b9baSPoul-Henning Kamp ktp->len = uap->len; 372e6c4b9baSPoul-Henning Kamp cp = (caddr_t)((char *)ktp + sizeof (struct ktr_user)); 373e6c4b9baSPoul-Henning Kamp if (!copyin(uap->addr, cp, uap->len)) { 374e6c4b9baSPoul-Henning Kamp kth->ktr_buf = (caddr_t)ktp; 375e6c4b9baSPoul-Henning Kamp kth->ktr_len = sizeof (struct ktr_user) + uap->len; 376e6c4b9baSPoul-Henning Kamp ktrwrite(p->p_tracep, kth); 377e6c4b9baSPoul-Henning Kamp } 378e6c4b9baSPoul-Henning Kamp FREE(kth, M_KTRACE); 379e6c4b9baSPoul-Henning Kamp FREE(ktp, M_KTRACE); 380e6c4b9baSPoul-Henning Kamp p->p_traceflag &= ~KTRFAC_ACTIVE; 381e6c4b9baSPoul-Henning Kamp 382e6c4b9baSPoul-Henning Kamp return (0); 383e6c4b9baSPoul-Henning Kamp #else 384e6c4b9baSPoul-Henning Kamp return (ENOSYS); 385e6c4b9baSPoul-Henning Kamp #endif 386e6c4b9baSPoul-Henning Kamp } 387e6c4b9baSPoul-Henning Kamp 388db6a20e2SGarrett Wollman #ifdef KTRACE 38987b6de2bSPoul-Henning Kamp static int 390df8bae1dSRodney W. Grimes ktrops(curp, p, ops, facs, vp) 391df8bae1dSRodney W. Grimes struct proc *p, *curp; 392df8bae1dSRodney W. Grimes int ops, facs; 393df8bae1dSRodney W. Grimes struct vnode *vp; 394df8bae1dSRodney W. Grimes { 395df8bae1dSRodney W. Grimes 396df8bae1dSRodney W. Grimes if (!ktrcanset(curp, p)) 397df8bae1dSRodney W. Grimes return (0); 398df8bae1dSRodney W. Grimes if (ops == KTROP_SET) { 399df8bae1dSRodney W. Grimes if (p->p_tracep != vp) { 400df8bae1dSRodney W. Grimes /* 401df8bae1dSRodney W. Grimes * if trace file already in use, relinquish 402df8bae1dSRodney W. Grimes */ 403df8bae1dSRodney W. Grimes if (p->p_tracep != NULL) 404df8bae1dSRodney W. Grimes vrele(p->p_tracep); 405df8bae1dSRodney W. Grimes VREF(vp); 406df8bae1dSRodney W. Grimes p->p_tracep = vp; 407df8bae1dSRodney W. Grimes } 408df8bae1dSRodney W. Grimes p->p_traceflag |= facs; 409df8bae1dSRodney W. Grimes if (curp->p_ucred->cr_uid == 0) 410df8bae1dSRodney W. Grimes p->p_traceflag |= KTRFAC_ROOT; 411df8bae1dSRodney W. Grimes } else { 412df8bae1dSRodney W. Grimes /* KTROP_CLEAR */ 413df8bae1dSRodney W. Grimes if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) { 414df8bae1dSRodney W. Grimes /* no more tracing */ 415df8bae1dSRodney W. Grimes p->p_traceflag = 0; 416df8bae1dSRodney W. Grimes if (p->p_tracep != NULL) { 417df8bae1dSRodney W. Grimes vrele(p->p_tracep); 418df8bae1dSRodney W. Grimes p->p_tracep = NULL; 419df8bae1dSRodney W. Grimes } 420df8bae1dSRodney W. Grimes } 421df8bae1dSRodney W. Grimes } 422df8bae1dSRodney W. Grimes 423df8bae1dSRodney W. Grimes return (1); 424df8bae1dSRodney W. Grimes } 425df8bae1dSRodney W. Grimes 42687b6de2bSPoul-Henning Kamp static int 427df8bae1dSRodney W. Grimes ktrsetchildren(curp, top, ops, facs, vp) 428df8bae1dSRodney W. Grimes struct proc *curp, *top; 429df8bae1dSRodney W. Grimes int ops, facs; 430df8bae1dSRodney W. Grimes struct vnode *vp; 431df8bae1dSRodney W. Grimes { 432df8bae1dSRodney W. Grimes register struct proc *p; 433df8bae1dSRodney W. Grimes register int ret = 0; 434df8bae1dSRodney W. Grimes 435df8bae1dSRodney W. Grimes p = top; 436df8bae1dSRodney W. Grimes for (;;) { 437df8bae1dSRodney W. Grimes ret |= ktrops(curp, p, ops, facs, vp); 438df8bae1dSRodney W. Grimes /* 439df8bae1dSRodney W. Grimes * If this process has children, descend to them next, 440df8bae1dSRodney W. Grimes * otherwise do any siblings, and if done with this level, 441df8bae1dSRodney W. Grimes * follow back up the tree (but not past top). 442df8bae1dSRodney W. Grimes */ 443b75356e1SJeffrey Hsu if (p->p_children.lh_first) 444b75356e1SJeffrey Hsu p = p->p_children.lh_first; 445df8bae1dSRodney W. Grimes else for (;;) { 446df8bae1dSRodney W. Grimes if (p == top) 447df8bae1dSRodney W. Grimes return (ret); 448b75356e1SJeffrey Hsu if (p->p_sibling.le_next) { 449b75356e1SJeffrey Hsu p = p->p_sibling.le_next; 450df8bae1dSRodney W. Grimes break; 451df8bae1dSRodney W. Grimes } 452b75356e1SJeffrey Hsu p = p->p_pptr; 453df8bae1dSRodney W. Grimes } 454df8bae1dSRodney W. Grimes } 455df8bae1dSRodney W. Grimes /*NOTREACHED*/ 456df8bae1dSRodney W. Grimes } 457df8bae1dSRodney W. Grimes 45887b6de2bSPoul-Henning Kamp static void 459df8bae1dSRodney W. Grimes ktrwrite(vp, kth) 460df8bae1dSRodney W. Grimes struct vnode *vp; 461df8bae1dSRodney W. Grimes register struct ktr_header *kth; 462df8bae1dSRodney W. Grimes { 463df8bae1dSRodney W. Grimes struct uio auio; 464df8bae1dSRodney W. Grimes struct iovec aiov[2]; 465df8bae1dSRodney W. Grimes register struct proc *p = curproc; /* XXX */ 466df8bae1dSRodney W. Grimes int error; 467df8bae1dSRodney W. Grimes 468df8bae1dSRodney W. Grimes if (vp == NULL) 469df8bae1dSRodney W. Grimes return; 470df8bae1dSRodney W. Grimes auio.uio_iov = &aiov[0]; 471df8bae1dSRodney W. Grimes auio.uio_offset = 0; 472df8bae1dSRodney W. Grimes auio.uio_segflg = UIO_SYSSPACE; 473df8bae1dSRodney W. Grimes auio.uio_rw = UIO_WRITE; 474df8bae1dSRodney W. Grimes aiov[0].iov_base = (caddr_t)kth; 475df8bae1dSRodney W. Grimes aiov[0].iov_len = sizeof(struct ktr_header); 476df8bae1dSRodney W. Grimes auio.uio_resid = sizeof(struct ktr_header); 477df8bae1dSRodney W. Grimes auio.uio_iovcnt = 1; 478df8bae1dSRodney W. Grimes auio.uio_procp = (struct proc *)0; 479df8bae1dSRodney W. Grimes if (kth->ktr_len > 0) { 480df8bae1dSRodney W. Grimes auio.uio_iovcnt++; 481df8bae1dSRodney W. Grimes aiov[1].iov_base = kth->ktr_buf; 482df8bae1dSRodney W. Grimes aiov[1].iov_len = kth->ktr_len; 483df8bae1dSRodney W. Grimes auio.uio_resid += kth->ktr_len; 484df8bae1dSRodney W. Grimes } 485df8bae1dSRodney W. Grimes VOP_LOCK(vp); 486df8bae1dSRodney W. Grimes error = VOP_WRITE(vp, &auio, IO_UNIT|IO_APPEND, p->p_ucred); 487df8bae1dSRodney W. Grimes VOP_UNLOCK(vp); 488df8bae1dSRodney W. Grimes if (!error) 489df8bae1dSRodney W. Grimes return; 490df8bae1dSRodney W. Grimes /* 491df8bae1dSRodney W. Grimes * If error encountered, give up tracing on this vnode. 492df8bae1dSRodney W. Grimes */ 493df8bae1dSRodney W. Grimes log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n", 494df8bae1dSRodney W. Grimes error); 495b75356e1SJeffrey Hsu for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 496df8bae1dSRodney W. Grimes if (p->p_tracep == vp) { 497df8bae1dSRodney W. Grimes p->p_tracep = NULL; 498df8bae1dSRodney W. Grimes p->p_traceflag = 0; 499df8bae1dSRodney W. Grimes vrele(vp); 500df8bae1dSRodney W. Grimes } 501df8bae1dSRodney W. Grimes } 502df8bae1dSRodney W. Grimes } 503df8bae1dSRodney W. Grimes 504df8bae1dSRodney W. Grimes /* 505df8bae1dSRodney W. Grimes * Return true if caller has permission to set the ktracing state 506df8bae1dSRodney W. Grimes * of target. Essentially, the target can't possess any 507df8bae1dSRodney W. Grimes * more permissions than the caller. KTRFAC_ROOT signifies that 508df8bae1dSRodney W. Grimes * root previously set the tracing status on the target process, and 509df8bae1dSRodney W. Grimes * so, only root may further change it. 510df8bae1dSRodney W. Grimes * 511df8bae1dSRodney W. Grimes * TODO: check groups. use caller effective gid. 512df8bae1dSRodney W. Grimes */ 51387b6de2bSPoul-Henning Kamp static int 514df8bae1dSRodney W. Grimes ktrcanset(callp, targetp) 515df8bae1dSRodney W. Grimes struct proc *callp, *targetp; 516df8bae1dSRodney W. Grimes { 517df8bae1dSRodney W. Grimes register struct pcred *caller = callp->p_cred; 518df8bae1dSRodney W. Grimes register struct pcred *target = targetp->p_cred; 519df8bae1dSRodney W. Grimes 520df8bae1dSRodney W. Grimes if ((caller->pc_ucred->cr_uid == target->p_ruid && 521df8bae1dSRodney W. Grimes target->p_ruid == target->p_svuid && 522df8bae1dSRodney W. Grimes caller->p_rgid == target->p_rgid && /* XXX */ 523df8bae1dSRodney W. Grimes target->p_rgid == target->p_svgid && 524df8bae1dSRodney W. Grimes (targetp->p_traceflag & KTRFAC_ROOT) == 0) || 525df8bae1dSRodney W. Grimes caller->pc_ucred->cr_uid == 0) 526df8bae1dSRodney W. Grimes return (1); 527df8bae1dSRodney W. Grimes 528df8bae1dSRodney W. Grimes return (0); 529df8bae1dSRodney W. Grimes } 530df8bae1dSRodney W. Grimes 531db6a20e2SGarrett Wollman #endif /* KTRACE */ 532