-
-
Notifications
You must be signed in to change notification settings - Fork 167
/
driver_fanotify.go
219 lines (170 loc) · 4.79 KB
/
driver_fanotify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package drivers
import (
"bytes"
"context"
"encoding/binary"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"golang.org/x/sys/unix"
"github.com/lxc/lxd/shared/logger"
)
var fanotifyLoaded bool
type fanotify struct {
common
fd int
}
type fanotifyEventInfoHeader struct {
InfoType uint8
Pad uint8
Len uint16
}
type fanotifyEventInfoFid struct {
fanotifyEventInfoHeader
FSID uint64
}
func (d *fanotify) Name() string {
return "fanotify"
}
func (d *fanotify) load(ctx context.Context) error {
if fanotifyLoaded {
return nil
}
var err error
d.fd, err = unix.FanotifyInit(unix.FAN_CLOEXEC|unix.FAN_REPORT_DFID_NAME, unix.O_CLOEXEC)
if err != nil {
return fmt.Errorf("Failed to initialize fanotify: %w", err)
}
err = unix.FanotifyMark(d.fd, unix.FAN_MARK_ADD|unix.FAN_MARK_FILESYSTEM, unix.FAN_CREATE|unix.FAN_DELETE|unix.FAN_ONDIR, unix.AT_FDCWD, d.prefixPath)
if err != nil {
_ = unix.Close(d.fd)
return fmt.Errorf("Failed to watch directory %q: %w", d.prefixPath, err)
}
fd, err := unix.Open(d.prefixPath, unix.O_DIRECTORY|unix.O_RDONLY|unix.O_CLOEXEC, 0)
if err != nil {
_ = unix.Close(d.fd)
return fmt.Errorf("Failed to open directory %q: %w", d.prefixPath, err)
}
go func() {
<-ctx.Done()
_ = unix.Close(d.fd)
fanotifyLoaded = false
}()
go d.getEvents(ctx, fd)
fanotifyLoaded = true
return nil
}
func (d *fanotify) getEvents(ctx context.Context, mountFd int) {
for {
buf := make([]byte, 256)
// Although the event is less than 256 bytes, we read as much to ensure the entire event
// is captured and following events are readable. Using only binary.Read() would require
// more manual cleanup as otherwise bytes from a previous event would still be present and
// make everything unreadable.
_, err := unix.Read(d.fd, buf)
if err != nil {
// Stop listening for events as the fanotify fd has been closed due to cleanup.
if ctx.Err() != nil || errors.Is(err, unix.EBADF) {
_ = unix.Close(mountFd)
return
}
d.logger.Error("Failed to read event", logger.Ctx{"err": err})
continue
}
rd := bytes.NewReader(buf)
event := unix.FanotifyEventMetadata{}
err = binary.Read(rd, binary.LittleEndian, &event)
if err != nil {
d.logger.Error("Failed to read event metadata", logger.Ctx{"err": err})
continue
}
// Read event info fid
fid := fanotifyEventInfoFid{}
err = binary.Read(rd, binary.LittleEndian, &fid)
if err != nil {
d.logger.Error("Failed to read event fid", logger.Ctx{"err": err})
continue
}
// Although unix.FileHandle exists, it cannot be used with binary.Read() as the
// variables inside are not exported.
type fileHandleInfo struct {
Bytes uint32
Type int32
}
// Read file handle information
fhInfo := fileHandleInfo{}
err = binary.Read(rd, binary.LittleEndian, &fhInfo)
if err != nil {
d.logger.Error("Failed to read file handle info", logger.Ctx{"err": err})
continue
}
// Read file handle
fileHandle := make([]byte, fhInfo.Bytes)
err = binary.Read(rd, binary.LittleEndian, fileHandle)
if err != nil {
d.logger.Error("Failed to read file handle", logger.Ctx{"err": err})
continue
}
fh := unix.NewFileHandle(fhInfo.Type, fileHandle)
fd, err := unix.OpenByHandleAt(mountFd, fh, 0)
if err != nil {
errno := err.(unix.Errno)
if errno != unix.ESTALE {
d.logger.Error("Failed to open file", logger.Ctx{"err": err})
}
continue
}
unix.CloseOnExec(fd)
// Determine the directory of the created or deleted file.
target, err := os.Readlink(fmt.Sprintf("/proc/self/fd/%d", fd))
if err != nil {
d.logger.Error("Failed to read symlink", logger.Ctx{"err": err})
continue
}
_ = unix.Close(fd)
// If the target file has been deleted, the returned value might contain a " (deleted)" suffix.
// This needs to be removed.
target = strings.TrimSuffix(target, " (deleted)")
// The file handle is followed by a null terminated string that identifies the
// created/deleted directory entry name.
sb := strings.Builder{}
sb.WriteString(target + "/")
for {
b, err := rd.ReadByte()
if err != nil || b == 0 {
break
}
err = sb.WriteByte(b)
if err != nil {
break
}
}
eventPath := filepath.Clean(sb.String())
// Check whether there's a watch on a specific file or directory.
d.mu.Lock()
for path := range d.watches {
if eventPath != path {
continue
}
var action Event
if event.Mask&unix.FAN_CREATE != 0 {
action = Add
} else if event.Mask&unix.FAN_DELETE != 0 || event.Mask&unix.FAN_DELETE_SELF != 0 {
action = Remove
}
for identifier, f := range d.watches[path] {
ret := f(path, action.String())
if !ret {
delete(d.watches[path], identifier)
if len(d.watches[path]) == 0 {
delete(d.watches, path)
}
}
}
break
}
d.mu.Unlock()
}
}