runsc: generate exec pidfile after everything is ready.

PiperOrigin-RevId: 221123160
Change-Id: Ia7061d60d114d69f49aba853fe6bae3c733522b5
This commit is contained in:
Lantao Liu
2018-11-12 11:12:39 -08:00
committed by Shentubot
parent c22da3e705
commit 4cd4b60352
+9 -6
View File
@@ -146,12 +146,6 @@ func (ex *Exec) Execute(_ context.Context, f *flag.FlagSet, args ...interface{})
return ex.execAndWait(waitStatus)
}
if ex.pidFile != "" {
if err := ioutil.WriteFile(ex.pidFile, []byte(strconv.Itoa(os.Getpid())), 0644); err != nil {
Fatalf("error writing pid file: %v", err)
}
}
// Start the new process and get it pid.
pid, err := c.Execute(e)
if err != nil {
@@ -173,6 +167,15 @@ func (ex *Exec) Execute(_ context.Context, f *flag.FlagSet, args ...interface{})
}
}
// Generate the pid file after the internal pid file is generated, so that users
// can safely assume that the internal pid file is ready after `runsc exec -d`
// returns.
if ex.pidFile != "" {
if err := ioutil.WriteFile(ex.pidFile, []byte(strconv.Itoa(os.Getpid())), 0644); err != nil {
Fatalf("error writing pid file: %v", err)
}
}
// Wait for the process to exit.
ws, err := c.WaitPID(pid, ex.clearStatus)
if err != nil {