diff --git a/pkg/process/process.go b/pkg/process/process.go new file mode 100644 index 0000000000..73078765de --- /dev/null +++ b/pkg/process/process.go @@ -0,0 +1,29 @@ +package process + +import "fmt" + +// Alive returns true if process with a given pid is running. +// +// It only considers positive PIDs; 0 (all processes in the current process +// group), -1 (all processes with a PID larger than 1), and negative (-n, +// all processes in process group "n") values for pid are never considered +// to be alive. +func Alive(pid int) bool { + if pid < 1 { + return false + } + return alive(pid) +} + +// Kill force-stops a process. It only allows positive PIDs; 0 (all processes +// in the current process group), -1 (all processes with a PID larger than 1), +// and negative (-n, all processes in process group "n") values for pid producs +// an error. Refer to [KILL(2)] for details. +// +// [KILL(2)]: https://man7.org/linux/man-pages/man2/kill.2.html +func Kill(pid int) error { + if pid < 1 { + return fmt.Errorf("invalid PID (%d): only positive PIDs are allowed", pid) + } + return kill(pid) +} diff --git a/pkg/process/process_unix.go b/pkg/process/process_unix.go index 13298bbdcc..43eabb5794 100644 --- a/pkg/process/process_unix.go +++ b/pkg/process/process_unix.go @@ -14,17 +14,10 @@ import ( "golang.org/x/sys/unix" ) -// Alive returns true if process with a given pid is running. It only considers -// positive PIDs; 0 (all processes in the current process group), -1 (all processes -// with a PID larger than 1), and negative (-n, all processes in process group -// "n") values for pid are never considered to be alive. -func Alive(pid int) bool { - if pid < 1 { - return false - } +func alive(pid int) bool { switch runtime.GOOS { case "darwin": - // OS X does not have a proc filesystem. Use kill -0 pid to judge if the + // macOS does not have a proc filesystem. Use kill -0 pid to judge if the // process exists. From KILL(2): https://www.freebsd.org/cgi/man.cgi?query=kill&sektion=2&manpath=OpenDarwin+7.2.1 // // Sig may be one of the signals specified in sigaction(2) or it may @@ -41,16 +34,7 @@ func Alive(pid int) bool { } } -// Kill force-stops a process. It only considers positive PIDs; 0 (all processes -// in the current process group), -1 (all processes with a PID larger than 1), -// and negative (-n, all processes in process group "n") values for pid are -// ignored. Refer to [KILL(2)] for details. -// -// [KILL(2)]: https://man7.org/linux/man-pages/man2/kill.2.html -func Kill(pid int) error { - if pid < 1 { - return fmt.Errorf("invalid PID (%d): only positive PIDs are allowed", pid) - } +func kill(pid int) error { err := unix.Kill(pid, unix.SIGKILL) if err != nil && !errors.Is(err, unix.ESRCH) { return err diff --git a/pkg/process/process_windows.go b/pkg/process/process_windows.go index 2dd57e8254..ba11f8199c 100644 --- a/pkg/process/process_windows.go +++ b/pkg/process/process_windows.go @@ -6,8 +6,7 @@ import ( "golang.org/x/sys/windows" ) -// Alive returns true if process with a given pid is running. -func Alive(pid int) bool { +func alive(pid int) bool { h, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, uint32(pid)) if err != nil { return false @@ -32,8 +31,7 @@ func Alive(pid int) bool { return true } -// Kill force-stops a process. -func Kill(pid int) error { +func kill(pid int) error { p, err := os.FindProcess(pid) if err == nil { err = p.Kill()