Skip to contentSkip to conversation

Use cross-platform rte_basename function

PATCH Applied Bruce Richardson 2 participants 5 messages Aug 6, 2025 – Oct 15, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Use cross-platform rte_basename function

To enable more of DPDK to be buildable and usable on Windows, common code should use rte_basename in place of regular basename() function. Therefore update EAL common code and code in apps to use that new rte_basename function.

In some cases rte_basename is easier to use than basename, since it outputs to a new buffer, so also update idxd driver to use rte_basename, since it simplifies the code, even though that code does not need to work on non-Linux systems.

Depends-on: series-35845 ("improve cmdline file handling in testpmd")

Bruce Richardson (3):
app: use cross-platform basename function
eal: use common basename function
dma/idxd: use DPDK basename function

 app/test-dma-perf/main.c         |  6 +++---
 app/test/process.h               |  2 +-
 drivers/dma/idxd/idxd_bus.c      | 12 ++++--------
 lib/eal/common/eal_common_proc.c |  5 +----
 4 files changed, 9 insertions(+), 16 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH 1/3 app: use cross-platform basename function

Replace use of the Linux/BSD-only basename function with "rte_basename". Since the DPDK basename function is guaranteed to do a copy into the destination buffer of known length, we can sometimes simplify the code, or make it safer, as part of this change.

---
 app/test-dma-perf/main.c | 6 +++---
 app/test/process.h       | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
app/test-dma-perf/main.c +3 -3
index 0586b3e1d0..3ad66637e6 100644
@@ -549,13 +549,13 @@ main(int argc, char *argv[])
 		return -1;
 	}
 	if (rst_path_ptr == NULL) {
-		strlcpy(rst_path, cfg_path_ptr, PATH_MAX);
-		char *token = strtok(basename(rst_path), ".");
+		rte_basename(cfg_path_ptr, rst_path, sizeof(rst_path));
+		char *token = strtok(rst_path, ".");
 		if (token == NULL) {
 			printf("Config file error.\n");
 			return -1;
 		}
-		strcat(token, "_result.csv");
+		strlcat(token, "_result.csv", sizeof(rst_path));
 		rst_path_ptr = rst_path;
 	}
 
app/test/process.h +1 -1
index 9fb2bf481c..0dba48aa18 100644
@@ -217,7 +217,7 @@ get_current_prefix(char *prefix, int size)
 		return NULL;
 
 	/* get the prefix */
-	snprintf(prefix, size, "%s", basename(dirname(buf)));
+	rte_basename(dirname(buf), prefix, size);
 
 	return prefix;
 }
Signature · 1 line
2.48.1
#2 lore raw

PATCH 2/3 eal: use common basename function

In reply to Bruce Richardson

Use the new "rte_basename" function which has common behaviour on all supported platforms. As extra benefit, it removes the need for recreating the socket path twice, since rte_basename guarantees not to modify its argument.

---
 lib/eal/common/eal_common_proc.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
lib/eal/common/eal_common_proc.c +1 -4
index 0dea787e38..62fd4ba88f 100644
@@ -625,10 +625,7 @@ rte_mp_channel_init(void)
 
 	/* create filter path */
 	create_socket_path("*", path, sizeof(path));
-	strlcpy(mp_filter, basename(path), sizeof(mp_filter));
-
-	/* path may have been modified, so recreate it */
-	create_socket_path("*", path, sizeof(path));
+	rte_basename(path, mp_filter, sizeof(mp_filter));
 	strlcpy(mp_dir_path, dirname(path), sizeof(mp_dir_path));
 
 	/* lock the directory */
Signature · 1 line
2.48.1
#3 lore raw

PATCH 3/3 dma/idxd: use DPDK basename function

In reply to Bruce Richardson

Even though the idxd_bus.c file is linux-only, we can simplify the code through using rte_basename rather than just basename. Since rte_basename does not modify its parameter, we can eliminate the strdup and free, and since it also handles NULL sanely, we can remove an extra NULL check.

---
 drivers/dma/idxd/idxd_bus.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
drivers/dma/idxd/idxd_bus.c +4 -8
index ba8076715d..5ae42151e6 100644
@@ -260,17 +260,14 @@ static int search_devargs(const char *name)
 static int
 is_for_this_process_use(struct rte_dsa_device *dev, const char *name)
 {
-	char *runtime_dir = strdup(rte_eal_get_runtime_dir());
+	char prefix[256];
 	int retval = 0;
-	int prefixlen;
-	char *prefix;
+	size_t prefixlen;
 
-	if (runtime_dir == NULL)
+	prefixlen = rte_basename(rte_eal_get_runtime_dir(), prefix, sizeof(prefix));
+	if (prefixlen >= sizeof(prefix) || strcmp(prefix, ".") == 0)
 		return retval;
 
-	prefix = basename(runtime_dir);
-	prefixlen = strlen(prefix);
-
 	if (strncmp(name, "dpdk_", 5) == 0)
 		retval = 1;
 	if (strncmp(name, prefix, prefixlen) == 0 && name[prefixlen] == '_')
@@ -283,7 +280,6 @@ is_for_this_process_use(struct rte_dsa_device *dev, const char *name)
 			retval = !search_devargs(dev->device.name);
 	}
 
-	free(runtime_dir);
 	return retval;
 }
 
Signature · 1 line
2.48.1