agent/proxy: pull exit status extraction to constrained file

This commit is contained in:
Mitchell Hashimoto 2018-05-02 11:47:57 -07:00
parent 1a2b28602c
commit 420edc4c1e
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
3 changed files with 38 additions and 6 deletions

View File

@ -7,7 +7,6 @@ import (
"os/exec"
"reflect"
"sync"
"syscall"
"time"
)
@ -151,10 +150,8 @@ func (p *Daemon) keepAlive(stopCh <-chan struct{}) {
process = nil
if err != nil {
p.Logger.Printf("[INFO] agent/proxy: daemon exited with error: %s", err)
} else if status, ok := ps.Sys().(syscall.WaitStatus); ok {
p.Logger.Printf(
"[INFO] agent/proxy: daemon exited with exit code: %d",
status.ExitStatus())
} else if status, ok := exitStatus(ps); ok {
p.Logger.Printf("[INFO] agent/proxy: daemon exited with exit code: %d", status)
}
}
}
@ -176,8 +173,15 @@ func (p *Daemon) start() (*os.Process, error) {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// Args must always contain a 0 entry which is usually the executed binary.
// To be safe and a bit more robust we default this, but only to prevent
// a panic below.
if len(cmd.Args) == 0 {
cmd.Args = []string{cmd.Path}
}
// Start it
p.Logger.Printf("[DEBUG] agent/proxy: starting proxy: %q %#v", cmd.Path, cmd.Args)
p.Logger.Printf("[DEBUG] agent/proxy: starting proxy: %q %#v", cmd.Path, cmd.Args[1:])
err := cmd.Start()
return cmd.Process, err
}

View File

@ -0,0 +1,10 @@
// +build !darwin,!linux,!windows
package proxy
import "os"
// exitStatus for other platforms where we don't know how to extract it.
func exitStatus(ps *os.ProcessState) (int, bool) {
return 0, false
}

View File

@ -0,0 +1,18 @@
// +build darwin linux windows
package proxy
import (
"os"
"syscall"
)
// exitStatus for platforms with syscall.WaitStatus which are listed
// at the top of this file in the build constraints.
func exitStatus(ps *os.ProcessState) (int, bool) {
if status, ok := ps.Sys().(syscall.WaitStatus); ok {
return status.ExitStatus(), true
}
return 0, false
}