Documentation
¶
Overview ¶
Package fanotify library provides a simple API to monitor filesystem for events.
The listener is initialized with flags automatically based on the kernel version. The mark flag features that specify the the events to monitor a file/directory are validated and checked for valid combinations and validated against the kernel version.
fanotify system has features spanning different kernel versions:
- For Linux kernel version 5.0 and earlier no additional information about the underlying filesystem object is available.
- For Linux kernel versions 5.1 to 5.8 additional information about the underlying filesystem object is correlated to an event.
- For Linux kernel version 5.9 or later the modified file name is made available in the event.
Index ¶
- Variables
- type Event
- type EventType
- type Listener
- func (l *Listener) AddWatch(path string, eventTypes EventType) error
- func (l *Listener) ClearWatch() error
- func (l *Listener) DeleteWatch(parentDir string, eventTypes EventType) error
- func (l *Listener) MarkMount(eventTypes EventType, remove bool) error
- func (l *Listener) Start()
- func (l *Listener) Stop()
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrCapSysAdmin indicates caller is missing CAP_SYS_ADMIN permissions ErrCapSysAdmin = errors.New("require CAP_SYS_ADMIN capability") // ErrInvalidFlagCombination indicates the bit/combination of flags are invalid ErrInvalidFlagCombination = errors.New("invalid flag bitmask") // ErrNilListener indicates the listener is nil ErrNilListener = errors.New("nil listener") // ErrUnsupportedOnKernelVersion indicates the feature/flag is unavailable for the current kernel version ErrUnsupportedOnKernelVersion = errors.New("feature unsupported on current kernel version") // ErrWatchPath indicates path needs to be specified for watching ErrWatchPath = errors.New("missing watch path") )
Functions ¶
This section is empty.
Types ¶
type Event ¶
type Event struct {
// Fd is the open file descriptor for the file/directory being watched
Fd int
// Path holds the name of the parent directory
Path string
// FileName holds the name of the file under the watched parent. The value is only available
// on kernels 5.1 or greater (that support the receipt of events which contain additional information
// about the underlying filesystem object correlated to an event).
FileName string
// EventTypes holds bit mask representing the operations
EventTypes EventType
// Pid Process ID of the process that caused the event
Pid int
}
Event represents a notification from the kernel for the file, directory or a filesystem marked for watching.
type EventType ¶ added in v0.2.0
type EventType uint64
EventType represents an event / operation on a particular file/directory
const ( // FileAccessed event when a file is accessed FileAccessed EventType = unix.FAN_ACCESS // FileOrDirectoryAccessed event when a file or directory is accessed FileOrDirectoryAccessed EventType = unix.FAN_ACCESS | unix.FAN_ONDIR // FileModified event when a file is modified FileModified EventType = unix.FAN_MODIFY // FileClosedAfterWrite event when a file is closed FileClosedAfterWrite EventType = unix.FAN_CLOSE_WRITE // FileClosedWithNoWrite event when a file is closed without writing FileClosedWithNoWrite EventType = unix.FAN_CLOSE_NOWRITE // FileClosed event when a file is closed after write or no write FileClosed EventType = unix.FAN_CLOSE_WRITE | unix.FAN_CLOSE_NOWRITE // FileOpened event when a file is opened // BUG Using FileOpened flag with any OrDirectory event types // causes an event flood and complete stoppage of events. The flag // can be used with other file only flags or by itself // without any errors/issues. FileOpened EventType = unix.FAN_OPEN // FileOrDirectoryOpened event when a file or directory is opened // BUG Using FileOrDirectoryOpened causes an event flood and complete // stoppage of events. The flag by itself without any errors/issues. FileOrDirectoryOpened EventType = unix.FAN_OPEN | unix.FAN_ONDIR // FileOpenedForExec event when a file is opened with the intent to be executed. // Requires Linux kernel 5.0 or later FileOpenedForExec EventType = unix.FAN_OPEN_EXEC // FileAttribChanged event when a file attribute has changed // Requires Linux kernel 5.1 or later (requires FID) FileAttribChanged EventType = unix.FAN_ATTRIB // FileOrDirectoryAttribChanged event when a file or directory attribute has changed // Requires Linux kernel 5.1 or later (requires FID) FileOrDirectoryAttribChanged EventType = unix.FAN_ATTRIB | unix.FAN_ONDIR // FileCreated event when file a has been created // Requires Linux kernel 5.1 or later (requires FID) // BUG FileCreated does not work with FileClosed, FileClosedAfterWrite or FileClosedWithNoWrite FileCreated EventType = unix.FAN_CREATE // FileOrDirectoryCreated event when a file or directory has been created // Requires Linux kernel 5.1 or later (requires FID) FileOrDirectoryCreated EventType = unix.FAN_CREATE | unix.FAN_ONDIR // FileDeleted event when file a has been deleted // Requires Linux kernel 5.1 or later (requires FID) FileDeleted EventType = unix.FAN_DELETE // FileOrDirectoryDeleted event when a file or directory has been deleted // Requires Linux kernel 5.1 or later (requires FID) FileOrDirectoryDeleted EventType = unix.FAN_DELETE | unix.FAN_ONDIR // WatchedFileDeleted event when a watched file has been deleted // Requires Linux kernel 5.1 or later (requires FID) WatchedFileDeleted EventType = unix.FAN_DELETE_SELF // WatchedFileOrDirectoryDeleted event when a watched file or directory has been deleted // Requires Linux kernel 5.1 or later (requires FID) WatchedFileOrDirectoryDeleted EventType = unix.FAN_DELETE_SELF | unix.FAN_ONDIR // FileMovedFrom event when a file has been moved from the watched directory // Requires Linux kernel 5.1 or later (requires FID) FileMovedFrom EventType = unix.FAN_MOVED_FROM // FileOrDirectoryMovedFrom event when a file or directory has been moved from the watched directory // Requires Linux kernel 5.1 or later (requires FID) FileOrDirectoryMovedFrom EventType = unix.FAN_MOVED_FROM | unix.FAN_ONDIR // FileMovedTo event when a file has been moved to the watched directory // Requires Linux kernel 5.1 or later (requires FID) FileMovedTo EventType = unix.FAN_MOVED_TO // FileOrDirectoryMovedTo event when a file or directory has been moved to the watched directory // Requires Linux kernel 5.1 or later (requires FID) FileOrDirectoryMovedTo EventType = unix.FAN_MOVED_TO | unix.FAN_ONDIR // WatchedFileMoved event when a watched file has moved // Requires Linux kernel 5.1 or later (requires FID) WatchedFileMoved EventType = unix.FAN_MOVE_SELF // WatchedFileOrDirectoryMoved event when a watched file or directory has moved // Requires Linux kernel 5.1 or later (requires FID) WatchedFileOrDirectoryMoved EventType = unix.FAN_MOVE_SELF | unix.FAN_ONDIR )
func (EventType) Has ¶ added in v0.2.0
Has returns true if event types (e) contains the passed in event type (et).
type Listener ¶
type Listener struct {
// Events a buffered channel holding fanotify notifications for the watched file/directory.
Events chan Event
// contains filtered or unexported fields
}
Listener represents a fanotify notification group that holds a list of files, directories or a mountpoint for which events shall be created.
func NewListener ¶
NewListener returns a fanotify listener from which filesystem events can be read. Each listener supports listening to events under a single mountpoint.
For cases where multiple mountpoints need to be monitored multiple listener instances need to be used.
mountPoint can be any file/directory under the mount point being watched. entireMount when "true" monitors the entire mount point for marked events which includes all directories, subdirectories, and the contained files of the mount point. Passing "false" allows specifying multiple paths (files/directories) under this mount point for monitoring filesystem events using AddWatch.
The function returns a new instance of the listener. The fanotify flags are set based on the running kernel version. ErrCapSysAdmin is returned if the process does not have CAP_SYS_ADM capability.
- For Linux kernel version 5.0 and earlier no additional information about the underlying filesystem object is available.
- For Linux kernel versions 5.1 till 5.8 (inclusive) additional information about the underlying filesystem object is correlated to an event.
- For Linux kernel version 5.9 or later the modified file name is made available in the event.
Example ¶
package main
import (
"log"
"github.com/opcoder0/fanotify"
)
func main() {
if _, err := fanotify.NewListener("/", true); err != nil {
log.Fatal("Cannot create listener for mount /", err)
}
}
Output:
func (*Listener) AddWatch ¶
AddWatch adds or modifies the fanotify mark for the specified path. The events are only raised for the specified directory and does raise events for subdirectories. Calling AddWatch to mark the entire mountpoint results in os.ErrInvalid. To mark the entire mount point use [MarkMount] method. Certain flag combinations are known to cause issues.
- FileCreated cannot be or-ed / combined with FileClosed. The fanotify system does not generate any event for this combination.
- FileOpened with any of the event types containing OrDirectory causes an event flood for the directory and then stopping raising any events at all.
- FileOrDirectoryOpened with any of the other event types causes an event flood for the directory and then stopping raising any events at all.
Example ¶
package main
import (
"log"
"github.com/opcoder0/fanotify"
)
func main() {
var listener *fanotify.Listener
listener, err := fanotify.NewListener("/", false)
if err != nil {
log.Fatal("Cannot create listener for mount /", err)
}
listener.AddWatch("/home/user", fanotify.FileModified)
}
Output:
Example (All) ¶
package main
import (
"log"
"github.com/opcoder0/fanotify"
)
func main() {
var listener *fanotify.Listener
var eventTypes fanotify.EventType
listener, err := fanotify.NewListener("/", false)
if err != nil {
log.Fatal("Cannot create listener for path /", err)
}
eventTypes = fanotify.FileAccessed |
fanotify.FileOrDirectoryAccessed |
fanotify.FileModified |
fanotify.FileOpenedForExec |
fanotify.FileAttribChanged |
fanotify.FileOrDirectoryAttribChanged |
fanotify.FileCreated |
fanotify.FileOrDirectoryCreated |
fanotify.FileDeleted |
fanotify.FileOrDirectoryDeleted |
fanotify.WatchedFileDeleted |
fanotify.WatchedFileOrDirectoryDeleted |
fanotify.FileMovedFrom |
fanotify.FileOrDirectoryMovedFrom |
fanotify.FileMovedTo |
fanotify.FileOrDirectoryMovedTo |
fanotify.WatchedFileMoved |
fanotify.WatchedFileOrDirectoryMoved
listener.AddWatch("/home/user", eventTypes)
}
Output:
func (*Listener) ClearWatch ¶
ClearWatch stops watching for all event types
func (*Listener) DeleteWatch ¶
DeleteWatch removes or modifies the fanotify mark for the specified path. Calling DeleteWatch on the listener initialized to monitor the entire mount point results in os.ErrInvalid. To modify the mark for the entire mount point use [MarkMount] method.
func (*Listener) MarkMount ¶ added in v0.2.0
MarkMount adds, modifies or removes the fanotify mark (eventTypes) for the entire mount point. Passing true to remove, removes the mark from the mount point. This method returns an ErrWatchPath if the listener was not initialized to monitor the entire mount point. To mark specific files or directories use [AddWatch] method. The entire mount cannot be monitored for any events for which new directory modification events are provided. Passing any of these directory modification flags in eventTypes will return ErrInvalidFlagCombination error. Valid eventTypes are FileAccessed, FileOrDirectoryAccessed, FileModified, FileOpenedForExec FileOpened, FileOrDirectoryOpened.