From dbc99dc0d27cd53170f5166a30935fd507594beb Mon Sep 17 00:00:00 2001 From: David Anderson Date: Tue, 3 Mar 2020 17:37:37 -0800 Subject: [PATCH] paths: use /var/db for state on BSDs, and /var/run for sockets. On BSD, /var/db is what linux calls /var/lib. On modern linux, /run and /var/run are the same directory, but on BSD the correct path is /var/run, so use that. Fixes #79 Signed-off-by: David Anderson --- paths/paths.go | 4 ++-- paths/paths_unix.go | 23 +++++++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/paths/paths.go b/paths/paths.go index 78b5f8d5c..f9f354b28 100644 --- a/paths/paths.go +++ b/paths/paths.go @@ -21,8 +21,8 @@ func DefaultTailscaledSocket() string { if runtime.GOOS == "windows" { return "" } - if fi, err := os.Stat("/run"); err == nil && fi.IsDir() { - return "/run/tailscale/tailscaled.sock" + if fi, err := os.Stat("/var/run"); err == nil && fi.IsDir() { + return "/var/run/tailscale/tailscaled.sock" } return "tailscaled.sock" } diff --git a/paths/paths_unix.go b/paths/paths_unix.go index 28ee2abf9..bde26948e 100644 --- a/paths/paths_unix.go +++ b/paths/paths_unix.go @@ -8,6 +8,7 @@ package paths import ( "path/filepath" + "runtime" "golang.org/x/sys/unix" ) @@ -16,14 +17,28 @@ func init() { stateFileFunc = stateFileUnix } +func statePath() string { + switch runtime.GOOS { + case "linux": + return "/var/lib/tailscale/tailscaled.state" + case "freebsd", "openbsd": + return "/var/db/tailscale/tailscaled.state" + default: + return "" + } +} + func stateFileUnix() string { - // TODO: use other default paths on other GOOSes probably. This works for Linux. - const varLib = "/var/lib/tailscale/tailscaled.state" - try := varLib + path := statePath() + if path == "" { + return "" + } + + try := path for i := 0; i < 3; i++ { // check writability of the file, /var/lib/tailscale, and /var/lib err := unix.Access(try, unix.O_RDWR) if err == nil { - return varLib + return path } try = filepath.Dir(try) }