2017-02-01 16:36:24 +00:00
|
|
|
// +build openbsd
|
|
|
|
|
|
|
|
package process
|
|
|
|
|
|
|
|
import (
|
|
|
|
"C"
|
2017-02-04 06:31:11 +00:00
|
|
|
"bytes"
|
2018-10-19 18:33:23 +00:00
|
|
|
"context"
|
2017-02-01 16:36:24 +00:00
|
|
|
"encoding/binary"
|
2020-07-01 12:47:56 +00:00
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2017-02-01 16:36:24 +00:00
|
|
|
"strings"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
cpu "github.com/shirou/gopsutil/cpu"
|
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
|
|
|
mem "github.com/shirou/gopsutil/mem"
|
2017-02-04 06:31:11 +00:00
|
|
|
net "github.com/shirou/gopsutil/net"
|
2018-10-19 18:33:23 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2017-02-01 16:36:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// MemoryInfoExStat is different between OSes
|
|
|
|
type MemoryInfoExStat struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
type MemoryMapsStat struct {
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
func pidsWithContext(ctx context.Context) ([]int32, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
var ret []int32
|
2018-10-19 18:33:23 +00:00
|
|
|
procs, err := Processes()
|
2017-02-01 16:36:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range procs {
|
|
|
|
ret = append(ret, p.Pid)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) Ppid() (int32, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.PpidWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) PpidWithContext(ctx context.Context) (int32, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
k, err := p.getKProc()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return k.Ppid, nil
|
|
|
|
}
|
|
|
|
func (p *Process) Name() (string, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.NameWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) NameWithContext(ctx context.Context) (string, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
k, err := p.getKProc()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-07-01 12:47:56 +00:00
|
|
|
name := common.IntToString(k.Comm[:])
|
|
|
|
|
|
|
|
if len(name) >= 15 {
|
|
|
|
cmdlineSlice, err := p.CmdlineSliceWithContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(cmdlineSlice) > 0 {
|
|
|
|
extendedName := filepath.Base(cmdlineSlice[0])
|
|
|
|
if strings.HasPrefix(extendedName, p.name) {
|
|
|
|
name = extendedName
|
|
|
|
} else {
|
|
|
|
name = cmdlineSlice[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-01 16:36:24 +00:00
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
return name, nil
|
2017-02-01 16:36:24 +00:00
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
func (p *Process) Tgid() (int32, error) {
|
|
|
|
return 0, common.ErrNotImplementedError
|
|
|
|
}
|
2017-02-01 16:36:24 +00:00
|
|
|
func (p *Process) Exe() (string, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.ExeWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) ExeWithContext(ctx context.Context) (string, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return "", common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) CmdlineSlice() ([]string, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.CmdlineSliceWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error) {
|
2017-02-04 06:31:11 +00:00
|
|
|
mib := []int32{CTLKern, KernProcArgs, p.Pid, KernProcArgv}
|
2017-02-01 16:36:24 +00:00
|
|
|
buf, _, err := common.CallSyscall(mib)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
argc := 0
|
|
|
|
argvp := unsafe.Pointer(&buf[0])
|
|
|
|
argv := *(**C.char)(unsafe.Pointer(argvp))
|
|
|
|
size := unsafe.Sizeof(argv)
|
|
|
|
var strParts []string
|
|
|
|
|
|
|
|
for argv != nil {
|
|
|
|
strParts = append(strParts, C.GoString(argv))
|
|
|
|
|
|
|
|
argc++
|
2017-02-04 06:31:11 +00:00
|
|
|
argv = *(**C.char)(unsafe.Pointer(uintptr(argvp) + uintptr(argc)*size))
|
2017-02-01 16:36:24 +00:00
|
|
|
}
|
|
|
|
return strParts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) Cmdline() (string, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.CmdlineWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) CmdlineWithContext(ctx context.Context) (string, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
argv, err := p.CmdlineSlice()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return strings.Join(argv, " "), nil
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return 0, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
func (p *Process) Cwd() (string, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.CwdWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return "", common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
func (p *Process) Parent() (*Process, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.ParentWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return p, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
func (p *Process) Status() (string, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.StatusWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) StatusWithContext(ctx context.Context) (string, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
k, err := p.getKProc()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
var s string
|
|
|
|
switch k.Stat {
|
|
|
|
case SIDL:
|
|
|
|
case SRUN:
|
|
|
|
case SONPROC:
|
|
|
|
s = "R"
|
|
|
|
case SSLEEP:
|
|
|
|
s = "S"
|
|
|
|
case SSTOP:
|
|
|
|
s = "T"
|
|
|
|
case SDEAD:
|
|
|
|
s = "Z"
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
2020-07-01 12:47:56 +00:00
|
|
|
func (p *Process) Foreground() (bool, error) {
|
|
|
|
return p.ForegroundWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) {
|
|
|
|
// see https://github.com/shirou/gopsutil/issues/596#issuecomment-432707831 for implementation details
|
|
|
|
pid := p.Pid
|
|
|
|
ps, err := exec.LookPath("ps")
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
out, err := invoke.CommandWithContext(ctx, ps, "-o", "stat=", "-p", strconv.Itoa(int(pid)))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return strings.IndexByte(string(out), '+') != -1, nil
|
|
|
|
}
|
2017-02-01 16:36:24 +00:00
|
|
|
func (p *Process) Uids() ([]int32, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.UidsWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
k, err := p.getKProc()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
uids := make([]int32, 0, 3)
|
|
|
|
|
|
|
|
uids = append(uids, int32(k.Ruid), int32(k.Uid), int32(k.Svuid))
|
|
|
|
|
|
|
|
return uids, nil
|
|
|
|
}
|
|
|
|
func (p *Process) Gids() ([]int32, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.GidsWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
k, err := p.getKProc()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
gids := make([]int32, 0, 3)
|
|
|
|
gids = append(gids, int32(k.Rgid), int32(k.Ngroups), int32(k.Svgid))
|
|
|
|
|
|
|
|
return gids, nil
|
|
|
|
}
|
|
|
|
func (p *Process) Terminal() (string, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.TerminalWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) TerminalWithContext(ctx context.Context) (string, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
k, err := p.getKProc()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
ttyNr := uint64(k.Tdev)
|
|
|
|
|
|
|
|
termmap, err := getTerminalMap()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return termmap[ttyNr], nil
|
|
|
|
}
|
|
|
|
func (p *Process) Nice() (int32, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.NiceWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) NiceWithContext(ctx context.Context) (int32, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
k, err := p.getKProc()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return int32(k.Nice), nil
|
|
|
|
}
|
|
|
|
func (p *Process) IOnice() (int32, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.IOniceWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) IOniceWithContext(ctx context.Context) (int32, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return 0, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
func (p *Process) Rlimit() ([]RlimitStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.RlimitWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) RlimitWithContext(ctx context.Context) ([]RlimitStat, error) {
|
|
|
|
var rlimit []RlimitStat
|
|
|
|
return rlimit, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
func (p *Process) RlimitUsage(gatherUsed bool) ([]RlimitStat, error) {
|
|
|
|
return p.RlimitUsageWithContext(context.Background(), gatherUsed)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) RlimitUsageWithContext(ctx context.Context, gatherUsed bool) ([]RlimitStat, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
var rlimit []RlimitStat
|
|
|
|
return rlimit, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
func (p *Process) IOCounters() (*IOCountersStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.IOCountersWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) IOCountersWithContext(ctx context.Context) (*IOCountersStat, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
k, err := p.getKProc()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &IOCountersStat{
|
|
|
|
ReadCount: uint64(k.Uru_inblock),
|
|
|
|
WriteCount: uint64(k.Uru_oublock),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
func (p *Process) NumCtxSwitches() (*NumCtxSwitchesStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.NumCtxSwitchesWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) NumCtxSwitchesWithContext(ctx context.Context) (*NumCtxSwitchesStat, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return nil, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
func (p *Process) NumFDs() (int32, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.NumFDsWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) NumFDsWithContext(ctx context.Context) (int32, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return 0, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
func (p *Process) NumThreads() (int32, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.NumThreadsWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
/* not supported, just return 1 */
|
|
|
|
return 1, nil
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
func (p *Process) Threads() (map[int32]*cpu.TimesStat, error) {
|
|
|
|
return p.ThreadsWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) ThreadsWithContext(ctx context.Context) (map[int32]*cpu.TimesStat, error) {
|
|
|
|
ret := make(map[int32]*cpu.TimesStat)
|
2017-02-01 16:36:24 +00:00
|
|
|
return ret, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
func (p *Process) Times() (*cpu.TimesStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.TimesWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
k, err := p.getKProc()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &cpu.TimesStat{
|
|
|
|
CPU: "cpu",
|
|
|
|
User: float64(k.Uutime_sec) + float64(k.Uutime_usec)/1000000,
|
|
|
|
System: float64(k.Ustime_sec) + float64(k.Ustime_usec)/1000000,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
func (p *Process) CPUAffinity() ([]int32, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.CPUAffinityWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) CPUAffinityWithContext(ctx context.Context) ([]int32, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return nil, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
func (p *Process) MemoryInfo() (*MemoryInfoStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.MemoryInfoWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
k, err := p.getKProc()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pageSize, err := mem.GetPageSize()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &MemoryInfoStat{
|
|
|
|
RSS: uint64(k.Vm_rssize) * pageSize,
|
|
|
|
VMS: uint64(k.Vm_tsize) + uint64(k.Vm_dsize) +
|
|
|
|
uint64(k.Vm_ssize),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.MemoryInfoExWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) MemoryInfoExWithContext(ctx context.Context) (*MemoryInfoExStat, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return nil, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
func (p *Process) PageFaults() (*PageFaultsStat, error) {
|
|
|
|
return p.PageFaultsWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) PageFaultsWithContext(ctx context.Context) (*PageFaultsStat, error) {
|
|
|
|
return nil, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
2017-02-01 16:36:24 +00:00
|
|
|
func (p *Process) Children() ([]*Process, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.ChildrenWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) {
|
|
|
|
pids, err := common.CallPgrepWithContext(ctx, invoke, p.Pid)
|
2017-02-01 16:36:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret := make([]*Process, 0, len(pids))
|
|
|
|
for _, pid := range pids {
|
|
|
|
np, err := NewProcess(pid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret = append(ret, np)
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) OpenFiles() ([]OpenFilesStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.OpenFilesWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return nil, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) Connections() ([]net.ConnectionStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.ConnectionsWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) ConnectionsWithContext(ctx context.Context) ([]net.ConnectionStat, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return nil, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
func (p *Process) ConnectionsMax(max int) ([]net.ConnectionStat, error) {
|
|
|
|
return p.ConnectionsMaxWithContext(context.Background(), max)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) ConnectionsMaxWithContext(ctx context.Context, max int) ([]net.ConnectionStat, error) {
|
|
|
|
return []net.ConnectionStat{}, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
2017-02-01 16:36:24 +00:00
|
|
|
func (p *Process) NetIOCounters(pernic bool) ([]net.IOCountersStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.NetIOCountersWithContext(context.Background(), pernic)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) NetIOCountersWithContext(ctx context.Context, pernic bool) ([]net.IOCountersStat, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return nil, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.MemoryMapsWithContext(context.Background(), grouped)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) MemoryMapsWithContext(ctx context.Context, grouped bool) (*[]MemoryMapsStat, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
var ret []MemoryMapsStat
|
|
|
|
return &ret, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
func Processes() ([]*Process, error) {
|
|
|
|
return ProcessesWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProcessesWithContext(ctx context.Context) ([]*Process, error) {
|
|
|
|
results := []*Process{}
|
2017-02-01 16:36:24 +00:00
|
|
|
|
|
|
|
buf, length, err := CallKernProcSyscall(KernProcAll, 0)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return results, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// get kinfo_proc size
|
|
|
|
count := int(length / uint64(sizeOfKinfoProc))
|
|
|
|
|
|
|
|
// parse buf to procs
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
b := buf[i*sizeOfKinfoProc : (i+1)*sizeOfKinfoProc]
|
|
|
|
k, err := parseKinfoProc(b)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
p, err := NewProcess(int32(k.Pid))
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
results = append(results, p)
|
2017-02-01 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseKinfoProc(buf []byte) (KinfoProc, error) {
|
|
|
|
var k KinfoProc
|
|
|
|
br := bytes.NewReader(buf)
|
|
|
|
err := common.Read(br, binary.LittleEndian, &k)
|
|
|
|
return k, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) getKProc() (*KinfoProc, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.getKProcWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) getKProcWithContext(ctx context.Context) (*KinfoProc, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
buf, length, err := CallKernProcSyscall(KernProcPID, p.Pid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if length != sizeOfKinfoProc {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
k, err := parseKinfoProc(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &k, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CallKernProcSyscall(op int32, arg int32) ([]byte, uint64, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return CallKernProcSyscallWithContext(context.Background(), op, arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func CallKernProcSyscallWithContext(ctx context.Context, op int32, arg int32) ([]byte, uint64, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
mib := []int32{CTLKern, KernProc, op, arg, sizeOfKinfoProc, 0}
|
|
|
|
mibptr := unsafe.Pointer(&mib[0])
|
|
|
|
miblen := uint64(len(mib))
|
|
|
|
length := uint64(0)
|
2018-10-19 18:33:23 +00:00
|
|
|
_, _, err := unix.Syscall6(
|
|
|
|
unix.SYS___SYSCTL,
|
2017-02-01 16:36:24 +00:00
|
|
|
uintptr(mibptr),
|
|
|
|
uintptr(miblen),
|
|
|
|
0,
|
|
|
|
uintptr(unsafe.Pointer(&length)),
|
|
|
|
0,
|
|
|
|
0)
|
|
|
|
if err != 0 {
|
|
|
|
return nil, length, err
|
|
|
|
}
|
|
|
|
|
|
|
|
count := int32(length / uint64(sizeOfKinfoProc))
|
|
|
|
mib = []int32{CTLKern, KernProc, op, arg, sizeOfKinfoProc, count}
|
|
|
|
mibptr = unsafe.Pointer(&mib[0])
|
|
|
|
miblen = uint64(len(mib))
|
|
|
|
// get proc info itself
|
|
|
|
buf := make([]byte, length)
|
2018-10-19 18:33:23 +00:00
|
|
|
_, _, err = unix.Syscall6(
|
|
|
|
unix.SYS___SYSCTL,
|
2017-02-01 16:36:24 +00:00
|
|
|
uintptr(mibptr),
|
|
|
|
uintptr(miblen),
|
|
|
|
uintptr(unsafe.Pointer(&buf[0])),
|
|
|
|
uintptr(unsafe.Pointer(&length)),
|
|
|
|
0,
|
|
|
|
0)
|
|
|
|
if err != 0 {
|
|
|
|
return buf, length, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, length, nil
|
|
|
|
}
|