Skip to contentSkip to conversation

app/test: simplify getting the current file prefix

PATCH In review Bruce Richardson 3 participants 7 messages Feb 20 – Mar 5

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 app/test: simplify getting the current file prefix

Rather than opening particular fd's in the /proc directory, just use the rte_eal_get_runtime_dir() function to derive the file-prefix in use. Since the runtime dir call cannot return NULL, we can simplify/remove the error handling where it's used.

---
 app/test/process.h | 23 ++++-------------------
 1 file changed, 4 insertions(+), 19 deletions(-)
app/test/process.h +4 -19
index 402dc4f96f..1ab5671943 100644
@@ -8,12 +8,11 @@
 #include <errno.h>  /* errno */
 #include <limits.h> /* PATH_MAX */
 #ifndef RTE_EXEC_ENV_WINDOWS
-#include <libgen.h> /* basename et al */
 #include <sys/wait.h>
 #endif
 #include <stdlib.h> /* NULL */
 #include <string.h> /* strerror */
-#include <unistd.h> /* readlink */
+#include <unistd.h>
 #include <dirent.h>
 
 #include <rte_string_fns.h> /* strlcpy */
@@ -239,17 +238,7 @@ file_prefix_arg(void)
 static inline char *
 get_current_prefix(char *prefix, int size)
 {
-	char buf[PATH_MAX];
-
-	/* get file for config (fd is always 3) return NULL on error */
-	if (readlink("/proc/self/fd/3", buf, sizeof(buf)) == -1)
-		return NULL;
-
-	/*
-	 * path should be something like "/var/run/dpdk/config"
-	 * which results in prefix of "dpdk"
-	 */
-	rte_basename(dirname(buf), prefix, size);
+	rte_basename(rte_eal_get_runtime_dir(), prefix, size);
 	return prefix;
 }
 
@@ -260,12 +249,8 @@ file_prefix_arg(void)
 	static char prefix[NAME_MAX + sizeof("--file-prefix=")];
 	char tmp[NAME_MAX];
 
-	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
-		fprintf(stderr, "Error - unable to get current prefix!\n");
-		return NULL;
-	}
-
-	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
+	snprintf(prefix, sizeof(prefix), "--file-prefix=%s",
+			get_current_prefix(tmp, sizeof(tmp)));
 	return prefix;
 #endif
 }
Signature · 1 line
2.51.0
Acked-by Tested-by on v1 1/1
#1 lore raw
Quoted 5 lines
app/test/process.h +0 -0
index 402dc4f96f..1ab5671943 100644

app/test/process.h line 15

Quoted 10 lines
app/test/process.h +1 -2
@@ -8,12 +8,11 @@
 #include <errno.h>  /* errno */
 #include <limits.h> /* PATH_MAX */
 #ifndef RTE_EXEC_ENV_WINDOWS
-#include <libgen.h> /* basename et al */
 #include <sys/wait.h>
 #endif
 #include <stdlib.h> /* NULL */
 #include <string.h> /* strerror */
-#include <unistd.h> /* readlink */
+#include <unistd.h>

Can we delete it entirely? Compiles for me at least.

Quoted 10 lines
app/test/process.h +0 -3
 #include <dirent.h>
 
 #include <rte_string_fns.h> /* strlcpy */
@@ -239,17 +238,7 @@ file_prefix_arg(void)
 static inline char *
 get_current_prefix(char *prefix, int size)
 {
-	char buf[PATH_MAX];
-
-	/* get file for config (fd is always 3) return NULL on error */

app/test/process.h file_prefix_arg(void) · line 242

Quoted 10 lines
app/test/process.h +1 -8
-	if (readlink("/proc/self/fd/3", buf, sizeof(buf)) == -1)
-		return NULL;
-
-	/*
-	 * path should be something like "/var/run/dpdk/config"
-	 * which results in prefix of "dpdk"
-	 */
-	rte_basename(dirname(buf), prefix, size);
+	rte_basename(rte_eal_get_runtime_dir(), prefix, size);
 	return prefix;

Do we even need it as a separate function anymore?

Quoted 18 lines
app/test/process.h +2 -7
 }
 
@@ -260,12 +249,8 @@ file_prefix_arg(void)
 	static char prefix[NAME_MAX + sizeof("--file-prefix=")];
 	char tmp[NAME_MAX];
 
-	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
-		fprintf(stderr, "Error - unable to get current prefix!\n");
-		return NULL;
-	}
-
-	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
+	snprintf(prefix, sizeof(prefix), "--file-prefix=%s",
+			get_current_prefix(tmp, sizeof(tmp)));
 	return prefix;
 #endif
 }
--
Quoted 1 line

2.51.0

With or without comments above addressed,

#2 lore raw
Quoted 9 lines · Marat Khalili
Quoted 9 lines
app/test/process.h +0 -1
index 402dc4f96f..1ab5671943 100644
@@ -8,12 +8,11 @@
 #include <errno.h>  /* errno */
 #include <limits.h> /* PATH_MAX */
 #ifndef RTE_EXEC_ENV_WINDOWS
-#include <libgen.h> /* basename et al */
Quoted 9 lines

app/test/process.h line 15

Quoted 6 lines
app/test/process.h +1 -1
 #include <sys/wait.h>
 #endif
 #include <stdlib.h> /* NULL */
 #include <string.h> /* strerror */
-#include <unistd.h> /* readlink */
+#include <unistd.h>

Can we delete it entirely? Compiles for me at least.

I believe there are still functions used from it, so I would keep it for safety.

Quoted 14 lines
Quoted 14 lines
app/test/process.h +0 -7
 #include <dirent.h>
 
 #include <rte_string_fns.h> /* strlcpy */
@@ -239,17 +238,7 @@ file_prefix_arg(void)
 static inline char *
 get_current_prefix(char *prefix, int size)
 {
-	char buf[PATH_MAX];
-
-	/* get file for config (fd is always 3) return NULL on error */
-	if (readlink("/proc/self/fd/3", buf, sizeof(buf)) == -1)
-		return NULL;
-
-	/*
Quoted 9 lines

app/test/process.h file_prefix_arg(void) · line 242

Quoted 6 lines
app/test/process.h +1 -4
-	 * path should be something like "/var/run/dpdk/config"
-	 * which results in prefix of "dpdk"
-	 */
-	rte_basename(dirname(buf), prefix, size);
+	rte_basename(rte_eal_get_runtime_dir(), prefix, size);
 	return prefix;

Do we even need it as a separate function anymore?

This function is used in test_eal_flags.c, which is why it's left here as separate function.

Quoted 24 lines
Quoted 18 lines
app/test/process.h +2 -7
 }
 
@@ -260,12 +249,8 @@ file_prefix_arg(void)
 	static char prefix[NAME_MAX + sizeof("--file-prefix=")];
 	char tmp[NAME_MAX];
 
-	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
-		fprintf(stderr, "Error - unable to get current prefix!\n");
-		return NULL;
-	}
-
-	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
+	snprintf(prefix, sizeof(prefix), "--file-prefix=%s",
+			get_current_prefix(tmp, sizeof(tmp)));
 	return prefix;
 #endif
 }
--
Quoted 1 line

2.51.0

With or without comments above addressed,

#3 lore raw

PATCH v2 1/2 app/test: fix ifdefs in header for managing subprocesses

In reply to Bruce Richardson

The #endif for the #ifdef Linux block to add the "get_current_prefix()" and the "file_prefix_arg()" functions to process.h was on the wrong line, leaving a stray "}" outside the block. Fix this and in the process (no pun intended) simplify the #ifdefs to just a non-Linux and a Linux block, removing one level of #ifdefs.

Fixes f17b6eacf00a ("test: avoid file prefix overflow")
---
 app/test/process.h | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
app/test/process.h +5 -9
index 402dc4f96f..a21cdfd46d 100644
@@ -225,17 +225,14 @@ process_dup(const char *const argv[], int numargs, const char *env_value)
 	return status;
 }
 
-/* FreeBSD doesn't support file prefixes, so no argument passed. */
-#ifdef RTE_EXEC_ENV_FREEBSD
+/* Only Linux supports file prefixes. */
+#ifndef RTE_EXEC_ENV_LINUX
 static inline const char *
 file_prefix_arg(void)
 {
-	/* BSD target doesn't support prefixes at this point */
 	return "";
 }
-#else
-
-#ifdef RTE_EXEC_ENV_LINUX
+#else /* RTE_EXEC_ENV_LINUX */
 static inline char *
 get_current_prefix(char *prefix, int size)
 {
@@ -253,7 +250,7 @@ get_current_prefix(char *prefix, int size)
 	return prefix;
 }
 
-/* Return a --file-prefix=XXXX argument or NULL */
+/* Return a --file-prefix=XXXX argument */
 static inline const char *
 file_prefix_arg(void)
 {
@@ -267,8 +264,7 @@ file_prefix_arg(void)
 
 	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
 	return prefix;
-#endif
 }
-#endif
+#endif /* RTE_EXEC_ENV_LINUX */
 
 #endif /* _PROCESS_H_ */
Signature · 1 line
2.51.0
#4 lore raw

PATCH v2 2/2 app/test: simplify getting the current file prefix

Rather than opening particular fd's in the /proc directory, just use the rte_eal_get_runtime_dir() function to derive the file-prefix in use. Since the runtime dir call cannot return NULL, we can simplify/remove the error handling where it's used.

---
 app/test/process.h | 23 ++++-------------------
 1 file changed, 4 insertions(+), 19 deletions(-)
app/test/process.h +4 -19
index a21cdfd46d..df43966a2a 100644
@@ -8,12 +8,11 @@
 #include <errno.h>  /* errno */
 #include <limits.h> /* PATH_MAX */
 #ifndef RTE_EXEC_ENV_WINDOWS
-#include <libgen.h> /* basename et al */
 #include <sys/wait.h>
 #endif
 #include <stdlib.h> /* NULL */
 #include <string.h> /* strerror */
-#include <unistd.h> /* readlink */
+#include <unistd.h>
 #include <dirent.h>
 
 #include <rte_string_fns.h> /* strlcpy */
@@ -236,17 +235,7 @@ file_prefix_arg(void)
 static inline char *
 get_current_prefix(char *prefix, int size)
 {
-	char buf[PATH_MAX];
-
-	/* get file for config (fd is always 3) return NULL on error */
-	if (readlink("/proc/self/fd/3", buf, sizeof(buf)) == -1)
-		return NULL;
-
-	/*
-	 * path should be something like "/var/run/dpdk/config"
-	 * which results in prefix of "dpdk"
-	 */
-	rte_basename(dirname(buf), prefix, size);
+	rte_basename(rte_eal_get_runtime_dir(), prefix, size);
 	return prefix;
 }
 
@@ -257,12 +246,8 @@ file_prefix_arg(void)
 	static char prefix[NAME_MAX + sizeof("--file-prefix=")];
 	char tmp[NAME_MAX];
 
-	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
-		fprintf(stderr, "Error - unable to get current prefix!\n");
-		return NULL;
-	}
-
-	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
+	snprintf(prefix, sizeof(prefix), "--file-prefix=%s",
+			get_current_prefix(tmp, sizeof(tmp)));
 	return prefix;
 }
 #endif /* RTE_EXEC_ENV_LINUX */
Signature · 1 line
2.51.0
#5 lore raw
Quoted 9 lines · Bruce Richardson

Rather than opening particular fd's in the /proc directory, just use the rte_eal_get_runtime_dir() function to derive the file-prefix in use. Since the runtime dir call cannot return NULL, we can simplify/remove the error handling where it's used.

Series applied, thanks Bruce.

Signature · 1 line
David Marchand
Acked-by on v2 1/2
#6 lore raw

RE: [PATCH v2 1/2] app/test: fix ifdefs in header for managing subprocesses

In reply to Bruce Richardson
Quoted 66 lines

-----Original Message-----
From: Bruce Richardson <[email protected]>
Sent: Wednesday 25 February 2026 16:21
To: [email protected]
Cc: Bruce Richardson <[email protected]>; [email protected]
Subject: [PATCH v2 1/2] app/test: fix ifdefs in header for managing subprocesses

The #endif for the #ifdef Linux block to add the "get_current_prefix()" and the "file_prefix_arg()" functions to process.h was on the wrong line, leaving a stray "}" outside the block. Fix this and in the process (no pun intended) simplify the #ifdefs to just a non-Linux and a Linux block, removing one level of #ifdefs.

Fixes f17b6eacf00a ("test: avoid file prefix overflow")
---
 app/test/process.h | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
app/test/process.h +5 -10
index 402dc4f96f..a21cdfd46d 100644
@@ -225,17 +225,14 @@ process_dup(const char *const argv[], int numargs, const char *env_value)
 	return status;
 }
 
-/* FreeBSD doesn't support file prefixes, so no argument passed. */
-#ifdef RTE_EXEC_ENV_FREEBSD
+/* Only Linux supports file prefixes. */
+#ifndef RTE_EXEC_ENV_LINUX
 static inline const char *
 file_prefix_arg(void)
 {
-	/* BSD target doesn't support prefixes at this point */
 	return "";
 }
-#else
-
-#ifdef RTE_EXEC_ENV_LINUX
+#else /* RTE_EXEC_ENV_LINUX */
 static inline char *
 get_current_prefix(char *prefix, int size)
 {
@@ -253,7 +250,7 @@ get_current_prefix(char *prefix, int size)
 	return prefix;
 }
 
-/* Return a --file-prefix=XXXX argument or NULL */
+/* Return a --file-prefix=XXXX argument */
 static inline const char *
 file_prefix_arg(void)
 {
@@ -267,8 +264,7 @@ file_prefix_arg(void)
 
 	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
 	return prefix;
-#endif
 }
-#endif
+#endif /* RTE_EXEC_ENV_LINUX */
 
 #endif /* _PROCESS_H_ */
--
Quoted 1 line

2.51.0

Tested on Linux.