git » dnss » commit 6b732a5

dnsserver: Fix systemd socket initialization

author Alberto Bertogli
2018-07-16 21:53:13 UTC
committer Alberto Bertogli
2018-07-16 21:53:13 UTC
parent 82389c3236487101ef4489c34b6c7bc9a925914e

dnsserver: Fix systemd socket initialization

github.com/coreos/go-systemd/activation made a backwards-incompatible
change, forbidding calls to activation.Listeners and
activation.PacketConns on the same run(calling either is ok, but not
both no matter the order).

dnss needs to call both, because it supports TCP and UDP listening
sockets.

This patch fixes the problem by calling activation.Files, and then
splitting the type of socket ourselves directly.

internal/dnsserver/server.go +10 -10

diff --git a/internal/dnsserver/server.go b/internal/dnsserver/server.go
index d8f13de..76b2b8b 100644
--- a/internal/dnsserver/server.go
+++ b/internal/dnsserver/server.go
@@ -186,16 +186,16 @@ func (s *Server) classicServe() {
 func (s *Server) systemdServe() {
 	// We will usually have at least one TCP socket and one UDP socket.
 	// PacketConns are UDP sockets, Listeners are TCP sockets.
-	// To make things more annoying, both can (and usually will) have nil
-	// entries for the file descriptors that don't match.
-	pconns, err := activation.PacketConns(false)
-	if err != nil {
-		log.Fatalf("Error getting systemd packet conns: %v", err)
-	}
-
-	listeners, err := activation.Listeners(false)
-	if err != nil {
-		log.Fatalf("Error getting systemd listeners: %v", err)
+	pconns := []net.PacketConn{}
+	listeners := []net.Listener{}
+	for _, f := range activation.Files(true) {
+		if lis, err := net.FileListener(f); err == nil {
+			listeners = append(listeners, lis)
+			f.Close()
+		} else if pc, err := net.FilePacketConn(f); err == nil {
+			pconns = append(pconns, pc)
+			f.Close()
+		}
 	}
 
 	var wg sync.WaitGroup