diff --git a/pexec/managed_process.go b/pexec/managed_process.go index 306e6311..14752998 100644 --- a/pexec/managed_process.go +++ b/pexec/managed_process.go @@ -35,6 +35,10 @@ type ManagedProcess interface { // Status return nil when the process is both alive and owned. // If err is non-nil, process may be a) alive but not owned or b) dead. Status() error + + // UnixPid returns the pid of the process. This method returns an error if the pid is + // unknown. For example, if the process hasn't been `Start`ed yet. Or if not on a unix system. + UnixPid() (int, error) } // NewManagedProcess returns a new, unstarted, from the given configuration. @@ -107,6 +111,13 @@ func (p *managedProcess) ID() string { return p.id } +func (p *managedProcess) UnixPid() (int, error) { + if p.cmd == nil || p.cmd.Process == nil { + return 0, errors.New("Process not started") + } + return p.cmd.Process.Pid, nil +} + func (p *managedProcess) Status() error { p.mu.Lock() defer p.mu.Unlock() diff --git a/pexec/managed_process_test.go b/pexec/managed_process_test.go index bde8917f..75e3e44f 100644 --- a/pexec/managed_process_test.go +++ b/pexec/managed_process_test.go @@ -695,3 +695,10 @@ func (fp *fakeProcess) Status() error { } return nil } + +func (fp *fakeProcess) UnixPid() (int, error) { + return 0, errors.New(`the NewManagedProcess API needlessly returns an interface + instead of the structure itself. Thus tests depend on the returned interface. When +in reality tests should just depend on the methods they rely on. UnixPid is not one +of those methods (for better or worse)`) +}