activation.go 1004 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package main
  2. import (
  3. "net"
  4. "github.com/coreos/go-systemd/activation"
  5. )
  6. func Listeners() ([]net.Listener, error) {
  7. files := activation.Files(false)
  8. listeners := make([]net.Listener, len(files))
  9. for i, f := range files {
  10. if pc, err := net.FileListener(f); err == nil {
  11. listeners[i] = pc
  12. f.Close()
  13. }
  14. }
  15. return listeners, nil
  16. }
  17. // PacketConns returns a slice containing a net.PacketConn for each matching socket type
  18. // passed to this process.
  19. //
  20. // The order of the file descriptors is preserved in the returned slice.
  21. // Nil values are used to fill any gaps. For example if systemd were to return file descriptors
  22. // corresponding with "udp, tcp, udp", then the slice would contain {net.PacketConn, nil, net.PacketConn}
  23. func PacketConns() ([]net.PacketConn, error) {
  24. files := activation.Files(false)
  25. conns := make([]net.PacketConn, len(files))
  26. for i, f := range files {
  27. if pc, err := net.FilePacketConn(f); err == nil {
  28. conns[i] = pc
  29. f.Close()
  30. }
  31. }
  32. return conns, nil
  33. }