From daffefdb10f5893d3beccee528d537d9c35d0574 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@musicpd.org>
Date: Tue, 30 Oct 2018 20:05:57 +0100
Subject: [PATCH] event/ServerSocket: pass UniqueSocketDescriptor to AddFD()

---
 src/Listen.cxx             |  3 ++-
 src/event/ServerSocket.cxx | 14 ++++++--------
 src/event/ServerSocket.hxx |  2 +-
 3 files changed, 9 insertions(+), 10 deletions(-)

diff --git a/src/Listen.cxx b/src/Listen.cxx
index 1dc97af2a..3b1893b4f 100644
--- a/src/Listen.cxx
+++ b/src/Listen.cxx
@@ -24,6 +24,7 @@
 #include "config/Data.hxx"
 #include "config/Option.hxx"
 #include "config/Net.hxx"
+#include "net/UniqueSocketDescriptor.hxx"
 #include "system/Error.hxx"
 #include "util/RuntimeError.hxx"
 #include "fs/AllocatedPath.hxx"
@@ -66,7 +67,7 @@ listen_systemd_activation(ClientListener &listener)
 
 	for (int i = SD_LISTEN_FDS_START, end = SD_LISTEN_FDS_START + n;
 	     i != end; ++i)
-		listener.AddFD(i);
+		listener.AddFD(UniqueSocketDescriptor(i));
 
 	return true;
 }
diff --git a/src/event/ServerSocket.cxx b/src/event/ServerSocket.cxx
index 55b154f44..db4679cf0 100644
--- a/src/event/ServerSocket.cxx
+++ b/src/event/ServerSocket.cxx
@@ -107,8 +107,8 @@ public:
 		return ::ToString(address);
 	}
 
-	void SetFD(SocketDescriptor _fd) noexcept {
-		SocketMonitor::Open(_fd);
+	void SetFD(UniqueSocketDescriptor _fd) noexcept {
+		SocketMonitor::Open(_fd.Release());
 		SocketMonitor::ScheduleRead();
 	}
 
@@ -194,7 +194,7 @@ OneServerSocket::Open()
 
 	/* register in the EventLoop */	
 
-	SetFD(_fd.Release());
+	SetFD(std::move(_fd));
 }
 
 ServerSocket::ServerSocket(EventLoop &_loop) noexcept
@@ -291,18 +291,16 @@ ServerSocket::AddAddress(AllocatedSocketAddress &&address) noexcept
 }
 
 void
-ServerSocket::AddFD(int _fd)
+ServerSocket::AddFD(UniqueSocketDescriptor fd)
 {
-	assert(_fd >= 0);
-
-	SocketDescriptor fd(_fd);
+	assert(fd.IsDefined());
 
 	StaticSocketAddress address = fd.GetLocalAddress();
 	if (!address.IsDefined())
 		throw MakeSocketError("Failed to get socket address");
 
 	OneServerSocket &s = AddAddress(address);
-	s.SetFD(fd);
+	s.SetFD(std::move(fd));
 }
 
 #ifdef HAVE_TCP
diff --git a/src/event/ServerSocket.hxx b/src/event/ServerSocket.hxx
index fc1ff7510..201d0ae2f 100644
--- a/src/event/ServerSocket.hxx
+++ b/src/event/ServerSocket.hxx
@@ -107,7 +107,7 @@ public:
 	 *
 	 * Throws #std::runtime_error on error.
 	 */
-	void AddFD(int fd);
+	void AddFD(UniqueSocketDescriptor fd);
 
 	bool IsEmpty() const noexcept {
 		return sockets.empty();