diff options
author | David Crawshaw <crawshaw@tailscale.com> | 2020-05-02 16:28:33 +1000 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-05-02 02:05:41 -0600 |
commit | bc77de2acaebe8589193e621933aa42783926aad (patch) | |
tree | c68f2b153ce679edf542d1e5d109472909efce8e /ipc/uapi_linux.go | |
parent | c8596328e7f769fdd313f981bb3b2de38b40c80c (diff) |
ipc: deduplicate some unix-specific code
Cleans up and splits out UAPIOpen to its own file.
Signed-off-by: David Crawshaw <crawshaw@tailscale.com>
[zx2c4: changed const to var for socketDirectory]
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'ipc/uapi_linux.go')
-rw-r--r-- | ipc/uapi_linux.go | 72 |
1 files changed, 1 insertions, 71 deletions
diff --git a/ipc/uapi_linux.go b/ipc/uapi_linux.go index dce6a45..70d4d0c 100644 --- a/ipc/uapi_linux.go +++ b/ipc/uapi_linux.go @@ -6,26 +6,13 @@ package ipc import ( - "errors" - "fmt" "net" "os" - "path" "golang.org/x/sys/unix" "golang.zx2c4.com/wireguard/rwcancel" ) -var socketDirectory = "/var/run/wireguard" - -const ( - IpcErrorIO = -int64(unix.EIO) - IpcErrorProtocol = -int64(unix.EPROTO) - IpcErrorInvalid = -int64(unix.EINVAL) - IpcErrorPortInUse = -int64(unix.EADDRINUSE) - socketName = "%s.sock" -) - type UAPIListener struct { listener net.Listener // unix socket listener connNew chan net.Conn @@ -84,10 +71,7 @@ func UAPIListen(name string, file *os.File) (net.Listener, error) { // watch for deletion of socket - socketPath := path.Join( - socketDirectory, - fmt.Sprintf(socketName, name), - ) + socketPath := sockPath(name) uapi.inotifyFd, err = unix.InotifyInit() if err != nil { @@ -143,57 +127,3 @@ func UAPIListen(name string, file *os.File) (net.Listener, error) { return uapi, nil } - -func UAPIOpen(name string) (*os.File, error) { - - // check if path exist - - if err := os.MkdirAll(socketDirectory, 0755); err != nil { - return nil, err - } - - // open UNIX socket - - socketPath := path.Join( - socketDirectory, - fmt.Sprintf(socketName, name), - ) - - addr, err := net.ResolveUnixAddr("unix", socketPath) - if err != nil { - return nil, err - } - - oldUmask := unix.Umask(0077) - listener, err := func() (*net.UnixListener, error) { - - // initial connection attempt - - listener, err := net.ListenUnix("unix", addr) - if err == nil { - return listener, nil - } - - // check if socket already active - - _, err = net.Dial("unix", socketPath) - if err == nil { - return nil, errors.New("unix socket in use") - } - - // cleanup & attempt again - - err = os.Remove(socketPath) - if err != nil { - return nil, err - } - return net.ListenUnix("unix", addr) - }() - unix.Umask(oldUmask) - - if err != nil { - return nil, err - } - - return listener.File() -} |