johnsonjh/gfpsgo

View on GitHub
internal/cgroups/cgroups.go

Summary

Maintainability
A
0 mins
Test Coverage
// Copyright 2021 Jeffrey H. Johnson <trnsz@pobox.com>
// Copyright 2019 psgo authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cgroups

import (
    "sync"
    "syscall"
)

const (
    // CgroupRoot ...
    CgroupRoot        = "/sys/fs/cgroup"
    cgroup2SuperMagic = 0x63677270
)

var (
    isUnifiedOnce sync.Once
    isUnified     bool
    isUnifiedErr  error
)

// IsCgroup2UnifiedMode returns whether we are running in cgroup or cgroupv2
// mode.
func IsCgroup2UnifiedMode() (bool, error) {
    isUnifiedOnce.Do(func() {
        var st syscall.Statfs_t
        if err := syscall.Statfs(CgroupRoot, &st); err != nil {
            isUnified, isUnifiedErr = false, err
        } else {
            isUnified, isUnifiedErr = st.Type == cgroup2SuperMagic, nil
        }
    })
    return isUnified, isUnifiedErr
}