Skip to content

Commit

Permalink
ctr: update WritePidFile to use atomicfile
Browse files Browse the repository at this point in the history
Signed-off-by: Samuel Karp <samuelkarp@google.com>
(cherry picked from commit ab53652)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
samuelkarp authored and thaJeztah committed Jul 14, 2023
1 parent 5f70b23 commit d75bf78
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions cmd/ctr/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"strings"

"github.com/containerd/containerd/defaults"
"github.com/containerd/containerd/pkg/atomicfile"

"github.com/urfave/cli"
)

Expand Down Expand Up @@ -259,15 +261,14 @@ func WritePidFile(path string, pid int) error {
if err != nil {
return err
}
tempPath := filepath.Join(filepath.Dir(path), fmt.Sprintf(".%s", filepath.Base(path)))
f, err := os.OpenFile(tempPath, os.O_RDWR|os.O_CREATE|os.O_EXCL|os.O_SYNC, 0666)
f, err := atomicfile.New(path, 0o666)
if err != nil {
return err
}
_, err = fmt.Fprintf(f, "%d", pid)
f.Close()
if err != nil {
f.Cancel()
return err
}
return os.Rename(tempPath, path)
return f.Close()
}

0 comments on commit d75bf78

Please sign in to comment.