Merge pull request #50454 from thaJeztah/pkg_process_split

pkg/process: separate exported funcs from implementation, and fix build-tag for implementation
This commit is contained in:
Sebastiaan van Stijn
2025-07-21 23:25:05 +02:00
committed by GitHub
5 changed files with 68 additions and 39 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

@@ -0,0 +1,24 @@
package process
import (
"bytes"
"fmt"
"os"
)
func zombie(pid int) (bool, error) {
if pid < 1 {
return false, nil
}
data, err := os.ReadFile(fmt.Sprintf("/proc/%d/stat", pid))
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
if cols := bytes.SplitN(data, []byte(" "), 4); len(cols) >= 3 && string(cols[2]) == "Z" {
return true, nil
}
return false, nil
}

View File

@@ -0,0 +1,7 @@
//go:build !linux
package process
func zombie(pid int) (bool, error) {
return false, nil
}

View File

@@ -3,9 +3,7 @@
package process
import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
@@ -14,17 +12,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 +32,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
@@ -63,20 +45,9 @@ func Kill(pid int) error {
// a PID larger than 1), and negative (-n, all processes in process group "n")
// values for pid are ignored. Refer to [PROC(5)] for details.
//
// Zombie is only implemented on Linux, and returns false on all other platforms.
//
// [PROC(5)]: https://man7.org/linux/man-pages/man5/proc.5.html
func Zombie(pid int) (bool, error) {
if pid < 1 {
return false, nil
}
data, err := os.ReadFile(fmt.Sprintf("/proc/%d/stat", pid))
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
if cols := bytes.SplitN(data, []byte(" "), 4); len(cols) >= 3 && string(cols[2]) == "Z" {
return true, nil
}
return false, nil
return zombie(pid)
}

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