// NewWatcher establishes a new watcher with the underlying OS and begins waiting for events.
func NewWatcher() (*Watcher, error) {
- poller, err := newFdPoller()
+ // Create inotify fd
+ fd, errno := syscall.InotifyInit()
+ if fd == -1 {
+ return nil, errno
+ }
+ // Create epoll
+ poller, err := newFdPoller(fd)
if err != nil {
return nil, err
}
// Create a new inotify poller.
// This creates an inotify handler, and an epoll handler.
-func newFdPoller() (*fdPoller, error) {
+func newFdPoller(fd int) (*fdPoller, error) {
var errno error
poller := new(fdPoller)
+ poller.fd = fd
- // Create inotify fd
- poller.fd, errno = syscall.InotifyInit()
- if poller.fd == -1 {
- return nil, errno
- }
// Create epoll fd
poller.epfd, errno = syscall.EpollCreate(1)
if poller.epfd == -1 {