Skip to content

Commit ca10cbd

Browse files
authored
Support for Permission events (#7)
* Support for permission events * Update README refer to github.com/opcoder0/fanotify-examples
1 parent 555f346 commit ca10cbd

File tree

7 files changed

+233
-237
lines changed

7 files changed

+233
-237
lines changed

README.md

Lines changed: 5 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -12,126 +12,17 @@ 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 on a directory
15+
## Examples
1616

17-
```
18-
package main
19-
20-
import (
21-
"flag"
22-
"fmt"
23-
"os"
24-
25-
"github.com/opcoder0/fanotify"
26-
)
27-
28-
func main() {
29-
var listenPath string
30-
31-
flag.StringVar(&listenPath, "listen-path", "", "path to watch events")
32-
flag.Parse()
33-
if listenPath == "" {
34-
fmt.Println("missing listen path")
35-
os.Exit(1)
36-
}
37-
mountPoint := "/"
38-
listener, err := fanotify.NewListener(mountPoint, false)
39-
if err != nil {
40-
fmt.Println(err)
41-
os.Exit(1)
42-
}
43-
fmt.Println("Listening to events for:", listenPath)
44-
var eventTypes EventType
45-
eventTypes =
46-
fanotify.FileAccessed |
47-
fanotify.FileOrDirectoryAccessed |
48-
fanotify.FileModified |
49-
fanotify.FileOpenedForExec |
50-
fanotify.FileAttribChanged |
51-
fanotify.FileOrDirectoryAttribChanged |
52-
fanotify.FileCreated |
53-
fanotify.FileOrDirectoryCreated |
54-
fanotify.FileDeleted |
55-
fanotify.FileOrDirectoryDeleted |
56-
fanotify.WatchedFileDeleted |
57-
fanotify.WatchedFileOrDirectoryDeleted |
58-
fanotify.FileMovedFrom |
59-
fanotify.FileOrDirectoryMovedFrom |
60-
fanotify.FileMovedTo |
61-
fanotify.FileOrDirectoryMovedTo |
62-
fanotify.WatchedFileMoved |
63-
fanotify.WatchedFileOrDirectoryMoved
64-
listener.AddWatch(listenPath, eventTypes)
65-
go listener.Start()
66-
i := 1
67-
for event := range listener.Events {
68-
fmt.Println(event)
69-
if i == 5 {
70-
fmt.Println("Enough events. Stopping...")
71-
listener.Stop()
72-
break
73-
}
74-
i++
75-
}
76-
}
77-
```
78-
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
17+
Example code for different use-cases can be found here https://github.com/opcoder0/fanotify-examples
9418

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-
```
12619
## Known Issues
12720

12821
Certain flag combinations / event types cause issues with event reporting.
12922

130-
- `fanotify.FileCreated` (`unix.FAN_CREATE`) cannot be or-ed / combined with `fanotify.FileClosed` (`unix.FAN_CLOSE_WRITE` or `unix.FAN_CLOSE_NOWRITE`). The `fanotify` event notification group does not generate any event for this combination.
131-
132-
- Using `fanotify.FileOpened` with any of the event types containing `OrDirectory` (`unix.FAN_ONDIR`) causes an event flood for the directory and then stopping raising any events at all.
133-
134-
- `fanotifyFileOrDirectoryOpened` with any of the other event types causes an event flood for the directory and then stopping raising any events at all.
23+
- `fanotify.FileCreated` cannot be or-ed / combined with `fanotify.FileClosed`. The `fanotify` event notification group does not generate any event for this combination.
24+
- Using `fanotify.FileOpened` with any of the event types containing `OrDirectory` causes numerous duplicate events for the path.
25+
- `fanotifyFileOrDirectoryOpened` with any of the other event types causes numerous duplicate events for the path.
13526

13627
## Tests
13728

doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Package fanotify library provides a simple API to monitor filesystem for events.
1+
// Package fanotify library provides a simple API to monitor filesystem for notification and permission events.
22
//
33
// The listener is initialized with flags automatically based on the kernel version. The mark flag features that specify the
44
// the events to monitor a file/directory are validated and checked for valid combinations and validated against the kernel

doc_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import (
77
)
88

99
func ExampleNewListener() {
10-
if _, err := fanotify.NewListener("/", true); err != nil {
10+
if _, err := fanotify.NewListener("/", true, fanotify.PostContent); err != nil {
1111
log.Fatal("Cannot create listener for mount /", err)
1212
}
1313
}
1414

1515
func ExampleListener_AddWatch() {
1616
var listener *fanotify.Listener
17-
listener, err := fanotify.NewListener("/", false)
17+
listener, err := fanotify.NewListener("/", false, fanotify.PermissionNone)
1818
if err != nil {
1919
log.Fatal("Cannot create listener for mount /", err)
2020
}
@@ -25,7 +25,7 @@ func ExampleListener_AddWatch_all() {
2525
var listener *fanotify.Listener
2626
var eventTypes fanotify.EventType
2727

28-
listener, err := fanotify.NewListener("/", false)
28+
listener, err := fanotify.NewListener("/", false, fanotify.PermissionNone)
2929
if err != nil {
3030
log.Fatal("Cannot create listener for path /", err)
3131
}

0 commit comments

Comments
 (0)