Skip to content

Commit 555f346

Browse files
committed
Fix issue with marking mount point
- 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
1 parent d8c9419 commit 555f346

File tree

3 files changed

+71
-10
lines changed

3 files changed

+71
-10
lines changed

README.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fanotify has features spanning different kernel versions -
1212
- For Linux kernel versions 5.1 - 5.8 additional information about the underlying filesystem object is correlated to an event.
1313
- For Linux kernel version 5.9 or later the modified file name is made available in the event.
1414

15-
## Example: Listener watching for events
15+
## Example: Listener watching for events on a directory
1616

1717
```
1818
package main
@@ -76,6 +76,53 @@ func main() {
7676
}
7777
```
7878

79+
## Example: Listener watching for events on a mount point
80+
81+
```
82+
package main
83+
84+
import (
85+
"flag"
86+
"fmt"
87+
"os"
88+
89+
"github.com/opcoder0/fanotify"
90+
)
91+
92+
func main() {
93+
var mountPoint string
94+
95+
flag.StringVar(&mountPoint, "mount-path", "", "mount point path")
96+
flag.Parse()
97+
98+
if mountPoint == "" {
99+
fmt.Println("missing mount path")
100+
os.Exit(1)
101+
}
102+
listener, err := fanotify.NewListener(mountPoint, true)
103+
if err != nil {
104+
fmt.Println(err)
105+
os.Exit(1)
106+
}
107+
fmt.Println("Listening to events for:", mountPoint)
108+
var eventTypes fanotify.EventType
109+
eventTypes = fanotify.FileAccessed |
110+
fanotify.FileOrDirectoryAccessed |
111+
fanotify.FileModified |
112+
fanotify.FileOpenedForExec |
113+
fanotify.FileOpened
114+
err = listener.MarkMount(eventTypes, false)
115+
if err != nil {
116+
fmt.Println("MarkMount:", err)
117+
os.Exit(1)
118+
}
119+
go listener.Start()
120+
for event := range listener.Events {
121+
fmt.Println(event)
122+
}
123+
listener.Stop()
124+
}
125+
```
79126
## Known Issues
80127

81128
Certain flag combinations / event types cause issues with event reporting.

fanotify_api.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@ type Listener struct {
7474
// multiple listener instances need to be used.
7575
//
7676
// mountPoint can be any file/directory under the mount point being watched.
77-
// Passing "true" to the entireMount parameter monitors the entire mount point for marked
78-
// events. Passing "false" allows specifying multiple paths (files/directories)
79-
// under this mount point for monitoring filesystem events.
77+
// entireMount when "true" monitors the entire mount point for marked
78+
// events which includes all directories, subdirectories, and the
79+
// contained files of the mount point. Passing "false" allows specifying
80+
// multiple paths (files/directories)
81+
// under this mount point for monitoring filesystem events using AddWatch.
8082
//
8183
// The function returns a new instance of the listener. The fanotify flags are set
8284
// based on the running kernel version. [ErrCapSysAdmin] is returned if the process does not
@@ -150,17 +152,26 @@ func (l *Listener) Stop() {
150152
// mount point. Passing true to remove, removes the mark from the mount point.
151153
// This method returns an [ErrWatchPath] if the listener was not initialized to monitor
152154
// the entire mount point. To mark specific files or directories use [AddWatch] method.
153-
// The entire mount cannot be monitored for the following events:
154-
// [FileCreated], [FileAttribChanged], [FileMovedFrom], [FileMovedTo], [WatchedFileDeleted]
155-
// Passing any of these flags in eventTypes will return [ErrInvalidFlagCombination] error
155+
// The entire mount cannot be monitored for any events for which new directory modification
156+
// events are provided. Passing any of these directory modification flags in eventTypes
157+
// will return [ErrInvalidFlagCombination] error. Valid eventTypes are
158+
// [FileAccessed], [FileOrDirectoryAccessed], [FileModified], [FileOpenedForExec]
159+
// [FileOpened], [FileOrDirectoryOpened].
156160
func (l *Listener) MarkMount(eventTypes EventType, remove bool) error {
157161
if l.entireMount == false {
158162
return ErrWatchPath
159163
}
160-
if eventTypes.Has(FileCreated) ||
161-
eventTypes.Has(FileAttribChanged) ||
162-
eventTypes.Has(FileMovedFrom) ||
164+
if eventTypes.Has(FileAttribChanged) ||
165+
eventTypes.Has(FileOrDirectoryAttribChanged) ||
166+
eventTypes.Has(FileCreated) ||
167+
eventTypes.Has(FileOrDirectoryCreated) ||
168+
eventTypes.Has(FileDeleted) ||
169+
eventTypes.Has(FileOrDirectoryDeleted) ||
170+
eventTypes.Has(WatchedFileDeleted) ||
171+
eventTypes.Has(WatchedFileOrDirectoryDeleted) ||
163172
eventTypes.Has(FileMovedTo) ||
173+
eventTypes.Has(FileMovedFrom) ||
174+
eventTypes.Has(WatchedFileMoved) ||
164175
eventTypes.Has(WatchedFileDeleted) {
165176
return ErrInvalidFlagCombination
166177
}

fanotify_event.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,9 @@ func (l *Listener) readEvents() error {
385385
Pid: int(metadata.Pid),
386386
}
387387
l.Events <- event
388+
i += int(metadata.Event_len)
389+
n -= int(metadata.Event_len)
390+
metadata = (*unix.FanotifyEventMetadata)(unsafe.Pointer(&buf[i]))
388391
} else {
389392
// fid (applicable to kernels 5.1+)
390393
fid = (*fanotifyEventInfoFID)(unsafe.Pointer(&buf[i+int(metadata.Metadata_len)]))

0 commit comments

Comments
 (0)