-
Notifications
You must be signed in to change notification settings - Fork 0
/
launchd.go
50 lines (46 loc) · 1.17 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
package launchd
import (
"fmt"
"net"
"os"
"syscall"
)
// Files returns the *os.File for a given socket name
func Files(name string) ([]*os.File, error) {
fds, err := libxpc_launch_activate_socket(name)
if err != nil {
return nil, err
}
files := make([]*os.File, len(fds))
for idx, fd := range fds {
files[idx] = os.NewFile(uintptr(fd), "")
}
return files, nil
}
// Sockets returns the net.Listener for each socket name
func Sockets(name string) ([]net.Listener, error) {
files, err := Files(name)
if err != nil {
return nil, err
}
listeners := make([]net.Listener, len(files))
for idx, file := range files {
listener, err := net.FileListener(file)
if err != nil {
return nil, fmt.Errorf("net.FileListener for %d failed: %w", file.Fd(), err)
}
listeners[idx] = listener
}
return listeners, nil
}
// Activates a single net.Listener with the given socket name
// If anything other than a single file descriptor is available syscall.EINVAL is returned
func Activate(name string) (net.Listener, error) {
listeners, err := Sockets(name)
if err != nil {
return nil, err
} else if len(listeners) != 1 {
return nil, syscall.EINVAL
}
return listeners[0], nil
}