fix socket path issues

This commit is contained in:
ppom
2023-05-05 15:33:00 +02:00
parent 3283077102
commit a1da122601
6 changed files with 27 additions and 12 deletions

View File

@ -23,12 +23,10 @@ type Response struct {
Actions ReadableMap
}
func SocketPath() string {
return fmt.Sprintf("/run/user/%v/reaction.sock", os.Getuid())
}
const SocketPath = "/run/reaction/reaction.sock"
func SendAndRetrieve(data Request) Response {
conn, err := net.Dial("unix", SocketPath())
conn, err := net.Dial("unix", SocketPath)
if err != nil {
log.Fatalln("Error opening connection top daemon:", err)
}

View File

@ -5,6 +5,7 @@ import (
"log"
"net"
"os"
"path"
"sync"
"gopkg.in/yaml.v3"
@ -110,18 +111,21 @@ func (r ReadableMap) ToString() string {
// Socket-related, server-related functions
func createOpenSocket() net.Listener {
socketPath := SocketPath()
_, err := os.Stat(socketPath)
err := os.MkdirAll(path.Dir(SocketPath), 0755)
if err != nil {
log.Fatalln("FATAL Failed to create socket directory")
}
_, err = os.Stat(SocketPath)
if err == nil {
log.Println("WARN socket", socketPath, "already exists: Is the daemon already running? Deleting.")
err = os.Remove(socketPath)
log.Println("WARN socket", SocketPath, "already exists: Is the daemon already running? Deleting.")
err = os.Remove(SocketPath)
if err != nil {
log.Println("FATAL Failed to remove socket:", err)
log.Fatalln("FATAL Failed to remove socket:", err)
}
}
ln, err := net.Listen("unix", socketPath)
ln, err := net.Listen("unix", SocketPath)
if err != nil {
log.Println("FATAL Failed to create socket:", err)
log.Fatalln("FATAL Failed to create socket:", err)
}
return ln
}

View File

@ -235,7 +235,10 @@ func quit() {
// wait for them to complete
wgActions.Wait()
// delete pipe
os.Remove(SocketPath())
err := os.Remove(SocketPath)
if err != nil {
log.Println("Failed to remove socket:", err)
}
os.Exit(3)
}