From 60fee87ba16ba08618e28db1a3a696646d418e1b Mon Sep 17 00:00:00 2001 From: Aleksander Mistewicz Date: Fri, 16 Feb 2018 17:42:45 +0100 Subject: [PATCH] Add systemd integration to stm executable stm binary is intended to be started in a server mode as a service. It is possible for systemd to wait for a connection on a socket and start a service only when such connection is established. Change-Id: Ica5374312679f8935f2b896e142c5ea63a76728c --- sw/nanopi/cmd/stm/server.go | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/sw/nanopi/cmd/stm/server.go b/sw/nanopi/cmd/stm/server.go index 6b3bf16..d8c7764 100644 --- a/sw/nanopi/cmd/stm/server.go +++ b/sw/nanopi/cmd/stm/server.go @@ -24,6 +24,7 @@ import ( "os/signal" "git.tizen.org/tools/muxpi/sw/nanopi/stm" + "github.com/coreos/go-systemd/activation" ) func registerUserServiceOnListener(impl stm.UserInterface, l net.Listener) { @@ -57,13 +58,31 @@ func registerService(i stm.Interface, path string) net.Listener { } func serveRemoteSTM(dev stm.Interface) { - if userServiceSocket != "" { - l := registerUserService(dev.(stm.UserInterface), userServiceSocket) - defer l.Close() - } - if serviceSocket != "" { - l := registerService(dev, serviceSocket) - defer l.Close() + listeners, err := activation.Listeners() + if err != nil { + if userServiceSocket != "" { + l := registerUserService(dev.(stm.UserInterface), userServiceSocket) + defer l.Close() + } + if serviceSocket != "" { + l := registerService(dev, serviceSocket) + defer l.Close() + } + } else if len(listeners) == 2 { + for _, l := range listeners { + name := l.Addr().String() + switch { + case serviceSocket != "" && name == serviceSocket: + registerServiceOnListener(dev, l) + case userServiceSocket != "" && name == userServiceSocket: + registerUserServiceOnListener(dev.(stm.UserInterface), l) + default: + log.Fatal("unknown fd name: ", name) + } + defer l.Close() + } + } else { + log.Fatal("unexpected number of file descriptors passed: ", len(listeners)) } // Wait for interrupt. -- 2.7.4