Skip to content

Commit 8305706

Browse files
committed
Keep IsCgroup2UnifiedMode funcs
Signed-off-by: Lokesh Mandvekar <lsm5@redhat.com>
1 parent 417ec98 commit 8305706

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

common/pkg/cgroups/cgroups_linux.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"path/filepath"
1414
"strconv"
1515
"strings"
16+
"sync"
1617
"syscall"
1718
"time"
1819

@@ -29,7 +30,13 @@ import (
2930
var (
3031
// ErrCgroupDeleted means the cgroup was deleted.
3132
ErrCgroupDeleted = errors.New("cgroup deleted")
32-
ErrStatCgroup = errors.New("no cgroup available for gathering user statistics")
33+
// ErrCgroupV1Rootless means the cgroup v1 were attempted to be used in rootless environment.
34+
ErrCgroupV1Rootless = errors.New("no support for CGroups V1 in rootless environments")
35+
ErrStatCgroup = errors.New("no cgroup available for gathering user statistics")
36+
37+
isUnifiedOnce sync.Once
38+
isUnified bool
39+
isUnifiedErr error
3340
)
3441

3542
// CgroupControl controls a cgroup hierarchy.
@@ -600,6 +607,19 @@ func SystemCPUUsage() (uint64, error) {
600607
return total, nil
601608
}
602609

610+
// IsCgroup2UnifiedMode returns whether we are running in cgroup 2 cgroup2 mode.
611+
func IsCgroup2UnifiedMode() (bool, error) {
612+
isUnifiedOnce.Do(func() {
613+
var st syscall.Statfs_t
614+
if err := syscall.Statfs("/sys/fs/cgroup", &st); err != nil {
615+
isUnified, isUnifiedErr = false, err
616+
} else {
617+
isUnified, isUnifiedErr = st.Type == unix.CGROUP2_SUPER_MAGIC, nil
618+
}
619+
})
620+
return isUnified, isUnifiedErr
621+
}
622+
603623
// UserConnection returns an user connection to D-BUS.
604624
func UserConnection(uid int) (*systemdDbus.Conn, error) {
605625
return systemdDbus.NewConnection(func() (*dbus.Conn, error) {

common/pkg/cgroups/cgroups_unsupported.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
package cgroups
44

5+
// IsCgroup2UnifiedMode returns whether we are running in cgroup 2 cgroup2 mode.
6+
func IsCgroup2UnifiedMode() (bool, error) {
7+
return false, nil
8+
}
9+
510
// UserOwnsCurrentSystemdCgroup checks whether the current EUID owns the
611
// current cgroup.
712
func UserOwnsCurrentSystemdCgroup() (bool, error) {

0 commit comments

Comments
 (0)