From 899342b5d49434611635d64f64c343e2a1aeee0a Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Fri, 11 Feb 2022 13:14:01 -0800 Subject: [PATCH] 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 --- main.go | 13 ++++++------- rootless_linux.go | 3 --- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index d6e4bbf81..56a6066f8 100644 --- a/main.go +++ b/main.go @@ -71,13 +71,12 @@ func main() { } app.Version = strings.Join(v, "\n") - xdgRuntimeDir := "" root := "/run/runc" - if shouldHonorXDGRuntimeDir() { - if runtimeDir := os.Getenv("XDG_RUNTIME_DIR"); runtimeDir != "" { - root = runtimeDir + "/runc" - xdgRuntimeDir = root - } + xdgDirUsed := false + xdgRuntimeDir := os.Getenv("XDG_RUNTIME_DIR") + if xdgRuntimeDir != "" && shouldHonorXDGRuntimeDir() { + root = xdgRuntimeDir + "/runc" + xdgDirUsed = true } app.Flags = []cli.Flag{ @@ -135,7 +134,7 @@ func main() { featuresCommand, } 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 // XDG_RUNTIME_DIR to have a sticky bit if we don't want it to get // auto-pruned. diff --git a/rootless_linux.go b/rootless_linux.go index ae0170336..a1f548586 100644 --- a/rootless_linux.go +++ b/rootless_linux.go @@ -52,9 +52,6 @@ func shouldUseRootlessCgroupManager(context *cli.Context) (bool, error) { } func shouldHonorXDGRuntimeDir() bool { - if os.Getenv("XDG_RUNTIME_DIR") == "" { - return false - } if os.Geteuid() != 0 { return true }