39 linhas
1 004 B
Go
39 linhas
1 004 B
Go
package main
|
|
|
|
import (
|
|
"net"
|
|
|
|
"github.com/coreos/go-systemd/activation"
|
|
)
|
|
|
|
func Listeners() ([]net.Listener, error) {
|
|
files := activation.Files(false)
|
|
listeners := make([]net.Listener, len(files))
|
|
|
|
for i, f := range files {
|
|
if pc, err := net.FileListener(f); err == nil {
|
|
listeners[i] = pc
|
|
f.Close()
|
|
}
|
|
}
|
|
return listeners, nil
|
|
}
|
|
|
|
// PacketConns returns a slice containing a net.PacketConn for each matching socket type
|
|
// passed to this process.
|
|
//
|
|
// The order of the file descriptors is preserved in the returned slice.
|
|
// Nil values are used to fill any gaps. For example if systemd were to return file descriptors
|
|
// corresponding with "udp, tcp, udp", then the slice would contain {net.PacketConn, nil, net.PacketConn}
|
|
func PacketConns() ([]net.PacketConn, error) {
|
|
files := activation.Files(false)
|
|
conns := make([]net.PacketConn, len(files))
|
|
|
|
for i, f := range files {
|
|
if pc, err := net.FilePacketConn(f); err == nil {
|
|
conns[i] = pc
|
|
f.Close()
|
|
}
|
|
}
|
|
return conns, nil
|
|
}
|