[PATCH v9 2/9] test: add new function to get current file-prefix arg

Stephen Hemminger
2025-12-01T22:04:42Z

Refactor so that there is one function to get current argument
for "--file-prefix=XXX".

The code does not need to initialize all these 4K buffers since
they are directly used by functions that set them.
The maximum sizeof of the current DPDK prefix is NAME_MAX (128)
not PATH_MAX(4096) so save a significant amount of space.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
app/test/process.h | 38 ++++++++++++++++++++++++++++++++------
1 file changed, 32 insertions(+), 6 deletions(-)

diff --git a/app/test/process.h b/app/test/process.h
index f948a89786..251262d948 100644
--- a/app/test/process.h
+++ b/app/test/process.h
@@ -199,15 +199,22 @@ process_dup(const char *const argv[], int numargs, const char *env_value)
return status;
}

-/* FreeBSD doesn't support file prefixes, so force compile failures for any
- * tests attempting to use this function on FreeBSD.
- */
+/* FreeBSD doesn't support file prefixes, so no argument passed. */
+#ifdef RTE_EXEC_ENV_FREEBSD
+static inline const char *
+file_prefix_arg(void)
+{
+ /* BSD target doesn't support prefixes at this point */
+ return "";
+}
+#else
+
#ifdef RTE_EXEC_ENV_LINUX
static inline char *
get_current_prefix(char *prefix, int size)
{
- char path[PATH_MAX] = {0};
- char buf[PATH_MAX] = {0};
+ char path[64];
+ char buf[PATH_MAX];

/* get file for config (fd is always 3) */
snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);
@@ -216,10 +223,29 @@ get_current_prefix(char *prefix, int size)
if (readlink(path, buf, sizeof(buf)) == -1)
return NULL;

- /* get the prefix */
+ /*
+ * path should be something like "/var/run/dpdk/config"
+ * which results in prefix of "dpdk"
+ */
rte_basename(dirname(buf), prefix, size);
+ return prefix;
+}
+
+/* Return a --file-prefix=XXXX argument or NULL */
+static inline const char *
+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);
return prefix;
+#endif
}
#endif

--
2.51.0