diff --git a/v3/process/process.go b/v3/process/process.go index 6f49321d..03cdfb44 100644 --- a/v3/process/process.go +++ b/v3/process/process.go @@ -41,6 +41,17 @@ type Process struct { tgid int32 } +// Process status +const ( + Running = "running" + Sleep = "sleep" + Stop = "stop" + Idle = "idle" + Zombie = "zombie" + Wait = "wait" + Lock = "lock" +) + type OpenFilesStat struct { Path string `json:"path"` Fd uint64 `json:"fd"` @@ -66,8 +77,8 @@ type SignalInfoStat struct { type RlimitStat struct { Resource int32 `json:"resource"` - Soft uint64 `json:"soft"` - Hard uint64 `json:"hard"` + Soft uint64 `json:"soft"` + Hard uint64 `json:"hard"` Used uint64 `json:"used"` } @@ -375,7 +386,7 @@ func (p *Process) Parent() (*Process, error) { // R: Running S: Sleep T: Stop I: Idle // Z: Zombie W: Wait L: Lock // The character is same within all supported platforms. -func (p *Process) Status() (string, error) { +func (p *Process) Status() ([]string, error) { return p.StatusWithContext(context.Background()) } @@ -493,7 +504,6 @@ func (p *Process) ConnectionsMax(max int) ([]net.ConnectionStat, error) { return p.ConnectionsMaxWithContext(context.Background(), max) } - // MemoryMaps get memory maps from /proc/(pid)/smaps func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) { return p.MemoryMapsWithContext(context.Background(), grouped) @@ -534,3 +544,23 @@ func (p *Process) Username() (string, error) { return p.UsernameWithContext(context.Background()) } +func convertStatusChar(letter string) string { + switch letter { + case "R": + return Running + case "S": + return Sleep + case "T": + return Stop + case "I": + return Idle + case "Z": + return Zombie + case "W": + return Wait + case "L": + return Lock + default: + return "" + } +} diff --git a/v3/process/process_darwin.go b/v3/process/process_darwin.go index 1ddebf17..644d5bc6 100644 --- a/v3/process/process_darwin.go +++ b/v3/process/process_darwin.go @@ -164,13 +164,13 @@ func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { return nil, fmt.Errorf("could not find parent line") } -func (p *Process) StatusWithContext(ctx context.Context) (string, error) { +func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { r, err := callPsWithContext(ctx, "state", p.Pid, false) if err != nil { - return "", err + return []string{""}, err } - - return r[0][0][0:1], err + status := convertStatusChar(r[0][0][0:1]) + return []string{status}, err } func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) { @@ -458,4 +458,3 @@ func callPsWithContext(ctx context.Context, arg string, pid int32, threadOption return ret, nil } - diff --git a/v3/process/process_fallback.go b/v3/process/process_fallback.go index 2a2f4ed2..317148e7 100644 --- a/v3/process/process_fallback.go +++ b/v3/process/process_fallback.go @@ -76,8 +76,8 @@ func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { return nil, common.ErrNotImplementedError } -func (p *Process) StatusWithContext(ctx context.Context) (string, error) { - return "", common.ErrNotImplementedError +func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { + return []string{""}, common.ErrNotImplementedError } func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) { @@ -199,4 +199,3 @@ func (p *Process) KillWithContext(ctx context.Context) error { func (p *Process) UsernameWithContext(ctx context.Context) (string, error) { return "", common.ErrNotImplementedError } - diff --git a/v3/process/process_freebsd.go b/v3/process/process_freebsd.go index 6820d54c..431a673d 100644 --- a/v3/process/process_freebsd.go +++ b/v3/process/process_freebsd.go @@ -113,30 +113,30 @@ func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { return nil, common.ErrNotImplementedError } -func (p *Process) StatusWithContext(ctx context.Context) (string, error) { +func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { k, err := p.getKProc() if err != nil { - return "", err + return []string{""}, err } var s string switch k.Stat { case SIDL: - s = "I" + s = Idle case SRUN: - s = "R" + s = Running case SSLEEP: - s = "S" + s = Sleep case SSTOP: - s = "T" + s = Stop case SZOMB: - s = "Z" + s = Zombie case SWAIT: - s = "W" + s = Wait case SLOCK: - s = "L" + s = Lock } - return s, nil + return []string{s}, nil } func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) { @@ -336,4 +336,3 @@ func (p *Process) getKProc() (*KinfoProc, error) { } return &k, nil } - diff --git a/v3/process/process_linux.go b/v3/process/process_linux.go index 8f41adba..7c1d4ce6 100644 --- a/v3/process/process_linux.go +++ b/v3/process/process_linux.go @@ -125,12 +125,12 @@ func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { return NewProcessWithContext(ctx, p.parent) } -func (p *Process) StatusWithContext(ctx context.Context) (string, error) { +func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { err := p.fillFromStatusWithContext(ctx) if err != nil { - return "", err + return []string{""}, err } - return p.status, nil + return []string{p.status}, nil } func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) { @@ -814,7 +814,7 @@ func (p *Process) fillFromStatusWithContext(ctx context.Context) error { } } case "State": - p.status = value[0:1] + p.status = convertStatusChar(value[0:1]) case "PPid", "Ppid": pval, err := strconv.ParseInt(value, 10, 32) if err != nil { @@ -1114,4 +1114,3 @@ func readPidsFromDir(path string) ([]int32, error) { return ret, nil } - diff --git a/v3/process/process_openbsd.go b/v3/process/process_openbsd.go index bc01b262..6e45d849 100644 --- a/v3/process/process_openbsd.go +++ b/v3/process/process_openbsd.go @@ -109,26 +109,26 @@ func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { return nil, common.ErrNotImplementedError } -func (p *Process) StatusWithContext(ctx context.Context) (string, error) { +func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { k, err := p.getKProc() if err != nil { - return "", err + return []string{""}, err } var s string switch k.Stat { case SIDL: case SRUN: case SONPROC: - s = "R" + s = Running case SSLEEP: - s = "S" + s = Sleep case SSTOP: - s = "T" + s = Stop case SDEAD: - s = "Z" + s = Zombie } - return s, nil + return []string{s}, nil } func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) { @@ -360,4 +360,3 @@ func callKernProcSyscall(op int32, arg int32) ([]byte, uint64, error) { return buf, length, nil } - diff --git a/v3/process/process_test.go b/v3/process/process_test.go index c270d17f..a34f0f02 100644 --- a/v3/process/process_test.go +++ b/v3/process/process_test.go @@ -195,8 +195,11 @@ func Test_Process_Status(t *testing.T) { if err != nil { t.Errorf("getting status error %v", err) } - if v != "R" && v != "S" { - t.Errorf("could not get state %v", v) + if len(v) == 0 { + t.Errorf("could not get state") + } + if v[0] != "R" && v[0] != "S" { + t.Errorf("get wrong state, %v", v) } } @@ -683,4 +686,3 @@ func BenchmarkProcessPpid(b *testing.B) { p.Ppid() } } - diff --git a/v3/process/process_windows.go b/v3/process/process_windows.go index f57a8a82..45e7df81 100644 --- a/v3/process/process_windows.go +++ b/v3/process/process_windows.go @@ -318,8 +318,8 @@ func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { return NewProcessWithContext(ctx, ppid) } -func (p *Process) StatusWithContext(ctx context.Context) (string, error) { - return "", common.ErrNotImplementedError +func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { + return []string{""}, common.ErrNotImplementedError } func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) { @@ -849,4 +849,3 @@ func convertUTF16ToString(src []byte) string { } return syscall.UTF16ToString(codePoints) } -