From c6f45fd2eef00d6dcdcf319400c0c6cd891e9d2a Mon Sep 17 00:00:00 2001 From: Phil Estes Date: Wed, 6 Apr 2016 17:24:17 -0400 Subject: [PATCH] Lazy init useradd and remove init() This should not have been in init() as it causes these lookups to happen in all reexecs of the Docker binary. The only time it needs to be resolved is when a user is added, which is extremely rare. Docker-DCO-1.1-Signed-off-by: Phil Estes --- pkg/idtools/usergroupadd_linux.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pkg/idtools/usergroupadd_linux.go b/pkg/idtools/usergroupadd_linux.go index 86d9e21e9ae4438b450fc5b034c0df5167cf24e2..4a4aaed04d0fe114a677faf8cf23083d32abcb99 100644 --- a/pkg/idtools/usergroupadd_linux.go +++ b/pkg/idtools/usergroupadd_linux.go @@ -8,6 +8,7 @@ import ( "sort" "strconv" "strings" + "sync" ) // add a user and/or group to Linux /etc/passwd, /etc/group using standard @@ -16,6 +17,7 @@ import ( // useradd -r -s /bin/false var ( + once sync.Once userCommand string cmdTemplates = map[string]string{ @@ -31,15 +33,6 @@ var ( userMod = "usermod" ) -func init() { - // set up which commands are used for adding users/groups dependent on distro - if _, err := resolveBinary("adduser"); err == nil { - userCommand = "adduser" - } else if _, err := resolveBinary("useradd"); err == nil { - userCommand = "useradd" - } -} - func resolveBinary(binname string) (string, error) { binaryPath, err := exec.LookPath(binname) if err != nil { @@ -94,7 +87,14 @@ func AddNamespaceRangesUser(name string) (int, int, error) { } func addUser(userName string) error { - + once.Do(func() { + // set up which commands are used for adding users/groups dependent on distro + if _, err := resolveBinary("adduser"); err == nil { + userCommand = "adduser" + } else if _, err := resolveBinary("useradd"); err == nil { + userCommand = "useradd" + } + }) if userCommand == "" { return fmt.Errorf("Cannot add user; no useradd/adduser binary found") }