pids.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // +build linux
  2. package fs2
  3. import (
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "strconv"
  8. "strings"
  9. "github.com/opencontainers/runc/libcontainer/cgroups"
  10. "github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
  11. "github.com/opencontainers/runc/libcontainer/configs"
  12. "github.com/pkg/errors"
  13. "golang.org/x/sys/unix"
  14. )
  15. func setPids(dirPath string, cgroup *configs.Cgroup) error {
  16. if cgroup.Resources.PidsLimit != 0 {
  17. // "max" is the fallback value.
  18. limit := "max"
  19. if cgroup.Resources.PidsLimit > 0 {
  20. limit = strconv.FormatInt(cgroup.Resources.PidsLimit, 10)
  21. }
  22. if err := fscommon.WriteFile(dirPath, "pids.max", limit); err != nil {
  23. return err
  24. }
  25. }
  26. return nil
  27. }
  28. func isNOTSUP(err error) bool {
  29. switch err := err.(type) {
  30. case *os.PathError:
  31. return err.Err == unix.ENOTSUP
  32. default:
  33. return false
  34. }
  35. }
  36. func statPidsWithoutController(dirPath string, stats *cgroups.Stats) error {
  37. // if the controller is not enabled, let's read PIDS from cgroups.procs
  38. // (or threads if cgroup.threads is enabled)
  39. contents, err := ioutil.ReadFile(filepath.Join(dirPath, "cgroup.procs"))
  40. if err != nil && isNOTSUP(err) {
  41. contents, err = ioutil.ReadFile(filepath.Join(dirPath, "cgroup.threads"))
  42. }
  43. if err != nil {
  44. return err
  45. }
  46. pids := make(map[string]string)
  47. for _, i := range strings.Split(string(contents), "\n") {
  48. if i != "" {
  49. pids[i] = i
  50. }
  51. }
  52. stats.PidsStats.Current = uint64(len(pids))
  53. stats.PidsStats.Limit = 0
  54. return nil
  55. }
  56. func statPids(dirPath string, stats *cgroups.Stats) error {
  57. current, err := fscommon.GetCgroupParamUint(dirPath, "pids.current")
  58. if err != nil {
  59. return errors.Wrap(err, "failed to parse pids.current")
  60. }
  61. maxString, err := fscommon.GetCgroupParamString(dirPath, "pids.max")
  62. if err != nil {
  63. return errors.Wrap(err, "failed to parse pids.max")
  64. }
  65. // Default if pids.max == "max" is 0 -- which represents "no limit".
  66. var max uint64
  67. if maxString != "max" {
  68. max, err = fscommon.ParseUint(maxString, 10, 64)
  69. if err != nil {
  70. return errors.Wrapf(err, "failed to parse pids.max - unable to parse %q as a uint from Cgroup file %q",
  71. maxString, filepath.Join(dirPath, "pids.max"))
  72. }
  73. }
  74. stats.PidsStats.Current = current
  75. stats.PidsStats.Limit = max
  76. return nil
  77. }