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