FILEMON(4)
π FILEMON(4) β the filemon device π
π NAME π
filemon β literally the filemon device. bestie, this is filemon(4) from the Kernel Interfaces Manual, and yes, that's the whole name situation π
β¨ SYNOPSIS β¨
device filemon
#include <dev/filemon/filemon.h>
π¦ DESCRIPTION π¦
okay so the filemon device lets a process collect file operations data from its children. the device /dev/filemon responds to two ioctl(2) calls, periodt.
β οΈ bestie, filemon is not intended to be a security auditing tool. Many system calls are not tracked and binaries using a non-native ABI may not be fully audited.
it is intended for auditing of processes for the purpose of determining their dependencies using an efficient and easily parsable format. an example of this is make(1) which uses this module with .MAKE.MODE=meta to handle incremental builds more smartly π₯
system calls are denoted using the following single letters:
Aβopenat(2). The next log entry may be lacking an absolute path or be inaccurate.Cβchdir(2)Dβunlink(2)Eβexecve(2)Fβfork(2),vfork(2)Lβlink(2),linkat(2),symlink(2)Mβrename(2)Rβopen(2)oropenat(2)for readWβopen(2)oropenat(2)for writeXβ_exit(2)
note that R following W records can represent a single open(2) for R/W, or two separate open(2) calls, one for R and one for W. also note that only successful system calls are captured π
πΈ IOCTLS πΈ
user mode programs communicate with the filemon driver through a number of ioctls which are described below. each takes a single argument.
FILEMON_SET_FDβ write the internal tracing buffer to the supplied open file descriptor.FILEMON_SET_PIDβ child process ID to trace. This should normally be done under the control of a parent in the child afterfork(2)but before anything else. See the example below.
π RETURN VALUES π
the ioctl() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.
π ERRORS π
the ioctl() system call with FILEMON_SET_FD will fail if:
[EEXIST]β the filemon handle is already associated with a file descriptor.[EINVAL]β the file descriptor has an invalid type and cannot be used for tracing.[EBADF]β the file descriptor is invalid or not opened for writing.
the ioctl() system call with FILEMON_SET_PID will fail if:
[ESRCH]β no process having the specified process ID exists.[EBUSY]β the process ID specified is already being traced and was not the current process.
the close() system call on the filemon file descriptor may fail with the errors from write(2) if any error is encountered while writing the log. it may also fail if:
[EFAULT]β an invalid address was used for a traced system call argument, resulting in no log entry for the system call.[ENAMETOOLONG]β an argument for a traced system call was too long, resulting in no log entry for the system call.
π§ FILES π§
/dev/filemon
π EXAMPLES π
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <dev/filemon/filemon.h>
#include <fcntl.h>
#include <err.h>
#include <errno.h>
#include <unistd.h>
static void
open_filemon(void)
{
pid_t child, wait_rv;
int fm_fd, fm_log;
if ((fm_fd = open("/dev/filemon", O_RDWR | O_CLOEXEC)) == -1)
err(1, "open(\"/dev/filemon\", O_RDWR)");
if ((fm_log = open("filemon.out",
O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, DEFFILEMODE)) == -1)
err(1, "open(filemon.out)");
if (ioctl(fm_fd, FILEMON_SET_FD, &fm_log) == -1)
err(1, "Cannot set filemon log file descriptor");
if ((child = fork()) == 0) {
child = getpid();
if (ioctl(fm_fd, FILEMON_SET_PID, &child) == -1)
err(1, "Cannot set filemon PID");
/* Do something here. */
} else if (child == -1)
err(1, "Cannot fork child");
else {
while ((wait_rv = wait(&child)) == -1 &&
errno == EINTR)
;
if (wait_rv == -1)
err(1, "cannot wait for child");
close(fm_fd);
}
}
creates a file named filemon.out and configures the filemon device to write the filemon buffer contents to it β¨
π SEE ALSO π
dtrace(1), ktrace(1), script(1), truss(1), ioctl(2)
π HISTORY π
a filemon device appeared in FreeBSD 9.1.
π BUGS π
unloading the module may panic the system, thus requires using kldunload -f. bestie, do not play with that casually π
mwah π β this FILEMON(4) page is from Debian, dated April 19, 2025. now go collect those file ops, bestie π
β¨
π¦ where this page comes from
- freebsd 15.1: base 15.1
- nixpkgs unstable: source
- Copyright (c) 2012 David E. O'Brien <obrien@FreeBSD.org>. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms