From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:45544) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ShHUx-0004jV-A2 for qemu-devel@nongnu.org; Wed, 20 Jun 2012 05:46:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ShHUr-0003gH-7I for qemu-devel@nongnu.org; Wed, 20 Jun 2012 05:46:50 -0400 Received: from e35.co.us.ibm.com ([32.97.110.153]:38572) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ShHUr-0003fs-0f for qemu-devel@nongnu.org; Wed, 20 Jun 2012 05:46:45 -0400 Received: from /spool/local by e35.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 20 Jun 2012 03:46:43 -0600 Received: from d01relay06.pok.ibm.com (d01relay06.pok.ibm.com [9.56.227.116]) by d01dlp03.pok.ibm.com (Postfix) with ESMTP id D3FBCC9005C for ; Wed, 20 Jun 2012 05:46:39 -0400 (EDT) Received: from d01av03.pok.ibm.com (d01av03.pok.ibm.com [9.56.224.217]) by d01relay06.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q5K9ke5i33554480 for ; Wed, 20 Jun 2012 05:46:40 -0400 Received: from d01av03.pok.ibm.com (loopback [127.0.0.1]) by d01av03.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q5K9kebZ020394 for ; Wed, 20 Jun 2012 06:46:40 -0300 From: zwu.kernel@gmail.com Date: Wed, 20 Jun 2012 17:46:22 +0800 Message-Id: <1340185582-30955-4-git-send-email-zwu.kernel@gmail.com> In-Reply-To: <1340185582-30955-1-git-send-email-zwu.kernel@gmail.com> References: <1340185582-30955-1-git-send-email-zwu.kernel@gmail.com> Subject: [Qemu-devel] [PATCH v2 3/3] net: complete NetSocketState lifecycle handling List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: aliguori@us.ibm.com, Zhi Yong Wu , stefanha@linux.vnet.ibm.com From: Zhi Yong Wu 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 --- net/socket.c | 20 +++++++++++++++++--- 1 files changed, 17 insertions(+), 3 deletions(-) diff --git a/net/socket.c b/net/socket.c index e61e346..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); @@ -445,6 +458,7 @@ static int net_socket_listen_init(NetClientState *peer, 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