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

Stephen Hemminger
2026-01-17T02:38:36Z

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>
Acked-by: Marat Khalili <marat.khalili@huawei.com>
---
app/test/process.h | 44 +++++++++----
app/test/test_eal_flags.c | 132 ++++++++++----------------------------
2 files changed, 68 insertions(+), 108 deletions(-)

diff --git a/app/test/process.h b/app/test/process.h
index 0704616c38..402dc4f96f 100644
--- a/app/test/process.h
+++ b/app/test/process.h
@@ -225,27 +225,49 @@ 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 buf[PATH_MAX];

- /* get file for config (fd is always 3) */
- snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);
-
- /* return NULL on error */
- if (readlink(path, buf, sizeof(buf)) == -1)
+ /* get file for config (fd is always 3) return NULL on error */
+ if (readlink("/proc/self/fd/3", 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

diff --git a/app/test/test_eal_flags.c b/app/test/test_eal_flags.c
index 4ddcafdecb..10a5ceb579 100644
--- a/app/test/test_eal_flags.c
+++ b/app/test/test_eal_flags.c
@@ -121,6 +121,8 @@ test_misc_flags(void)
#define no_shconf "--no-shconf"
#define allow "--allow"
#define vdev "--vdev"
+#define file_prefix "--file-prefix"
+
#define memtest "memtest"
#define memtest1 "memtest1"
#define memtest2 "memtest2"
@@ -320,18 +322,10 @@ get_number_of_sockets(void)
static int
test_allow_flag(void)
{
- unsigned i;
-#ifdef RTE_EXEC_ENV_FREEBSD
- /* BSD target doesn't support prefixes at this point */
- const char * prefix = "";
-#else
- char prefix[PATH_MAX], tmp[PATH_MAX];
- if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
- printf("Error - unable to get current prefix!\n");
+ unsigned int i;
+ const char *prefix = file_prefix_arg();
+ if (prefix == NULL)
return -1;
- }
- snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
-#endif

const char *wlinval[][7] = {
{prgname, prefix, mp_flag,
@@ -387,17 +381,9 @@ test_allow_flag(void)
static int
test_invalid_b_flag(void)
{
-#ifdef RTE_EXEC_ENV_FREEBSD
- /* BSD target doesn't support prefixes at this point */
- const char * prefix = "";
-#else
- char prefix[PATH_MAX], tmp[PATH_MAX];
- if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
- printf("Error - unable to get current prefix!\n");
+ const char *prefix = file_prefix_arg();
+ if (prefix == NULL)
return -1;
- }
- snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
-#endif

const char *blinval[][5] = {
{prgname, prefix, mp_flag, "-b", "error"},
@@ -491,17 +477,9 @@ test_invalid_vdev_flag(void)
static int
test_invalid_r_flag(void)
{
-#ifdef RTE_EXEC_ENV_FREEBSD
- /* BSD target doesn't support prefixes at this point */
- const char * prefix = "";
-#else
- char prefix[PATH_MAX], tmp[PATH_MAX];
- if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
- printf("Error - unable to get current prefix!\n");
+ const char *prefix = file_prefix_arg();
+ if (prefix == NULL)
return -1;
- }
- snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
-#endif

const char *rinval[][5] = {
{prgname, prefix, mp_flag, "-r", "error"},
@@ -535,17 +513,9 @@ test_invalid_r_flag(void)
static int
test_missing_c_flag(void)
{
-#ifdef RTE_EXEC_ENV_FREEBSD
- /* BSD target doesn't support prefixes at this point */
- const char * prefix = "";
-#else
- char prefix[PATH_MAX], tmp[PATH_MAX];
- if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
- printf("Error - unable to get current prefix!\n");
+ const char *prefix = file_prefix_arg();
+ if (prefix == NULL)
return -1;
- }
- snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
-#endif

/* -c flag but no coremask value */
const char *argv1[] = { prgname, prefix, mp_flag, "-c"};
@@ -694,17 +664,9 @@ test_missing_c_flag(void)
static int
test_main_lcore_flag(void)
{
-#ifdef RTE_EXEC_ENV_FREEBSD
- /* BSD target doesn't support prefixes at this point */
- const char *prefix = "";
-#else
- char prefix[PATH_MAX], tmp[PATH_MAX];
- if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
- printf("Error - unable to get current prefix!\n");
+ const char *prefix = file_prefix_arg();
+ if (prefix == NULL)
return -1;
- }
- snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
-#endif

if (!rte_lcore_is_enabled(0) || !rte_lcore_is_enabled(1))
return TEST_SKIPPED;
@@ -751,17 +713,9 @@ test_main_lcore_flag(void)
static int
test_invalid_n_flag(void)
{
-#ifdef RTE_EXEC_ENV_FREEBSD
- /* BSD target doesn't support prefixes at this point */
- const char * prefix = "";
-#else
- char prefix[PATH_MAX], tmp[PATH_MAX];
- if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
- printf("Error - unable to get current prefix!\n");
+ const char *prefix = file_prefix_arg();
+ if (prefix == NULL)
return -1;
- }
- snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
-#endif

/* -n flag but no value */
const char *argv1[] = { prgname, prefix, no_huge, no_shconf,
@@ -803,18 +757,12 @@ test_invalid_n_flag(void)
static int
test_no_hpet_flag(void)
{
- char prefix[PATH_MAX] = "";
-
#ifdef RTE_EXEC_ENV_FREEBSD
return 0;
#else
- char tmp[PATH_MAX];
- if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
- printf("Error - unable to get current prefix!\n");
+ const char *prefix = file_prefix_arg();
+ if (prefix == NULL)
return -1;
- }
- snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
-#endif

/* With --no-hpet */
const char *argv1[] = {prgname, prefix, mp_flag, no_hpet};
@@ -830,6 +778,7 @@ test_no_hpet_flag(void)
return -1;
}
return 0;
+#endif
}

/*
@@ -853,6 +802,18 @@ test_no_huge_flag(void)
const char *argv2[] = {prgname, prefix, no_huge,
"-m", DEFAULT_MEM_SIZE};

+ if (launch_proc(argv1) != 0) {
+ printf("Error - process did not run ok with --no-huge flag\n");
+ return -1;
+ }
+ if (launch_proc(argv2) != 0) {
+ printf("Error - process did not run ok with --no-huge and -m flags\n");
+ return -1;
+ }
+#ifdef RTE_EXEC_ENV_FREEBSD
+ /* no other tests are applicable to FreeBSD */
+ return 0;
+#else
/* With --no-huge and --socket-mem */
const char *argv3[] = {prgname, prefix, no_huge,
"--socket-mem=" DEFAULT_MEM_SIZE};
@@ -867,19 +828,6 @@ test_no_huge_flag(void)
const char * const argv6[] = {prgname, prefix, no_huge,
"--huge-worker-stack=512"};

- if (launch_proc(argv1) != 0) {
- printf("Error - process did not run ok with --no-huge flag\n");
- return -1;
- }
- if (launch_proc(argv2) != 0) {
- printf("Error - process did not run ok with --no-huge and -m flags\n");
- return -1;
- }
-#ifdef RTE_EXEC_ENV_FREEBSD
- /* no other tests are applicable to FreeBSD */
- return 0;
-#endif
-
if (launch_proc(argv3) == 0) {
printf("Error - process run ok with --no-huge and --socket-mem "
"flags\n");
@@ -898,6 +846,7 @@ test_no_huge_flag(void)
printf("Error - process run ok with --no-huge and --huge-worker-stack=size flags");
return -1;
}
+#endif
return 0;
}

@@ -913,17 +862,14 @@ test_misc_flags(void)
const char * prefix = "";
const char * nosh_prefix = "";
#else
- char prefix[PATH_MAX], tmp[PATH_MAX];
+ const char *prefix = file_prefix_arg();
const char * nosh_prefix = "--file-prefix=noshconf";
FILE * hugedir_handle = NULL;
char line[PATH_MAX] = {0};
unsigned i, isempty = 1;

- if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
- printf("Error - unable to get current prefix!\n");
+ if (prefix == NULL)
return -1;
- }
- snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);

/*
* get first valid hugepage path
@@ -1525,17 +1471,9 @@ populate_socket_mem_param(int num_sockets, const char *mem,
static int
test_memory_flags(void)
{
-#ifdef RTE_EXEC_ENV_FREEBSD
- /* BSD target doesn't support prefixes at this point */
- const char * prefix = "";
-#else
- char prefix[PATH_MAX], tmp[PATH_MAX];
- if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
- printf("Error - unable to get current prefix!\n");
+ const char *prefix = file_prefix_arg();
+ if (prefix == NULL)
return -1;
- }
- snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
-#endif

/* valid -m flag and mp flag */
const char *argv0[] = {prgname, prefix, mp_flag,
--
2.51.0