[Nbd] [PATCH] nbd-server: fix server socket initialization
- To: nbd-general@lists.sourceforge.net
- Subject: [Nbd] [PATCH] nbd-server: fix server socket initialization
- From: =?UTF-8?q?Tuomas=20Jorma=20Juhani=20R=C3=A4s=C3=A4nen?= <tuomasjjrasanen@...1261...>
- Date: Fri, 28 Dec 2012 00:22:41 +0200
- Message-id: <20121227222241.GA2281@...1259...>
Change the value for undefined descriptor from 0 to -1 because any value
greater or equal to 0 can be a valid file descriptor.
Signed-off-by: Tuomas Jorma Juhani Räsänen <tuomasjjrasanen@...1261...>
---
nbd-server.c | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)
diff --git a/nbd-server.c b/nbd-server.c
index f5aa6ca..ec617a9 100644
--- a/nbd-server.c
+++ b/nbd-server.c
@@ -2268,7 +2268,7 @@ int serveloop(GArray* servers) {
max=0;
FD_ZERO(&mset);
for(i=0;i<servers->len;i++) {
- if((sock=(g_array_index(servers, SERVER, i)).socket)) {
+ if((sock=(g_array_index(servers, SERVER, i)).socket) >= 0) {
FD_SET(sock, &mset);
max=sock>max?sock:max;
}
@@ -2358,6 +2358,22 @@ int setup_serve(SERVER *serve) {
gchar *port = NULL;
int e;
+ /* Without this, it's possible that socket == 0, even if it's
+ * not initialized at all. And that would be wrong because 0 is
+ * totally legal value for properly initialized descriptor. This
+ * line is required to ensure that unused/uninitialized
+ * descriptors are marked as such (new style configuration
+ * case). Currently, servers are being initialized in multiple
+ * places, and some of the them do the socket initialization
+ * incorrectly. This is the only point common to all code paths,
+ * and therefore setting -1 is put here. However, the whole
+ * server initialization procedure should be extracted to its
+ * own function and all code paths wanting to mess with servers
+ * should initialize servers with that function.
+ *
+ * TODO: fix server initialization */
+ serve->socket = -1;
+
if(!(glob_flags & F_OLDSTYLE)) {
return serve->servename ? 1 : 0;
}
--
1.7.10.4
Reply to: