]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
Remove stale unix socket
authorMathias Hall-Andersen <mathias@hall-andersen.dk>
Tue, 1 Aug 2017 10:45:11 +0000 (12:45 +0200)
committerMathias Hall-Andersen <mathias@hall-andersen.dk>
Tue, 1 Aug 2017 10:45:11 +0000 (12:45 +0200)
src/uapi_linux.go

index 1055dca581f8ee8e121d4da38f8af5509029ce0e..17c5a9d7b46c44487655ef999a0ca5c62fc47c9e 100644 (file)
@@ -1,6 +1,7 @@
 package main
 
 import (
+       "errors"
        "fmt"
        "golang.org/x/sys/unix"
        "net"
@@ -48,12 +49,38 @@ func (l *UAPIListener) Addr() net.Addr {
        return nil
 }
 
+func connectUnixSocket(path string) (net.Listener, error) {
+
+       // attempt inital connection
+
+       listener, err := net.Listen("unix", path)
+       if err == nil {
+               return listener, nil
+       }
+
+       // check if active
+
+       _, err = net.Dial("unix", path)
+       if err == nil {
+               return nil, errors.New("Unix socket in use")
+       }
+
+       // attempt cleanup
+
+       err = os.Remove(path)
+       if err != nil {
+               return nil, err
+       }
+
+       return net.Listen("unix", path)
+}
+
 func NewUAPIListener(name string) (net.Listener, error) {
 
        // open UNIX socket
 
        socketPath := fmt.Sprintf("/var/run/wireguard/%s.sock", name)
-       listener, err := net.Listen("unix", socketPath)
+       listener, err := connectUnixSocket(socketPath)
        if err != nil {
                return nil, err
        }