kern_ktrace_vfs.c revision 1.1.4.2 1 1.1.4.2 rmind /* $NetBSD: kern_ktrace_vfs.c,v 1.1.4.2 2014/05/18 17:46:07 rmind Exp $ */
2 1.1.4.2 rmind
3 1.1.4.2 rmind /*-
4 1.1.4.2 rmind * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
5 1.1.4.2 rmind * All rights reserved.
6 1.1.4.2 rmind *
7 1.1.4.2 rmind * This code is derived from software contributed to The NetBSD Foundation
8 1.1.4.2 rmind * by Andrew Doran.
9 1.1.4.2 rmind *
10 1.1.4.2 rmind * Redistribution and use in source and binary forms, with or without
11 1.1.4.2 rmind * modification, are permitted provided that the following conditions
12 1.1.4.2 rmind * are met:
13 1.1.4.2 rmind * 1. Redistributions of source code must retain the above copyright
14 1.1.4.2 rmind * notice, this list of conditions and the following disclaimer.
15 1.1.4.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.4.2 rmind * notice, this list of conditions and the following disclaimer in the
17 1.1.4.2 rmind * documentation and/or other materials provided with the distribution.
18 1.1.4.2 rmind *
19 1.1.4.2 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.4.2 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.4.2 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.4.2 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.4.2 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.4.2 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.4.2 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.4.2 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.4.2 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.4.2 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.4.2 rmind * POSSIBILITY OF SUCH DAMAGE.
30 1.1.4.2 rmind */
31 1.1.4.2 rmind
32 1.1.4.2 rmind /*
33 1.1.4.2 rmind * Copyright (c) 1989, 1993
34 1.1.4.2 rmind * The Regents of the University of California. All rights reserved.
35 1.1.4.2 rmind *
36 1.1.4.2 rmind * Redistribution and use in source and binary forms, with or without
37 1.1.4.2 rmind * modification, are permitted provided that the following conditions
38 1.1.4.2 rmind * are met:
39 1.1.4.2 rmind * 1. Redistributions of source code must retain the above copyright
40 1.1.4.2 rmind * notice, this list of conditions and the following disclaimer.
41 1.1.4.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
42 1.1.4.2 rmind * notice, this list of conditions and the following disclaimer in the
43 1.1.4.2 rmind * documentation and/or other materials provided with the distribution.
44 1.1.4.2 rmind * 3. Neither the name of the University nor the names of its contributors
45 1.1.4.2 rmind * may be used to endorse or promote products derived from this software
46 1.1.4.2 rmind * without specific prior written permission.
47 1.1.4.2 rmind *
48 1.1.4.2 rmind * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 1.1.4.2 rmind * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 1.1.4.2 rmind * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 1.1.4.2 rmind * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 1.1.4.2 rmind * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 1.1.4.2 rmind * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 1.1.4.2 rmind * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 1.1.4.2 rmind * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 1.1.4.2 rmind * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 1.1.4.2 rmind * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 1.1.4.2 rmind * SUCH DAMAGE.
59 1.1.4.2 rmind *
60 1.1.4.2 rmind * @(#)kern_ktrace.c 8.5 (Berkeley) 5/14/95
61 1.1.4.2 rmind */
62 1.1.4.2 rmind
63 1.1.4.2 rmind #include <sys/cdefs.h>
64 1.1.4.2 rmind __KERNEL_RCSID(0, "$NetBSD: kern_ktrace_vfs.c,v 1.1.4.2 2014/05/18 17:46:07 rmind Exp $");
65 1.1.4.2 rmind
66 1.1.4.2 rmind #include <sys/param.h>
67 1.1.4.2 rmind #include <sys/systm.h>
68 1.1.4.2 rmind #include <sys/file.h>
69 1.1.4.2 rmind #include <sys/filedesc.h>
70 1.1.4.2 rmind #include <sys/namei.h>
71 1.1.4.2 rmind #include <sys/vnode.h>
72 1.1.4.2 rmind #include <sys/kernel.h>
73 1.1.4.2 rmind #include <sys/ktrace.h>
74 1.1.4.2 rmind #include <sys/kauth.h>
75 1.1.4.2 rmind
76 1.1.4.2 rmind #include <sys/mount.h>
77 1.1.4.2 rmind #include <sys/syscallargs.h>
78 1.1.4.2 rmind
79 1.1.4.2 rmind /*
80 1.1.4.2 rmind * ktrace system call, the part of the ktrace framework that
81 1.1.4.2 rmind * explicitly interacts with VFS
82 1.1.4.2 rmind */
83 1.1.4.2 rmind /* ARGSUSED */
84 1.1.4.2 rmind int
85 1.1.4.2 rmind sys_ktrace(struct lwp *l, const struct sys_ktrace_args *uap, register_t *retval)
86 1.1.4.2 rmind {
87 1.1.4.2 rmind /* {
88 1.1.4.2 rmind syscallarg(const char *) fname;
89 1.1.4.2 rmind syscallarg(int) ops;
90 1.1.4.2 rmind syscallarg(int) facs;
91 1.1.4.2 rmind syscallarg(int) pid;
92 1.1.4.2 rmind } */
93 1.1.4.2 rmind struct vnode *vp = NULL;
94 1.1.4.2 rmind file_t *fp = NULL;
95 1.1.4.2 rmind struct pathbuf *pb;
96 1.1.4.2 rmind struct nameidata nd;
97 1.1.4.2 rmind int error = 0;
98 1.1.4.2 rmind int fd;
99 1.1.4.2 rmind
100 1.1.4.2 rmind if (ktrenter(l))
101 1.1.4.2 rmind return EAGAIN;
102 1.1.4.2 rmind
103 1.1.4.2 rmind if (KTROP(SCARG(uap, ops)) != KTROP_CLEAR) {
104 1.1.4.2 rmind /*
105 1.1.4.2 rmind * an operation which requires a file argument.
106 1.1.4.2 rmind */
107 1.1.4.2 rmind error = pathbuf_copyin(SCARG(uap, fname), &pb);
108 1.1.4.2 rmind if (error) {
109 1.1.4.2 rmind ktrexit(l);
110 1.1.4.2 rmind return (error);
111 1.1.4.2 rmind }
112 1.1.4.2 rmind NDINIT(&nd, LOOKUP, FOLLOW, pb);
113 1.1.4.2 rmind if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
114 1.1.4.2 rmind pathbuf_destroy(pb);
115 1.1.4.2 rmind ktrexit(l);
116 1.1.4.2 rmind return (error);
117 1.1.4.2 rmind }
118 1.1.4.2 rmind vp = nd.ni_vp;
119 1.1.4.2 rmind pathbuf_destroy(pb);
120 1.1.4.2 rmind VOP_UNLOCK(vp);
121 1.1.4.2 rmind if (vp->v_type != VREG) {
122 1.1.4.2 rmind vn_close(vp, FREAD|FWRITE, l->l_cred);
123 1.1.4.2 rmind ktrexit(l);
124 1.1.4.2 rmind return (EACCES);
125 1.1.4.2 rmind }
126 1.1.4.2 rmind /*
127 1.1.4.2 rmind * This uses up a file descriptor slot in the
128 1.1.4.2 rmind * tracing process for the duration of this syscall.
129 1.1.4.2 rmind * This is not expected to be a problem.
130 1.1.4.2 rmind */
131 1.1.4.2 rmind if ((error = fd_allocfile(&fp, &fd)) != 0) {
132 1.1.4.2 rmind vn_close(vp, FWRITE, l->l_cred);
133 1.1.4.2 rmind ktrexit(l);
134 1.1.4.2 rmind return error;
135 1.1.4.2 rmind }
136 1.1.4.2 rmind fp->f_flag = FWRITE;
137 1.1.4.2 rmind fp->f_type = DTYPE_VNODE;
138 1.1.4.2 rmind fp->f_ops = &vnops;
139 1.1.4.2 rmind fp->f_data = (void *)vp;
140 1.1.4.2 rmind vp = NULL;
141 1.1.4.2 rmind }
142 1.1.4.2 rmind error = ktrace_common(l, SCARG(uap, ops), SCARG(uap, facs),
143 1.1.4.2 rmind SCARG(uap, pid), &fp);
144 1.1.4.2 rmind if (KTROP(SCARG(uap, ops)) != KTROP_CLEAR)
145 1.1.4.2 rmind fd_abort(curproc, fp, fd);
146 1.1.4.2 rmind return (error);
147 1.1.4.2 rmind }
148