From patchwork Mon Dec 12 16:18:10 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ross Lagerwall X-Patchwork-Id: 9470927 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 2413460573 for ; Mon, 12 Dec 2016 16:24:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1B61F284D4 for ; Mon, 12 Dec 2016 16:24:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 10496284D9; Mon, 12 Dec 2016 16:24:30 +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 A33B7284D4 for ; Mon, 12 Dec 2016 16:24:29 +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 1cGTMf-0002Dh-11; Mon, 12 Dec 2016 16:22:09 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cGTMd-0002D6-J9 for xen-devel@lists.xen.org; Mon, 12 Dec 2016 16:22:07 +0000 Received: from [85.158.143.35] by server-9.bemta-6.messagelabs.com id 7C/B7-28694-EAECE485; Mon, 12 Dec 2016 16:22:06 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmpjkeJIrShJLcpLzFFi42JxWrohUnfdOb8 Ig9PTRSyWfFzM4sDocXT3b6YAxijWzLyk/IoE1oz2SXPYC85qVTzedo25gfGoTBcjJ4eEgL/E 5PZ3zCA2m4CBxK1L38FsEQFZidVdc9i7GLk4mAVWMErc+76TBSQhLOAhseHcKzYQm0VAVaKvd wFYnFfAU2LR6sWMEEPlJG6e6wQbxCngJXF7Szs7iC0EVLOs8xMrhK0m8Xb5GaheQYmTM5+A2c wCEhIHX7xgnsDIOwtJahaS1AJGplWMGsWpRWWpRbpGpnpJRZnpGSW5iZk5uoYGZnq5qcXFiem pOYlJxXrJ+bmbGIHhwwAEOxhXLQg8xCjJwaQkyvvlgF+EEF9SfkplRmJxRnxRaU5q8SFGGQ4O JQneo2eBcoJFqempFWmZOcBAhklLcPAoifCKAoNZiLe4IDG3ODMdInWKUVFKnPc4SJ8ASCKjN A+uDRY9lxhlpYR5GYEOEeIpSC3KzSxBlX/FKM7BqCTMextkCk9mXgnc9FdAi5mAFj/f5w2yuC QRISXVwGjK0Lc8s1LguKRv4B8rJoUFP+0NA0rWS+z8dODvXXGpZ61t6iq+Uy9ZZ7n0vzjrvv9 i74aN5c7sKT1bbt/eMv/Hrgoe1v+aqm7L1835YHrbwj2g59CPH5cVVsz4MbeSv+DldVNZq103 F3dp2ypmuC7IPd99Y939w2ZB4QfP84ul7T2+TTLi3UYlluKMREMt5qLiRABIDcMgmQIAAA== X-Env-Sender: prvs=147044df7=ross.lagerwall@citrix.com X-Msg-Ref: server-4.tower-21.messagelabs.com!1481559724!41703460!1 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.1.1; banners=-,-,- X-VirusChecked: Checked Received: (qmail 32199 invoked from network); 12 Dec 2016 16:22:06 -0000 Received: from smtp.citrix.com (HELO SMTP.CITRIX.COM) (66.165.176.89) by server-4.tower-21.messagelabs.com with RC4-SHA encrypted SMTP; 12 Dec 2016 16:22:06 -0000 X-IronPort-AV: E=Sophos;i="5.33,337,1477958400"; d="scan'208";a="394308069" From: Ross Lagerwall To: Date: Mon, 12 Dec 2016 16:18:10 +0000 Message-ID: <1481559490-13844-8-git-send-email-ross.lagerwall@citrix.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1481559490-13844-1-git-send-email-ross.lagerwall@citrix.com> References: <1481559490-13844-1-git-send-email-ross.lagerwall@citrix.com> MIME-Version: 1.0 Cc: Ross Lagerwall , Ian Jackson , Wei Liu Subject: [Xen-devel] [PATCH v1 7/7] tools/livepatch: Exit with 2 if a timeout occurs 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 Exit with 0 for success. Exit with 1 for an error. Exit with 2 if the operation should be retried for any reason (e.g. a timeout or because another operation was in progress). This allows a program or script driving xen-livepatch to determine if the operation should be retried without parsing the output. Fix a number of incorrect uses of errno after an operation that could set it (e.g. fprintf, close). Signed-off-by: Ross Lagerwall --- tools/misc/xen-livepatch.c | 60 +++++++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/tools/misc/xen-livepatch.c b/tools/misc/xen-livepatch.c index 9633c4a..370ad60 100644 --- a/tools/misc/xen-livepatch.c +++ b/tools/misc/xen-livepatch.c @@ -101,9 +101,10 @@ static int list_func(int argc, char *argv[]) rc = xc_livepatch_list(xch, MAX_LEN, idx, info, name, len, &done, &left); if ( rc ) { + rc = errno; fprintf(stderr, "Failed to list %d/%d.\n" "Error %d: %s\n", - idx, left, errno, strerror(errno)); + idx, left, rc, strerror(rc)); break; } if ( !idx ) @@ -175,37 +176,40 @@ static int upload_func(int argc, char *argv[]) fd = open(filename, O_RDONLY); if ( fd < 0 ) { + int saved_errno = errno; fprintf(stderr, "Could not open %s.\n" "Error %d: %s\n", - filename, errno, strerror(errno)); - return errno; + filename, saved_errno, strerror(saved_errno)); + return saved_errno; } if ( stat(filename, &buf) != 0 ) { + int saved_errno = errno; fprintf(stderr, "Could not get size of %s.\n" "Error %d: %s\n", - filename, errno, strerror(errno)); + filename, saved_errno, strerror(saved_errno)); close(fd); - return errno; + return saved_errno; } len = buf.st_size; fbuf = mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0); if ( fbuf == MAP_FAILED ) { + int saved_errno = errno; fprintf(stderr, "Could not map %s.\n" "Error %d: %s\n", - filename, errno, strerror(errno)); + filename, saved_errno, strerror(saved_errno)); close (fd); - return errno; + return saved_errno; } printf("Uploading %s... ", filename); rc = xc_livepatch_upload(xch, name, fbuf, len); if ( rc ) { + rc = errno; printf("failed\n"); - fprintf(stderr, "Error %d: %s\n", - errno, strerror(errno)); + fprintf(stderr, "Error %d: %s\n", rc, strerror(rc)); } else printf("completed\n"); @@ -216,8 +220,6 @@ static int upload_func(int argc, char *argv[]) fprintf(stderr, "Could not unmap %s.\n" "Error %d: %s\n", filename, errno, strerror(errno)); - if ( !rc ) - rc = errno; } close(fd); @@ -307,17 +309,18 @@ int action_func(int argc, char *argv[], unsigned int idx) rc = xc_livepatch_get(xch, name, &status); if ( rc ) { + int saved_errno = errno; fprintf(stderr, "Failed to get status of %s.\n" "Error %d: %s\n", - name, errno, strerror(errno)); - return -1; + name, saved_errno, strerror(saved_errno)); + return saved_errno; } if ( status.rc == -XEN_EAGAIN ) { fprintf(stderr, "Cannot execute %s.\n" "Operation already in progress.\n", action_options[idx].name); - return -1; + return EAGAIN; } if ( status.state == action_options[idx].expected ) @@ -333,9 +336,11 @@ int action_func(int argc, char *argv[], unsigned int idx) rc = action_options[idx].function(xch, name, HYPERVISOR_TIMEOUT); if ( rc ) { + int saved_errno = errno; printf("failed\n"); - fprintf(stderr, "Error %d: %s\n", errno, strerror(errno)); - return -1; + fprintf(stderr, "Error %d: %s\n", + saved_errno, strerror(saved_errno)); + return saved_errno; } } else @@ -360,7 +365,7 @@ int action_func(int argc, char *argv[], unsigned int idx) { printf("failed\n"); fprintf(stderr, "Operation didn't complete.\n"); - return -1; + return EAGAIN; } if ( rc == 0 ) @@ -372,7 +377,7 @@ int action_func(int argc, char *argv[], unsigned int idx) { printf("failed\n"); fprintf(stderr, "Error %d: %s\n", -rc, strerror(-rc)); - return -1; + return -rc; } else { @@ -485,7 +490,24 @@ int main(int argc, char *argv[]) xc_interface_close(xch); - return !!ret; + /* + * Exitcode 0 for success. + * Exitcode 1 for an error. + * Exitcode 2 if the operation should be retried for any reason (e.g. a + * timeout or because another operation was in progress). + */ + +#define EXIT_TIMEOUT (EXIT_FAILURE + 1) + switch ( ret ) + { + case 0: + return EXIT_SUCCESS; + case EAGAIN: + case EBUSY: + return EXIT_TIMEOUT; + default: + return EXIT_FAILURE; + } } /*