From f55f202e81c91ce3e55e540252e9797f83cc97e7 Mon Sep 17 00:00:00 2001 From: TakayukiB Goto Date: Mon, 7 Oct 2019 12:59:36 +0900 Subject: [PATCH] [Process] Implement Groups() to get additional gids. --- process/process.go | 1 + process/process_darwin.go | 17 +++++++++++++++++ process/process_fallback.go | 7 +++++++ process/process_freebsd.go | 17 +++++++++++++++++ process/process_linux.go | 23 +++++++++++++++++++++++ process/process_openbsd.go | 12 ++++++++++++ process/process_test.go | 14 ++++++++++++++ process/process_windows.go | 8 ++++++++ 8 files changed, 99 insertions(+) diff --git a/process/process.go b/process/process.go index 87b647b..4148e65 100644 --- a/process/process.go +++ b/process/process.go @@ -28,6 +28,7 @@ type Process struct { numCtxSwitches *NumCtxSwitchesStat uids []int32 gids []int32 + groups []int32 numThreads int32 memInfo *MemoryInfoStat sigInfo *SignalInfoStat diff --git a/process/process_darwin.go b/process/process_darwin.go index 198cce2..24dd8fb 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -250,6 +250,23 @@ func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) { return gids, nil } +func (p *Process) Groups() ([]int32, error) { + return p.GroupsWithContext(context.Background()) +} + +func (p *Process) GroupsWithContext(ctx context.Context) ([]int32, error) { + k, err := p.getKProc() + if err != nil { + return nil, err + } + + groups := make([]int32, k.Eproc.Ucred.Ngroups) + for i := int16(0); i < k.Eproc.Ucred.Ngroups; i++ { + groups[i] = int32(k.Eproc.Ucred.Groups[i]) + } + + return groups, nil +} func (p *Process) Terminal() (string, error) { return p.TerminalWithContext(context.Background()) } diff --git a/process/process_fallback.go b/process/process_fallback.go index 1cb55c8..cf707ed 100644 --- a/process/process_fallback.go +++ b/process/process_fallback.go @@ -139,6 +139,13 @@ func (p *Process) Gids() ([]int32, error) { func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) { return []int32{}, common.ErrNotImplementedError } +func (p *Process) Groups() ([]int32, error) { + return p.GroupsWithContext(context.Background()) +} + +func (p *Process) GroupsWithContext(ctx context.Context) ([]int32, error) { + return []int32{}, common.ErrNotImplementedError +} func (p *Process) Terminal() (string, error) { return p.TerminalWithContext(context.Background()) } diff --git a/process/process_freebsd.go b/process/process_freebsd.go index 3da66b5..18e9de9 100644 --- a/process/process_freebsd.go +++ b/process/process_freebsd.go @@ -213,6 +213,23 @@ func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) { return gids, nil } +func (p *Process) Groups() ([]int32, error) { + return p.GroupsWithContext(context.Background()) +} + +func (p *Process) GroupsWithContext(ctx context.Context) ([]int32, error) { + k, err := p.getKProc() + if err != nil { + return nil, err + } + + groups := make([]int32, k.Ngroups) + for i := int16(0); i < k.Ngroups; i++ { + groups[i] = int32(k.Groups[i]) + } + + return groups, nil +} func (p *Process) Terminal() (string, error) { return p.TerminalWithContext(context.Background()) } diff --git a/process/process_linux.go b/process/process_linux.go index fab7b55..b1f4b49 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -228,6 +228,19 @@ func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) { return p.gids, nil } +// Groups returns groups of the process as a slice of the int +func (p *Process) Groups() ([]int32, error) { + return p.GroupsWithContext(context.Background()) +} + +func (p *Process) GroupsWithContext(ctx context.Context) ([]int32, error) { + err := p.fillFromStatusWithContext(ctx) + if err != nil { + return []int32{}, err + } + return p.groups, nil +} + // Terminal returns a terminal which is associated with the process. func (p *Process) Terminal() (string, error) { return p.TerminalWithContext(context.Background()) @@ -1014,6 +1027,16 @@ func (p *Process) fillFromStatusWithContext(ctx context.Context) error { } p.gids = append(p.gids, int32(v)) } + case "Groups": + groups := strings.Fields(value) + p.groups = make([]int32, 0, len(groups)) + for _, i := range groups { + v, err := strconv.ParseInt(i, 10, 32) + if err != nil { + return err + } + p.groups = append(p.groups, int32(v)) + } case "Threads": v, err := strconv.ParseInt(value, 10, 32) if err != nil { diff --git a/process/process_openbsd.go b/process/process_openbsd.go index 8bac098..e78087a 100644 --- a/process/process_openbsd.go +++ b/process/process_openbsd.go @@ -204,6 +204,18 @@ func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) { return gids, nil } +func (p *Process) Groups() ([]int32, error) { + return p.GroupsWithContext(context.Background()) +} + +func (p *Process) GroupsWithContext(ctx context.Context) ([]int32, error) { + k, err := p.getKProc() + if err != nil { + return nil, err + } + + return k.Groups, nil +} func (p *Process) Terminal() (string, error) { return p.TerminalWithContext(context.Background()) } diff --git a/process/process_test.go b/process/process_test.go index ffa8257..9c003e4 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -242,6 +242,20 @@ func Test_Process_Nice(t *testing.T) { t.Errorf("invalid nice: %d", n) } } + +func Test_Process_Groups(t *testing.T) { + p := testGetProcess() + + v, err := p.Groups() + skipIfNotImplementedErr(t, err) + if err != nil { + t.Errorf("geting groups error %v", err) + } + if len(v) <= 0 || v[0] < 0 { + t.Errorf("invalid Groups: %v", v) + } +} + func Test_Process_NumThread(t *testing.T) { p := testGetProcess() diff --git a/process/process_windows.go b/process/process_windows.go index d42b34f..ff318d0 100644 --- a/process/process_windows.go +++ b/process/process_windows.go @@ -408,6 +408,14 @@ func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) { var gids []int32 return gids, common.ErrNotImplementedError } +func (p *Process) Groups() ([]int32, error) { + return p.GroupsWithContext(context.Background()) +} + +func (p *Process) GroupsWithContext(ctx context.Context) ([]int32, error) { + var groups []int32 + return groups, common.ErrNotImplementedError +} func (p *Process) Terminal() (string, error) { return p.TerminalWithContext(context.Background()) }