FILEMON(4)
β¨ FILEMON(4) β¨
okay bestie so this is the filemon page from the Kernel Interfaces Manual β yes it's a section 4 man page, we love a kernel-adjacent moment π
πΈ NAME πΈ
filemon β the filemon device, and yes that's literally its whole name, she's simple, she's iconic
π¦ SYNOPSIS π¦
#include <dev/filemon/filemon.h>
π DESCRIPTION π
So filemon is this device that lets a process collect file operations data of its children. Parent behavior, honestly. The device lives at /dev/filemon and it responds to exactly two ioctl(2) calls. Two. That's it. Minimalist queen. β¨
π¨ okay important vibe check: filemon is not intended to be a security auditing tool. Not even a little bit. Many system calls are not tracked, and binaries of foreign ABI will not be fully audited. So do not come for her with your security dreams, that's not what she does.
What she is for: auditing processes to figure out their dependencies β in an efficient, easily parsable format. That's the assignment. The classic example is make(1), which uses this module with .MAKE.MODE=meta to handle incremental builds more smartly. periodt. π₯
System calls get denoted using these single letters, and you're gonna memorize them, I believe in you:
- βAβ β
openat(2). Heads up: the next log entry may be lacking an absolute path or be inaccurate. π - βCβ β
chdir(2) - βDβ β
unlink(2) - βEβ β
exec(2) - βFβ β
fork(2),vfork(2) - βLβ β
link(2),linkat(2),symlink(2),symlinkat(2) - βMβ β
rename(2) - βRβ β
open(2)oropenat(2)for read - βWβ β
open(2)oropenat(2)for write - βXβ β
_exit(2)
ngl this next bit is a lil sneaky: note that a βRβ following βWβ record can represent a single open(2) for R/W, OR two separate open(2) calls β one for βRβ and one for βWβ. Can't tell the difference, she's mysterious like that. π
Also, and this is important: only successful system calls are captured. If it failed, it never happened. main character energy honestly. π
π IOCTLS π
User mode programs talk to the filemon driver through a number of ioctls, described below. Each one takes a single argument. One argument. Don't be extra.
FILEMON_SET_FDβ write the internal tracing buffer to the supplied open file descriptor. that's the whole thing. π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. Read that again. After the fork, before anything else. See the example below, she shows you exactly how. β
π 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. Classic. The binary of outcomes. π«Ά
π ERRORS π
The ioctl() system call with FILEMON_SET_FD will fail if:
[EEXIST]β the filemon handle is already associated with a file descriptor. she's taken, bestie.
The ioctl() system call with FILEMON_SET_PID will fail if:
[ESRCH]β no process having the specified process ID exists. ghosted fr fr π»[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. also π
π§ FILES π§
/dev/filemon
π EXAMPLES π
okay here's the moment, here's the whole thing, copy it exactly, I'm not even joking, character for character:
#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 <unistd.h>
static void
open_filemon(void)
{
pid_t child;
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 {
wait(&child);
close(fm_fd);
}
}
So what's happening here? This creates a file named filemon.out and configures the filemon device to write the filemon buffer contents to it. She ate. No crumbs. π½οΈβ¨
π₯ SEE ALSO π₯
dtrace(1), ktrace(1), script(1), truss(1), ioctl(2)
π HISTORY π
A filemon device appeared in FreeBSD 9.1. she's been around, she's seen things. π¦
π BUGS π
β οΈ Unloading the module may panic the system, thus requires usingkldunload -f. Not a drill. Not a cute lil oopsie. A panic. The-fis mandatory, do not skip her. π
okay that's the whole page, you're welcome, go forth and trace some file operations responsibly πβ¨
β from the Debian edition, dated March 22, 2016, signing off ππ«Ά
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2012 David E. O'Brien <obrien@FreeBSD.org>. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms