xref: /freebsd/share/man/man4/filemon.4 (revision 63d1fd5970ec814904aa0f4580b10a0d302d08b2)
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 March 22, 2016
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
43The
44.Nm
45device allows a process to collect file operations data of its children.
46The device
47.Pa /dev/filemon
48responds to two
49.Xr ioctl 2
50calls.
51.Pp
52.Nm
53is not intended to be a security auditing tool.
54Many system calls are not tracked and binaries of foreign ABI will not be fully
55audited.
56It is intended for auditing of processes for the purpose of determining its
57dependencies in an efficient and easily parsable format.
58An example of this is
59.Xr make 1
60which uses this module with
61.Sy .MAKE.MODE=meta
62to handle incremental builds more smartly.
63.Pp
64System calls are denoted using the following single letters:
65.Pp
66.Bl -tag -width indent -compact
67.It Ql A
68.Xr openat 2 .
69The 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.Xr symlinkat 2
84.It Ql M
85.Xr rename 2
86.It Ql R
87.Xr open 2
88or
89.Xr openat 2
90for read
91.It Ql W
92.Xr open 2
93or
94.Xr openat 2
95for write
96.It Ql X
97.Xr _exit 2
98.El
99.Pp
100Note that
101.Ql R
102following
103.Ql W
104records can represent a single
105.Xr open 2
106for R/W,
107or two separate
108.Xr open 2
109calls, one for
110.Ql R
111and one for
112.Ql W .
113Note that only successful system calls are captured.
114.Sh IOCTLS
115User mode programs communicate with the
116.Nm
117driver through a number of ioctls which are described below.
118Each takes a single argument.
119.Bl -tag -width ".Dv FILEMON_SET_PID"
120.It Dv FILEMON_SET_FD
121Write the internal tracing buffer to the supplied open file descriptor.
122.It Dv FILEMON_SET_PID
123Child process ID to trace.
124This should normally be done under the control of a parent in the child after
125.Xr fork 2
126but before anything else.
127See the example below.
128.El
129.Sh RETURN VALUES
130.\" .Rv -std ioctl
131The
132.Fn ioctl
133function returns the value 0 if successful;
134otherwise the value \-1 is returned and the global variable
135.Va errno
136is set to indicate the error.
137.Sh ERRORS
138The
139.Fn ioctl
140system call
141with
142.Dv FILEMON_SET_FD
143will fail if:
144.Bl -tag -width Er
145.It Bq Er EEXIST
146The
147.Nm
148handle is already associated with a file descriptor.
149.El
150.Pp
151The
152.Fn ioctl
153system call
154with
155.Dv FILEMON_SET_PID
156will fail if:
157.Bl -tag -width Er
158.It Bq Er ESRCH
159No process having the specified process ID exists.
160.It Bq Er EBUSY
161The process ID specified is already being traced and was not the current
162process.
163.El
164.Pp
165The
166.Fn close
167system call on the filemon file descriptor may fail with the errors from
168.Xr write 2
169if any error is encountered while writing the log.
170It may also fail if:
171.Bl -tag -width Er
172.It Bq Er EFAULT
173An invalid address was used for a traced system call argument, resulting in
174no log entry for the system call.
175.It Bq Er ENAMETOOLONG
176An argument for a traced system call was too long, resulting in
177no log entry for the system call.
178.El
179.Sh FILES
180.Bl -tag -width ".Pa /dev/filemon"
181.It Pa /dev/filemon
182.El
183.Sh EXAMPLES
184.Bd -literal
185#include <sys/types.h>
186#include <sys/stat.h>
187#include <sys/wait.h>
188#include <sys/ioctl.h>
189#include <dev/filemon/filemon.h>
190#include <fcntl.h>
191#include <err.h>
192#include <unistd.h>
193
194static void
195open_filemon(void)
196{
197	pid_t child;
198	int fm_fd, fm_log;
199
200	if ((fm_fd = open("/dev/filemon", O_RDWR | O_CLOEXEC)) == -1)
201		err(1, "open(\e"/dev/filemon\e", O_RDWR)");
202	if ((fm_log = open("filemon.out",
203	    O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, DEFFILEMODE)) == -1)
204		err(1, "open(filemon.out)");
205
206	if (ioctl(fm_fd, FILEMON_SET_FD, &fm_log) == -1)
207		err(1, "Cannot set filemon log file descriptor");
208
209	if ((child = fork()) == 0) {
210		child = getpid();
211		if (ioctl(fm_fd, FILEMON_SET_PID, &child) == -1)
212			err(1, "Cannot set filemon PID");
213		/* Do something here. */
214	} else {
215		wait(&child);
216		close(fm_fd);
217	}
218}
219.Ed
220.Pp
221Creates a file named
222.Pa filemon.out
223and configures the
224.Nm
225device to write the
226.Nm
227buffer contents to it.
228.Sh SEE ALSO
229.Xr dtrace 1 ,
230.Xr ktrace 1 ,
231.Xr script 1 ,
232.Xr truss 1 ,
233.Xr ioctl 2
234.Sh HISTORY
235A
236.Nm
237device appeared in
238.Fx 9.1 .
239.Sh BUGS
240Unloading the module may panic the system, thus requires using
241.Ic kldunload -f .
242