From 7a9791f096c753d9a39bba4127782661035d1f44 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 11 Aug 2022 12:38:30 +0200 Subject: [PATCH] libcontainerd/supervisor: store location of pidFile Adding a remote.pidFile to store the location instead of re-constructing its location each time. Also performing a small refactor to use `strconv.Itoa` instead of `fmt.Sprintf`. Signed-off-by: Sebastiaan van Stijn --- libcontainerd/supervisor/remote_daemon.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libcontainerd/supervisor/remote_daemon.go b/libcontainerd/supervisor/remote_daemon.go index 2df2794b55..f5e35677d1 100644 --- a/libcontainerd/supervisor/remote_daemon.go +++ b/libcontainerd/supervisor/remote_daemon.go @@ -2,7 +2,6 @@ package supervisor // import "github.com/docker/docker/libcontainerd/supervisor" import ( "context" - "fmt" "io" "os" "os/exec" @@ -34,6 +33,7 @@ type remote struct { config.Config daemonPid int + pidFile string logger *logrus.Entry daemonWaitCh chan struct{} @@ -65,6 +65,7 @@ func Start(ctx context.Context, rootDir, stateDir string, opts ...DaemonOpt) (Da State: filepath.Join(stateDir, "daemon"), }, daemonPid: -1, + pidFile: filepath.Join(stateDir, pidFile), logger: logrus.WithField("module", "libcontainerd"), daemonStartCh: make(chan error, 1), daemonStopCh: make(chan struct{}), @@ -114,8 +115,7 @@ func (r *remote) Address() string { return r.GRPC.Address } func (r *remote) getContainerdPid() (int, error) { - pidFile := filepath.Join(r.stateDir, pidFile) - f, err := os.OpenFile(pidFile, os.O_RDWR, 0600) + f, err := os.OpenFile(r.pidFile, os.O_RDWR, 0600) if err != nil { if os.IsNotExist(err) { return -1, nil @@ -211,7 +211,7 @@ func (r *remote) startContainerd() error { r.logger.WithError(err).Warn("failed to adjust OOM score") } - err = os.WriteFile(filepath.Join(r.stateDir, pidFile), []byte(fmt.Sprintf("%d", r.daemonPid)), 0660) + err = os.WriteFile(r.pidFile, []byte(strconv.Itoa(r.daemonPid)), 0660) if err != nil { system.KillProcess(r.daemonPid) return errors.Wrap(err, "libcontainerd: failed to save daemon pid to disk") @@ -250,7 +250,7 @@ func (r *remote) monitorDaemon(ctx context.Context) { } // cleanup some files - os.Remove(filepath.Join(r.stateDir, pidFile)) + _ = os.Remove(r.pidFile) r.platformCleanup()