kern_ktrace.c revision 112199
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1989, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 3. All advertising materials mentioning features or use of this software
141541Srgrimes *    must display the following acknowledgement:
151541Srgrimes *	This product includes software developed by the University of
161541Srgrimes *	California, Berkeley and its contributors.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes *
331541Srgrimes *	@(#)kern_ktrace.c	8.2 (Berkeley) 9/23/93
3450477Speter * $FreeBSD: head/sys/kern/kern_ktrace.c 112199 2003-03-13 18:31:15Z jhb $
351541Srgrimes */
361541Srgrimes
3713203Swollman#include "opt_ktrace.h"
38101123Srwatson#include "opt_mac.h"
391541Srgrimes
401541Srgrimes#include <sys/param.h>
412112Swollman#include <sys/systm.h>
4297993Sjhb#include <sys/fcntl.h>
4397993Sjhb#include <sys/jail.h>
4497993Sjhb#include <sys/kernel.h>
4597993Sjhb#include <sys/kthread.h>
4676166Smarkm#include <sys/lock.h>
4776166Smarkm#include <sys/mutex.h>
48101123Srwatson#include <sys/mac.h>
4997993Sjhb#include <sys/malloc.h>
5097993Sjhb#include <sys/namei.h>
511541Srgrimes#include <sys/proc.h>
5297993Sjhb#include <sys/unistd.h>
531541Srgrimes#include <sys/vnode.h>
541541Srgrimes#include <sys/ktrace.h>
5597993Sjhb#include <sys/sema.h>
5674927Sjhb#include <sys/sx.h>
5797993Sjhb#include <sys/sysctl.h>
581541Srgrimes#include <sys/syslog.h>
5997993Sjhb#include <sys/sysproto.h>
601541Srgrimes
6130354Sphkstatic MALLOC_DEFINE(M_KTRACE, "KTRACE", "KTRACE");
6230309Sphk
6313203Swollman#ifdef KTRACE
6412577Sbde
6597993Sjhb#ifndef KTRACE_REQUEST_POOL
6697993Sjhb#define	KTRACE_REQUEST_POOL	100
6797993Sjhb#endif
6812819Sphk
6997993Sjhbstruct ktr_request {
7097993Sjhb	struct	ktr_header ktr_header;
7197993Sjhb	struct	ucred *ktr_cred;
7297993Sjhb	struct	vnode *ktr_vp;
7397993Sjhb	union {
7497993Sjhb		struct	ktr_syscall ktr_syscall;
7597993Sjhb		struct	ktr_sysret ktr_sysret;
7697993Sjhb		struct	ktr_genio ktr_genio;
7797993Sjhb		struct	ktr_psig ktr_psig;
7897993Sjhb		struct	ktr_csw ktr_csw;
7997993Sjhb	} ktr_data;
8097993Sjhb	STAILQ_ENTRY(ktr_request) ktr_list;
8197993Sjhb};
8297993Sjhb
8397993Sjhbstatic int data_lengths[] = {
8497993Sjhb	0,					/* none */
8597993Sjhb	offsetof(struct ktr_syscall, ktr_args),	/* KTR_SYSCALL */
8697993Sjhb	sizeof(struct ktr_sysret),		/* KTR_SYSRET */
8797993Sjhb	0,					/* KTR_NAMEI */
8897993Sjhb	sizeof(struct ktr_genio),		/* KTR_GENIO */
8997993Sjhb	sizeof(struct ktr_psig),		/* KTR_PSIG */
9097993Sjhb	sizeof(struct ktr_csw),			/* KTR_CSW */
9197993Sjhb	0					/* KTR_USER */
9297993Sjhb};
9397993Sjhb
9497993Sjhbstatic STAILQ_HEAD(, ktr_request) ktr_todo;
9597993Sjhbstatic STAILQ_HEAD(, ktr_request) ktr_free;
9697993Sjhb
97103234SjhbSYSCTL_NODE(_kern, OID_AUTO, ktrace, CTLFLAG_RD, 0, "KTRACE options");
98103234Sjhb
9997993Sjhbstatic uint ktr_requestpool = KTRACE_REQUEST_POOL;
100103234SjhbTUNABLE_INT("kern.ktrace.request_pool", &ktr_requestpool);
10197993Sjhb
102103234Sjhbstatic uint ktr_geniosize = PAGE_SIZE;
103103234SjhbTUNABLE_INT("kern.ktrace.genio_size", &ktr_geniosize);
104103234SjhbSYSCTL_UINT(_kern_ktrace, OID_AUTO, genio_size, CTLFLAG_RW, &ktr_geniosize,
105103234Sjhb    0, "Maximum size of genio event payload");
106103234Sjhb
10797993Sjhbstatic int print_message = 1;
10897993Sjhbstruct mtx ktrace_mtx;
10997993Sjhbstatic struct sema ktrace_sema;
11097993Sjhb
11197993Sjhbstatic void ktrace_init(void *dummy);
11297993Sjhbstatic int sysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS);
11397993Sjhbstatic uint ktrace_resize_pool(uint newsize);
11497993Sjhbstatic struct ktr_request *ktr_getrequest(int type);
11597993Sjhbstatic void ktr_submitrequest(struct ktr_request *req);
11697993Sjhbstatic void ktr_freerequest(struct ktr_request *req);
11797993Sjhbstatic void ktr_loop(void *dummy);
11897993Sjhbstatic void ktr_writerequest(struct ktr_request *req);
11997993Sjhbstatic int ktrcanset(struct thread *,struct proc *);
12097993Sjhbstatic int ktrsetchildren(struct thread *,struct proc *,int,int,struct vnode *);
12197993Sjhbstatic int ktrops(struct thread *,struct proc *,int,int,struct vnode *);
12297993Sjhb
12397993Sjhbstatic void
12497993Sjhbktrace_init(void *dummy)
1251541Srgrimes{
12697993Sjhb	struct ktr_request *req;
12797993Sjhb	int i;
1281541Srgrimes
12997993Sjhb	mtx_init(&ktrace_mtx, "ktrace", NULL, MTX_DEF | MTX_QUIET);
13097993Sjhb	sema_init(&ktrace_sema, 0, "ktrace");
13197993Sjhb	STAILQ_INIT(&ktr_todo);
13297993Sjhb	STAILQ_INIT(&ktr_free);
13397993Sjhb	for (i = 0; i < ktr_requestpool; i++) {
134111119Simp		req = malloc(sizeof(struct ktr_request), M_KTRACE, M_WAITOK);
13597993Sjhb		STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
13697993Sjhb	}
137104354Sscottl	kthread_create(ktr_loop, NULL, NULL, RFHIGHPID, 0, "ktrace");
1381541Srgrimes}
13997993SjhbSYSINIT(ktrace_init, SI_SUB_KTRACE, SI_ORDER_ANY, ktrace_init, NULL);
1401541Srgrimes
14197993Sjhbstatic int
14297993Sjhbsysctl_kern_ktrace_request_pool(SYSCTL_HANDLER_ARGS)
14397993Sjhb{
14497993Sjhb	struct thread *td;
14597993Sjhb	uint newsize, oldsize, wantsize;
14697993Sjhb	int error;
14797993Sjhb
14897993Sjhb	/* Handle easy read-only case first to avoid warnings from GCC. */
14997993Sjhb	if (!req->newptr) {
15097993Sjhb		mtx_lock(&ktrace_mtx);
15197993Sjhb		oldsize = ktr_requestpool;
15297993Sjhb		mtx_unlock(&ktrace_mtx);
15397993Sjhb		return (SYSCTL_OUT(req, &oldsize, sizeof(uint)));
15497993Sjhb	}
15597993Sjhb
15697993Sjhb	error = SYSCTL_IN(req, &wantsize, sizeof(uint));
15797993Sjhb	if (error)
15897993Sjhb		return (error);
15997993Sjhb	td = curthread;
16097993Sjhb	td->td_inktrace = 1;
16197993Sjhb	mtx_lock(&ktrace_mtx);
16297993Sjhb	oldsize = ktr_requestpool;
16397993Sjhb	newsize = ktrace_resize_pool(wantsize);
16497993Sjhb	mtx_unlock(&ktrace_mtx);
16597993Sjhb	td->td_inktrace = 0;
16697993Sjhb	error = SYSCTL_OUT(req, &oldsize, sizeof(uint));
16797993Sjhb	if (error)
16897993Sjhb		return (error);
16997993Sjhb	if (newsize != wantsize)
17097993Sjhb		return (ENOSPC);
17197993Sjhb	return (0);
17297993Sjhb}
173103234SjhbSYSCTL_PROC(_kern_ktrace, OID_AUTO, request_pool, CTLTYPE_UINT|CTLFLAG_RW,
17497993Sjhb    &ktr_requestpool, 0, sysctl_kern_ktrace_request_pool, "IU", "");
17597993Sjhb
17697993Sjhbstatic uint
17797993Sjhbktrace_resize_pool(uint newsize)
17897993Sjhb{
17997993Sjhb	struct ktr_request *req;
18097993Sjhb
18197993Sjhb	mtx_assert(&ktrace_mtx, MA_OWNED);
18297993Sjhb	print_message = 1;
18397993Sjhb	if (newsize == ktr_requestpool)
18497993Sjhb		return (newsize);
18597993Sjhb	if (newsize < ktr_requestpool)
18697993Sjhb		/* Shrink pool down to newsize if possible. */
18797993Sjhb		while (ktr_requestpool > newsize) {
18897993Sjhb			req = STAILQ_FIRST(&ktr_free);
18997993Sjhb			if (req == NULL)
19097993Sjhb				return (ktr_requestpool);
19197993Sjhb			STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
19297993Sjhb			ktr_requestpool--;
19397993Sjhb			mtx_unlock(&ktrace_mtx);
19497993Sjhb			free(req, M_KTRACE);
19597993Sjhb			mtx_lock(&ktrace_mtx);
19697993Sjhb		}
19797993Sjhb	else
19897993Sjhb		/* Grow pool up to newsize. */
19997993Sjhb		while (ktr_requestpool < newsize) {
20097993Sjhb			mtx_unlock(&ktrace_mtx);
20197993Sjhb			req = malloc(sizeof(struct ktr_request), M_KTRACE,
202111119Simp			    M_WAITOK);
20397993Sjhb			mtx_lock(&ktrace_mtx);
20497993Sjhb			STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
20597993Sjhb			ktr_requestpool++;
20697993Sjhb		}
20797993Sjhb	return (ktr_requestpool);
20897993Sjhb}
20997993Sjhb
21097993Sjhbstatic struct ktr_request *
21197993Sjhbktr_getrequest(int type)
21297993Sjhb{
21397993Sjhb	struct ktr_request *req;
21497993Sjhb	struct thread *td = curthread;
21597993Sjhb	struct proc *p = td->td_proc;
21697993Sjhb	int pm;
21797993Sjhb
21897993Sjhb	td->td_inktrace = 1;
21997993Sjhb	mtx_lock(&ktrace_mtx);
22097993Sjhb	if (!KTRCHECK(td, type)) {
22197993Sjhb		mtx_unlock(&ktrace_mtx);
22297993Sjhb		td->td_inktrace = 0;
22397993Sjhb		return (NULL);
22497993Sjhb	}
22597993Sjhb	req = STAILQ_FIRST(&ktr_free);
22697993Sjhb	if (req != NULL) {
22797993Sjhb		STAILQ_REMOVE_HEAD(&ktr_free, ktr_list);
22897993Sjhb		req->ktr_header.ktr_type = type;
229112199Sjhb		if (p->p_traceflag & KTRFAC_DROP) {
230112199Sjhb			req->ktr_header.ktr_type |= KTR_DROP;
231112199Sjhb			p->p_traceflag &= ~KTRFAC_DROP;
232112199Sjhb		}
233112198Sjhb		KASSERT(p->p_tracevp != NULL, ("ktrace: no trace vnode"));
234112198Sjhb		KASSERT(p->p_tracecred != NULL, ("ktrace: no trace cred"));
235112198Sjhb		req->ktr_vp = p->p_tracevp;
236112198Sjhb		VREF(p->p_tracevp);
237112198Sjhb		req->ktr_cred = crhold(p->p_tracecred);
23897993Sjhb		mtx_unlock(&ktrace_mtx);
23997993Sjhb		microtime(&req->ktr_header.ktr_time);
24097993Sjhb		req->ktr_header.ktr_pid = p->p_pid;
24197993Sjhb		bcopy(p->p_comm, req->ktr_header.ktr_comm, MAXCOMLEN + 1);
24297993Sjhb		req->ktr_header.ktr_buffer = NULL;
24397993Sjhb		req->ktr_header.ktr_len = 0;
24497993Sjhb	} else {
245112199Sjhb		p->p_traceflag |= KTRFAC_DROP;
24697993Sjhb		pm = print_message;
24797993Sjhb		print_message = 0;
24897993Sjhb		mtx_unlock(&ktrace_mtx);
24997993Sjhb		if (pm)
25097993Sjhb			printf("Out of ktrace request objects.\n");
25197993Sjhb		td->td_inktrace = 0;
25297993Sjhb	}
25397993Sjhb	return (req);
25497993Sjhb}
25597993Sjhb
25697993Sjhbstatic void
25797993Sjhbktr_submitrequest(struct ktr_request *req)
25897993Sjhb{
25997993Sjhb
26097993Sjhb	mtx_lock(&ktrace_mtx);
26197993Sjhb	STAILQ_INSERT_TAIL(&ktr_todo, req, ktr_list);
26297993Sjhb	sema_post(&ktrace_sema);
26397993Sjhb	mtx_unlock(&ktrace_mtx);
26497993Sjhb	curthread->td_inktrace = 0;
26597993Sjhb}
26697993Sjhb
26797993Sjhbstatic void
26897993Sjhbktr_freerequest(struct ktr_request *req)
26997993Sjhb{
27097993Sjhb
27197993Sjhb	crfree(req->ktr_cred);
272101153Sjhb	if (req->ktr_vp != NULL) {
273101153Sjhb		mtx_lock(&Giant);
274101153Sjhb		vrele(req->ktr_vp);
275101153Sjhb		mtx_unlock(&Giant);
276101153Sjhb	}
277103235Sjhb	if (req->ktr_header.ktr_buffer != NULL)
278103235Sjhb		free(req->ktr_header.ktr_buffer, M_KTRACE);
27997993Sjhb	mtx_lock(&ktrace_mtx);
28097993Sjhb	STAILQ_INSERT_HEAD(&ktr_free, req, ktr_list);
28197993Sjhb	mtx_unlock(&ktrace_mtx);
28297993Sjhb}
28397993Sjhb
28497993Sjhbstatic void
28597993Sjhbktr_loop(void *dummy)
28697993Sjhb{
28797993Sjhb	struct ktr_request *req;
28897993Sjhb	struct thread *td;
28997993Sjhb	struct ucred *cred;
29097993Sjhb
29197993Sjhb	/* Only cache these values once. */
29297993Sjhb	td = curthread;
29397993Sjhb	cred = td->td_ucred;
29497993Sjhb	for (;;) {
29597993Sjhb		sema_wait(&ktrace_sema);
29697993Sjhb		mtx_lock(&ktrace_mtx);
29797993Sjhb		req = STAILQ_FIRST(&ktr_todo);
29897993Sjhb		STAILQ_REMOVE_HEAD(&ktr_todo, ktr_list);
29997993Sjhb		KASSERT(req != NULL, ("got a NULL request"));
300103236Sjhb		mtx_unlock(&ktrace_mtx);
301103236Sjhb		/*
302103236Sjhb		 * It is not enough just to pass the cached cred
303103236Sjhb		 * to the VOP's in ktr_writerequest().  Some VFS
304103236Sjhb		 * operations use curthread->td_ucred, so we need
305103236Sjhb		 * to modify our thread's credentials as well.
306103236Sjhb		 * Evil.
307103236Sjhb		 */
308103236Sjhb		td->td_ucred = req->ktr_cred;
309103236Sjhb		ktr_writerequest(req);
310103236Sjhb		td->td_ucred = cred;
31197993Sjhb		ktr_freerequest(req);
31297993Sjhb	}
31397993Sjhb}
31497993Sjhb
31582585Sdillon/*
31682585Sdillon * MPSAFE
31782585Sdillon */
3181549Srgrimesvoid
31997993Sjhbktrsyscall(code, narg, args)
32047955Sdt	int code, narg;
32147955Sdt	register_t args[];
3221541Srgrimes{
32397993Sjhb	struct ktr_request *req;
32497993Sjhb	struct ktr_syscall *ktp;
32597993Sjhb	size_t buflen;
326103233Sjhb	char *buf = NULL;
3271541Srgrimes
328103233Sjhb	buflen = sizeof(register_t) * narg;
329103233Sjhb	if (buflen > 0) {
330111119Simp		buf = malloc(buflen, M_KTRACE, M_WAITOK);
331103233Sjhb		bcopy(args, buf, buflen);
332103233Sjhb	}
33397993Sjhb	req = ktr_getrequest(KTR_SYSCALL);
334104230Sphk	if (req == NULL) {
335104230Sphk		if (buf != NULL)
336104230Sphk			free(buf, M_KTRACE);
33797993Sjhb		return;
338104230Sphk	}
33997993Sjhb	ktp = &req->ktr_data.ktr_syscall;
3401541Srgrimes	ktp->ktr_code = code;
3411541Srgrimes	ktp->ktr_narg = narg;
34297993Sjhb	if (buflen > 0) {
34397993Sjhb		req->ktr_header.ktr_len = buflen;
344103233Sjhb		req->ktr_header.ktr_buffer = buf;
34597993Sjhb	}
34697993Sjhb	ktr_submitrequest(req);
3471541Srgrimes}
3481541Srgrimes
34982585Sdillon/*
35082585Sdillon * MPSAFE
35182585Sdillon */
3521549Srgrimesvoid
35397993Sjhbktrsysret(code, error, retval)
35447955Sdt	int code, error;
35547955Sdt	register_t retval;
3561541Srgrimes{
35797993Sjhb	struct ktr_request *req;
35897993Sjhb	struct ktr_sysret *ktp;
3591541Srgrimes
36097993Sjhb	req = ktr_getrequest(KTR_SYSRET);
36197993Sjhb	if (req == NULL)
36297993Sjhb		return;
36397993Sjhb	ktp = &req->ktr_data.ktr_sysret;
36497993Sjhb	ktp->ktr_code = code;
36597993Sjhb	ktp->ktr_error = error;
36697993Sjhb	ktp->ktr_retval = retval;		/* what about val2 ? */
36797993Sjhb	ktr_submitrequest(req);
3681541Srgrimes}
3691541Srgrimes
3701549Srgrimesvoid
37197993Sjhbktrnamei(path)
3721541Srgrimes	char *path;
3731541Srgrimes{
37497993Sjhb	struct ktr_request *req;
37597993Sjhb	int namelen;
376103233Sjhb	char *buf = NULL;
3771541Srgrimes
378103233Sjhb	namelen = strlen(path);
379103233Sjhb	if (namelen > 0) {
380111119Simp		buf = malloc(namelen, M_KTRACE, M_WAITOK);
381103233Sjhb		bcopy(path, buf, namelen);
382103233Sjhb	}
38397993Sjhb	req = ktr_getrequest(KTR_NAMEI);
384104230Sphk	if (req == NULL) {
385104230Sphk		if (buf != NULL)
386104230Sphk			free(buf, M_KTRACE);
38797993Sjhb		return;
388104230Sphk	}
38997993Sjhb	if (namelen > 0) {
39097993Sjhb		req->ktr_header.ktr_len = namelen;
391103233Sjhb		req->ktr_header.ktr_buffer = buf;
39297993Sjhb	}
39397993Sjhb	ktr_submitrequest(req);
3941541Srgrimes}
3951541Srgrimes
39697993Sjhb/*
39797993Sjhb * Since the uio may not stay valid, we can not hand off this request to
39897993Sjhb * the thread and need to process it synchronously.  However, we wish to
39997993Sjhb * keep the relative order of records in a trace file correct, so we
40097993Sjhb * do put this request on the queue (if it isn't empty) and then block.
40197993Sjhb * The ktrace thread waks us back up when it is time for this event to
40297993Sjhb * be posted and blocks until we have completed writing out the event
40397993Sjhb * and woken it back up.
40497993Sjhb */
4051549Srgrimesvoid
40697993Sjhbktrgenio(fd, rw, uio, error)
4071541Srgrimes	int fd;
4081541Srgrimes	enum uio_rw rw;
40962378Sgreen	struct uio *uio;
41062378Sgreen	int error;
4111541Srgrimes{
41297993Sjhb	struct ktr_request *req;
41397993Sjhb	struct ktr_genio *ktg;
414103235Sjhb	int datalen;
415103235Sjhb	char *buf;
4168876Srgrimes
4171541Srgrimes	if (error)
4181541Srgrimes		return;
419103235Sjhb	uio->uio_offset = 0;
420103235Sjhb	uio->uio_rw = UIO_WRITE;
421103235Sjhb	datalen = imin(uio->uio_resid, ktr_geniosize);
422111119Simp	buf = malloc(datalen, M_KTRACE, M_WAITOK);
423103235Sjhb	if (uiomove(buf, datalen, uio)) {
424103235Sjhb		free(buf, M_KTRACE);
425103235Sjhb		return;
426103235Sjhb	}
42797993Sjhb	req = ktr_getrequest(KTR_GENIO);
428103235Sjhb	if (req == NULL) {
429103235Sjhb		free(buf, M_KTRACE);
43097993Sjhb		return;
431103235Sjhb	}
43297993Sjhb	ktg = &req->ktr_data.ktr_genio;
43397993Sjhb	ktg->ktr_fd = fd;
43497993Sjhb	ktg->ktr_rw = rw;
435103235Sjhb	req->ktr_header.ktr_len = datalen;
436103235Sjhb	req->ktr_header.ktr_buffer = buf;
43797993Sjhb	ktr_submitrequest(req);
4381541Srgrimes}
4391541Srgrimes
4401549Srgrimesvoid
44197993Sjhbktrpsig(sig, action, mask, code)
44251941Smarcel	int sig;
4431541Srgrimes	sig_t action;
44451791Smarcel	sigset_t *mask;
44551941Smarcel	int code;
4461541Srgrimes{
44797993Sjhb	struct ktr_request *req;
44897993Sjhb	struct ktr_psig	*kp;
4491541Srgrimes
45097993Sjhb	req = ktr_getrequest(KTR_PSIG);
45197993Sjhb	if (req == NULL)
45297993Sjhb		return;
45397993Sjhb	kp = &req->ktr_data.ktr_psig;
45497993Sjhb	kp->signo = (char)sig;
45597993Sjhb	kp->action = action;
45697993Sjhb	kp->mask = *mask;
45797993Sjhb	kp->code = code;
45897993Sjhb	ktr_submitrequest(req);
4591541Srgrimes}
4601541Srgrimes
4611549Srgrimesvoid
46297993Sjhbktrcsw(out, user)
4631541Srgrimes	int out, user;
4641541Srgrimes{
46597993Sjhb	struct ktr_request *req;
46697993Sjhb	struct ktr_csw *kc;
4671541Srgrimes
46897993Sjhb	req = ktr_getrequest(KTR_CSW);
46997993Sjhb	if (req == NULL)
47097993Sjhb		return;
47197993Sjhb	kc = &req->ktr_data.ktr_csw;
47297993Sjhb	kc->out = out;
47397993Sjhb	kc->user = user;
47497993Sjhb	ktr_submitrequest(req);
4751541Srgrimes}
47613203Swollman#endif
4771541Srgrimes
4781541Srgrimes/* Interface and common routines */
4791541Srgrimes
4801541Srgrimes/*
4811541Srgrimes * ktrace system call
4821541Srgrimes */
48312221Sbde#ifndef _SYS_SYSPROTO_H_
4841541Srgrimesstruct ktrace_args {
4851541Srgrimes	char	*fname;
4861541Srgrimes	int	ops;
4871541Srgrimes	int	facs;
4881541Srgrimes	int	pid;
4891541Srgrimes};
49012221Sbde#endif
4911541Srgrimes/* ARGSUSED */
4921549Srgrimesint
49383366Sjulianktrace(td, uap)
49483366Sjulian	struct thread *td;
4951541Srgrimes	register struct ktrace_args *uap;
4961541Srgrimes{
49713203Swollman#ifdef KTRACE
4981541Srgrimes	register struct vnode *vp = NULL;
4991541Srgrimes	register struct proc *p;
5001541Srgrimes	struct pgrp *pg;
5011541Srgrimes	int facs = uap->facs & ~KTRFAC_ROOT;
5021541Srgrimes	int ops = KTROP(uap->ops);
5031541Srgrimes	int descend = uap->ops & KTRFLAG_DESCEND;
5041541Srgrimes	int ret = 0;
50562550Smckusick	int flags, error = 0;
5061541Srgrimes	struct nameidata nd;
507112198Sjhb	struct ucred *cred;
5081541Srgrimes
50997993Sjhb	td->td_inktrace = 1;
5101541Srgrimes	if (ops != KTROP_CLEAR) {
5111541Srgrimes		/*
5121541Srgrimes		 * an operation which requires a file argument.
5131541Srgrimes		 */
51483366Sjulian		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->fname, td);
51562550Smckusick		flags = FREAD | FWRITE | O_NOFOLLOW;
51662550Smckusick		error = vn_open(&nd, &flags, 0);
5173308Sphk		if (error) {
51897993Sjhb			td->td_inktrace = 0;
5191541Srgrimes			return (error);
5201541Srgrimes		}
52154655Seivind		NDFREE(&nd, NDF_ONLY_PNBUF);
5221541Srgrimes		vp = nd.ni_vp;
52383366Sjulian		VOP_UNLOCK(vp, 0, td);
5241541Srgrimes		if (vp->v_type != VREG) {
52591406Sjhb			(void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
52697993Sjhb			td->td_inktrace = 0;
5271541Srgrimes			return (EACCES);
5281541Srgrimes		}
5291541Srgrimes	}
5301541Srgrimes	/*
53185397Sdillon	 * Clear all uses of the tracefile.
5321541Srgrimes	 */
5331541Srgrimes	if (ops == KTROP_CLEARFILE) {
53474927Sjhb		sx_slock(&allproc_lock);
53553212Sphk		LIST_FOREACH(p, &allproc, p_list) {
53694618Sjhb			PROC_LOCK(p);
537112198Sjhb			if (p->p_tracevp == vp) {
53897993Sjhb				if (ktrcanset(td, p)) {
53997993Sjhb					mtx_lock(&ktrace_mtx);
540112198Sjhb					cred = p->p_tracecred;
541112198Sjhb					p->p_tracecred = NULL;
542112198Sjhb					p->p_tracevp = NULL;
5431541Srgrimes					p->p_traceflag = 0;
54497993Sjhb					mtx_unlock(&ktrace_mtx);
54594618Sjhb					PROC_UNLOCK(p);
5461541Srgrimes					(void) vn_close(vp, FREAD|FWRITE,
547112198Sjhb						cred, td);
548112198Sjhb					crfree(cred);
54985397Sdillon				} else {
55094618Sjhb					PROC_UNLOCK(p);
5511541Srgrimes					error = EPERM;
55285397Sdillon				}
55394618Sjhb			} else
55494618Sjhb				PROC_UNLOCK(p);
5551541Srgrimes		}
55674927Sjhb		sx_sunlock(&allproc_lock);
5571541Srgrimes		goto done;
5581541Srgrimes	}
5591541Srgrimes	/*
5601541Srgrimes	 * need something to (un)trace (XXX - why is this here?)
5611541Srgrimes	 */
5621541Srgrimes	if (!facs) {
5631541Srgrimes		error = EINVAL;
5641541Srgrimes		goto done;
5651541Srgrimes	}
5668876Srgrimes	/*
5671541Srgrimes	 * do it
5681541Srgrimes	 */
5691541Srgrimes	if (uap->pid < 0) {
5701541Srgrimes		/*
5711541Srgrimes		 * by process group
5721541Srgrimes		 */
57394861Sjhb		sx_slock(&proctree_lock);
5741541Srgrimes		pg = pgfind(-uap->pid);
5751541Srgrimes		if (pg == NULL) {
57694861Sjhb			sx_sunlock(&proctree_lock);
5771541Srgrimes			error = ESRCH;
5781541Srgrimes			goto done;
5791541Srgrimes		}
58091140Stanimura		/*
58191140Stanimura		 * ktrops() may call vrele(). Lock pg_members
58294861Sjhb		 * by the proctree_lock rather than pg_mtx.
58391140Stanimura		 */
58491140Stanimura		PGRP_UNLOCK(pg);
58553212Sphk		LIST_FOREACH(p, &pg->pg_members, p_pglist)
5861541Srgrimes			if (descend)
58794618Sjhb				ret |= ktrsetchildren(td, p, ops, facs, vp);
5888876Srgrimes			else
58994618Sjhb				ret |= ktrops(td, p, ops, facs, vp);
59094861Sjhb		sx_sunlock(&proctree_lock);
5911541Srgrimes	} else {
5921541Srgrimes		/*
5931541Srgrimes		 * by pid
5941541Srgrimes		 */
5951541Srgrimes		p = pfind(uap->pid);
5961541Srgrimes		if (p == NULL) {
5971541Srgrimes			error = ESRCH;
5981541Srgrimes			goto done;
5991541Srgrimes		}
60075893Sjhb		PROC_UNLOCK(p);
60194618Sjhb		/* XXX: UNLOCK above has a race */
6021541Srgrimes		if (descend)
60394618Sjhb			ret |= ktrsetchildren(td, p, ops, facs, vp);
6041541Srgrimes		else
60594618Sjhb			ret |= ktrops(td, p, ops, facs, vp);
6061541Srgrimes	}
6071541Srgrimes	if (!ret)
6081541Srgrimes		error = EPERM;
6091541Srgrimesdone:
6101541Srgrimes	if (vp != NULL)
61191406Sjhb		(void) vn_close(vp, FWRITE, td->td_ucred, td);
61297993Sjhb	td->td_inktrace = 0;
6131541Srgrimes	return (error);
61413203Swollman#else
61513203Swollman	return ENOSYS;
61613203Swollman#endif
6171541Srgrimes}
6181541Srgrimes
61918398Sphk/*
62018398Sphk * utrace system call
62118398Sphk */
62218398Sphk/* ARGSUSED */
62318398Sphkint
62483366Sjulianutrace(td, uap)
62583366Sjulian	struct thread *td;
62618398Sphk	register struct utrace_args *uap;
62718398Sphk{
62883366Sjulian
62913203Swollman#ifdef KTRACE
63097993Sjhb	struct ktr_request *req;
63199009Salfred	void *cp;
632103237Sjhb	int error;
63318398Sphk
634103237Sjhb	if (!KTRPOINT(td, KTR_USER))
635103237Sjhb		return (0);
63670792Salfred	if (uap->len > KTR_USER_MAXLEN)
63770707Salfred		return (EINVAL);
638111119Simp	cp = malloc(uap->len, M_KTRACE, M_WAITOK);
639103237Sjhb	error = copyin(uap->addr, cp, uap->len);
640104230Sphk	if (error) {
641104230Sphk		free(cp, M_KTRACE);
642103237Sjhb		return (error);
643104230Sphk	}
64497993Sjhb	req = ktr_getrequest(KTR_USER);
645104230Sphk	if (req == NULL) {
646104230Sphk		free(cp, M_KTRACE);
64797993Sjhb		return (0);
648104230Sphk	}
649103237Sjhb	req->ktr_header.ktr_buffer = cp;
650103237Sjhb	req->ktr_header.ktr_len = uap->len;
651103237Sjhb	ktr_submitrequest(req);
65218398Sphk	return (0);
65318398Sphk#else
65418398Sphk	return (ENOSYS);
65518398Sphk#endif
65618398Sphk}
65718398Sphk
65818398Sphk#ifdef KTRACE
65912819Sphkstatic int
66094618Sjhbktrops(td, p, ops, facs, vp)
66194618Sjhb	struct thread *td;
66294618Sjhb	struct proc *p;
6631541Srgrimes	int ops, facs;
6641541Srgrimes	struct vnode *vp;
6651541Srgrimes{
66697993Sjhb	struct vnode *tracevp = NULL;
667112198Sjhb	struct ucred *tracecred = NULL;
6681541Srgrimes
66994618Sjhb	PROC_LOCK(p);
67094618Sjhb	if (!ktrcanset(td, p)) {
67194618Sjhb		PROC_UNLOCK(p);
6721541Srgrimes		return (0);
67394618Sjhb	}
67497993Sjhb	mtx_lock(&ktrace_mtx);
6751541Srgrimes	if (ops == KTROP_SET) {
676112198Sjhb		if (p->p_tracevp != vp) {
6771541Srgrimes			/*
67894618Sjhb			 * if trace file already in use, relinquish below
6791541Srgrimes			 */
680112198Sjhb			tracevp = p->p_tracevp;
68197993Sjhb			VREF(vp);
682112198Sjhb			p->p_tracevp = vp;
6831541Srgrimes		}
684112198Sjhb		if (p->p_tracecred != td->td_ucred) {
685112198Sjhb			tracecred = p->p_tracecred;
686112198Sjhb			p->p_tracecred = crhold(td->td_ucred);
687112198Sjhb		}
6881541Srgrimes		p->p_traceflag |= facs;
68994618Sjhb		if (td->td_ucred->cr_uid == 0)
6901541Srgrimes			p->p_traceflag |= KTRFAC_ROOT;
6918876Srgrimes	} else {
6921541Srgrimes		/* KTROP_CLEAR */
6931541Srgrimes		if (((p->p_traceflag &= ~facs) & KTRFAC_MASK) == 0) {
6941541Srgrimes			/* no more tracing */
6951541Srgrimes			p->p_traceflag = 0;
696112198Sjhb			tracevp = p->p_tracevp;
697112198Sjhb			p->p_tracevp = NULL;
698112198Sjhb			tracecred = p->p_tracecred;
699112198Sjhb			p->p_tracecred = NULL;
7001541Srgrimes		}
7011541Srgrimes	}
70297993Sjhb	mtx_unlock(&ktrace_mtx);
70394618Sjhb	PROC_UNLOCK(p);
70497993Sjhb	if (tracevp != NULL)
70597993Sjhb		vrele(tracevp);
706112198Sjhb	if (tracecred != NULL)
707112198Sjhb		crfree(tracecred);
7081541Srgrimes
7091541Srgrimes	return (1);
7101541Srgrimes}
7111541Srgrimes
71212819Sphkstatic int
71394618Sjhbktrsetchildren(td, top, ops, facs, vp)
71494618Sjhb	struct thread *td;
71594618Sjhb	struct proc *top;
7161541Srgrimes	int ops, facs;
7171541Srgrimes	struct vnode *vp;
7181541Srgrimes{
7191541Srgrimes	register struct proc *p;
7201541Srgrimes	register int ret = 0;
7211541Srgrimes
7221541Srgrimes	p = top;
72374927Sjhb	sx_slock(&proctree_lock);
7241541Srgrimes	for (;;) {
72594618Sjhb		ret |= ktrops(td, p, ops, facs, vp);
7261541Srgrimes		/*
7271541Srgrimes		 * If this process has children, descend to them next,
7281541Srgrimes		 * otherwise do any siblings, and if done with this level,
7291541Srgrimes		 * follow back up the tree (but not past top).
7301541Srgrimes		 */
73153212Sphk		if (!LIST_EMPTY(&p->p_children))
73253212Sphk			p = LIST_FIRST(&p->p_children);
7331541Srgrimes		else for (;;) {
73470317Sjake			if (p == top) {
73574927Sjhb				sx_sunlock(&proctree_lock);
7361541Srgrimes				return (ret);
73770317Sjake			}
73853212Sphk			if (LIST_NEXT(p, p_sibling)) {
73953212Sphk				p = LIST_NEXT(p, p_sibling);
7401541Srgrimes				break;
7411541Srgrimes			}
74214529Shsu			p = p->p_pptr;
7431541Srgrimes		}
7441541Srgrimes	}
7451541Srgrimes	/*NOTREACHED*/
7461541Srgrimes}
7471541Srgrimes
74812819Sphkstatic void
74997993Sjhbktr_writerequest(struct ktr_request *req)
75097993Sjhb{
75197993Sjhb	struct ktr_header *kth;
7521541Srgrimes	struct vnode *vp;
75397993Sjhb	struct proc *p;
75497993Sjhb	struct thread *td;
75597993Sjhb	struct ucred *cred;
7561541Srgrimes	struct uio auio;
75797993Sjhb	struct iovec aiov[3];
75862976Smckusick	struct mount *mp;
75997993Sjhb	int datalen, buflen, vrele_count;
7601541Srgrimes	int error;
7611541Srgrimes
76297993Sjhb	vp = req->ktr_vp;
76397993Sjhb	/*
76497993Sjhb	 * If vp is NULL, the vp has been cleared out from under this
76597993Sjhb	 * request, so just drop it.
76697993Sjhb	 */
7671541Srgrimes	if (vp == NULL)
7681541Srgrimes		return;
76997993Sjhb	kth = &req->ktr_header;
770112199Sjhb	datalen = data_lengths[(ushort)kth->ktr_type & ~KTR_DROP];
77197993Sjhb	buflen = kth->ktr_len;
77297993Sjhb	cred = req->ktr_cred;
77397993Sjhb	td = curthread;
7741541Srgrimes	auio.uio_iov = &aiov[0];
7751541Srgrimes	auio.uio_offset = 0;
7761541Srgrimes	auio.uio_segflg = UIO_SYSSPACE;
7771541Srgrimes	auio.uio_rw = UIO_WRITE;
7781541Srgrimes	aiov[0].iov_base = (caddr_t)kth;
7791541Srgrimes	aiov[0].iov_len = sizeof(struct ktr_header);
7801541Srgrimes	auio.uio_resid = sizeof(struct ktr_header);
7811541Srgrimes	auio.uio_iovcnt = 1;
78297993Sjhb	auio.uio_td = td;
78397993Sjhb	if (datalen != 0) {
78497993Sjhb		aiov[1].iov_base = (caddr_t)&req->ktr_data;
78597993Sjhb		aiov[1].iov_len = datalen;
78697993Sjhb		auio.uio_resid += datalen;
7871541Srgrimes		auio.uio_iovcnt++;
78897993Sjhb		kth->ktr_len += datalen;
7891541Srgrimes	}
79097993Sjhb	if (buflen != 0) {
79197993Sjhb		KASSERT(kth->ktr_buffer != NULL, ("ktrace: nothing to write"));
79297993Sjhb		aiov[auio.uio_iovcnt].iov_base = kth->ktr_buffer;
79397993Sjhb		aiov[auio.uio_iovcnt].iov_len = buflen;
79497993Sjhb		auio.uio_resid += buflen;
79597993Sjhb		auio.uio_iovcnt++;
796103235Sjhb	}
79797993Sjhb	mtx_lock(&Giant);
79862976Smckusick	vn_start_write(vp, &mp, V_WAIT);
79983366Sjulian	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
80097993Sjhb	(void)VOP_LEASE(vp, td, cred, LEASE_WRITE);
801101123Srwatson#ifdef MAC
802102129Srwatson	error = mac_check_vnode_write(cred, NOCRED, vp);
803101123Srwatson	if (error == 0)
804101123Srwatson#endif
805101123Srwatson		error = VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, cred);
80683366Sjulian	VOP_UNLOCK(vp, 0, td);
80762976Smckusick	vn_finished_write(mp);
80897993Sjhb	mtx_unlock(&Giant);
8091541Srgrimes	if (!error)
8101541Srgrimes		return;
8111541Srgrimes	/*
81297993Sjhb	 * If error encountered, give up tracing on this vnode.  We defer
81397993Sjhb	 * all the vrele()'s on the vnode until after we are finished walking
81497993Sjhb	 * the various lists to avoid needlessly holding locks.
8151541Srgrimes	 */
8161541Srgrimes	log(LOG_NOTICE, "ktrace write failed, errno %d, tracing stopped\n",
8171541Srgrimes	    error);
81897993Sjhb	vrele_count = 0;
81997993Sjhb	/*
82097993Sjhb	 * First, clear this vnode from being used by any processes in the
82197993Sjhb	 * system.
82297993Sjhb	 * XXX - If one process gets an EPERM writing to the vnode, should
82397993Sjhb	 * we really do this?  Other processes might have suitable
82497993Sjhb	 * credentials for the operation.
82597993Sjhb	 */
826112198Sjhb	cred = NULL;
82774927Sjhb	sx_slock(&allproc_lock);
82853212Sphk	LIST_FOREACH(p, &allproc, p_list) {
82997993Sjhb		PROC_LOCK(p);
830112198Sjhb		if (p->p_tracevp == vp) {
83197993Sjhb			mtx_lock(&ktrace_mtx);
832112198Sjhb			p->p_tracevp = NULL;
8331541Srgrimes			p->p_traceflag = 0;
834112198Sjhb			cred = p->p_tracecred;
835112198Sjhb			p->p_tracecred = NULL;
83697993Sjhb			mtx_unlock(&ktrace_mtx);
83797993Sjhb			vrele_count++;
8381541Srgrimes		}
83997993Sjhb		PROC_UNLOCK(p);
840112198Sjhb		if (cred != NULL) {
841112198Sjhb			crfree(cred);
842112198Sjhb			cred = NULL;
843112198Sjhb		}
8441541Srgrimes	}
84574927Sjhb	sx_sunlock(&allproc_lock);
84697993Sjhb	/*
84797993Sjhb	 * Second, clear this vnode from any pending requests.
84897993Sjhb	 */
84997993Sjhb	mtx_lock(&ktrace_mtx);
85097993Sjhb	STAILQ_FOREACH(req, &ktr_todo, ktr_list) {
85197993Sjhb		if (req->ktr_vp == vp) {
85297993Sjhb			req->ktr_vp = NULL;
85397993Sjhb			vrele_count++;
85497993Sjhb		}
85597993Sjhb	}
85697993Sjhb	mtx_unlock(&ktrace_mtx);
85797993Sjhb	mtx_lock(&Giant);
85897993Sjhb	while (vrele_count-- > 0)
85997993Sjhb		vrele(vp);
86097993Sjhb	mtx_unlock(&Giant);
8611541Srgrimes}
8621541Srgrimes
8631541Srgrimes/*
8641541Srgrimes * Return true if caller has permission to set the ktracing state
8651541Srgrimes * of target.  Essentially, the target can't possess any
8661541Srgrimes * more permissions than the caller.  KTRFAC_ROOT signifies that
8678876Srgrimes * root previously set the tracing status on the target process, and
8681541Srgrimes * so, only root may further change it.
8691541Srgrimes */
87012819Sphkstatic int
87194618Sjhbktrcanset(td, targetp)
87294618Sjhb	struct thread *td;
87394618Sjhb	struct proc *targetp;
8741541Srgrimes{
8751541Srgrimes
87694618Sjhb	PROC_LOCK_ASSERT(targetp, MA_OWNED);
87779335Srwatson	if (targetp->p_traceflag & KTRFAC_ROOT &&
87894618Sjhb	    suser_cred(td->td_ucred, PRISON_ROOT))
87946155Sphk		return (0);
8801541Srgrimes
88196886Sjhb	if (p_candebug(td, targetp) != 0)
88279335Srwatson		return (0);
88379335Srwatson
88479335Srwatson	return (1);
8851541Srgrimes}
8861541Srgrimes
88713203Swollman#endif /* KTRACE */
888