summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMathias Hall-Andersen <mathias@hall-andersen.dk>2017-08-01 12:45:11 +0200
committerMathias Hall-Andersen <mathias@hall-andersen.dk>2017-08-01 12:45:11 +0200
commit91c1822473923049755782fcb6d0e101556f4a16 (patch)
tree5fd9b88223dcfa33482fa585084fae98e6f38583
parentb03a6ab1b1ef422d832a5451312ecae1363fa171 (diff)
Remove stale unix socket
-rw-r--r--src/uapi_linux.go29
1 files changed, 28 insertions, 1 deletions
diff --git a/src/uapi_linux.go b/src/uapi_linux.go
index 1055dca..17c5a9d 100644
--- a/src/uapi_linux.go
+++ b/src/uapi_linux.go
@@ -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
}