Skip to content

Commit

Permalink
Fix issue with marking mount point
Browse files Browse the repository at this point in the history
- Fix mark validation for marking mount point
- Fix issue in readEvent loop to increment metadata when fd != NO_FD
  case
- Update package documentation
- Update README
  • Loading branch information
opcoder0 committed Dec 10, 2022
1 parent d8c9419 commit 555f346
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 10 deletions.
49 changes: 48 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fanotify has features spanning different kernel versions -
- For Linux kernel versions 5.1 - 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.

## Example: Listener watching for events
## Example: Listener watching for events on a directory

```
package main
Expand Down Expand Up @@ -76,6 +76,53 @@ func main() {
}
```

## Example: Listener watching for events on a mount point

```
package main
import (
"flag"
"fmt"
"os"
"github.com/opcoder0/fanotify"
)
func main() {
var mountPoint string
flag.StringVar(&mountPoint, "mount-path", "", "mount point path")
flag.Parse()
if mountPoint == "" {
fmt.Println("missing mount path")
os.Exit(1)
}
listener, err := fanotify.NewListener(mountPoint, true)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Listening to events for:", mountPoint)
var eventTypes fanotify.EventType
eventTypes = fanotify.FileAccessed |
fanotify.FileOrDirectoryAccessed |
fanotify.FileModified |
fanotify.FileOpenedForExec |
fanotify.FileOpened
err = listener.MarkMount(eventTypes, false)
if err != nil {
fmt.Println("MarkMount:", err)
os.Exit(1)
}
go listener.Start()
for event := range listener.Events {
fmt.Println(event)
}
listener.Stop()
}
```
## Known Issues

Certain flag combinations / event types cause issues with event reporting.
Expand Down
29 changes: 20 additions & 9 deletions fanotify_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ type Listener struct {
// multiple listener instances need to be used.
//
// mountPoint can be any file/directory under the mount point being watched.
// Passing "true" to the entireMount parameter monitors the entire mount point for marked
// events. Passing "false" allows specifying multiple paths (files/directories)
// under this mount point for monitoring filesystem events.
// 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
Expand Down Expand Up @@ -150,17 +152,26 @@ func (l *Listener) Stop() {
// 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 the following events:
// [FileCreated], [FileAttribChanged], [FileMovedFrom], [FileMovedTo], [WatchedFileDeleted]
// Passing any of these flags in eventTypes will return [ErrInvalidFlagCombination] error
// 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].
func (l *Listener) MarkMount(eventTypes EventType, remove bool) error {
if l.entireMount == false {
return ErrWatchPath
}
if eventTypes.Has(FileCreated) ||
eventTypes.Has(FileAttribChanged) ||
eventTypes.Has(FileMovedFrom) ||
if eventTypes.Has(FileAttribChanged) ||
eventTypes.Has(FileOrDirectoryAttribChanged) ||
eventTypes.Has(FileCreated) ||
eventTypes.Has(FileOrDirectoryCreated) ||
eventTypes.Has(FileDeleted) ||
eventTypes.Has(FileOrDirectoryDeleted) ||
eventTypes.Has(WatchedFileDeleted) ||
eventTypes.Has(WatchedFileOrDirectoryDeleted) ||
eventTypes.Has(FileMovedTo) ||
eventTypes.Has(FileMovedFrom) ||
eventTypes.Has(WatchedFileMoved) ||
eventTypes.Has(WatchedFileDeleted) {
return ErrInvalidFlagCombination
}
Expand Down
3 changes: 3 additions & 0 deletions fanotify_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,9 @@ func (l *Listener) readEvents() error {
Pid: int(metadata.Pid),
}
l.Events <- event
i += int(metadata.Event_len)
n -= int(metadata.Event_len)
metadata = (*unix.FanotifyEventMetadata)(unsafe.Pointer(&buf[i]))
} else {
// fid (applicable to kernels 5.1+)
fid = (*fanotifyEventInfoFID)(unsafe.Pointer(&buf[i+int(metadata.Metadata_len)]))
Expand Down

0 comments on commit 555f346

Please sign in to comment.