[v3 migration] change process.Status returns []string with no letter status string.

pull/938/head
shirou 4 years ago
parent 4b5a200e71
commit b7e74218ca

@ -41,6 +41,17 @@ type Process struct {
tgid int32 tgid int32
} }
// Process status
const (
Running = "running"
Sleep = "sleep"
Stop = "stop"
Idle = "idle"
Zombie = "zombie"
Wait = "wait"
Lock = "lock"
)
type OpenFilesStat struct { type OpenFilesStat struct {
Path string `json:"path"` Path string `json:"path"`
Fd uint64 `json:"fd"` Fd uint64 `json:"fd"`
@ -375,7 +386,7 @@ func (p *Process) Parent() (*Process, error) {
// R: Running S: Sleep T: Stop I: Idle // R: Running S: Sleep T: Stop I: Idle
// Z: Zombie W: Wait L: Lock // Z: Zombie W: Wait L: Lock
// The character is same within all supported platforms. // 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()) return p.StatusWithContext(context.Background())
} }
@ -493,7 +504,6 @@ func (p *Process) ConnectionsMax(max int) ([]net.ConnectionStat, error) {
return p.ConnectionsMaxWithContext(context.Background(), max) return p.ConnectionsMaxWithContext(context.Background(), max)
} }
// 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) return p.MemoryMapsWithContext(context.Background(), grouped)
@ -534,3 +544,23 @@ func (p *Process) Username() (string, error) {
return p.UsernameWithContext(context.Background()) 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 ""
}
}

@ -164,13 +164,13 @@ func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
return nil, fmt.Errorf("could not find parent line") 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) r, err := callPsWithContext(ctx, "state", p.Pid, false)
if err != nil { if err != nil {
return "", err return []string{""}, err
} }
status := convertStatusChar(r[0][0][0:1])
return r[0][0][0:1], err return []string{status}, err
} }
func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) { 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 return ret, nil
} }

@ -76,8 +76,8 @@ func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) StatusWithContext(ctx context.Context) (string, error) { func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
return "", common.ErrNotImplementedError return []string{""}, common.ErrNotImplementedError
} }
func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) { 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) { func (p *Process) UsernameWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError return "", common.ErrNotImplementedError
} }

@ -113,30 +113,30 @@ func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
return nil, common.ErrNotImplementedError 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() k, err := p.getKProc()
if err != nil { if err != nil {
return "", err return []string{""}, err
} }
var s string var s string
switch k.Stat { switch k.Stat {
case SIDL: case SIDL:
s = "I" s = Idle
case SRUN: case SRUN:
s = "R" s = Running
case SSLEEP: case SSLEEP:
s = "S" s = Sleep
case SSTOP: case SSTOP:
s = "T" s = Stop
case SZOMB: case SZOMB:
s = "Z" s = Zombie
case SWAIT: case SWAIT:
s = "W" s = Wait
case SLOCK: case SLOCK:
s = "L" s = Lock
} }
return s, nil return []string{s}, nil
} }
func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) { func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) {
@ -336,4 +336,3 @@ func (p *Process) getKProc() (*KinfoProc, error) {
} }
return &k, nil return &k, nil
} }

@ -125,12 +125,12 @@ func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
return NewProcessWithContext(ctx, p.parent) 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) err := p.fillFromStatusWithContext(ctx)
if err != nil { 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) { func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) {
@ -814,7 +814,7 @@ func (p *Process) fillFromStatusWithContext(ctx context.Context) error {
} }
} }
case "State": case "State":
p.status = value[0:1] p.status = convertStatusChar(value[0:1])
case "PPid", "Ppid": case "PPid", "Ppid":
pval, err := strconv.ParseInt(value, 10, 32) pval, err := strconv.ParseInt(value, 10, 32)
if err != nil { if err != nil {
@ -1114,4 +1114,3 @@ func readPidsFromDir(path string) ([]int32, error) {
return ret, nil return ret, nil
} }

@ -109,26 +109,26 @@ func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
return nil, common.ErrNotImplementedError 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() k, err := p.getKProc()
if err != nil { if err != nil {
return "", err return []string{""}, err
} }
var s string var s string
switch k.Stat { switch k.Stat {
case SIDL: case SIDL:
case SRUN: case SRUN:
case SONPROC: case SONPROC:
s = "R" s = Running
case SSLEEP: case SSLEEP:
s = "S" s = Sleep
case SSTOP: case SSTOP:
s = "T" s = Stop
case SDEAD: case SDEAD:
s = "Z" s = Zombie
} }
return s, nil return []string{s}, nil
} }
func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) { 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 return buf, length, nil
} }

@ -195,8 +195,11 @@ func Test_Process_Status(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("getting status error %v", err) t.Errorf("getting status error %v", err)
} }
if v != "R" && v != "S" { if len(v) == 0 {
t.Errorf("could not get state %v", v) 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() p.Ppid()
} }
} }

@ -318,8 +318,8 @@ func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
return NewProcessWithContext(ctx, ppid) return NewProcessWithContext(ctx, ppid)
} }
func (p *Process) StatusWithContext(ctx context.Context) (string, error) { func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
return "", common.ErrNotImplementedError return []string{""}, common.ErrNotImplementedError
} }
func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) { func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) {
@ -849,4 +849,3 @@ func convertUTF16ToString(src []byte) string {
} }
return syscall.UTF16ToString(codePoints) return syscall.UTF16ToString(codePoints)
} }

Loading…
Cancel
Save