@@ -13,6 +13,7 @@ import (
1313 "path/filepath"
1414 "strconv"
1515 "strings"
16+ "sync"
1617 "syscall"
1718 "time"
1819
@@ -29,7 +30,13 @@ import (
2930var (
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.
604624func UserConnection (uid int ) (* systemdDbus.Conn , error ) {
605625 return systemdDbus .NewConnection (func () (* dbus.Conn , error ) {
0 commit comments