xref: /freebsd/share/man/man4/filemon.4 (revision e72055b7feba695a760d45f01f0f8268b1cb4a74)
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 June 14, 2013
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
52System calls are denoted using the following single letters:
53.Pp
54.Bl -tag -width indent -compact
55.It Ql C
56.Xr chdir 2
57.It Ql D
58.Xr unlink 2
59.It Ql E
60.Xr exec 2
61.It Ql F
62.Xr fork 2 ,
63.Xr vfork 2
64.It Ql L
65.Xr link 2 ,
66.Xr linkat 2 ,
67.Xr symlink 2 ,
68.Xr symlinkat 2
69.It Ql M
70.Xr rename 2
71.It Ql R
72.Xr open 2
73for read
74.It Ql S
75.Xr stat 2
76.It Ql W
77.Xr open 2
78for write
79.It Ql X
80.Xr _exit 2
81.El
82.Pp
83Note that
84.Ql R
85following
86.Ql W
87records can represent a single
88.Xr open 2
89for R/W,
90or two separate
91.Xr open 2
92calls, one for
93.Ql R
94and one for
95.Ql W .
96Note that only successful system calls are captured.
97.Sh IOCTLS
98User mode programs communicate with the
99.Nm
100driver through a number of ioctls which are described below.
101Each takes a single argument.
102.Bl -tag -width ".Dv FILEMON_SET_PID"
103.It Dv FILEMON_SET_FD
104Write the internal tracing buffer to the supplied open file descriptor.
105.It Dv FILEMON_SET_PID
106Child process ID to trace.
107.El
108.Sh RETURN VALUES
109.\" .Rv -std ioctl
110The
111.Fn ioctl
112function returns the value 0 if successful;
113otherwise the value \-1 is returned and the global variable
114.Va errno
115is set to indicate the error.
116.Sh FILES
117.Bl -tag -width ".Pa /dev/filemon"
118.It Pa /dev/filemon
119.El
120.Sh EXAMPLES
121.Bd -literal
122#include <sys/types.h>
123#include <sys/stat.h>
124#include <sys/wait.h>
125#include <sys/ioctl.h>
126#include <dev/filemon/filemon.h>
127#include <fcntl.h>
128#include <err.h>
129#include <unistd.h>
130
131static void
132open_filemon(void)
133{
134	pid_t child;
135	int fm_fd, fm_log;
136
137	if ((fm_fd = open("/dev/filemon", O_RDWR | O_CLOEXEC)) == -1)
138		err(1, "open(\e"/dev/filemon\e", O_RDWR)");
139	if ((fm_log = open("filemon.out",
140	    O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, DEFFILEMODE)) == -1)
141		err(1, "open(filemon.out)");
142
143	if (ioctl(fm_fd, FILEMON_SET_FD, &fm_log) == -1)
144		err(1, "Cannot set filemon log file descriptor");
145
146	if ((child = fork()) == 0) {
147		child = getpid();
148		if (ioctl(fm_fd, FILEMON_SET_PID, &child) == -1)
149			err(1, "Cannot set filemon PID");
150		/* Do something here. */
151	} else {
152		wait(&child);
153		close(fm_fd);
154	}
155}
156.Ed
157.Pp
158Creates a file named
159.Pa filemon.out
160and configures the
161.Nm
162device to write the
163.Nm
164buffer contents to it.
165.Sh SEE ALSO
166.Xr dtrace 1 ,
167.Xr ktrace 1 ,
168.Xr script 1 ,
169.Xr truss 1 ,
170.Xr ioctl 2
171.Sh HISTORY
172A
173.Nm
174device appeared in
175.Fx 9.1 .
176