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.\" 3227db57a9SDavid E. O'Brien.\" $FreeBSD$ 3327db57a9SDavid E. O'Brien.\" 34*044fd543SBryan Drewery.Dd March 9, 2016 3527db57a9SDavid E. O'Brien.Dt FILEMON 4 3627db57a9SDavid E. O'Brien.Os 3727db57a9SDavid E. O'Brien.Sh NAME 3827db57a9SDavid E. O'Brien.Nm filemon 3927db57a9SDavid E. O'Brien.Nd the filemon device 4027db57a9SDavid E. O'Brien.Sh SYNOPSIS 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. 5422bcf8a6SBryan DreweryMany syscalls are not tracked and binaries of foreign ABI will not be fully 5522bcf8a6SBryan Dreweryaudited. 5622bcf8a6SBryan DreweryIt is intended for auditing of processes for the purpose of determining its 5722bcf8a6SBryan Drewerydependencies in 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 67db852c28SDavid E. O'Brien.It Ql C 6827db57a9SDavid E. O'Brien.Xr chdir 2 69db852c28SDavid E. O'Brien.It Ql D 7027db57a9SDavid E. O'Brien.Xr unlink 2 71db852c28SDavid E. O'Brien.It Ql E 7227db57a9SDavid E. O'Brien.Xr exec 2 73db852c28SDavid E. O'Brien.It Ql F 7427db57a9SDavid E. O'Brien.Xr fork 2 , 7527db57a9SDavid E. O'Brien.Xr vfork 2 76db852c28SDavid E. O'Brien.It Ql L 7727db57a9SDavid E. O'Brien.Xr link 2 , 7827db57a9SDavid E. O'Brien.Xr linkat 2 , 7927db57a9SDavid E. O'Brien.Xr symlink 2 , 8027db57a9SDavid E. O'Brien.Xr symlinkat 2 81db852c28SDavid E. O'Brien.It Ql M 8227db57a9SDavid E. O'Brien.Xr rename 2 83db852c28SDavid E. O'Brien.It Ql R 8427db57a9SDavid E. O'Brien.Xr open 2 8527db57a9SDavid E. O'Brienfor read 86db852c28SDavid E. O'Brien.It Ql S 8727db57a9SDavid E. O'Brien.Xr stat 2 88db852c28SDavid E. O'Brien.It Ql W 8927db57a9SDavid E. O'Brien.Xr open 2 9027db57a9SDavid E. O'Brienfor write 91db852c28SDavid E. O'Brien.It Ql X 9227db57a9SDavid E. O'Brien.Xr _exit 2 9327db57a9SDavid E. O'Brien.El 9427db57a9SDavid E. O'Brien.Pp 9527db57a9SDavid E. O'BrienNote that 96db852c28SDavid E. O'Brien.Ql R 9727db57a9SDavid E. O'Brienfollowing 98db852c28SDavid E. O'Brien.Ql W 9927db57a9SDavid E. O'Brienrecords can represent a single 10027db57a9SDavid E. O'Brien.Xr open 2 10127db57a9SDavid E. O'Brienfor R/W, 1028124c91fSDavid E. O'Brienor two separate 10327db57a9SDavid E. O'Brien.Xr open 2 10427db57a9SDavid E. O'Briencalls, one for 105db852c28SDavid E. O'Brien.Ql R 10627db57a9SDavid E. O'Brienand one for 107db852c28SDavid E. O'Brien.Ql W . 1088124c91fSDavid E. O'BrienNote that only successful system calls are captured. 10927db57a9SDavid E. O'Brien.Sh IOCTLS 110d5064cc2SJoel DahlUser mode programs communicate with the 111db852c28SDavid E. O'Brien.Nm 112db852c28SDavid E. O'Briendriver through a number of ioctls which are described below. 11327db57a9SDavid E. O'BrienEach takes a single argument. 114db852c28SDavid E. O'Brien.Bl -tag -width ".Dv FILEMON_SET_PID" 11527db57a9SDavid E. O'Brien.It Dv FILEMON_SET_FD 11627db57a9SDavid E. O'BrienWrite the internal tracing buffer to the supplied open file descriptor. 117d5064cc2SJoel Dahl.It Dv FILEMON_SET_PID 11827db57a9SDavid E. O'BrienChild process ID to trace. 11927db57a9SDavid E. O'Brien.El 12027db57a9SDavid E. O'Brien.Sh RETURN VALUES 121db852c28SDavid E. O'Brien.\" .Rv -std ioctl 122db852c28SDavid E. O'BrienThe 123db852c28SDavid E. O'Brien.Fn ioctl 124db852c28SDavid E. O'Brienfunction returns the value 0 if successful; 125db852c28SDavid E. O'Brienotherwise the value \-1 is returned and the global variable 126db852c28SDavid E. O'Brien.Va errno 127db852c28SDavid E. O'Brienis set to indicate the error. 128*044fd543SBryan Drewery.Sh ERRORS 129*044fd543SBryan DreweryThe 130*044fd543SBryan Drewery.Fn ioctl 131*044fd543SBryan Drewerysystem call 132*044fd543SBryan Drewerywith 133*044fd543SBryan Drewery.Dv FILEMON_SET_FD 134*044fd543SBryan Drewerywill fail if: 135*044fd543SBryan Drewery.Bl -tag -width Er 136*044fd543SBryan Drewery.It Bq Er EEXIST 137*044fd543SBryan DreweryThe 138*044fd543SBryan Drewery.Nm 139*044fd543SBryan Dreweryhandle is already associated with a file descriptor. 140*044fd543SBryan Drewery.El 141d5064cc2SJoel Dahl.Sh FILES 142db852c28SDavid E. O'Brien.Bl -tag -width ".Pa /dev/filemon" 143d5064cc2SJoel Dahl.It Pa /dev/filemon 144d5064cc2SJoel Dahl.El 14527db57a9SDavid E. O'Brien.Sh EXAMPLES 146d5064cc2SJoel Dahl.Bd -literal 14727db57a9SDavid E. O'Brien#include <sys/types.h> 14827db57a9SDavid E. O'Brien#include <sys/stat.h> 14927db57a9SDavid E. O'Brien#include <sys/wait.h> 15027db57a9SDavid E. O'Brien#include <sys/ioctl.h> 15127db57a9SDavid E. O'Brien#include <dev/filemon/filemon.h> 15227db57a9SDavid E. O'Brien#include <fcntl.h> 15327db57a9SDavid E. O'Brien#include <err.h> 1540ec5ac10SSergey Kandaurov#include <unistd.h> 15527db57a9SDavid E. O'Brien 15627db57a9SDavid E. O'Brienstatic void 15727db57a9SDavid E. O'Brienopen_filemon(void) 15827db57a9SDavid E. O'Brien{ 15927db57a9SDavid E. O'Brien pid_t child; 16027db57a9SDavid E. O'Brien int fm_fd, fm_log; 16127db57a9SDavid E. O'Brien 1620ec5ac10SSergey Kandaurov if ((fm_fd = open("/dev/filemon", O_RDWR | O_CLOEXEC)) == -1) 163db852c28SDavid E. O'Brien err(1, "open(\e"/dev/filemon\e", O_RDWR)"); 16427db57a9SDavid E. O'Brien if ((fm_log = open("filemon.out", 1650ec5ac10SSergey Kandaurov O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, DEFFILEMODE)) == -1) 16627db57a9SDavid E. O'Brien err(1, "open(filemon.out)"); 16727db57a9SDavid E. O'Brien 16830a39288SDavid E. O'Brien if (ioctl(fm_fd, FILEMON_SET_FD, &fm_log) == -1) 16927db57a9SDavid E. O'Brien err(1, "Cannot set filemon log file descriptor"); 17027db57a9SDavid E. O'Brien 17127db57a9SDavid E. O'Brien if ((child = fork()) == 0) { 17230a39288SDavid E. O'Brien child = getpid(); 17330a39288SDavid E. O'Brien if (ioctl(fm_fd, FILEMON_SET_PID, &child) == -1) 17430a39288SDavid E. O'Brien err(1, "Cannot set filemon PID"); 17527db57a9SDavid E. O'Brien /* Do something here. */ 17627db57a9SDavid E. O'Brien } else { 17727db57a9SDavid E. O'Brien wait(&child); 17827db57a9SDavid E. O'Brien close(fm_fd); 17927db57a9SDavid E. O'Brien } 18027db57a9SDavid E. O'Brien} 18127db57a9SDavid E. O'Brien.Ed 18227db57a9SDavid E. O'Brien.Pp 18327db57a9SDavid E. O'BrienCreates a file named 18427db57a9SDavid E. O'Brien.Pa filemon.out 18527db57a9SDavid E. O'Brienand configures the 18627db57a9SDavid E. O'Brien.Nm 187db852c28SDavid E. O'Briendevice to write the 188db852c28SDavid E. O'Brien.Nm 189db852c28SDavid E. O'Brienbuffer contents to it. 19027db57a9SDavid E. O'Brien.Sh SEE ALSO 19127db57a9SDavid E. O'Brien.Xr dtrace 1 , 19227db57a9SDavid E. O'Brien.Xr ktrace 1 , 193d630b56dSDavid E. O'Brien.Xr script 1 , 1948124c91fSDavid E. O'Brien.Xr truss 1 , 1958124c91fSDavid E. O'Brien.Xr ioctl 2 19627db57a9SDavid E. O'Brien.Sh HISTORY 19727db57a9SDavid E. O'BrienA 19827db57a9SDavid E. O'Brien.Nm 19927db57a9SDavid E. O'Briendevice appeared in 20027db57a9SDavid E. O'Brien.Fx 9.1 . 20122bcf8a6SBryan Drewery.Sh BUGS 20222bcf8a6SBryan DreweryLoading 20322bcf8a6SBryan Drewery.Nm 20422bcf8a6SBryan Drewerymay reduce system performance for the noted syscalls. 2050c370c1aSBryan Drewery.Pp 2060c370c1aSBryan DreweryOnly children of the set process are logged. 2070c370c1aSBryan DreweryProcesses can escape being traced by double forking. 2080c370c1aSBryan DreweryThis is not seen as a problem as the intended use is build monitoring, which 2090c370c1aSBryan Drewerydoes not make sense to have daemons for. 2104039c531SBryan Drewery.Pp 2114039c531SBryan DreweryUnloading the module may panic the system, thus requires using 2124039c531SBryan Drewery.Ic kldunload -f . 213