proc

package
v0.23.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StatLastField                 = StatExitCode
	StatMaxNumFields              = StatLastField + 1
	StatReadFileInitialBufferSize = 256 // greater than average size (~95) calculated from ~1.4k stat files
)
View Source
const (
	StatusLastField                 = x86ThreadFeaturesLocked
	StatusMaxNumFields              = StatusLastField + 1
	StatusReadFileInitialBufferSize = 1480 // greater than average size (~1290) calculated from ~700 status files
)

Variables

This section is empty.

Functions

func GetAllBinaryProcs added in v0.11.0

func GetAllBinaryProcs() (map[string][]int32, error)

GetProcNS returns the namespace ID of a given namespace and process. To do so, it requires access to the /proc file system of the host, and CAP_SYS_PTRACE capability.

func GetMountNSFirstProcesses

func GetMountNSFirstProcesses() (map[uint32]int32, error)

GetMountNSFirstProcesses return mapping between mount NS to its first process (aka, the process with the oldest start time in the mount NS)

func GetProcBinary added in v0.11.0

func GetProcBinary(pid int32) (string, error)

GetProcNS returns the namespace ID of a given namespace and process. To do so, it requires access to the /proc file system of the host, and CAP_SYS_PTRACE capability.

func GetProcExePath added in v0.23.0

func GetProcExePath(pid int32) string

GetProcExePath returns the path to the executable of a process given its PID. The path is in the form /proc/<pid>/exe.

func GetProcNS

func GetProcNS(pid int32, nsName string) (uint32, error)

GetProcNS returns the namespace ID of a given namespace and process. To do so, it requires access to the /proc file system of the host, and CAP_SYS_PTRACE capability.

func GetProcNSDirPath added in v0.23.0

func GetProcNSDirPath(pid int32) string

GetProcNSDirPath returns the path to the directory containing the namespaces of a process given its PID. The path is in the form /proc/<pid>/ns.

func GetProcNSPath added in v0.23.0

func GetProcNSPath(pid int32, nsName string) string

GetProcNSPath returns the path to a specific namespace of a process given its PID and the namespace name. The path is in the form /proc/<pid>/ns/<nsName>.

func GetStatPath added in v0.23.0

func GetStatPath(pid int32) string

GetStatPath returns the path to the stat file of a process given its PID. The path is in the form /proc/<pid>/stat.

func GetStatusPath added in v0.23.0

func GetStatusPath(pid int32) string

GetStatusPath returns the path to the status file of a process given its PID. The path is in the form /proc/<pid>/status.

func GetTaskPath added in v0.23.0

func GetTaskPath(pid int32) string

GetTaskPath returns the path to the task directory of a process given its PID. The path is in the form /proc/<pid>/task.

func GetTaskStatPath added in v0.23.0

func GetTaskStatPath(pid, tid int32) string

GetTaskStatPath returns the path to the stat file of a thread given its PID and TID. The path is in the form /proc/<pid>/task/<tid>/stat.

func GetTaskStatusPath added in v0.23.0

func GetTaskStatusPath(pid, tid int32) string

GetTaskStatusPath returns the path to the status file of a thread given its PID and TID. The path is in the form /proc/<pid>/task/<tid>/status.

func ParseInt32 added in v0.23.0

func ParseInt32(value string) (int32, error)

func ParseInt64 added in v0.23.0

func ParseInt64(value string) (int64, error)

func ParseUint32 added in v0.23.0

func ParseUint32(value string) (uint32, error)

func ParseUint64 added in v0.23.0

func ParseUint64(value string) (uint64, error)

func ReadFile added in v0.23.0

func ReadFile(filePath string, initialBufferSize int) ([]byte, error)

ReadFile reads the content of a file and returns it as a byte slice. This function is specifically optimized for reading small files in the /proc directory, where the file size reported by the stat syscall is often 0. It reads the file in chunks, dynamically growing the buffer as needed to ensure all content is retrieved efficiently.

Types

type ProcNS

type ProcNS struct {
	Cgroup          uint32
	Ipc             uint32
	Mnt             uint32
	Net             uint32
	Pid             uint32
	PidForChildren  uint32
	Time            uint32
	TimeForChildren uint32
	User            uint32
	Uts             uint32
}

https://elixir.bootlin.com/linux/v6.13/source/include/linux/ns_common.h#L12 struct ns_common inum member is unsigned int

func GetAllProcNS added in v0.11.0

func GetAllProcNS(pid int32) (*ProcNS, error)

GetAllProcNS return all the namespaces of a given process. To do so, it requires access to the /proc file system of the host, and CAP_SYS_PTRACE capability.

type ProcStat added in v0.18.0

type ProcStat struct {
	// contains filtered or unexported fields
}

ProcStat represents the minimal required fields of the /proc stat file.

func NewProcStat added in v0.18.0

func NewProcStat(pid int32) (*ProcStat, error)

NewProcStat reads the /proc/<pid>/stat file and parses it into a ProcStat struct. Populates all default fields.

func NewProcStatFields added in v0.23.0

func NewProcStatFields(pid int32, fields []StatField) (*ProcStat, error)

NewProcStatFields reads the /proc/<pid>/stat file and parses it into a ProcStat struct. Populates only the specified fields.

func NewThreadProcStat added in v0.18.0

func NewThreadProcStat(pid, tid int32) (*ProcStat, error)

NewThreadProcStat reads the /proc/<pid>/task/<tid>/stat file and parses it into a ProcStat struct. Populates all default fields.

func NewThreadProcStatFields added in v0.23.0

func NewThreadProcStatFields(pid, tid int32, fields []StatField) (*ProcStat, error)

NewThreadProcStatFields reads the /proc/<pid>/task/<tid>/stat file and parses it into a ProcStat struct. Populates only the specified fields.

func (*ProcStat) GetStartTime added in v0.23.0

func (s *ProcStat) GetStartTime() uint64

GetStartTime returns the time the process started after system boot (in clock ticks).

type ProcStatus added in v0.18.0

type ProcStatus struct {
	// contains filtered or unexported fields
}

ProcStatus represents the minimal required fields of the /proc status file.

func NewProcStatus added in v0.18.0

func NewProcStatus(pid int32) (*ProcStatus, error)

NewProcStatus reads the /proc/<pid>/status file and parses it into a ProcStatus struct. Populates all default fields.

func NewProcStatusFields added in v0.23.0

func NewProcStatusFields(pid int32, fields []StatusField) (*ProcStatus, error)

NewProcStatusFields reads the /proc/<pid>/status file and parses it into a ProcStatus struct. Populates only the specified fields.

func NewThreadProcStatus added in v0.18.0

func NewThreadProcStatus(pid, tid int32) (*ProcStatus, error)

NewThreadProcStatus reads the /proc/<pid>/task/<tid>/status file and parses it into a ProcStatus struct. Populates all default fields.

func NewThreadProcStatusFields added in v0.23.0

func NewThreadProcStatusFields(pid, tid int32, fields []StatusField) (*ProcStatus, error)

NewThreadProcStatusFields reads the /proc/<pid>/task/<tid>/status file and parses it into a ProcStatus struct. Populates only the specified fields.

func (*ProcStatus) GetName added in v0.18.0

func (s *ProcStatus) GetName() string

GetName returns the name of the process.

func (*ProcStatus) GetNsPPid added in v0.18.0

func (s *ProcStatus) GetNsPPid() int32

GetNsPPid returns process group ID in the namespace of the process.

func (*ProcStatus) GetNsPid added in v0.18.0

func (s *ProcStatus) GetNsPid() int32

GetNsPid returns process ID in the namespace of the process.

func (*ProcStatus) GetNsTgid added in v0.18.0

func (s *ProcStatus) GetNsTgid() int32

GetNsTgid returns thread group ID in the namespace of the process.

func (*ProcStatus) GetPPid added in v0.18.0

func (s *ProcStatus) GetPPid() int32

GetPPid returns the parent process ID.

func (*ProcStatus) GetPid added in v0.18.0

func (s *ProcStatus) GetPid() int32

GetPid returns the process ID.

func (*ProcStatus) GetTgid added in v0.18.0

func (s *ProcStatus) GetTgid() int32

GetTgid returns the thread group ID.

type StatField added in v0.23.0

type StatField byte
const (
	// There are signedness discrepancies between the fmt and the kernel C type in some cases, e.g.:
	// - StatCutime, StatCstime and StatCguestTime are `int64` in fmt but `u64` in kernel C type.
	// - StatRss is `int64` in fmt but `unsigned long` in kernel C type.
	// To avoid confusion, the parse type is based on the fmt since it is the representation made
	// available in the stat file. A conversion to the actual kernel C type should be done after parsing.
	//
	// parse type:    type to be used to parse the field value.
	// fmt:           format specifier string specified in stat man page.
	// kernel C type: actual type of the field in the kernel.
	//
	//                                       // parse type  // fmt  // kernel C type      // description
	//                                       // ----------  // ---  // ------------------ // ----------------------------------------------------------------------
	StatPid                 StatField = iota // int32          %d      pid_t (int)           process id
	StatComm                                 // string         %s      char[64]              the name of the task - up to 64 + 2 for ()
	StatState                                // byte           %c      char                  process state
	StatPpid                                 // int32          %d      pid_t (int)           parent process id
	StatPgrp                                 // int32          %d      pid_t (int)           process group id
	StatSession                              // int32          %d      pid_t (int)           session id
	StatTtyNr                                // int32          %d      int                   controlling terminal
	StatTpgid                                // int32          %d      int                   foreground process group id of the controlling terminal
	StatFlags                                // uint32         %u      unsigned int          process flags
	StatMinFlt                               // uint64         %lu     unsigned long         number of minor faults
	StatCminFlt                              // uint64         %lu     unsigned long         number of minor faults (all childs)
	StatMajFlt                               // uint64         %lu     unsigned long         number of major faults
	StatCmajFlt                              // uint64         %lu     unsigned long         number of major faults (all childs)
	StatUtime                                // uint64         %lu     u64                   user mode jiffies (clock ticks)
	StatStime                                // uint64         %lu     u64                   kernel mode jiffies (clock ticks)
	StatCutime                               // int64          %ld     u64                   user mode jiffies (all childs)
	StatCstime                               // int64          %ld     u64                   kernel mode jiffies (all childs)
	StatPriority                             // int32          %ld     int                   process priority
	StatNice                                 // int32          %ld     int                   process nice value
	StatNumThreads                           // int32          %ld     int                   number of threads in this process
	StatItRealValue                          // always 0 (obsolete)
	StatStartTime                            // uint64         %llu    unsigned long long    time the process started after system boot (in clock ticks)
	StatVsize                                // uint64         %lu     unsigned long         virtual memory size
	StatRss                                  // int64          %ld     unsigned long         resident set memory size
	StatRsslim                               // uint64         %lu     unsigned long         current limit in bytes on the rss
	StatStartcode                            // uint64         %lu     unsigned long         address above which program text can run
	StatEndcode                              // uint64         %lu     unsigned long         address below which program text can run
	StatStartStack                           // uint64         %lu     unsigned long         address of the start of the main process stack
	StatKstkesp                              // uint64         %lu     unsigned long         current value of stack pointer
	StatKstkeip                              // uint64         %lu     unsigned long         current value of instruction pointer
	StatSignal                               // uint64         %lu     unsigned long         bitmap of pending signals
	StatBlocked                              // uint64         %lu     unsigned long         bitmap of blocked signals
	StatSigIgnore                            // uint64         %lu     unsigned long         bitmap of ignored signals
	StatSigcatch                             // uint64         %lu     unsigned long         bitmap of catched signals
	StatWchan                                // uint64         %lu     unsigned long         address of the syscall where process is in sleep mode
	StatNswap                                // always 0 (not maintained)
	StatCnswap                               // always 0 (not maintained)
	StatExitSignal                           // int32          %d      int                   signal to be sent to parent when we die
	StatProcessor                            // uint32         %d      unsigned int          current CPU
	StatRtPriority                           // uint32         %u      unsigned int          realtime priority
	StatPolicy                               // uint32         %u      unsigned int          scheduling policy
	StatDelayacctBlkioTicks                  // uint64         %llu    u64                   time spent waiting for block IO
	StatGuestTime                            // uint64         %lu     u64                   guest time of the process
	StatCguestTime                           // int64          %ld     u64                   guest time of the process's children
	StatStartData                            // uint64         %lu     unsigned long         address above which program data+bss is placed
	StatEndData                              // uint64         %lu     unsigned long         address below which program data+bss is placed
	StatStartBrk                             // uint64         %lu     unsigned long         address above which program heap can be expanded with brk()
	StatArgStart                             // uint64         %lu     unsigned long         address above which program command line is placed
	StatArgEnd                               // uint64         %lu     unsigned long         address below which program command line is placed
	StatEnvStart                             // uint64         %lu     unsigned long         address above which program environment is placed
	StatEnvEnd                               // uint64         %lu     unsigned long         address below which program environment is placed
	StatExitCode                             // int32          %d      int                   the thread's exit_code in the form reported by the waitpid system call
)

Fields from /proc/<pid>/[task/<tid>/]stat file

type StatusField added in v0.23.0

type StatusField byte
const (
	//                        // parse type  // fmt  // kernel C type            // description
	//                        // ----------  // ---  // ----------------------   // ----------------------------------------------------------------------
	Name StatusField = iota // string         %s      char[64]                 // Name of the process
	// if umask >= 0
	Umask //                     int32          %#04o   int                      // Process umask, expressed in octal with a leading zero
	// endif umask >= 0
	//
	State     //                 byte           %c      char                     // State of the process (leftmost char of the field)
	Tgid      //                 uint64         %llu    pid_t (int)              // Thread group ID
	Ngid      //                 uint64         %llu    pid_t (int)              // NUMA group ID (if available)
	Pid       //                 uint64         %llu    pid_t (int)              // Process ID
	PPid      //                 uint64         %llu    pid_t (int)              // Parent Process ID
	TracerPid //                 uint64         %llu    pid_t (int)              // PID of process tracing this process
	Uid
	// (separator is '\t')
	// UidReal                   uint64         %llu    uid_t (unsigned int)     // Real UID
	// UidEffective              uint64         %llu    uid_t (unsigned int)     // Effective UID
	// UidSaved                  uint64         %llu    uid_t (unsigned int)     // Saved set UID
	// UidFS                     uint64         %llu    uid_t (unsigned int)     // Filesystem UID
	Gid
	// (separator is '\t')
	// GidReal                   uint64         %llu    gid_t (unsigned int)     // Real GID
	// GidEffective              uint64         %llu    gid_t (unsigned int)     // Effective GID
	// GidSaved                  uint64         %llu    gid_t (unsigned int)     // Saved set GID
	// GidFS                     uint64         %llu    gid_t (unsigned int)     // Filesystem GID
	FDSize //                    uint64         %llu    unsigned int             // Number of file descriptor slots currently allocated
	Groups //                    []uint64       %llu    gid_t (unsigned int)     // Supplementary group list (separator is ' ')
	//
	// #ifdef CONFIG_PID_NS
	NStgid  //                   []uint64       %llu    pid_t (int)              // Thread group ID in each of the PID namespaces of which pid is a member
	NSpid   //                   []uint64       %llu    pid_t (int)              // Thread ID in each of the PID namespaces of which pid is a member
	NSpgid  //                   []uint64       %llu    pid_t (int)              // Process group ID in each of the PID namespaces of which pid is a member
	NSsid   //                   []uint64       %llu    pid_t (int)              // Namespace session ID in each of the PID namespaces of which pid is a member
	Kthread //                   bool           %c      char                     // Is the process a kernel thread? (0 = no, 1 = yes)
	// #endif // CONFIG_PID_NS
	//
	// if mm != NULL
	VmPeak       //              uint64         %8llu   unsigned long            // Peak virtual memory size (kB)
	VmSize       //              uint64         %8llu   unsigned long            // Total program size (kB)
	VmLck        //              uint64         %8llu   unsigned long            // Locked memory size (kB)
	VmPin        //              uint64         %8llu   s64                      // Pinned memory size (guaranteed never to be swapped out) (kB)
	VmHWM        //              uint64         %8llu   unsigned long            // Peak resident set size ("high water mark") (kB)
	VmRSS        //              uint64         %8llu   unsigned long            // Resident set size (kB)
	RssAnon      //              uint64         %8llu   unsigned long            // Size of resident anonymous memory (kB)
	RssFile      //              uint64         %8llu   unsigned long            // Size of resident file mappings (kB)
	RssShmem     //              uint64         %8llu   unsigned long            // Size of shared memory resident (kB)
	VmData       //              uint64         %8llu   unsigned long            // Size of data segment (kB)
	VmStk        //              uint64         %8llu   unsigned long            // Size of stack segment (kB)
	VmExe        //              uint64         %8llu   unsigned long            // Size of text segment (kB)
	VmLib        //              uint64         %8llu   unsigned long            // Shared library code size (kB)
	VmPTE        //              uint64         %8llu   unsigned long            // Page table entries size (kB)
	VmSwap       //              uint64         %8llu   unsigned long            // Swap size (kB)
	HugetlbPages //              uint64         %8lu    long                     // Size of hugetlb memory portions (kB)
	// endif mm != NULL
	//
	CoreDumping //               bool           %llu    int                      // Is the process dumping core? (0 = no, 1 = yes)
	THPEnabled  //               bool           %d      bool                     // Is transparent huge pages enabled? (0 = no, 1 = yes)
	UntagMask   //               uint64         %#lx    unsigned long            // Untag mask
	Threads     //               uint64         %llu    int                      // Number of threads in process containing this thread
	SigQ
	// (separator is '/')
	// SigQ                      uint64         %llu    unsigned int             // Number of signals queued for the thread
	// SigQLimit                 uint64         %llu    unsigned long            // Resource limit on number of signals that can be queued
	SigPnd //                    uint64         %016llx sigset_t (unsigned long) // Mask of signals pending for the thread
	ShdPnd //                    uint64         %016llx sigset_t (unsigned long) // Mask of signals that thread is sharing
	SigBlk //                    uint64         %016llx sigset_t (unsigned long) // Mask of signals being blocked
	SigIgn //                    uint64         %016llx sigset_t (unsigned long) // Mask of signals being ignored
	SigCgt //                    uint64         %016llx sigset_t (unsigned long) // Mask of signals being caught
	//
	CapInh     //                uint64         %016llx cap_user_header_t (u64)  // Mask of inheritable capabilities
	CapPrm     //                uint64         %016llx cap_user_header_t (u64)  // Mask of permitted capabilities
	CapEff     //                uint64         %016llx cap_user_header_t (u64)  // Mask of effective capabilities
	CapBnd     //                uint64         %016llx cap_user_header_t (u64)  // Mask of capabilities bounding set
	CapAmb     //                uint64         %016llx cap_user_header_t (u64)  // Ambient capability set
	NoNewPrivs //                bool           %d      bool                     // Was the process started with no new privileges? (0 = no, 1 = yes)
	//
	// #ifdef CONFIG_SECCOMP
	Seccomp //                   uint64         %llu    int                      // Seccomp mode of the process
	// #endif // CONFIG_SECCOMP
	//
	// #ifdef CONFIG_SECCOMP_FILTER
	Seccomp_filters //           uint64         %llu    int                      // Number of seccomp filters attached to the process
	// #endif // CONFIG_SECCOMP_FILTER
	//
	SpeculationStoreBypass    // string         %s      char *                   // Speculation flaw mitigation state
	SpeculationIndirectBranch // string         %s      char *                   // Speculation indirect branch mitigation state
	//
	CpusAllowed     //           uint64         %016llx unsigned long            // Mask of CPUs on which this process may run
	CpusAllowedList //           string         %s      char *                   // Same as previous, but in "list format" (e.g. "0-3,5,7")
	//
	MemsAllowed     //           uint64         %016llx unsigned long            // Mask of memory nodes allowed to this process
	MemsAllowedList //           string         %s      char *                   // Same as previous, but in "list format" (e.g. "0-3,5,7")
	//
	VoluntaryCtxtSwitches    //  uint64         %llu    unsigned long            // Number of voluntary context switches
	NonVoluntaryCtxtSwitches //  uint64         %llu    unsigned long            // Number of involuntary context switches

)

Fields from /proc/<pid>/[task/<tid>/]status file:

Jump to

Keyboard shortcuts

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