[Mesa-dev] [PATCH v3 2/5] util: Get program name based on path when possible
Nicholas Kazlauskas
nicholas.kazlauskas at amd.com
Fri Oct 5 20:42:57 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. It only uses the realpath if it
was a prefix of the invocation to avoid breaking wine programs.
Signed-off-by: Nicholas Kazlauskas <nicholas.kazlauskas at amd.com>
---
src/util/u_process.c | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)
diff --git a/src/util/u_process.c b/src/util/u_process.c
index 5e5927678d..cd16521ab3 100644
--- a/src/util/u_process.c
+++ b/src/util/u_process.c
@@ -41,8 +41,24 @@ static const char *
__getProgramName()
{
char * arg = strrchr(program_invocation_name, '/');
- if (arg)
+ if (arg) {
+ /* If the / character was found this is likely a linux path or
+ * an invocation path for a 64-bit wine program.
+ *
+ * However, some programs pass command line arguments into argv[0].
+ * Strip these arguments out by using the realpath only if it was
+ * a prefix of the invocation name.
+ */
+ static char *path;
+
+ if (!path)
+ path = realpath("/proc/self/exe", NULL);
+
+ if (path && strncmp(path, program_invocation_name, strlen(path)) == 0)
+ return path + (arg - program_invocation_name + 1);
+
return arg+1;
+ }
/* If there was no '/' at all we likely have a windows like path from
* a wine application.
--
2.19.0
More information about the mesa-dev
mailing list