Browse Source

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 <github@gone.nl>
Sebastiaan van Stijn 2 năm trước cách đây
mục cha
commit
7d3e1ad943
2 tập tin đã thay đổi với 3 bổ sung5 xóa
  1. 3 0
      cmd/dockerd/daemon.go
  2. 0 5
      pkg/pidfile/pidfile.go

+ 3 - 0
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")
 		}

+ 0 - 5
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)
 }