From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id C7E9EA0471 for ; Mon, 15 Jul 2019 09:54:47 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id C18421BC07; Mon, 15 Jul 2019 09:54:22 +0200 (CEST) Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by dpdk.org (Postfix) with ESMTP id 3DB071B995 for ; Mon, 15 Jul 2019 09:54:16 +0200 (CEST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 15 Jul 2019 00:54:15 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.63,493,1557212400"; d="scan'208";a="175025620" Received: from npg-dpdk-virtio-tbie-2.sh.intel.com ([10.67.104.66]) by FMSMGA003.fm.intel.com with ESMTP; 15 Jul 2019 00:54:14 -0700 From: Tiwei Bie To: dev@dpdk.org Cc: ferruh.yigit@intel.com, anatoly.burakov@intel.com, bruce.richardson@intel.com, keith.wiles@intel.com, david.marchand@redhat.com, alejandro.lucero@netronome.com, cunming.liang@intel.com Date: Mon, 15 Jul 2019 15:52:13 +0800 Message-Id: <20190715075214.16616-5-tiwei.bie@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190715075214.16616-1-tiwei.bie@intel.com> References: <20190403071844.21126-1-tiwei.bie@intel.com> <20190715075214.16616-1-tiwei.bie@intel.com> Subject: [dpdk-dev] [RFC v2 4/5] eal: add a helper for reading string from sysfs X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" This patch adds a helper for reading string from sysfs. Signed-off-by: Cunming Liang Signed-off-by: Tiwei Bie --- lib/librte_eal/common/eal_filesystem.h | 10 ++++++++++ lib/librte_eal/freebsd/eal/eal.c | 22 ++++++++++++++++++++++ lib/librte_eal/linux/eal/eal.c | 22 ++++++++++++++++++++++ lib/librte_eal/rte_eal_version.map | 1 + 4 files changed, 55 insertions(+) diff --git a/lib/librte_eal/common/eal_filesystem.h b/lib/librte_eal/common/eal_filesystem.h index 5d21f07c2..be4c51ebb 100644 --- a/lib/librte_eal/common/eal_filesystem.h +++ b/lib/librte_eal/common/eal_filesystem.h @@ -104,4 +104,14 @@ eal_get_hugefile_path(char *buffer, size_t buflen, const char *hugedir, int f_id * Used to read information from files on /sys */ int eal_parse_sysfs_value(const char *filename, unsigned long *val); +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice + * + * Function to read a line from a file on the filesystem. + * Used to read information from files on /sys + */ +__rte_experimental +int rte_eal_parse_sysfs_str(const char *filename, char *buf, unsigned long sz); + #endif /* EAL_FILESYSTEM_H */ diff --git a/lib/librte_eal/freebsd/eal/eal.c b/lib/librte_eal/freebsd/eal/eal.c index d53f0fe69..78720685f 100644 --- a/lib/librte_eal/freebsd/eal/eal.c +++ b/lib/librte_eal/freebsd/eal/eal.c @@ -209,6 +209,28 @@ eal_parse_sysfs_value(const char *filename, unsigned long *val) return 0; } +int +rte_eal_parse_sysfs_str(const char *filename, char *buf, unsigned long sz) +{ + FILE *f; + + f = fopen(filename, "r"); + if (f == NULL) { + RTE_LOG(ERR, EAL, "%s(): cannot open sysfs file %s\n", + __func__, filename); + return -1; + } + + if (fgets(buf, sz, f) == NULL) { + RTE_LOG(ERR, EAL, "%s(): cannot read sysfs file %s\n", + __func__, filename); + fclose(f); + return -1; + } + + fclose(f); + return 0; +} /* create memory configuration in shared/mmap memory. Take out * a write lock on the memsegs, so we can auto-detect primary/secondary. diff --git a/lib/librte_eal/linux/eal/eal.c b/lib/librte_eal/linux/eal/eal.c index 2e5499f9b..44bad45d3 100644 --- a/lib/librte_eal/linux/eal/eal.c +++ b/lib/librte_eal/linux/eal/eal.c @@ -295,6 +295,28 @@ eal_parse_sysfs_value(const char *filename, unsigned long *val) return 0; } +int +rte_eal_parse_sysfs_str(const char *filename, char *buf, unsigned long sz) +{ + FILE *f; + + f = fopen(filename, "r"); + if (f == NULL) { + RTE_LOG(ERR, EAL, "%s(): cannot open sysfs file %s\n", + __func__, filename); + return -1; + } + + if (fgets(buf, sz, f) == NULL) { + RTE_LOG(ERR, EAL, "%s(): cannot read sysfs file %s\n", + __func__, filename); + fclose(f); + return -1; + } + + fclose(f); + return 0; +} /* create memory configuration in shared/mmap memory. Take out * a write lock on the memsegs, so we can auto-detect primary/secondary. diff --git a/lib/librte_eal/rte_eal_version.map b/lib/librte_eal/rte_eal_version.map index 1892d9ea9..a9559176b 100644 --- a/lib/librte_eal/rte_eal_version.map +++ b/lib/librte_eal/rte_eal_version.map @@ -331,6 +331,7 @@ EXPERIMENTAL { rte_dev_hotplug_handle_enable; rte_dev_iterator_init; rte_dev_iterator_next; + rte_eal_parse_sysfs_str; rte_extmem_attach; rte_extmem_detach; rte_extmem_register; -- 2.17.1