From patchwork Tue Mar 1 08:09:43 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chunyan Liu X-Patchwork-Id: 8462891 Return-Path: X-Original-To: patchwork-xen-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 57E67C0553 for ; Tue, 1 Mar 2016 08:12:40 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 6AA49201BB for ; Tue, 1 Mar 2016 08:12:39 +0000 (UTC) Received: from lists.xen.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.kernel.org (Postfix) with ESMTPS id 7F3FA201F4 for ; Tue, 1 Mar 2016 08:12:38 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xen.org) by lists.xen.org with esmtp (Exim 4.84) (envelope-from ) id 1aafNy-0006sf-RH; Tue, 01 Mar 2016 08:10:26 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.84) (envelope-from ) id 1aafNy-0006sS-Bf for xen-devel@lists.xen.org; Tue, 01 Mar 2016 08:10:26 +0000 Received: from [85.158.139.211] by server-4.bemta-5.messagelabs.com id 1A/A0-04025-17E45D65; Tue, 01 Mar 2016 08:10:25 +0000 X-Env-Sender: cyliu@suse.com X-Msg-Ref: server-4.tower-206.messagelabs.com!1456819823!25909235!1 X-Originating-IP: [137.65.250.26] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 8.11; banners=-,-,- X-VirusChecked: Checked Received: (qmail 21124 invoked from network); 1 Mar 2016 08:10:24 -0000 Received: from prv3-mh.provo.novell.com (HELO prv3-mh.provo.novell.com) (137.65.250.26) by server-4.tower-206.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 1 Mar 2016 08:10:24 -0000 Received: from linux-3hvf.site (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by prv3-mh.provo.novell.com with ESMTP (TLS encrypted); Tue, 01 Mar 2016 01:10:18 -0700 From: Chunyan Liu To: xen-devel@lists.xen.org Date: Tue, 1 Mar 2016 16:09:43 +0800 Message-Id: <1456819787-11568-3-git-send-email-cyliu@suse.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1456819787-11568-1-git-send-email-cyliu@suse.com> References: <1456819787-11568-1-git-send-email-cyliu@suse.com> Cc: jgross@suse.com, wei.liu2@citrix.com, ian.campbell@citrix.com, george.dunlap@eu.citrix.com, Ian.Jackson@eu.citrix.com, Chunyan Liu , jfehlig@suse.com Subject: [Xen-devel] [PATCH V15 2/6] libxl_utils: add internal function to read sysfs file contents 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: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, 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 Add a new function libxl_read_sysfs_file_contents to handle sysfs file specially. It would be used in later pvusb work. Signed-off-by: Chunyan Liu Acked-by: Ian Jackson --- tools/libxl/libxl_internal.h | 4 +++ tools/libxl/libxl_utils.c | 74 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 9ea62e8..21b9b8c 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -4078,6 +4078,10 @@ void libxl__bitmap_copy_best_effort(libxl__gc *gc, libxl_bitmap *dptr, int libxl__count_physical_sockets(libxl__gc *gc, int *sockets); +_hidden int libxl__read_sysfs_file_contents(libxl__gc *gc, + const char *filename, + void **data_r, + int *datalen_r); #define LIBXL_QEMU_USER_PREFIX "xen-qemuuser" #define LIBXL_QEMU_USER_BASE LIBXL_QEMU_USER_PREFIX"-domid" diff --git a/tools/libxl/libxl_utils.c b/tools/libxl/libxl_utils.c index 672d3f8..b0cb9e1 100644 --- a/tools/libxl/libxl_utils.c +++ b/tools/libxl/libxl_utils.c @@ -396,6 +396,80 @@ int libxl_read_file_contents(libxl_ctx *ctx, const char *filename, return e; } +int libxl__read_sysfs_file_contents(libxl__gc *gc, const char *filename, + void **data_r, int *datalen_r) +{ + FILE *f = 0; + uint8_t *data = 0; + int datalen = 0; + int e; + struct stat stab; + ssize_t rs; + + f = fopen(filename, "r"); + if (!f) { + if (errno == ENOENT) return ENOENT; + LOGE(ERROR, "failed to open %s", filename); + goto xe; + } + + if (fstat(fileno(f), &stab)) { + LOGE(ERROR, "failed to fstat %s", filename); + goto xe; + } + + if (!S_ISREG(stab.st_mode)) { + LOGE(ERROR, "%s is not a plain file", filename); + errno = ENOTTY; + goto xe; + } + + if (stab.st_size > INT_MAX) { + LOG(ERROR, "file %s is far too large", filename); + errno = EFBIG; + goto xe; + } + + datalen = stab.st_size; + + if (stab.st_size && data_r) { + data = libxl__malloc(gc, datalen); + + /* For sysfs file, datalen is always PAGE_SIZE. 'read' + * will return the number of bytes of the actual content, + * rs <= datalen is expected. + */ + rs = fread(data, 1, datalen, f); + if (rs < datalen) { + if (ferror(f)) { + LOGE(ERROR, "failed to read %s", filename); + goto xe; + } + + datalen = rs; + data = libxl__realloc(gc, data, datalen); + } + } + + if (fclose(f)) { + f = 0; + LOGE(ERROR, "failed to close %s", filename); + goto xe; + } + + if (data_r) *data_r = data; + if (datalen_r) *datalen_r = datalen; + + return 0; + + xe: + e = errno; + assert(e != ENOENT); + if (f) fclose(f); + return e; +} + + #define READ_WRITE_EXACTLY(rw, zero_is_eof, constdata) \ \ int libxl_##rw##_exactly(libxl_ctx *ctx, int fd, \