]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man4/filemon.4
MFV c144cc54795d: zlib 1.2.12.
[FreeBSD/FreeBSD.git] / share / man / man4 / filemon.4
1 .\" Copyright (c) 2012
2 .\"     David E. O'Brien <obrien@FreeBSD.org>.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\"    must display the following acknowledgment:
14 .\"     This product includes software developed by David E. O'Brien and
15 .\"     contributors.
16 .\" 4. Neither the name of the author nor the names of its contributors
17 .\"    may be used to endorse or promote products derived from this software
18 .\"    without specific prior written permission.
19 .\"
20 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANT ABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" SUCH DAMAGE.
31 .\"
32 .\" $FreeBSD$
33 .\"
34 .Dd February 1, 2022
35 .Dt FILEMON 4
36 .Os
37 .Sh NAME
38 .Nm filemon
39 .Nd the filemon device
40 .Sh SYNOPSIS
41 .In dev/filemon/filemon.h
42 .Sh DESCRIPTION
43 The
44 .Nm
45 device allows a process to collect file operations data of its children.
46 The device
47 .Pa /dev/filemon
48 responds to two
49 .Xr ioctl 2
50 calls.
51 .Pp
52 .Nm
53 is not intended to be a security auditing tool.
54 Many system calls are not tracked and binaries using a non-native ABI may not
55 be fully audited.
56 It is intended for auditing of processes for the purpose of determining their
57 dependencies using an efficient and easily parsable format.
58 An example of this is
59 .Xr make 1
60 which uses this module with
61 .Sy .MAKE.MODE=meta
62 to handle incremental builds more smartly.
63 .Pp
64 System calls are denoted using the following single letters:
65 .Pp
66 .Bl -tag -width indent -compact
67 .It Ql A
68 .Xr openat 2 .
69 The next log entry may be lacking an absolute path or be inaccurate.
70 .It Ql C
71 .Xr chdir 2
72 .It Ql D
73 .Xr unlink 2
74 .It Ql E
75 .Xr exec 2
76 .It Ql F
77 .Xr fork 2 ,
78 .Xr vfork 2
79 .It Ql L
80 .Xr link 2 ,
81 .Xr linkat 2 ,
82 .Xr symlink 2
83 .It Ql M
84 .Xr rename 2
85 .It Ql R
86 .Xr open 2
87 or
88 .Xr openat 2
89 for read
90 .It Ql W
91 .Xr open 2
92 or
93 .Xr openat 2
94 for write
95 .It Ql X
96 .Xr _exit 2
97 .El
98 .Pp
99 Note that
100 .Ql R
101 following
102 .Ql W
103 records can represent a single
104 .Xr open 2
105 for R/W,
106 or two separate
107 .Xr open 2
108 calls, one for
109 .Ql R
110 and one for
111 .Ql W .
112 Note that only successful system calls are captured.
113 .Sh IOCTLS
114 User mode programs communicate with the
115 .Nm
116 driver through a number of ioctls which are described below.
117 Each takes a single argument.
118 .Bl -tag -width ".Dv FILEMON_SET_PID"
119 .It Dv FILEMON_SET_FD
120 Write the internal tracing buffer to the supplied open file descriptor.
121 .It Dv FILEMON_SET_PID
122 Child process ID to trace.
123 This should normally be done under the control of a parent in the child after
124 .Xr fork 2
125 but before anything else.
126 See the example below.
127 .El
128 .Sh RETURN VALUES
129 .\" .Rv -std ioctl
130 The
131 .Fn ioctl
132 function returns the value 0 if successful;
133 otherwise the value \-1 is returned and the global variable
134 .Va errno
135 is set to indicate the error.
136 .Sh ERRORS
137 The
138 .Fn ioctl
139 system call
140 with
141 .Dv FILEMON_SET_FD
142 will fail if:
143 .Bl -tag -width Er
144 .It Bq Er EEXIST
145 The
146 .Nm
147 handle is already associated with a file descriptor.
148 .It Bq Er EINVAL
149 The file descriptor has an invalid type and cannot be used for
150 tracing.
151 .It Bq Er EBADF
152 The file descriptor is invalid or not opened for writing.
153 .El
154 .Pp
155 The
156 .Fn ioctl
157 system call
158 with
159 .Dv FILEMON_SET_PID
160 will fail if:
161 .Bl -tag -width Er
162 .It Bq Er ESRCH
163 No process having the specified process ID exists.
164 .It Bq Er EBUSY
165 The process ID specified is already being traced and was not the current
166 process.
167 .El
168 .Pp
169 The
170 .Fn close
171 system call on the filemon file descriptor may fail with the errors from
172 .Xr write 2
173 if any error is encountered while writing the log.
174 It may also fail if:
175 .Bl -tag -width Er
176 .It Bq Er EFAULT
177 An invalid address was used for a traced system call argument, resulting in
178 no log entry for the system call.
179 .It Bq Er ENAMETOOLONG
180 An argument for a traced system call was too long, resulting in
181 no log entry for the system call.
182 .El
183 .Sh FILES
184 .Bl -tag -width ".Pa /dev/filemon"
185 .It Pa /dev/filemon
186 .El
187 .Sh EXAMPLES
188 .Bd -literal
189 #include <sys/types.h>
190 #include <sys/stat.h>
191 #include <sys/wait.h>
192 #include <sys/ioctl.h>
193 #include <dev/filemon/filemon.h>
194 #include <fcntl.h>
195 #include <err.h>
196 #include <unistd.h>
197
198 static void
199 open_filemon(void)
200 {
201         pid_t child;
202         int fm_fd, fm_log;
203
204         if ((fm_fd = open("/dev/filemon", O_RDWR | O_CLOEXEC)) == -1)
205                 err(1, "open(\e"/dev/filemon\e", O_RDWR)");
206         if ((fm_log = open("filemon.out",
207             O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, DEFFILEMODE)) == -1)
208                 err(1, "open(filemon.out)");
209
210         if (ioctl(fm_fd, FILEMON_SET_FD, &fm_log) == -1)
211                 err(1, "Cannot set filemon log file descriptor");
212
213         if ((child = fork()) == 0) {
214                 child = getpid();
215                 if (ioctl(fm_fd, FILEMON_SET_PID, &child) == -1)
216                         err(1, "Cannot set filemon PID");
217                 /* Do something here. */
218         } else {
219                 wait(&child);
220                 close(fm_fd);
221         }
222 }
223 .Ed
224 .Pp
225 Creates a file named
226 .Pa filemon.out
227 and configures the
228 .Nm
229 device to write the
230 .Nm
231 buffer contents to it.
232 .Sh SEE ALSO
233 .Xr dtrace 1 ,
234 .Xr ktrace 1 ,
235 .Xr script 1 ,
236 .Xr truss 1 ,
237 .Xr ioctl 2
238 .Sh HISTORY
239 A
240 .Nm
241 device appeared in
242 .Fx 9.1 .
243 .Sh BUGS
244 Unloading the module may panic the system, thus requires using
245 .Ic kldunload -f .