From 10fe87bd4ac1361dca26c0f2098e63de49ddafb0 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 2 May 2018 11:47:57 -0700 Subject: [PATCH] agent/proxy: pull exit status extraction to constrained file --- agent/proxy/daemon.go | 16 ++++++++++------ agent/proxy/exitstatus_other.go | 10 ++++++++++ agent/proxy/exitstatus_syscall.go | 18 ++++++++++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 agent/proxy/exitstatus_other.go create mode 100644 agent/proxy/exitstatus_syscall.go diff --git a/agent/proxy/daemon.go b/agent/proxy/daemon.go index 1d716950b..d5fd30256 100644 --- a/agent/proxy/daemon.go +++ b/agent/proxy/daemon.go @@ -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 } diff --git a/agent/proxy/exitstatus_other.go b/agent/proxy/exitstatus_other.go new file mode 100644 index 000000000..84dd88867 --- /dev/null +++ b/agent/proxy/exitstatus_other.go @@ -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 +} diff --git a/agent/proxy/exitstatus_syscall.go b/agent/proxy/exitstatus_syscall.go new file mode 100644 index 000000000..1caeda4bf --- /dev/null +++ b/agent/proxy/exitstatus_syscall.go @@ -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 +}