kubo/cmd/ipfs/sockets/sockets_linux.go
2019-09-27 16:08:30 -07:00

58 lines
1.2 KiB
Go

// +build linux
package sockets
import (
"sync"
activation "github.com/coreos/go-systemd/activation"
logging "github.com/ipfs/go-log"
manet "github.com/multiformats/go-multiaddr-net"
)
var log = logging.Logger("socket-activation")
var (
socketsMu sync.Mutex
sockets map[string][]manet.Listener
)
func initSockets() {
if sockets != nil {
return
}
nlisteners, err := activation.ListenersWithNames()
// Do this before checking the error. We need this to be non-nil so we
// don't try again.
sockets = make(map[string][]manet.Listener, len(nlisteners))
if err != nil {
log.Errorf("error parsing systemd sockets: %s", err)
return
}
for name, nls := range nlisteners {
mls := make([]manet.Listener, 0, len(nls))
for _, nl := range nls {
ml, err := manet.WrapNetListener(nl)
if err != nil {
log.Errorf("error converting a systemd-socket to a multiaddr listener: %s", err)
nl.Close()
continue
}
mls = append(mls, ml)
}
sockets[name] = mls
}
}
// TakeSockets takes the sockets associated with the given name.
func TakeSockets(name string) ([]manet.Listener, error) {
socketsMu.Lock()
defer socketsMu.Unlock()
initSockets()
s := sockets[name]
delete(sockets, name)
return s, nil
}