From 5a0c37aafd931821cd56b802c2bf48f831b5749b Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Mon, 10 Aug 2020 20:44:26 -0700 Subject: [PATCH] logpolicy: consider /var/lib/tailscale when no STATE_DIRECTORY Signed-off-by: Brad Fitzpatrick --- logpolicy/logpolicy.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/logpolicy/logpolicy.go b/logpolicy/logpolicy.go index 3f648d508..41b1c9189 100644 --- a/logpolicy/logpolicy.go +++ b/logpolicy/logpolicy.go @@ -31,6 +31,7 @@ import ( "tailscale.com/logtail/filch" "tailscale.com/net/netns" "tailscale.com/net/tlsdial" + "tailscale.com/paths" "tailscale.com/smallzstd" "tailscale.com/types/logger" "tailscale.com/version" @@ -103,12 +104,23 @@ func (l logWriter) Write(buf []byte) (int, error) { // logsDir returns the directory to use for log configuration and // buffer storage. func logsDir(logf logger.Logf) string { + // STATE_DIRECTORY is set by systemd 240+ but we support older + // systems-d. For example, Ubuntu 18.04 (Bionic Beaver) is 237. systemdStateDir := os.Getenv("STATE_DIRECTORY") if systemdStateDir != "" { logf("logpolicy: using $STATE_DIRECTORY, %q", systemdStateDir) return systemdStateDir } + // Default to e.g. /var/lib/tailscale or /var/db/tailscale on Unix. + if d := paths.DefaultTailscaledStateFile(); d != "" { + d = filepath.Dir(d) // directory of e.g. "/var/lib/tailscale/tailscaled.state" + if err := os.MkdirAll(d, 0700); err == nil { + logf("logpolicy: using system state directory %q", d) + return d + } + } + cacheDir, err := os.UserCacheDir() if err == nil { d := filepath.Join(cacheDir, "Tailscale")