process_unix.go 941 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // +build linux freebsd darwin
  2. package system // import "github.com/docker/docker/pkg/system"
  3. import (
  4. "fmt"
  5. "io/ioutil"
  6. "strings"
  7. "syscall"
  8. "golang.org/x/sys/unix"
  9. )
  10. // IsProcessAlive returns true if process with a given pid is running.
  11. func IsProcessAlive(pid int) bool {
  12. err := unix.Kill(pid, syscall.Signal(0))
  13. if err == nil || err == unix.EPERM {
  14. return true
  15. }
  16. return false
  17. }
  18. // KillProcess force-stops a process.
  19. func KillProcess(pid int) {
  20. unix.Kill(pid, unix.SIGKILL)
  21. }
  22. // IsProcessZombie return true if process has a state with "Z"
  23. // http://man7.org/linux/man-pages/man5/proc.5.html
  24. func IsProcessZombie(pid int) (bool, error) {
  25. statPath := fmt.Sprintf("/proc/%d/stat", pid)
  26. dataBytes, err := ioutil.ReadFile(statPath)
  27. if err != nil {
  28. return false, err
  29. }
  30. data := string(dataBytes)
  31. sdata := strings.SplitN(data, " ", 4)
  32. if len(sdata) >= 3 && sdata[2] == "Z" {
  33. return true, nil
  34. }
  35. return false, nil
  36. }