libproc

package
v0.6.21 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

View Source
var ErrUnsupported = errors.New("libproc: operation not supported")

ErrUnsupported reports that libproc operations are not supported on the current operating system.

Functions

func ListAllPIDs

func ListAllPIDs() ([]int, error)

ListAllPIDs returns the process IDs of all running processes.

func ListPIDs

func ListPIDs(filter PIDFilter, typeInfo uint32) ([]int, error)

ListPIDs returns the process IDs matching filter and typeInfo.

func ListPIDsByPath

func ListPIDsByPath(filter PIDFilter, typeInfo uint32, path string, flags PathFlag) ([]int, error)

ListPIDsByPath returns the process IDs holding an open file reference matching path.

func Name

func Name(pid int) (string, error)

Name returns the registered name of pid.

func PIDPath

func PIDPath(pid int) (string, error)

PIDPath returns the executable path of pid.

Types

type FDInfo

type FDInfo struct {
	FD   int32
	Type FDType
}

FDInfo describes a single open file descriptor entry from proc_pidinfo(PROC_PIDLISTFDS).

func ListFDs

func ListFDs(pid int) ([]FDInfo, error)

ListFDs returns all open file descriptors of pid.

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
)

func (FDType) String

func (t FDType) String() string

String returns a human-readable name for the file descriptor type.

type FileInfo

type FileInfo struct {
	OpenFlags  uint32
	Status     uint32
	Offset     int64
	Type       int32
	GuardFlags uint32
}

FileInfo holds generic open-file metadata from struct proc_fileinfo.

type PIDFilter

type PIDFilter uint32

PIDFilter selects which processes ListPIDs and ListPIDsByPath enumerate, corresponding to the PROC_*_ONLY constants in <sys/proc_info.h>.

const (
	AllPIDs  PIDFilter = 1 // PROC_ALL_PIDS
	PGRPOnly PIDFilter = 2 // PROC_PGRP_ONLY
	TTYOnly  PIDFilter = 3 // PROC_TTY_ONLY
	UIDOnly  PIDFilter = 4 // PROC_UID_ONLY
	RUIDOnly PIDFilter = 5 // PROC_RUID_ONLY
	PPIDOnly PIDFilter = 6 // PROC_PPID_ONLY
	KDBGOnly PIDFilter = 7 // PROC_KDBG_ONLY
)

type PathFlag

type PathFlag uint32

PathFlag controls how ListPIDsByPath matches open file references, corresponding to PROC_LISTPIDSPATH_* constants in <libproc.h>.

const (
	PathIsVolume   PathFlag = 1 // PROC_LISTPIDSPATH_PATH_IS_VOLUME
	ExcludeEvtOnly PathFlag = 2 // PROC_LISTPIDSPATH_EXCLUDE_EVTONLY
)

type PipeFDInfo

type PipeFDInfo struct {
	File       FileInfo
	Handle     uint64
	PeerHandle uint64
	Status     int32
}

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

type ProcVnodePathInfo struct {
	Cwd     string
	RootDir string
}

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
)

func (TCPState) String

func (s TCPState) String() string

String returns a human-readable name for the TCP state.

type VnodeFDInfo

type VnodeFDInfo struct {
	File      FileInfo
	Stat      VnodeStat
	VnodeType int32
	Path      string
}

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.

type VnodeStat

type VnodeStat struct {
	Dev     uint32
	Mode    uint16
	Nlink   uint16
	Ino     uint64
	UID     uint32
	GID     uint32
	Size    int64
	Blocks  int64
	Blksize int32
	Flags   uint32
	Gen     uint32
	Rdev    uint32
}

VnodeStat holds file metadata from struct vinfo_stat.

Jump to

Keyboard shortcuts

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