提交 1f153580 编写于 作者: A Alessandro Arzilli 提交者: Derek Parker

proc/native: Detach should use Kill with child processes we want killed (#822)

While implementing the gdbserial backend everything was changed to call
Detach to "close" a process so that gdbserial could do its clean up in
a single place. However the native implementation of Detach does not
actually kill processes we launched.

Fixes #821
上级 f29b9eda
......@@ -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
......
......@@ -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]))
}
......
......@@ -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)
......
......@@ -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)
}
......
......@@ -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) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册