From ab47993a00e8854d64ae90de0cdd4b9272653473 Mon Sep 17 00:00:00 2001 From: Tom Jones Date: Wed, 24 Apr 2024 10:30:20 +0000 Subject: 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 Signed-off-by: Damjan Marion --- src/vppinfra/unix-misc.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/vppinfra/unix-misc.c') 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 #ifdef __linux__ #include +#else +#include #endif #include @@ -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 * -- cgit 1.2.3-korg