summaryrefslogtreecommitdiffhomepage
path: root/uapi_darwin.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-05-03 04:49:35 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2018-05-03 14:55:10 +0200
commit258a9223b9ab18a973c44b238e029a0dc5640102 (patch)
tree5207280a53e690ed09777adabee95d586ac85393 /uapi_darwin.go
parent168ef61a638e4875b260edbc51551bae0dc34ac3 (diff)
Start to dust off Darwin
Diffstat (limited to 'uapi_darwin.go')
-rw-r--r--uapi_darwin.go79
1 files changed, 64 insertions, 15 deletions
diff --git a/uapi_darwin.go b/uapi_darwin.go
index 2850184..4cb4e62 100644
--- a/uapi_darwin.go
+++ b/uapi_darwin.go
@@ -1,6 +1,7 @@
package main
import (
+ "errors"
"fmt"
"golang.org/x/sys/unix"
"net"
@@ -44,23 +45,11 @@ func (l *UAPIListener) Addr() net.Addr {
return nil
}
-func NewUAPIListener(name string) (net.Listener, error) {
+func UAPIListen(name string, file *os.File) (net.Listener, error) {
- // check if path exist
-
- err := os.MkdirAll(socketDirectory, 077)
- if err != nil && !os.IsExist(err) {
- return nil, err
- }
-
- // open UNIX socket
+ // wrap file in listener
- socketPath := path.Join(
- socketDirectory,
- fmt.Sprintf(socketName, name),
- )
-
- listener, err := net.Listen("unix", socketPath)
+ listener, err := net.FileListener(file)
if err != nil {
return nil, err
}
@@ -73,6 +62,13 @@ func NewUAPIListener(name string) (net.Listener, error) {
// watch for deletion of socket
+ socketPath := path.Join(
+ socketDirectory,
+ fmt.Sprintf(socketName, name),
+ )
+
+ // watch for deletion of socket
+
go func(l *UAPIListener) {
for ; ; time.Sleep(time.Second) {
if _, err := os.Stat(socketPath); os.IsNotExist(err) {
@@ -97,3 +93,56 @@ func NewUAPIListener(name string) (net.Listener, error) {
return uapi, nil
}
+
+func UAPIOpen(name string) (*os.File, error) {
+
+ // check if path exist
+
+ err := os.MkdirAll(socketDirectory, 0600)
+ if err != nil && !os.IsExist(err) {
+ 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
+ }
+
+ 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)
+ }()
+
+ if err != nil {
+ return nil, err
+ }
+
+ return listener.File()
+}