summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiuseppe Scrivano <gscrivan@redhat.com>2022-07-12 10:55:39 +0200
committerGiuseppe Scrivano <gscrivan@redhat.com>2022-07-13 09:13:06 +0200
commit16b8d77f9ef5a06b90d1507fff1f9c21fbbcd0cb (patch)
tree40a2893bd9dc606c21712a5a9114f7636f6e885b
parent4b547a72ab32f86537fec0907c0fbbb98595a688 (diff)
downloadpodman-16b8d77f9ef5a06b90d1507fff1f9c21fbbcd0cb.tar.gz
podman-16b8d77f9ef5a06b90d1507fff1f9c21fbbcd0cb.tar.bz2
podman-16b8d77f9ef5a06b90d1507fff1f9c21fbbcd0cb.zip
utils: call MaybeMoveToSubCgroup once
memoize its result and use it for subsequent calls. Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
-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
}