diff options
author | Nicolas Lacasse <nlacasse@google.com> | 2018-07-25 09:10:32 -0700 |
---|---|---|
committer | Shentubot <shentubot@google.com> | 2018-07-25 09:11:45 -0700 |
commit | 1129b35c92034d04ed22cf69e270ed9c034069d7 (patch) | |
tree | c632bcb8969ee49fc31ab7148883cbe17712fd62 | |
parent | 32aa0f5465832c437a9de83c1c1a04b615d68122 (diff) |
runsc: Fix "exec" command when called without --pid-file.
When "exec" command is called without the "--detach" flag, we spawn a second
"exec" command and wait for that one to start. We use the pid file passed in
--pid-file to detect when this second command has started running.
However if "exec" is called with no --pid-file flag, this system breaks down,
as we don't have a pid file to wait for.
This CL ensures that the second instance of the "exec" command always writes a
pid-file, so the wait is successful.
PiperOrigin-RevId: 206002403
Change-Id: If9f2be31eb6e831734b1b833f25054ec71ab94a6
-rw-r--r-- | runsc/cmd/exec.go | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/runsc/cmd/exec.go b/runsc/cmd/exec.go index cbce07c8e..4ee370656 100644 --- a/runsc/cmd/exec.go +++ b/runsc/cmd/exec.go @@ -20,6 +20,7 @@ import ( "io/ioutil" "os" "os/exec" + "path/filepath" "strconv" "strings" "syscall" @@ -156,11 +157,28 @@ func (ex *Exec) execAndWait(waitStatus *syscall.WaitStatus) subcommands.ExitStat Fatalf("error getting bin path: %v", err) } var args []string + + // The command needs to write a pid file so that execAndWait can tell + // when it has started. If no pid-file was provided, we should use a + // filename in a temp directory. + pidFile := ex.pidFile + if pidFile == "" { + tmpDir, err := ioutil.TempDir("", "exec-pid-") + if err != nil { + Fatalf("error creating TempDir: %v", err) + } + defer os.RemoveAll(tmpDir) + pidFile = filepath.Join(tmpDir, "pid") + args = append(args, "--pid-file="+pidFile) + } + + // Add the rest of the args, excluding the "detach" flag. for _, a := range os.Args[1:] { if !strings.Contains(a, "detach") { args = append(args, a) } } + cmd := exec.Command(binPath, args...) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout @@ -175,7 +193,7 @@ func (ex *Exec) execAndWait(waitStatus *syscall.WaitStatus) subcommands.ExitStat // '--process' file is deleted as soon as this process returns and the child // may fail to read it. ready := func() (bool, error) { - _, err := os.Stat(ex.pidFile) + _, err := os.Stat(pidFile) if err == nil { // File appeared, we're done! return true, nil |