main: improve XDG_RUNTIME_DIR handling

1. Variable xdgRuntimeDir is only checked to be non-empty. Change it to
   a boolean.

2. Refactor so that os.Getenv is not called twice.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin
2022-02-11 13:14:01 -08:00
parent eb2f08dc4e
commit 899342b5d4
2 changed files with 6 additions and 10 deletions

13
main.go
View File

@@ -71,13 +71,12 @@ func main() {
} }
app.Version = strings.Join(v, "\n") app.Version = strings.Join(v, "\n")
xdgRuntimeDir := ""
root := "/run/runc" root := "/run/runc"
if shouldHonorXDGRuntimeDir() { xdgDirUsed := false
if runtimeDir := os.Getenv("XDG_RUNTIME_DIR"); runtimeDir != "" { xdgRuntimeDir := os.Getenv("XDG_RUNTIME_DIR")
root = runtimeDir + "/runc" if xdgRuntimeDir != "" && shouldHonorXDGRuntimeDir() {
xdgRuntimeDir = root root = xdgRuntimeDir + "/runc"
} xdgDirUsed = true
} }
app.Flags = []cli.Flag{ app.Flags = []cli.Flag{
@@ -135,7 +134,7 @@ func main() {
featuresCommand, featuresCommand,
} }
app.Before = func(context *cli.Context) error { app.Before = func(context *cli.Context) error {
if !context.IsSet("root") && xdgRuntimeDir != "" { if !context.IsSet("root") && xdgDirUsed {
// According to the XDG specification, we need to set anything in // According to the XDG specification, we need to set anything in
// XDG_RUNTIME_DIR to have a sticky bit if we don't want it to get // XDG_RUNTIME_DIR to have a sticky bit if we don't want it to get
// auto-pruned. // auto-pruned.

View File

@@ -52,9 +52,6 @@ func shouldUseRootlessCgroupManager(context *cli.Context) (bool, error) {
} }
func shouldHonorXDGRuntimeDir() bool { func shouldHonorXDGRuntimeDir() bool {
if os.Getenv("XDG_RUNTIME_DIR") == "" {
return false
}
if os.Geteuid() != 0 { if os.Geteuid() != 0 {
return true return true
} }