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 May 30, 2012 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.Bl -tag -width indent -compact 54.It Dq Li C 55.Xr chdir 2 56.It Dq Li D 57.Xr unlink 2 58.It Dq Li E 59.Xr exec 2 60.It Dq Li F 61.Xr fork 2 , 62.Xr vfork 2 63.It Dq Li L 64.Xr link 2 , 65.Xr linkat 2 , 66.Xr symlink 2 , 67.Xr symlinkat 2 68.It Dq Li M 69.Xr rename 2 70.It Dq Li R 71.Xr open 2 72for read 73.It Dq Li S 74.Xr stat 2 75.It Dq Li W 76.Xr open 2 77for write 78.It Dq Li X 79.Xr _exit 2 80.El 81.Pp 82Note that 83.Dq R 84following 85.Dq W 86records can represent a single 87.Xr open 2 88for R/W, 89or two seperate 90.Xr open 2 91calls, one for 92R 93and one for 94W. 95.Sh IOCTLS 96User mode programs communicate with the filemon driver through a 97number of ioctls which are described below. 98Each takes a single argument. 99.Bl -tag -width FILEMON_SET_PID 100.It Dv FILEMON_SET_FD 101Write the internal tracing buffer to the supplied open file descriptor. 102.It Dv FILEMON_SET_PID . 103Child process ID to trace. 104.El 105.Pp 106.Sh RETURN VALUES 107The ioctl returns zero on success and non-zero on failure. 108.Sh EXAMPLES 109.Bd -literal -offset indent 110#include <sys/types.h> 111#include <sys/stat.h> 112#include <sys/wait.h> 113#include <sys/ioctl.h> 114#include <dev/filemon/filemon.h> 115#include <fcntl.h> 116#include <err.h> 117 118static void 119open_filemon(void) 120{ 121 pid_t child; 122 int fm_fd, fm_log; 123 124 if ((fm_fd = open("/dev/filemon", O_RDWR)) == -1) 125 err(1, "open(\"/dev/filemon\", O_RDWR)"); 126 if ((fm_log = open("filemon.out", 127 O_CREAT | O_WRONLY | O_TRUNC, DEFFILEMODE)) == -1) 128 err(1, "open(filemon.out)"); 129 130 if (ioctl(fm_fd, FILEMON_SET_FD, &fm_log) < 0) 131 err(1, "Cannot set filemon log file descriptor"); 132 /* Set up these two fd's to close on exec. */ 133 (void)fcntl(fm_fd, F_SETFD, FD_CLOEXEC); 134 (void)fcntl(fm_log, F_SETFD, FD_CLOEXEC); 135 136 if ((child = fork()) == 0) { 137 /* Do something here. */ 138 return 0; 139 } else { 140 if (ioctl(fm_fd, FILEMON_SET_PID, &child) < 0) 141 err(1, "Cannot set filemon PID"); 142 wait(&child); 143 close(fm_fd); 144 } 145 return 0; 146} 147.Ed 148.Pp 149Creates a file named 150.Pa filemon.out 151and configures the 152.Nm 153device to write the filemon buffer contents to it. 154.Sh FILES 155.Bl -tag -width /dev/zero 156.It Pa /dev/filemon 157.El 158.Sh SEE ALSO 159.Xr dtrace 1 , 160.Xr ktrace 1 , 161.Xr truss 1 162.Sh HISTORY 163A 164.Nm 165device appeared in 166.Fx 9.1 . 167