state.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. Copyright The containerd Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cgroup2
  14. import (
  15. "os"
  16. "path/filepath"
  17. "strings"
  18. )
  19. // State is a type that represents the state of the current cgroup
  20. type State string
  21. const (
  22. Unknown State = ""
  23. Thawed State = "thawed"
  24. Frozen State = "frozen"
  25. Deleted State = "deleted"
  26. cgroupFreeze = "cgroup.freeze"
  27. )
  28. func (s State) Values() []Value {
  29. v := Value{
  30. filename: cgroupFreeze,
  31. }
  32. switch s {
  33. case Frozen:
  34. v.value = "1"
  35. case Thawed:
  36. v.value = "0"
  37. }
  38. return []Value{
  39. v,
  40. }
  41. }
  42. func fetchState(path string) (State, error) {
  43. current, err := os.ReadFile(filepath.Join(path, cgroupFreeze))
  44. if err != nil {
  45. return Unknown, err
  46. }
  47. switch strings.TrimSpace(string(current)) {
  48. case "1":
  49. return Frozen, nil
  50. case "0":
  51. return Thawed, nil
  52. default:
  53. return Unknown, nil
  54. }
  55. }