process_unix.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //go:build !windows
  2. // +build !windows
  3. package process
  4. import (
  5. "bytes"
  6. "fmt"
  7. "os"
  8. "path/filepath"
  9. "runtime"
  10. "strconv"
  11. "golang.org/x/sys/unix"
  12. )
  13. // Alive returns true if process with a given pid is running. It only considers
  14. // positive PIDs; 0 (all processes in the current process group), -1 (all processes
  15. // with a PID larger than 1), and negative (-n, all processes in process group
  16. // "n") values for pid are never considered to be alive.
  17. func Alive(pid int) bool {
  18. if pid < 1 {
  19. return false
  20. }
  21. switch runtime.GOOS {
  22. case "darwin":
  23. // OS X does not have a proc filesystem. Use kill -0 pid to judge if the
  24. // process exists. From KILL(2): https://www.freebsd.org/cgi/man.cgi?query=kill&sektion=2&manpath=OpenDarwin+7.2.1
  25. //
  26. // Sig may be one of the signals specified in sigaction(2) or it may
  27. // be 0, in which case error checking is performed but no signal is
  28. // actually sent. This can be used to check the validity of pid.
  29. err := unix.Kill(pid, 0)
  30. // Either the PID was found (no error) or we get an EPERM, which means
  31. // the PID exists, but we don't have permissions to signal it.
  32. return err == nil || err == unix.EPERM
  33. default:
  34. _, err := os.Stat(filepath.Join("/proc", strconv.Itoa(pid)))
  35. return err == nil
  36. }
  37. }
  38. // Kill force-stops a process. It only considers positive PIDs; 0 (all processes
  39. // in the current process group), -1 (all processes with a PID larger than 1),
  40. // and negative (-n, all processes in process group "n") values for pid are
  41. // ignored. Refer to [KILL(2)] for details.
  42. //
  43. // [KILL(2)]: https://man7.org/linux/man-pages/man2/kill.2.html
  44. func Kill(pid int) error {
  45. if pid < 1 {
  46. return fmt.Errorf("invalid PID (%d): only positive PIDs are allowed", pid)
  47. }
  48. err := unix.Kill(pid, unix.SIGKILL)
  49. if err != nil && err != unix.ESRCH {
  50. return err
  51. }
  52. return nil
  53. }
  54. // Zombie return true if process has a state with "Z". It only considers positive
  55. // PIDs; 0 (all processes in the current process group), -1 (all processes with
  56. // a PID larger than 1), and negative (-n, all processes in process group "n")
  57. // values for pid are ignored. Refer to [PROC(5)] for details.
  58. //
  59. // [PROC(5)]: https://man7.org/linux/man-pages/man5/proc.5.html
  60. func Zombie(pid int) (bool, error) {
  61. if pid < 1 {
  62. return false, nil
  63. }
  64. data, err := os.ReadFile(fmt.Sprintf("/proc/%d/stat", pid))
  65. if err != nil {
  66. if os.IsNotExist(err) {
  67. return false, nil
  68. }
  69. return false, err
  70. }
  71. if cols := bytes.SplitN(data, []byte(" "), 4); len(cols) >= 3 && string(cols[2]) == "Z" {
  72. return true, nil
  73. }
  74. return false, nil
  75. }