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

Backport of client: defer nobody user lookup so Windows doesn't panic into release/1.4.x #14794

Merged
Show file tree
Hide file tree
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
9 changes: 6 additions & 3 deletions client/allocdir/fs_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@ func dropDirPermissions(path string, desired os.FileMode) error {
return nil
}

nobody := users.Nobody()
nobody, err := users.Nobody()
if err != nil {
return err
}

uid, err := getUid(&nobody)
uid, err := getUid(nobody)
if err != nil {
return err
}

gid, err := getGid(&nobody)
gid, err := getGid(nobody)
if err != nil {
return err
}
Expand Down
24 changes: 10 additions & 14 deletions helper/users/lookup.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
package users

import (
"fmt"
"os/user"
"sync"
)

// lock is used to serialize all user lookup at the process level, because
// some NSS implementations are not concurrency safe
var lock *sync.Mutex
var lock sync.Mutex

// nobody is a cached copy of the nobody user, which is going to be looked-up
// frequently and is unlikely to be modified on the underlying system.
var nobody user.User
var nobody *user.User

// Nobody returns User data for the "nobody" user on the system, bypassing the
// locking / file read / NSS lookup.
func Nobody() user.User {
// original is immutable via copy by value
return nobody
}

func init() {
lock = new(sync.Mutex)
u, err := Lookup("nobody")
if err != nil {
panic(fmt.Sprintf("unable to lookup the nobody user: %v", err))
func Nobody() (*user.User, error) {
lock.Lock()
defer lock.Unlock()
if nobody != nil {
return nobody, nil
}
nobody = *u
u, err := user.Lookup("nobody")
nobody = u
return u, err
}

// Lookup username while holding a global process lock.
Expand Down