-
Notifications
You must be signed in to change notification settings - Fork 0
/
launchd.go
59 lines (50 loc) · 1.41 KB
/
launchd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// +build darwin
package main
// #include <launch.h>
// #include <stdlib.h>
// #include <errno.h>
import "C"
import (
"errors"
"fmt"
"net"
"os"
"unsafe"
)
func bootstrap() (net.PacketConn, net.Listener, error) {
udpFile, err := serviceFilePointer("UdpListener")
if err != nil {
return nil, nil, err
}
udp, err := net.FilePacketConn(udpFile)
tcpFile, err := serviceFilePointer("TcpListener")
if err != nil {
return nil, nil, err
}
tcp, err := net.FileListener(tcpFile)
return udp, tcp, err
}
func serviceFilePointer(name string) (*os.File, error) {
var fdCount C.size_t
var fds *C.int
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
eCode := int(C.launch_activate_socket(cName, &fds, &fdCount))
if eCode == C.ENOENT {
return nil, errors.New("OS socket activation failed with ENOENT")
} else if eCode == C.ESRCH {
return nil, errors.New("OS socket activation failed with ESRCH")
} else if eCode == C.EALREADY {
return nil, errors.New("OS socket activation failed with EALREADY")
} else if eCode != 0 {
return nil, fmt.Errorf("OS socket activation failed with unknown error %d", eCode)
}
defer C.free(unsafe.Pointer(fds))
if fdCount > 1 {
return nil, errors.New("multiple fds returned; I don't know how to deal with that")
} else if fdCount == 0 {
return nil, errors.New("unable to get FD for service " + name)
}
fdPtr := (uintptr)(*fds)
return os.NewFile(fdPtr, name), nil
}