2016-09-22 21:03:45 +00:00
|
|
|
package monitor
|
|
|
|
|
|
|
|
import (
|
2016-10-06 22:06:34 +00:00
|
|
|
"io"
|
2016-09-22 21:03:45 +00:00
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-09-22 21:08:41 +00:00
|
|
|
"github.com/docker/containerd/epoll"
|
2016-09-22 21:03:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Monitorable interface {
|
|
|
|
FD() int
|
2016-10-06 22:06:34 +00:00
|
|
|
// Remove returns true if the monitorable should be removed
|
|
|
|
// from the event monitor under the lock of when the event was received
|
|
|
|
Remove() bool
|
2016-09-22 21:03:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Flusher interface {
|
|
|
|
Flush() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new process monitor that emits events whenever the
|
|
|
|
// state of the fd refering to a process changes
|
|
|
|
func New() (*Monitor, error) {
|
2016-09-22 21:08:41 +00:00
|
|
|
fd, err := epoll.EpollCreate1(0)
|
2016-09-22 21:03:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Monitor{
|
|
|
|
epollFd: fd,
|
|
|
|
receivers: make(map[int]Monitorable),
|
|
|
|
events: make(chan Monitorable, 1024),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Monitor struct {
|
|
|
|
m sync.Mutex
|
|
|
|
receivers map[int]Monitorable
|
|
|
|
events chan Monitorable
|
|
|
|
epollFd int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Events returns a chan that receives a Monitorable when it's FD changes state
|
|
|
|
func (m *Monitor) Events() chan Monitorable {
|
|
|
|
return m.events
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds a process to the list of the one being monitored
|
|
|
|
func (m *Monitor) Add(ma Monitorable) error {
|
|
|
|
m.m.Lock()
|
|
|
|
defer m.m.Unlock()
|
|
|
|
fd := ma.FD()
|
|
|
|
event := syscall.EpollEvent{
|
|
|
|
Fd: int32(fd),
|
|
|
|
Events: syscall.EPOLLHUP,
|
|
|
|
}
|
2016-09-22 21:08:41 +00:00
|
|
|
if err := epoll.EpollCtl(m.epollFd, syscall.EPOLL_CTL_ADD, fd, &event); err != nil {
|
2016-09-22 21:03:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.receivers[fd] = ma
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove deletes the Monitorable type from the monitor so that
|
|
|
|
// no other events are generated
|
|
|
|
func (m *Monitor) Remove(ma Monitorable) error {
|
|
|
|
m.m.Lock()
|
|
|
|
defer m.m.Unlock()
|
2016-10-06 22:06:34 +00:00
|
|
|
return m.remove(ma)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Monitor) remove(ma Monitorable) error {
|
2016-09-22 21:03:45 +00:00
|
|
|
fd := ma.FD()
|
|
|
|
delete(m.receivers, fd)
|
|
|
|
return syscall.EpollCtl(m.epollFd, syscall.EPOLL_CTL_DEL, fd, &syscall.EpollEvent{
|
|
|
|
Events: syscall.EPOLLHUP,
|
|
|
|
Fd: int32(fd),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close cleans up resources allocated to the Monitor
|
|
|
|
func (m *Monitor) Close() error {
|
|
|
|
return syscall.Close(m.epollFd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Monitor) Run() {
|
|
|
|
var events [128]syscall.EpollEvent
|
|
|
|
for {
|
2016-09-22 21:08:41 +00:00
|
|
|
n, err := epoll.EpollWait(m.epollFd, events[:], -1)
|
2016-09-22 21:03:45 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == syscall.EINTR {
|
|
|
|
continue
|
|
|
|
}
|
2016-10-06 22:06:34 +00:00
|
|
|
logrus.WithField("error", err).Fatal("shim: epoll wait")
|
2016-09-22 21:03:45 +00:00
|
|
|
}
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
fd := int(events[i].Fd)
|
|
|
|
m.m.Lock()
|
|
|
|
r := m.receivers[fd]
|
|
|
|
if f, ok := r.(Flusher); ok {
|
|
|
|
if err := f.Flush(); err != nil {
|
2016-10-06 22:06:34 +00:00
|
|
|
logrus.WithField("error", err).Fatal("shim: flush event FD")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if r.Remove() {
|
|
|
|
if err := m.remove(r); err != nil {
|
|
|
|
logrus.WithField("error", err).Fatal("shim: remove event FD")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if f, ok := r.(io.Closer); ok {
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
logrus.WithField("error", err).Fatal("shim: close event FD")
|
2016-09-22 21:03:45 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-06 22:06:34 +00:00
|
|
|
m.m.Unlock()
|
2016-09-22 21:03:45 +00:00
|
|
|
m.events <- r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|