From patchwork Wed Oct 4 15:57:22 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Jackson X-Patchwork-Id: 9985033 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C61066028E for ; Wed, 4 Oct 2017 16:00:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B8D6C28B45 for ; Wed, 4 Oct 2017 16:00:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AD82C28B4B; Wed, 4 Oct 2017 16:00:22 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 7FBFB28B45 for ; Wed, 4 Oct 2017 16:00:20 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dzm3R-0008A6-RF; Wed, 04 Oct 2017 15:57:49 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dzm3Q-00089O-4c for xen-devel@lists.xensource.com; Wed, 04 Oct 2017 15:57:48 +0000 Received: from [85.158.137.68] by server-7.bemta-3.messagelabs.com id 6A/15-02224-AF405D95; Wed, 04 Oct 2017 15:57:46 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrLIsWRWlGSWpSXmKPExsXitHRDpO4vlqu RBmtXilncm/Ke3YHRY3vfLvYAxijWzLyk/IoE1ozP844zFhznrji05xlbA+Nezi5GDg4JAX+J zQsjuhg5gUwPietnTzCB2GwCuhJNW/6ygdgiAsoSx5u+sHYxcnEwC5xjlGidsoEZJCEsECIx8 +c2sCIWARWJKU9egzXzCnhJtCx4zQwxVE7i/PGfYDYnUPzpzg5mkL1CAp4Sx9aaQphqEnPXx0 N0CkqcnPmEBcRmFpCQOPjiBdQUS4lv658yT2Dkn4WkbBaSsgWMTKsYNYpTi8pSi3QNDfWSijL TM0pyEzNzdA0NjPVyU4uLE9NTcxKTivWS83M3MQIDjQEIdjCu/u10iFGSg0lJlPfXzyuRQnxJ +SmVGYnFGfFFpTmpxYcYZTg4lCR4lzFfjRQSLEpNT61Iy8wBhjxMWoKDR0mE1wAY9kK8xQWJu cWZ6RCpU4zGHMc2Xf7DxNFx8+4fJiGWvPy8VClx3l8gkwRASjNK8+AGwWLxEqOslDAvI9BpQj wFqUW5mSWo8q8YxTkYlYR5D4NM4cnMK4Hb9wroFCagU+Y0XQE5pSQRISXVwLh5V7X1XO+pMSu dnrHO45scccen9GNL9fz8DX3Pt3/6EvWN//y6zY7GepMfNX5vfscUNeOevmh2cfK9A9ZMayxe 7n1pIOtvKnGP9WDE8WadFdHGoa6p3DkS5zOPT1ymf8C3q+O02Bo5wa8SHbt4JkqsnfT4xEHpv FW10WvdmVZEM/h83fG4ZLYSS3FGoqEWc1FxIgBby6pBwAIAAA== X-Env-Sender: prvs=4438c69fc=Ian.Jackson@citrix.com X-Msg-Ref: server-7.tower-31.messagelabs.com!1507132663!110366889!2 X-Originating-IP: [66.165.176.89] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni44OSA9PiAyMDMwMDc=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 30197 invoked from network); 4 Oct 2017 15:57:45 -0000 Received: from smtp.citrix.com (HELO SMTP.CITRIX.COM) (66.165.176.89) by server-7.tower-31.messagelabs.com with RC4-SHA encrypted SMTP; 4 Oct 2017 15:57:45 -0000 X-IronPort-AV: E=Sophos;i="5.42,477,1500940800"; d="scan'208";a="442505941" X-CrossPremisesHeadersFilteredBySendConnector: FTLPEX02AMS01.citrite.net From: Ian Jackson To: Date: Wed, 4 Oct 2017 16:57:22 +0100 Message-ID: <1507132650-25376-17-git-send-email-ian.jackson@eu.citrix.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1507132650-25376-1-git-send-email-ian.jackson@eu.citrix.com> References: <1507132650-25376-1-git-send-email-ian.jackson@eu.citrix.com> MIME-Version: 1.0 X-OrganizationHeadersPreserved: FTLPEX02AMS01.citrite.net Cc: Ross Lagerwall , Stefano Stabellini , Wei Liu , Ian Jackson Subject: [Xen-devel] [PATCH 16/24] tools/xenstore: get_handle: Allocate struct before opening fd X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Now we can also abolish the temporary local variable "fd" and simply use h->fd. This ordering is necessary to be able to call xentoolcore__register_active_handle sensibly. Signed-off-by: Ian Jackson Acked-by: Wei Liu --- tools/xenstore/xs.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/tools/xenstore/xs.c b/tools/xenstore/xs.c index 65cba86..7f85bb2 100644 --- a/tools/xenstore/xs.c +++ b/tools/xenstore/xs.c @@ -223,27 +223,26 @@ static struct xs_handle *get_handle(const char *connect_to) { struct stat buf; struct xs_handle *h = NULL; - int fd = -1, saved_errno; + int saved_errno; + + h = malloc(sizeof(*h)); + if (h == NULL) + goto err; + + memset(h, 0, sizeof(*h)); + h->fd = -1; if (stat(connect_to, &buf) != 0) goto err; if (S_ISSOCK(buf.st_mode)) - fd = get_socket(connect_to); + h->fd = get_socket(connect_to); else - fd = get_dev(connect_to); + h->fd = get_dev(connect_to); - if (fd == -1) + if (h->fd == -1) goto err; - h = malloc(sizeof(*h)); - if (h == NULL) - goto err; - - memset(h, 0, sizeof(*h)); - - h->fd = fd; - INIT_LIST_HEAD(&h->reply_list); INIT_LIST_HEAD(&h->watch_list); @@ -267,7 +266,10 @@ static struct xs_handle *get_handle(const char *connect_to) err: saved_errno = errno; - if (fd >= 0) close(fd); + if (h) { + if (h->fd >= 0) + close(h->fd); + } free(h); errno = saved_errno;