Documentation
¶
Overview ¶
Package libproc provides zero-cgo Darwin bindings for /usr/lib/libproc.dylib, wrapping process enumeration, process metadata, and file-descriptor inspection from <libproc.h> and <sys/proc_info.h>.
ListAllPIDs, ListPIDs, and ListPIDsByPath enumerate running process IDs via proc_listpids and proc_listpidspath.
PIDPath and Name query a process's executable path and registered name via proc_pidpath and proc_name.
BSDInfo, TaskInfo, TaskAllInfo, VnodePathInfo, and ListFDs wrap proc_pidinfo to inspect BSD process identity (parent PID, UIDs/GIDs, start time), Mach task resource counters, working directories, and open file descriptors.
FDVnodePathInfo, FDSocketInfo, and FDPipeInfo wrap proc_pidfdinfo to inspect individual vnode, socket, and pipe descriptors.
On non-Darwin platforms, all functions return ErrUnsupported.
Basic usage:
pid := os.Getpid()
path, err := libproc.PIDPath(pid)
if err != nil {
log.Fatal(err)
}
info, err := libproc.BSDInfo(pid)
if err != nil {
log.Fatal(err)
}
fmt.Printf("pid=%d ppid=%d started=%s exe=%s\n", info.PID, info.PPID, info.StartTime, path)
Index ¶
- Variables
- func ListAllPIDs() ([]int, error)
- func ListPIDs(filter PIDFilter, typeInfo uint32) ([]int, error)
- func ListPIDsByPath(filter PIDFilter, typeInfo uint32, path string, flags PathFlag) ([]int, error)
- func Name(pid int) (string, error)
- func PIDPath(pid int) (string, error)
- type FDInfo
- type FDType
- type FileInfo
- type PIDFilter
- type PathFlag
- type PipeFDInfo
- type ProcBSDInfo
- type ProcTaskAllInfo
- type ProcTaskInfo
- type ProcVnodePathInfo
- type SocketFDInfo
- type SocketKind
- type TCPState
- type VnodeFDInfo
- type VnodeStat
Constants ¶
This section is empty.
Variables ¶
var ErrUnsupported = errors.New("libproc: operation not supported")
ErrUnsupported reports that libproc operations are not supported on the current operating system.
Functions ¶
func ListAllPIDs ¶
ListAllPIDs returns the process IDs of all running processes.
func ListPIDsByPath ¶
ListPIDsByPath returns the process IDs holding an open file reference matching path.
Types ¶
type FDInfo ¶
FDInfo describes a single open file descriptor entry from proc_pidinfo(PROC_PIDLISTFDS).
type FDType ¶
type FDType uint32
FDType identifies the kind of an open file descriptor returned by ListFDs, corresponding to PROX_FDTYPE_* constants in <sys/proc_info.h>.
const ( FDTypeATalk FDType = 0 // PROX_FDTYPE_ATALK FDTypeVnode FDType = 1 // PROX_FDTYPE_VNODE FDTypeSocket FDType = 2 // PROX_FDTYPE_SOCKET FDTypePSHM FDType = 3 // PROX_FDTYPE_PSHM FDTypePSEM FDType = 4 // PROX_FDTYPE_PSEM FDTypeKqueue FDType = 5 // PROX_FDTYPE_KQUEUE FDTypePipe FDType = 6 // PROX_FDTYPE_PIPE FDTypeFSEvents FDType = 7 // PROX_FDTYPE_FSEVENTS FDTypeNetPolicy FDType = 9 // PROX_FDTYPE_NETPOLICY FDTypeChannel FDType = 10 // PROX_FDTYPE_CHANNEL FDTypeNexus FDType = 11 // PROX_FDTYPE_NEXUS )
type PIDFilter ¶
type PIDFilter uint32
PIDFilter selects which processes ListPIDs and ListPIDsByPath enumerate, corresponding to the PROC_*_ONLY constants in <sys/proc_info.h>.
type PathFlag ¶
type PathFlag uint32
PathFlag controls how ListPIDsByPath matches open file references, corresponding to PROC_LISTPIDSPATH_* constants in <libproc.h>.
type PipeFDInfo ¶
PipeFDInfo holds pipe file-descriptor details from proc_pidfdinfo(PROC_PIDFDPIPEINFO).
func FDPipeInfo ¶
func FDPipeInfo(pid, fd int) (PipeFDInfo, error)
FDPipeInfo returns pipe metadata for fd in pid.
type ProcBSDInfo ¶
type ProcBSDInfo struct {
Flags uint32
Status uint32
ExitStatus uint32
PID uint32
PPID uint32
UID uint32
GID uint32
RUID uint32
RGID uint32
SVUID uint32
SVGID uint32
Comm string
Name string
NFiles uint32
PGID uint32
PJobc uint32
ETDev uint32
ETPGID uint32
Nice int32
StartTime time.Time
}
ProcBSDInfo holds BSD process metadata from proc_pidinfo(PROC_PIDTBSDINFO).
func BSDInfo ¶
func BSDInfo(pid int) (ProcBSDInfo, error)
BSDInfo returns BSD process metadata for pid.
type ProcTaskAllInfo ¶
type ProcTaskAllInfo struct {
BSD ProcBSDInfo
Task ProcTaskInfo
}
ProcTaskAllInfo combines ProcBSDInfo and ProcTaskInfo from a single proc_pidinfo(PROC_PIDTASKALLINFO) call.
func TaskAllInfo ¶
func TaskAllInfo(pid int) (ProcTaskAllInfo, error)
TaskAllInfo returns both BSD and Mach task metadata for pid.
type ProcTaskInfo ¶
type ProcTaskInfo struct {
VirtualSize uint64
ResidentSize uint64
TotalUser uint64
TotalSystem uint64
ThreadsUser uint64
ThreadsSystem uint64
Policy int32
Faults int32
Pageins int32
COWFaults int32
MessagesSent int32
MessagesReceived int32
SyscallsMach int32
SyscallsUnix int32
ContextSwitches int32
ThreadCount int32
NumRunning int32
Priority int32
}
ProcTaskInfo holds Mach task memory, CPU, and thread counters from proc_pidinfo(PROC_PIDTASKINFO).
func TaskInfo ¶
func TaskInfo(pid int) (ProcTaskInfo, error)
TaskInfo returns Mach task memory, CPU, and thread counters for pid.
type ProcVnodePathInfo ¶
ProcVnodePathInfo holds a process's current working directory and root directory from proc_pidinfo(PROC_PIDVNODEPATHINFO). RootDir is empty when the process has not called chroot.
func VnodePathInfo ¶
func VnodePathInfo(pid int) (ProcVnodePathInfo, error)
VnodePathInfo returns the current working directory and root directory of pid.
type SocketFDInfo ¶
type SocketFDInfo struct {
File FileInfo
Family int32
Type int32
Protocol int32
State int16
Kind SocketKind
LocalAddr netip.Addr
LocalPort uint16
RemoteAddr netip.Addr
RemotePort uint16
TCPState TCPState
UnixPath string
UnixPeerPath string
VSockLocalCID uint32
VSockLocalPort uint32
VSockRemoteCID uint32
VSockRemotePort uint32
}
SocketFDInfo holds socket file-descriptor details from proc_pidfdinfo(PROC_PIDFDSOCKETINFO).
func FDSocketInfo ¶
func FDSocketInfo(pid, fd int) (SocketFDInfo, error)
FDSocketInfo returns socket metadata for fd in pid.
type SocketKind ¶
type SocketKind int32
SocketKind identifies the protocol-specific union member in SocketFDInfo, corresponding to SOCKINFO_* constants in <sys/proc_info.h>.
const ( SocketGeneric SocketKind = 0 // SOCKINFO_GENERIC SocketIn SocketKind = 1 // SOCKINFO_IN SocketTCP SocketKind = 2 // SOCKINFO_TCP SocketUnix SocketKind = 3 // SOCKINFO_UN SocketNDRV SocketKind = 4 // SOCKINFO_NDRV SocketKernEvent SocketKind = 5 // SOCKINFO_KERN_EVENT SocketKernCtl SocketKind = 6 // SOCKINFO_KERN_CTL SocketVSock SocketKind = 7 // SOCKINFO_VSOCK )
func (SocketKind) String ¶
func (k SocketKind) String() string
String returns a human-readable name for the socket kind.
type TCPState ¶
type TCPState int32
TCPState represents the TCP connection state reported in SocketFDInfo, corresponding to TSI_S_* constants in <sys/proc_info.h>.
const ( TCPClosed TCPState = 0 // TSI_S_CLOSED TCPListen TCPState = 1 // TSI_S_LISTEN TCPSynSent TCPState = 2 // TSI_S_SYN_SENT TCPSynReceived TCPState = 3 // TSI_S_SYN_RECEIVED TCPEstablished TCPState = 4 // TSI_S_ESTABLISHED TCPCloseWait TCPState = 5 // TSI_S__CLOSE_WAIT TCPFinWait1 TCPState = 6 // TSI_S_FIN_WAIT_1 TCPClosing TCPState = 7 // TSI_S_CLOSING TCPLastAck TCPState = 8 // TSI_S_LAST_ACK TCPFinWait2 TCPState = 9 // TSI_S_FIN_WAIT_2 TCPTimeWait TCPState = 10 // TSI_S_TIME_WAIT TCPReserved TCPState = 11 // TSI_S_RESERVED )
type VnodeFDInfo ¶
VnodeFDInfo holds vnode file-descriptor details and resolved path from proc_pidfdinfo(PROC_PIDFDVNODEPATHINFO).
func FDVnodePathInfo ¶
func FDVnodePathInfo(pid, fd int) (VnodeFDInfo, error)
FDVnodePathInfo returns vnode metadata and resolved path for fd in pid.