summaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
Diffstat (limited to 'utils')
-rw-r--r--utils/utils.go40
1 files changed, 25 insertions, 15 deletions
diff --git a/utils/utils.go b/utils/utils.go
index 7cf28fda5..aa1c6a958 100644
--- a/utils/utils.go
+++ b/utils/utils.go
@@ -191,22 +191,32 @@ func MovePauseProcessToScope(pausePidPath string) {
}
}
+var (
+ maybeMoveToSubCgroupSync sync.Once
+ maybeMoveToSubCgroupSyncErr error
+)
+
// MaybeMoveToSubCgroup moves the current process in a sub cgroup when
// it is running in the root cgroup on a system that uses cgroupv2.
func MaybeMoveToSubCgroup() error {
- unifiedMode, err := cgroups.IsCgroup2UnifiedMode()
- if err != nil {
- return err
- }
- if !unifiedMode {
- return nil
- }
- cgroup, err := GetOwnCgroup()
- if err != nil {
- return err
- }
- if cgroup == "/" {
- return MoveUnderCgroupSubtree("init")
- }
- return nil
+ maybeMoveToSubCgroupSync.Do(func() {
+ unifiedMode, err := cgroups.IsCgroup2UnifiedMode()
+ if err != nil {
+ maybeMoveToSubCgroupSyncErr = err
+ return
+ }
+ if !unifiedMode {
+ maybeMoveToSubCgroupSyncErr = nil
+ return
+ }
+ cgroup, err := GetOwnCgroup()
+ if err != nil {
+ maybeMoveToSubCgroupSyncErr = err
+ return
+ }
+ if cgroup == "/" {
+ maybeMoveToSubCgroupSyncErr = MoveUnderCgroupSubtree("init")
+ }
+ })
+ return maybeMoveToSubCgroupSyncErr
}