[Mesa-dev] [PATCH 4/4] util: Get program name based on path when possible

Nicholas Kazlauskas nicholas.kazlauskas at amd.com
Tue Sep 11 16:24:17 UTC 2018


Some programs start with the path and command line arguments in
argv[0] (program_invocation_name). Chromium is an example of
an application using mesa that does this.

This tries to query the real path for the symbolic link /proc/self/exe
to find the program name instead.

Signed-off-by: Nicholas Kazlauskas <nicholas.kazlauskas at amd.com>
---
 src/util/u_process.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/src/util/u_process.c b/src/util/u_process.c
index 5e5927678d..ca4d0770cb 100644
--- a/src/util/u_process.c
+++ b/src/util/u_process.c
@@ -29,6 +29,7 @@
 #include <string.h>
 #include <errno.h>
 #include <stdlib.h>
+#include <stdio.h>
 
 #undef GET_PROGRAM_NAME
 
@@ -36,22 +37,32 @@
 #    if !defined(__GLIBC__) || (__GLIBC__ < 2)
 /* These aren't declared in any libc5 header */
 extern char *program_invocation_name, *program_invocation_short_name;
+extern char *__progname;
 #    endif
 static const char *
 __getProgramName()
 {
-   char * arg = strrchr(program_invocation_name, '/');
+   static char * actual_path;
+   char * path = NULL;
+   char * arg = NULL;
+
+   if (!actual_path)
+      actual_path = realpath("/proc/self/exe", NULL);
+
+   path = actual_path ? actual_path : program_invocation_name;
+
+   arg = strrchr(path, '/');
    if (arg)
       return arg+1;
 
    /* If there was no '/' at all we likely have a windows like path from
     * a wine application.
     */
-   arg = strrchr(program_invocation_name, '\\');
+   arg = strrchr(path, '\\');
    if (arg)
       return arg+1;
 
-   return program_invocation_name;
+   return path;
 }
 #    define GET_PROGRAM_NAME() __getProgramName()
 #elif defined(__CYGWIN__)
-- 
2.18.0



More information about the mesa-dev mailing list