From 2a9e942b7888e0f92120836e2d51462806d0003d Mon Sep 17 00:00:00 2001 From: Finn Date: Wed, 9 Jun 2021 03:09:10 -0700 Subject: [PATCH] if socket path is empty, try socket in $XDG_RUNTIME_DIR first --- cmd/signaldctl/config/config.go | 4 +--- signald/signald.go | 30 ++++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/cmd/signaldctl/config/config.go b/cmd/signaldctl/config/config.go index 3a22c7f..319f0a1 100644 --- a/cmd/signaldctl/config/config.go +++ b/cmd/signaldctl/config/config.go @@ -36,9 +36,7 @@ var Path string // Config is the variable the configuration gets loaded into // values shown here are used as defaults -var Config = Configuration{ - SocketPath: "/var/run/signald/signald.sock", -} +var Config = Configuration{} func Load() error { f, err := os.Open(Path) diff --git a/signald/signald.go b/signald/signald.go index e997f3c..2c55a79 100644 --- a/signald/signald.go +++ b/signald/signald.go @@ -31,8 +31,14 @@ import ( "gitlab.com/signald/signald-go/signald/client-protocol/v0" ) +const ( + defaultSocketPrefix = "/var/run" + socketSuffix = "/signald/signald.sock" +) + var ( debugSignaldIO, _ = strconv.ParseBool(os.Getenv("DEBUG_SIGNALD_IO")) + xdgRuntimeDir = os.Getenv("XDG_RUNTIME_DIR") ) func init() { @@ -59,9 +65,29 @@ type UnexpectedError struct { // Connect connects to the signad socket func (s *Signald) Connect() error { - if s.SocketPath == "" { - s.SocketPath = "/var/run/signald/signald.sock" + if s.SocketPath != "" { + return s.connect() } + + s.SocketPath = xdgRuntimeDir + socketSuffix + err := s.connect() + if err != nil { + _, ok := err.(net.Error) + if ok { + s.SocketPath = defaultSocketPrefix + socketSuffix + err = s.connect() + if err != nil { + return err + } + } else { + return err + } + } + + return nil +} + +func (s *Signald) connect() error { socket, err := net.Dial("unix", s.SocketPath) if err != nil { return err