MCPcopy
hub / github.com/opencontainers/runc / registerMemoryEvent

Function registerMemoryEvent

libcontainer/notify_linux.go:20–61  ·  view source on GitHub ↗
(cgDir, evName, arg string)

Source from the content-addressed store, hash-verified

18)
19
20func registerMemoryEvent(cgDir, evName, arg string) (<-chan struct{}, error) {
21 evFile, err := os.Open(filepath.Join(cgDir, evName))
22 if err != nil {
23 return nil, err
24 }
25 fd, err := unix.Eventfd(0, unix.EFD_CLOEXEC)
26 if err != nil {
27 evFile.Close()
28 return nil, err
29 }
30
31 eventfd := os.NewFile(uintptr(fd), "eventfd")
32
33 eventControlPath := filepath.Join(cgDir, "cgroup.event_control")
34 data := fmt.Sprintf("%d %d %s", eventfd.Fd(), evFile.Fd(), arg)
35 if err := os.WriteFile(eventControlPath, []byte(data), 0o700); err != nil {
36 eventfd.Close()
37 evFile.Close()
38 return nil, err
39 }
40 ch := make(chan struct{})
41 go func() {
42 defer func() {
43 eventfd.Close()
44 evFile.Close()
45 close(ch)
46 }()
47 buf := make([]byte, 8)
48 for {
49 if _, err := eventfd.Read(buf); err != nil {
50 return
51 }
52 // When a cgroup is destroyed, an event is sent to eventfd.
53 // So if the control path is gone, return instead of notifying.
54 if _, err := os.Lstat(eventControlPath); errors.Is(err, os.ErrNotExist) {
55 return
56 }
57 ch <- struct{}{}
58 }
59 }()
60 return ch, nil
61}
62
63// notifyOnOOM returns channel on which you can expect event about OOM,
64// if process died without OOM this channel will be closed.

Callers 2

notifyOnOOMFunction · 0.85
notifyMemoryPressureFunction · 0.85

Calls 1

CloseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…