Ensure unix socket is removed before we startup

This commit is contained in:
Kristoffer Dalby 2021-11-07 09:55:32 +00:00
parent 2dfd42f80c
commit b15efb5201

15
app.go
View file

@ -351,6 +351,16 @@ func (h *Headscale) httpAuthenticationMiddleware(c *gin.Context) {
// c.Next() // c.Next()
} }
// ensureUnixSocketIsAbsent will check if the given path for headscales unix socket is clear
// and will remove it if it is not.
func (h *Headscale) ensureUnixSocketIsAbsent() error {
// File does not exist, all fine
if _, err := os.Stat(h.cfg.UnixSocket); errors.Is(err, os.ErrNotExist) {
return nil
}
return os.Remove(h.cfg.UnixSocket)
}
// Serve launches a GIN server with the Headscale API. // Serve launches a GIN server with the Headscale API.
func (h *Headscale) Serve() error { func (h *Headscale) Serve() error {
var err error var err error
@ -360,6 +370,11 @@ func (h *Headscale) Serve() error {
defer cancel() defer cancel()
err = h.ensureUnixSocketIsAbsent()
if err != nil {
panic(err)
}
socketListener, err := net.Listen("unix", h.cfg.UnixSocket) socketListener, err := net.Listen("unix", h.cfg.UnixSocket)
if err != nil { if err != nil {
panic(err) panic(err)