From 7d3e1ad943b1135d7323fc319ab0de4660180e6c Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 15 Oct 2022 20:56:48 +0200 Subject: [PATCH] pkg/pidfile: Write(): don't automatically create parent directories While this was convenient for our use, it's somewhat unexpected for a function that writes a file to also create all parent directories; even more because this function may be executed as root. This patch makes the package more "safe" to use as a generic package by removing this functionality, and leaving it up to the caller to create parent directories, if needed. Signed-off-by: Sebastiaan van Stijn --- cmd/dockerd/daemon.go | 3 +++ pkg/pidfile/pidfile.go | 5 ----- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/cmd/dockerd/daemon.go b/cmd/dockerd/daemon.go index 6f2bf7b803..5fd4149c24 100644 --- a/cmd/dockerd/daemon.go +++ b/cmd/dockerd/daemon.go @@ -139,6 +139,9 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) { potentiallyUnderRuntimeDir := []string{cli.Config.ExecRoot} if cli.Pidfile != "" { + if err = system.MkdirAll(filepath.Dir(cli.Pidfile), 0o755); err != nil { + return errors.Wrap(err, "failed to create pidfile directory") + } if err = pidfile.Write(cli.Pidfile, os.Getpid()); err != nil { return errors.Wrap(err, "failed to start daemon") } diff --git a/pkg/pidfile/pidfile.go b/pkg/pidfile/pidfile.go index 48a90387a6..8017d9a784 100644 --- a/pkg/pidfile/pidfile.go +++ b/pkg/pidfile/pidfile.go @@ -7,11 +7,9 @@ import ( "bytes" "fmt" "os" - "path/filepath" "strconv" "github.com/docker/docker/pkg/process" - "github.com/docker/docker/pkg/system" ) func checkPIDFileAlreadyExists(path string) error { @@ -41,8 +39,5 @@ func Write(path string, pid int) error { if err := checkPIDFileAlreadyExists(path); err != nil { return err } - if err := system.MkdirAll(filepath.Dir(path), 0o755); err != nil { - return err - } return os.WriteFile(path, []byte(strconv.Itoa(pid)), 0o644) }