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 ExecStopPost=/path/to/iptables -w -X reaction
StateDirectory=reaction StateDirectory=reaction
RuntimeDirectory=reaction
WorkingDirectory=/var/lib/reaction WorkingDirectory=/var/lib/reaction
``` ```
See [reaction.service](./config/reaction.service) and [reaction.yml](./config/reaction.yml) for the fully commented examples. 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. 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. 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 ### terminology
- **streams** are commands. they're run and their ouptut is captured. *example:* `tail -f /var/log/nginx/access.log` - **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 Actions ReadableMap
} }
func SocketPath() string { const SocketPath = "/run/reaction/reaction.sock"
return fmt.Sprintf("/run/user/%v/reaction.sock", os.Getuid())
}
func SendAndRetrieve(data Request) Response { func SendAndRetrieve(data Request) Response {
conn, err := net.Dial("unix", SocketPath()) conn, err := net.Dial("unix", SocketPath)
if err != nil { if err != nil {
log.Fatalln("Error opening connection top daemon:", err) log.Fatalln("Error opening connection top daemon:", err)
} }

View File

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

View File

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

View File

@ -2,6 +2,7 @@
[Unit] [Unit]
WantedBy=multi-user.target WantedBy=multi-user.target
# See `man systemd.exec` and `man systemd.service` for most options below
[Service] [Service]
ExecStart=/path/to/reaction -c /etc/reaction.yml 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) # Ask systemd to create /var/lib/reaction (/var/lib/ is implicit)
StateDirectory=reaction StateDirectory=reaction
# Ask systemd to create /run/reaction at runtime (/run/ is implicit)
RuntimeDirectory=reaction
# Start reaction in its state directory # Start reaction in its state directory
WorkingDirectory=/var/lib/reaction 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 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=