...

Source file src/pkg/os/user/getgrouplist_unix.go

     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	// +build dragonfly freebsd !android,linux netbsd openbsd
     6	// +build cgo,!osusergo
     7	
     8	package user
     9	
    10	/*
    11	#include <unistd.h>
    12	#include <sys/types.h>
    13	#include <grp.h>
    14	
    15	static int mygetgrouplist(const char* user, gid_t group, gid_t* groups, int* ngroups) {
    16		return getgrouplist(user, group, groups, ngroups);
    17	}
    18	*/
    19	import "C"
    20	import (
    21		"fmt"
    22		"unsafe"
    23	)
    24	
    25	func getGroupList(name *C.char, userGID C.gid_t, gids *C.gid_t, n *C.int) C.int {
    26		return C.mygetgrouplist(name, userGID, gids, n)
    27	}
    28	
    29	// groupRetry retries getGroupList with much larger size for n. The result is
    30	// stored in gids.
    31	func groupRetry(username string, name []byte, userGID C.gid_t, gids *[]C.gid_t, n *C.int) error {
    32		// More than initial buffer, but now n contains the correct size.
    33		if *n > maxGroups {
    34			return fmt.Errorf("user: %q is a member of more than %d groups", username, maxGroups)
    35		}
    36		*gids = make([]C.gid_t, *n)
    37		rv := getGroupList((*C.char)(unsafe.Pointer(&name[0])), userGID, &(*gids)[0], n)
    38		if rv == -1 {
    39			return fmt.Errorf("user: list groups for %s failed", username)
    40		}
    41		return nil
    42	}
    43	

View as plain text