kernel

package
v0.0.0-...-3ed63b5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 2, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package kernel is utilities for the Linux kernel

Index

Constants

This section is empty.

Variables

View Source
var BootRoot = funcs.MemoizeNoError(func() string {
	if v := os.Getenv("HOST_BOOT"); v != "" {
		return v
	}
	if env.IsContainerized() {
		if _, err := os.Stat("/host"); err == nil {
			return "/host/boot"
		}
	}
	return "/boot"
})

BootRoot retrieves the current boot dir we should use

View Source
var ErrMemFdFileNotFound = errors.New("memfd file not found")

ErrMemFdFileNotFound is an error for when there was no file found for a given process.

View Source
var Family = funcs.Memoize(func() (string, error) {
	pi, err := platformInformation()
	return pi.family, err
})

Family is the string describing the Linux distribution family (rhel, debian, etc.)

View Source
var Machine = funcs.Memoize(func() (string, error) {
	u, err := uname()
	if err != nil {
		return "", err
	}
	return unix.ByteSliceToString(u.Machine[:]), nil
})

Machine is the equivalent of uname -m

View Source
var OnlineCPUs = funcs.Memoize(func() ([]uint, error) {
	return parseCPUMultipleRangeFromFile("/sys/devices/system/cpu/online")
})

OnlineCPUs returns the individual CPU numbers that are online for the system

View Source
var Platform = funcs.Memoize(func() (string, error) {
	pi, err := platformInformation()
	return pi.platform, err
})

Platform is the string describing the Linux distribution (ubuntu, debian, fedora, etc.)

View Source
var PlatformVersion = funcs.Memoize(func() (string, error) {
	pi, err := platformInformation()
	return pi.version, err
})

PlatformVersion is the string describing the platform version (`22.04` for Ubuntu jammy, etc.)

View Source
var PossibleCPUs = funcs.Memoize(func() (int, error) {
	return parseCPUSingleRangeFromFile("/sys/devices/system/cpu/possible")
})

PossibleCPUs returns the max number of CPUs a system may possibly have Logical CPU numbers must be of the form 0-n

View Source
var ProcFSRoot = funcs.MemoizeNoError(procFsRoot)

ProcFSRoot retrieves the current procfs dir we should use

View Source
var Release = funcs.Memoize(func() (string, error) {
	u, err := uname()
	if err != nil {
		return "", err
	}
	return unix.ByteSliceToString(u.Release[:]), nil
})

Release is the equivalent of uname -r

View Source
var RootNSPID = funcs.Memoize(func() (int, error) {
	pidPath := filepath.Join(ProcFSRoot(), "self")
	pidStr, err := os.Readlink(pidPath)
	if err != nil {
		return 0, err
	}

	return strconv.Atoi(pidStr)
})

RootNSPID returns the current PID from the root namespace

View Source
var SysFSRoot = funcs.MemoizeNoError(func() string {
	if v := os.Getenv("HOST_SYS"); v != "" {
		return v
	}
	if os.Getenv("DOCKER_DD_AGENT") != "" {
		if _, err := os.Stat("/host"); err == nil {
			return "/host/sys"
		}
	}
	return "/sys"
})

SysFSRoot retrieves the current sysfs dir we should use

View Source
var UnameVersion = funcs.Memoize(func() (string, error) {
	u, err := uname()
	if err != nil {
		return "", err
	}
	return unix.ByteSliceToString(u.Version[:]), nil
})

UnameVersion is the equivalent of uname -v

Functions

func AllPidsProcs

func AllPidsProcs(procRoot string) ([]int, error)

AllPidsProcs will return all pids under procRoot

func Arch

func Arch() string

Arch returns the kernel architecture value, often used within the kernel `include/arch` directory.

func GetProcessEnvVariable

func GetProcessEnvVariable(pid int, procRoot string, envVar string) (string, error)

GetProcessEnvVariable retrieves the given environment variable for the specified process ID, without loading the entire environment into memory. Will return an empty string if the variable is not found.

func GetProcessMemFdFile

func GetProcessMemFdFile(pid int, procRoot string, memFdFileName string, memFdMaxSize int) ([]byte, error)

GetProcessMemFdFile reads a maximum amount of bytes from the memFdFileName if it was found.

func HostBoot

func HostBoot(combineWith ...string) string

HostBoot returns the location of a host's /boot folder. This can and will be overridden when running inside a container.

func HostProc

func HostProc(combineWith ...string) string

HostProc returns the location of a host's procfs. This can and will be overridden when running inside a container.

func HostSys

func HostSys(combineWith ...string) string

HostSys returns the location of a host's sysfs. This can and will be overridden when running inside a container.

func IsIPv6Enabled

func IsIPv6Enabled() bool

IsIPv6Enabled returns whether IPv6 has been enabled on the host

func MountInfoPidPath

func MountInfoPidPath(pid int32) string

MountInfoPidPath returns the path to the mountinfo file of a pid in /proc

func ParseMountInfoFile

func ParseMountInfoFile(pid int32) ([]*mountinfo.Info, error)

ParseMountInfoFile collects the mounts for a specific process ID.

func ProcessExists

func ProcessExists(pid int) bool

ProcessExists returns true if the process exists in the procfs

func ReadMemFdFile

func ReadMemFdFile(path string, memFdMaxSize int) ([]byte, error)

ReadMemFdFile reads a maximum amount of bytes from the memfd file at the given path. The path should be a full path to an fd (e.g., /proc/1234/fd/5).

func WithAllProcs

func WithAllProcs(procRoot string, fn func(int) error) error

WithAllProcs will execute `fn` for every pid under procRoot. `fn` is passed the `pid`. If `fn` returns an error the iteration aborts, returning the last error returned from `fn`.

Types

type Integer

type Integer interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

Integer represents all possible integer types. Remove when x/exp/constraints is moved to the standard library.

type LockdownMode

type LockdownMode string

LockdownMode defines a lockdown type

const (
	// None mode
	None LockdownMode = "none"
	// Integrity mode
	Integrity LockdownMode = "integrity"
	// Confidentiality mode
	Confidentiality LockdownMode = "confidentiality"
	// Unknown mode
	Unknown LockdownMode = "unknown"
)

func GetLockdownMode

func GetLockdownMode() LockdownMode

GetLockdownMode returns the lockdown

type UbuntuKernelVersion

type UbuntuKernelVersion struct {
	Major  int
	Minor  int
	Patch  int // always 0
	Abi    int
	Flavor string
}

UbuntuKernelVersion represents a version from an ubuntu kernel Please see: https://ubuntu.com/kernel for the documentation of this scheme

func NewUbuntuKernelVersion

func NewUbuntuKernelVersion(unameRelease string) (*UbuntuKernelVersion, error)

NewUbuntuKernelVersion parses the ubuntu release string and returns a structure with each extracted fields

type Version

type Version uint32

Version is a numerical representation of a kernel version

func HostVersion

func HostVersion() (Version, error)

HostVersion returns the running kernel version of the host

func MustHostVersion

func MustHostVersion() Version

MustHostVersion returns the running kernel version of the host

func ParseReleaseString

func ParseReleaseString(releaseString string) (Version, error)

ParseReleaseString converts a release string with format 4.4.2[-1] to a kernel version number in LINUX_VERSION_CODE format. That is, for kernel "a.b.c", the version number will be (a<<16 + b<<8 + c)

func ParseVersion

func ParseVersion(s string) Version

ParseVersion parses a string in the format of x.x.x to a Version

func VersionCode

func VersionCode(major, minor, patch byte) Version

VersionCode returns a Version computed from the individual parts of a x.x.x version

func (Version) Major

func (v Version) Major() uint8

Major returns the major number of the version code

func (Version) Minor

func (v Version) Minor() uint8

Minor returns the minor number of the version code

func (Version) Patch

func (v Version) Patch() uint8

Patch returns the patch number of the version code

func (Version) String

func (v Version) String() string

String returns a string representing the version in x.x.x format

Directories

Path Synopsis
Package netns provides utility functions for network ns handling on linux platform.
Package netns provides utility functions for network ns handling on linux platform.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL