Browse Source

pkg/pidfile: Write(): take pid as argument

This allows it to be used for processes other than the daemon itself.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 2 years ago
parent
commit
81945da0ac
3 changed files with 17 additions and 5 deletions
  1. 1 1
      cmd/dockerd/daemon.go
  2. 7 2
      pkg/pidfile/pidfile.go
  3. 9 2
      pkg/pidfile/pidfile_test.go

+ 1 - 1
cmd/dockerd/daemon.go

@@ -139,7 +139,7 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
 	potentiallyUnderRuntimeDir := []string{cli.Config.ExecRoot}
 
 	if cli.Pidfile != "" {
-		if err := pidfile.Write(cli.Pidfile); err != nil {
+		if err = pidfile.Write(cli.Pidfile, os.Getpid()); err != nil {
 			return errors.Wrap(err, "failed to start daemon")
 		}
 		potentiallyUnderRuntimeDir = append(potentiallyUnderRuntimeDir, cli.Pidfile)

+ 7 - 2
pkg/pidfile/pidfile.go

@@ -32,12 +32,17 @@ func checkPIDFileAlreadyExists(path string) error {
 // Write writes a "PID file" at the specified path. It returns an error if the
 // file exists and contains a valid PID of a running process, or when failing
 // to write the file.
-func Write(path string) error {
+func Write(path string, pid int) error {
+	if pid < 1 {
+		// We might be running as PID 1 when running docker-in-docker,
+		// but 0 or negative PIDs are not acceptable.
+		return fmt.Errorf("invalid PID (%d): only positive PIDs are allowed", pid)
+	}
 	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(os.Getpid())), 0o644)
+	return os.WriteFile(path, []byte(strconv.Itoa(pid)), 0o644)
 }

+ 9 - 2
pkg/pidfile/pidfile_test.go

@@ -1,18 +1,25 @@
 package pidfile // import "github.com/docker/docker/pkg/pidfile"
 
 import (
+	"os"
 	"path/filepath"
 	"testing"
 )
 
 func TestWrite(t *testing.T) {
 	path := filepath.Join(t.TempDir(), "testfile")
-	err := Write(path)
+
+	err := Write(path, 0)
+	if err == nil {
+		t.Fatal("writing PID < 1 should fail")
+	}
+
+	err = Write(path, os.Getpid())
 	if err != nil {
 		t.Fatal("Could not create test file", err)
 	}
 
-	err = Write(path)
+	err = Write(path, os.Getpid())
 	if err == nil {
 		t.Fatal("Test file creation not blocked")
 	}