...

Source file src/os/user/cgo_listgroups_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 || darwin || freebsd || (!android && linux) || netbsd || openbsd || (solaris && !illumos)) && cgo && !osusergo
     6  
     7  package user
     8  
     9  import (
    10  	"fmt"
    11  	"strconv"
    12  	"unsafe"
    13  )
    14  
    15  /*
    16  #include <unistd.h>
    17  #include <sys/types.h>
    18  */
    19  import "C"
    20  
    21  const maxGroups = 2048
    22  
    23  func listGroups(u *User) ([]string, error) {
    24  	ug, err := strconv.Atoi(u.Gid)
    25  	if err != nil {
    26  		return nil, fmt.Errorf("user: list groups for %s: invalid gid %q", u.Username, u.Gid)
    27  	}
    28  	userGID := C.gid_t(ug)
    29  	nameC := make([]byte, len(u.Username)+1)
    30  	copy(nameC, u.Username)
    31  
    32  	n := C.int(256)
    33  	gidsC := make([]C.gid_t, n)
    34  	rv := getGroupList((*C.char)(unsafe.Pointer(&nameC[0])), userGID, &gidsC[0], &n)
    35  	if rv == -1 {
    36  		// Mac is the only Unix that does not set n properly when rv == -1, so
    37  		// we need to use different logic for Mac vs. the other OS's.
    38  		if err := groupRetry(u.Username, nameC, userGID, &gidsC, &n); err != nil {
    39  			return nil, err
    40  		}
    41  	}
    42  	gidsC = gidsC[:n]
    43  	gids := make([]string, 0, n)
    44  	for _, g := range gidsC[:n] {
    45  		gids = append(gids, strconv.Itoa(int(g)))
    46  	}
    47  	return gids, nil
    48  }
    49  

View as plain text