xref: /freebsd/share/man/man4/filemon.4 (revision 057453ffdf57bdfcbadb5d1834d425812e01ff43)
127db57a9SDavid E. O'Brien.\" Copyright (c) 2012
227db57a9SDavid E. O'Brien.\"	David E. O'Brien <obrien@FreeBSD.org>.  All rights reserved.
327db57a9SDavid E. O'Brien.\"
427db57a9SDavid E. O'Brien.\" Redistribution and use in source and binary forms, with or without
527db57a9SDavid E. O'Brien.\" modification, are permitted provided that the following conditions
627db57a9SDavid E. O'Brien.\" are met:
727db57a9SDavid E. O'Brien.\" 1. Redistributions of source code must retain the above copyright
827db57a9SDavid E. O'Brien.\"    notice, this list of conditions and the following disclaimer.
927db57a9SDavid E. O'Brien.\" 2. Redistributions in binary form must reproduce the above copyright
1027db57a9SDavid E. O'Brien.\"    notice, this list of conditions and the following disclaimer in the
1127db57a9SDavid E. O'Brien.\"    documentation and/or other materials provided with the distribution.
1227db57a9SDavid E. O'Brien.\" 3. All advertising materials mentioning features or use of this software
138124c91fSDavid E. O'Brien.\"    must display the following acknowledgment:
1427db57a9SDavid E. O'Brien.\"	This product includes software developed by David E. O'Brien and
1527db57a9SDavid E. O'Brien.\"	contributors.
1627db57a9SDavid E. O'Brien.\" 4. Neither the name of the author nor the names of its contributors
1727db57a9SDavid E. O'Brien.\"    may be used to endorse or promote products derived from this software
1827db57a9SDavid E. O'Brien.\"    without specific prior written permission.
1927db57a9SDavid E. O'Brien.\"
2027db57a9SDavid E. O'Brien.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2127db57a9SDavid E. O'Brien.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2227db57a9SDavid E. O'Brien.\" IMPLIED WARRANTIES OF MERCHANT ABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2327db57a9SDavid E. O'Brien.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2427db57a9SDavid E. O'Brien.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2527db57a9SDavid E. O'Brien.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2627db57a9SDavid E. O'Brien.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2727db57a9SDavid E. O'Brien.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2827db57a9SDavid E. O'Brien.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2927db57a9SDavid E. O'Brien.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3027db57a9SDavid E. O'Brien.\" SUCH DAMAGE.
3127db57a9SDavid E. O'Brien.\"
32*057453ffSDag-Erling Smørgrav.Dd July 30, 2024
3327db57a9SDavid E. O'Brien.Dt FILEMON 4
3427db57a9SDavid E. O'Brien.Os
3527db57a9SDavid E. O'Brien.Sh NAME
3627db57a9SDavid E. O'Brien.Nm filemon
3727db57a9SDavid E. O'Brien.Nd the filemon device
3827db57a9SDavid E. O'Brien.Sh SYNOPSIS
39*057453ffSDag-Erling Smørgrav.Cd device filemon
40*057453ffSDag-Erling Smørgrav.Pp
4127db57a9SDavid E. O'Brien.In dev/filemon/filemon.h
4227db57a9SDavid E. O'Brien.Sh DESCRIPTION
4327db57a9SDavid E. O'BrienThe
4427db57a9SDavid E. O'Brien.Nm
4527db57a9SDavid E. O'Briendevice allows a process to collect file operations data of its children.
4627db57a9SDavid E. O'BrienThe device
4727db57a9SDavid E. O'Brien.Pa /dev/filemon
4827db57a9SDavid E. O'Brienresponds to two
4927db57a9SDavid E. O'Brien.Xr ioctl 2
5027db57a9SDavid E. O'Briencalls.
5127db57a9SDavid E. O'Brien.Pp
5222bcf8a6SBryan Drewery.Nm
5322bcf8a6SBryan Dreweryis not intended to be a security auditing tool.
5403399cd6SMark JohnstonMany system calls are not tracked and binaries using a non-native ABI may not
5503399cd6SMark Johnstonbe fully audited.
5603399cd6SMark JohnstonIt is intended for auditing of processes for the purpose of determining their
5703399cd6SMark Johnstondependencies using an efficient and easily parsable format.
5822bcf8a6SBryan DreweryAn example of this is
5922bcf8a6SBryan Drewery.Xr make 1
6022bcf8a6SBryan Drewerywhich uses this module with
6122bcf8a6SBryan Drewery.Sy .MAKE.MODE=meta
6222bcf8a6SBryan Dreweryto handle incremental builds more smartly.
6322bcf8a6SBryan Drewery.Pp
6427db57a9SDavid E. O'BrienSystem calls are denoted using the following single letters:
65db852c28SDavid E. O'Brien.Pp
6627db57a9SDavid E. O'Brien.Bl -tag -width indent -compact
676c8b789fSBryan Drewery.It Ql A
686c8b789fSBryan Drewery.Xr openat 2 .
696c8b789fSBryan DreweryThe next log entry may be lacking an absolute path or be inaccurate.
70db852c28SDavid E. O'Brien.It Ql C
7127db57a9SDavid E. O'Brien.Xr chdir 2
72db852c28SDavid E. O'Brien.It Ql D
7327db57a9SDavid E. O'Brien.Xr unlink 2
74db852c28SDavid E. O'Brien.It Ql E
7527db57a9SDavid E. O'Brien.Xr exec 2
76db852c28SDavid E. O'Brien.It Ql F
7727db57a9SDavid E. O'Brien.Xr fork 2 ,
7827db57a9SDavid E. O'Brien.Xr vfork 2
79db852c28SDavid E. O'Brien.It Ql L
8027db57a9SDavid E. O'Brien.Xr link 2 ,
8127db57a9SDavid E. O'Brien.Xr linkat 2 ,
8215a04cecSBryan Drewery.Xr symlink 2
83db852c28SDavid E. O'Brien.It Ql M
8427db57a9SDavid E. O'Brien.Xr rename 2
85db852c28SDavid E. O'Brien.It Ql R
8627db57a9SDavid E. O'Brien.Xr open 2
876c8b789fSBryan Dreweryor
886c8b789fSBryan Drewery.Xr openat 2
8927db57a9SDavid E. O'Brienfor read
90db852c28SDavid E. O'Brien.It Ql W
9127db57a9SDavid E. O'Brien.Xr open 2
926c8b789fSBryan Dreweryor
936c8b789fSBryan Drewery.Xr openat 2
9427db57a9SDavid E. O'Brienfor write
95db852c28SDavid E. O'Brien.It Ql X
9627db57a9SDavid E. O'Brien.Xr _exit 2
9727db57a9SDavid E. O'Brien.El
9827db57a9SDavid E. O'Brien.Pp
9927db57a9SDavid E. O'BrienNote that
100db852c28SDavid E. O'Brien.Ql R
10127db57a9SDavid E. O'Brienfollowing
102db852c28SDavid E. O'Brien.Ql W
10327db57a9SDavid E. O'Brienrecords can represent a single
10427db57a9SDavid E. O'Brien.Xr open 2
10527db57a9SDavid E. O'Brienfor R/W,
1068124c91fSDavid E. O'Brienor two separate
10727db57a9SDavid E. O'Brien.Xr open 2
10827db57a9SDavid E. O'Briencalls, one for
109db852c28SDavid E. O'Brien.Ql R
11027db57a9SDavid E. O'Brienand one for
111db852c28SDavid E. O'Brien.Ql W .
1128124c91fSDavid E. O'BrienNote that only successful system calls are captured.
11327db57a9SDavid E. O'Brien.Sh IOCTLS
114d5064cc2SJoel DahlUser mode programs communicate with the
115db852c28SDavid E. O'Brien.Nm
116db852c28SDavid E. O'Briendriver through a number of ioctls which are described below.
11727db57a9SDavid E. O'BrienEach takes a single argument.
118db852c28SDavid E. O'Brien.Bl -tag -width ".Dv FILEMON_SET_PID"
11927db57a9SDavid E. O'Brien.It Dv FILEMON_SET_FD
12027db57a9SDavid E. O'BrienWrite the internal tracing buffer to the supplied open file descriptor.
121d5064cc2SJoel Dahl.It Dv FILEMON_SET_PID
12227db57a9SDavid E. O'BrienChild process ID to trace.
123e0d84b9eSBryan DreweryThis should normally be done under the control of a parent in the child after
124e0d84b9eSBryan Drewery.Xr fork 2
125e0d84b9eSBryan Drewerybut before anything else.
126e0d84b9eSBryan DrewerySee the example below.
12727db57a9SDavid E. O'Brien.El
12827db57a9SDavid E. O'Brien.Sh RETURN VALUES
129db852c28SDavid E. O'Brien.\" .Rv -std ioctl
130db852c28SDavid E. O'BrienThe
131db852c28SDavid E. O'Brien.Fn ioctl
132db852c28SDavid E. O'Brienfunction returns the value 0 if successful;
133db852c28SDavid E. O'Brienotherwise the value \-1 is returned and the global variable
134db852c28SDavid E. O'Brien.Va errno
135db852c28SDavid E. O'Brienis set to indicate the error.
136044fd543SBryan Drewery.Sh ERRORS
137044fd543SBryan DreweryThe
138044fd543SBryan Drewery.Fn ioctl
139044fd543SBryan Drewerysystem call
140044fd543SBryan Drewerywith
141044fd543SBryan Drewery.Dv FILEMON_SET_FD
142044fd543SBryan Drewerywill fail if:
143044fd543SBryan Drewery.Bl -tag -width Er
144044fd543SBryan Drewery.It Bq Er EEXIST
145044fd543SBryan DreweryThe
146044fd543SBryan Drewery.Nm
147044fd543SBryan Dreweryhandle is already associated with a file descriptor.
14803399cd6SMark Johnston.It Bq Er EINVAL
14903399cd6SMark JohnstonThe file descriptor has an invalid type and cannot be used for
15003399cd6SMark Johnstontracing.
15103399cd6SMark Johnston.It Bq Er EBADF
15203399cd6SMark JohnstonThe file descriptor is invalid or not opened for writing.
153044fd543SBryan Drewery.El
154e0d84b9eSBryan Drewery.Pp
155e0d84b9eSBryan DreweryThe
156e0d84b9eSBryan Drewery.Fn ioctl
157e0d84b9eSBryan Drewerysystem call
158e0d84b9eSBryan Drewerywith
159e0d84b9eSBryan Drewery.Dv FILEMON_SET_PID
160e0d84b9eSBryan Drewerywill fail if:
161e0d84b9eSBryan Drewery.Bl -tag -width Er
162e0d84b9eSBryan Drewery.It Bq Er ESRCH
163e0d84b9eSBryan DreweryNo process having the specified process ID exists.
164e0d84b9eSBryan Drewery.It Bq Er EBUSY
165e0d84b9eSBryan DreweryThe process ID specified is already being traced and was not the current
166e0d84b9eSBryan Dreweryprocess.
167e0d84b9eSBryan Drewery.El
1684177d9f7SBryan Drewery.Pp
1694177d9f7SBryan DreweryThe
1704177d9f7SBryan Drewery.Fn close
1714177d9f7SBryan Drewerysystem call on the filemon file descriptor may fail with the errors from
1724177d9f7SBryan Drewery.Xr write 2
1734177d9f7SBryan Dreweryif any error is encountered while writing the log.
1749b511ce9SBryan DreweryIt may also fail if:
1759b511ce9SBryan Drewery.Bl -tag -width Er
1769b511ce9SBryan Drewery.It Bq Er EFAULT
1779b511ce9SBryan DreweryAn invalid address was used for a traced system call argument, resulting in
1789b511ce9SBryan Dreweryno log entry for the system call.
1799b511ce9SBryan Drewery.It Bq Er ENAMETOOLONG
1809b511ce9SBryan DreweryAn argument for a traced system call was too long, resulting in
1819b511ce9SBryan Dreweryno log entry for the system call.
1829b511ce9SBryan Drewery.El
183d5064cc2SJoel Dahl.Sh FILES
184db852c28SDavid E. O'Brien.Bl -tag -width ".Pa /dev/filemon"
185d5064cc2SJoel Dahl.It Pa /dev/filemon
186d5064cc2SJoel Dahl.El
18727db57a9SDavid E. O'Brien.Sh EXAMPLES
188d5064cc2SJoel Dahl.Bd -literal
18927db57a9SDavid E. O'Brien#include <sys/types.h>
19027db57a9SDavid E. O'Brien#include <sys/stat.h>
19127db57a9SDavid E. O'Brien#include <sys/wait.h>
19227db57a9SDavid E. O'Brien#include <sys/ioctl.h>
19327db57a9SDavid E. O'Brien#include <dev/filemon/filemon.h>
19427db57a9SDavid E. O'Brien#include <fcntl.h>
19527db57a9SDavid E. O'Brien#include <err.h>
196258c6d5eSPau Amma#include <errno.h>
1970ec5ac10SSergey Kandaurov#include <unistd.h>
19827db57a9SDavid E. O'Brien
19927db57a9SDavid E. O'Brienstatic void
20027db57a9SDavid E. O'Brienopen_filemon(void)
20127db57a9SDavid E. O'Brien{
202258c6d5eSPau Amma	pid_t child, wait_rv;
20327db57a9SDavid E. O'Brien	int fm_fd, fm_log;
20427db57a9SDavid E. O'Brien
2050ec5ac10SSergey Kandaurov	if ((fm_fd = open("/dev/filemon", O_RDWR | O_CLOEXEC)) == -1)
206db852c28SDavid E. O'Brien		err(1, "open(\e"/dev/filemon\e", O_RDWR)");
20727db57a9SDavid E. O'Brien	if ((fm_log = open("filemon.out",
2080ec5ac10SSergey Kandaurov	    O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, DEFFILEMODE)) == -1)
20927db57a9SDavid E. O'Brien		err(1, "open(filemon.out)");
21027db57a9SDavid E. O'Brien
21130a39288SDavid E. O'Brien	if (ioctl(fm_fd, FILEMON_SET_FD, &fm_log) == -1)
21227db57a9SDavid E. O'Brien		err(1, "Cannot set filemon log file descriptor");
21327db57a9SDavid E. O'Brien
21427db57a9SDavid E. O'Brien	if ((child = fork()) == 0) {
21530a39288SDavid E. O'Brien		child = getpid();
21630a39288SDavid E. O'Brien		if (ioctl(fm_fd, FILEMON_SET_PID, &child) == -1)
21730a39288SDavid E. O'Brien			err(1, "Cannot set filemon PID");
21827db57a9SDavid E. O'Brien		/* Do something here. */
219258c6d5eSPau Amma	} else if (child == -1)
220258c6d5eSPau Amma		err(1, "Cannot fork child");
221258c6d5eSPau Amma	else {
222258c6d5eSPau Amma		while ((wait_rv = wait(&child)) == -1 &&
223258c6d5eSPau Amma		    errno == EINTR)
224258c6d5eSPau Amma			;
225258c6d5eSPau Amma		if (wait_rv == -1)
226258c6d5eSPau Amma			err(1, "cannot wait for child");
22727db57a9SDavid E. O'Brien		close(fm_fd);
22827db57a9SDavid E. O'Brien	}
22927db57a9SDavid E. O'Brien}
23027db57a9SDavid E. O'Brien.Ed
23127db57a9SDavid E. O'Brien.Pp
23227db57a9SDavid E. O'BrienCreates a file named
23327db57a9SDavid E. O'Brien.Pa filemon.out
23427db57a9SDavid E. O'Brienand configures the
23527db57a9SDavid E. O'Brien.Nm
236db852c28SDavid E. O'Briendevice to write the
237db852c28SDavid E. O'Brien.Nm
238db852c28SDavid E. O'Brienbuffer contents to it.
23927db57a9SDavid E. O'Brien.Sh SEE ALSO
24027db57a9SDavid E. O'Brien.Xr dtrace 1 ,
24127db57a9SDavid E. O'Brien.Xr ktrace 1 ,
242d630b56dSDavid E. O'Brien.Xr script 1 ,
2438124c91fSDavid E. O'Brien.Xr truss 1 ,
2448124c91fSDavid E. O'Brien.Xr ioctl 2
24527db57a9SDavid E. O'Brien.Sh HISTORY
24627db57a9SDavid E. O'BrienA
24727db57a9SDavid E. O'Brien.Nm
24827db57a9SDavid E. O'Briendevice appeared in
24927db57a9SDavid E. O'Brien.Fx 9.1 .
25022bcf8a6SBryan Drewery.Sh BUGS
2514039c531SBryan DreweryUnloading the module may panic the system, thus requires using
2524039c531SBryan Drewery.Ic kldunload -f .
253