Skip to content

Commit 134f148

Browse files
committedMay 10, 2020
logName(): lazily lookup userName instead of on init()
Commit c46b9e1 implemented a workaround for situations on Windows where `user.Current()` was not available. On Linux/Unix ennvironments, `user.Current()` may be calling (among others) `getgrnam_r` (https://linux.die.net/man/3/getgrgid_r), which: > Returns a pointer to a structure containing the broken-out fields of > the record in the group database (e.g., the local group file /etc/group, > NIS, and LDAP) that matches the group name name. This means that the `init()` function might be making network connections, which is not desirable. This patch changes the lookup to be performed lazily. A `sync.Once` was added so that lookup is only performed once (to keep the behavior that was previously provided by using `init()`. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent ea583d2 commit 134f148

File tree

1 file changed

+35
-29
lines changed

1 file changed

+35
-29
lines changed
 

‎klog_file.go

+35-29
Original file line numberDiff line numberDiff line change
@@ -44,43 +44,49 @@ func createLogDirs() {
4444
}
4545

4646
var (
47-
pid = os.Getpid()
48-
program = filepath.Base(os.Args[0])
49-
host = "unknownhost"
50-
userName = "unknownuser"
47+
pid = os.Getpid()
48+
program = filepath.Base(os.Args[0])
49+
host = "unknownhost"
50+
userName = "unknownuser"
51+
userNameOnce sync.Once
5152
)
5253

5354
func init() {
54-
h, err := os.Hostname()
55-
if err == nil {
55+
if h, err := os.Hostname(); err == nil {
5656
host = shortHostname(h)
5757
}
58+
}
5859

59-
// On Windows, the Go 'user' package requires netapi32.dll.
60-
// This affects Windows Nano Server:
61-
// https://github.com/golang/go/issues/21867
62-
// Fallback to using environment variables.
63-
if runtime.GOOS == "windows" {
64-
u := os.Getenv("USERNAME")
65-
if len(u) == 0 {
66-
return
67-
}
68-
// Sanitize the USERNAME since it may contain filepath separators.
69-
u = strings.Replace(u, `\`, "_", -1)
60+
func getUserName() string {
61+
userNameOnce.Do(func() {
62+
// On Windows, the Go 'user' package requires netapi32.dll.
63+
// This affects Windows Nano Server:
64+
// https://github.com/golang/go/issues/21867
65+
// Fallback to using environment variables.
66+
if runtime.GOOS == "windows" {
67+
u := os.Getenv("USERNAME")
68+
if len(u) == 0 {
69+
return
70+
}
71+
// Sanitize the USERNAME since it may contain filepath separators.
72+
u = strings.Replace(u, `\`, "_", -1)
7073

71-
// user.Current().Username normally produces something like 'USERDOMAIN\USERNAME'
72-
d := os.Getenv("USERDOMAIN")
73-
if len(d) != 0 {
74-
userName = d + "_" + u
74+
// user.Current().Username normally produces something like 'USERDOMAIN\USERNAME'
75+
d := os.Getenv("USERDOMAIN")
76+
if len(d) != 0 {
77+
userName = d + "_" + u
78+
} else {
79+
userName = u
80+
}
7581
} else {
76-
userName = u
77-
}
78-
} else {
79-
current, err := user.Current()
80-
if err == nil {
81-
userName = current.Username
82+
current, err := user.Current()
83+
if err == nil {
84+
userName = current.Username
85+
}
8286
}
83-
}
87+
})
88+
89+
return userName
8490
}
8591

8692
// shortHostname returns its argument, truncating at the first period.
@@ -98,7 +104,7 @@ func logName(tag string, t time.Time) (name, link string) {
98104
name = fmt.Sprintf("%s.%s.%s.log.%s.%04d%02d%02d-%02d%02d%02d.%d",
99105
program,
100106
host,
101-
userName,
107+
getUserName(),
102108
tag,
103109
t.Year(),
104110
t.Month(),

0 commit comments

Comments
 (0)
Please sign in to comment.