top_unix.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //+build !windows
  2. package daemon
  3. import (
  4. "fmt"
  5. "os/exec"
  6. "regexp"
  7. "strconv"
  8. "strings"
  9. "github.com/docker/engine-api/types"
  10. )
  11. func validatePSArgs(psArgs string) error {
  12. // NOTE: \\s does not detect unicode whitespaces.
  13. // So we use fieldsASCII instead of strings.Fields in parsePSOutput.
  14. // See https://github.com/docker/docker/pull/24358
  15. re := regexp.MustCompile("\\s+([^\\s]*)=\\s*(PID[^\\s]*)")
  16. for _, group := range re.FindAllStringSubmatch(psArgs, -1) {
  17. if len(group) >= 3 {
  18. k := group[1]
  19. v := group[2]
  20. if k != "pid" {
  21. return fmt.Errorf("specifying \"%s=%s\" is not allowed", k, v)
  22. }
  23. }
  24. }
  25. return nil
  26. }
  27. // fieldsASCII is similar to strings.Fields but only allows ASCII whitespaces
  28. func fieldsASCII(s string) []string {
  29. fn := func(r rune) bool {
  30. switch r {
  31. case '\t', '\n', '\f', '\r', ' ':
  32. return true
  33. }
  34. return false
  35. }
  36. return strings.FieldsFunc(s, fn)
  37. }
  38. func parsePSOutput(output []byte, pids []int) (*types.ContainerProcessList, error) {
  39. procList := &types.ContainerProcessList{}
  40. lines := strings.Split(string(output), "\n")
  41. procList.Titles = fieldsASCII(lines[0])
  42. pidIndex := -1
  43. for i, name := range procList.Titles {
  44. if name == "PID" {
  45. pidIndex = i
  46. }
  47. }
  48. if pidIndex == -1 {
  49. return nil, fmt.Errorf("Couldn't find PID field in ps output")
  50. }
  51. // loop through the output and extract the PID from each line
  52. for _, line := range lines[1:] {
  53. if len(line) == 0 {
  54. continue
  55. }
  56. fields := fieldsASCII(line)
  57. p, err := strconv.Atoi(fields[pidIndex])
  58. if err != nil {
  59. return nil, fmt.Errorf("Unexpected pid '%s': %s", fields[pidIndex], err)
  60. }
  61. for _, pid := range pids {
  62. if pid == p {
  63. // Make sure number of fields equals number of header titles
  64. // merging "overhanging" fields
  65. process := fields[:len(procList.Titles)-1]
  66. process = append(process, strings.Join(fields[len(procList.Titles)-1:], " "))
  67. procList.Processes = append(procList.Processes, process)
  68. }
  69. }
  70. }
  71. return procList, nil
  72. }
  73. // ContainerTop lists the processes running inside of the given
  74. // container by calling ps with the given args, or with the flags
  75. // "-ef" if no args are given. An error is returned if the container
  76. // is not found, or is not running, or if there are any problems
  77. // running ps, or parsing the output.
  78. func (daemon *Daemon) ContainerTop(name string, psArgs string) (*types.ContainerProcessList, error) {
  79. if psArgs == "" {
  80. psArgs = "-ef"
  81. }
  82. if err := validatePSArgs(psArgs); err != nil {
  83. return nil, err
  84. }
  85. container, err := daemon.GetContainer(name)
  86. if err != nil {
  87. return nil, err
  88. }
  89. if !container.IsRunning() {
  90. return nil, errNotRunning{container.ID}
  91. }
  92. if container.IsRestarting() {
  93. return nil, errContainerIsRestarting(container.ID)
  94. }
  95. pids, err := daemon.containerd.GetPidsForContainer(container.ID)
  96. if err != nil {
  97. return nil, err
  98. }
  99. output, err := exec.Command("ps", strings.Split(psArgs, " ")...).Output()
  100. if err != nil {
  101. return nil, fmt.Errorf("Error running ps: %v", err)
  102. }
  103. procList, err := parsePSOutput(output, pids)
  104. if err != nil {
  105. return nil, err
  106. }
  107. daemon.LogContainerEvent(container, "top")
  108. return procList, nil
  109. }