Update vendor

This commit is contained in:
vcaesar 2018-01-11 20:15:38 +08:00
parent 4011915749
commit 50ce138395
9 changed files with 995 additions and 10 deletions

View File

@ -1,6 +1,7 @@
package process package process
import ( import (
"context"
"encoding/json" "encoding/json"
"runtime" "runtime"
"time" "time"
@ -30,6 +31,8 @@ type Process struct {
lastCPUTimes *cpu.TimesStat lastCPUTimes *cpu.TimesStat
lastCPUTime time.Time lastCPUTime time.Time
tgid int32
} }
type OpenFilesStat struct { type OpenFilesStat struct {
@ -125,6 +128,10 @@ func (p NumCtxSwitchesStat) String() string {
} }
func PidExists(pid int32) (bool, error) { func PidExists(pid int32) (bool, error) {
return PidExistsWithContext(context.Background(), pid)
}
func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) {
pids, err := Pids() pids, err := Pids()
if err != nil { if err != nil {
return false, err return false, err
@ -142,6 +149,10 @@ func PidExists(pid int32) (bool, error) {
// If interval is 0, return difference from last call(non-blocking). // If interval is 0, return difference from last call(non-blocking).
// If interval > 0, wait interval sec and return diffrence between start and end. // If interval > 0, wait interval sec and return diffrence between start and end.
func (p *Process) Percent(interval time.Duration) (float64, error) { func (p *Process) Percent(interval time.Duration) (float64, error) {
return p.PercentWithContext(context.Background(), interval)
}
func (p *Process) PercentWithContext(ctx context.Context, interval time.Duration) (float64, error) {
cpuTimes, err := p.Times() cpuTimes, err := p.Times()
if err != nil { if err != nil {
return 0, err return 0, err
@ -185,6 +196,10 @@ func calculatePercent(t1, t2 *cpu.TimesStat, delta float64, numcpu int) float64
// MemoryPercent returns how many percent of the total RAM this process uses // MemoryPercent returns how many percent of the total RAM this process uses
func (p *Process) MemoryPercent() (float32, error) { func (p *Process) MemoryPercent() (float32, error) {
return p.MemoryPercentWithContext(context.Background())
}
func (p *Process) MemoryPercentWithContext(ctx context.Context) (float32, error) {
machineMemory, err := mem.VirtualMemory() machineMemory, err := mem.VirtualMemory()
if err != nil { if err != nil {
return 0, err return 0, err
@ -202,6 +217,10 @@ func (p *Process) MemoryPercent() (float32, error) {
// CPU_Percent returns how many percent of the CPU time this process uses // CPU_Percent returns how many percent of the CPU time this process uses
func (p *Process) CPUPercent() (float64, error) { func (p *Process) CPUPercent() (float64, error) {
return p.CPUPercentWithContext(context.Background())
}
func (p *Process) CPUPercentWithContext(ctx context.Context) (float64, error) {
crt_time, err := p.CreateTime() crt_time, err := p.CreateTime()
if err != nil { if err != nil {
return 0, err return 0, err

View File

@ -4,6 +4,7 @@ package process
import ( import (
"bytes" "bytes"
"context"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"os/exec" "os/exec"
@ -44,6 +45,10 @@ type MemoryMapsStat struct {
} }
func Pids() ([]int32, error) { func Pids() ([]int32, error) {
return PidsWithContext(context.Background())
}
func PidsWithContext(ctx context.Context) ([]int32, error) {
var ret []int32 var ret []int32
pids, err := callPs("pid", 0, false) pids, err := callPs("pid", 0, false)
@ -63,6 +68,10 @@ func Pids() ([]int32, error) {
} }
func (p *Process) Ppid() (int32, error) { func (p *Process) Ppid() (int32, error) {
return p.PpidWithContext(context.Background())
}
func (p *Process) PpidWithContext(ctx context.Context) (int32, error) {
r, err := callPs("ppid", p.Pid, false) r, err := callPs("ppid", p.Pid, false)
if err != nil { if err != nil {
return 0, err return 0, err
@ -76,6 +85,10 @@ func (p *Process) Ppid() (int32, error) {
return int32(v), err return int32(v), err
} }
func (p *Process) Name() (string, error) { func (p *Process) Name() (string, error) {
return p.NameWithContext(context.Background())
}
func (p *Process) NameWithContext(ctx context.Context) (string, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return "", err return "", err
@ -83,7 +96,14 @@ func (p *Process) Name() (string, error) {
return common.IntToString(k.Proc.P_comm[:]), nil return common.IntToString(k.Proc.P_comm[:]), nil
} }
func (p *Process) Tgid() (int32, error) {
return 0, common.ErrNotImplementedError
}
func (p *Process) Exe() (string, error) { func (p *Process) Exe() (string, error) {
return p.ExeWithContext(context.Background())
}
func (p *Process) ExeWithContext(ctx context.Context) (string, error) {
lsof_bin, err := exec.LookPath("lsof") lsof_bin, err := exec.LookPath("lsof")
if err != nil { if err != nil {
return "", err return "", err
@ -117,6 +137,10 @@ func (p *Process) Exe() (string, error) {
// Cmdline returns the command line arguments of the process as a string with // Cmdline returns the command line arguments of the process as a string with
// each argument separated by 0x20 ascii character. // each argument separated by 0x20 ascii character.
func (p *Process) Cmdline() (string, error) { func (p *Process) Cmdline() (string, error) {
return p.CmdlineWithContext(context.Background())
}
func (p *Process) CmdlineWithContext(ctx context.Context) (string, error) {
r, err := callPs("command", p.Pid, false) r, err := callPs("command", p.Pid, false)
if err != nil { if err != nil {
return "", err return "", err
@ -130,6 +154,10 @@ func (p *Process) Cmdline() (string, error) {
// reported as two separate items. In order to do something better CGO would be needed // reported as two separate items. In order to do something better CGO would be needed
// to use the native darwin functions. // to use the native darwin functions.
func (p *Process) CmdlineSlice() ([]string, error) { func (p *Process) CmdlineSlice() ([]string, error) {
return p.CmdlineSliceWithContext(context.Background())
}
func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error) {
r, err := callPs("command", p.Pid, false) r, err := callPs("command", p.Pid, false)
if err != nil { if err != nil {
return nil, err return nil, err
@ -137,6 +165,10 @@ func (p *Process) CmdlineSlice() ([]string, error) {
return r[0], err return r[0], err
} }
func (p *Process) CreateTime() (int64, error) { func (p *Process) CreateTime() (int64, error) {
return p.CreateTimeWithContext(context.Background())
}
func (p *Process) CreateTimeWithContext(ctx context.Context) (int64, error) {
r, err := callPs("etime", p.Pid, false) r, err := callPs("etime", p.Pid, false)
if err != nil { if err != nil {
return 0, err return 0, err
@ -167,9 +199,17 @@ func (p *Process) CreateTime() (int64, error) {
return start.Unix() * 1000, nil return start.Unix() * 1000, nil
} }
func (p *Process) Cwd() (string, error) { func (p *Process) Cwd() (string, error) {
return p.CwdWithContext(context.Background())
}
func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError return "", common.ErrNotImplementedError
} }
func (p *Process) Parent() (*Process, error) { func (p *Process) Parent() (*Process, error) {
return p.ParentWithContext(context.Background())
}
func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
rr, err := common.CallLsof(invoke, p.Pid, "-FR") rr, err := common.CallLsof(invoke, p.Pid, "-FR")
if err != nil { if err != nil {
return nil, err return nil, err
@ -188,6 +228,10 @@ func (p *Process) Parent() (*Process, error) {
return nil, fmt.Errorf("could not find parent line") return nil, fmt.Errorf("could not find parent line")
} }
func (p *Process) Status() (string, error) { func (p *Process) Status() (string, error) {
return p.StatusWithContext(context.Background())
}
func (p *Process) StatusWithContext(ctx context.Context) (string, error) {
r, err := callPs("state", p.Pid, false) r, err := callPs("state", p.Pid, false)
if err != nil { if err != nil {
return "", err return "", err
@ -196,6 +240,10 @@ func (p *Process) Status() (string, error) {
return r[0][0], err return r[0][0], err
} }
func (p *Process) Uids() ([]int32, error) { func (p *Process) Uids() ([]int32, error) {
return p.UidsWithContext(context.Background())
}
func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return nil, err return nil, err
@ -207,6 +255,10 @@ func (p *Process) Uids() ([]int32, error) {
return []int32{userEffectiveUID}, nil return []int32{userEffectiveUID}, nil
} }
func (p *Process) Gids() ([]int32, error) { func (p *Process) Gids() ([]int32, error) {
return p.GidsWithContext(context.Background())
}
func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return nil, err return nil, err
@ -218,6 +270,10 @@ func (p *Process) Gids() ([]int32, error) {
return gids, nil return gids, nil
} }
func (p *Process) Terminal() (string, error) { func (p *Process) Terminal() (string, error) {
return p.TerminalWithContext(context.Background())
}
func (p *Process) TerminalWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError return "", common.ErrNotImplementedError
/* /*
k, err := p.getKProc() k, err := p.getKProc()
@ -235,6 +291,10 @@ func (p *Process) Terminal() (string, error) {
*/ */
} }
func (p *Process) Nice() (int32, error) { func (p *Process) Nice() (int32, error) {
return p.NiceWithContext(context.Background())
}
func (p *Process) NiceWithContext(ctx context.Context) (int32, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return 0, err return 0, err
@ -242,26 +302,54 @@ func (p *Process) Nice() (int32, error) {
return int32(k.Proc.P_nice), nil return int32(k.Proc.P_nice), nil
} }
func (p *Process) IOnice() (int32, error) { func (p *Process) IOnice() (int32, error) {
return p.IOniceWithContext(context.Background())
}
func (p *Process) IOniceWithContext(ctx context.Context) (int32, error) {
return 0, common.ErrNotImplementedError return 0, common.ErrNotImplementedError
} }
func (p *Process) Rlimit() ([]RlimitStat, error) { func (p *Process) Rlimit() ([]RlimitStat, error) {
return p.RlimitWithContext(context.Background())
}
func (p *Process) RlimitWithContext(ctx context.Context) ([]RlimitStat, error) {
var rlimit []RlimitStat var rlimit []RlimitStat
return rlimit, common.ErrNotImplementedError return rlimit, common.ErrNotImplementedError
} }
func (p *Process) RlimitUsage(_ bool) ([]RlimitStat, error) { 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) {
var rlimit []RlimitStat var rlimit []RlimitStat
return rlimit, common.ErrNotImplementedError return rlimit, common.ErrNotImplementedError
} }
func (p *Process) IOCounters() (*IOCountersStat, error) { func (p *Process) IOCounters() (*IOCountersStat, error) {
return p.IOCountersWithContext(context.Background())
}
func (p *Process) IOCountersWithContext(ctx context.Context) (*IOCountersStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) NumCtxSwitches() (*NumCtxSwitchesStat, error) { func (p *Process) NumCtxSwitches() (*NumCtxSwitchesStat, error) {
return p.NumCtxSwitchesWithContext(context.Background())
}
func (p *Process) NumCtxSwitchesWithContext(ctx context.Context) (*NumCtxSwitchesStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) NumFDs() (int32, error) { func (p *Process) NumFDs() (int32, error) {
return p.NumFDsWithContext(context.Background())
}
func (p *Process) NumFDsWithContext(ctx context.Context) (int32, error) {
return 0, common.ErrNotImplementedError return 0, common.ErrNotImplementedError
} }
func (p *Process) NumThreads() (int32, error) { func (p *Process) NumThreads() (int32, error) {
return p.NumThreadsWithContext(context.Background())
}
func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) {
r, err := callPs("utime,stime", p.Pid, true) r, err := callPs("utime,stime", p.Pid, true)
if err != nil { if err != nil {
return 0, err return 0, err
@ -269,6 +357,10 @@ func (p *Process) NumThreads() (int32, error) {
return int32(len(r)), nil return int32(len(r)), nil
} }
func (p *Process) Threads() (map[int32]*cpu.TimesStat, error) { 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) ret := make(map[int32]*cpu.TimesStat)
return ret, common.ErrNotImplementedError return ret, common.ErrNotImplementedError
} }
@ -299,6 +391,10 @@ func convertCPUTimes(s string) (ret float64, err error) {
return float64(t) / ClockTicks, nil return float64(t) / ClockTicks, nil
} }
func (p *Process) Times() (*cpu.TimesStat, error) { func (p *Process) Times() (*cpu.TimesStat, error) {
return p.TimesWithContext(context.Background())
}
func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) {
r, err := callPs("utime,stime", p.Pid, false) r, err := callPs("utime,stime", p.Pid, false)
if err != nil { if err != nil {
@ -322,9 +418,17 @@ func (p *Process) Times() (*cpu.TimesStat, error) {
return ret, nil return ret, nil
} }
func (p *Process) CPUAffinity() ([]int32, error) { func (p *Process) CPUAffinity() ([]int32, error) {
return p.CPUAffinityWithContext(context.Background())
}
func (p *Process) CPUAffinityWithContext(ctx context.Context) ([]int32, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) MemoryInfo() (*MemoryInfoStat, error) { func (p *Process) MemoryInfo() (*MemoryInfoStat, error) {
return p.MemoryInfoWithContext(context.Background())
}
func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, error) {
r, err := callPs("rss,vsize,pagein", p.Pid, false) r, err := callPs("rss,vsize,pagein", p.Pid, false)
if err != nil { if err != nil {
return nil, err return nil, err
@ -351,10 +455,18 @@ func (p *Process) MemoryInfo() (*MemoryInfoStat, error) {
return ret, nil return ret, nil
} }
func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) { func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) {
return p.MemoryInfoExWithContext(context.Background())
}
func (p *Process) MemoryInfoExWithContext(ctx context.Context) (*MemoryInfoExStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) Children() ([]*Process, error) { func (p *Process) Children() ([]*Process, error) {
return p.ChildrenWithContext(context.Background())
}
func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) {
pids, err := common.CallPgrep(invoke, p.Pid) pids, err := common.CallPgrep(invoke, p.Pid)
if err != nil { if err != nil {
return nil, err return nil, err
@ -371,26 +483,50 @@ func (p *Process) Children() ([]*Process, error) {
} }
func (p *Process) OpenFiles() ([]OpenFilesStat, error) { func (p *Process) OpenFiles() ([]OpenFilesStat, error) {
return p.OpenFilesWithContext(context.Background())
}
func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) Connections() ([]net.ConnectionStat, error) { func (p *Process) Connections() ([]net.ConnectionStat, error) {
return p.ConnectionsWithContext(context.Background())
}
func (p *Process) ConnectionsWithContext(ctx context.Context) ([]net.ConnectionStat, error) {
return net.ConnectionsPid("all", p.Pid) return net.ConnectionsPid("all", p.Pid)
} }
func (p *Process) NetIOCounters(pernic bool) ([]net.IOCountersStat, error) { func (p *Process) NetIOCounters(pernic bool) ([]net.IOCountersStat, error) {
return p.NetIOCountersWithContext(context.Background(), pernic)
}
func (p *Process) NetIOCountersWithContext(ctx context.Context, pernic bool) ([]net.IOCountersStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) IsRunning() (bool, error) { func (p *Process) IsRunning() (bool, error) {
return p.IsRunningWithContext(context.Background())
}
func (p *Process) IsRunningWithContext(ctx context.Context) (bool, error) {
return true, common.ErrNotImplementedError return true, common.ErrNotImplementedError
} }
func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) { func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) {
return p.MemoryMapsWithContext(context.Background(), grouped)
}
func (p *Process) MemoryMapsWithContext(ctx context.Context, grouped bool) (*[]MemoryMapsStat, error) {
var ret []MemoryMapsStat var ret []MemoryMapsStat
return &ret, common.ErrNotImplementedError return &ret, common.ErrNotImplementedError
} }
func Processes() ([]*Process, error) { func Processes() ([]*Process, error) {
return ProcessesWithContext(context.Background())
}
func ProcessesWithContext(ctx context.Context) ([]*Process, error) {
results := []*Process{} results := []*Process{}
mib := []int32{CTLKern, KernProc, KernProcAll, 0} mib := []int32{CTLKern, KernProc, KernProcAll, 0}
@ -436,6 +572,10 @@ func parseKinfoProc(buf []byte) (KinfoProc, error) {
// Returns a proc as defined here: // Returns a proc as defined here:
// http://unix.superglobalmegacorp.com/Net2/newsrc/sys/kinfo_proc.h.html // http://unix.superglobalmegacorp.com/Net2/newsrc/sys/kinfo_proc.h.html
func (p *Process) getKProc() (*KinfoProc, error) { func (p *Process) getKProc() (*KinfoProc, error) {
return p.getKProcWithContext(context.Background())
}
func (p *Process) getKProcWithContext(ctx context.Context) (*KinfoProc, error) {
mib := []int32{CTLKern, KernProc, KernProcPID, p.Pid} mib := []int32{CTLKern, KernProc, KernProcPID, p.Pid}
procK := KinfoProc{} procK := KinfoProc{}
length := uint64(unsafe.Sizeof(procK)) length := uint64(unsafe.Sizeof(procK))

View File

@ -3,6 +3,7 @@
package process package process
import ( import (
"context"
"syscall" "syscall"
"github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/cpu"
@ -28,6 +29,10 @@ type MemoryInfoExStat struct {
} }
func Pids() ([]int32, error) { func Pids() ([]int32, error) {
return PidsWithContext(context.Background())
}
func PidsWithContext(ctx context.Context) ([]int32, error) {
return []int32{}, common.ErrNotImplementedError return []int32{}, common.ErrNotImplementedError
} }
@ -36,113 +41,264 @@ func NewProcess(pid int32) (*Process, error) {
} }
func (p *Process) Ppid() (int32, error) { func (p *Process) Ppid() (int32, error) {
return p.PpidWithContext(context.Background())
}
func (p *Process) PpidWithContext(ctx context.Context) (int32, error) {
return 0, common.ErrNotImplementedError return 0, common.ErrNotImplementedError
} }
func (p *Process) Name() (string, error) { func (p *Process) Name() (string, error) {
return p.NameWithContext(context.Background())
}
func (p *Process) NameWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError return "", common.ErrNotImplementedError
} }
func (p *Process) Tgid() (int32, error) {
return 0, common.ErrNotImplementedError
}
func (p *Process) Exe() (string, error) { func (p *Process) Exe() (string, error) {
return p.ExeWithContext(context.Background())
}
func (p *Process) ExeWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError return "", common.ErrNotImplementedError
} }
func (p *Process) Cmdline() (string, error) { func (p *Process) Cmdline() (string, error) {
return p.CmdlineWithContext(context.Background())
}
func (p *Process) CmdlineWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError return "", common.ErrNotImplementedError
} }
func (p *Process) CmdlineSlice() ([]string, error) { func (p *Process) CmdlineSlice() ([]string, error) {
return p.CmdlineSliceWithContext(context.Background())
}
func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error) {
return []string{}, common.ErrNotImplementedError return []string{}, common.ErrNotImplementedError
} }
func (p *Process) CreateTime() (int64, error) { func (p *Process) CreateTime() (int64, error) {
return p.CreateTimeWithContext(context.Background())
}
func (p *Process) CreateTimeWithContext(ctx context.Context) (int64, error) {
return 0, common.ErrNotImplementedError return 0, common.ErrNotImplementedError
} }
func (p *Process) Cwd() (string, error) { func (p *Process) Cwd() (string, error) {
return p.CwdWithContext(context.Background())
}
func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError return "", common.ErrNotImplementedError
} }
func (p *Process) Parent() (*Process, error) { func (p *Process) Parent() (*Process, error) {
return p.ParentWithContext(context.Background())
}
func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) Status() (string, error) { func (p *Process) Status() (string, error) {
return p.StatusWithContext(context.Background())
}
func (p *Process) StatusWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError return "", common.ErrNotImplementedError
} }
func (p *Process) Uids() ([]int32, error) { func (p *Process) Uids() ([]int32, error) {
return p.UidsWithContext(context.Background())
}
func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) {
return []int32{}, common.ErrNotImplementedError return []int32{}, common.ErrNotImplementedError
} }
func (p *Process) Gids() ([]int32, error) { func (p *Process) Gids() ([]int32, error) {
return p.GidsWithContext(context.Background())
}
func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) {
return []int32{}, common.ErrNotImplementedError return []int32{}, common.ErrNotImplementedError
} }
func (p *Process) Terminal() (string, error) { func (p *Process) Terminal() (string, error) {
return p.TerminalWithContext(context.Background())
}
func (p *Process) TerminalWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError return "", common.ErrNotImplementedError
} }
func (p *Process) Nice() (int32, error) { func (p *Process) Nice() (int32, error) {
return p.NiceWithContext(context.Background())
}
func (p *Process) NiceWithContext(ctx context.Context) (int32, error) {
return 0, common.ErrNotImplementedError return 0, common.ErrNotImplementedError
} }
func (p *Process) IOnice() (int32, error) { func (p *Process) IOnice() (int32, error) {
return p.IOniceWithContext(context.Background())
}
func (p *Process) IOniceWithContext(ctx context.Context) (int32, error) {
return 0, common.ErrNotImplementedError return 0, common.ErrNotImplementedError
} }
func (p *Process) Rlimit() ([]RlimitStat, error) { func (p *Process) Rlimit() ([]RlimitStat, error) {
return p.RlimitWithContext(context.Background())
}
func (p *Process) RlimitWithContext(ctx context.Context) ([]RlimitStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) RlimitUsage(_ bool) ([]RlimitStat, error) { 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) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) IOCounters() (*IOCountersStat, error) { func (p *Process) IOCounters() (*IOCountersStat, error) {
return p.IOCountersWithContext(context.Background())
}
func (p *Process) IOCountersWithContext(ctx context.Context) (*IOCountersStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) NumCtxSwitches() (*NumCtxSwitchesStat, error) { func (p *Process) NumCtxSwitches() (*NumCtxSwitchesStat, error) {
return p.NumCtxSwitchesWithContext(context.Background())
}
func (p *Process) NumCtxSwitchesWithContext(ctx context.Context) (*NumCtxSwitchesStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) NumFDs() (int32, error) { func (p *Process) NumFDs() (int32, error) {
return p.NumFDsWithContext(context.Background())
}
func (p *Process) NumFDsWithContext(ctx context.Context) (int32, error) {
return 0, common.ErrNotImplementedError return 0, common.ErrNotImplementedError
} }
func (p *Process) NumThreads() (int32, error) { func (p *Process) NumThreads() (int32, error) {
return p.NumThreadsWithContext(context.Background())
}
func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) {
return 0, common.ErrNotImplementedError return 0, common.ErrNotImplementedError
} }
func (p *Process) Threads() (map[int32]*cpu.TimesStat, error) { 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) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) Times() (*cpu.TimesStat, error) { func (p *Process) Times() (*cpu.TimesStat, error) {
return p.TimesWithContext(context.Background())
}
func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) CPUAffinity() ([]int32, error) { func (p *Process) CPUAffinity() ([]int32, error) {
return p.CPUAffinityWithContext(context.Background())
}
func (p *Process) CPUAffinityWithContext(ctx context.Context) ([]int32, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) MemoryInfo() (*MemoryInfoStat, error) { func (p *Process) MemoryInfo() (*MemoryInfoStat, error) {
return p.MemoryInfoWithContext(context.Background())
}
func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) { func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) {
return p.MemoryInfoExWithContext(context.Background())
}
func (p *Process) MemoryInfoExWithContext(ctx context.Context) (*MemoryInfoExStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) Children() ([]*Process, error) { func (p *Process) Children() ([]*Process, error) {
return p.ChildrenWithContext(context.Background())
}
func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) OpenFiles() ([]OpenFilesStat, error) { func (p *Process) OpenFiles() ([]OpenFilesStat, error) {
return p.OpenFilesWithContext(context.Background())
}
func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, error) {
return []OpenFilesStat{}, common.ErrNotImplementedError return []OpenFilesStat{}, common.ErrNotImplementedError
} }
func (p *Process) Connections() ([]net.ConnectionStat, error) { func (p *Process) Connections() ([]net.ConnectionStat, error) {
return p.ConnectionsWithContext(context.Background())
}
func (p *Process) ConnectionsWithContext(ctx context.Context) ([]net.ConnectionStat, error) {
return []net.ConnectionStat{}, common.ErrNotImplementedError return []net.ConnectionStat{}, common.ErrNotImplementedError
} }
func (p *Process) NetIOCounters(pernic bool) ([]net.IOCountersStat, error) { func (p *Process) NetIOCounters(pernic bool) ([]net.IOCountersStat, error) {
return p.NetIOCountersWithContext(context.Background(), pernic)
}
func (p *Process) NetIOCountersWithContext(ctx context.Context, pernic bool) ([]net.IOCountersStat, error) {
return []net.IOCountersStat{}, common.ErrNotImplementedError return []net.IOCountersStat{}, common.ErrNotImplementedError
} }
func (p *Process) IsRunning() (bool, error) { func (p *Process) IsRunning() (bool, error) {
return p.IsRunningWithContext(context.Background())
}
func (p *Process) IsRunningWithContext(ctx context.Context) (bool, error) {
return true, common.ErrNotImplementedError return true, common.ErrNotImplementedError
} }
func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) { func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) {
return p.MemoryMapsWithContext(context.Background(), grouped)
}
func (p *Process) MemoryMapsWithContext(ctx context.Context, grouped bool) (*[]MemoryMapsStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) SendSignal(sig syscall.Signal) error { func (p *Process) SendSignal(sig syscall.Signal) error {
return p.SendSignalWithContext(context.Background(), sig)
}
func (p *Process) SendSignalWithContext(ctx context.Context, sig syscall.Signal) error {
return common.ErrNotImplementedError return common.ErrNotImplementedError
} }
func (p *Process) Suspend() error { func (p *Process) Suspend() error {
return p.SuspendWithContext(context.Background())
}
func (p *Process) SuspendWithContext(ctx context.Context) error {
return common.ErrNotImplementedError return common.ErrNotImplementedError
} }
func (p *Process) Resume() error { func (p *Process) Resume() error {
return p.ResumeWithContext(context.Background())
}
func (p *Process) ResumeWithContext(ctx context.Context) error {
return common.ErrNotImplementedError return common.ErrNotImplementedError
} }
func (p *Process) Terminate() error { func (p *Process) Terminate() error {
return p.TerminateWithContext(context.Background())
}
func (p *Process) TerminateWithContext(ctx context.Context) error {
return common.ErrNotImplementedError return common.ErrNotImplementedError
} }
func (p *Process) Kill() error { func (p *Process) Kill() error {
return p.KillWithContext(context.Background())
}
func (p *Process) KillWithContext(ctx context.Context) error {
return common.ErrNotImplementedError return common.ErrNotImplementedError
} }
func (p *Process) Username() (string, error) { func (p *Process) Username() (string, error) {
return p.UsernameWithContext(context.Background())
}
func (p *Process) UsernameWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError return "", common.ErrNotImplementedError
} }

View File

@ -4,6 +4,7 @@ package process
import ( import (
"bytes" "bytes"
"context"
"encoding/binary" "encoding/binary"
"strings" "strings"
@ -21,6 +22,10 @@ type MemoryMapsStat struct {
} }
func Pids() ([]int32, error) { func Pids() ([]int32, error) {
return PidsWithContext(context.Background())
}
func PidsWithContext(ctx context.Context) ([]int32, error) {
var ret []int32 var ret []int32
procs, err := Processes() procs, err := Processes()
if err != nil { if err != nil {
@ -35,6 +40,10 @@ func Pids() ([]int32, error) {
} }
func (p *Process) Ppid() (int32, error) { func (p *Process) Ppid() (int32, error) {
return p.PpidWithContext(context.Background())
}
func (p *Process) PpidWithContext(ctx context.Context) (int32, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return 0, err return 0, err
@ -43,6 +52,10 @@ func (p *Process) Ppid() (int32, error) {
return k.Ppid, nil return k.Ppid, nil
} }
func (p *Process) Name() (string, error) { func (p *Process) Name() (string, error) {
return p.NameWithContext(context.Background())
}
func (p *Process) NameWithContext(ctx context.Context) (string, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return "", err return "", err
@ -50,11 +63,22 @@ func (p *Process) Name() (string, error) {
return common.IntToString(k.Comm[:]), nil return common.IntToString(k.Comm[:]), nil
} }
func (p *Process) Tgid() (int32, error) {
return 0, common.ErrNotImplementedError
}
func (p *Process) Exe() (string, error) { func (p *Process) Exe() (string, error) {
return p.ExeWithContext(context.Background())
}
func (p *Process) ExeWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError return "", common.ErrNotImplementedError
} }
func (p *Process) Cmdline() (string, error) { func (p *Process) Cmdline() (string, error) {
return p.CmdlineWithContext(context.Background())
}
func (p *Process) CmdlineWithContext(ctx context.Context) (string, error) {
mib := []int32{CTLKern, KernProc, KernProcArgs, p.Pid} mib := []int32{CTLKern, KernProc, KernProcArgs, p.Pid}
buf, _, err := common.CallSyscall(mib) buf, _, err := common.CallSyscall(mib)
if err != nil { if err != nil {
@ -71,6 +95,10 @@ func (p *Process) Cmdline() (string, error) {
} }
func (p *Process) CmdlineSlice() ([]string, error) { func (p *Process) CmdlineSlice() ([]string, error) {
return p.CmdlineSliceWithContext(context.Background())
}
func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error) {
mib := []int32{CTLKern, KernProc, KernProcArgs, p.Pid} mib := []int32{CTLKern, KernProc, KernProcArgs, p.Pid}
buf, _, err := common.CallSyscall(mib) buf, _, err := common.CallSyscall(mib)
if err != nil { if err != nil {
@ -91,15 +119,31 @@ func (p *Process) CmdlineSlice() ([]string, error) {
return strParts, nil return strParts, nil
} }
func (p *Process) CreateTime() (int64, error) { func (p *Process) CreateTime() (int64, error) {
return p.CreateTimeWithContext(context.Background())
}
func (p *Process) CreateTimeWithContext(ctx context.Context) (int64, error) {
return 0, common.ErrNotImplementedError return 0, common.ErrNotImplementedError
} }
func (p *Process) Cwd() (string, error) { func (p *Process) Cwd() (string, error) {
return p.CwdWithContext(context.Background())
}
func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError return "", common.ErrNotImplementedError
} }
func (p *Process) Parent() (*Process, error) { func (p *Process) Parent() (*Process, error) {
return p.ParentWithContext(context.Background())
}
func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
return p, common.ErrNotImplementedError return p, common.ErrNotImplementedError
} }
func (p *Process) Status() (string, error) { func (p *Process) Status() (string, error) {
return p.StatusWithContext(context.Background())
}
func (p *Process) StatusWithContext(ctx context.Context) (string, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return "", err return "", err
@ -125,6 +169,10 @@ func (p *Process) Status() (string, error) {
return s, nil return s, nil
} }
func (p *Process) Uids() ([]int32, error) { func (p *Process) Uids() ([]int32, error) {
return p.UidsWithContext(context.Background())
}
func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return nil, err return nil, err
@ -137,6 +185,10 @@ func (p *Process) Uids() ([]int32, error) {
return uids, nil return uids, nil
} }
func (p *Process) Gids() ([]int32, error) { func (p *Process) Gids() ([]int32, error) {
return p.GidsWithContext(context.Background())
}
func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return nil, err return nil, err
@ -148,6 +200,10 @@ func (p *Process) Gids() ([]int32, error) {
return gids, nil return gids, nil
} }
func (p *Process) Terminal() (string, error) { func (p *Process) Terminal() (string, error) {
return p.TerminalWithContext(context.Background())
}
func (p *Process) TerminalWithContext(ctx context.Context) (string, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return "", err return "", err
@ -163,6 +219,10 @@ func (p *Process) Terminal() (string, error) {
return termmap[ttyNr], nil return termmap[ttyNr], nil
} }
func (p *Process) Nice() (int32, error) { func (p *Process) Nice() (int32, error) {
return p.NiceWithContext(context.Background())
}
func (p *Process) NiceWithContext(ctx context.Context) (int32, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return 0, err return 0, err
@ -170,17 +230,33 @@ func (p *Process) Nice() (int32, error) {
return int32(k.Nice), nil return int32(k.Nice), nil
} }
func (p *Process) IOnice() (int32, error) { func (p *Process) IOnice() (int32, error) {
return p.IOniceWithContext(context.Background())
}
func (p *Process) IOniceWithContext(ctx context.Context) (int32, error) {
return 0, common.ErrNotImplementedError return 0, common.ErrNotImplementedError
} }
func (p *Process) Rlimit() ([]RlimitStat, error) { func (p *Process) Rlimit() ([]RlimitStat, error) {
return p.RlimitWithContext(context.Background())
}
func (p *Process) RlimitWithContext(ctx context.Context) ([]RlimitStat, error) {
var rlimit []RlimitStat var rlimit []RlimitStat
return rlimit, common.ErrNotImplementedError return rlimit, common.ErrNotImplementedError
} }
func (p *Process) RlimitUsage(_ bool) ([]RlimitStat, error) { 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) {
var rlimit []RlimitStat var rlimit []RlimitStat
return rlimit, common.ErrNotImplementedError return rlimit, common.ErrNotImplementedError
} }
func (p *Process) IOCounters() (*IOCountersStat, error) { func (p *Process) IOCounters() (*IOCountersStat, error) {
return p.IOCountersWithContext(context.Background())
}
func (p *Process) IOCountersWithContext(ctx context.Context) (*IOCountersStat, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return nil, err return nil, err
@ -191,12 +267,24 @@ func (p *Process) IOCounters() (*IOCountersStat, error) {
}, nil }, nil
} }
func (p *Process) NumCtxSwitches() (*NumCtxSwitchesStat, error) { func (p *Process) NumCtxSwitches() (*NumCtxSwitchesStat, error) {
return p.NumCtxSwitchesWithContext(context.Background())
}
func (p *Process) NumCtxSwitchesWithContext(ctx context.Context) (*NumCtxSwitchesStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) NumFDs() (int32, error) { func (p *Process) NumFDs() (int32, error) {
return p.NumFDsWithContext(context.Background())
}
func (p *Process) NumFDsWithContext(ctx context.Context) (int32, error) {
return 0, common.ErrNotImplementedError return 0, common.ErrNotImplementedError
} }
func (p *Process) NumThreads() (int32, error) { func (p *Process) NumThreads() (int32, error) {
return p.NumThreadsWithContext(context.Background())
}
func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return 0, err return 0, err
@ -205,10 +293,18 @@ func (p *Process) NumThreads() (int32, error) {
return k.Numthreads, nil return k.Numthreads, nil
} }
func (p *Process) Threads() (map[int32]*cpu.TimesStat, error) { 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) ret := make(map[int32]*cpu.TimesStat)
return ret, common.ErrNotImplementedError return ret, common.ErrNotImplementedError
} }
func (p *Process) Times() (*cpu.TimesStat, error) { func (p *Process) Times() (*cpu.TimesStat, error) {
return p.TimesWithContext(context.Background())
}
func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return nil, err return nil, err
@ -220,9 +316,17 @@ func (p *Process) Times() (*cpu.TimesStat, error) {
}, nil }, nil
} }
func (p *Process) CPUAffinity() ([]int32, error) { func (p *Process) CPUAffinity() ([]int32, error) {
return p.CPUAffinityWithContext(context.Background())
}
func (p *Process) CPUAffinityWithContext(ctx context.Context) ([]int32, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) MemoryInfo() (*MemoryInfoStat, error) { func (p *Process) MemoryInfo() (*MemoryInfoStat, error) {
return p.MemoryInfoWithContext(context.Background())
}
func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return nil, err return nil, err
@ -239,10 +343,18 @@ func (p *Process) MemoryInfo() (*MemoryInfoStat, error) {
}, nil }, nil
} }
func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) { func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) {
return p.MemoryInfoExWithContext(context.Background())
}
func (p *Process) MemoryInfoExWithContext(ctx context.Context) (*MemoryInfoExStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) Children() ([]*Process, error) { func (p *Process) Children() ([]*Process, error) {
return p.ChildrenWithContext(context.Background())
}
func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) {
pids, err := common.CallPgrep(invoke, p.Pid) pids, err := common.CallPgrep(invoke, p.Pid)
if err != nil { if err != nil {
return nil, err return nil, err
@ -259,26 +371,50 @@ func (p *Process) Children() ([]*Process, error) {
} }
func (p *Process) OpenFiles() ([]OpenFilesStat, error) { func (p *Process) OpenFiles() ([]OpenFilesStat, error) {
return p.OpenFilesWithContext(context.Background())
}
func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) Connections() ([]net.ConnectionStat, error) { func (p *Process) Connections() ([]net.ConnectionStat, error) {
return p.ConnectionsWithContext(context.Background())
}
func (p *Process) ConnectionsWithContext(ctx context.Context) ([]net.ConnectionStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) NetIOCounters(pernic bool) ([]net.IOCountersStat, error) { func (p *Process) NetIOCounters(pernic bool) ([]net.IOCountersStat, error) {
return p.NetIOCountersWithContext(context.Background(), pernic)
}
func (p *Process) NetIOCountersWithContext(ctx context.Context, pernic bool) ([]net.IOCountersStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) IsRunning() (bool, error) { func (p *Process) IsRunning() (bool, error) {
return p.IsRunningWithContext(context.Background())
}
func (p *Process) IsRunningWithContext(ctx context.Context) (bool, error) {
return true, common.ErrNotImplementedError return true, common.ErrNotImplementedError
} }
func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) { func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) {
return p.MemoryMapsWithContext(context.Background(), grouped)
}
func (p *Process) MemoryMapsWithContext(ctx context.Context, grouped bool) (*[]MemoryMapsStat, error) {
var ret []MemoryMapsStat var ret []MemoryMapsStat
return &ret, common.ErrNotImplementedError return &ret, common.ErrNotImplementedError
} }
func Processes() ([]*Process, error) { func Processes() ([]*Process, error) {
return ProcessesWithContext(context.Background())
}
func ProcessesWithContext(ctx context.Context) ([]*Process, error) {
results := []*Process{} results := []*Process{}
mib := []int32{CTLKern, KernProc, KernProcProc, 0} mib := []int32{CTLKern, KernProc, KernProcProc, 0}
@ -316,6 +452,10 @@ func parseKinfoProc(buf []byte) (KinfoProc, error) {
} }
func (p *Process) getKProc() (*KinfoProc, error) { func (p *Process) getKProc() (*KinfoProc, error) {
return p.getKProcWithContext(context.Background())
}
func (p *Process) getKProcWithContext(ctx context.Context) (*KinfoProc, error) {
mib := []int32{CTLKern, KernProc, KernProcPID, p.Pid} mib := []int32{CTLKern, KernProc, KernProcPID, p.Pid}
buf, length, err := common.CallSyscall(mib) buf, length, err := common.CallSyscall(mib)

View File

@ -5,6 +5,7 @@ package process
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -83,6 +84,10 @@ func NewProcess(pid int32) (*Process, error) {
// Ppid returns Parent Process ID of the process. // Ppid returns Parent Process ID of the process.
func (p *Process) Ppid() (int32, error) { func (p *Process) Ppid() (int32, error) {
return p.PpidWithContext(context.Background())
}
func (p *Process) PpidWithContext(ctx context.Context) (int32, error) {
_, ppid, _, _, _, _, err := p.fillFromStat() _, ppid, _, _, _, _, err := p.fillFromStat()
if err != nil { if err != nil {
return -1, err return -1, err
@ -92,6 +97,10 @@ func (p *Process) Ppid() (int32, error) {
// Name returns name of the process. // Name returns name of the process.
func (p *Process) Name() (string, error) { func (p *Process) Name() (string, error) {
return p.NameWithContext(context.Background())
}
func (p *Process) NameWithContext(ctx context.Context) (string, error) {
if p.name == "" { if p.name == "" {
if err := p.fillFromStatus(); err != nil { if err := p.fillFromStatus(); err != nil {
return "", err return "", err
@ -100,25 +109,51 @@ func (p *Process) Name() (string, error) {
return p.name, nil return p.name, nil
} }
// Tgid returns tgid, a Linux-synonym for user-space Pid
func (p *Process) Tgid() (int32, error) {
if p.tgid == 0 {
if err := p.fillFromStatus(); err != nil {
return 0, err
}
}
return p.tgid, nil
}
// Exe returns executable path of the process. // Exe returns executable path of the process.
func (p *Process) Exe() (string, error) { func (p *Process) Exe() (string, error) {
return p.ExeWithContext(context.Background())
}
func (p *Process) ExeWithContext(ctx context.Context) (string, error) {
return p.fillFromExe() return p.fillFromExe()
} }
// Cmdline returns the command line arguments of the process as a string with // Cmdline returns the command line arguments of the process as a string with
// each argument separated by 0x20 ascii character. // each argument separated by 0x20 ascii character.
func (p *Process) Cmdline() (string, error) { func (p *Process) Cmdline() (string, error) {
return p.CmdlineWithContext(context.Background())
}
func (p *Process) CmdlineWithContext(ctx context.Context) (string, error) {
return p.fillFromCmdline() return p.fillFromCmdline()
} }
// CmdlineSlice returns the command line arguments of the process as a slice with each // CmdlineSlice returns the command line arguments of the process as a slice with each
// element being an argument. // element being an argument.
func (p *Process) CmdlineSlice() ([]string, error) { func (p *Process) CmdlineSlice() ([]string, error) {
return p.CmdlineSliceWithContext(context.Background())
}
func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error) {
return p.fillSliceFromCmdline() return p.fillSliceFromCmdline()
} }
// CreateTime returns created time of the process in milliseconds since the epoch, in UTC. // CreateTime returns created time of the process in milliseconds since the epoch, in UTC.
func (p *Process) CreateTime() (int64, error) { func (p *Process) CreateTime() (int64, error) {
return p.CreateTimeWithContext(context.Background())
}
func (p *Process) CreateTimeWithContext(ctx context.Context) (int64, error) {
_, _, _, createTime, _, _, err := p.fillFromStat() _, _, _, createTime, _, _, err := p.fillFromStat()
if err != nil { if err != nil {
return 0, err return 0, err
@ -128,11 +163,19 @@ func (p *Process) CreateTime() (int64, error) {
// Cwd returns current working directory of the process. // Cwd returns current working directory of the process.
func (p *Process) Cwd() (string, error) { func (p *Process) Cwd() (string, error) {
return p.CwdWithContext(context.Background())
}
func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
return p.fillFromCwd() return p.fillFromCwd()
} }
// Parent returns parent Process of the process. // Parent returns parent Process of the process.
func (p *Process) Parent() (*Process, error) { func (p *Process) Parent() (*Process, error) {
return p.ParentWithContext(context.Background())
}
func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
err := p.fillFromStatus() err := p.fillFromStatus()
if err != nil { if err != nil {
return nil, err return nil, err
@ -149,6 +192,10 @@ func (p *Process) Parent() (*Process, error) {
// Z: Zombie W: Wait L: Lock // Z: Zombie W: Wait L: Lock
// The charactor is same within all supported platforms. // The charactor is same within all supported platforms.
func (p *Process) Status() (string, error) { func (p *Process) Status() (string, error) {
return p.StatusWithContext(context.Background())
}
func (p *Process) StatusWithContext(ctx context.Context) (string, error) {
err := p.fillFromStatus() err := p.fillFromStatus()
if err != nil { if err != nil {
return "", err return "", err
@ -158,6 +205,10 @@ func (p *Process) Status() (string, error) {
// Uids returns user ids of the process as a slice of the int // Uids returns user ids of the process as a slice of the int
func (p *Process) Uids() ([]int32, error) { func (p *Process) Uids() ([]int32, error) {
return p.UidsWithContext(context.Background())
}
func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) {
err := p.fillFromStatus() err := p.fillFromStatus()
if err != nil { if err != nil {
return []int32{}, err return []int32{}, err
@ -167,6 +218,10 @@ func (p *Process) Uids() ([]int32, error) {
// Gids returns group ids of the process as a slice of the int // Gids returns group ids of the process as a slice of the int
func (p *Process) Gids() ([]int32, error) { func (p *Process) Gids() ([]int32, error) {
return p.GidsWithContext(context.Background())
}
func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) {
err := p.fillFromStatus() err := p.fillFromStatus()
if err != nil { if err != nil {
return []int32{}, err return []int32{}, err
@ -176,6 +231,10 @@ func (p *Process) Gids() ([]int32, error) {
// Terminal returns a terminal which is associated with the process. // Terminal returns a terminal which is associated with the process.
func (p *Process) Terminal() (string, error) { func (p *Process) Terminal() (string, error) {
return p.TerminalWithContext(context.Background())
}
func (p *Process) TerminalWithContext(ctx context.Context) (string, error) {
terminal, _, _, _, _, _, err := p.fillFromStat() terminal, _, _, _, _, _, err := p.fillFromStat()
if err != nil { if err != nil {
return "", err return "", err
@ -186,6 +245,10 @@ func (p *Process) Terminal() (string, error) {
// Nice returns a nice value (priority). // Nice returns a nice value (priority).
// Notice: gopsutil can not set nice value. // Notice: gopsutil can not set nice value.
func (p *Process) Nice() (int32, error) { func (p *Process) Nice() (int32, error) {
return p.NiceWithContext(context.Background())
}
func (p *Process) NiceWithContext(ctx context.Context) (int32, error) {
_, _, _, _, _, nice, err := p.fillFromStat() _, _, _, _, _, nice, err := p.fillFromStat()
if err != nil { if err != nil {
return 0, err return 0, err
@ -195,11 +258,19 @@ func (p *Process) Nice() (int32, error) {
// IOnice returns process I/O nice value (priority). // IOnice returns process I/O nice value (priority).
func (p *Process) IOnice() (int32, error) { func (p *Process) IOnice() (int32, error) {
return p.IOniceWithContext(context.Background())
}
func (p *Process) IOniceWithContext(ctx context.Context) (int32, error) {
return 0, common.ErrNotImplementedError return 0, common.ErrNotImplementedError
} }
// Rlimit returns Resource Limits. // Rlimit returns Resource Limits.
func (p *Process) Rlimit() ([]RlimitStat, error) { func (p *Process) Rlimit() ([]RlimitStat, error) {
return p.RlimitWithContext(context.Background())
}
func (p *Process) RlimitWithContext(ctx context.Context) ([]RlimitStat, error) {
return p.RlimitUsage(false) return p.RlimitUsage(false)
} }
@ -207,6 +278,10 @@ func (p *Process) Rlimit() ([]RlimitStat, error) {
// If gatherUsed is true, the currently used value will be gathered and added // If gatherUsed is true, the currently used value will be gathered and added
// to the resulting RlimitStat. // to the resulting RlimitStat.
func (p *Process) RlimitUsage(gatherUsed bool) ([]RlimitStat, error) { 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) {
rlimits, err := p.fillFromLimits() rlimits, err := p.fillFromLimits()
if !gatherUsed || err != nil { if !gatherUsed || err != nil {
return rlimits, err return rlimits, err
@ -263,11 +338,19 @@ func (p *Process) RlimitUsage(gatherUsed bool) ([]RlimitStat, error) {
// IOCounters returns IO Counters. // IOCounters returns IO Counters.
func (p *Process) IOCounters() (*IOCountersStat, error) { func (p *Process) IOCounters() (*IOCountersStat, error) {
return p.IOCountersWithContext(context.Background())
}
func (p *Process) IOCountersWithContext(ctx context.Context) (*IOCountersStat, error) {
return p.fillFromIO() return p.fillFromIO()
} }
// NumCtxSwitches returns the number of the context switches of the process. // NumCtxSwitches returns the number of the context switches of the process.
func (p *Process) NumCtxSwitches() (*NumCtxSwitchesStat, error) { func (p *Process) NumCtxSwitches() (*NumCtxSwitchesStat, error) {
return p.NumCtxSwitchesWithContext(context.Background())
}
func (p *Process) NumCtxSwitchesWithContext(ctx context.Context) (*NumCtxSwitchesStat, error) {
err := p.fillFromStatus() err := p.fillFromStatus()
if err != nil { if err != nil {
return nil, err return nil, err
@ -277,12 +360,20 @@ func (p *Process) NumCtxSwitches() (*NumCtxSwitchesStat, error) {
// NumFDs returns the number of File Descriptors used by the process. // NumFDs returns the number of File Descriptors used by the process.
func (p *Process) NumFDs() (int32, error) { func (p *Process) NumFDs() (int32, error) {
return p.NumFDsWithContext(context.Background())
}
func (p *Process) NumFDsWithContext(ctx context.Context) (int32, error) {
_, fnames, err := p.fillFromfdList() _, fnames, err := p.fillFromfdList()
return int32(len(fnames)), err return int32(len(fnames)), err
} }
// NumThreads returns the number of threads used by the process. // NumThreads returns the number of threads used by the process.
func (p *Process) NumThreads() (int32, error) { func (p *Process) NumThreads() (int32, error) {
return p.NumThreadsWithContext(context.Background())
}
func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) {
err := p.fillFromStatus() err := p.fillFromStatus()
if err != nil { if err != nil {
return 0, err return 0, err
@ -291,6 +382,10 @@ func (p *Process) NumThreads() (int32, error) {
} }
func (p *Process) Threads() (map[int32]*cpu.TimesStat, error) { 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) ret := make(map[int32]*cpu.TimesStat)
taskPath := common.HostProc(strconv.Itoa(int(p.Pid)), "task") taskPath := common.HostProc(strconv.Itoa(int(p.Pid)), "task")
@ -312,6 +407,10 @@ func (p *Process) Threads() (map[int32]*cpu.TimesStat, error) {
// Times returns CPU times of the process. // Times returns CPU times of the process.
func (p *Process) Times() (*cpu.TimesStat, error) { func (p *Process) Times() (*cpu.TimesStat, error) {
return p.TimesWithContext(context.Background())
}
func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) {
_, _, cpuTimes, _, _, _, err := p.fillFromStat() _, _, cpuTimes, _, _, _, err := p.fillFromStat()
if err != nil { if err != nil {
return nil, err return nil, err
@ -323,11 +422,19 @@ func (p *Process) Times() (*cpu.TimesStat, error) {
// //
// Notice: Not implemented yet. // Notice: Not implemented yet.
func (p *Process) CPUAffinity() ([]int32, error) { func (p *Process) CPUAffinity() ([]int32, error) {
return p.CPUAffinityWithContext(context.Background())
}
func (p *Process) CPUAffinityWithContext(ctx context.Context) ([]int32, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
// MemoryInfo returns platform in-dependend memory information, such as RSS, VMS and Swap // MemoryInfo returns platform in-dependend memory information, such as RSS, VMS and Swap
func (p *Process) MemoryInfo() (*MemoryInfoStat, error) { func (p *Process) MemoryInfo() (*MemoryInfoStat, error) {
return p.MemoryInfoWithContext(context.Background())
}
func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, error) {
meminfo, _, err := p.fillFromStatm() meminfo, _, err := p.fillFromStatm()
if err != nil { if err != nil {
return nil, err return nil, err
@ -337,6 +444,10 @@ func (p *Process) MemoryInfo() (*MemoryInfoStat, error) {
// MemoryInfoEx returns platform dependend memory information. // MemoryInfoEx returns platform dependend memory information.
func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) { func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) {
return p.MemoryInfoExWithContext(context.Background())
}
func (p *Process) MemoryInfoExWithContext(ctx context.Context) (*MemoryInfoExStat, error) {
_, memInfoEx, err := p.fillFromStatm() _, memInfoEx, err := p.fillFromStatm()
if err != nil { if err != nil {
return nil, err return nil, err
@ -346,6 +457,10 @@ func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) {
// Children returns a slice of Process of the process. // Children returns a slice of Process of the process.
func (p *Process) Children() ([]*Process, error) { func (p *Process) Children() ([]*Process, error) {
return p.ChildrenWithContext(context.Background())
}
func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) {
pids, err := common.CallPgrep(invoke, p.Pid) pids, err := common.CallPgrep(invoke, p.Pid)
if err != nil { if err != nil {
if pids == nil || len(pids) == 0 { if pids == nil || len(pids) == 0 {
@ -367,6 +482,10 @@ func (p *Process) Children() ([]*Process, error) {
// OpenFiles returns a slice of OpenFilesStat opend by the process. // OpenFiles returns a slice of OpenFilesStat opend by the process.
// OpenFilesStat includes a file path and file descriptor. // OpenFilesStat includes a file path and file descriptor.
func (p *Process) OpenFiles() ([]OpenFilesStat, error) { func (p *Process) OpenFiles() ([]OpenFilesStat, error) {
return p.OpenFilesWithContext(context.Background())
}
func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, error) {
_, ofs, err := p.fillFromfd() _, ofs, err := p.fillFromfd()
if err != nil { if err != nil {
return nil, err return nil, err
@ -382,11 +501,19 @@ func (p *Process) OpenFiles() ([]OpenFilesStat, error) {
// Connections returns a slice of net.ConnectionStat used by the process. // Connections returns a slice of net.ConnectionStat used by the process.
// This returns all kind of the connection. This measn TCP, UDP or UNIX. // This returns all kind of the connection. This measn TCP, UDP or UNIX.
func (p *Process) Connections() ([]net.ConnectionStat, error) { func (p *Process) Connections() ([]net.ConnectionStat, error) {
return p.ConnectionsWithContext(context.Background())
}
func (p *Process) ConnectionsWithContext(ctx context.Context) ([]net.ConnectionStat, error) {
return net.ConnectionsPid("all", p.Pid) return net.ConnectionsPid("all", p.Pid)
} }
// NetIOCounters returns NetIOCounters of the process. // NetIOCounters returns NetIOCounters of the process.
func (p *Process) NetIOCounters(pernic bool) ([]net.IOCountersStat, error) { func (p *Process) NetIOCounters(pernic bool) ([]net.IOCountersStat, error) {
return p.NetIOCountersWithContext(context.Background(), pernic)
}
func (p *Process) NetIOCountersWithContext(ctx context.Context, pernic bool) ([]net.IOCountersStat, error) {
filename := common.HostProc(strconv.Itoa(int(p.Pid)), "net/dev") filename := common.HostProc(strconv.Itoa(int(p.Pid)), "net/dev")
return net.IOCountersByFile(pernic, filename) return net.IOCountersByFile(pernic, filename)
} }
@ -394,11 +521,19 @@ func (p *Process) NetIOCounters(pernic bool) ([]net.IOCountersStat, error) {
// IsRunning returns whether the process is running or not. // IsRunning returns whether the process is running or not.
// Not implemented yet. // Not implemented yet.
func (p *Process) IsRunning() (bool, error) { func (p *Process) IsRunning() (bool, error) {
return p.IsRunningWithContext(context.Background())
}
func (p *Process) IsRunningWithContext(ctx context.Context) (bool, error) {
return true, common.ErrNotImplementedError return true, common.ErrNotImplementedError
} }
// MemoryMaps get memory maps from /proc/(pid)/smaps // MemoryMaps get memory maps from /proc/(pid)/smaps
func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) { func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) {
return p.MemoryMapsWithContext(context.Background(), grouped)
}
func (p *Process) MemoryMapsWithContext(ctx context.Context, grouped bool) (*[]MemoryMapsStat, error) {
pid := p.Pid pid := p.Pid
var ret []MemoryMapsStat var ret []MemoryMapsStat
smapsPath := common.HostProc(strconv.Itoa(int(pid)), "smaps") smapsPath := common.HostProc(strconv.Itoa(int(pid)), "smaps")
@ -493,6 +628,10 @@ func limitToInt(val string) (int32, error) {
// Get num_fds from /proc/(pid)/limits // Get num_fds from /proc/(pid)/limits
func (p *Process) fillFromLimits() ([]RlimitStat, error) { func (p *Process) fillFromLimits() ([]RlimitStat, error) {
return p.fillFromLimitsWithContext(context.Background())
}
func (p *Process) fillFromLimitsWithContext(ctx context.Context) ([]RlimitStat, error) {
pid := p.Pid pid := p.Pid
limitsFile := common.HostProc(strconv.Itoa(int(pid)), "limits") limitsFile := common.HostProc(strconv.Itoa(int(pid)), "limits")
d, err := os.Open(limitsFile) d, err := os.Open(limitsFile)
@ -586,6 +725,10 @@ func (p *Process) fillFromLimits() ([]RlimitStat, error) {
// Get list of /proc/(pid)/fd files // Get list of /proc/(pid)/fd files
func (p *Process) fillFromfdList() (string, []string, error) { func (p *Process) fillFromfdList() (string, []string, error) {
return p.fillFromfdListWithContext(context.Background())
}
func (p *Process) fillFromfdListWithContext(ctx context.Context) (string, []string, error) {
pid := p.Pid pid := p.Pid
statPath := common.HostProc(strconv.Itoa(int(pid)), "fd") statPath := common.HostProc(strconv.Itoa(int(pid)), "fd")
d, err := os.Open(statPath) d, err := os.Open(statPath)
@ -599,6 +742,10 @@ func (p *Process) fillFromfdList() (string, []string, error) {
// Get num_fds from /proc/(pid)/fd // Get num_fds from /proc/(pid)/fd
func (p *Process) fillFromfd() (int32, []*OpenFilesStat, error) { func (p *Process) fillFromfd() (int32, []*OpenFilesStat, error) {
return p.fillFromfdWithContext(context.Background())
}
func (p *Process) fillFromfdWithContext(ctx context.Context) (int32, []*OpenFilesStat, error) {
statPath, fnames, err := p.fillFromfdList() statPath, fnames, err := p.fillFromfdList()
if err != nil { if err != nil {
return 0, nil, err return 0, nil, err
@ -628,6 +775,10 @@ func (p *Process) fillFromfd() (int32, []*OpenFilesStat, error) {
// Get cwd from /proc/(pid)/cwd // Get cwd from /proc/(pid)/cwd
func (p *Process) fillFromCwd() (string, error) { func (p *Process) fillFromCwd() (string, error) {
return p.fillFromCwdWithContext(context.Background())
}
func (p *Process) fillFromCwdWithContext(ctx context.Context) (string, error) {
pid := p.Pid pid := p.Pid
cwdPath := common.HostProc(strconv.Itoa(int(pid)), "cwd") cwdPath := common.HostProc(strconv.Itoa(int(pid)), "cwd")
cwd, err := os.Readlink(cwdPath) cwd, err := os.Readlink(cwdPath)
@ -639,6 +790,10 @@ func (p *Process) fillFromCwd() (string, error) {
// Get exe from /proc/(pid)/exe // Get exe from /proc/(pid)/exe
func (p *Process) fillFromExe() (string, error) { func (p *Process) fillFromExe() (string, error) {
return p.fillFromExeWithContext(context.Background())
}
func (p *Process) fillFromExeWithContext(ctx context.Context) (string, error) {
pid := p.Pid pid := p.Pid
exePath := common.HostProc(strconv.Itoa(int(pid)), "exe") exePath := common.HostProc(strconv.Itoa(int(pid)), "exe")
exe, err := os.Readlink(exePath) exe, err := os.Readlink(exePath)
@ -650,6 +805,10 @@ func (p *Process) fillFromExe() (string, error) {
// Get cmdline from /proc/(pid)/cmdline // Get cmdline from /proc/(pid)/cmdline
func (p *Process) fillFromCmdline() (string, error) { func (p *Process) fillFromCmdline() (string, error) {
return p.fillFromCmdlineWithContext(context.Background())
}
func (p *Process) fillFromCmdlineWithContext(ctx context.Context) (string, error) {
pid := p.Pid pid := p.Pid
cmdPath := common.HostProc(strconv.Itoa(int(pid)), "cmdline") cmdPath := common.HostProc(strconv.Itoa(int(pid)), "cmdline")
cmdline, err := ioutil.ReadFile(cmdPath) cmdline, err := ioutil.ReadFile(cmdPath)
@ -667,6 +826,10 @@ func (p *Process) fillFromCmdline() (string, error) {
} }
func (p *Process) fillSliceFromCmdline() ([]string, error) { func (p *Process) fillSliceFromCmdline() ([]string, error) {
return p.fillSliceFromCmdlineWithContext(context.Background())
}
func (p *Process) fillSliceFromCmdlineWithContext(ctx context.Context) ([]string, error) {
pid := p.Pid pid := p.Pid
cmdPath := common.HostProc(strconv.Itoa(int(pid)), "cmdline") cmdPath := common.HostProc(strconv.Itoa(int(pid)), "cmdline")
cmdline, err := ioutil.ReadFile(cmdPath) cmdline, err := ioutil.ReadFile(cmdPath)
@ -690,6 +853,10 @@ func (p *Process) fillSliceFromCmdline() ([]string, error) {
// Get IO status from /proc/(pid)/io // Get IO status from /proc/(pid)/io
func (p *Process) fillFromIO() (*IOCountersStat, error) { func (p *Process) fillFromIO() (*IOCountersStat, error) {
return p.fillFromIOWithContext(context.Background())
}
func (p *Process) fillFromIOWithContext(ctx context.Context) (*IOCountersStat, error) {
pid := p.Pid pid := p.Pid
ioPath := common.HostProc(strconv.Itoa(int(pid)), "io") ioPath := common.HostProc(strconv.Itoa(int(pid)), "io")
ioline, err := ioutil.ReadFile(ioPath) ioline, err := ioutil.ReadFile(ioPath)
@ -729,6 +896,10 @@ func (p *Process) fillFromIO() (*IOCountersStat, error) {
// Get memory info from /proc/(pid)/statm // Get memory info from /proc/(pid)/statm
func (p *Process) fillFromStatm() (*MemoryInfoStat, *MemoryInfoExStat, error) { func (p *Process) fillFromStatm() (*MemoryInfoStat, *MemoryInfoExStat, error) {
return p.fillFromStatmWithContext(context.Background())
}
func (p *Process) fillFromStatmWithContext(ctx context.Context) (*MemoryInfoStat, *MemoryInfoExStat, error) {
pid := p.Pid pid := p.Pid
memPath := common.HostProc(strconv.Itoa(int(pid)), "statm") memPath := common.HostProc(strconv.Itoa(int(pid)), "statm")
contents, err := ioutil.ReadFile(memPath) contents, err := ioutil.ReadFile(memPath)
@ -781,6 +952,10 @@ func (p *Process) fillFromStatm() (*MemoryInfoStat, *MemoryInfoExStat, error) {
// Get various status from /proc/(pid)/status // Get various status from /proc/(pid)/status
func (p *Process) fillFromStatus() error { func (p *Process) fillFromStatus() error {
return p.fillFromStatusWithContext(context.Background())
}
func (p *Process) fillFromStatusWithContext(ctx context.Context) error {
pid := p.Pid pid := p.Pid
statPath := common.HostProc(strconv.Itoa(int(pid)), "status") statPath := common.HostProc(strconv.Itoa(int(pid)), "status")
contents, err := ioutil.ReadFile(statPath) contents, err := ioutil.ReadFile(statPath)
@ -820,6 +995,12 @@ func (p *Process) fillFromStatus() error {
return err return err
} }
p.parent = int32(pval) p.parent = int32(pval)
case "Tgid":
pval, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return err
}
p.tgid = int32(pval)
case "Uid": case "Uid":
p.uids = make([]int32, 0, 4) p.uids = make([]int32, 0, 4)
for _, i := range strings.Split(value, "\t") { for _, i := range strings.Split(value, "\t") {
@ -935,6 +1116,10 @@ func (p *Process) fillFromStatus() error {
} }
func (p *Process) fillFromTIDStat(tid int32) (string, int32, *cpu.TimesStat, int64, uint32, int32, error) { func (p *Process) fillFromTIDStat(tid int32) (string, int32, *cpu.TimesStat, int64, uint32, int32, error) {
return p.fillFromTIDStatWithContext(context.Background(), tid)
}
func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (string, int32, *cpu.TimesStat, int64, uint32, int32, error) {
pid := p.Pid pid := p.Pid
var statPath string var statPath string
@ -1009,17 +1194,29 @@ func (p *Process) fillFromTIDStat(tid int32) (string, int32, *cpu.TimesStat, int
} }
func (p *Process) fillFromStat() (string, int32, *cpu.TimesStat, int64, uint32, int32, error) { func (p *Process) fillFromStat() (string, int32, *cpu.TimesStat, int64, uint32, int32, error) {
return p.fillFromStatWithContext(context.Background())
}
func (p *Process) fillFromStatWithContext(ctx context.Context) (string, int32, *cpu.TimesStat, int64, uint32, int32, error) {
return p.fillFromTIDStat(-1) return p.fillFromTIDStat(-1)
} }
// Pids returns a slice of process ID list which are running now. // Pids returns a slice of process ID list which are running now.
func Pids() ([]int32, error) { func Pids() ([]int32, error) {
return PidsWithContext(context.Background())
}
func PidsWithContext(ctx context.Context) ([]int32, error) {
return readPidsFromDir(common.HostProc()) return readPidsFromDir(common.HostProc())
} }
// Process returns a slice of pointers to Process structs for all // Process returns a slice of pointers to Process structs for all
// currently running processes. // currently running processes.
func Processes() ([]*Process, error) { func Processes() ([]*Process, error) {
return ProcessesWithContext(context.Background())
}
func ProcessesWithContext(ctx context.Context) ([]*Process, error) {
out := []*Process{} out := []*Process{}
pids, err := Pids() pids, err := Pids()

View File

@ -15,6 +15,7 @@ import (
net "github.com/shirou/gopsutil/net" net "github.com/shirou/gopsutil/net"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
import "context"
// MemoryInfoExStat is different between OSes // MemoryInfoExStat is different between OSes
type MemoryInfoExStat struct { type MemoryInfoExStat struct {
@ -24,6 +25,10 @@ type MemoryMapsStat struct {
} }
func Pids() ([]int32, error) { func Pids() ([]int32, error) {
return PidsWithContext(context.Background())
}
func PidsWithContext(ctx context.Context) ([]int32, error) {
var ret []int32 var ret []int32
procs, err := Processes() procs, err := Processes()
if err != nil { if err != nil {
@ -38,6 +43,10 @@ func Pids() ([]int32, error) {
} }
func (p *Process) Ppid() (int32, error) { func (p *Process) Ppid() (int32, error) {
return p.PpidWithContext(context.Background())
}
func (p *Process) PpidWithContext(ctx context.Context) (int32, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return 0, err return 0, err
@ -46,6 +55,10 @@ func (p *Process) Ppid() (int32, error) {
return k.Ppid, nil return k.Ppid, nil
} }
func (p *Process) Name() (string, error) { func (p *Process) Name() (string, error) {
return p.NameWithContext(context.Background())
}
func (p *Process) NameWithContext(ctx context.Context) (string, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return "", err return "", err
@ -53,11 +66,22 @@ func (p *Process) Name() (string, error) {
return common.IntToString(k.Comm[:]), nil return common.IntToString(k.Comm[:]), nil
} }
func (p *Process) Tgid() (int32, error) {
return 0, common.ErrNotImplementedError
}
func (p *Process) Exe() (string, error) { func (p *Process) Exe() (string, error) {
return p.ExeWithContext(context.Background())
}
func (p *Process) ExeWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError return "", common.ErrNotImplementedError
} }
func (p *Process) CmdlineSlice() ([]string, error) { func (p *Process) CmdlineSlice() ([]string, error) {
return p.CmdlineSliceWithContext(context.Background())
}
func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error) {
mib := []int32{CTLKern, KernProcArgs, p.Pid, KernProcArgv} mib := []int32{CTLKern, KernProcArgs, p.Pid, KernProcArgv}
buf, _, err := common.CallSyscall(mib) buf, _, err := common.CallSyscall(mib)
@ -81,6 +105,10 @@ func (p *Process) CmdlineSlice() ([]string, error) {
} }
func (p *Process) Cmdline() (string, error) { func (p *Process) Cmdline() (string, error) {
return p.CmdlineWithContext(context.Background())
}
func (p *Process) CmdlineWithContext(ctx context.Context) (string, error) {
argv, err := p.CmdlineSlice() argv, err := p.CmdlineSlice()
if err != nil { if err != nil {
return "", err return "", err
@ -89,15 +117,31 @@ func (p *Process) Cmdline() (string, error) {
} }
func (p *Process) CreateTime() (int64, error) { func (p *Process) CreateTime() (int64, error) {
return p.CreateTimeWithContext(context.Background())
}
func (p *Process) CreateTimeWithContext(ctx context.Context) (int64, error) {
return 0, common.ErrNotImplementedError return 0, common.ErrNotImplementedError
} }
func (p *Process) Cwd() (string, error) { func (p *Process) Cwd() (string, error) {
return p.CwdWithContext(context.Background())
}
func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError return "", common.ErrNotImplementedError
} }
func (p *Process) Parent() (*Process, error) { func (p *Process) Parent() (*Process, error) {
return p.ParentWithContext(context.Background())
}
func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
return p, common.ErrNotImplementedError return p, common.ErrNotImplementedError
} }
func (p *Process) Status() (string, error) { func (p *Process) Status() (string, error) {
return p.StatusWithContext(context.Background())
}
func (p *Process) StatusWithContext(ctx context.Context) (string, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return "", err return "", err
@ -119,6 +163,10 @@ func (p *Process) Status() (string, error) {
return s, nil return s, nil
} }
func (p *Process) Uids() ([]int32, error) { func (p *Process) Uids() ([]int32, error) {
return p.UidsWithContext(context.Background())
}
func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return nil, err return nil, err
@ -131,6 +179,10 @@ func (p *Process) Uids() ([]int32, error) {
return uids, nil return uids, nil
} }
func (p *Process) Gids() ([]int32, error) { func (p *Process) Gids() ([]int32, error) {
return p.GidsWithContext(context.Background())
}
func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return nil, err return nil, err
@ -142,6 +194,10 @@ func (p *Process) Gids() ([]int32, error) {
return gids, nil return gids, nil
} }
func (p *Process) Terminal() (string, error) { func (p *Process) Terminal() (string, error) {
return p.TerminalWithContext(context.Background())
}
func (p *Process) TerminalWithContext(ctx context.Context) (string, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return "", err return "", err
@ -157,6 +213,10 @@ func (p *Process) Terminal() (string, error) {
return termmap[ttyNr], nil return termmap[ttyNr], nil
} }
func (p *Process) Nice() (int32, error) { func (p *Process) Nice() (int32, error) {
return p.NiceWithContext(context.Background())
}
func (p *Process) NiceWithContext(ctx context.Context) (int32, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return 0, err return 0, err
@ -164,17 +224,33 @@ func (p *Process) Nice() (int32, error) {
return int32(k.Nice), nil return int32(k.Nice), nil
} }
func (p *Process) IOnice() (int32, error) { func (p *Process) IOnice() (int32, error) {
return p.IOniceWithContext(context.Background())
}
func (p *Process) IOniceWithContext(ctx context.Context) (int32, error) {
return 0, common.ErrNotImplementedError return 0, common.ErrNotImplementedError
} }
func (p *Process) Rlimit() ([]RlimitStat, error) { func (p *Process) Rlimit() ([]RlimitStat, error) {
return p.RlimitWithContext(context.Background())
}
func (p *Process) RlimitWithContext(ctx context.Context) ([]RlimitStat, error) {
var rlimit []RlimitStat var rlimit []RlimitStat
return rlimit, common.ErrNotImplementedError return rlimit, common.ErrNotImplementedError
} }
func (p *Process) RlimitUsage(_ bool) ([]RlimitStat, error) { 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) {
var rlimit []RlimitStat var rlimit []RlimitStat
return rlimit, common.ErrNotImplementedError return rlimit, common.ErrNotImplementedError
} }
func (p *Process) IOCounters() (*IOCountersStat, error) { func (p *Process) IOCounters() (*IOCountersStat, error) {
return p.IOCountersWithContext(context.Background())
}
func (p *Process) IOCountersWithContext(ctx context.Context) (*IOCountersStat, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return nil, err return nil, err
@ -185,20 +261,40 @@ func (p *Process) IOCounters() (*IOCountersStat, error) {
}, nil }, nil
} }
func (p *Process) NumCtxSwitches() (*NumCtxSwitchesStat, error) { func (p *Process) NumCtxSwitches() (*NumCtxSwitchesStat, error) {
return p.NumCtxSwitchesWithContext(context.Background())
}
func (p *Process) NumCtxSwitchesWithContext(ctx context.Context) (*NumCtxSwitchesStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) NumFDs() (int32, error) { func (p *Process) NumFDs() (int32, error) {
return p.NumFDsWithContext(context.Background())
}
func (p *Process) NumFDsWithContext(ctx context.Context) (int32, error) {
return 0, common.ErrNotImplementedError return 0, common.ErrNotImplementedError
} }
func (p *Process) NumThreads() (int32, error) { func (p *Process) NumThreads() (int32, error) {
return p.NumThreadsWithContext(context.Background())
}
func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) {
/* not supported, just return 1 */ /* not supported, just return 1 */
return 1, nil return 1, nil
} }
func (p *Process) Threads() (map[int32]*cpu.TimesStat, error) { 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) ret := make(map[int32]*cpu.TimesStat)
return ret, common.ErrNotImplementedError return ret, common.ErrNotImplementedError
} }
func (p *Process) Times() (*cpu.TimesStat, error) { func (p *Process) Times() (*cpu.TimesStat, error) {
return p.TimesWithContext(context.Background())
}
func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return nil, err return nil, err
@ -210,9 +306,17 @@ func (p *Process) Times() (*cpu.TimesStat, error) {
}, nil }, nil
} }
func (p *Process) CPUAffinity() ([]int32, error) { func (p *Process) CPUAffinity() ([]int32, error) {
return p.CPUAffinityWithContext(context.Background())
}
func (p *Process) CPUAffinityWithContext(ctx context.Context) ([]int32, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) MemoryInfo() (*MemoryInfoStat, error) { func (p *Process) MemoryInfo() (*MemoryInfoStat, error) {
return p.MemoryInfoWithContext(context.Background())
}
func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, error) {
k, err := p.getKProc() k, err := p.getKProc()
if err != nil { if err != nil {
return nil, err return nil, err
@ -229,10 +333,18 @@ func (p *Process) MemoryInfo() (*MemoryInfoStat, error) {
}, nil }, nil
} }
func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) { func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) {
return p.MemoryInfoExWithContext(context.Background())
}
func (p *Process) MemoryInfoExWithContext(ctx context.Context) (*MemoryInfoExStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) Children() ([]*Process, error) { func (p *Process) Children() ([]*Process, error) {
return p.ChildrenWithContext(context.Background())
}
func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) {
pids, err := common.CallPgrep(invoke, p.Pid) pids, err := common.CallPgrep(invoke, p.Pid)
if err != nil { if err != nil {
return nil, err return nil, err
@ -249,26 +361,50 @@ func (p *Process) Children() ([]*Process, error) {
} }
func (p *Process) OpenFiles() ([]OpenFilesStat, error) { func (p *Process) OpenFiles() ([]OpenFilesStat, error) {
return p.OpenFilesWithContext(context.Background())
}
func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) Connections() ([]net.ConnectionStat, error) { func (p *Process) Connections() ([]net.ConnectionStat, error) {
return p.ConnectionsWithContext(context.Background())
}
func (p *Process) ConnectionsWithContext(ctx context.Context) ([]net.ConnectionStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) NetIOCounters(pernic bool) ([]net.IOCountersStat, error) { func (p *Process) NetIOCounters(pernic bool) ([]net.IOCountersStat, error) {
return p.NetIOCountersWithContext(context.Background(), pernic)
}
func (p *Process) NetIOCountersWithContext(ctx context.Context, pernic bool) ([]net.IOCountersStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) IsRunning() (bool, error) { func (p *Process) IsRunning() (bool, error) {
return p.IsRunningWithContext(context.Background())
}
func (p *Process) IsRunningWithContext(ctx context.Context) (bool, error) {
return true, common.ErrNotImplementedError return true, common.ErrNotImplementedError
} }
func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) { func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) {
return p.MemoryMapsWithContext(context.Background(), grouped)
}
func (p *Process) MemoryMapsWithContext(ctx context.Context, grouped bool) (*[]MemoryMapsStat, error) {
var ret []MemoryMapsStat var ret []MemoryMapsStat
return &ret, common.ErrNotImplementedError return &ret, common.ErrNotImplementedError
} }
func Processes() ([]*Process, error) { func Processes() ([]*Process, error) {
return ProcessesWithContext(context.Background())
}
func ProcessesWithContext(ctx context.Context) ([]*Process, error) {
results := []*Process{} results := []*Process{}
buf, length, err := CallKernProcSyscall(KernProcAll, 0) buf, length, err := CallKernProcSyscall(KernProcAll, 0)
@ -306,6 +442,10 @@ func parseKinfoProc(buf []byte) (KinfoProc, error) {
} }
func (p *Process) getKProc() (*KinfoProc, error) { func (p *Process) getKProc() (*KinfoProc, error) {
return p.getKProcWithContext(context.Background())
}
func (p *Process) getKProcWithContext(ctx context.Context) (*KinfoProc, error) {
buf, length, err := CallKernProcSyscall(KernProcPID, p.Pid) buf, length, err := CallKernProcSyscall(KernProcPID, p.Pid)
if err != nil { if err != nil {
return nil, err return nil, err
@ -328,6 +468,10 @@ func NewProcess(pid int32) (*Process, error) {
} }
func CallKernProcSyscall(op int32, arg int32) ([]byte, uint64, error) { func CallKernProcSyscall(op int32, arg int32) ([]byte, uint64, error) {
return CallKernProcSyscallWithContext(context.Background(), op, arg)
}
func CallKernProcSyscallWithContext(ctx context.Context, op int32, arg int32) ([]byte, uint64, error) {
mib := []int32{CTLKern, KernProc, op, arg, sizeOfKinfoProc, 0} mib := []int32{CTLKern, KernProc, op, arg, sizeOfKinfoProc, 0}
mibptr := unsafe.Pointer(&mib[0]) mibptr := unsafe.Pointer(&mib[0])
miblen := uint64(len(mib)) miblen := uint64(len(mib))

View File

@ -3,6 +3,7 @@
package process package process
import ( import (
"context"
"os" "os"
"os/user" "os/user"
"path/filepath" "path/filepath"
@ -65,6 +66,10 @@ func getTerminalMap() (map[uint64]string, error) {
// SendSignal sends a unix.Signal to the process. // SendSignal sends a unix.Signal to the process.
// Currently, SIGSTOP, SIGCONT, SIGTERM and SIGKILL are supported. // Currently, SIGSTOP, SIGCONT, SIGTERM and SIGKILL are supported.
func (p *Process) SendSignal(sig syscall.Signal) error { func (p *Process) SendSignal(sig syscall.Signal) error {
return p.SendSignalWithContext(context.Background(), sig)
}
func (p *Process) SendSignalWithContext(ctx context.Context, sig syscall.Signal) error {
process, err := os.FindProcess(int(p.Pid)) process, err := os.FindProcess(int(p.Pid))
if err != nil { if err != nil {
return err return err
@ -80,26 +85,46 @@ func (p *Process) SendSignal(sig syscall.Signal) error {
// Suspend sends SIGSTOP to the process. // Suspend sends SIGSTOP to the process.
func (p *Process) Suspend() error { func (p *Process) Suspend() error {
return p.SuspendWithContext(context.Background())
}
func (p *Process) SuspendWithContext(ctx context.Context) error {
return p.SendSignal(unix.SIGSTOP) return p.SendSignal(unix.SIGSTOP)
} }
// Resume sends SIGCONT to the process. // Resume sends SIGCONT to the process.
func (p *Process) Resume() error { func (p *Process) Resume() error {
return p.ResumeWithContext(context.Background())
}
func (p *Process) ResumeWithContext(ctx context.Context) error {
return p.SendSignal(unix.SIGCONT) return p.SendSignal(unix.SIGCONT)
} }
// Terminate sends SIGTERM to the process. // Terminate sends SIGTERM to the process.
func (p *Process) Terminate() error { func (p *Process) Terminate() error {
return p.TerminateWithContext(context.Background())
}
func (p *Process) TerminateWithContext(ctx context.Context) error {
return p.SendSignal(unix.SIGTERM) return p.SendSignal(unix.SIGTERM)
} }
// Kill sends SIGKILL to the process. // Kill sends SIGKILL to the process.
func (p *Process) Kill() error { func (p *Process) Kill() error {
return p.KillWithContext(context.Background())
}
func (p *Process) KillWithContext(ctx context.Context) error {
return p.SendSignal(unix.SIGKILL) return p.SendSignal(unix.SIGKILL)
} }
// Username returns a username of the process. // Username returns a username of the process.
func (p *Process) Username() (string, error) { func (p *Process) Username() (string, error) {
return p.UsernameWithContext(context.Background())
}
func (p *Process) UsernameWithContext(ctx context.Context) (string, error) {
uids, err := p.Uids() uids, err := p.Uids()
if err != nil { if err != nil {
return "", err return "", err

View File

@ -95,6 +95,10 @@ func init() {
} }
func Pids() ([]int32, error) { func Pids() ([]int32, error) {
return PidsWithContext(context.Background())
}
func PidsWithContext(ctx context.Context) ([]int32, error) {
// inspired by https://gist.github.com/henkman/3083408 // inspired by https://gist.github.com/henkman/3083408
// and https://github.com/giampaolo/psutil/blob/1c3a15f637521ba5c0031283da39c733fda53e4c/psutil/arch/windows/process_info.c#L315-L329 // and https://github.com/giampaolo/psutil/blob/1c3a15f637521ba5c0031283da39c733fda53e4c/psutil/arch/windows/process_info.c#L315-L329
var ret []int32 var ret []int32
@ -121,6 +125,10 @@ func Pids() ([]int32, error) {
} }
func (p *Process) Ppid() (int32, error) { func (p *Process) Ppid() (int32, error) {
return p.PpidWithContext(context.Background())
}
func (p *Process) PpidWithContext(ctx context.Context) (int32, error) {
ppid, _, _, err := getFromSnapProcess(p.Pid) ppid, _, _, err := getFromSnapProcess(p.Pid)
if err != nil { if err != nil {
return 0, err return 0, err
@ -129,6 +137,10 @@ func (p *Process) Ppid() (int32, error) {
} }
func GetWin32Proc(pid int32) ([]Win32_Process, error) { func GetWin32Proc(pid int32) ([]Win32_Process, error) {
return GetWin32ProcWithContext(context.Background(), pid)
}
func GetWin32ProcWithContext(ctx context.Context, pid int32) ([]Win32_Process, error) {
var dst []Win32_Process var dst []Win32_Process
query := fmt.Sprintf("WHERE ProcessId = %d", pid) query := fmt.Sprintf("WHERE ProcessId = %d", pid)
q := wmi.CreateQuery(&dst, query) q := wmi.CreateQuery(&dst, query)
@ -147,6 +159,10 @@ func GetWin32Proc(pid int32) ([]Win32_Process, error) {
} }
func (p *Process) Name() (string, error) { func (p *Process) Name() (string, error) {
return p.NameWithContext(context.Background())
}
func (p *Process) NameWithContext(ctx context.Context) (string, error) {
_, _, name, err := getFromSnapProcess(p.Pid) _, _, name, err := getFromSnapProcess(p.Pid)
if err != nil { if err != nil {
return "", fmt.Errorf("could not get Name: %s", err) return "", fmt.Errorf("could not get Name: %s", err)
@ -154,7 +170,15 @@ func (p *Process) Name() (string, error) {
return name, nil return name, nil
} }
func (p *Process) Tgid() (int32, error) {
return 0, common.ErrNotImplementedError
}
func (p *Process) Exe() (string, error) { func (p *Process) Exe() (string, error) {
return p.ExeWithContext(context.Background())
}
func (p *Process) ExeWithContext(ctx context.Context) (string, error) {
dst, err := GetWin32Proc(p.Pid) dst, err := GetWin32Proc(p.Pid)
if err != nil { if err != nil {
return "", fmt.Errorf("could not get ExecutablePath: %s", err) return "", fmt.Errorf("could not get ExecutablePath: %s", err)
@ -163,6 +187,10 @@ func (p *Process) Exe() (string, error) {
} }
func (p *Process) Cmdline() (string, error) { func (p *Process) Cmdline() (string, error) {
return p.CmdlineWithContext(context.Background())
}
func (p *Process) CmdlineWithContext(ctx context.Context) (string, error) {
dst, err := GetWin32Proc(p.Pid) dst, err := GetWin32Proc(p.Pid)
if err != nil { if err != nil {
return "", fmt.Errorf("could not get CommandLine: %s", err) return "", fmt.Errorf("could not get CommandLine: %s", err)
@ -174,6 +202,10 @@ func (p *Process) Cmdline() (string, error) {
// element being an argument. This merely returns the CommandLine informations passed // element being an argument. This merely returns the CommandLine informations passed
// to the process split on the 0x20 ASCII character. // to the process split on the 0x20 ASCII character.
func (p *Process) CmdlineSlice() ([]string, error) { func (p *Process) CmdlineSlice() ([]string, error) {
return p.CmdlineSliceWithContext(context.Background())
}
func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error) {
cmdline, err := p.Cmdline() cmdline, err := p.Cmdline()
if err != nil { if err != nil {
return nil, err return nil, err
@ -182,6 +214,10 @@ func (p *Process) CmdlineSlice() ([]string, error) {
} }
func (p *Process) CreateTime() (int64, error) { func (p *Process) CreateTime() (int64, error) {
return p.CreateTimeWithContext(context.Background())
}
func (p *Process) CreateTimeWithContext(ctx context.Context) (int64, error) {
ru, err := getRusage(p.Pid) ru, err := getRusage(p.Pid)
if err != nil { if err != nil {
return 0, fmt.Errorf("could not get CreationDate: %s", err) return 0, fmt.Errorf("could not get CreationDate: %s", err)
@ -191,9 +227,17 @@ func (p *Process) CreateTime() (int64, error) {
} }
func (p *Process) Cwd() (string, error) { func (p *Process) Cwd() (string, error) {
return p.CwdWithContext(context.Background())
}
func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError return "", common.ErrNotImplementedError
} }
func (p *Process) Parent() (*Process, error) { func (p *Process) Parent() (*Process, error) {
return p.ParentWithContext(context.Background())
}
func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
dst, err := GetWin32Proc(p.Pid) dst, err := GetWin32Proc(p.Pid)
if err != nil { if err != nil {
return nil, fmt.Errorf("could not get ParentProcessID: %s", err) return nil, fmt.Errorf("could not get ParentProcessID: %s", err)
@ -202,9 +246,17 @@ func (p *Process) Parent() (*Process, error) {
return NewProcess(int32(dst[0].ParentProcessID)) return NewProcess(int32(dst[0].ParentProcessID))
} }
func (p *Process) Status() (string, error) { func (p *Process) Status() (string, error) {
return p.StatusWithContext(context.Background())
}
func (p *Process) StatusWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError return "", common.ErrNotImplementedError
} }
func (p *Process) Username() (string, error) { func (p *Process) Username() (string, error) {
return p.UsernameWithContext(context.Background())
}
func (p *Process) UsernameWithContext(ctx context.Context) (string, error) {
pid := p.Pid pid := p.Pid
// 0x1000 is PROCESS_QUERY_LIMITED_INFORMATION // 0x1000 is PROCESS_QUERY_LIMITED_INFORMATION
c, err := syscall.OpenProcess(0x1000, false, uint32(pid)) c, err := syscall.OpenProcess(0x1000, false, uint32(pid))
@ -226,20 +278,36 @@ func (p *Process) Username() (string, error) {
} }
func (p *Process) Uids() ([]int32, error) { func (p *Process) Uids() ([]int32, error) {
return p.UidsWithContext(context.Background())
}
func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) {
var uids []int32 var uids []int32
return uids, common.ErrNotImplementedError return uids, common.ErrNotImplementedError
} }
func (p *Process) Gids() ([]int32, error) { func (p *Process) Gids() ([]int32, error) {
return p.GidsWithContext(context.Background())
}
func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) {
var gids []int32 var gids []int32
return gids, common.ErrNotImplementedError return gids, common.ErrNotImplementedError
} }
func (p *Process) Terminal() (string, error) { func (p *Process) Terminal() (string, error) {
return p.TerminalWithContext(context.Background())
}
func (p *Process) TerminalWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError return "", common.ErrNotImplementedError
} }
// Nice returnes priority in Windows // Nice returnes priority in Windows
func (p *Process) Nice() (int32, error) { func (p *Process) Nice() (int32, error) {
return p.NiceWithContext(context.Background())
}
func (p *Process) NiceWithContext(ctx context.Context) (int32, error) {
dst, err := GetWin32Proc(p.Pid) dst, err := GetWin32Proc(p.Pid)
if err != nil { if err != nil {
return 0, fmt.Errorf("could not get Priority: %s", err) return 0, fmt.Errorf("could not get Priority: %s", err)
@ -247,20 +315,36 @@ func (p *Process) Nice() (int32, error) {
return int32(dst[0].Priority), nil return int32(dst[0].Priority), nil
} }
func (p *Process) IOnice() (int32, error) { func (p *Process) IOnice() (int32, error) {
return p.IOniceWithContext(context.Background())
}
func (p *Process) IOniceWithContext(ctx context.Context) (int32, error) {
return 0, common.ErrNotImplementedError return 0, common.ErrNotImplementedError
} }
func (p *Process) Rlimit() ([]RlimitStat, error) { func (p *Process) Rlimit() ([]RlimitStat, error) {
return p.RlimitWithContext(context.Background())
}
func (p *Process) RlimitWithContext(ctx context.Context) ([]RlimitStat, error) {
var rlimit []RlimitStat var rlimit []RlimitStat
return rlimit, common.ErrNotImplementedError return rlimit, common.ErrNotImplementedError
} }
func (p *Process) RlimitUsage(_ bool) ([]RlimitStat, error) { 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) {
var rlimit []RlimitStat var rlimit []RlimitStat
return rlimit, common.ErrNotImplementedError return rlimit, common.ErrNotImplementedError
} }
func (p *Process) IOCounters() (*IOCountersStat, error) { func (p *Process) IOCounters() (*IOCountersStat, error) {
return p.IOCountersWithContext(context.Background())
}
func (p *Process) IOCountersWithContext(ctx context.Context) (*IOCountersStat, error) {
dst, err := GetWin32Proc(p.Pid) dst, err := GetWin32Proc(p.Pid)
if err != nil || len(dst) == 0 { if err != nil || len(dst) == 0 {
return nil, fmt.Errorf("could not get Win32Proc: %s", err) return nil, fmt.Errorf("could not get Win32Proc: %s", err)
@ -275,12 +359,24 @@ func (p *Process) IOCounters() (*IOCountersStat, error) {
return ret, nil return ret, nil
} }
func (p *Process) NumCtxSwitches() (*NumCtxSwitchesStat, error) { func (p *Process) NumCtxSwitches() (*NumCtxSwitchesStat, error) {
return p.NumCtxSwitchesWithContext(context.Background())
}
func (p *Process) NumCtxSwitchesWithContext(ctx context.Context) (*NumCtxSwitchesStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) NumFDs() (int32, error) { func (p *Process) NumFDs() (int32, error) {
return p.NumFDsWithContext(context.Background())
}
func (p *Process) NumFDsWithContext(ctx context.Context) (int32, error) {
return 0, common.ErrNotImplementedError return 0, common.ErrNotImplementedError
} }
func (p *Process) NumThreads() (int32, error) { func (p *Process) NumThreads() (int32, error) {
return p.NumThreadsWithContext(context.Background())
}
func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) {
dst, err := GetWin32Proc(p.Pid) dst, err := GetWin32Proc(p.Pid)
if err != nil { if err != nil {
return 0, fmt.Errorf("could not get ThreadCount: %s", err) return 0, fmt.Errorf("could not get ThreadCount: %s", err)
@ -288,10 +384,18 @@ func (p *Process) NumThreads() (int32, error) {
return int32(dst[0].ThreadCount), nil return int32(dst[0].ThreadCount), nil
} }
func (p *Process) Threads() (map[int32]*cpu.TimesStat, error) { 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) ret := make(map[int32]*cpu.TimesStat)
return ret, common.ErrNotImplementedError return ret, common.ErrNotImplementedError
} }
func (p *Process) Times() (*cpu.TimesStat, error) { func (p *Process) Times() (*cpu.TimesStat, error) {
return p.TimesWithContext(context.Background())
}
func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) {
sysTimes, err := getProcessCPUTimes(p.Pid) sysTimes, err := getProcessCPUTimes(p.Pid)
if err != nil { if err != nil {
return nil, err return nil, err
@ -315,9 +419,17 @@ func (p *Process) Times() (*cpu.TimesStat, error) {
}, nil }, nil
} }
func (p *Process) CPUAffinity() ([]int32, error) { func (p *Process) CPUAffinity() ([]int32, error) {
return p.CPUAffinityWithContext(context.Background())
}
func (p *Process) CPUAffinityWithContext(ctx context.Context) ([]int32, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) MemoryInfo() (*MemoryInfoStat, error) { func (p *Process) MemoryInfo() (*MemoryInfoStat, error) {
return p.MemoryInfoWithContext(context.Background())
}
func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, error) {
mem, err := getMemoryInfo(p.Pid) mem, err := getMemoryInfo(p.Pid)
if err != nil { if err != nil {
return nil, err return nil, err
@ -331,10 +443,18 @@ func (p *Process) MemoryInfo() (*MemoryInfoStat, error) {
return ret, nil return ret, nil
} }
func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) { func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) {
return p.MemoryInfoExWithContext(context.Background())
}
func (p *Process) MemoryInfoExWithContext(ctx context.Context) (*MemoryInfoExStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) Children() ([]*Process, error) { func (p *Process) Children() ([]*Process, error) {
return p.ChildrenWithContext(context.Background())
}
func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) {
var dst []Win32_Process var dst []Win32_Process
query := wmi.CreateQuery(&dst, fmt.Sprintf("Where ParentProcessId = %d", p.Pid)) query := wmi.CreateQuery(&dst, fmt.Sprintf("Where ParentProcessId = %d", p.Pid))
ctx, cancel := context.WithTimeout(context.Background(), common.Timeout) ctx, cancel := context.WithTimeout(context.Background(), common.Timeout)
@ -357,22 +477,42 @@ func (p *Process) Children() ([]*Process, error) {
} }
func (p *Process) OpenFiles() ([]OpenFilesStat, error) { func (p *Process) OpenFiles() ([]OpenFilesStat, error) {
return p.OpenFilesWithContext(context.Background())
}
func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) Connections() ([]net.ConnectionStat, error) { func (p *Process) Connections() ([]net.ConnectionStat, error) {
return p.ConnectionsWithContext(context.Background())
}
func (p *Process) ConnectionsWithContext(ctx context.Context) ([]net.ConnectionStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) NetIOCounters(pernic bool) ([]net.IOCountersStat, error) { func (p *Process) NetIOCounters(pernic bool) ([]net.IOCountersStat, error) {
return p.NetIOCountersWithContext(context.Background(), pernic)
}
func (p *Process) NetIOCountersWithContext(ctx context.Context, pernic bool) ([]net.IOCountersStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) IsRunning() (bool, error) { func (p *Process) IsRunning() (bool, error) {
return p.IsRunningWithContext(context.Background())
}
func (p *Process) IsRunningWithContext(ctx context.Context) (bool, error) {
return true, common.ErrNotImplementedError return true, common.ErrNotImplementedError
} }
func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) { func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) {
return p.MemoryMapsWithContext(context.Background(), grouped)
}
func (p *Process) MemoryMapsWithContext(ctx context.Context, grouped bool) (*[]MemoryMapsStat, error) {
var ret []MemoryMapsStat var ret []MemoryMapsStat
return &ret, common.ErrNotImplementedError return &ret, common.ErrNotImplementedError
} }
@ -384,17 +524,33 @@ func NewProcess(pid int32) (*Process, error) {
} }
func (p *Process) SendSignal(sig windows.Signal) error { func (p *Process) SendSignal(sig windows.Signal) error {
return p.SendSignalWithContext(context.Background(), sig)
}
func (p *Process) SendSignalWithContext(ctx context.Context, sig windows.Signal) error {
return common.ErrNotImplementedError return common.ErrNotImplementedError
} }
func (p *Process) Suspend() error { func (p *Process) Suspend() error {
return p.SuspendWithContext(context.Background())
}
func (p *Process) SuspendWithContext(ctx context.Context) error {
return common.ErrNotImplementedError return common.ErrNotImplementedError
} }
func (p *Process) Resume() error { func (p *Process) Resume() error {
return p.ResumeWithContext(context.Background())
}
func (p *Process) ResumeWithContext(ctx context.Context) error {
return common.ErrNotImplementedError return common.ErrNotImplementedError
} }
func (p *Process) Terminate() error { func (p *Process) Terminate() error {
return p.TerminateWithContext(context.Background())
}
func (p *Process) TerminateWithContext(ctx context.Context) error {
// PROCESS_TERMINATE = 0x0001 // PROCESS_TERMINATE = 0x0001
proc := w32.OpenProcess(0x0001, false, uint32(p.Pid)) proc := w32.OpenProcess(0x0001, false, uint32(p.Pid))
ret := w32.TerminateProcess(proc, 0) ret := w32.TerminateProcess(proc, 0)
@ -408,6 +564,10 @@ func (p *Process) Terminate() error {
} }
func (p *Process) Kill() error { func (p *Process) Kill() error {
return p.KillWithContext(context.Background())
}
func (p *Process) KillWithContext(ctx context.Context) error {
process := os.Process{Pid: int(p.Pid)} process := os.Process{Pid: int(p.Pid)}
return process.Kill() return process.Kill()
} }
@ -440,6 +600,10 @@ func getFromSnapProcess(pid int32) (int32, int32, string, error) {
// Get processes // Get processes
func Processes() ([]*Process, error) { func Processes() ([]*Process, error) {
return ProcessesWithContext(context.Background())
}
func ProcessesWithContext(ctx context.Context) ([]*Process, error) {
pids, err := Pids() pids, err := Pids()
if err != nil { if err != nil {
return []*Process{}, fmt.Errorf("could not get Processes %s", err) return []*Process{}, fmt.Errorf("could not get Processes %s", err)

10
vendor/vendor.json vendored
View File

@ -59,8 +59,8 @@
{ {
"checksumSHA1": "jWpwWWcywJPNhKTYxi4RXds+amQ=", "checksumSHA1": "jWpwWWcywJPNhKTYxi4RXds+amQ=",
"path": "github.com/shirou/gopsutil/internal/common", "path": "github.com/shirou/gopsutil/internal/common",
"revision": "27389f01ec9364f60d6dba4fbe9751d3b7058d46", "revision": "6a368fb7cd1221fa6ea90facc9447c9a2234c255",
"revisionTime": "2017-12-14T06:29:47Z" "revisionTime": "2018-01-11T02:47:13Z"
}, },
{ {
"checksumSHA1": "UuwHornIODuEq7fYnsZwdVgERLk=", "checksumSHA1": "UuwHornIODuEq7fYnsZwdVgERLk=",
@ -75,10 +75,10 @@
"revisionTime": "2017-11-12T16:40:41Z" "revisionTime": "2017-11-12T16:40:41Z"
}, },
{ {
"checksumSHA1": "DRj2E2ZXWdb8UtaGLIDKa64IRHk=", "checksumSHA1": "a5m6WLMC9Y0N96wxwM1n4JoIIyg=",
"path": "github.com/shirou/gopsutil/process", "path": "github.com/shirou/gopsutil/process",
"revision": "27389f01ec9364f60d6dba4fbe9751d3b7058d46", "revision": "6a368fb7cd1221fa6ea90facc9447c9a2234c255",
"revisionTime": "2017-12-14T06:29:47Z" "revisionTime": "2018-01-11T02:47:13Z"
}, },
{ {
"checksumSHA1": "Nve7SpDmjsv6+rhkXAkfg/UQx94=", "checksumSHA1": "Nve7SpDmjsv6+rhkXAkfg/UQx94=",