process_unix.go 2.5 KB

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