-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add static_build tag which avoids user.Current()
Its not safe to use os/user.Current() in a statically linked program. You will be getting warnings like: warning: Using 'getpwnam_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking warning: Using 'getpwuid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking We avoid this when building with -tag static_build by parsing /etc/passwd directly for the homedir.
- Loading branch information
1 parent
164977e
commit da6051a
Showing
4 changed files
with
89 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package dbus | ||
|
||
import ( | ||
"os" | ||
"sync" | ||
) | ||
|
||
var ( | ||
homeDir string | ||
homeDirLock sync.Mutex | ||
) | ||
|
||
func getHomeDir() string { | ||
homeDirLock.Lock() | ||
defer homeDirLock.Unlock() | ||
|
||
if homeDir != "" { | ||
return homeDir | ||
} | ||
|
||
homeDir = os.Getenv("HOME") | ||
if homeDir != "" { | ||
return homeDir | ||
} | ||
|
||
homeDir = lookupHomeDir() | ||
return homeDir | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// +build !static_build | ||
|
||
package dbus | ||
|
||
import ( | ||
"os/user" | ||
) | ||
|
||
func lookupHomeDir() string { | ||
u, err := user.Current() | ||
if err != nil { | ||
return "/" | ||
} | ||
return u.HomeDir | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// +build static_build | ||
|
||
package dbus | ||
|
||
import ( | ||
"bufio" | ||
"os" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func lookupHomeDir() string { | ||
myUid := os.Getuid() | ||
|
||
f, err := os.Open("/etc/passwd") | ||
if err != nil { | ||
return "/" | ||
} | ||
defer f.Close() | ||
|
||
s := bufio.NewScanner(f) | ||
|
||
for s.Scan() { | ||
if err := s.Err(); err != nil { | ||
break | ||
} | ||
|
||
line := strings.TrimSpace(s.Text()) | ||
if line == "" { | ||
continue | ||
} | ||
|
||
parts := strings.Split(line, ":") | ||
|
||
if len(parts) >= 6 { | ||
uid, err := strconv.Atoi(parts[2]) | ||
if err == nil && uid == myUid { | ||
return parts[5] | ||
} | ||
} | ||
} | ||
|
||
// Default to / if we can't get a better value | ||
return "/" | ||
} |