diff --git a/pkg/proc/native/proc.go b/pkg/proc/native/proc.go index 500072ecd436954974d323e58111ee003a407cdf..336e760ca3b7db302eed8fbe1d216d916ca0438d 100644 --- a/pkg/proc/native/proc.go +++ b/pkg/proc/native/proc.go @@ -41,6 +41,7 @@ type Process struct { exited bool ptraceChan chan func() ptraceDoneChan chan interface{} + childProcess bool // this process was launched, not attached to } // New returns an initialized Process struct. Before returning, @@ -71,6 +72,14 @@ func (dbp *Process) Detach(kill bool) (err error) { if dbp.exited { return nil } + if kill && dbp.childProcess { + err := dbp.Kill() + if err != nil { + return err + } + dbp.bi.Close() + return nil + } if dbp.Running() { if err = dbp.Halt(); err != nil { return diff --git a/pkg/proc/native/proc_darwin.go b/pkg/proc/native/proc_darwin.go index ef8703898217f77d2356bb5833f7af3454031a57..5333819ec627bfd735630355b60e492a4aa5a99e 100644 --- a/pkg/proc/native/proc_darwin.go +++ b/pkg/proc/native/proc_darwin.go @@ -75,6 +75,7 @@ func Launch(cmd []string, wd string) (*Process, error) { return nil, fmt.Errorf("could not fork/exec") } dbp.pid = pid + dbp.childProcess = true for i := range argvSlice { C.free(unsafe.Pointer(argvSlice[i])) } diff --git a/pkg/proc/native/proc_linux.go b/pkg/proc/native/proc_linux.go index eabc6b88f3f6be82b45d7df79660e3301993829b..b04274ecc80948ef1428c3b347b06523c8d00ece 100644 --- a/pkg/proc/native/proc_linux.go +++ b/pkg/proc/native/proc_linux.go @@ -68,6 +68,7 @@ func Launch(cmd []string, wd string) (*Process, error) { return nil, err } dbp.pid = process.Process.Pid + dbp.childProcess = true _, _, err = dbp.wait(process.Process.Pid, 0) if err != nil { return nil, fmt.Errorf("waiting for target execve failed: %s", err) diff --git a/pkg/proc/native/proc_windows.go b/pkg/proc/native/proc_windows.go index 11461e96bb6576712da3aac59af1bfb68aecac79..5741479a35e9938809de07a2c3fcf20eb385f23a 100644 --- a/pkg/proc/native/proc_windows.go +++ b/pkg/proc/native/proc_windows.go @@ -125,6 +125,7 @@ func Launch(cmd []string, wd string) (*Process, error) { sys.CloseHandle(sys.Handle(pi.Thread)) dbp.pid = int(pi.ProcessId) + dbp.childProcess = true return newDebugProcess(dbp, argv0Go) } diff --git a/pkg/proc/proc_test.go b/pkg/proc/proc_test.go index 0e10d779ceab37471c70e0b0141b3e62b5ef4117..f62e36c6c7b2e57241924aef6fcd4c62849d1ae5 100644 --- a/pkg/proc/proc_test.go +++ b/pkg/proc/proc_test.go @@ -918,6 +918,20 @@ func TestKill(t *testing.T) { } } }) + withTestProcess("testprog", t, func(p proc.Process, fixture protest.Fixture) { + if err := p.Detach(true); err != nil { + t.Fatal(err) + } + if p.Exited() != true { + t.Fatal("expected process to have exited") + } + if runtime.GOOS == "linux" { + _, err := os.Open(fmt.Sprintf("/proc/%d/", p.Pid())) + if err == nil { + t.Fatal("process has not exited", p.Pid()) + } + } + }) } func testGSupportFunc(name string, t *testing.T, p proc.Process, fixture protest.Fixture) {