oom_linux.go 752 B

12345678910111213141516171819202122232425262728293031
  1. package libcontainerd
  2. import (
  3. "fmt"
  4. "os"
  5. "strconv"
  6. "github.com/opencontainers/runc/libcontainer/system"
  7. "github.com/sirupsen/logrus"
  8. )
  9. func setOOMScore(pid, score int) error {
  10. oomScoreAdjPath := fmt.Sprintf("/proc/%d/oom_score_adj", pid)
  11. f, err := os.OpenFile(oomScoreAdjPath, os.O_WRONLY, 0)
  12. if err != nil {
  13. return err
  14. }
  15. stringScore := strconv.Itoa(score)
  16. _, err = f.WriteString(stringScore)
  17. f.Close()
  18. if os.IsPermission(err) {
  19. // Setting oom_score_adj does not work in an
  20. // unprivileged container. Ignore the error, but log
  21. // it if we appear not to be in that situation.
  22. if !system.RunningInUserNS() {
  23. logrus.Debugf("Permission denied writing %q to %s", stringScore, oomScoreAdjPath)
  24. }
  25. return nil
  26. }
  27. return err
  28. }