Source file
src/os/user/cgo_listgroups_unix.go
1
2
3
4
5
6
7 package user
8
9 import (
10 "fmt"
11 "strconv"
12 "unsafe"
13 )
14
15 const maxGroups = 2048
16
17 func listGroups(u *User) ([]string, error) {
18 ug, err := strconv.Atoi(u.Gid)
19 if err != nil {
20 return nil, fmt.Errorf("user: list groups for %s: invalid gid %q", u.Username, u.Gid)
21 }
22 userGID := _C_gid_t(ug)
23 nameC := make([]byte, len(u.Username)+1)
24 copy(nameC, u.Username)
25
26 n := _C_int(256)
27 gidsC := make([]_C_gid_t, n)
28 rv := getGroupList((*_C_char)(unsafe.Pointer(&nameC[0])), userGID, &gidsC[0], &n)
29 if rv == -1 {
30
31
32 if err := groupRetry(u.Username, nameC, userGID, &gidsC, &n); err != nil {
33 return nil, err
34 }
35 }
36 gidsC = gidsC[:n]
37 gids := make([]string, 0, n)
38 for _, g := range gidsC[:n] {
39 gids = append(gids, strconv.Itoa(int(g)))
40 }
41 return gids, nil
42 }
43
44
45
46 func groupRetry(username string, name []byte, userGID _C_gid_t, gids *[]_C_gid_t, n *_C_int) error {
47
48 if *n > maxGroups {
49 return fmt.Errorf("user: %q is a member of more than %d groups", username, maxGroups)
50 }
51 *gids = make([]_C_gid_t, *n)
52 rv := getGroupList((*_C_char)(unsafe.Pointer(&name[0])), userGID, &(*gids)[0], n)
53 if rv == -1 {
54 return fmt.Errorf("user: list groups for %s failed", username)
55 }
56 return nil
57 }
58
View as plain text