Black Lives Matter. Support the Equal Justice Initiative.

Source file src/os/user/getgrouplist_unix.go

Documentation: os/user

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

View as plain text