Calling the new process.Groups() fails on macOS
Describe the bug
Calling the new method process.Groups() fails on macOS Catalina (10.15.6).
Instead I get: runtime error: index out of range [16] with length 16
To Reproduce
p, err := process.NewProcess(pID)
if err != nil {
panic(err)
}
sgs, err := p.Groups()
if err != nil {
panic(err)
}
Expected behavior To get a list of supplementary groups.
Environment (please complete the following information):
- [ ] Windows: [paste the result of
ver] - [ ] Linux: [paste contents of
/etc/os-releaseand the result ofuname -a] - [x] Mac OS: [ ProductName: Mac OS X ProductVersion: 10.15.6 BuildVersion: 19G73 Darwin mbp.local 19.6.0 Darwin Kernel Version 19.6.0: Sun Jul 5 00:43:10 PDT 2020; root:xnu-6153.141.1~9/RELEASE_X86_64 x86_64 ]
- [ ] FreeBSD: [paste the result of
freebsd-version -k -r -uanduname -a] - [ ] OpenBSD: [paste the result of
uname -a]
Additional context
The problem seems to be the usage of k.Eproc.Ucred.Ngroups as the end of the iteration instead of the length of the array being iterated (k.Eproc.Ucred.Groups).
https://github.com/shirou/gopsutil/blob/3202231bcdbd3a6d236dbcc3ec92a96ddfa7168f/process/process_darwin.go#L220-L223
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])
}
I noticed that k.Eproc.Ucred.Ngroups is also being use by Gids() as the effective group.
This "dual meaning" confuses me a little, specially if it is based on this: https://unix.superglobalmegacorp.com/Net2/newsrc/sys/ucred.h.html
Leaving that aside, if iterating k.Eproc.Ucred.Groups directly is the proper fix I'm happy to submit a PR.
Yes I could confirm that running tests on darwin, this was obviously never tested in #775 on this platform as it fails systematically (openbsd is probably not affected byt freebsd yes surely, I have some issue on my VM though). Yet another argument for running tests on all platforms automatically (#734).
I'm not exactly sure the results are right with this diff
diff --git a/process/process_darwin.go b/process/process_darwin.go
index 1a0ccfb..1f32ac1 100644
--- a/process/process_darwin.go
+++ b/process/process_darwin.go
@@ -274,8 +274,9 @@ func (p *Process) GroupsWithContext(ctx context.Context) ([]int32, error) {
return nil, err
}
- groups := make([]int32, k.Eproc.Ucred.Ngroups)
- for i := int16(0); i < k.Eproc.Ucred.Ngroups; i++ {
+ groupsLen := len(k.Eproc.Ucred.Groups)
+ groups := make([]int32, groupsLen)
+ for i := 0; i < groupsLen; i++ {
groups[i] = int32(k.Eproc.Ucred.Groups[i])
}
With this sample program
package main
import (
"fmt"
"github.com/shirou/gopsutil/process"
)
func main() {
procs, err := process.Processes()
if err != nil {
panic(err)
}
for _, p := range procs {
groups, err := p.Groups()
if err != nil {
fmt.Println("pid", p.Pid, "err", err)
continue
}
fmt.Println("pid", p.Pid, "groups", groups)
}
}
Returning so many groups as 0
pid 1 groups [0 0 0 1151 0 1 0 0 0 0 0 0 0 0 0 0]
pid 33 groups [0 0 0 55 0 1 0 0 0 0 0 0 0 0 0 0]
pid 34 groups [0 0 0 262 0 1 0 0 0 0 0 0 0 0 0 0]
pid 36 groups [0 0 0 8 0 1 0 0 0 0 0 0 0 0 0 0]
pid 37 groups [0 0 0 329 0 1 0 0 0 0 0 0 0 0 0 0]
pid 38 groups [0 0 0 54 0 1 0 0 0 0 0 0 0 0 0 0]
pid 40 groups [0 0 0 12 0 1 0 0 0 0 0 0 0 0 0 0]
pid 43 groups [55 0 0 6 55 1 55 0 0 0 0 0 0 0 0 0]
pid 44 groups [0 0 0 116 0 1 0 0 0 0 0 0 0 0 0 0]
pid 45 groups [0 0 0 10 0 1 0 0 0 0 0 0 0 0 0 0]
pid 46 groups [0 0 0 15 0 1 0 0 0 0 0 0 0 0 0 0]
pid 47 groups [0 0 0 6 0 1 0 0 0 0 0 0 0 0 0 0]
pid 50 groups [0 0 0 162 0 1 0 0 0 0 0 0 0 0 0 0]
pid 54 groups [0 0 0 19 0 1 0 0 0 0 0 0 0 0 0 0]
pid 55 groups [0 0 0 11 0 1 0 0 0 0 0 0 0 0 0 0]
pid 57 groups [0 0 0 844 0 1 0 0 0 0 0 0 0 0 0 0]
pid 60 groups [0 0 0 6 0 1 0 0 0 0 0 0 0 0 0 0]
pid 62 groups [240 0 0 8 240 1 240 0 0 0 0 0 0 0 0 0]
pid 63 groups [0 0 0 8 0 1 0 0 0 0 0 0 0 0 0 0]
pid 65 groups [0 0 0 58 0 1 0 0 0 0 0 0 0 0 0 0]
pid 67 groups [0 0 0 86 0 1 0 0 0 0 0 0 0 0 0 0]
pid 68 groups [0 0 0 5 0 1 0 0 0 0 0 0 0 0 0 0]
pid 71 groups [0 0 0 6 0 1 0 0 0 0 0 0 0 0 0 0]
pid 73 groups [0 0 0 279 0 1 0 0 0 0 0 0 0 0 0 0]
pid 74 groups [0 0 0 6 0 1 0 0 0 0 0 0 0 0 0 0]
pid 76 groups [0 0 0 20 0 1 0 0 0 0 0 0 0 0 0 0]
pid 77 groups [0 0 0 5 0 1 0 0 0 0 0 0 0 0 0 0]
pid 78 groups [0 0 0 5 0 1 0 0 0 0 0 0 0 0 0 0]
pid 79 groups [213 0 0 59 213 1 213 0 0 0 0 0 0 0 0 0]
pid 80 groups [0 0 0 82 0 1 0 0 0 0 0 0 0 0 0 0]
pid 82 groups [205 0 0 75 205 2 205 207 0 0 0 0 0 0 0 0]
pid 84 groups [0 0 0 32 0 1 0 0 0 0 0 0 0 0 0 0]
pid 86 groups [0 0 0 14 0 1 0 0 0 0 0 0 0 0 0 0]
pid 87 groups [0 0 0 6 0 1 0 0 0 0 0 0 0 0 0 0]
pid 90 groups [0 0 0 23 0 1 0 0 0 0 0 0 0 0 0 0]
pid 91 groups [0 0 0 72 0 1 0 0 0 0 0 0 0 0 0 0]
pid 92 groups [0 0 0 6 0 1 0 0 0 0 0 0 0 0 0 0]
pid 93 groups [0 0 0 11 0 1 0 0 0 0 0 0 0 0 0 0]
pid 95 groups [261 0 0 57 261 1 261 0 0 0 0 0 0 0 0 0]
pid 96 groups [0 0 0 12 0 1 0 0 0 0 0 0 0 0 0 0]
pid 97 groups [0 0 0 77 0 1 0 0 0 0 0 0 0 0 0 0]
pid 98 groups [241 0 0 6 241 1 241 0 0 0 0 0 0 0 0 0]
pid 99 groups [0 0 0 797 0 1 0 0 0 0 0 0 0 0 0 0]
pid 100 groups [0 0 0 45 0 1 0 0 0 0 0 0 0 0 0 0]
pid 104 groups [0 0 0 37 0 1 0 0 0 0 0 0 0 0 0 0]
pid 105 groups [0 0 0 6 0 1 0 0 0 0 0 0 0 0 0 0]
pid 115 groups [0 0 0 10 0 1 0 0 0 0 0 0 0 0 0 0]
pid 116 groups [0 0 0 40 0 1 0 0 0 0 0 0 0 0 0 0]
pid 117 groups [259 0 0 6 259 1 259 0 0 0 0 0 0 0 0 0]
pid 118 groups [0 0 0 12 0 1 0 0 0 0 0 0 0 0 0 0]
pid 122 groups [0 0 0 111 88 1 88 0 0 0 0 0 0 0 0 0]
pid 134 groups [0 0 0 8 0 1 0 0 0 0 0 0 0 0 0 0]
pid 135 groups [0 0 0 45 0 1 0 0 0 0 0 0 0 0 0 0]
pid 136 groups [0 0 0 19 0 1 0 0 0 0 0 0 0 0 0 0]
pid 137 groups [0 0 0 173 0 1 0 0 0 0 0 0 0 0 0 0]
pid 138 groups [65 0 0 45 65 1 65 0 0 0 0 0 0 0 0 0]
pid 139 groups [0 0 0 9 0 1 0 0 0 0 0 0 0 0 0 0]
pid 140 groups [202 0 0 51 202 1 202 0 0 0 0 0 0 0 0 0]
pid 145 groups [202 0 0 35 202 5 202 12 61 701 100 0 0 0 0 0]
pid 148 groups [0 0 0 6 0 1 0 0 0 0 0 0 0 0 0 0]
pid 149 groups [0 0 0 6 0 1 0 0 0 0 0 0 0 0 0 0]
pid 150 groups [242 0 0 63 242 1 242 0 0 0 0 0 0 0 0 0]
pid 151 groups [0 0 0 40 0 1 0 0 0 0 0 0 0 0 0 0]
pid 152 groups [0 0 0 13 0 1 0 0 0 0 0 0 0 0 0 0]
pid 160 groups [24 0 0 57 24 1 24 0 0 0 0 0 0 0 0 0]
pid 161 groups [0 0 0 9 0 1 0 0 0 0 0 0 0 0 0 0]
pid 171 groups [0 0 0 22 0 1 0 0 0 0 0 0 0 0 0 0]
pid 172 groups [0 0 0 20 0 1 0 0 0 0 0 0 0 0 0 0]
pid 173 groups [0 0 0 7 0 1 0 0 0 0 0 0 0 0 0 0]
pid 174 groups [0 0 0 12 0 1 0 0 0 0 0 0 0 0 0 0]
pid 176 groups [0 0 0 10 0 1 0 0 0 0 0 0 0 0 0 0]
pid 177 groups [0 0 0 9 0 1 0 0 0 0 0 0 0 0 0 0]
pid 178 groups [243 0 0 8 243 1 243 0 0 0 0 0 0 0 0 0]
pid 179 groups [200 0 0 5 200 1 200 0 0 0 0 0 0 0 0 0]
pid 180 groups [0 0 0 6 0 1 0 0 0 0 0 0 0 0 0 0]
pid 182 groups [222 0 0 7 222 1 222 0 0 0 0 0 0 0 0 0]
pid 185 groups [0 0 0 20 0 1 0 0 0 0 0 0 0 0 0 0]
pid 186 groups [0 0 0 7 0 1 0 0 0 0 0 0 0 0 0 0]
pid 188 groups [0 0 0 9 0 1 0 0 0 0 0 0 0 0 0 0]
pid 191 groups [0 0 0 45 0 16 0 1 2 3 4 5 8 9 12 20]
pid 192 groups [0 0 0 39 0 16 0 1 2 3 4 5 8 9 12 20]
pid 193 groups [0 0 0 66 0 16 0 1 2 3 4 5 8 9 12 20]
pid 195 groups [0 0 0 23 0 16 0 1 2 3 4 5 8 9 12 20]
pid 197 groups [0 0 0 21 0 16 0 1 2 3 4 5 8 9 12 20]
pid 198 groups [0 0 0 155 0 16 0 1 2 3 4 5 8 9 12 20]
pid 200 groups [0 0 0 10 0 1 0 0 0 0 0 0 0 0 0 0]
pid 201 groups [0 0 0 249 0 16 0 1 2 3 4 5 8 9 12 20]
pid 202 groups [92 0 0 520 92 1 92 0 0 0 0 0 0 0 0 0]
pid 204 groups [97 0 0 317 97 1 97 0 0 0 0 0 0 0 0 0]
pid 205 groups [0 0 0 14 0 1 0 0 0 0 0 0 0 0 0 0]
pid 208 groups [0 0 0 10 0 1 0 0 0 0 0 0 0 0 0 0]
pid 209 groups [0 0 0 63 0 16 0 1 2 3 4 5 8 9 12 20]
pid 212 groups [0 0 0 7 0 1 0 0 0 0 0 0 0 0 0 0]
pid 213 groups [0 0 0 19 0 1 0 0 0 0 0 0 0 0 0 0]
pid 215 groups [0 0 0 8 0 1 0 0 0 0 0 0 0 0 0 0]
pid 216 groups [0 0 0 5 0 1 0 0 0 0 0 0 0 0 0 0]
pid 218 groups [0 0 0 14 0 1 0 0 0 0 0 0 0 0 0 0]
pid 219 groups [0 0 0 5 0 1 0 0 0 0 0 0 0 0 0 0]
pid 220 groups [0 0 0 23 0 1 0 0 0 0 0 0 0 0 0 0]
pid 222 groups [0 0 0 10 0 16 0 1 2 3 4 5 8 9 12 20]
pid 223 groups [0 0 0 10 0 16 0 1 2 3 4 5 8 9 12 20]
pid 225 groups [200 0 0 620 200 1 200 0 0 0 0 0 0 0 0 0]
pid 226 groups [0 0 0 321 0 1 0 0 0 0 0 0 0 0 0 0]
pid 228 groups [20 0 0 11 501 14 20 701 12 61 79 80 81 98 33 100]
pid 229 groups [20 0 0 20 501 14 20 701 12 61 79 80 81 98 33 100]
pid 237 groups [0 0 0 18 0 1 0 0 0 0 0 0 0 0 0 0]
pid 239 groups [0 0 0 6 0 1 0 0 0 0 0 0 0 0 0 0]
pid 298 groups [20 0 0 12 501 14 20 701 12 61 79 80 81 98 33 100]
pid 299 groups [20 0 0 11 501 14 20 701 12 61 79 80 81 98 33 100]
pid 300 groups [0 0 0 17 0 1 0 0 0 0 0 0 0 0 0 0]
pid 302 groups [20 0 0 5 501 14 20 701 12 61 79 80 81 98 33 100]
pid 303 groups [89 0 0 17 89 5 89 12 61 701 100 0 0 0 0 0]
pid 304 groups [89 0 0 14 89 5 89 12 61 701 100 0 0 0 0 0]
pid 305 groups [89 0 0 7 89 5 89 12 61 701 100 0 0 0 0 0]
pid 313 groups [20 0 0 2954 501 14 20 701 12 61 79 80 81 98 33 100]
pid 331 groups [20 0 0 2954 501 14 20 701 12 61 79 80 81 98 33 100]