FILEMON(4) | Device Drivers Manual | FILEMON(4) |
filemon
— the
filemon device
#include
<dev/filemon/filemon.h>
The filemon
device allows a process to
collect file operations data of its children. The device
/dev/filemon responds to two
ioctl(2) calls.
filemon
is not
intended to be a security auditing tool. Many system calls are not tracked
and binaries of foreign ABI will not be fully audited. It is intended for
auditing of processes for the purpose of determining its dependencies in 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
’C
’D
’E
’F
’L
’M
’R
’W
’X
’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
’. Note that only successful system
calls are captured.
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
FILEMON_SET_PID
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.
The ioctl
() system call with
FILEMON_SET_FD
will fail if:
EEXIST
]filemon
handle is already associated with a
file descriptor.The ioctl
() system call with
FILEMON_SET_PID
will fail if:
ESRCH
]EBUSY
]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
]ENAMETOOLONG
]#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); } }
Creates a file named filemon.out and
configures the filemon
device to write the
filemon
buffer contents to it.
A filemon
device appeared in
FreeBSD 9.1.
Unloading the module may panic the system, thus requires using
kldunload -f
.
March 22, 2016 | Debian |