Files
weave-scope/vendor/github.com/opencontainers/runc/libcontainer/notify_linux.go
Marc Carré a82ba60760 Update opencontainers/runc to v1.0.0-rc5
```
$ gvt delete github.com/opencontainers/runc/libcontainer/cgroups
$ gvt delete github.com/opencontainers/runc/libcontainer/configs
$ gvt delete github.com/opencontainers/runc/libcontainer/system
$ gvt delete github.com/opencontainers/runc/libcontainer/user
$ gvt delete github.com/opencontainers/runc/libcontainer/utils
$ gvt fetch --tag v1.0.0-rc5 github.com/opencontainers/runc/libcontainer
2018/07/23 17:08:18 Fetching: github.com/opencontainers/runc/libcontainer
2018/07/23 17:08:24 · Fetching recursive dependency: github.com/opencontainers/runc/vendor/github.com/vishvananda/netlink
2018/07/23 17:08:24 · Fetching recursive dependency: github.com/opencontainers/runc/vendor/golang.org/x/sys/unix
2018/07/23 17:08:24 · Fetching recursive dependency: github.com/opencontainers/runc/vendor/github.com/cyphar/filepath-securejoin
2018/07/23 17:08:24 ·· Fetching recursive dependency: github.com/opencontainers/runc/vendor/github.com/pkg/errors
2018/07/23 17:08:24 · Fetching recursive dependency: github.com/opencontainers/runc/vendor/github.com/opencontainers/selinux/go-selinux/label
2018/07/23 17:08:25 ·· Fetching recursive dependency: github.com/opencontainers/runc/vendor/github.com/opencontainers/selinux/go-selinux
2018/07/23 17:08:25 · Fetching recursive dependency: github.com/opencontainers/runc/vendor/github.com/containerd/console
2018/07/23 17:08:25 ·· Fetching recursive dependency: github.com/opencontainers/runc/vendor/golang.org/x/sys/windows
2018/07/23 17:08:25 · Fetching recursive dependency: github.com/opencontainers/runc/vendor/github.com/sirupsen/logrus
2018/07/23 17:08:25 · Fetching recursive dependency: github.com/opencontainers/runc/vendor/github.com/godbus/dbus
2018/07/23 17:08:25 · Fetching recursive dependency: github.com/opencontainers/runc/vendor/github.com/mrunalp/fileutils
2018/07/23 17:08:25 · Fetching recursive dependency: github.com/opencontainers/runc/vendor/github.com/coreos/go-systemd/util
2018/07/23 17:08:25 ·· Fetching recursive dependency: github.com/opencontainers/runc/vendor/github.com/coreos/pkg/dlopen
2018/07/23 17:08:25 · Fetching recursive dependency: github.com/opencontainers/runc/vendor/github.com/golang/protobuf/proto
2018/07/23 17:08:25 · Fetching recursive dependency: github.com/opencontainers/runc/vendor/github.com/syndtr/gocapability/capability
2018/07/23 17:08:25 · Fetching recursive dependency: github.com/opencontainers/runc/vendor/github.com/coreos/go-systemd/dbus
2018/07/23 17:08:25 · Fetching recursive dependency: github.com/opencontainers/runc/vendor/github.com/opencontainers/runtime-spec/specs-go
2018/07/23 17:08:25 · Fetching recursive dependency: github.com/opencontainers/runc/vendor/github.com/seccomp/libseccomp-golang
2018/07/23 17:08:25 · Fetching recursive dependency: github.com/opencontainers/runc/vendor/github.com/docker/go-units
```
2018-07-23 20:10:16 +02:00

91 lines
2.1 KiB
Go

// +build linux
package libcontainer
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"golang.org/x/sys/unix"
)
const oomCgroupName = "memory"
type PressureLevel uint
const (
LowPressure PressureLevel = iota
MediumPressure
CriticalPressure
)
func registerMemoryEvent(cgDir string, evName string, arg string) (<-chan struct{}, error) {
evFile, err := os.Open(filepath.Join(cgDir, evName))
if err != nil {
return nil, err
}
fd, err := unix.Eventfd(0, unix.EFD_CLOEXEC)
if err != nil {
evFile.Close()
return nil, err
}
eventfd := os.NewFile(uintptr(fd), "eventfd")
eventControlPath := filepath.Join(cgDir, "cgroup.event_control")
data := fmt.Sprintf("%d %d %s", eventfd.Fd(), evFile.Fd(), arg)
if err := ioutil.WriteFile(eventControlPath, []byte(data), 0700); err != nil {
eventfd.Close()
evFile.Close()
return nil, err
}
ch := make(chan struct{})
go func() {
defer func() {
eventfd.Close()
evFile.Close()
close(ch)
}()
buf := make([]byte, 8)
for {
if _, err := eventfd.Read(buf); err != nil {
return
}
// When a cgroup is destroyed, an event is sent to eventfd.
// So if the control path is gone, return instead of notifying.
if _, err := os.Lstat(eventControlPath); os.IsNotExist(err) {
return
}
ch <- struct{}{}
}
}()
return ch, nil
}
// notifyOnOOM returns channel on which you can expect event about OOM,
// if process died without OOM this channel will be closed.
func notifyOnOOM(paths map[string]string) (<-chan struct{}, error) {
dir := paths[oomCgroupName]
if dir == "" {
return nil, fmt.Errorf("path %q missing", oomCgroupName)
}
return registerMemoryEvent(dir, "memory.oom_control", "")
}
func notifyMemoryPressure(paths map[string]string, level PressureLevel) (<-chan struct{}, error) {
dir := paths[oomCgroupName]
if dir == "" {
return nil, fmt.Errorf("path %q missing", oomCgroupName)
}
if level > CriticalPressure {
return nil, fmt.Errorf("invalid pressure level %d", level)
}
levelStr := []string{"low", "medium", "critical"}[level]
return registerMemoryEvent(dir, "memory.pressure_level", levelStr)
}