From 79bc17afab8c9f84f68bce024fbfd951277d8991 Mon Sep 17 00:00:00 2001 From: networkException Date: Fri, 29 Nov 2024 19:23:32 +0100 Subject: [PATCH] feat: add support for systemd listener activation sockets To support starting a network service on demand and to support a "least privilege-approach" with regards to the permission a network service process needs to have, systemd supports opening a network socket on behalf of the service and passing it as an open file descriptor. The service gets notified about open file descriptors for this purpose as well as metadata such as named listeners via environment variables. This patch adds support for prefixing the listen address passed with --address with "sd-listen-fd:" to access these file descriptors, taking either a listener name passed using the `LISTEN_FDNAMES` environment variable or `LISTEN_FD_$n` for unnamed file descriptiors where `n` is the id of the descriptor starting at 3 (LISTEN_FD_3). See sd_listen_fds(3) --- cmd/root.go | 17 ++++++++++++++++- go.mod | 1 + go.sum | 3 +++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/cmd/root.go b/cmd/root.go index 449e237..adedd09 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -10,6 +10,7 @@ import ( "strings" "syscall" + "github.com/coreos/go-systemd/v22/activation" "github.com/hacdias/webdav/v5/lib" "github.com/spf13/cobra" "go.uber.org/zap" @@ -115,7 +116,21 @@ func getListener(cfg *lib.Config) (net.Listener, error) { network string ) - if strings.HasPrefix(cfg.Address, "unix:") { + if strings.HasPrefix(cfg.Address, "sd-listen-fd:") { + listeners, err := activation.ListenersWithNames() + if err != nil { + return nil, err + } + + address := cfg.Address[13:] + listener, ok := listeners[address] + + if !ok || len(listener) < 1 { + return nil, errors.New("unknown sd-listen-fd address '" + address + "'") + } + + return listener[0], nil + } else if strings.HasPrefix(cfg.Address, "unix:") { address = cfg.Address[5:] network = "unix" } else { diff --git a/go.mod b/go.mod index b6188a9..db2dde9 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/hacdias/webdav/v5 go 1.23 require ( + github.com/coreos/go-systemd/v22 v22.3.2 github.com/go-viper/mapstructure/v2 v2.2.1 github.com/rs/cors v1.11.1 github.com/spf13/cobra v1.8.1 diff --git a/go.sum b/go.sum index 2d3ac85..3fe6dab 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/coreos/go-systemd/v22 v22.3.2 h1:D9/bQk5vlXQFZ6Kwuu6zaiXJ9oTPe68++AzAJc1DzSI= +github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= @@ -8,6 +10,7 @@ github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/ github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss= github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=