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

Fix user query when using libc implementation of os/user #972

Merged
merged 1 commit into from
Oct 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions os.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,19 @@ func init() {

u, err := user.Current()
if err != nil {
// When the user is not in /etc/passwd (for e.g. LDAP) and CGO_ENABLED=1 in go env,
// the cgo implementation of user.Current() fails even when HOME and USER are set.

log.Printf("user: %s", err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment here, something along the lines:

// When the user is not in /etc/passwd (for e.g. LDAP) and CGO_ENABLED=1 in go env,
// the cgo implementation of user.Current() fails even when HOME and USER are set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done !

if os.Getenv("HOME") == "" {
log.Print("$HOME variable is empty or not set")
panic("$HOME variable is empty or not set")
}
if os.Getenv("USER") == "" {
log.Print("$USER variable is empty or not set")
panic("$USER variable is empty or not set")
}
u = &user.User{
Username: os.Getenv("USER"),
HomeDir: os.Getenv("HOME"),
}
}
gUser = u
Expand Down