From patchwork Mon Oct 26 22:34:45 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 536335 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id F19C41412FD for ; Tue, 27 Oct 2015 09:36:01 +1100 (AEDT) Received: from localhost ([::1]:55556 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZqqMx-0004OI-99 for incoming@patchwork.ozlabs.org; Mon, 26 Oct 2015 18:35:59 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54856) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZqqMB-0003Gf-MN for qemu-devel@nongnu.org; Mon, 26 Oct 2015 18:35:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZqqM9-0006yX-Ay for qemu-devel@nongnu.org; Mon, 26 Oct 2015 18:35:11 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52336) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZqqM9-0006y8-0M for qemu-devel@nongnu.org; Mon, 26 Oct 2015 18:35:09 -0400 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id BA82EC0B5917 for ; Mon, 26 Oct 2015 22:35:08 +0000 (UTC) Received: from red.redhat.com (ovpn-113-189.phx2.redhat.com [10.3.113.189]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t9QMZ4De006171; Mon, 26 Oct 2015 18:35:08 -0400 From: Eric Blake To: qemu-devel@nongnu.org Date: Mon, 26 Oct 2015 16:34:45 -0600 Message-Id: <1445898903-12082-7-git-send-email-eblake@redhat.com> In-Reply-To: <1445898903-12082-1-git-send-email-eblake@redhat.com> References: <1445898903-12082-1-git-send-email-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: armbru@redhat.com, Gerd Hoffmann Subject: [Qemu-devel] [PATCH v11 06/24] vnc: Hoist allocation of VncBasicInfo to callers 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 A future qapi patch will rework generated structs with a base class to be unboxed. In preparation for that, change the code that allocates then populates an info struct to instead merely populate the fields of an info field passed in as a parameter (renaming vnc_basic_info_get* to vnc_init_basic_info*). Add rudimentary Error handling at the lowest levels for cases where the old code returned NULL; but rather than plumb Error all the way through the stack, the callers drop the error and return NULL as before. Signed-off-by: Eric Blake --- v11: no change v10: improve commit message (including a retitle), rename functions, tweak error message, don't change semantics of vnc_server_info_get() v9: (no v6-8): hoist from v5 33/46 --- ui/vnc.c | 57 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/ui/vnc.c b/ui/vnc.c index faff054..502a10a 100644 --- a/ui/vnc.c +++ b/ui/vnc.c @@ -156,10 +156,11 @@ char *vnc_socket_remote_addr(const char *format, int fd) { return addr_to_string(format, &sa, salen); } -static VncBasicInfo *vnc_basic_info_get(struct sockaddr_storage *sa, - socklen_t salen) +static void vnc_init_basic_info(struct sockaddr_storage *sa, + socklen_t salen, + VncBasicInfo *info, + Error **errp) { - VncBasicInfo *info; char host[NI_MAXHOST]; char serv[NI_MAXSERV]; int err; @@ -168,42 +169,44 @@ static VncBasicInfo *vnc_basic_info_get(struct sockaddr_storage *sa, host, sizeof(host), serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV)) != 0) { - VNC_DEBUG("Cannot resolve address %d: %s\n", - err, gai_strerror(err)); - return NULL; + error_setg(errp, "Cannot resolve address: %s", + gai_strerror(err)); + return; } - info = g_malloc0(sizeof(VncBasicInfo)); info->host = g_strdup(host); info->service = g_strdup(serv); info->family = inet_netfamily(sa->ss_family); - return info; } -static VncBasicInfo *vnc_basic_info_get_from_server_addr(int fd) +static void vnc_init_basic_info_from_server_addr(int fd, VncBasicInfo *info, + Error **errp) { struct sockaddr_storage sa; socklen_t salen; salen = sizeof(sa); if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0) { - return NULL; + error_setg_errno(errp, errno, "getsockname failed"); + return; } - return vnc_basic_info_get(&sa, salen); + vnc_init_basic_info(&sa, salen, info, errp); } -static VncBasicInfo *vnc_basic_info_get_from_remote_addr(int fd) +static void vnc_init_basic_info_from_remote_addr(int fd, VncBasicInfo *info, + Error **errp) { struct sockaddr_storage sa; socklen_t salen; salen = sizeof(sa); if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0) { - return NULL; + error_setg_errno(errp, errno, "getpeername failed"); + return; } - return vnc_basic_info_get(&sa, salen); + vnc_init_basic_info(&sa, salen, info, errp); } static const char *vnc_auth_name(VncDisplay *vd) { @@ -256,15 +259,18 @@ static const char *vnc_auth_name(VncDisplay *vd) { static VncServerInfo *vnc_server_info_get(VncDisplay *vd) { VncServerInfo *info; - VncBasicInfo *bi = vnc_basic_info_get_from_server_addr(vd->lsock); - if (!bi) { - return NULL; - } + Error *err = NULL; info = g_malloc(sizeof(*info)); - info->base = bi; + info->base = g_malloc(sizeof(*info->base)); + vnc_init_basic_info_from_server_addr(vd->lsock, info->base, &err); info->has_auth = true; info->auth = g_strdup(vnc_auth_name(vd)); + if (err) { + qapi_free_VncServerInfo(info); + info = NULL; + error_free(err); + } return info; } @@ -291,11 +297,16 @@ static void vnc_client_cache_auth(VncState *client) static void vnc_client_cache_addr(VncState *client) { - VncBasicInfo *bi = vnc_basic_info_get_from_remote_addr(client->csock); + Error *err = NULL; - if (bi) { - client->info = g_malloc0(sizeof(*client->info)); - client->info->base = bi; + client->info = g_malloc0(sizeof(*client->info)); + client->info->base = g_malloc0(sizeof(*client->info->base)); + vnc_init_basic_info_from_remote_addr(client->csock, client->info->base, + &err); + if (err) { + qapi_free_VncClientInfo(client->info); + client->info = NULL; + error_free(err); } }