From 55b372a79fd3a23fc4a26af5be08acdb27f52f0f Mon Sep 17 00:00:00 2001 From: Percy Wegmann Date: Sat, 10 Feb 2024 11:32:25 -0600 Subject: [PATCH] tailscaled: revert to using pointers for subcommands As part of #10631, we stopped using function pointers for subcommands, preventing us from registering platform-specific installSystemDaemon and uninstallSystemDaemon subcommands. Fixes #11099 Signed-off-by: Percy Wegmann --- cmd/tailscaled/tailscaled.go | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/cmd/tailscaled/tailscaled.go b/cmd/tailscaled/tailscaled.go index 1f9d8ba8f..8c4365548 100644 --- a/cmd/tailscaled/tailscaled.go +++ b/cmd/tailscaled/tailscaled.go @@ -136,12 +136,16 @@ var ( createBIRDClient func(string) (wgengine.BIRDClient, error) // non-nil on some platforms ) -var subCommands = map[string]func([]string) error{ - "install-system-daemon": installSystemDaemon, - "uninstall-system-daemon": uninstallSystemDaemon, - "debug": debugModeFunc, - "be-child": beChild, - "serve-tailfs": serveTailFS, +// Note - we use function pointers for subcommands so that subcommands like +// installSystemDaemon and uninstallSystemDaemon can be assigned platform- +// specific variants. + +var subCommands = map[string]*func([]string) error{ + "install-system-daemon": &installSystemDaemon, + "uninstall-system-daemon": &uninstallSystemDaemon, + "debug": &debugModeFunc, + "be-child": &beChildFunc, + "serve-tailfs": &serveTailFSFunc, } var beCLI func() // non-nil if CLI is linked in @@ -173,12 +177,12 @@ func main() { if len(os.Args) > 1 { sub := os.Args[1] - if fn, ok := subCommands[sub]; ok { - if fn == nil { + if fp, ok := subCommands[sub]; ok { + if fp == nil { log.SetFlags(0) log.Fatalf("%s not available on %v", sub, runtime.GOOS) } - if err := fn(os.Args[2:]); err != nil { + if err := (*fp)(os.Args[2:]); err != nil { log.SetFlags(0) log.Fatal(err) } @@ -799,6 +803,8 @@ func mustStartProxyListeners(socksAddr, httpAddr string) (socksListener, httpLis return socksListener, httpListener } +var beChildFunc = beChild + func beChild(args []string) error { if len(args) == 0 { return errors.New("missing mode argument") @@ -811,6 +817,8 @@ func beChild(args []string) error { return f(args[1:]) } +var serveTailFSFunc = serveTailFS + // serveTailFS serves one or more tailfs on localhost using the WebDAV // protocol. On UNIX and MacOS tailscaled environment, tailfs spawns child // tailscaled processes in serve-tailfs mode in order to access the fliesystem