Skip to content

Commit

Permalink
Backport of client: defer nobody user lookup so Windows doesn't pan…
Browse files Browse the repository at this point in the history
…ic into release/1.4.x (#14794)

This pull request was automerged via backport-assistant
  • Loading branch information
hc-github-team-nomad-core committed Oct 4, 2022
1 parent d7ac5d7 commit 2ef073c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
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

0 comments on commit 2ef073c

Please sign in to comment.