Skip to content

[exec] Write trailing newline to -pid-file and -internal-pid-file #11901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions runsc/cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ func (ex *Exec) exec(conf *config.Config, c *container.Container, e *control.Exe

// Write the sandbox-internal pid if required.
if ex.internalPidFile != "" {
pidStr := []byte(strconv.Itoa(int(pid)))
if err := os.WriteFile(ex.internalPidFile, pidStr, 0644); err != nil {
pidStr := fmt.Sprintf("%d\n", pid)
if err := os.WriteFile(ex.internalPidFile, []byte(pidStr), 0644); err != nil {
return util.Errorf("writing internal pid file %q: %v", ex.internalPidFile, err)
}
}
Expand All @@ -207,7 +207,8 @@ func (ex *Exec) exec(conf *config.Config, c *container.Container, e *control.Exe
// users can safely assume that the internal pid file is ready after
// `runsc exec -d` returns.
if ex.pidFile != "" {
if err := os.WriteFile(ex.pidFile, []byte(strconv.Itoa(os.Getpid())), 0644); err != nil {
pidStr := fmt.Sprintf("%d\n", os.Getpid())
if err := os.WriteFile(ex.pidFile, []byte(pidStr), 0644); err != nil {
return util.Errorf("writing pid file: %v", err)
}
}
Expand Down Expand Up @@ -289,7 +290,11 @@ func (ex *Exec) execChildAndWait(waitStatus *unix.WaitStatus) subcommands.ExitSt
pidb, err := os.ReadFile(pidFile)
if err == nil {
// File appeared, check whether pid is fully written.
pid, err := strconv.Atoi(string(pidb))
pids, cut := strings.CutSuffix(string(pidb), "\n")
if !cut {
return false, nil
}
pid, err := strconv.Atoi(pids)
if err != nil {
return false, nil
}
Expand Down