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