From b15efb52014e14132c873f4ccb4a74cdef43d97d Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Sun, 7 Nov 2021 09:55:32 +0000 Subject: [PATCH] Ensure unix socket is removed before we startup --- app.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/app.go b/app.go index c0f43981..89d605cb 100644 --- a/app.go +++ b/app.go @@ -351,6 +351,16 @@ func (h *Headscale) httpAuthenticationMiddleware(c *gin.Context) { // 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. func (h *Headscale) Serve() error { var err error @@ -360,6 +370,11 @@ func (h *Headscale) Serve() error { defer cancel() + err = h.ensureUnixSocketIsAbsent() + if err != nil { + panic(err) + } + socketListener, err := net.Listen("unix", h.cfg.UnixSocket) if err != nil { panic(err)