Skip to content

Commit

Permalink
Use getexepath in mono to deduplicate related code
Browse files Browse the repository at this point in the history
  • Loading branch information
am11 committed Oct 15, 2021
1 parent 1174290 commit 94682c4
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 114 deletions.
5 changes: 4 additions & 1 deletion src/mono/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ endif()

project(mono)

include(../../eng/native/configurepaths.cmake)

set(CMAKE_C_FLAGS_CHECKED "")
set(CMAKE_CXX_FLAGS_CHECKED "")
set(CMAKE_EXE_LINKER_FLAGS_CHECKED "")
Expand Down Expand Up @@ -586,7 +588,7 @@ endif()
######################################
# ICU CHECKS
######################################
set(ICU_SHIM_PATH "../../../libraries/Native/Unix/System.Globalization.Native")
set(ICU_SHIM_PATH "${CLR_SRC_LIBS_NATIVE_DIR}/Unix/System.Globalization.Native")
if(MONO_CROSS_COMPILE)
elseif(HOST_OSX AND NOT HOST_MACCAT)
include(FindPkgConfig)
Expand Down Expand Up @@ -809,6 +811,7 @@ if (TARGET_BROWSER)
endif()
### End of OS specific checks

include_directories("${CLR_SRC_NATIVE_DIR}/common")
add_subdirectory(mono)
if (ENABLE_MSCORDBI AND NOT TARGET_ARCH STREQUAL "arm64" AND NOT CMAKE_CROSSCOMPILING AND NOT TARGET_IOS AND NOT TARGET_ANDROID AND NOT TARGET_BROWSER AND NOT HOST_MACCAT)
add_subdirectory(dlls/mscordbi)
Expand Down
2 changes: 2 additions & 0 deletions src/mono/cmake/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,8 @@
/* Define to 1 if you have the `pthread_jit_write_protect_np' function. */
#cmakedefine HAVE_PTHREAD_JIT_WRITE_PROTECT_NP 1

#cmakedefine01 HAVE_GETAUXVAL

/* Define to 1 if you have the declaration of `pthread_mutexattr_setprotocol',
and to 0 if you don't. */
#cmakedefine HAVE_DECL_PTHREAD_MUTEXATTR_SETPROTOCOL 1
Expand Down
1 change: 1 addition & 0 deletions src/mono/cmake/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ check_symbol_exists(CLOCK_MONOTONIC "time.h" HAVE_CLOCK_MONOTONIC)
check_symbol_exists(CLOCK_MONOTONIC_COARSE "time.h" HAVE_CLOCK_MONOTONIC_COARSE)
check_symbol_exists(sys_signame "signal.h" HAVE_SYSSIGNAME)
check_symbol_exists(pthread_jit_write_protect_np "pthread.h" HAVE_PTHREAD_JIT_WRITE_PROTECT_NP)
check_symbol_exists(getauxval sys/auxv.h HAVE_GETAUXVAL)

ac_check_type("struct sockaddr_in6" sockaddr_in6 "netinet/in.h")
ac_check_type("struct timeval" timeval "sys/time.h;sys/types.h;utime.h")
Expand Down
6 changes: 1 addition & 5 deletions src/mono/dlls/dbgshim/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ if(HOST_WIN32)
endif()
endif()

include(${PROJECT_SOURCE_DIR}/../../../../eng/native/configuretools.cmake)
include(${PROJECT_SOURCE_DIR}/../../../../eng/native/configurepaths.cmake)
include(${PROJECT_SOURCE_DIR}/../../../../eng/native/configureplatform.cmake)
include(${PROJECT_SOURCE_DIR}/../../../../eng/native/configurecompiler.cmake)

include(${CLR_ENG_NATIVE_DIR}/configurecompiler.cmake)

add_definitions(-D_WIN32_WINNT=0x0602)

Expand Down
5 changes: 1 addition & 4 deletions src/mono/dlls/mscordbi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ endif()

add_subdirectory(${PROJECT_SOURCE_DIR}/socket-dbi)

include(${PROJECT_SOURCE_DIR}/../../../../eng/native/configuretools.cmake)
include(${PROJECT_SOURCE_DIR}/../../../../eng/native/configurepaths.cmake)
include(${PROJECT_SOURCE_DIR}/../../../../eng/native/configureplatform.cmake)
include(${PROJECT_SOURCE_DIR}/../../../../eng/native/configurecompiler.cmake)
include(${CLR_ENG_NATIVE_DIR}/configurecompiler.cmake)

if (CLR_CMAKE_HOST_UNIX)
# Add custom targets that the pal build expects.
Expand Down
76 changes: 13 additions & 63 deletions src/mono/mono/metadata/assembly.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <mono/utils/atomic.h>
#include <mono/utils/mono-os-mutex.h>
#include <mono/metadata/mono-private-unstable.h>
#include <getexepath.h>

#ifndef HOST_WIN32
#include <sys/types.h>
Expand Down Expand Up @@ -524,37 +525,18 @@ set_dirs (char *exe)
void
mono_set_rootdir (void)
{
#if defined(HOST_WIN32) || (defined(HOST_DARWIN) && !defined(TARGET_ARM))
gchar *bindir, *installdir, *root, *name, *resolvedname, *config;

#ifdef HOST_WIN32
name = mono_get_module_file_name ((HMODULE) &__ImageBase);
#else
{
/*
* _NSGetExecutablePath may return -1 to indicate buf is not large
* enough, but we ignore that case to avoid having to do extra dynamic
* allocation for the path and hope that 4096 is enough - this is
* ok in the Linux/Solaris case below at least...
*/

gchar buf[4096];
guint buf_size = sizeof (buf);

name = NULL;
if (_NSGetExecutablePath (buf, &buf_size) == 0)
name = g_strdup (buf);

if (name == NULL) {
fallback ();
return;
}
}
char *path = getexepath();
if (path == NULL) {
#ifndef HOST_WIN32
fallback ();
#endif
return;
}

resolvedname = mono_path_resolve_symlinks (name);
#if defined(HOST_WIN32) || (defined(HOST_DARWIN) && !defined(TARGET_ARM))
gchar *bindir, *installdir, *root, *config;

bindir = g_path_get_dirname (resolvedname);
bindir = g_path_get_dirname (path);
installdir = g_path_get_dirname (bindir);
root = g_build_path (G_DIR_SEPARATOR_S, installdir, "lib", (const char*)NULL);

Expand All @@ -572,44 +554,12 @@ mono_set_rootdir (void)
g_free (root);
g_free (installdir);
g_free (bindir);
g_free (name);
g_free (resolvedname);
g_free (path);
#elif defined(DISABLE_MONO_AUTODETECTION)
fallback ();
#else
char buf [4096];
int s;
char *str;

#if defined(HAVE_READLINK)
/* Linux style */
s = readlink ("/proc/self/exe", buf, sizeof (buf)-1);
#else
s = -1;
#endif

if (s != -1){
buf [s] = 0;
set_dirs (buf);
return;
}

/* Solaris 10 style */
str = g_strdup_printf ("/proc/%d/path/a.out", mono_process_current_pid ());

#if defined(HAVE_READLINK)
s = readlink (str, buf, sizeof (buf)-1);
#else
s = -1;
#endif

g_free (str);
if (s != -1){
buf [s] = 0;
set_dirs (buf);
return;
}
fallback ();
set_dirs (path);
return;
#endif
}

Expand Down
9 changes: 0 additions & 9 deletions src/mono/mono/utils/mono-dl-darwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,6 @@ mono_dl_get_so_suffixes (void)
return suffixes;
}

int
mono_dl_get_executable_path (char *buf, int buflen)
{
uint32_t bsize = buflen;
if (_NSGetExecutablePath (buf, &bsize) == 0)
return strlen (buf);
return -1;
}

const char*
mono_dl_get_system_dir (void)
{
Expand Down
10 changes: 0 additions & 10 deletions src/mono/mono/utils/mono-dl-posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,6 @@ mono_dl_get_so_suffixes (void)
return suffixes;
}

int
mono_dl_get_executable_path (char *buf, int buflen)
{
#if defined(HAVE_READLINK)
return readlink ("/proc/self/exe", buf, buflen - 1);
#else
return -1;
#endif
}

const char*
mono_dl_get_system_dir (void)
{
Expand Down
7 changes: 0 additions & 7 deletions src/mono/mono/utils/mono-dl-wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ mono_dl_get_so_suffixes (void)
return suffixes;
}

int
mono_dl_get_executable_path (char *buf, int buflen)
{
strncpy (buf, "/managed", buflen); //This is a packaging convertion that our tooling should enforce
return 0;
}

const char*
mono_dl_get_system_dir (void)
{
Expand Down
6 changes: 0 additions & 6 deletions src/mono/mono/utils/mono-dl-windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,6 @@ mono_dl_current_error_string (void)
}
#endif /* HAVE_API_SUPPORT_WIN32_FORMAT_MESSAGE */

int
mono_dl_get_executable_path (char *buf, int buflen)
{
return -1; //TODO
}

const char*
mono_dl_get_system_dir (void)
{
Expand Down
11 changes: 4 additions & 7 deletions src/mono/mono/utils/mono-dl.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <ctype.h>
#include <string.h>
#include <glib.h>
#include <getexepath.h>

#if defined(TARGET_ANDROID) && !defined(WIN32)
#include <dlfcn.h>
Expand Down Expand Up @@ -613,18 +614,14 @@ MonoDl*
mono_dl_open_runtime_lib (const char* lib_name, int flags, char **error_msg)
{
MonoDl *runtime_lib = NULL;
char buf [4096];
int binl;
*error_msg = NULL;

binl = mono_dl_get_executable_path (buf, sizeof (buf));
char *resolvedname = getexepath();

if (binl != -1) {
if (!resolvedname) {
char *base;
char *resolvedname, *name;
char *name;
char *baseparent = NULL;
buf [binl] = 0;
resolvedname = mono_path_resolve_symlinks (buf);
base = g_path_get_dirname (resolvedname);
name = g_strdup_printf ("%s/.libs", base);
runtime_lib = try_load (lib_name, name, flags, error_msg);
Expand Down
1 change: 0 additions & 1 deletion src/mono/mono/utils/mono-dl.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ void mono_dl_close_handle (MonoDl *module);
void* mono_dl_lookup_symbol (MonoDl *module, const char *name);
int mono_dl_convert_flags (int mono_flags, int native_flags);
char* mono_dl_current_error_string (void);
int mono_dl_get_executable_path (char *buf, int buflen);
const char* mono_dl_get_system_dir (void);

#endif /* __MONO_UTILS_DL_H__ */
Expand Down
15 changes: 14 additions & 1 deletion src/native/common/getexepath.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#elif defined(_WIN32)
#include <windows.h>
#endif

#ifdef __cplusplus
Expand All @@ -23,7 +25,7 @@ extern "C" {

// Returns the full path to the executable for the current process, resolving symbolic links.
// The caller is responsible for releasing the buffer. Returns null on error.
extern inline char* getexepath(void)
static inline char* getexepath(void)
{
#if defined(__APPLE__)
uint32_t path_length = 0;
Expand Down Expand Up @@ -59,6 +61,17 @@ extern inline char* getexepath(void)
}

return realpath(path, NULL);
#elif defined(_WIN32)
char path[MAX_PATH];
if (GetModuleFileNameA(NULL, path, MAX_PATH) == 0)
{
return NULL;
}

return strdup(path);
#elif defined(TARGET_WASM)
// 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);
Expand Down

0 comments on commit 94682c4

Please sign in to comment.