packetconns.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package activation
  15. import (
  16. "net"
  17. )
  18. // PacketConns returns a slice containing a net.PacketConn for each matching socket type
  19. // passed to this process.
  20. //
  21. // The order of the file descriptors is preserved in the returned slice.
  22. // Nil values are used to fill any gaps. For example if systemd were to return file descriptors
  23. // corresponding with "udp, tcp, udp", then the slice would contain {net.PacketConn, nil, net.PacketConn}
  24. func PacketConns(unsetEnv bool) ([]net.PacketConn, error) {
  25. files := Files(unsetEnv)
  26. conns := make([]net.PacketConn, len(files))
  27. for i, f := range files {
  28. if pc, err := net.FilePacketConn(f); err == nil {
  29. conns[i] = pc
  30. }
  31. }
  32. return conns, nil
  33. }