From 0dac03876a70b906d8e98d44aad8cde7374b214f Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sat, 4 Apr 2020 17:18:04 -0700 Subject: [PATCH] logpolicy: don't put log state in /. Signed-off-by: David Anderson --- logpolicy/logpolicy.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/logpolicy/logpolicy.go b/logpolicy/logpolicy.go index 4fa3389b9..931e3ce07 100644 --- a/logpolicy/logpolicy.go +++ b/logpolicy/logpolicy.go @@ -111,10 +111,22 @@ func logsDir() string { return filepath.Join(cacheDir, "Tailscale") } - // No idea where to put stuff. This only happens when $HOME is - // unset, which os.UserCacheDir doesn't like. Use the current - // working directory and hope for the best. - return "" + // Use the current working directory, unless we're being run by a + // service manager that sets it to /. + wd, err := os.Getwd() + if err == nil && wd != "/" { + return wd + } + + // No idea where to put stuff. Try to create a temp dir. It'll + // mean we might lose some logs and rotate through log IDs, but + // it's something. + tmp, err := ioutil.TempDir("", "tailscaled-log-*") + if err != nil { + panic("no safe place found to store log state") + } + + return tmp } // runningUnderSystemd reports whether we're running under systemd.