-
Notifications
You must be signed in to change notification settings - Fork 2
/
mountcap.go
83 lines (78 loc) · 1.81 KB
/
mountcap.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
package mountcap
import (
"github.com/golang/glog"
syscall "golang.org/x/sys/unix"
"os"
"unsafe"
)
func PollMount(changed chan bool, quit chan error) {
f, _ := os.Open("/proc/self/mountinfo")
defer f.Close()
fdOri := f.Fd()
var fd *int32 = (*int32)(unsafe.Pointer(&fdOri))
pollFds := make([]syscall.PollFd, 1)
pollFds[0] = syscall.PollFd{
Fd: *fd,
Events: syscall.POLLERR | syscall.POLLPRI,
Revents: 0,
}
for {
//set a minute so that this loop will check if it should exit
ret, _ := syscall.Poll(pollFds, 60000)
if ret >= 0 {
if (pollFds[0].Revents & syscall.POLLERR) == 8 {
changed <- true
}
}
pollFds[0].Revents = 0
select {
case <-quit:
glog.Infoln("recive quit msg.")
return
default:
//do nothing and go next poll.
}
}
}
func PollMountEver() bool {
f, _ := os.Open("/proc/self/mountinfo")
defer f.Close()
fdOri := f.Fd()
var fd *int32 = (*int32)(unsafe.Pointer(&fdOri))
pollFds := make([]syscall.PollFd, 1)
pollFds[0] = syscall.PollFd{
Fd: *fd,
Events: syscall.POLLERR | syscall.POLLPRI,
Revents: 0,
}
for {
//set a minute so that this loop will check if it should exit
ret, _ := syscall.Poll(pollFds, 6000)
if ret >= 0 {
if (pollFds[0].Revents & syscall.POLLERR) == 8 {
return true
}
}
pollFds[0].Revents = 0
}
}
func PollMountWithTimeout(msTimeout int) bool {
f, _ := os.Open("/proc/self/mountinfo")
defer f.Close()
fdOri := f.Fd()
var fd *int32 = (*int32)(unsafe.Pointer(&fdOri))
pollFds := make([]syscall.PollFd, 1)
pollFds[0] = syscall.PollFd{
Fd: *fd,
Events: syscall.POLLERR | syscall.POLLPRI,
Revents: 0,
}
//set a minute so that this loop will check if it should exit
ret, _ := syscall.Poll(pollFds, msTimeout)
if ret >= 0 {
if (pollFds[0].Revents & syscall.POLLERR) == 8 {
return true
}
}
return false
}