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