]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - share/man/man4/filemon.4
MFC r294949,r294952,r294953,r294957,r294965,r294967,r294968,r295017,r295026,
[FreeBSD/stable/9.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 acknowledgement:
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 MERCHANTABILITY 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 January 28, 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
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 syscalls are not tracked and binaries of foreign ABI will not be fully
55 audited.
56 It is intended for auditing of processes for the purpose of determining its
57 dependencies in 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 C
68 .Xr chdir 2
69 .It Ql D
70 .Xr unlink 2
71 .It Ql E
72 .Xr exec 2
73 .It Ql F
74 .Xr fork 2 ,
75 .Xr vfork 2
76 .It Ql L
77 .Xr link 2 ,
78 .Xr linkat 2 ,
79 .Xr symlink 2 ,
80 .Xr symlinkat 2
81 .It Ql M
82 .Xr rename 2
83 .It Ql R
84 .Xr open 2
85 for read
86 .It Ql S
87 .Xr stat 2
88 .It Ql W
89 .Xr open 2
90 for write
91 .It Ql X
92 .Xr _exit 2
93 .El
94 .Pp
95 Note that
96 .Ql R
97 following
98 .Ql W
99 records can represent a single
100 .Xr open 2
101 for R/W,
102 or two seperate
103 .Xr open 2
104 calls, one for
105 .Ql R
106 and one for
107 .Ql W .
108 .Sh IOCTLS
109 User mode programs communicate with the
110 .Nm
111 driver through a number of ioctls which are described below.
112 Each takes a single argument.
113 .Bl -tag -width ".Dv FILEMON_SET_PID"
114 .It Dv FILEMON_SET_FD
115 Write the internal tracing buffer to the supplied open file descriptor.
116 .It Dv FILEMON_SET_PID
117 Child process ID to trace.
118 .El
119 .Sh RETURN VALUES
120 .\" .Rv -std ioctl
121 The
122 .Fn ioctl
123 function returns the value 0 if successful;
124 otherwise the value \-1 is returned and the global variable
125 .Va errno
126 is set to indicate the error.
127 .Sh FILES
128 .Bl -tag -width ".Pa /dev/filemon"
129 .It Pa /dev/filemon
130 .El
131 .Sh EXAMPLES
132 .Bd -literal
133 #include <sys/types.h>
134 #include <sys/stat.h>
135 #include <sys/wait.h>
136 #include <sys/ioctl.h>
137 #include <dev/filemon/filemon.h>
138 #include <fcntl.h>
139 #include <err.h>
140
141 static void
142 open_filemon(void)
143 {
144         pid_t child;
145         int fm_fd, fm_log;
146
147         if ((fm_fd = open("/dev/filemon", O_RDWR)) == -1)
148                 err(1, "open(\e"/dev/filemon\e", O_RDWR)");
149         if ((fm_log = open("filemon.out",
150             O_CREAT | O_WRONLY | O_TRUNC, DEFFILEMODE)) == -1)
151                 err(1, "open(filemon.out)");
152
153         if (ioctl(fm_fd, FILEMON_SET_FD, &fm_log) == -1)
154                 err(1, "Cannot set filemon log file descriptor");
155         /* Set up these two fd's to close on exec. */
156         (void)fcntl(fm_fd, F_SETFD, FD_CLOEXEC);
157         (void)fcntl(fm_log, F_SETFD, FD_CLOEXEC);
158
159         if ((child = fork()) == 0) {
160                 child = getpid();
161                 if (ioctl(fm_fd, FILEMON_SET_PID, &child) == -1)
162                         err(1, "Cannot set filemon PID");
163                 /* Do something here. */
164                 return 0;
165         } else {
166                 wait(&child);
167                 close(fm_fd);
168         }
169         return 0;
170 }
171 .Ed
172 .Pp
173 Creates a file named
174 .Pa filemon.out
175 and configures the
176 .Nm
177 device to write the
178 .Nm
179 buffer contents to it.
180 .Sh SEE ALSO
181 .Xr dtrace 1 ,
182 .Xr ktrace 1 ,
183 .Xr truss 1
184 .Sh HISTORY
185 A
186 .Nm
187 device appeared in
188 .Fx 9.1 .
189 .Sh BUGS
190 Loading
191 .Nm
192 may reduce system performance for the noted syscalls.
193 .Pp
194 Only children of the set process are logged.
195 Processes can escape being traced by double forking.
196 This is not seen as a problem as the intended use is build monitoring, which
197 does not make sense to have daemons for.