process

package
v0.0.0-...-d7273bf Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2022 License: ISC Imports: 8 Imported by: 0

Documentation

Overview

Package process enumerates the process table for all processes or descendants of a process.

Index

Examples

Constants

View Source
const (
	// Procfs is the default mount point for procfs filesystems. The default
	// mountpoint can be changed by setting the PROC environment variable.
	Procfs = "/proc"

	ErrSearch = unix.ESRCH // No such process
)

Variables

View Source
var (
	ErrInvalid  = fs.ErrInvalid  // "invalid argument"
	ErrNotExist = fs.ErrNotExist // "file does not exist"
)

Functions

This section is empty.

Types

type Option

type Option func(*Ps)

func WithPid

func WithPid(pid int) Option

WithPid sets the process ID.

func WithProcfs

func WithProcfs(procfs string) Option

WithPid sets the location of the procfs mount point.

func WithSnapshot

func WithSnapshot(snapshot SnapshotStrategy) Option

WithSnapshot sets the method for discovering subprocesses.

type PID

type PID struct {
	Pid  int // process ID
	PPid int // parent process ID
}

PID contains the contents of /proc/stat for a process.

func Snapshot

func Snapshot(procfs string) (p []PID, err error)

Snapshot returns a snapshot of the system process table by walking through /proc.

type ProcChildren

type ProcChildren struct {
	*Ps
}

ProcChildren sets the configuration for generating a process snapshot by reading the procfs(5) children file:

A space-separated list of child tasks of this task.  Each child task
is represented by its TID.

The kernel must be compiled with CONFIG_PROC_CHILDREN enabled.

func (*ProcChildren) Children

func (ps *ProcChildren) Children() ([]int, error)

Children returns the list of subprocesses for a PID by reading /proc/self/task/*/children.

If CONFIG_PROC_CHILDREN is not enabled, the error is set to ErrNotExist.

type Process

type Process interface {
	Pid() int
	Children() ([]int, error)
	Snapshot() ([]PID, error)
}

func New

func New(opts ...Option) Process

New sets the default configuration state for the process.

type Ps

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

Ps contains the state for a process when scanning /proc.

func (*Ps) Children

func (ps *Ps) Children() ([]int, error)

Children returns a snapshot of the list of subprocesses for a PID by walking /proc.

Example
package main

import (
	"fmt"

	"github.com/msantos/goreap/process"
)

func main() {
	ps := process.New(process.WithPid(1))
	pids, err := ps.Children()
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(pids)
}
Output:

func (*Ps) Pid

func (ps *Ps) Pid() int

Pid retrieves the process identifier.

func (*Ps) Snapshot

func (ps *Ps) Snapshot() ([]PID, error)

Snapshot returns a snapshot of the system process table.

Example
package main

import (
	"fmt"

	"github.com/msantos/goreap/process"
)

func main() {
	ps := process.New()
	pids, err := ps.Snapshot()
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("%+v\n", pids)
}
Output:

type SnapshotStrategy

type SnapshotStrategy string
const (
	SnapshotAny      SnapshotStrategy = ""
	SnapshotPs       SnapshotStrategy = "ps"
	SnapshotChildren SnapshotStrategy = "children"
)

Jump to

Keyboard shortcuts

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