2022-09-29 14:30:13 +00:00
|
|
|
package users
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/user"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// lock is used to serialize all user lookup at the process level, because
|
|
|
|
// some NSS implementations are not concurrency safe
|
2022-10-04 15:52:12 +00:00
|
|
|
var lock sync.Mutex
|
2022-09-29 14:30:13 +00:00
|
|
|
|
|
|
|
// Lookup username while holding a global process lock.
|
|
|
|
func Lookup(username string) (*user.User, error) {
|
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
return user.Lookup(username)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LookupGroupId while holding a global process lock.
|
|
|
|
func LookupGroupId(gid string) (*user.Group, error) {
|
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
return user.LookupGroupId(gid)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Current returns the current user, acquired while holding a global process
|
|
|
|
// lock.
|
|
|
|
func Current() (*user.User, error) {
|
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
return user.Current()
|
|
|
|
}
|