procfs_linux.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package procfs
  2. /*
  3. Copyright 2015 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. import (
  15. "bytes"
  16. "fmt"
  17. "io"
  18. "io/ioutil"
  19. "os"
  20. "path/filepath"
  21. "regexp"
  22. "strconv"
  23. "strings"
  24. "unicode"
  25. "github.com/sirupsen/logrus"
  26. )
  27. // PidOf finds process(es) with a specified name (regexp match)
  28. // and return their pid(s)
  29. func PidOf(name string) ([]int, error) {
  30. if len(name) == 0 {
  31. return []int{}, fmt.Errorf("name should not be empty")
  32. }
  33. re, err := regexp.Compile("(^|/)" + name + "$")
  34. if err != nil {
  35. return []int{}, err
  36. }
  37. return getPids(re), nil
  38. }
  39. func getPids(re *regexp.Regexp) []int {
  40. pids := []int{}
  41. dirFD, err := os.Open("/proc")
  42. if err != nil {
  43. return nil
  44. }
  45. defer dirFD.Close()
  46. for {
  47. // Read a small number at a time in case there are many entries, we don't want to
  48. // allocate a lot here.
  49. ls, err := dirFD.Readdir(10)
  50. if err == io.EOF {
  51. break
  52. }
  53. if err != nil {
  54. return nil
  55. }
  56. for _, entry := range ls {
  57. if !entry.IsDir() {
  58. continue
  59. }
  60. // If the directory is not a number (i.e. not a PID), skip it
  61. pid, err := strconv.Atoi(entry.Name())
  62. if err != nil {
  63. continue
  64. }
  65. cmdline, err := ioutil.ReadFile(filepath.Join("/proc", entry.Name(), "cmdline"))
  66. if err != nil {
  67. logrus.Infof("Error reading file %s: %+v", filepath.Join("/proc", entry.Name(), "cmdline"), err)
  68. continue
  69. }
  70. // The bytes we read have '\0' as a separator for the command line
  71. parts := bytes.SplitN(cmdline, []byte{0}, 2)
  72. if len(parts) == 0 {
  73. continue
  74. }
  75. // Split the command line itself we are interested in just the first part
  76. exe := strings.FieldsFunc(string(parts[0]), func(c rune) bool {
  77. return unicode.IsSpace(c) || c == ':'
  78. })
  79. if len(exe) == 0 {
  80. continue
  81. }
  82. // Check if the name of the executable is what we are looking for
  83. if re.MatchString(exe[0]) {
  84. // Grab the PID from the directory path
  85. pids = append(pids, pid)
  86. }
  87. }
  88. }
  89. return pids
  90. }