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>
This commit is contained in:
Sebastiaan van Stijn 2022-10-15 20:56:48 +02:00
parent 81945da0ac
commit 7d3e1ad943
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
2 changed files with 3 additions and 5 deletions

View file

@ -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")
}

View file

@ -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)
}