Skip to content

Commit 62f0127

Browse files
kolyshkinbradfitz
authored andcommitted
os/user: add a way to enforce pure Go implementation
This provides a way to enforce pure Go implementation of os/user lookup functions on UNIX platforms by means of "osusergo" build tag, in a manner similar to netgo/netcgo tags in the net package. If "osusergo" build tag is set, Go implementation is selected. If "osusergo" build tag is NOT set, the old behavior is retained, that is to use cgo (libc-backed) implementation if both cgo and such and such implementation are available. The reason behind this change is to make it possible to build proper static binaries on Linux. The problem is, glibc implementation of getpw*, getgrp* and getgrouplist functions relies on presense of libnss*.so libraries during runtime, making it impossible to build a self-contained static binary which uses both cgo and os/user. In such case, linker warnings like this are shown: > warning: Using 'getgrouplist' in statically linked applications > requires at runtime the shared libraries from the glibc version > used for linking While this can be solved by recompiling glibc with --enable-static-nss flag or using a different libc implementation (like musl on Alpine Linux), it is not always practical or even possible. Fixes #23265 Change-Id: I383a448a2ecf15493ec93dbd5d076b6330cb14cb Signed-off-by: Kir Kolyshkin <[email protected]> Reviewed-on: https://go-review.googlesource.com/92456 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 3c64c86 commit 62f0127

File tree

5 files changed

+16
-4
lines changed

5 files changed

+16
-4
lines changed

src/os/user/cgo_lookup_unix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// license that can be found in the LICENSE file.
44

55
// +build darwin dragonfly freebsd !android,linux netbsd openbsd solaris
6-
// +build cgo
6+
// +build cgo,!osusergo
77

88
package user
99

src/os/user/listgroups_unix.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// license that can be found in the LICENSE file.
44

55
// +build dragonfly darwin freebsd !android,linux netbsd openbsd
6+
// +build cgo,!osusergo
67

78
package user
89

src/os/user/lookup_stubs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build !cgo,!windows,!plan9 android
5+
// +build !cgo,!windows,!plan9 android osusergo
66

77
package user
88

src/os/user/lookup_unix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// license that can be found in the LICENSE file.
44

55
// +build darwin dragonfly freebsd !android,linux nacl netbsd openbsd solaris
6-
// +build !cgo
6+
// +build !cgo osusergo
77

88
package user
99

src/os/user/user.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,18 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// Package user allows user account lookups by name or id.
5+
/*
6+
Package user allows user account lookups by name or id.
7+
8+
For most Unix systems, this package has two internal implementations of
9+
resolving user and group ids to names. One is written in pure Go and
10+
parses /etc/passwd and /etc/group. The other is cgo-based and relies on
11+
the standard C library (libc) routines such as getpwuid_r and getgrnam_r.
12+
13+
When cgo is available, cgo-based (libc-backed) code is used by default.
14+
This can be overriden by using osusergo build tag, which enforces
15+
the pure Go implementation.
16+
*/
617
package user
718

819
import (

0 commit comments

Comments
 (0)