All of lore.kernel.org
 help / color / mirror / Atom feed
From: zwu.kernel@gmail.com
To: qemu-devel@nongnu.org
Cc: pbonzini@redhat.com, aliguori@us.ibm.com,
	Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>,
	stefanha@linux.vnet.ibm.com
Subject: [Qemu-devel] [PATCH 3/3] net: complete NetSocketState lifecycle handling
Date: Tue, 19 Jun 2012 01:13:52 +0800	[thread overview]
Message-ID: <1340039632-28562-5-git-send-email-zwu.kernel@gmail.com> (raw)
In-Reply-To: <1340039632-28562-1-git-send-email-zwu.kernel@gmail.com>

From: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>

The NetSocketState struct contains two file descriptors: an active
connection and a listen socket for new connections.  It's important that
we clean up after ourselves so these file descriptors are initialized to
-1 when unused.  This allows makes it possible to call cleanup functions
only when the file descriptors are valid (not -1).

The specific issue solved by this patch is that we avoid calling
close(-1), close(0), and qemu_set_fd_handler(-1, net_socket_accept,
NULL, s).  All of these are either problematic or unclean (e.g. reported
as warnings by Valgrind).

Also stay consistent by bringing the link down when the active
connection is closed.

Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
---
 net/socket.c |   24 +++++++++++++++++++-----
 1 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/net/socket.c b/net/socket.c
index 408d00e..9b15479 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -82,13 +82,16 @@ static void net_socket_send(void *opaque)
         /* end of connection */
     eoc:
         qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
-        qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
+        if (s->listen_fd != -1) {
+            qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
+        }
         closesocket(s->fd);
 
         s->fd = -1;
         s->state = 0;
         s->index = 0;
         s->packet_len = 0;
+        s->nc.link_down = true;
         memset(s->buf, 0, sizeof(s->buf));
         memset(s->nc.info_str, 0, sizeof(s->nc.info_str));
 
@@ -239,8 +242,16 @@ fail:
 static void net_socket_cleanup(NetClientState *nc)
 {
     NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
-    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
-    close(s->fd);
+    if (s->fd != -1) {
+        qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
+        close(s->fd);
+        s->fd = -1;
+    }
+    if (s->listen_fd != -1) {
+        qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
+        closesocket(s->listen_fd);
+        s->listen_fd = -1;
+    }
 }
 
 static NetClientInfo net_dgram_socket_info = {
@@ -302,6 +313,7 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
     s = DO_UPCAST(NetSocketState, nc, nc);
 
     s->fd = fd;
+    s->listen_fd = -1;
 
     qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
 
@@ -345,6 +357,7 @@ static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
     s = DO_UPCAST(NetSocketState, nc, nc);
 
     s->fd = fd;
+    s->listen_fd = -1;
 
     if (is_connected) {
         net_socket_connect(s);
@@ -411,7 +424,7 @@ static int net_socket_listen_init(NetClientState *peer,
                                   const char *name,
                                   const char *host_str)
 {
-    VLANClientState *nc;
+    NetClientState *nc;
     NetSocketState *s;
     struct sockaddr_in saddr;
     int fd, val, ret;
@@ -443,8 +456,9 @@ static int net_socket_listen_init(NetClientState *peer,
         return -1;
     }
 
-    nc = qemu_new_net_client(&net_socket_info, vlan, NULL, model, name);
+    nc = qemu_new_net_client(&net_socket_info, peer, model, name);
     s = DO_UPCAST(NetSocketState, nc, nc);
+    s->fd = -1;
     s->listen_fd = fd;
     s->nc.link_down = true;
 
-- 
1.7.6

      parent reply	other threads:[~2012-06-18 17:15 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-18 17:13 [Qemu-devel] [PATCH 0/3] Some socket fix patches zwu.kernel
2012-06-18 17:13 ` [Qemu-devel] [PATCH 1/3] net: fix the coding style zwu.kernel
2012-06-18 17:13 ` [Qemu-devel] [PATCH 2/3] net: add the support for -netdev socket, listen zwu.kernel
2012-06-18 17:13 ` [Qemu-devel] [PATCH 3/3] net: complete NetSocketS?tate lifecycle handling zwu.kernel
2012-06-18 17:16   ` Zhi Yong Wu
2012-06-18 17:13 ` zwu.kernel [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1340039632-28562-5-git-send-email-zwu.kernel@gmail.com \
    --to=zwu.kernel@gmail.com \
    --cc=aliguori@us.ibm.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@linux.vnet.ibm.com \
    --cc=wuzhy@linux.vnet.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.