From patchwork Mon Apr 25 18:46:27 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cole Robinson X-Patchwork-Id: 66597 Delivered-To: patch@linaro.org Received: by 10.140.93.198 with SMTP id d64csp1196340qge; Mon, 25 Apr 2016 11:49:18 -0700 (PDT) X-Received: by 10.140.36.231 with SMTP id p94mr35662805qgp.79.1461610157611; Mon, 25 Apr 2016 11:49:17 -0700 (PDT) Return-Path: Received: from mx6-phx2.redhat.com (mx6-phx2.redhat.com. [209.132.183.39]) by mx.google.com with ESMTPS id d21si11293330qkb.86.2016.04.25.11.49.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Apr 2016 11:49:17 -0700 (PDT) Received-SPF: pass (google.com: domain of libvir-list-bounces@redhat.com designates 209.132.183.39 as permitted sender) client-ip=209.132.183.39; Authentication-Results: mx.google.com; spf=pass (google.com: domain of libvir-list-bounces@redhat.com designates 209.132.183.39 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx6-phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u3PIkg7B028965; Mon, 25 Apr 2016 14:46:43 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id u3PIkd45022100 for ; Mon, 25 Apr 2016 14:46:39 -0400 Received: from colepc.redhat.com (ovpn-113-101.phx2.redhat.com [10.3.113.101]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u3PIkcl7012964; Mon, 25 Apr 2016 14:46:39 -0400 From: Cole Robinson To: libvirt-list@redhat.com Date: Mon, 25 Apr 2016 14:46:27 -0400 Message-Id: <7e0c85d46907b6ff3299c5ded3238f590f2c5946.1461609759.git.crobinso@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 1/9] fdstream: separate out virCommandPtr cleanup X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com Let's us de-nest some of the logic, and will simplify upcoming patches --- src/fdstream.c | 73 +++++++++++++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 31 deletions(-) -- 2.7.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list diff --git a/src/fdstream.c b/src/fdstream.c index a6a0fbe..681b90e 100644 --- a/src/fdstream.c +++ b/src/fdstream.c @@ -240,6 +240,46 @@ virFDStreamAddCallback(virStreamPtr st, return ret; } +static int +virFDStreamCloseCommand(struct virFDStreamData *fdst) +{ + char buf[1024]; + ssize_t len; + int status; + int ret = -1; + + if (!fdst->cmd) + return 0; + + if ((len = saferead(fdst->errfd, buf, sizeof(buf)-1)) < 0) + buf[0] = '\0'; + else + buf[len] = '\0'; + + virCommandRawStatus(fdst->cmd); + if (virCommandWait(fdst->cmd, &status) < 0) + goto error; + + if (status != 0) { + if (buf[0] != '\0') { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", buf); + } else if (WIFEXITED(status)) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("I/O helper exited with status %d"), + WEXITSTATUS(status)); + } else { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("I/O helper exited abnormally")); + } + goto error; + } + + ret = 0; + error: + virCommandFree(fdst->cmd); + fdst->cmd = NULL; + return ret; +} static int virFDStreamCloseInt(virStreamPtr st, bool streamAbort) @@ -285,37 +325,8 @@ virFDStreamCloseInt(virStreamPtr st, bool streamAbort) /* mutex locked */ ret = VIR_CLOSE(fdst->fd); - if (fdst->cmd) { - char buf[1024]; - ssize_t len; - int status; - if ((len = saferead(fdst->errfd, buf, sizeof(buf)-1)) < 0) - buf[0] = '\0'; - else - buf[len] = '\0'; - - virCommandRawStatus(fdst->cmd); - if (virCommandWait(fdst->cmd, &status) < 0) { - ret = -1; - } else if (status != 0) { - if (buf[0] == '\0') { - if (WIFEXITED(status)) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("I/O helper exited with status %d"), - WEXITSTATUS(status)); - } else { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("I/O helper exited abnormally")); - } - } else { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - buf); - } - ret = -1; - } - virCommandFree(fdst->cmd); - fdst->cmd = NULL; - } + if (virFDStreamCloseCommand(fdst) < 0) + ret = -1; if (VIR_CLOSE(fdst->errfd) < 0) VIR_DEBUG("ignoring failed close on fd %d", fdst->errfd);