procfs_linux.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // +build linux
  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. package procfs
  15. import (
  16. "bytes"
  17. "fmt"
  18. "io/ioutil"
  19. "os"
  20. "path"
  21. "path/filepath"
  22. "regexp"
  23. "strconv"
  24. "strings"
  25. "syscall"
  26. "unicode"
  27. "github.com/golang/glog"
  28. utilerrors "k8s.io/kubernetes/pkg/util/errors"
  29. )
  30. type ProcFS struct{}
  31. func NewProcFS() ProcFSInterface {
  32. return &ProcFS{}
  33. }
  34. func containerNameFromProcCgroup(content string) (string, error) {
  35. lines := strings.Split(content, "\n")
  36. for _, line := range lines {
  37. entries := strings.SplitN(line, ":", 3)
  38. if len(entries) == 3 && entries[1] == "devices" {
  39. return strings.TrimSpace(entries[2]), nil
  40. }
  41. }
  42. return "", fmt.Errorf("could not find devices cgroup location")
  43. }
  44. // getFullContainerName gets the container name given the root process id of the container.
  45. // Eg. If the devices cgroup for the container is stored in /sys/fs/cgroup/devices/docker/nginx,
  46. // return docker/nginx. Assumes that the process is part of exactly one cgroup hierarchy.
  47. func (pfs *ProcFS) GetFullContainerName(pid int) (string, error) {
  48. filePath := path.Join("/proc", strconv.Itoa(pid), "cgroup")
  49. content, err := ioutil.ReadFile(filePath)
  50. if err != nil {
  51. if os.IsNotExist(err) {
  52. return "", os.ErrNotExist
  53. }
  54. return "", err
  55. }
  56. return containerNameFromProcCgroup(string(content))
  57. }
  58. // Find process(es) using a regular expression and send a specified
  59. // signal to each process
  60. func PKill(name string, sig syscall.Signal) error {
  61. if len(name) == 0 {
  62. return fmt.Errorf("name should not be empty")
  63. }
  64. re, err := regexp.Compile(name)
  65. if err != nil {
  66. return err
  67. }
  68. pids := getPids(re)
  69. if len(pids) == 0 {
  70. return fmt.Errorf("unable to fetch pids for process name : %q", name)
  71. }
  72. errList := []error{}
  73. for _, pid := range pids {
  74. if err = syscall.Kill(pid, sig); err != nil {
  75. errList = append(errList, err)
  76. }
  77. }
  78. return utilerrors.NewAggregate(errList)
  79. }
  80. // Find process(es) with a specified name (exact match)
  81. // and return their pid(s)
  82. func PidOf(name string) ([]int, error) {
  83. if len(name) == 0 {
  84. return []int{}, fmt.Errorf("name should not be empty")
  85. }
  86. re, err := regexp.Compile("(^|/)" + name + "$")
  87. if err != nil {
  88. return []int{}, err
  89. }
  90. return getPids(re), nil
  91. }
  92. func getPids(re *regexp.Regexp) []int {
  93. pids := []int{}
  94. filepath.Walk("/proc", func(path string, info os.FileInfo, err error) error {
  95. if err != nil {
  96. // We should continue processing other directories/files
  97. return nil
  98. }
  99. base := filepath.Base(path)
  100. // Traverse only the directories we are interested in
  101. if info.IsDir() && path != "/proc" {
  102. // If the directory is not a number (i.e. not a PID), skip it
  103. if _, err := strconv.Atoi(base); err != nil {
  104. return filepath.SkipDir
  105. }
  106. }
  107. if base != "cmdline" {
  108. return nil
  109. }
  110. cmdline, err := ioutil.ReadFile(path)
  111. if err != nil {
  112. glog.V(4).Infof("Error reading file %s: %+v", path, err)
  113. return nil
  114. }
  115. // The bytes we read have '\0' as a separator for the command line
  116. parts := bytes.SplitN(cmdline, []byte{0}, 2)
  117. if len(parts) == 0 {
  118. return nil
  119. }
  120. // Split the command line itself we are interested in just the first part
  121. exe := strings.FieldsFunc(string(parts[0]), func(c rune) bool {
  122. return unicode.IsSpace(c) || c == ':'
  123. })
  124. if len(exe) == 0 {
  125. return nil
  126. }
  127. // Check if the name of the executable is what we are looking for
  128. if re.MatchString(exe[0]) {
  129. dirname := filepath.Base(filepath.Dir(path))
  130. // Grab the PID from the directory path
  131. pid, _ := strconv.Atoi(dirname)
  132. pids = append(pids, pid)
  133. }
  134. return nil
  135. })
  136. return pids
  137. }