1
0
mirror of https://github.com/golang/go synced 2024-11-13 18:00:30 -07:00

os/user: don't create C function mygetgrouplist

Instead of exporting the C function mygetgrouplist as a global symbol to
conflict with other symbols of the same name, use trivial Go code and a
static C function.

Change-Id: I98dd667814d0a0ed8f7b1d4cfc6483d5a6965b26
Reviewed-on: https://go-review.googlesource.com/23008
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Ian Lance Taylor 2016-05-10 14:27:32 -07:00
parent 81a89606ef
commit 9780bf2a95
3 changed files with 21 additions and 8 deletions

View File

@ -2,13 +2,14 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// +build cgo package user
/*
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <stdlib.h> #include <stdlib.h>
int mygetgrouplist(const char* user, gid_t group, gid_t* groups, int* ngroups) { static int mygetgrouplist(const char* user, gid_t group, gid_t* groups, int* ngroups) {
int* buf = malloc(*ngroups * sizeof(int)); int* buf = malloc(*ngroups * sizeof(int));
int rv = getgrouplist(user, (int) group, buf, ngroups); int rv = getgrouplist(user, (int) group, buf, ngroups);
int i; int i;
@ -20,3 +21,9 @@ int mygetgrouplist(const char* user, gid_t group, gid_t* groups, int* ngroups) {
free(buf); free(buf);
return rv; return rv;
} }
*/
import "C"
func getGroupList(name *C.char, userGID C.gid_t, gids *C.gid_t, n *C.int) C.int {
return C.mygetgrouplist(name, userGID, gids, n)
}

View File

@ -2,13 +2,21 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// +build cgo
// +build dragonfly freebsd !android,linux netbsd openbsd // +build dragonfly freebsd !android,linux netbsd openbsd
package user
/*
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <grp.h> #include <grp.h>
int mygetgrouplist(const char* user, gid_t group, gid_t* groups, int* ngroups) { static int mygetgrouplist(const char* user, gid_t group, gid_t* groups, int* ngroups) {
return getgrouplist(user, group, groups, ngroups); return getgrouplist(user, group, groups, ngroups);
} }
*/
import "C"
func getGroupList(name *C.char, userGID C.gid_t, gids *C.gid_t, n *C.int) C.int {
return C.mygetgrouplist(name, userGID, gids, n)
}

View File

@ -16,8 +16,6 @@ import (
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <stdlib.h> #include <stdlib.h>
extern int mygetgrouplist(const char* user, gid_t group, gid_t* groups, int* ngroups);
*/ */
import "C" import "C"
@ -32,7 +30,7 @@ func listGroups(u *User) ([]string, error) {
n := C.int(256) n := C.int(256)
gidsC := make([]C.gid_t, n) gidsC := make([]C.gid_t, n)
rv := C.mygetgrouplist(nameC, userGID, &gidsC[0], &n) rv := getGroupList(nameC, userGID, &gidsC[0], &n)
if rv == -1 { if rv == -1 {
// More than initial buffer, but now n contains the correct size. // More than initial buffer, but now n contains the correct size.
const maxGroups = 2048 const maxGroups = 2048
@ -40,7 +38,7 @@ func listGroups(u *User) ([]string, error) {
return nil, fmt.Errorf("user: list groups for %s: member of more than %d groups", u.Username, maxGroups) return nil, fmt.Errorf("user: list groups for %s: member of more than %d groups", u.Username, maxGroups)
} }
gidsC = make([]C.gid_t, n) gidsC = make([]C.gid_t, n)
rv := C.mygetgrouplist(nameC, userGID, &gidsC[0], &n) rv := getGroupList(nameC, userGID, &gidsC[0], &n)
if rv == -1 { if rv == -1 {
return nil, fmt.Errorf("user: list groups for %s failed (changed groups?)", u.Username) return nil, fmt.Errorf("user: list groups for %s failed (changed groups?)", u.Username)
} }