File: notify_linux.go

package info (click to toggle)
docker.io 27.5.1%2Bdfsg4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,384 kB
  • sloc: sh: 5,847; makefile: 1,146; ansic: 664; python: 162; asm: 133
file content (54 lines) | stat: -rw-r--r-- 1,348 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package container

import (
	"context"
	"net"
	"syscall"

	"github.com/containerd/log"
	"github.com/docker/docker/internal/unix_noeintr"
	"golang.org/x/sys/unix"
)

func notifyClosed(ctx context.Context, conn net.Conn, notify func()) {
	sc, ok := conn.(syscall.Conn)
	if !ok {
		log.G(ctx).Debug("notifyClosed: conn does not support close notifications")
		return
	}

	rc, err := sc.SyscallConn()
	if err != nil {
		log.G(ctx).WithError(err).Warn("notifyClosed: failed get raw conn for close notifications")
		return
	}

	epFd, err := unix_noeintr.EpollCreate()
	if err != nil {
		log.G(ctx).WithError(err).Warn("notifyClosed: failed to create epoll fd")
		return
	}
	defer unix.Close(epFd)

	err = rc.Control(func(fd uintptr) {
		err := unix_noeintr.EpollCtl(epFd, unix.EPOLL_CTL_ADD, int(fd), &unix.EpollEvent{
			Events: unix.EPOLLHUP,
			Fd:     int32(fd),
		})
		if err != nil {
			log.G(ctx).WithError(err).Warn("notifyClosed: failed to register fd for close notifications")
			return
		}

		events := make([]unix.EpollEvent, 1)
		if _, err := unix_noeintr.EpollWait(epFd, events, -1); err != nil {
			log.G(ctx).WithError(err).Warn("notifyClosed: failed to wait for close notifications")
			return
		}
		notify()
	})
	if err != nil {
		log.G(ctx).WithError(err).Warn("notifyClosed: failed to register for close notifications")
		return
	}
}