From patchwork Tue Jun 12 14:04:12 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 164421 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 35966B700C for ; Wed, 13 Jun 2012 00:06:14 +1000 (EST) Received: from localhost ([::1]:50171 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SeRjX-0004A1-Ti for incoming@patchwork.ozlabs.org; Tue, 12 Jun 2012 10:06:11 -0400 Received: from eggs.gnu.org ([208.118.235.92]:53858) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SeRjK-00049c-O5 for qemu-devel@nongnu.org; Tue, 12 Jun 2012 10:06:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SeRjA-0001Ez-O4 for qemu-devel@nongnu.org; Tue, 12 Jun 2012 10:05:58 -0400 Received: from e06smtp16.uk.ibm.com ([195.75.94.112]:43558) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SeRjA-0001DQ-Fl for qemu-devel@nongnu.org; Tue, 12 Jun 2012 10:05:48 -0400 Received: from /spool/local by e06smtp16.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 12 Jun 2012 15:05:43 +0100 Received: from d06nrmr1407.portsmouth.uk.ibm.com (9.149.38.185) by e06smtp16.uk.ibm.com (192.168.101.146) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 12 Jun 2012 15:05:40 +0100 Received: from d06av10.portsmouth.uk.ibm.com (d06av10.portsmouth.uk.ibm.com [9.149.37.251]) by d06nrmr1407.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q5CE5dKR2326720 for ; Tue, 12 Jun 2012 15:05:39 +0100 Received: from d06av10.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av10.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q5CDoOCb015060 for ; Tue, 12 Jun 2012 09:50:25 -0400 Received: from localhost (stefanha-thinkpad.manchester-maybrook.uk.ibm.com [9.174.219.145]) by d06av10.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id q5CDoOHJ014713; Tue, 12 Jun 2012 09:50:24 -0400 From: Stefan Hajnoczi To: Date: Tue, 12 Jun 2012 15:04:12 +0100 Message-Id: <1339509852-16213-1-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.10 In-Reply-To: <1339463152-21193-1-git-send-email-zwu.kernel@gmail.com> References: <1339463152-21193-1-git-send-email-zwu.kernel@gmail.com> x-cbid: 12061214-3548-0000-0000-000002333A6D X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 195.75.94.112 Cc: Paolo Bonzini , Stefan Hajnoczi , Zhi Yong Wu Subject: [Qemu-devel] [PATCH] 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 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 Reviewed-by: Zhi Yong Wu --- This patch adds additional fixes on top of Zhi Yong's first 2 patches. With this patch in place I'm happy with this series. net/socket.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/net/socket.c b/net/socket.c index f67de47..403221c 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(VLANClientState *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(VLANState *vlan, 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(VLANState *vlan, 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(VLANState *vlan, nc = qemu_new_net_client(&net_socket_info, vlan, NULL, model, name); s = DO_UPCAST(NetSocketState, nc, nc); + s->fd = -1; s->listen_fd = fd; s->nc.link_down = true;