From patchwork Fri Oct 30 18:26:55 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andre Przywara X-Patchwork-Id: 7528731 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id CD30A9F2F7 for ; Fri, 30 Oct 2015 18:29:20 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id EDFA22077C for ; Fri, 30 Oct 2015 18:29:19 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 1F4542077A for ; Fri, 30 Oct 2015 18:29:19 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZsEP1-0005GG-95; Fri, 30 Oct 2015 18:27:51 +0000 Received: from foss.arm.com ([217.140.101.70]) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZsEO4-0004nd-C4 for linux-arm-kernel@lists.infradead.org; Fri, 30 Oct 2015 18:26:54 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 9B9433FB; Fri, 30 Oct 2015 11:26:23 -0700 (PDT) Received: from e104803-lin.lan (unknown [10.1.203.153]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E51C93F308; Fri, 30 Oct 2015 11:26:30 -0700 (PDT) From: Andre Przywara To: will.deacon@arm.com, kvm@vger.kernel.org Subject: [PATCH 2/7] provide generic read_file() implementation Date: Fri, 30 Oct 2015 18:26:55 +0000 Message-Id: <1446229620-28088-3-git-send-email-andre.przywara@arm.com> X-Mailer: git-send-email 2.5.1 In-Reply-To: <1446229620-28088-1-git-send-email-andre.przywara@arm.com> References: <1446229620-28088-1-git-send-email-andre.przywara@arm.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20151030_112652_502015_16AFCD06 X-CRM114-Status: GOOD ( 11.41 ) X-Spam-Score: -7.9 (-------) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kvm-ppc@vger.kernel.org, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-5.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP In various parts of kvmtool we simply try to read files into memory, but fail to do so in a safe way. The read(2) syscall can return early having only parts of the file read, or it may return -1 due to being interrupted by a signal (in which case we should simply retry). The ARM code seems to provide the only safe implementation, so take that as an inspiration to provide a generic read_file() function usable by every part of kvmtool. Signed-off-by: Andre Przywara --- include/kvm/read-write.h | 2 ++ util/read-write.c | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/include/kvm/read-write.h b/include/kvm/read-write.h index 67571f9..acbd6f0 100644 --- a/include/kvm/read-write.h +++ b/include/kvm/read-write.h @@ -12,6 +12,8 @@ ssize_t xread(int fd, void *buf, size_t count); ssize_t xwrite(int fd, const void *buf, size_t count); +ssize_t read_file(int fd, char *buf, size_t max_size); + ssize_t read_in_full(int fd, void *buf, size_t count); ssize_t write_in_full(int fd, const void *buf, size_t count); diff --git a/util/read-write.c b/util/read-write.c index 44709df..bf6fb2f 100644 --- a/util/read-write.c +++ b/util/read-write.c @@ -32,6 +32,27 @@ restart: return nr; } +/* + * Read in the whole file while not exceeding max_size bytes of the buffer. + * Returns -1 (with errno set) in case of an error (ENOMEM if buffer was + * too small) or the filesize if the whole file could be read. + */ +ssize_t read_file(int fd, char *buf, size_t max_size) +{ + ssize_t ret; + char dummy; + + errno = 0; + ret = read_in_full(fd, buf, max_size); + + /* Probe whether we reached EOF. */ + if (xread(fd, &dummy, 1) == 0) + return ret; + + errno = ENOMEM; + return -1; +} + ssize_t read_in_full(int fd, void *buf, size_t count) { ssize_t total = 0;