Revert "homedir: add cgo or osusergo buildtag constraints for unix"
TL;DR: there is no way to do this right.
We do know that in some combination of build tags set (or unset),
linker flags, environment variables, and libc implementation,
this package won't work right. In fact, there is one specific
combination:
1. `CGO_ENABLED=1` (or unset)
2. static binary is being built (e.g. `go build` is run with `-extldflags -static`)
3. `go build` links the binary against glibc
4. `osusergo` is not set
This particular combination results in the following legitimate linker warning:
> cgo_lookup_unix.go: warning: Using 'getpwuid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
If this warning is ignored and the resulting binary is used on a system
with files from a different glibc version (or without those files), it
could result in a segfault.
The commit being reverted tried to guard against such possibility,
but the problem is, we can only use build tags to account for items
1 and 4 from the above list, while items 2 and 3 do not result in
any build tags being set or unset, making this guard excessive.
Remove it.
This reverts commit 023b072288eab3c9e768d4aeeb917f27f06034c7.
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2019-10-24 15:57:03 -04:00
|
|
|
// +build !windows
|
2017-07-10 09:10:43 -04:00
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package homedir // import "github.com/docker/docker/pkg/homedir"
|
2015-02-06 13:18:49 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2019-09-23 18:16:38 -04:00
|
|
|
"os/user"
|
2015-02-06 13:18:49 -05:00
|
|
|
)
|
|
|
|
|
2015-02-06 17:17:34 -05:00
|
|
|
// Key returns the env var name for the user's home dir based on
|
|
|
|
// the platform being run on
|
|
|
|
func Key() string {
|
|
|
|
return "HOME"
|
|
|
|
}
|
|
|
|
|
2015-02-06 13:18:49 -05:00
|
|
|
// Get returns the home directory of the current user with the help of
|
|
|
|
// environment variables depending on the target operating system.
|
|
|
|
// Returned path should be used with "path/filepath" to form new paths.
|
2019-10-24 19:39:24 -04:00
|
|
|
//
|
|
|
|
// If linking statically with cgo enabled against glibc, ensure the
|
|
|
|
// osusergo build tag is used.
|
|
|
|
//
|
|
|
|
// If needing to do nss lookups, do not disable cgo or set osusergo.
|
2015-02-06 13:18:49 -05:00
|
|
|
func Get() string {
|
2015-03-10 09:55:55 -04:00
|
|
|
home := os.Getenv(Key())
|
2017-07-10 09:10:43 -04:00
|
|
|
if home == "" {
|
2019-09-23 18:16:38 -04:00
|
|
|
if u, err := user.Current(); err == nil {
|
|
|
|
return u.HomeDir
|
2015-03-10 09:55:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return home
|
2015-02-06 13:18:49 -05:00
|
|
|
}
|
2015-02-19 01:53:04 -05:00
|
|
|
|
|
|
|
// GetShortcutString returns the string that is shortcut to user's home directory
|
|
|
|
// in the native shell of the platform running on.
|
|
|
|
func GetShortcutString() string {
|
|
|
|
return "~"
|
|
|
|
}
|