From c26316153098e72a9b30668befc36fcfcba3b76f Mon Sep 17 00:00:00 2001 From: jingrui Date: Sat, 23 Feb 2019 15:55:21 +0800 Subject: [PATCH 26/27] exit: optimize init.exit record Change-Id: If1319f7d87defed16d1113337957f36b7320e9b9 Signed-off-by: jingrui --- events/exit.go | 21 +++++++++++++++++++++ runtime/v1/linux/proc/init.go | 1 - runtime/v1/linux/runtime.go | 2 +- runtime/v1/shim/service.go | 2 +- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/events/exit.go b/events/exit.go index e1ce089..772dc24 100644 --- a/events/exit.go +++ b/events/exit.go @@ -7,11 +7,13 @@ import ( "path/filepath" "strconv" "strings" + "github.com/sirupsen/logrus" ) const ExitDir = "/var/run/docker/containerd/exit" const ExitStatusDefault = 137 +const InitExit = "init.exit" func ExitFile(cid string, pid uint32, status uint32) string { return fmt.Sprintf("%s.%d.%d", cid, pid, status) @@ -77,3 +79,22 @@ func ExitPending(ns string, cid string, pid uint32) bool { } return false } + +func InitExitWrite(bundle string, pid int) { + if _, err := os.Stat(bundle); err != nil { + logrus.Infof("skip write init.exit %s error=%v", bundle, err) + return + } + err := ioutil.WriteFile(filepath.Join(bundle, InitExit), []byte(fmt.Sprintf("%d", pid)), 0600) + if err != nil { + logrus.Infof("failed write init.exit error=%s", bundle, err) + } +} + +func InitExitExist(bundle string) bool { + if _, err := os.Stat(filepath.Join(bundle, InitExit)); err == nil { + return true + } + return false +} + diff --git a/runtime/v1/linux/proc/init.go b/runtime/v1/linux/proc/init.go index caa31c3..5b23671 100644 --- a/runtime/v1/linux/proc/init.go +++ b/runtime/v1/linux/proc/init.go @@ -43,7 +43,6 @@ import ( // InitPidFile name of the file that contains the init pid const InitPidFile = "init.pid" -const InitExit = "init.exit" // Init represents an initial process for a container type Init struct { diff --git a/runtime/v1/linux/runtime.go b/runtime/v1/linux/runtime.go index 5647f94..e92904e 100644 --- a/runtime/v1/linux/runtime.go +++ b/runtime/v1/linux/runtime.go @@ -429,7 +429,7 @@ func (r *Runtime) loadTasks(ctx context.Context, ns string) ([]*Task, error) { log.G(ctx).Warnf("skip load task in creating %s", id) continue } - if _, err := os.Stat(filepath.Join(bundle.path, proc.InitExit)); err == nil { + if events.InitExitExist(bundle.path) { if !events.ExitPending(ns, t.id, uint32(pid)) { events.ExitAddFile(ns, events.ExitFile(t.id, uint32(pid), uint32(events.ExitStatusDefault)), "cleanup dirty task") } diff --git a/runtime/v1/shim/service.go b/runtime/v1/shim/service.go index d7fdcaf..f421fde 100644 --- a/runtime/v1/shim/service.go +++ b/runtime/v1/shim/service.go @@ -513,7 +513,7 @@ func (s *Service) checkProcesses(e runc.Exit) { if ip, ok := p.(*proc.Init); ok { ns := filepath.Base(filepath.Dir(ip.Bundle)) events.ExitAddFile(ns, events.ExitFile(s.id, uint32(e.Pid), uint32(e.Status)), "init exited") - ioutil.WriteFile(filepath.Join(ip.Bundle, proc.InitExit), []byte(fmt.Sprintf("%d", e.Pid)), 0600) + events.InitExitWrite(ip.Bundle, e.Pid) } if shouldKillAll { if ip, ok := p.(*proc.Init); ok { -- 2.7.4.3