diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2021-07-02 20:16:03 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-02 20:16:03 -0400 |
commit | 878a2231f9eae037d15bc27c50cdf3a9418793c7 (patch) | |
tree | 8611adc09be6ba26b9a5388e9e576a6b4dbdfc44 /pkg | |
parent | fc2f9cc3e6f923fcb09b83414e222574228ec94e (diff) | |
parent | ed51e3f54898128e68330de395736c80868100e8 (diff) | |
download | podman-878a2231f9eae037d15bc27c50cdf3a9418793c7.tar.gz podman-878a2231f9eae037d15bc27c50cdf3a9418793c7.tar.bz2 podman-878a2231f9eae037d15bc27c50cdf3a9418793c7.zip |
Merge pull request #10851 from Luap99/service-reaper
podman service reaper
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/servicereaper/service.go | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/pkg/servicereaper/service.go b/pkg/servicereaper/service.go new file mode 100644 index 000000000..e9c4fe908 --- /dev/null +++ b/pkg/servicereaper/service.go @@ -0,0 +1,64 @@ +//+build linux + +package servicereaper + +import ( + "os" + "os/signal" + "sync" + "syscall" + + "github.com/sirupsen/logrus" +) + +type service struct { + pidMap map[int]bool + mutex *sync.Mutex +} + +var s = service{ + pidMap: map[int]bool{}, + mutex: &sync.Mutex{}, +} + +func AddPID(pid int) { + s.mutex.Lock() + s.pidMap[pid] = true + s.mutex.Unlock() +} + +func Start() { + // create signal channel and only wait for SIGCHLD + sigc := make(chan os.Signal, 1) + signal.Notify(sigc, syscall.SIGCHLD) + // wait and reap in an extra goroutine + go reaper(sigc) +} + +func reaper(sigc chan os.Signal) { + for { + // block until we receive SIGCHLD + <-sigc + s.mutex.Lock() + for pid := range s.pidMap { + var status syscall.WaitStatus + waitpid, err := syscall.Wait4(pid, &status, syscall.WNOHANG, nil) + if err != nil { + // do not log error for ECHILD + if err != syscall.ECHILD { + logrus.Warnf("wait for pid %d failed: %v ", pid, err) + } + delete(s.pidMap, pid) + continue + } + // if pid == 0 nothing happened + if waitpid == 0 { + continue + } + if status.Exited() { + delete(s.pidMap, pid) + } + } + s.mutex.Unlock() + } +} |