pkg/process: separate exported funcs from implementation

This allows us to maintain GoDoc in a single place, and for
"Kill" and "Alive" to have consistent error-handling (Windows
does not support negative process-IDs).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-07-17 22:00:48 +02:00
parent f776cd6922
commit 94618ac3ab
3 changed files with 34 additions and 23 deletions

29
pkg/process/process.go Normal file
View File

@@ -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)
}

View File

@@ -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

View File

@@ -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()