]> Cypherpunks repositories - gostls13.git/commitdiff
os/user: add a way to enforce pure Go implementation
authorKir Kolyshkin <kolyshkin@gmail.com>
Tue, 6 Feb 2018 21:16:02 +0000 (13:16 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Fri, 6 Apr 2018 19:42:41 +0000 (19:42 +0000)
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 <kolyshkin@gmail.com>
Reviewed-on: https://go-review.googlesource.com/92456
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/os/user/cgo_lookup_unix.go
src/os/user/listgroups_unix.go
src/os/user/lookup_stubs.go
src/os/user/lookup_unix.go
src/os/user/user.go

index 987a2d8c7d16a85a0b7c93af1a6d8580c7cc5c53..210bd6e0b35c175eef1313a0bf60491569f15466 100644 (file)
@@ -3,7 +3,7 @@
 // license that can be found in the LICENSE file.
 
 // +build darwin dragonfly freebsd !android,linux netbsd openbsd solaris
-// +build cgo
+// +build cgo,!osusergo
 
 package user
 
index 44f4ae1714e218bd1bd180de777247cdc880cb73..70f7af7f9792f2a1aefe80a875f4d041fd1d4d60 100644 (file)
@@ -3,6 +3,7 @@
 // license that can be found in the LICENSE file.
 
 // +build dragonfly darwin freebsd !android,linux netbsd openbsd
+// +build cgo,!osusergo
 
 package user
 
index d23870fda883b73948c1529322990fe0bf3e3635..dc5ab4005590fdb3a28ff0d2df35fb8e137427ec 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build !cgo,!windows,!plan9 android
+// +build !cgo,!windows,!plan9 android osusergo
 
 package user
 
index 5f34ba8611c9b8121b8a6a6982182301c8b9e081..05f39be40b31c3d438a30985b907a23e44378959 100644 (file)
@@ -3,7 +3,7 @@
 // license that can be found in the LICENSE file.
 
 // +build darwin dragonfly freebsd !android,linux nacl netbsd openbsd solaris
-// +build !cgo
+// +build !cgo osusergo
 
 package user
 
index ad61992ad311ead4def63fbf9293c6c9d25850c0..1f733b80235a5489ff1667dd26ea2d6bba1aaff6 100644 (file)
@@ -2,7 +2,18 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// Package user allows user account lookups by name or id.
+/*
+Package user allows user account lookups by name or id.
+
+For most Unix systems, this package has two internal implementations of
+resolving user and group ids to names. One is written in pure Go and
+parses /etc/passwd and /etc/group. The other is cgo-based and relies on
+the standard C library (libc) routines such as getpwuid_r and getgrnam_r.
+
+When cgo is available, cgo-based (libc-backed) code is used by default.
+This can be overriden by using osusergo build tag, which enforces
+the pure Go implementation.
+*/
 package user
 
 import (