proc.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package system
  2. import (
  3. "io/ioutil"
  4. "path/filepath"
  5. "strconv"
  6. "strings"
  7. )
  8. // look in /proc to find the process start time so that we can verify
  9. // that this pid has started after ourself
  10. func GetProcessStartTime(pid int) (string, error) {
  11. data, err := ioutil.ReadFile(filepath.Join("/proc", strconv.Itoa(pid), "stat"))
  12. if err != nil {
  13. return "", err
  14. }
  15. return parseStartTime(string(data))
  16. }
  17. func parseStartTime(stat string) (string, error) {
  18. // the starttime is located at pos 22
  19. // from the man page
  20. //
  21. // starttime %llu (was %lu before Linux 2.6)
  22. // (22) The time the process started after system boot. In kernels before Linux 2.6, this
  23. // value was expressed in jiffies. Since Linux 2.6, the value is expressed in clock ticks
  24. // (divide by sysconf(_SC_CLK_TCK)).
  25. //
  26. // NOTE:
  27. // pos 2 could contain space and is inside `(` and `)`:
  28. // (2) comm %s
  29. // The filename of the executable, in parentheses.
  30. // This is visible whether or not the executable is
  31. // swapped out.
  32. //
  33. // the following is an example:
  34. // 89653 (gunicorn: maste) S 89630 89653 89653 0 -1 4194560 29689 28896 0 3 146 32 76 19 20 0 1 0 2971844 52965376 3920 18446744073709551615 1 1 0 0 0 0 0 16781312 137447943 0 0 0 17 1 0 0 0 0 0 0 0 0 0 0 0 0 0
  35. // get parts after last `)`:
  36. s := strings.Split(stat, ")")
  37. parts := strings.Split(strings.TrimSpace(s[len(s)-1]), " ")
  38. return parts[22-3], nil // starts at 3 (after the filename pos `2`)
  39. }