func (m *Monitor) DeviceChan(done <-chan struct{}) (<-chan *Device, error) {
	var event unix.EpollEvent
	var events [maxEpollEvents]unix.EpollEvent
	m.lock()
	defer m.unlock()
	if C.udev_monitor_enable_receiving(m.ptr) != 0 {
		return nil, errors.New("udev: udev_monitor_enable_receiving failed")
	}
	fd := C.udev_monitor_get_fd(m.ptr)
	if e := unix.SetNonblock(int(fd), true); e != nil {
		return nil, errors.New("udev: unix.SetNonblock failed")
	}
	epfd, e := unix.EpollCreate1(0)
	if e != nil {
		return nil, errors.New("udev: unix.EpollCreate1 failed")
	}
	event.Events = unix.EPOLLIN | unix.EPOLLET
	event.Fd = int32(fd)
	if e = unix.EpollCtl(epfd, unix.EPOLL_CTL_ADD, int(fd), &event); e != nil {
		return nil, errors.New("udev: unix.EpollCtl failed")
	}
	ch := make(chan *Device)
	go func(done <-chan struct{}, fd int32) {
		defer unix.Close(epfd)
		defer close(ch)
		for {
			nevents, e := unix.EpollWait(epfd, events[:], epollTimeout)
			if e != nil {
				if errno, ok := e.(syscall.Errno); ok {
					if errno == syscall.EINTR {
						continue
					}
				}
				return
			}
			for ev := 0; ev < nevents; ev++ {
				if events[ev].Fd == fd {
					if (events[ev].Events & unix.EPOLLIN) != 0 {
						for d := m.receiveDevice(); d != nil; d = m.receiveDevice() {
							ch <- d
						}
					}
				}
			}
			select {
			case <-done:
				return
			default:
			}
		}
	}(done, int32(fd))
	return ch, nil
}
DeviceChan binds the udev_monitor socket to the event source and spawns a
goroutine. The goroutine efficiently waits on the monitor socket using epoll.
Data is received from the udev monitor socket and a new Device is created
with the data received. Pointers to the device are sent on the returned channel.
The function takes a done signalling channel as a parameter, which when
triggered will stop the goroutine and close the device channel.
Only socket connections with uid=0 are accepted.
package udev
import "C"
import (
	"errors"
	"syscall"
	"golang.org/x/sys/unix"
)
type Monitor struct {
	ptr *C.struct_udev_monitor
	u   *Udev
}
const (
	maxEpollEvents = 32
	epollTimeout   = 1000
)
func (m *Monitor) lock() {
	m.u.m.Lock()
}
func (m *Monitor) unlock() {
	m.u.m.Unlock()
}
func monitorUnref(m *Monitor) {
	C.udev_monitor_unref(m.ptr)
}
func (m *Monitor) SetReceiveBufferSize(size int) (err error) {
	m.lock()
	defer m.unlock()
	if C.udev_monitor_set_receive_buffer_size(m.ptr, (C.int)(size)) != 0 {
		err = errors.New("udev: udev_monitor_set_receive_buffer_size failed")
	}
	return
}
func (m *Monitor) FilterAddMatchSubsystem(subsystem string) (err error) {
	m.lock()
	defer m.unlock()
	s := C.CString(subsystem)
	defer freeCharPtr(s)
	if C.udev_monitor_filter_add_match_subsystem_devtype(m.ptr, s, nil) != 0 {
		err = errors.New("udev: udev_monitor_filter_add_match_subsystem_devtype failed")
	}
	return
}
func (m *Monitor) FilterAddMatchSubsystemDevtype(subsystem, devtype string) (err error) {
	m.lock()
	defer m.unlock()
	s, d := C.CString(subsystem), C.CString(devtype)
	defer freeCharPtr(s)
	defer freeCharPtr(d)
	if C.udev_monitor_filter_add_match_subsystem_devtype(m.ptr, s, d) != 0 {
		err = errors.New("udev: udev_monitor_filter_add_match_subsystem_devtype failed")
	}
	return
}
func (m *Monitor) FilterAddMatchTag(tag string) (err error) {
	m.lock()
	defer m.unlock()
	t := C.CString(tag)
	defer freeCharPtr(t)
	if C.udev_monitor_filter_add_match_tag(m.ptr, t) != 0 {
		err = errors.New("udev: udev_monitor_filter_add_match_tag failed")
	}
	return
}
func (m *Monitor) FilterUpdate() (err error) {
	m.lock()
	defer m.unlock()
	if C.udev_monitor_filter_update(m.ptr) != 0 {
		err = errors.New("udev: udev_monitor_filter_update failed")
	}
	return
}
func (m *Monitor) FilterRemove() (err error) {
	m.lock()
	defer m.unlock()
	if C.udev_monitor_filter_remove(m.ptr) != 0 {
		err = errors.New("udev: udev_monitor_filter_remove failed")
	}
	return
}
func (m *Monitor) receiveDevice() (d *Device) {
	m.lock()
	defer m.unlock()
	return m.u.newDevice(C.udev_monitor_receive_device(m.ptr))
}