-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon_linux.go
37 lines (30 loc) · 864 Bytes
/
daemon_linux.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
package daemon
import (
"net"
"os"
"path/filepath"
"github.com/pkg/errors"
)
func (d *Daemon) createListener() (net.Listener, error) {
var location string
if xdgRuntimeDir := os.Getenv("XDG_RUNTIME_DIR"); xdgRuntimeDir != "" {
location = filepath.Join(xdgRuntimeDir, "trustless", "daemon.sock")
} else {
location = filepath.Join(os.Getenv("HOME"), ".trustless", "daemon.sock")
}
if err := os.MkdirAll(filepath.Dir(location), 0700); err != nil {
return nil, errors.Wrap(err, "Failed to create socket dir")
}
listener, err := net.ListenUnix("unix", &net.UnixAddr{
Net: "unix",
Name: location,
})
if err != nil {
return nil, errors.Wrap(err, "Failed to create listener")
}
listener.SetUnlinkOnClose(true)
if err := os.Chmod(location, 0700); err != nil {
return nil, errors.Wrap(err, "Failed to chmod")
}
return listener, nil
}