Skip to content

Commit e41d4f5

Browse files
stepancheggvisor-bot
authored andcommitted
[exec] Write trailing newline to -pid-file and -internal-pid-file
When executing command, otherwise it is not possible to know when the pid file is fully written. Resolves #11851 This PR only changes `exec` command, and not other commands that write pid files. FUTURE_COPYBARA_INTEGRATE_REVIEW=#11861 from stepancheg:pid-file 726d8d9 PiperOrigin-RevId: 780722929
1 parent 4238de2 commit e41d4f5

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

runsc/cmd/exec.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ func (ex *Exec) exec(conf *config.Config, c *container.Container, e *control.Exe
197197

198198
// Write the sandbox-internal pid if required.
199199
if ex.internalPidFile != "" {
200-
pidStr := []byte(strconv.Itoa(int(pid)))
201-
if err := os.WriteFile(ex.internalPidFile, pidStr, 0644); err != nil {
200+
pidStr := fmt.Sprintf("%d\n", pid)
201+
if err := os.WriteFile(ex.internalPidFile, []byte(pidStr), 0644); err != nil {
202202
return util.Errorf("writing internal pid file %q: %v", ex.internalPidFile, err)
203203
}
204204
}
@@ -207,7 +207,8 @@ func (ex *Exec) exec(conf *config.Config, c *container.Container, e *control.Exe
207207
// users can safely assume that the internal pid file is ready after
208208
// `runsc exec -d` returns.
209209
if ex.pidFile != "" {
210-
if err := os.WriteFile(ex.pidFile, []byte(strconv.Itoa(os.Getpid())), 0644); err != nil {
210+
pidStr := fmt.Sprintf("%d\n", os.Getpid())
211+
if err := os.WriteFile(ex.pidFile, []byte(pidStr), 0644); err != nil {
211212
return util.Errorf("writing pid file: %v", err)
212213
}
213214
}
@@ -289,7 +290,11 @@ func (ex *Exec) execChildAndWait(waitStatus *unix.WaitStatus) subcommands.ExitSt
289290
pidb, err := os.ReadFile(pidFile)
290291
if err == nil {
291292
// File appeared, check whether pid is fully written.
292-
pid, err := strconv.Atoi(string(pidb))
293+
pids, cut := strings.CutSuffix(string(pidb), "\n")
294+
if !cut {
295+
return false, nil
296+
}
297+
pid, err := strconv.Atoi(pids)
293298
if err != nil {
294299
return false, nil
295300
}

0 commit comments

Comments
 (0)