process_pid.go 696 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package linux
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. "strconv"
  7. "strings"
  8. )
  9. func ReadMaxPID(path string) (uint64, error) {
  10. b, err := ioutil.ReadFile(path)
  11. if err != nil {
  12. return 0, err
  13. }
  14. s := strings.TrimSpace(string(b))
  15. i, err := strconv.ParseUint(s, 10, 64)
  16. if err != nil {
  17. return 0, err
  18. }
  19. return i, nil
  20. }
  21. func ListPID(path string, max uint64) ([]uint64, error) {
  22. l := make([]uint64, 0, 5)
  23. for i := uint64(1); i <= max; i++ {
  24. p := filepath.Join(path, strconv.FormatUint(i, 10))
  25. s, err := os.Stat(p)
  26. if err != nil && !os.IsNotExist(err) {
  27. return nil, err
  28. }
  29. if err != nil || !s.IsDir() {
  30. continue
  31. }
  32. l = append(l, i)
  33. }
  34. return l, nil
  35. }