From patchwork Mon Jun 18 17:13:52 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhiyong Wu X-Patchwork-Id: 165538 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 15ADCB70A2 for ; Tue, 19 Jun 2012 03:15:50 +1000 (EST) Received: from localhost ([::1]:32877 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SgfYJ-00049l-T4 for incoming@patchwork.ozlabs.org; Mon, 18 Jun 2012 13:15:47 -0400 Received: from eggs.gnu.org ([208.118.235.92]:43228) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SgfY7-0003uF-D9 for qemu-devel@nongnu.org; Mon, 18 Jun 2012 13:15:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SgfY0-0005w7-Er for qemu-devel@nongnu.org; Mon, 18 Jun 2012 13:15:34 -0400 Received: from e39.co.us.ibm.com ([32.97.110.160]:47266) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SgfY0-0005vi-6K for qemu-devel@nongnu.org; Mon, 18 Jun 2012 13:15:28 -0400 Received: from /spool/local by e39.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 18 Jun 2012 11:15:26 -0600 Received: from d03dlp01.boulder.ibm.com (9.17.202.177) by e39.co.us.ibm.com (192.168.1.139) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 18 Jun 2012 11:15:24 -0600 Received: from d03relay05.boulder.ibm.com (d03relay05.boulder.ibm.com [9.17.195.107]) by d03dlp01.boulder.ibm.com (Postfix) with ESMTP id 7E8BFC400B7 for ; Mon, 18 Jun 2012 17:14:35 +0000 (WET) Received: from d03av01.boulder.ibm.com (d03av01.boulder.ibm.com [9.17.195.167]) by d03relay05.boulder.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q5IHEIHa018764 for ; Mon, 18 Jun 2012 11:14:25 -0600 Received: from d03av01.boulder.ibm.com (loopback [127.0.0.1]) by d03av01.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q5IHEHGH025529 for ; Mon, 18 Jun 2012 11:14:17 -0600 Received: from us.ibm.com (f15.cn.ibm.com [9.115.118.27]) by d03av01.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with SMTP id q5IHEDED025195; Mon, 18 Jun 2012 11:14:15 -0600 Received: by us.ibm.com (sSMTP sendmail emulation); Tue, 19 Jun 2012 01:14:13 +0800 From: zwu.kernel@gmail.com To: qemu-devel@nongnu.org Date: Tue, 19 Jun 2012 01:13:52 +0800 Message-Id: <1340039632-28562-5-git-send-email-zwu.kernel@gmail.com> X-Mailer: git-send-email 1.7.6 In-Reply-To: <1340039632-28562-1-git-send-email-zwu.kernel@gmail.com> References: <1340039632-28562-1-git-send-email-zwu.kernel@gmail.com> X-Content-Scanned: Fidelis XPS MAILER x-cbid: 12061817-4242-0000-0000-00000202DB4F X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 32.97.110.160 Cc: pbonzini@redhat.com, aliguori@us.ibm.com, Zhi Yong Wu , stefanha@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH 3/3] net: complete NetSocketState lifecycle handling X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 | 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;