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

@ -65,6 +65,7 @@ ExecStopPost=/path/to/iptables -w -F reaction
ExecStopPost=/path/to/iptables -w -X reaction
StateDirectory=reaction
RuntimeDirectory=reaction
WorkingDirectory=/var/lib/reaction
```
See [reaction.service](./config/reaction.service) and [reaction.yml](./config/reaction.yml) for the fully commented examples.
@ -74,6 +75,10 @@ See [reaction.service](./config/reaction.service) and [reaction.yml](./config/re
the working directory of `reaction` will be used to create and read from the embedded database.
if you don't know where to start it, `/var/lib/reaction` should be a sane choice.
### socket
the socket allowing communication between the cli and server will be created at `/run/reaction/reaction.socket`.
### terminology
- **streams** are commands. they're run and their ouptut is captured. *example:* `tail -f /var/log/nginx/access.log`

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)
}

View file

@ -2,6 +2,7 @@
[Unit]
WantedBy=multi-user.target
# See `man systemd.exec` and `man systemd.service` for most options below
[Service]
ExecStart=/path/to/reaction -c /etc/reaction.yml
@ -24,5 +25,7 @@ ExecStopPost=/path/to/iptables -w -X reaction
# Ask systemd to create /var/lib/reaction (/var/lib/ is implicit)
StateDirectory=reaction
# Ask systemd to create /run/reaction at runtime (/run/ is implicit)
RuntimeDirectory=reaction
# Start reaction in its state directory
WorkingDirectory=/var/lib/reaction

2
go.sum
View file

@ -1,2 +1,4 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=