mirror of
https://github.com/zrepl/zrepl.git
synced 2024-11-22 16:34:32 +01:00
c69ebd3806
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
50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
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)
|
|
}
|