From 33bc69cf1f7a026221368c1cebbbdcee66529a80 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 30 Mar 2021 08:20:21 -0700 Subject: [PATCH] paths: fall back to XDG_DATA_HOME for non-root users' state dir So peerapi has a default state directory, mostly for netstack mode testing. Signed-off-by: Brad Fitzpatrick --- paths/paths_unix.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/paths/paths_unix.go b/paths/paths_unix.go index 1633fc32e..4012f79de 100644 --- a/paths/paths_unix.go +++ b/paths/paths_unix.go @@ -7,6 +7,7 @@ package paths import ( + "os" "path/filepath" "runtime" @@ -45,8 +46,17 @@ func stateFileUnix() string { try = filepath.Dir(try) } - // TODO: try some $HOME/.tailscale or XDG path? But will it - // even work usefully enough as non-root? Probably not. Maybe - // best to require it be explicit in that case. - return "" + if os.Getuid() == 0 { + return "" + } + + // For non-root users, fall back to $XDG_DATA_HOME/tailscale/*. + return filepath.Join(xdgDataHome(), "tailscale", "tailscaled.state") +} + +func xdgDataHome() string { + if e := os.Getenv("XDG_DATA_HOME"); e != "" { + return e + } + return filepath.Join(os.Getenv("HOME"), ".local/share") }