Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os/user: implement basic cgo-free Current user function #14626

Closed
bradfitz opened this issue Mar 3, 2016 · 2 comments
Closed

os/user: implement basic cgo-free Current user function #14626

bradfitz opened this issue Mar 3, 2016 · 2 comments

Comments

@bradfitz
Copy link
Contributor

bradfitz commented Mar 3, 2016

os/user.Current returns a (*User, error). User is:

type User struct {
        Uid      string // user id
        Gid      string // primary group id
        Username string
        Name     string
        HomeDir  string
}

When the binary is built without cgo, os/user.Current returns an error.

This bites people often (most recently: #14625) and is confusing. I propose that in cgo-free builds, we make Current work using:

user := &User{
   Uid: strconv.Itoa(os.Getuid()),
   Gid: strconv.Itoa(os.Getgid()),
   Username: os.Getenv("USER"),  // or USERNAME on Windows
   HomeDir: os.Getenv("HOME"),   // or HOMEDRIVE+HOMEDIR on windows
}

and only if either Username or HomeDir are empty do we return an error.

I think we can ignore the User.Name field.

@minux
Copy link
Member

minux commented Mar 3, 2016 via email

@gopherbot
Copy link
Contributor

CL https://golang.org/cl/20192 mentions this issue.

Arista-Jenkins pushed a commit to aristanetworks/glog that referenced this issue Feb 2, 2017
It's not necessary, and pretty much nothing else in our code base
depends on it.  Also it doesn't really work when cross-compiling.
In fact it was "fixed" in Go 1.7 [golang/go#14626] to return the
value of $USER, which is what this commit basically does.

Change-Id: I35fd95c6913a9d11112a0b1fd061b73549fd91c0
@golang golang locked and limited conversation to collaborators Mar 13, 2017
gopherbot pushed a commit that referenced this issue Sep 9, 2018
Per #27524 there are situations where the username for the
uid does not match the value in the $USER environment variable and it
seems sensible to choose the value in /etc/passwd when they disagree.

This may make the Current() call slightly more expensive, since we
read /etc/passwd with cgo disabled instead of just checking the
environment. However, we cache the result of Current() calls, so we
only invoke this cost once in the lifetime of the process.

Fixes #14626.
Fixes #27524.
Updates #24884.

Change-Id: I0dcd224cf7f61dd5292f3fcc363aa2e9656a2cb1
Reviewed-on: https://go-review.googlesource.com/134218
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants