Skip to content

Commit

Permalink
Create socket dir on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
dsh2dsh committed Dec 25, 2024
1 parent 7890777 commit ca9af4c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
8 changes: 3 additions & 5 deletions dist/freebsd/etc/rc.d/zrepl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ load_rc_config "$name"
: "${zrepl_gracefully:=NO}"

: "${zrepl_control_group:=$zrepl_group}"
: "${zrepl_control_mode:=0700}"
: "${zrepl_control_mode:=0750}"

pidfile="/var/run/zrepl.pid"
command="/usr/sbin/daemon"
Expand All @@ -71,10 +71,8 @@ zrepl_checkconfig() {
}

zrepl_precmd() {
if [ ! -d "/var/run/zrepl" ]; then
install -d -g "$zrepl_control_group" -o "$zrepl_user" \
-m "$zrepl_control_mode" "/var/run/zrepl"
fi
install -d -g "$zrepl_control_group" -o "$zrepl_user" \
-m "$zrepl_control_mode" "/var/run/zrepl"
zrepl_checkconfig
}

Expand Down
41 changes: 39 additions & 2 deletions internal/daemon/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"net/http"
"os"
"path/filepath"
"sync"
)

Expand All @@ -32,15 +33,24 @@ func (self *server) Clone() *server {
}

func (self *server) WithUnix(path string, mode uint32) error {
if err := unlinkStaleUnix(path); err != nil {
return err
}

self.Addr = path
laddr, err := net.ResolveUnixAddr("unix", path)
if err != nil {
return fmt.Errorf("resolve unix address %q: %w", path, err)
}

if self.listener, err = net.ListenUnix("unix", laddr); err != nil {
l, err := net.ListenUnix("unix", laddr)
if err != nil {
return fmt.Errorf("listen unix on %q: %w", path, err)
} else if mode == 0 {
}

l.SetUnlinkOnClose(true)
self.listener = l
if mode == 0 {
return nil
}

Expand All @@ -50,6 +60,33 @@ func (self *server) WithUnix(path string, mode uint32) error {
return nil
}

func unlinkStaleUnix(path string) error {
sockdir := filepath.Dir(path)
stat, err := os.Stat(sockdir)
switch {
case err != nil && os.IsNotExist(err):
if err := os.MkdirAll(sockdir, 0o755); err != nil {
return fmt.Errorf("cannot mkdir %q: %w", sockdir, err)
}
return nil
case err != nil:
return fmt.Errorf("cannot stat(2) %q: %w", sockdir, err)
case !stat.IsDir():
return fmt.Errorf("not a directory: %q", sockdir)
}

_, err = os.Stat(path)
switch {
case err == nil:
if err := os.Remove(path); err != nil {
return fmt.Errorf("cannot remove stale socket %q: %w", path, err)
}
case !os.IsNotExist(err):
return fmt.Errorf("cannot stat(2) %q: %w", path, err)
}
return nil
}

//nolint:wrapcheck // not needed
func (self *server) Serve() error {
self.initTLSConfig()
Expand Down

0 comments on commit ca9af4c

Please sign in to comment.