packetconns.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. Copyright 2014 CoreOS Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package activation
  14. import (
  15. "net"
  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(unsetEnv bool) ([]net.PacketConn, error) {
  24. files := Files(unsetEnv)
  25. conns := make([]net.PacketConn, 0)
  26. for i := 0; i < len(files); i++ {
  27. if pc, err := net.FilePacketConn(files[i]); err == nil {
  28. conns = append(conns, pc)
  29. continue
  30. } else {
  31. conns = append(conns, nil)
  32. }
  33. }
  34. return conns, nil
  35. }