cmd/tailscaled: make embedded CLI run earlier, support triggering via env

Not all platforms have hardlinks, or not easily.

This lets a "tailscale" wrapper script set an environment variable
before calling tailscaled.

Updates #2233

Change-Id: I9eccc18651e56c106f336fcbbd0fd97a661d312e
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2025-03-27 19:31:47 -07:00 committed by Brad Fitzpatrick
parent 6a9a7f35d9
commit 4c5112eba6

View File

@ -151,10 +151,33 @@ var subCommands = map[string]*func([]string) error{
"serve-taildrive": &serveDriveFunc,
}
var beCLI func() // non-nil if CLI is linked in
var beCLI func() // non-nil if CLI is linked in with the "ts_include_cli" build tag
// shouldRunCLI reports whether we should run the Tailscale CLI (cmd/tailscale)
// instead of the daemon (cmd/tailscaled) in the case when the two are linked
// together into one binary for space savings reasons.
func shouldRunCLI() bool {
if beCLI == nil {
// Not linked in with the "ts_include_cli" build tag.
return false
}
if len(os.Args) > 0 && filepath.Base(os.Args[0]) == "tailscale" {
// The binary was named (or hardlinked) as "tailscale".
return true
}
if envknob.Bool("TS_BE_CLI") {
// The environment variable was set to force it.
return true
}
return false
}
func main() {
envknob.PanicIfAnyEnvCheckedInInit()
if shouldRunCLI() {
beCLI()
return
}
envknob.ApplyDiskConfig()
applyIntegrationTestEnvKnob()
@ -175,11 +198,6 @@ func main() {
flag.BoolVar(&args.disableLogs, "no-logs-no-support", false, "disable log uploads; this also disables any technical support")
flag.StringVar(&args.confFile, "config", "", "path to config file, or 'vm:user-data' to use the VM's user-data (EC2)")
if len(os.Args) > 0 && filepath.Base(os.Args[0]) == "tailscale" && beCLI != nil {
beCLI()
return
}
if len(os.Args) > 1 {
sub := os.Args[1]
if fp, ok := subCommands[sub]; ok {