[v3,3/3] eal: move common filesystem setup code into one file

Message ID 20220209065403.168475-4-stephen@networkplumber.org (mailing list archive)
State Accepted, archived
Delegated to: Thomas Monjalon
Headers
Series eal: support configuring runtime directory |

Checks

Context Check Description
ci/checkpatch warning coding style issues
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/github-robot: build success github build: passed
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-broadcom-Functional success Functional Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-aarch64-unit-testing success Testing PASS
ci/iol-x86_64-compile-testing success Testing PASS
ci/iol-aarch64-compile-testing success Testing PASS
ci/iol-abi-testing success Testing PASS
ci/iol-x86_64-unit-testing success Testing PASS

Commit Message

Stephen Hemminger Feb. 9, 2022, 6:54 a.m. UTC
  Both Linux and FreeBSD have same code for creating runtime
directory and reading sysfs files. Put them in the new lib/eal/unix
subdirectory.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/freebsd/eal.c         |  88 ---------------------------
 lib/eal/linux/eal.c           |  90 ----------------------------
 lib/eal/unix/eal_filesystem.c | 110 ++++++++++++++++++++++++++++++++++
 lib/eal/unix/meson.build      |   1 +
 4 files changed, 111 insertions(+), 178 deletions(-)
 create mode 100644 lib/eal/unix/eal_filesystem.c
  

Comments

Bruce Richardson Feb. 9, 2022, 9:18 a.m. UTC | #1
On Tue, Feb 08, 2022 at 10:54:03PM -0800, Stephen Hemminger wrote:
> Both Linux and FreeBSD have same code for creating runtime
> directory and reading sysfs files. Put them in the new lib/eal/unix
> subdirectory.
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
LGTM, apart from one minor nit inline below.

Acked-by: Bruce Richardson <bruce.richardson@intel.com>

> ---
>  lib/eal/freebsd/eal.c         |  88 ---------------------------
>  lib/eal/linux/eal.c           |  90 ----------------------------
>  lib/eal/unix/eal_filesystem.c | 110 ++++++++++++++++++++++++++++++++++
>  lib/eal/unix/meson.build      |   1 +
>  4 files changed, 111 insertions(+), 178 deletions(-)
>  create mode 100644 lib/eal/unix/eal_filesystem.c
> 
<snip>
> +
> +	/* create the path if it doesn't exist. no "mkdir -p" here, so do it
> +	 * step by step.
> +	 */
> +	ret = mkdir(tmp, 0700);
> +	if (ret < 0 && errno != EEXIST) {
> +		RTE_LOG(ERR, EAL, "Error creating '%s': %s\n",
> +			tmp, strerror(errno));
> +		return -1;
> +	}
> +
> +	ret = mkdir(run_dir, 0700);
> +	if (ret < 0 && errno != EEXIST) {
> +		RTE_LOG(ERR, EAL, "Error creating '%s': %s\n",
> +			run_dir, strerror(errno));
> +		return -1;
> +	}
> +
> +	if (eal_set_runtime_dir(run_dir))
> +		return -1;
> +
> +	return 0;
> +}

Missing a blank line here between the two functions.

> +/* parse a sysfs (or other) file containing one integer value */
> +int
> +eal_parse_sysfs_value(const char *filename, unsigned long *val)
> +{
> +	FILE *f;
> +	char buf[BUFSIZ];
> +	char *end = NULL;
> +
> +	if ((f = fopen(filename, "r")) == NULL) {
> +		RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
> +			__func__, filename);
> +		return -1;
> +	}
> +
> +	if (fgets(buf, sizeof(buf), f) == NULL) {
> +		RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
> +			__func__, filename);
> +		fclose(f);
> +		return -1;
> +	}
> +	*val = strtoul(buf, &end, 0);
> +	if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
> +		RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
> +				__func__, filename);
> +		fclose(f);
> +		return -1;
> +	}
> +	fclose(f);
> +	return 0;
> +}
> diff --git a/lib/eal/unix/meson.build b/lib/eal/unix/meson.build
> index e3ecd3e956b2..a22ea7cabc46 100644
> --- a/lib/eal/unix/meson.build
> +++ b/lib/eal/unix/meson.build
> @@ -6,5 +6,6 @@ sources += files(
>          'eal_unix_memory.c',
>          'eal_unix_timer.c',
>          'eal_firmware.c',
> +        'eal_filesystem.c',
>          'rte_thread.c',
>  )
> -- 
> 2.34.1
>
  

Patch

diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c
index 503e276dc27f..eacd432ab621 100644
--- a/lib/eal/freebsd/eal.c
+++ b/lib/eal/freebsd/eal.c
@@ -72,62 +72,6 @@  struct lcore_config lcore_config[RTE_MAX_LCORE];
 /* used by rte_rdtsc() */
 int rte_cycles_vmware_tsc_map;
 
-static const char *default_runtime_dir = "/var/run";
-
-int
-eal_create_runtime_dir(void)
-{
-	const char *directory = default_runtime_dir;
-	const char *xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
-	const char *fallback = "/tmp";
-	char run_dir[PATH_MAX];
-	char tmp[PATH_MAX];
-	int ret;
-
-	if (getuid() != 0) {
-		/* try XDG path first, fall back to /tmp */
-		if (xdg_runtime_dir != NULL)
-			directory = xdg_runtime_dir;
-		else
-			directory = fallback;
-	}
-	/* create DPDK subdirectory under runtime dir */
-	ret = snprintf(tmp, sizeof(tmp), "%s/dpdk", directory);
-	if (ret < 0 || ret == sizeof(tmp)) {
-		RTE_LOG(ERR, EAL, "Error creating DPDK runtime path name\n");
-		return -1;
-	}
-
-	/* create prefix-specific subdirectory under DPDK runtime dir */
-	ret = snprintf(run_dir, sizeof(run_dir), "%s/%s",
-			tmp, eal_get_hugefile_prefix());
-	if (ret < 0 || ret == sizeof(run_dir)) {
-		RTE_LOG(ERR, EAL, "Error creating prefix-specific runtime path name\n");
-		return -1;
-	}
-
-	/* create the path if it doesn't exist. no "mkdir -p" here, so do it
-	 * step by step.
-	 */
-	ret = mkdir(tmp, 0700);
-	if (ret < 0 && errno != EEXIST) {
-		RTE_LOG(ERR, EAL, "Error creating '%s': %s\n",
-			tmp, strerror(errno));
-		return -1;
-	}
-
-	ret = mkdir(run_dir, 0700);
-	if (ret < 0 && errno != EEXIST) {
-		RTE_LOG(ERR, EAL, "Error creating '%s': %s\n",
-			run_dir, strerror(errno));
-		return -1;
-	}
-
-	if (eal_set_runtime_dir(run_dir))
-		return -1;
-
-	return 0;
-}
 
 int
 eal_clean_runtime_dir(void)
@@ -138,38 +82,6 @@  eal_clean_runtime_dir(void)
 	return 0;
 }
 
-/* parse a sysfs (or other) file containing one integer value */
-int
-eal_parse_sysfs_value(const char *filename, unsigned long *val)
-{
-	FILE *f;
-	char buf[BUFSIZ];
-	char *end = NULL;
-
-	if ((f = fopen(filename, "r")) == NULL) {
-		RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
-			__func__, filename);
-		return -1;
-	}
-
-	if (fgets(buf, sizeof(buf), f) == NULL) {
-		RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
-			__func__, filename);
-		fclose(f);
-		return -1;
-	}
-	*val = strtoul(buf, &end, 0);
-	if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
-		RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %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.
  * This means we never close the file while running (auto-close on exit).
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index e9764f6c9f37..b99cab25ce48 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -86,65 +86,6 @@  struct lcore_config lcore_config[RTE_MAX_LCORE];
 /* used by rte_rdtsc() */
 int rte_cycles_vmware_tsc_map;
 
-int
-eal_create_runtime_dir(void)
-{
-	const char *directory;
-	char run_dir[PATH_MAX];
-	char tmp[PATH_MAX];
-	int ret;
-
-	/* from RuntimeDirectory= see systemd.exec */
-	directory = getenv("RUNTIME_DIRECTORY");
-	if (directory == NULL) {
-		/*
-		 * Used standard convention defined in
-		 * XDG Base Directory Specification and
-		 * Filesystem Hierarchy Standard.
-		 */
-		if (getuid() == 0)
-			directory = "/var/run";
-		else
-			directory = getenv("XDG_RUNTIME_DIR") ? : "/tmp";
-	}
-
-	/* create DPDK subdirectory under runtime dir */
-	ret = snprintf(tmp, sizeof(tmp), "%s/dpdk", directory);
-	if (ret < 0 || ret == sizeof(tmp)) {
-		RTE_LOG(ERR, EAL, "Error creating DPDK runtime path name\n");
-		return -1;
-	}
-
-	/* create prefix-specific subdirectory under DPDK runtime dir */
-	ret = snprintf(run_dir, sizeof(run_dir), "%s/%s",
-			tmp, eal_get_hugefile_prefix());
-	if (ret < 0 || ret == sizeof(run_dir)) {
-		RTE_LOG(ERR, EAL, "Error creating prefix-specific runtime path name\n");
-		return -1;
-	}
-
-	/* create the path if it doesn't exist. no "mkdir -p" here, so do it
-	 * step by step.
-	 */
-	ret = mkdir(tmp, 0700);
-	if (ret < 0 && errno != EEXIST) {
-		RTE_LOG(ERR, EAL, "Error creating '%s': %s\n",
-			tmp, strerror(errno));
-		return -1;
-	}
-
-	ret = mkdir(run_dir, 0700);
-	if (ret < 0 && errno != EEXIST) {
-		RTE_LOG(ERR, EAL, "Error creating '%s': %s\n",
-			run_dir, strerror(errno));
-		return -1;
-	}
-
-	if (eal_set_runtime_dir(run_dir))
-		return -1;
-
-	return 0;
-}
 
 int
 eal_clean_runtime_dir(void)
@@ -232,37 +173,6 @@  eal_clean_runtime_dir(void)
 	return -1;
 }
 
-/* parse a sysfs (or other) file containing one integer value */
-int
-eal_parse_sysfs_value(const char *filename, unsigned long *val)
-{
-	FILE *f;
-	char buf[BUFSIZ];
-	char *end = NULL;
-
-	if ((f = fopen(filename, "r")) == NULL) {
-		RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
-			__func__, filename);
-		return -1;
-	}
-
-	if (fgets(buf, sizeof(buf), f) == NULL) {
-		RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
-			__func__, filename);
-		fclose(f);
-		return -1;
-	}
-	*val = strtoul(buf, &end, 0);
-	if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
-		RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %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/eal/unix/eal_filesystem.c b/lib/eal/unix/eal_filesystem.c
new file mode 100644
index 000000000000..4759651a9581
--- /dev/null
+++ b/lib/eal/unix/eal_filesystem.c
@@ -0,0 +1,110 @@ 
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2010-2018 Intel Corporation.
+ * Copyright(c) 2012-2014 6WIND S.A.
+ */
+
+#include <errno.h>
+#include <limits.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <rte_common.h>
+#include <rte_log.h>
+
+#include "eal_private.h"
+#include "eal_filesystem.h"
+
+int
+eal_create_runtime_dir(void)
+{
+	const char *directory;
+	char run_dir[PATH_MAX];
+	char tmp[PATH_MAX];
+	int ret;
+
+	/* from RuntimeDirectory= see systemd.exec */
+	directory = getenv("RUNTIME_DIRECTORY");
+	if (directory == NULL) {
+		/*
+		 * Used standard convention defined in
+		 * XDG Base Directory Specification and
+		 * Filesystem Hierarchy Standard.
+		 */
+		if (getuid() == 0)
+			directory = "/var/run";
+		else
+			directory = getenv("XDG_RUNTIME_DIR") ? : "/tmp";
+	}
+
+	/* create DPDK subdirectory under runtime dir */
+	ret = snprintf(tmp, sizeof(tmp), "%s/dpdk", directory);
+	if (ret < 0 || ret == sizeof(tmp)) {
+		RTE_LOG(ERR, EAL, "Error creating DPDK runtime path name\n");
+		return -1;
+	}
+
+	/* create prefix-specific subdirectory under DPDK runtime dir */
+	ret = snprintf(run_dir, sizeof(run_dir), "%s/%s",
+			tmp, eal_get_hugefile_prefix());
+	if (ret < 0 || ret == sizeof(run_dir)) {
+		RTE_LOG(ERR, EAL, "Error creating prefix-specific runtime path name\n");
+		return -1;
+	}
+
+	/* create the path if it doesn't exist. no "mkdir -p" here, so do it
+	 * step by step.
+	 */
+	ret = mkdir(tmp, 0700);
+	if (ret < 0 && errno != EEXIST) {
+		RTE_LOG(ERR, EAL, "Error creating '%s': %s\n",
+			tmp, strerror(errno));
+		return -1;
+	}
+
+	ret = mkdir(run_dir, 0700);
+	if (ret < 0 && errno != EEXIST) {
+		RTE_LOG(ERR, EAL, "Error creating '%s': %s\n",
+			run_dir, strerror(errno));
+		return -1;
+	}
+
+	if (eal_set_runtime_dir(run_dir))
+		return -1;
+
+	return 0;
+}
+/* parse a sysfs (or other) file containing one integer value */
+int
+eal_parse_sysfs_value(const char *filename, unsigned long *val)
+{
+	FILE *f;
+	char buf[BUFSIZ];
+	char *end = NULL;
+
+	if ((f = fopen(filename, "r")) == NULL) {
+		RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
+			__func__, filename);
+		return -1;
+	}
+
+	if (fgets(buf, sizeof(buf), f) == NULL) {
+		RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
+			__func__, filename);
+		fclose(f);
+		return -1;
+	}
+	*val = strtoul(buf, &end, 0);
+	if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
+		RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
+				__func__, filename);
+		fclose(f);
+		return -1;
+	}
+	fclose(f);
+	return 0;
+}
diff --git a/lib/eal/unix/meson.build b/lib/eal/unix/meson.build
index e3ecd3e956b2..a22ea7cabc46 100644
--- a/lib/eal/unix/meson.build
+++ b/lib/eal/unix/meson.build
@@ -6,5 +6,6 @@  sources += files(
         'eal_unix_memory.c',
         'eal_unix_timer.c',
         'eal_firmware.c',
+        'eal_filesystem.c',
         'rte_thread.c',
 )