From 4cbdc84d275ccdd68f7f8b11f0eee5b5ca81cdbb Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 24 Feb 2022 14:03:36 -0800 Subject: [PATCH] cmd/tailscaled/childproc: add be-child registration mechanism For ssh and maybe windows service babysitter later. Updates #3802 Change-Id: I7492b98df98971b3fb72d148ba92c2276cca491f Signed-off-by: Brad Fitzpatrick --- cmd/tailscaled/childproc/childproc.go | 20 +++++++++++++++++++ cmd/tailscaled/depaware.txt | 1 + cmd/tailscaled/tailscaled.go | 16 +++++++++++++++ ssh/tailssh/tailssh.go | 10 ++++++++++ .../tailscaled_deps_test_darwin.go | 1 + .../tailscaled_deps_test_freebsd.go | 1 + .../integration/tailscaled_deps_test_linux.go | 1 + .../tailscaled_deps_test_openbsd.go | 1 + .../tailscaled_deps_test_windows.go | 1 + 9 files changed, 52 insertions(+) create mode 100644 cmd/tailscaled/childproc/childproc.go diff --git a/cmd/tailscaled/childproc/childproc.go b/cmd/tailscaled/childproc/childproc.go new file mode 100644 index 000000000..3fedd9a40 --- /dev/null +++ b/cmd/tailscaled/childproc/childproc.go @@ -0,0 +1,20 @@ +// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package childproc allows other packages to register "tailscaled be-child" +// child process hook code. This avoids duplicating build tags in the +// tailscaled package. Instead, the code that needs to fork/exec the self +// executable (when it's tailscaled) can instead register the code +// they want to run. +package childproc + +var Code = map[string]func([]string) error{} + +// Add registers code f to run as 'tailscaled be-child [args]'. +func Add(typ string, f func(args []string) error) { + if _, dup := Code[typ]; dup { + panic("dup hook " + typ) + } + Code[typ] = f +} diff --git a/cmd/tailscaled/depaware.txt b/cmd/tailscaled/depaware.txt index 9070329fa..cd2eef9d1 100644 --- a/cmd/tailscaled/depaware.txt +++ b/cmd/tailscaled/depaware.txt @@ -168,6 +168,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de LD tailscale.com/chirp from tailscale.com/cmd/tailscaled tailscale.com/client/tailscale from tailscale.com/derp tailscale.com/client/tailscale/apitype from tailscale.com/client/tailscale+ + tailscale.com/cmd/tailscaled/childproc from tailscale.com/cmd/tailscaled+ tailscale.com/control/controlclient from tailscale.com/ipn/ipnlocal+ tailscale.com/control/controlknobs from tailscale.com/control/controlclient+ tailscale.com/derp from tailscale.com/derp/derphttp+ diff --git a/cmd/tailscaled/tailscaled.go b/cmd/tailscaled/tailscaled.go index 942ee2ba6..84c24251a 100644 --- a/cmd/tailscaled/tailscaled.go +++ b/cmd/tailscaled/tailscaled.go @@ -28,6 +28,7 @@ import ( "time" "inet.af/netaddr" + "tailscale.com/cmd/tailscaled/childproc" "tailscale.com/control/controlclient" "tailscale.com/envknob" "tailscale.com/ipn" @@ -105,6 +106,7 @@ var subCommands = map[string]*func([]string) error{ "install-system-daemon": &installSystemDaemon, "uninstall-system-daemon": &uninstallSystemDaemon, "debug": &debugModeFunc, + "be-child": &beChildFunc, } func main() { @@ -601,3 +603,17 @@ 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") + } + typ := args[0] + f, ok := childproc.Code[typ] + if !ok { + return fmt.Errorf("unknown be-child mode %q", typ) + } + return f(args[1:]) +} diff --git a/ssh/tailssh/tailssh.go b/ssh/tailssh/tailssh.go index 15ca13133..4aaaeafdb 100644 --- a/ssh/tailssh/tailssh.go +++ b/ssh/tailssh/tailssh.go @@ -27,6 +27,7 @@ import ( "github.com/creack/pty" "github.com/gliderlabs/ssh" "inet.af/netaddr" + "tailscale.com/cmd/tailscaled/childproc" "tailscale.com/envknob" "tailscale.com/ipn/ipnlocal" "tailscale.com/net/tsaddr" @@ -34,6 +35,15 @@ import ( "tailscale.com/types/logger" ) +func init() { + childproc.Add("ssh", sshChild) +} + +func sshChild([]string) error { + fmt.Println("TODO(maisem): ssh dbus stuff") + return nil +} + // TODO(bradfitz): this is all very temporary as code is temporarily // being moved around; it will be restructured and documented in // following commits. diff --git a/tstest/integration/tailscaled_deps_test_darwin.go b/tstest/integration/tailscaled_deps_test_darwin.go index 577a66937..3bccd21d2 100644 --- a/tstest/integration/tailscaled_deps_test_darwin.go +++ b/tstest/integration/tailscaled_deps_test_darwin.go @@ -13,6 +13,7 @@ import ( // process and can cache a prior success when a dependency changes. _ "inet.af/netaddr" _ "tailscale.com/chirp" + _ "tailscale.com/cmd/tailscaled/childproc" _ "tailscale.com/control/controlclient" _ "tailscale.com/derp/derphttp" _ "tailscale.com/envknob" diff --git a/tstest/integration/tailscaled_deps_test_freebsd.go b/tstest/integration/tailscaled_deps_test_freebsd.go index 577a66937..3bccd21d2 100644 --- a/tstest/integration/tailscaled_deps_test_freebsd.go +++ b/tstest/integration/tailscaled_deps_test_freebsd.go @@ -13,6 +13,7 @@ import ( // process and can cache a prior success when a dependency changes. _ "inet.af/netaddr" _ "tailscale.com/chirp" + _ "tailscale.com/cmd/tailscaled/childproc" _ "tailscale.com/control/controlclient" _ "tailscale.com/derp/derphttp" _ "tailscale.com/envknob" diff --git a/tstest/integration/tailscaled_deps_test_linux.go b/tstest/integration/tailscaled_deps_test_linux.go index 577a66937..3bccd21d2 100644 --- a/tstest/integration/tailscaled_deps_test_linux.go +++ b/tstest/integration/tailscaled_deps_test_linux.go @@ -13,6 +13,7 @@ import ( // process and can cache a prior success when a dependency changes. _ "inet.af/netaddr" _ "tailscale.com/chirp" + _ "tailscale.com/cmd/tailscaled/childproc" _ "tailscale.com/control/controlclient" _ "tailscale.com/derp/derphttp" _ "tailscale.com/envknob" diff --git a/tstest/integration/tailscaled_deps_test_openbsd.go b/tstest/integration/tailscaled_deps_test_openbsd.go index 577a66937..3bccd21d2 100644 --- a/tstest/integration/tailscaled_deps_test_openbsd.go +++ b/tstest/integration/tailscaled_deps_test_openbsd.go @@ -13,6 +13,7 @@ import ( // process and can cache a prior success when a dependency changes. _ "inet.af/netaddr" _ "tailscale.com/chirp" + _ "tailscale.com/cmd/tailscaled/childproc" _ "tailscale.com/control/controlclient" _ "tailscale.com/derp/derphttp" _ "tailscale.com/envknob" diff --git a/tstest/integration/tailscaled_deps_test_windows.go b/tstest/integration/tailscaled_deps_test_windows.go index 5a938122d..7586e832a 100644 --- a/tstest/integration/tailscaled_deps_test_windows.go +++ b/tstest/integration/tailscaled_deps_test_windows.go @@ -16,6 +16,7 @@ import ( _ "golang.org/x/sys/windows/svc/mgr" _ "golang.zx2c4.com/wireguard/windows/tunnel/winipcfg" _ "inet.af/netaddr" + _ "tailscale.com/cmd/tailscaled/childproc" _ "tailscale.com/control/controlclient" _ "tailscale.com/derp/derphttp" _ "tailscale.com/envknob"