github.com/ltltlt/go-source-code@v0.0.0-20190830023027-95be009773aa/os/user/getgrouplist_darwin.go (about) 1 // Copyright 2016 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package user 6 7 /* 8 #include <unistd.h> 9 #include <sys/types.h> 10 #include <stdlib.h> 11 12 static int mygetgrouplist(const char* user, gid_t group, gid_t* groups, int* ngroups) { 13 int* buf = malloc(*ngroups * sizeof(int)); 14 int rv = getgrouplist(user, (int) group, buf, ngroups); 15 int i; 16 if (rv == 0) { 17 for (i = 0; i < *ngroups; i++) { 18 groups[i] = (gid_t) buf[i]; 19 } 20 } 21 free(buf); 22 return rv; 23 } 24 */ 25 import "C" 26 import ( 27 "fmt" 28 "unsafe" 29 ) 30 31 func getGroupList(name *C.char, userGID C.gid_t, gids *C.gid_t, n *C.int) C.int { 32 return C.mygetgrouplist(name, userGID, gids, n) 33 } 34 35 // groupRetry retries getGroupList with an increasingly large size for n. The 36 // result is stored in gids. 37 func groupRetry(username string, name []byte, userGID C.gid_t, gids *[]C.gid_t, n *C.int) error { 38 *n = C.int(256 * 2) 39 for { 40 *gids = make([]C.gid_t, *n) 41 rv := getGroupList((*C.char)(unsafe.Pointer(&name[0])), userGID, &(*gids)[0], n) 42 if rv >= 0 { 43 // n is set correctly 44 break 45 } 46 if *n > maxGroups { 47 return fmt.Errorf("user: %q is a member of more than %d groups", username, maxGroups) 48 } 49 *n = *n * C.int(2) 50 } 51 return nil 52 }