package inotify import ( "errors" "golang.org/x/sys/unix" ) type poller struct { fd int epfd int } func newPoller(fd int) (*poller, error) { var err error p := &poller{fd: -1, epfd: -1} p.fd = fd p.epfd, err = unix.EpollCreate1(unix.EPOLL_CLOEXEC) if err != nil { return nil, err } event := unix.EpollEvent{ Fd: int32(p.fd), Events: unix.EPOLLIN | unix.EPOLLET | unix.EPOLLERR | unix.EPOLLHUP, } err = unix.EpollCtl(p.epfd, unix.EPOLL_CTL_ADD, p.fd, &event) if err != nil { return nil, err } return p, nil } func (p *poller) wait() (bool, error) { for { events := make([]unix.EpollEvent, 1) n, err := unix.EpollWait(p.epfd, events, -1) if err != nil { if err == unix.EINTR { continue } return false, err } if n == 0 { continue } event := events[0] if event.Events&unix.EPOLLHUP != 0 { return false, errors.New("epoll hup") } if event.Events&unix.EPOLLERR != 0 { return false, errors.New("epoll error") } if event.Events&unix.EPOLLIN != 0 { return true, nil } return false, errors.New("unkown epoll event") } } func (p *poller) close() { if p.fd != -1 { unix.Close(p.fd) } if p.epfd != -1 { unix.Close(p.epfd) } }