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