From patchwork Thu Mar 22 03:52:54 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amos Kong X-Patchwork-Id: 148156 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 3453CB6F13 for ; Thu, 22 Mar 2012 14:53:16 +1100 (EST) Received: from localhost ([::1]:56233 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SAZ5O-0002QB-2W for incoming@patchwork.ozlabs.org; Wed, 21 Mar 2012 23:53:14 -0400 Received: from eggs.gnu.org ([208.118.235.92]:37331) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SAZ5B-0002E8-7b for qemu-devel@nongnu.org; Wed, 21 Mar 2012 23:53:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SAZ59-00032S-3k for qemu-devel@nongnu.org; Wed, 21 Mar 2012 23:53:00 -0400 Received: from mx1.redhat.com ([209.132.183.28]:12678) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SAZ58-000328-SC for qemu-devel@nongnu.org; Wed, 21 Mar 2012 23:52:59 -0400 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q2M3quwV009208 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 21 Mar 2012 23:52:56 -0400 Received: from dhcp-8-167.nay.redhat.com (dhcp-8-167.nay.redhat.com [10.66.8.167]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q2M3qqCr005400; Wed, 21 Mar 2012 23:52:53 -0400 To: aliguori@us.ibm.com, kvm@vger.kernel.org, quintela@redhat.com, jasowang@redhat.com, qemu-devel@nongnu.org, mdroth@linux.vnet.ibm.com, owasserm@redhat.com, laine@redhat.com From: Amos Kong Date: Thu, 22 Mar 2012 11:52:54 +0800 Message-ID: <20120322035254.2431.12215.stgit@dhcp-8-167.nay.redhat.com> In-Reply-To: <20120322035052.2431.4994.stgit@dhcp-8-167.nay.redhat.com> References: <20120322035052.2431.4994.stgit@dhcp-8-167.nay.redhat.com> User-Agent: StGit/0.15 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v5 3/4] sockets: pass back errors in inet_listen() 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 Use set_socket_error() to restore real erron, set errno to EINVAL for parse error. Signed-off-by: Amos Kong --- qemu-sockets.c | 21 ++++++++++++++++----- 1 files changed, 16 insertions(+), 5 deletions(-) diff --git a/qemu-sockets.c b/qemu-sockets.c index 908479e..f1c6524 100644 --- a/qemu-sockets.c +++ b/qemu-sockets.c @@ -110,7 +110,7 @@ int inet_listen_opts(QemuOpts *opts, int port_offset) char port[33]; char uaddr[INET6_ADDRSTRLEN+1]; char uport[33]; - int slisten, rc, to, port_min, port_max, p; + int slisten, rc, to, port_min, port_max, p, err; memset(&ai,0, sizeof(ai)); ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; @@ -120,7 +120,8 @@ int inet_listen_opts(QemuOpts *opts, int port_offset) if ((qemu_opt_get(opts, "host") == NULL) || (qemu_opt_get(opts, "port") == NULL)) { fprintf(stderr, "%s: host and/or port not specified\n", __FUNCTION__); - return -1; + err = -EINVAL; + goto err; } pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port")); addr = qemu_opt_get(opts, "host"); @@ -138,7 +139,8 @@ int inet_listen_opts(QemuOpts *opts, int port_offset) if (rc != 0) { fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port, gai_strerror(rc)); - return -1; + err = -EINVAL; + goto err; } /* create socket + bind */ @@ -150,6 +152,7 @@ int inet_listen_opts(QemuOpts *opts, int port_offset) if (slisten < 0) { fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__, inet_strfamily(e->ai_family), strerror(errno)); + err = -socket_error(); continue; } @@ -169,6 +172,7 @@ int inet_listen_opts(QemuOpts *opts, int port_offset) if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) { goto listen; } + err = -socket_error(); if (p == port_max) { fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__, inet_strfamily(e->ai_family), uaddr, inet_getport(e), @@ -179,14 +183,15 @@ int inet_listen_opts(QemuOpts *opts, int port_offset) } fprintf(stderr, "%s: FAILED\n", __FUNCTION__); freeaddrinfo(res); - return -1; + goto err; listen: if (listen(slisten,1) != 0) { + err = -socket_error(); perror("listen"); closesocket(slisten); freeaddrinfo(res); - return -1; + goto err; } snprintf(uport, sizeof(uport), "%d", inet_getport(e) - port_offset); qemu_opt_set(opts, "host", uaddr); @@ -195,6 +200,10 @@ listen: qemu_opt_set(opts, "ipv4", (e->ai_family != PF_INET6) ? "on" : "off"); freeaddrinfo(res); return slisten; + +err: + set_socket_error(-err); + return -1; } int inet_connect_opts(QemuOpts *opts) @@ -474,6 +483,8 @@ int inet_listen(const char *str, char *ostr, int olen, optstr ? optstr : ""); } } + } else { + set_socket_error(EINVAL); } qemu_opts_del(opts); return sock;