summaryrefslogtreecommitdiffstats
path: root/src/vppinfra
diff options
context:
space:
mode:
authorTom Jones <thj@freebsd.org>2024-04-24 10:30:20 +0000
committerTom Jones <thj@freebsd.org>2024-04-24 10:30:20 +0000
commitab47993a00e8854d64ae90de0cdd4b9272653473 (patch)
treebbf7b7fc9ac6ae379506fcc1914e040e265831b6 /src/vppinfra
parentc0580f91a14cb4a68f1978c6702a91efc46d12a9 (diff)
vppinfra: Add method for getting current executable name
Add a unix method for getting the current executable name. This is implemented to match the readlink api for existing calls. Type: improvement Change-Id: Id06a55892d09d0b305a56b55a424f53ffb685a72 Signed-off-by: Tom Jones <thj@freebsd.org> Signed-off-by: Damjan Marion <damarion@cisco.com>
Diffstat (limited to 'src/vppinfra')
-rw-r--r--src/vppinfra/unix-misc.c24
-rw-r--r--src/vppinfra/unix.h4
2 files changed, 28 insertions, 0 deletions
diff --git a/src/vppinfra/unix-misc.c b/src/vppinfra/unix-misc.c
index 4dbc5ce98ce..29cbe0a557d 100644
--- a/src/vppinfra/unix-misc.c
+++ b/src/vppinfra/unix-misc.c
@@ -42,6 +42,8 @@
#include <vppinfra/format.h>
#ifdef __linux__
#include <vppinfra/linux/sysfs.h>
+#else
+#include <sys/sysctl.h>
#endif
#include <sys/stat.h>
@@ -358,6 +360,28 @@ os_get_cpu_phys_core_id (int cpu_id)
#endif
}
+__clib_export u8 *
+os_get_exec_path ()
+{
+ u8 *rv = 0;
+#ifdef __linux__
+ char tmp[PATH_MAX];
+ ssize_t sz = readlink ("/proc/self/exe", tmp, sizeof (tmp));
+
+ if (sz <= 0)
+ return 0;
+#else
+ char tmp[MAXPATHLEN];
+ int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
+ size_t sz = MAXPATHLEN;
+
+ if (sysctl (mib, 4, tmp, &sz, NULL, 0) == -1)
+ return 0;
+#endif
+ vec_add (rv, tmp, sz);
+ return rv;
+}
+
/*
* fd.io coding-style-patch-verification: ON
*
diff --git a/src/vppinfra/unix.h b/src/vppinfra/unix.h
index 3ad57b05e72..abda21879f9 100644
--- a/src/vppinfra/unix.h
+++ b/src/vppinfra/unix.h
@@ -71,6 +71,10 @@ clib_bitmap_t *os_get_cpu_on_node_bitmap (int node);
/* Retrieve physical core id of specific cpu, -1 if not available */
int os_get_cpu_phys_core_id (int cpu);
+/* Retrieve the path of the current executable as a vector (not
+ * null-terminated). */
+u8 *os_get_exec_path ();
+
#endif /* included_clib_unix_h */
/*