forked from jianfengye/go-superviser
/
monitor_linux.go
41 lines (36 loc) · 966 Bytes
/
monitor_linux.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
package main
import (
"syscall"
"unsafe"
"log"
)
func monitor(root string, action chan uint32) {
fd, err := syscall.InotifyInit()
if fd == -1 || err != nil {
end <- true
return
}
flags := syscall.IN_MODIFY | syscall.IN_CREATE | syscall.IN_DELETE // 2/128/512
wd, _ := syscall.InotifyAddWatch(fd, root, uint32(flags))
if wd == -1 {
end <- true
return
}
var (
buf [syscall.SizeofInotifyEvent * 10]byte
n int
)
for {
n, _ = syscall.Read(fd, buf[0:])
if n > syscall.SizeofInotifyEvent {
var offset = 0
for offset < n {
raw := (*syscall.InotifyEvent)(unsafe.Pointer(&buf[offset]))
mask := uint32(raw.Mask)
offset = offset + int(raw.Len) + syscall.SizeofInotifyEvent
action <- mask
log.Println("action:", mask)
}
}
}
}