From patchwork Mon Jun 18 17:13:51 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhiyong Wu X-Patchwork-Id: 165537 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 C75ADB70A2 for ; Tue, 19 Jun 2012 03:15:32 +1000 (EST) Received: from localhost ([::1]:59376 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SgfY2-0003Cx-KP for incoming@patchwork.ozlabs.org; Mon, 18 Jun 2012 13:15:30 -0400 Received: from eggs.gnu.org ([208.118.235.92]:56014) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SgfXD-0001Ks-1x for qemu-devel@nongnu.org; Mon, 18 Jun 2012 13:14:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SgfX6-0005Om-OF for qemu-devel@nongnu.org; Mon, 18 Jun 2012 13:14:38 -0400 Received: from e7.ny.us.ibm.com ([32.97.182.137]:48073) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SgfX6-0005OM-KH for qemu-devel@nongnu.org; Mon, 18 Jun 2012 13:14:32 -0400 Received: from /spool/local by e7.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 18 Jun 2012 13:14:30 -0400 Received: from d01dlp01.pok.ibm.com (9.56.224.56) by e7.ny.us.ibm.com (192.168.1.107) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 18 Jun 2012 13:14:14 -0400 Received: from d01relay05.pok.ibm.com (d01relay05.pok.ibm.com [9.56.227.237]) by d01dlp01.pok.ibm.com (Postfix) with ESMTP id 6580738C805E for ; Mon, 18 Jun 2012 13:14:13 -0400 (EDT) Received: from d01av02.pok.ibm.com (d01av02.pok.ibm.com [9.56.224.216]) by d01relay05.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q5IHEDqo128200 for ; Mon, 18 Jun 2012 13:14:13 -0400 Received: from d01av02.pok.ibm.com (loopback [127.0.0.1]) by d01av02.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q5IHECia020481 for ; Mon, 18 Jun 2012 14:14:13 -0300 Received: from us.ibm.com (f15.cn.ibm.com [9.115.118.27]) by d01av02.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with SMTP id q5IHE86G020204; Mon, 18 Jun 2012 14:14:10 -0300 Received: by us.ibm.com (sSMTP sendmail emulation); Tue, 19 Jun 2012 01:14:08 +0800 From: zwu.kernel@gmail.com To: qemu-devel@nongnu.org Date: Tue, 19 Jun 2012 01:13:51 +0800 Message-Id: <1340039632-28562-4-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-5806-0000-0000-000016574B70 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 32.97.182.137 Cc: pbonzini@redhat.com, aliguori@us.ibm.com, Zhi Yong Wu , stefanha@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH 3/3] net: complete NetSocketS?tate 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;