system

package module
v0.60.1 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2024 License: Apache-2.0 Imports: 18 Imported by: 2

Documentation

Overview

Package system provides various helper functions and types to interact with system information

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CountProcessFileDescriptors

func CountProcessFileDescriptors(procPath string, pid int) (int, error)

CountProcessFileDescriptors gets the number of open file descriptors for a given pid

func CountProcessesFileDescriptors

func CountProcessesFileDescriptors(procPath string, pids []int) (uint64, bool)

CountProcessesFileDescriptors returns the sum of open file descriptors for all given PIDs. Failed PIDs are silently skipped. A boolean is returned to indicate whether all PIDs failed or not.

func GetDefaultGateway

func GetDefaultGateway(procPath string) (net.IP, error)

GetDefaultGateway parses /proc/net/route and extract the first route with Destination == "00000000"

func GetFileInode added in v0.57.0

func GetFileInode(path string) (uint64, error)

GetFileInode returns the inode number of a file

func GetProcessNamespaceInode

func GetProcessNamespaceInode(procPath string, pid string, namespace string) (uint64, error)

GetProcessNamespaceInode performs a stat() call on /proc/<pid>/ns/<namespace> When targeting PID different than self, requires CAP_SYS_PTRACE to be able to read /proc/<pid>/ns/<namespace> in the first place

func GetProcessNetDevInode added in v0.57.0

func GetProcessNetDevInode(procPath string, pid int) (uint64, error)

GetProcessNetDevInode returns the inode of /proc/<pid>/net/dev

func HostCPUCount

func HostCPUCount() int

HostCPUCount returns the number of logical CPUs from host

func IsLocalAddress

func IsLocalAddress(address string) (string, error)

IsLocalAddress returns the given address if it is local or an error if it is not

func IsProcessHostNetwork

func IsProcessHostNetwork(procPath string, netDevInode uint64) *bool

IsProcessHostNetwork returns true if the process is using the host network by checking if /proc/<pid>/net/dev has the same inode as /proc/1/net/dev

func IsProcessHostUTSNamespace

func IsProcessHostUTSNamespace(_ string, namespaceID uint64) *bool

IsProcessHostUTSNamespace compares namespaceID with known, harcoded host PID Namespace inode Keeps same signature as `IsProcessHostNetwork` as we may need to change implementation depending on Kernel evolution

func ParseProcessIPs

func ParseProcessIPs(procPath string, pid int, filterFunc func(string) bool) ([]string, error)

ParseProcessIPs parses /proc/<pid>/net/fib_trie and returns the /32 IP addresses found. The result does not contain duplicate IPs.

Here's an example of /proc/<pid>/net/fib_trie that shows its format:

Main:
  +-- 0.0.0.0/1 2 0 2
     +-- 0.0.0.0/4 2 0 2
        |-- 0.0.0.0
           /0 universe UNICAST
        +-- 10.4.0.0/24 2 1 2
           |-- 10.4.0.0
              /32 link BROADCAST
              /24 link UNICAST
           +-- 10.4.0.192/26 2 0 2
              |-- 10.4.0.216
                 /32 host LOCAL
              |-- 10.4.0.255
                 /32 link BROADCAST
     +-- 127.0.0.0/8 2 0 2
        +-- 127.0.0.0/31 1 0 0
           |-- 127.0.0.0
              /32 link BROADCAST
              /8 host LOCAL
           |-- 127.0.0.1
              /32 host LOCAL
        |-- 127.255.255.255
           /32 link BROADCAST
Local:
  +-- 0.0.0.0/1 2 0 2
     +-- 0.0.0.0/4 2 0 2
        |-- 0.0.0.0
           /0 universe UNICAST
        +-- 10.4.0.0/24 2 1 2
           |-- 10.4.0.0
              /32 link BROADCAST
              /24 link UNICAST
           +-- 10.4.0.192/26 2 0 2
              |-- 10.4.0.216
                 /32 host LOCAL
              |-- 10.4.0.255
                 /32 link BROADCAST
     +-- 127.0.0.0/8 2 0 2
        +-- 127.0.0.0/31 1 0 0
           |-- 127.0.0.0
              /32 link BROADCAST
              /8 host LOCAL
           |-- 127.0.0.1
              /32 host LOCAL
        |-- 127.255.255.255
           /32 link BROADCAST

The IPs that we're interested in are the ones that appear above lines that contain "/32 host".

Types

type NetworkRoute

type NetworkRoute struct {
	Interface string
	Subnet    uint64
	Gateway   uint64
	Mask      uint64
}

NetworkRoute holds one network destination subnet and it's linked interface name

func ParseProcessRoutes

func ParseProcessRoutes(procPath string, pid int) ([]NetworkRoute, error)

ParseProcessRoutes parses /proc/<pid>/net/route into a list of NetworkDestionation If PID is 0, it parses /proc/net/route instead

Directories

Path Synopsis
socket module

Jump to

Keyboard shortcuts

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