2017-02-01 16:36:24 +00:00
|
|
|
// +build freebsd
|
|
|
|
|
|
|
|
package process
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-10-19 18:33:23 +00:00
|
|
|
"context"
|
2020-07-01 12:47:56 +00:00
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2017-02-01 16:36:24 +00:00
|
|
|
"strings"
|
|
|
|
|
2020-11-07 01:48:38 +00:00
|
|
|
cpu "github.com/shirou/gopsutil/v3/cpu"
|
|
|
|
"github.com/shirou/gopsutil/v3/internal/common"
|
|
|
|
net "github.com/shirou/gopsutil/v3/net"
|
2018-10-19 18:33:23 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2017-02-01 16:36:24 +00:00
|
|
|
)
|
|
|
|
|
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
|
2020-11-07 01:48:38 +00:00
|
|
|
procs, err := ProcessesWithContext(ctx)
|
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
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
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
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
|
|
|
|
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) ExeWithContext(ctx context.Context) (string, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return "", common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
func (p *Process) CmdlineWithContext(ctx context.Context) (string, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
mib := []int32{CTLKern, KernProc, KernProcArgs, p.Pid}
|
|
|
|
buf, _, err := common.CallSyscall(mib)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
ret := strings.FieldsFunc(string(buf), func(r rune) bool {
|
|
|
|
if r == '\u0000' {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
return strings.Join(ret, " "), nil
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
mib := []int32{CTLKern, KernProc, KernProcArgs, p.Pid}
|
|
|
|
buf, _, err := common.CallSyscall(mib)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(buf) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if buf[len(buf)-1] == 0 {
|
|
|
|
buf = buf[:len(buf)-1]
|
|
|
|
}
|
|
|
|
parts := bytes.Split(buf, []byte{0})
|
|
|
|
var strParts []string
|
|
|
|
for _, p := range parts {
|
|
|
|
strParts = append(strParts, string(p))
|
|
|
|
}
|
|
|
|
|
|
|
|
return strParts, nil
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
|
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
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
|
|
|
|
func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
|
2020-11-07 01:48:38 +00:00
|
|
|
return nil, common.ErrNotImplementedError
|
2018-10-19 18:33:23 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 01:48:38 +00:00
|
|
|
func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
k, err := p.getKProc()
|
|
|
|
if err != nil {
|
2020-11-07 01:48:38 +00:00
|
|
|
return []string{""}, err
|
2017-02-01 16:36:24 +00:00
|
|
|
}
|
|
|
|
var s string
|
|
|
|
switch k.Stat {
|
|
|
|
case SIDL:
|
2020-11-07 01:48:38 +00:00
|
|
|
s = Idle
|
2017-02-01 16:36:24 +00:00
|
|
|
case SRUN:
|
2020-11-07 01:48:38 +00:00
|
|
|
s = Running
|
2017-02-01 16:36:24 +00:00
|
|
|
case SSLEEP:
|
2020-11-07 01:48:38 +00:00
|
|
|
s = Sleep
|
2017-02-01 16:36:24 +00:00
|
|
|
case SSTOP:
|
2020-11-07 01:48:38 +00:00
|
|
|
s = Stop
|
2017-02-01 16:36:24 +00:00
|
|
|
case SZOMB:
|
2020-11-07 01:48:38 +00:00
|
|
|
s = Zombie
|
2017-02-01 16:36:24 +00:00
|
|
|
case SWAIT:
|
2020-11-07 01:48:38 +00:00
|
|
|
s = Wait
|
2017-02-01 16:36:24 +00:00
|
|
|
case SLOCK:
|
2020-11-07 01:48:38 +00:00
|
|
|
s = Lock
|
2017-02-01 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 01:48:38 +00:00
|
|
|
return []string{s}, nil
|
2020-07-01 12:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
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
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2020-10-07 16:57:18 +00:00
|
|
|
|
|
|
|
func (p *Process) GroupsWithContext(ctx context.Context) ([]int32, error) {
|
|
|
|
k, err := p.getKProc()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
groups := make([]int32, k.Ngroups)
|
|
|
|
for i := int16(0); i < k.Ngroups; i++ {
|
|
|
|
groups[i] = int32(k.Groups[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return groups, nil
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
|
|
|
|
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.Rusage.Inblock),
|
|
|
|
WriteCount: uint64(k.Rusage.Oublock),
|
|
|
|
}, nil
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
|
|
|
|
func (p *Process) NumThreadsWithContext(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.Numthreads, nil
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
|
|
|
|
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.Rusage.Utime.Sec) + float64(k.Rusage.Utime.Usec)/1000000,
|
|
|
|
System: float64(k.Rusage.Stime.Sec) + float64(k.Rusage.Stime.Usec)/1000000,
|
|
|
|
}, nil
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
v, err := unix.Sysctl("vm.stats.vm.v_page_size")
|
2017-02-01 16:36:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pageSize := common.LittleEndian.Uint16([]byte(v))
|
|
|
|
|
|
|
|
return &MemoryInfoStat{
|
|
|
|
RSS: uint64(k.Rssize) * uint64(pageSize),
|
|
|
|
VMS: uint64(k.Size),
|
|
|
|
}, nil
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
|
|
|
|
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 {
|
2020-11-07 01:48:38 +00:00
|
|
|
np, err := NewProcessWithContext(ctx, pid)
|
2017-02-01 16:36:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret = append(ret, np)
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
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) ConnectionsMaxWithContext(ctx context.Context, max int) ([]net.ConnectionStat, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return nil, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
func ProcessesWithContext(ctx context.Context) ([]*Process, error) {
|
|
|
|
results := []*Process{}
|
2017-02-01 16:36:24 +00:00
|
|
|
|
|
|
|
mib := []int32{CTLKern, KernProc, KernProcProc, 0}
|
|
|
|
buf, length, err := common.CallSyscall(mib)
|
|
|
|
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
|
|
|
|
}
|
2020-11-07 01:48:38 +00:00
|
|
|
p, err := NewProcessWithContext(ctx, int32(k.Pid))
|
2017-02-01 16:36:24 +00:00
|
|
|
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 (p *Process) getKProc() (*KinfoProc, error) {
|
|
|
|
mib := []int32{CTLKern, KernProc, KernProcPID, p.Pid}
|
|
|
|
|
|
|
|
buf, length, err := common.CallSyscall(mib)
|
|
|
|
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
|
|
|
|
}
|