mirror of
https://github.com/zrepl/zrepl.git
synced 2025-08-08 23:04:46 +02:00
WIP rewrite the daemon
cmd subdir does not build on purpose, it's only left in tree to grab old code and move it to github.com/zrepl/zrepl/daemon
This commit is contained in:
49
daemon/nethelpers/helpers.go
Normal file
49
daemon/nethelpers/helpers.go
Normal file
@ -0,0 +1,49 @@
|
||||
package nethelpers
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func PreparePrivateSockpath(sockpath string) error {
|
||||
sockdir := filepath.Dir(sockpath)
|
||||
sdstat, err := os.Stat(sockdir)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "cannot stat(2) '%s'", sockdir)
|
||||
}
|
||||
if !sdstat.IsDir() {
|
||||
return errors.Errorf("not a directory: %s", sockdir)
|
||||
}
|
||||
p := sdstat.Mode().Perm()
|
||||
if p&0007 != 0 {
|
||||
return errors.Errorf("socket directory must not be world-accessible: %s (permissions are %#o)", sockdir, p)
|
||||
}
|
||||
|
||||
// Maybe things have not been cleaned up before
|
||||
s, err := os.Stat(sockpath)
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "cannot stat(2) '%s'", sockpath)
|
||||
}
|
||||
if s.Mode()&os.ModeSocket == 0 {
|
||||
return errors.Errorf("unexpected file type at path '%s'", sockpath)
|
||||
}
|
||||
err = os.Remove(sockpath)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "cannot remove presumably stale socket '%s'", sockpath)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ListenUnixPrivate(sockaddr *net.UnixAddr) (*net.UnixListener, error) {
|
||||
|
||||
if err := PreparePrivateSockpath(sockaddr.Name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return net.ListenUnix("unix", sockaddr)
|
||||
}
|
Reference in New Issue
Block a user