paths, version/distro: detect Synology DSM version better, use for socket path

Resolves a TODO in the code noted while discussing QNAP defaults.

Tested on DSM6 and DSM7.

Change-Id: Icce03ff41fafd7b3a358cfee16f2ed13d5cc3c5d
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2022-12-02 09:35:49 -08:00
committed by Brad Fitzpatrick
parent 5b8323509f
commit b9dd3fa534
2 changed files with 33 additions and 8 deletions

View File

@@ -6,11 +6,14 @@
package distro
import (
"bytes"
"io"
"os"
"runtime"
"strconv"
"tailscale.com/syncs"
"tailscale.com/util/lineread"
)
type Distro string
@@ -97,6 +100,8 @@ func freebsdDistro() Distro {
return ""
}
var dsmVersion syncs.AtomicValue[int]
// DSMVersion reports the Synology DSM major version.
//
// If not Synology, it reports 0.
@@ -107,6 +112,30 @@ func DSMVersion() int {
if Get() != Synology {
return 0
}
if v, ok := dsmVersion.LoadOk(); ok && v != 0 {
return v
}
// This is set when running as a package:
v, _ := strconv.Atoi(os.Getenv("SYNOPKG_DSM_VERSION_MAJOR"))
if v != 0 {
dsmVersion.Store(v)
return v
}
// But when run from the command line, we have to read it from the file:
lineread.File("/etc/VERSION", func(line []byte) error {
line = bytes.TrimSpace(line)
if string(line) == `majorversion="7"` {
v = 7
return io.EOF
}
if string(line) == `majorversion="6"` {
v = 6
return io.EOF
}
return nil
})
if v != 0 {
dsmVersion.Store(v)
}
return v
}