From 8f543a192557b01efb1c9a3258c8195d845c34e0 Mon Sep 17 00:00:00 2001 From: Adeel Mujahid <3840695+am11@users.noreply.github.com> Date: Wed, 30 Nov 2022 11:40:17 +0200 Subject: [PATCH] Move AT_EXECFN to fallback for /proc/self/exe (#78958) * Move AT_EXECFN to fallback for /proc/self/exe * Update src/native/minipal/getexepath.h --- src/native/minipal/getexepath.h | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/native/minipal/getexepath.h b/src/native/minipal/getexepath.h index 0db1b73aa6f01..af0f404235013 100644 --- a/src/native/minipal/getexepath.h +++ b/src/native/minipal/getexepath.h @@ -75,13 +75,6 @@ static inline char* minipal_getexepath(void) // This is a packaging convention that our tooling should enforce. return strdup("/managed"); #else -#if HAVE_GETAUXVAL && defined(AT_EXECFN) - const char* path = (const char *)(getauxval(AT_EXECFN)); - if (path && !errno) - { - return realpath(path, NULL); - } -#endif // HAVE_GETAUXVAL && defined(AT_EXECFN) #ifdef __linux__ const char* symlinkEntrypointExecutable = "/proc/self/exe"; #else @@ -89,7 +82,23 @@ static inline char* minipal_getexepath(void) #endif // Resolve the symlink to the executable from /proc - return realpath(symlinkEntrypointExecutable, NULL); + char* path = realpath(symlinkEntrypointExecutable, NULL); + if (path) + { + return path; + } + +#if HAVE_GETAUXVAL && defined(AT_EXECFN) + // fallback to AT_EXECFN, which does not work properly in rare cases + // when .NET process is set as interpreter (shebang). + const char* exePath = (const char *)(getauxval(AT_EXECFN)); + if (exePath && !errno) + { + return realpath(exePath, NULL); + } +#endif // HAVE_GETAUXVAL && defined(AT_EXECFN) + + return NULL; #endif // defined(__APPLE__) }