From 556f673b3e962d77f8a19072de9754e2e2998f4b Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Mon, 15 Nov 2021 09:59:10 +0800 Subject: [PATCH] build: split os syscall --- muzan.go | 14 +------------- muzanctl.go | 24 ++++++++++++++++++++++++ muzanctrl_windows.go | 13 +++++++++++++ 3 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 muzanctl.go create mode 100644 muzanctrl_windows.go diff --git a/muzan.go b/muzan.go index 2ec21cc..3a2d993 100644 --- a/muzan.go +++ b/muzan.go @@ -77,7 +77,7 @@ func (oni *Oni) Start() error { } var err error cmd := exec.Command(oni.Cmd, oni.Args...) - cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} + setgpid(cmd) cmd.Env = oni.Envs cmd.Dir = oni.Dir var out io.Writer @@ -111,18 +111,6 @@ func (oni *Oni) Stop() error { return oni.cmd.Process.Signal(syscall.SIGTERM) } -func (oni *Oni) Kill() error { - if oni.cmd == nil { - return nil - } - pgid, err := syscall.Getpgid(oni.cmd.Process.Pid) - if err == nil { - return syscall.Kill(-pgid, 15) // note the minus sign - } - return oni.cmd.Process.Kill() - // return oni.cmd.Process.Kill() -} - func (oni *Oni) end(v interface{}) { if v == nil { oni.StateChange(ENDED) diff --git a/muzanctl.go b/muzanctl.go new file mode 100644 index 0000000..2cd2e1e --- /dev/null +++ b/muzanctl.go @@ -0,0 +1,24 @@ +//go:build !windows +// +build !windows + +package configui + +import ( + "os/exec" + "syscall" +) + +func setgpid(cmd *exec.Cmd) { + cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} +} + +func (oni *Oni) Kill() error { + if oni.cmd == nil { + return nil + } + pgid, err := syscall.Getpgid(oni.cmd.Process.Pid) + if err == nil { + return syscall.Kill(-pgid, 15) // note the minus sign + } + return oni.cmd.Process.Kill() +} diff --git a/muzanctrl_windows.go b/muzanctrl_windows.go new file mode 100644 index 0000000..64a8953 --- /dev/null +++ b/muzanctrl_windows.go @@ -0,0 +1,13 @@ +package configui + +import "os/exec" + +func setgpid(cmd *exec.Cmd) { +} + +func (oni *Oni) Kill() error { + if oni.cmd == nil { + return nil + } + return oni.cmd.Process.Kill() +}