From patchwork Tue Jan 19 08:39:52 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chunyan Liu X-Patchwork-Id: 8059151 Return-Path: X-Original-To: patchwork-xen-devel@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 ECFFC9F744 for ; Tue, 19 Jan 2016 08:42:40 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 16F3B20386 for ; Tue, 19 Jan 2016 08:42:40 +0000 (UTC) Received: from lists.xen.org (lists.xenproject.org [50.57.142.19]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2CA3A20120 for ; Tue, 19 Jan 2016 08:42:39 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xen.org) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1aLRqE-0007uE-0b; Tue, 19 Jan 2016 08:40:42 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1aLRqC-0007q7-OD for xen-devel@lists.xen.org; Tue, 19 Jan 2016 08:40:40 +0000 Received: from [85.158.137.68] by server-2.bemta-3.messagelabs.com id FF/23-21201-786FD965; Tue, 19 Jan 2016 08:40:39 +0000 X-Env-Sender: cyliu@suse.com X-Msg-Ref: server-8.tower-31.messagelabs.com!1453192837!16788870!1 X-Originating-IP: [137.65.250.26] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 7.35.1; banners=-,-,- X-VirusChecked: Checked Received: (qmail 14220 invoked from network); 19 Jan 2016 08:40:38 -0000 Received: from prv3-mh.provo.novell.com (HELO prv3-mh.provo.novell.com) (137.65.250.26) by server-8.tower-31.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 19 Jan 2016 08:40:38 -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, 19 Jan 2016 01:40:24 -0700 From: Chunyan Liu To: xen-devel@lists.xen.org Date: Tue, 19 Jan 2016 16:39:52 +0800 Message-Id: <1453192795-15693-3-git-send-email-cyliu@suse.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1453192795-15693-1-git-send-email-cyliu@suse.com> References: <1453192795-15693-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 V13 2/5] libxl_utils: add internal function to read sysfs file contents X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, 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 9e94835..d1eb18f 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -4039,6 +4039,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 e42422a..e64f301 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, \