diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index d74642b263e6e..9fb3926e39a35 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -34,6 +34,8 @@ /src/mono/mono/metadata/thread* @lateralusX @lambdageek /src/mono/mono/metadata/w32* @lateralusX @lambdageek +/src/mono/mono/eventpipe @lateralusX @lambdageek + /src/mono/mono/mini @vargaz @lambdageek @SamMonoRT /src/mono/mono/mini/*cfgdump* @vargaz /src/mono/mono/mini/*exceptions* @vargaz @BrzVlad diff --git a/src/mono/cmake/config.h.in b/src/mono/cmake/config.h.in index 7d6ef733105e7..d700fe55fbd13 100644 --- a/src/mono/cmake/config.h.in +++ b/src/mono/cmake/config.h.in @@ -82,6 +82,9 @@ /* Host Platform is Darwin */ #cmakedefine HOST_DARWIN 1 +/* Host Platform is OSX or Mac Catalyst */ +#cmakedefine HOST_OSX 1 + /* Host Platform is iOS */ #cmakedefine HOST_IOS 1 @@ -154,9 +157,6 @@ /* Define to 1 if you have the header file. */ #cmakedefine HAVE_SYS_SYSCTL_H 1 -/* Define to 1 if you have the header file. */ -#cmakedefine HAVE_LIBPROC_H 1 - /* Define to 1 if you have the header file. */ #cmakedefine HAVE_SYS_PRCTL_H 1 @@ -399,9 +399,6 @@ /* Define to 1 if you have the `getpwuid_r' function. */ #cmakedefine HAVE_GETPWUID_R 1 -/* Define to 1 if you have the `readlink' function. */ -#cmakedefine HAVE_READLINK 1 - /* Define to 1 if you have the `chmod' function. */ #cmakedefine HAVE_CHMOD 1 @@ -819,7 +816,7 @@ /* The JIT/AOT targets Mac Catalyst */ #cmakedefine TARGET_MACCAT 1 -/* The JIT/AOT targets OSX */ +/* The JIT/AOT targets OSX or Mac Catalyst */ #cmakedefine TARGET_OSX 1 /* The JIT/AOT targets Apple platforms */ diff --git a/src/mono/cmake/configure.cmake b/src/mono/cmake/configure.cmake index e0bdc98fc8fdb..e251fc04ea415 100644 --- a/src/mono/cmake/configure.cmake +++ b/src/mono/cmake/configure.cmake @@ -68,7 +68,7 @@ ac_check_headers ( sys/types.h sys/stat.h sys/filio.h sys/sockio.h sys/utime.h sys/un.h sys/syscall.h sys/uio.h sys/param.h sys/prctl.h sys/socket.h sys/utsname.h sys/select.h sys/poll.h sys/wait.h sts/auxv.h sys/resource.h sys/ioctl.h sys/errno.h sys/sendfile.h sys/statvfs.h sys/statfs.h sys/mman.h sys/mount.h sys/time.h sys/random.h - strings.h stdint.h unistd.h signal.h setjmp.h syslog.h netdb.h utime.h semaphore.h libproc.h alloca.h ucontext.h pwd.h elf.h + strings.h stdint.h unistd.h signal.h setjmp.h syslog.h netdb.h utime.h semaphore.h alloca.h ucontext.h pwd.h elf.h gnu/lib-names.h netinet/tcp.h netinet/in.h link.h arpa/inet.h unwind.h poll.h wchar.h linux/magic.h android/legacy_signal_inlines.h execinfo.h pthread.h pthread_np.h net/if.h dirent.h CommonCrypto/CommonDigest.h dlfcn.h getopt.h pwd.h alloca.h @@ -77,7 +77,7 @@ ac_check_headers ( ac_check_funcs ( sigaction kill clock_nanosleep backtrace_symbols mkstemp mmap getrusage dladdr sysconf getrlimit prctl nl_langinfo - sched_getaffinity sched_setaffinity getpwuid_r readlink chmod lstat getdtablesize ftruncate msync + sched_getaffinity sched_setaffinity getpwuid_r chmod lstat getdtablesize ftruncate msync getpeername utime utimes openlog closelog atexit popen strerror_r inet_pton inet_aton shm_open poll getfsstat mremap posix_fadvise vsnprintf sendfile statfs statvfs setpgid system fork execv execve waitpid localtime_r mkdtemp getrandom execvp strlcpy stpcpy strtok_r rewinddir diff --git a/src/mono/mono/eventpipe/ep-rt-mono.c b/src/mono/mono/eventpipe/ep-rt-mono.c index cc294dbe53947..5b6e5acb36192 100644 --- a/src/mono/mono/eventpipe/ep-rt-mono.c +++ b/src/mono/mono/eventpipe/ep-rt-mono.c @@ -26,7 +26,8 @@ #include #include #include -#include "mono/utils/mono-logger-internals.h" +#include +#include #include #include @@ -1123,6 +1124,18 @@ ep_rt_mono_rand_try_get_bytes ( uint8_t *buffer, size_t buffer_size); +char * +ep_rt_mono_get_cmd_line (bool add_host); + +ep_rt_file_handle_t +ep_rt_mono_file_open_write(const ep_char8_t *path); + +bool +ep_rt_mono_file_close (ep_rt_file_handle_t handle); + +bool +ep_rt_mono_file_write (ep_rt_file_handle_t handle, const uint8_t *buffer, uint32_t numbytes, uint32_t *byteswritten); + EventPipeThread * ep_rt_mono_thread_get_or_create (void); @@ -2069,6 +2082,252 @@ ep_rt_mono_rand_try_get_bytes ( return mono_rand_try_get_bytes (&_ep_rt_mono_rand_provider, (guchar *)buffer, (gssize)buffer_size, error); } +static GString * +quote_escape_and_append_string (char *src_str, GString *target_str) +{ +#ifdef HOST_WIN32 + char quote_char = '\"'; + char escape_chars[] = "\"\\"; +#else + char quote_char = '\''; + char escape_chars[] = "\'\\"; +#endif + + gboolean need_quote = FALSE; + gboolean need_escape = FALSE; + + for (char *pos = src_str; *pos; ++pos) { + if (isspace (*pos)) + need_quote = TRUE; + if (strchr (escape_chars, *pos)) + need_escape = TRUE; + } + + if (need_quote) + target_str = g_string_append_c (target_str, quote_char); + + if (need_escape) { + for (char *pos = src_str; *pos; ++pos) { + if (strchr (escape_chars, *pos)) + target_str = g_string_append_c (target_str, '\\'); + target_str = g_string_append_c (target_str, *pos); + } + } else { + target_str = g_string_append (target_str, src_str); + } + + if (need_quote) + target_str = g_string_append_c (target_str, quote_char); + + return target_str; +} + +char * +ep_rt_mono_get_managed_cmd_line () +{ + MONO_REQ_GC_NEUTRAL_MODE; + + int argc = mono_runtime_get_main_args_argc_raw (); + if (argc == 0) + return NULL; + + // managed cmdline -> native host + managed argv (which includes the entrypoint assembly) + size_t total_size = 0; + char *host_path = NULL; + char **argv = mono_runtime_get_main_args_argv_raw (); + GString *cmd_line = NULL; + + host_path = minipal_getexepath(); + + if (host_path) + // quote + string + quote + total_size += strlen (host_path) + 2; + + for (int i = 0; i < argc; ++i) { + if (argv [i]) { + if (total_size > 0) { + // add space + total_size++; + } + // quote + string + quote + total_size += strlen (argv [i]) + 2; + } + } + + // String will grow if needed, so not over allocating + // to handle case of escaped characters in arguments, if + // that happens string will automatically grow. + cmd_line = g_string_sized_new (total_size + 1); + + if (cmd_line) { + if (host_path) + cmd_line = quote_escape_and_append_string (host_path, cmd_line); + + for (int i = 0; i < argc; ++i) { + if (argv [i]) { + if (cmd_line->len > 0) { + // add space + cmd_line = g_string_append_c (cmd_line, ' '); + } + cmd_line = quote_escape_and_append_string (argv [i], cmd_line); + } + } + } + + g_free (host_path); + + return cmd_line ? g_string_free (cmd_line, FALSE) : NULL; +} + +char * +ep_rt_mono_get_os_cmd_line () +{ + MONO_REQ_GC_NEUTRAL_MODE; + + // we only return the native host here since getting the full commandline is complicated and + // it's not super important to have the correct value since it'll only be used during startup + // until we have the managed commandline + return minipal_getexepath(); +} + +#ifdef HOST_WIN32 + +ep_rt_file_handle_t +ep_rt_mono_file_open_write(const ep_char8_t *path) +{ + if (!path) + return INVALID_HANDLE_VALUE; + + ep_char16_t *path_utf16 = ep_rt_utf8_to_utf16_string (path, -1); + + if (!path_utf16) + return INVALID_HANDLE_VALUE; + + ep_rt_file_handle_t res; + MONO_ENTER_GC_SAFE; + res = (ep_rt_file_handle_t)CreateFileW (path_utf16, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + MONO_EXIT_GC_SAFE; + ep_rt_utf16_string_free (path_utf16); + + return res; +} + +bool +ep_rt_mono_file_close (ep_rt_file_handle_t handle) +{ + bool res; + MONO_ENTER_GC_SAFE; + res = CloseHandle (handle); + MONO_EXIT_GC_SAFE; + return res; +} + +static void +win32_io_interrupt_handler (void* ignored) +{ +} + +bool +ep_rt_mono_file_write (ep_rt_file_handle_t handle, const uint8_t *buffer, uint32_t numbytes, uint32_t *byteswritten) +{ + bool res; + MonoThreadInfo *info = mono_thread_info_current (); + gboolean alerted = FALSE; + + if (info) { + mono_thread_info_install_interrupt (win32_io_interrupt_handler, NULL, &alerted); + if (alerted) { + return FALSE; + } + mono_win32_enter_blocking_io_call (info, handle); + } + + MONO_ENTER_GC_SAFE; + if (info && mono_thread_info_is_interrupt_state (info)) { + res = FALSE; + } else { + res = WriteFile (handle, buffer, numbytes, (PDWORD)byteswritten, NULL) ? TRUE : FALSE; + } + MONO_EXIT_GC_SAFE; + + if (info) { + mono_win32_leave_blocking_io_call (info, handle); + mono_thread_info_uninstall_interrupt (&alerted); + } + + return res; +} + +#else + +#include +#include + +ep_rt_file_handle_t +ep_rt_mono_file_open_write(const ep_char8_t *path) +{ + int fd; + mode_t perms = 0666; + + if (!path) + return INVALID_HANDLE_VALUE; + + MONO_ENTER_GC_SAFE; + fd = creat (path, perms); + MONO_EXIT_GC_SAFE; + + if (fd == -1) + return INVALID_HANDLE_VALUE; + + return (ep_rt_file_handle_t)(ptrdiff_t)fd; +} + +bool +ep_rt_mono_file_close (ep_rt_file_handle_t handle) +{ + int fd = (int)(ptrdiff_t)handle; + + MONO_ENTER_GC_SAFE; + close (fd); + MONO_EXIT_GC_SAFE; + + return TRUE; +} + +bool +ep_rt_mono_file_write (ep_rt_file_handle_t handle, const uint8_t *buffer, uint32_t numbytes, uint32_t *byteswritten) +{ + MONO_REQ_GC_UNSAFE_MODE; + + int fd = (int)(ptrdiff_t)handle; + uint32_t ret; + MonoThreadInfo *info = mono_thread_info_current (); + + if (byteswritten != NULL) + *byteswritten = 0; + + do { + MONO_ENTER_GC_SAFE; + ret = write (fd, buffer, numbytes); + MONO_EXIT_GC_SAFE; + } while (ret == -1 && errno == EINTR && + !mono_thread_info_is_interrupt_state (info)); + + if (ret == -1) { + if (errno == EINTR) + ret = 0; + else + return FALSE; + } + + if (byteswritten != NULL) + *byteswritten = ret; + + return TRUE; +} + +#endif // HOST_WIN32 + EventPipeThread * ep_rt_mono_thread_get_or_create (void) { @@ -2224,7 +2483,7 @@ static const int64_t SECS_TO_NS = 1000000000; static const int64_t MSECS_TO_MIS = 1000; /* clock_gettime () is found by configure on Apple builds, but its only present from ios 10, macos 10.12, tvos 10 and watchos 3 */ -#if defined (HAVE_CLOCK_MONOTONIC) && (defined(TARGET_IOS) || defined(TARGET_OSX) || defined(TARGET_WATCHOS) || defined(TARGET_TVOS)) +#if defined (HAVE_CLOCK_MONOTONIC) && (defined(HOST_IOS) || defined(HOST_OSX) || defined(HOST_WATCHOS) || defined(HOST_TVOS)) #undef HAVE_CLOCK_MONOTONIC #endif @@ -2375,7 +2634,7 @@ ep_rt_mono_system_timestamp_get (void) #ifndef HOST_WIN32 #if defined(__APPLE__) -#if defined (TARGET_OSX) +#if defined (HOST_OSX) G_BEGIN_DECLS gchar ***_NSGetEnviron(void); G_END_DECLS @@ -2383,7 +2642,7 @@ G_END_DECLS #else static char *_ep_rt_mono_environ[1] = { NULL }; #define environ _ep_rt_mono_environ -#endif /* defined (TARGET_OSX) */ +#endif /* defined (HOST_OSX) */ #else G_BEGIN_DECLS extern char **environ; diff --git a/src/mono/mono/eventpipe/ep-rt-mono.h b/src/mono/mono/eventpipe/ep-rt-mono.h index 47e055fff8338..9b276bb0d6d7c 100644 --- a/src/mono/mono/eventpipe/ep-rt-mono.h +++ b/src/mono/mono/eventpipe/ep-rt-mono.h @@ -21,9 +21,7 @@ #include #include #include -#include #include -#include #include #include #include @@ -354,6 +352,11 @@ extern mono_lazy_init_t _ep_rt_mono_os_cmd_line_init; extern char *_ep_rt_mono_managed_cmd_line; extern mono_lazy_init_t _ep_rt_mono_managed_cmd_line_init; extern ep_rt_spin_lock_handle_t _ep_rt_mono_config_lock; +extern char * ep_rt_mono_get_managed_cmd_line (void); +extern char * ep_rt_mono_get_os_cmd_line (void); +extern ep_rt_file_handle_t ep_rt_mono_file_open_write(const ep_char8_t *path); +extern bool ep_rt_mono_file_close (ep_rt_file_handle_t handle); +extern bool ep_rt_mono_file_write (ep_rt_file_handle_t handle, const uint8_t *buffer, uint32_t numbytes, uint32_t *byteswritten); extern void * ep_rt_mono_thread_attach (bool background_thread); extern void * ep_rt_mono_thread_attach_2 (bool background_thread, EventPipeThreadType thread_type); extern void ep_rt_mono_thread_detach (void); @@ -382,7 +385,7 @@ inline char * os_command_line_get (void) { - return mono_get_os_cmd_line (); + return ep_rt_mono_get_os_cmd_line (); } static @@ -424,7 +427,7 @@ inline char * managed_command_line_get (void) { - return mono_runtime_get_managed_cmd_line (); + return ep_rt_mono_get_managed_cmd_line (); } static @@ -511,33 +514,6 @@ ep_rt_mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId i return mono_native_thread_id_equals (id1, id2); } -static -inline -gpointer -ep_rt_mono_w32file_create (const gunichar2 *name, guint32 fileaccess, guint32 sharemode, guint32 createmode, guint32 attrs) -{ - //TODO, replace with low level PAL implementation. - return mono_w32file_create (name, fileaccess, sharemode, createmode, attrs); -} - -static -inline -gboolean -ep_rt_mono_w32file_write (gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten, gint32 *win32error) -{ - //TODO, replace with low level PAL implementation. - return mono_w32file_write (handle, buffer, numbytes, byteswritten, win32error); -} - -static -inline -gboolean -ep_rt_mono_w32file_close (gpointer handle) -{ - //TODO, replace with low level PAL implementation. - return mono_w32file_close (handle); -} - static inline void @@ -1407,13 +1383,12 @@ inline ep_rt_file_handle_t ep_rt_file_open_write (const ep_char8_t *path) { - ep_char16_t *path_utf16 = ep_rt_utf8_to_utf16_string (path, -1); - ep_return_null_if_nok (path_utf16 != NULL); + ep_rt_file_handle_t res = ep_rt_mono_file_open_write (path); - gpointer file_handle = ep_rt_mono_w32file_create ((gunichar2 *)path_utf16, GENERIC_WRITE, FILE_SHARE_READ, CREATE_ALWAYS, FileAttributes_Normal); - ep_rt_utf16_string_free (path_utf16); - - return file_handle; + if (res == INVALID_HANDLE_VALUE) + return NULL; + else + return res; } static @@ -1422,7 +1397,7 @@ bool ep_rt_file_close (ep_rt_file_handle_t file_handle) { ep_return_false_if_nok (file_handle != NULL); - return ep_rt_mono_w32file_close (file_handle); + return ep_rt_mono_file_close (file_handle); } static @@ -1437,8 +1412,7 @@ ep_rt_file_write ( ep_return_false_if_nok (file_handle != NULL); EP_ASSERT (buffer != NULL); - gint32 win32_error; - bool result = ep_rt_mono_w32file_write (file_handle, buffer, bytes_to_write, bytes_written, &win32_error); + bool result = ep_rt_mono_file_write (file_handle, buffer, bytes_to_write, bytes_written); if (result) *bytes_written = bytes_to_write; @@ -1847,6 +1821,7 @@ ep_rt_diagnostics_command_line_get (void) { const ep_char8_t * cmd_line = ep_rt_managed_command_line_get (); + // if the managed command line isn't available yet (e.g. because we're during runtime startup) then fallback to the OS command line. // Checkout https://github.com/dotnet/coreclr/pull/24433 for more information about this fall back. if (cmd_line == NULL) cmd_line = ep_rt_os_command_line_get (); diff --git a/src/mono/mono/eventpipe/test/CMakeLists.txt b/src/mono/mono/eventpipe/test/CMakeLists.txt index 401da523507b7..72e4bafe9e4cb 100644 --- a/src/mono/mono/eventpipe/test/CMakeLists.txt +++ b/src/mono/mono/eventpipe/test/CMakeLists.txt @@ -33,7 +33,7 @@ if(ENABLE_PERFTRACING) set(CMAKE_SKIP_RPATH 1) add_executable(ep-test ${EVENTPIPE_TEST_SOURCES} ${EVENTPIPE_TEST_HEADERS}) target_sources(ep-test PRIVATE "${mono-components-objects}") - target_link_libraries(ep-test monosgen-static ${OS_LIBS} ${LLVM_LIBS} ${ICU_LIBS}) + target_link_libraries(ep-test monosgen-static ${OS_LIBS} ${LLVM_LIBS} ${ICU_LIBS} ${Z_LIBS} monoapi) if(ICU_LDFLAGS) set_target_properties(ep-test PROPERTIES LINK_FLAGS ${ICU_LDFLAGS}) endif() diff --git a/src/mono/mono/eventpipe/test/ep-buffer-tests.c b/src/mono/mono/eventpipe/test/ep-buffer-tests.c index add8bfe358b6e..0a256c0dcbf72 100644 --- a/src/mono/mono/eventpipe/test/ep-buffer-tests.c +++ b/src/mono/mono/eventpipe/test/ep-buffer-tests.c @@ -135,10 +135,10 @@ load_buffer ( for (; i < event_count; ++i) { EventPipeEventPayload payload; gchar *event_data = NULL; - size_t event_data_len = 0; + uint32_t event_data_len = 0; if (!perf_test) { event_data = g_strdup_printf (event_instance_data, i); - event_data_len = strlen (event_data) + 1; + event_data_len = (uint32_t)strlen (event_data) + 1; }else { event_data = (gchar *)TEST_EVENT_DATA; event_data_len = ARRAY_SIZE (TEST_EVENT_DATA); diff --git a/src/mono/mono/metadata/CMakeLists.txt b/src/mono/mono/metadata/CMakeLists.txt index 75221053d7bb9..2aee8f6588308 100644 --- a/src/mono/mono/metadata/CMakeLists.txt +++ b/src/mono/mono/metadata/CMakeLists.txt @@ -32,17 +32,11 @@ else() endif() set(metadata_win32_sources - w32file-win32.c w32event-win32.c w32error-win32.c) set(metadata_unix_sources w32event-unix.c - w32process-unix-osx.c - w32process-unix-bsd.c - w32process-unix-haiku.c - w32process-unix-default.c - w32file-unix.c w32error-unix.c) if(HOST_WIN32) @@ -83,7 +77,6 @@ set(metadata_common_sources environment.c exception.c exception-internals.h - w32file.h gc-internals.h gc_wrapper.h icall.c @@ -133,7 +126,6 @@ set(metadata_common_sources opcodes.c property-bag.h property-bag.c - w32process.h profiler.c profiler-private.h runtime.c @@ -169,8 +161,6 @@ set(metadata_common_sources sre.c sre-encode.c custom-attrs.c - fdhandle.h - fdhandle.c callspec.h callspec.c assembly-load-context.c diff --git a/src/mono/mono/metadata/appdomain.c b/src/mono/mono/metadata/appdomain.c index 24144be72b739..f0818f455e65a 100644 --- a/src/mono/mono/metadata/appdomain.c +++ b/src/mono/mono/metadata/appdomain.c @@ -50,7 +50,6 @@ #include #include #include -#include #include #include #include @@ -65,8 +64,6 @@ #include #include #include -#include -#include #include #include diff --git a/src/mono/mono/metadata/domain.c b/src/mono/mono/metadata/domain.c index fc9534a9e40eb..7d24c1fb377f2 100644 --- a/src/mono/mono/metadata/domain.c +++ b/src/mono/mono/metadata/domain.c @@ -43,7 +43,6 @@ #include #include #include -#include #include #include #include @@ -171,7 +170,6 @@ mono_init_internal (const char *filename, const char *exe_filename, const char * #endif mono_w32event_init (); - mono_w32file_init (); #ifndef DISABLE_PERFCOUNTERS mono_perfcounters_init (); diff --git a/src/mono/mono/metadata/environment-internals.h b/src/mono/mono/metadata/environment-internals.h deleted file mode 100644 index 510f4222c90ed..0000000000000 --- a/src/mono/mono/metadata/environment-internals.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * \file - * - * Copyright 2020 Microsoft - * Licensed under the MIT license. See LICENSE file in the project root for full license information. - */ -#ifndef _MONO_METADATA_ENVIRONMENT_INTERNALS_H_ -#define _MONO_METADATA_ENVIRONMENT_INTERNALS_H_ - -#include - -void -mono_set_os_args (int argc, char **argv); - -MONO_COMPONENT_API char * -mono_get_os_cmd_line (void); - -#endif /* _MONO_METADATA_ENVIRONMENT_INTERNALS_H_ */ diff --git a/src/mono/mono/metadata/environment.c b/src/mono/mono/metadata/environment.c index 42567ac120e33..2cef1cfc88e44 100644 --- a/src/mono/mono/metadata/environment.c +++ b/src/mono/mono/metadata/environment.c @@ -16,7 +16,6 @@ #include #include -#include #include #include #include @@ -45,19 +44,3 @@ mono_environment_exitcode_set (gint32 value) { exitcode=value; } - -static int mini_argc = 0; -static char **mini_argv = NULL; - -void -mono_set_os_args (int argc, char **argv) -{ - mini_argc = argc; - mini_argv = argv; -} - -char * -mono_get_os_cmd_line (void) -{ - return mono_runtime_get_cmd_line (mini_argc, mini_argv); -} diff --git a/src/mono/mono/metadata/fdhandle.c b/src/mono/mono/metadata/fdhandle.c deleted file mode 100644 index fd41505b8b666..0000000000000 --- a/src/mono/mono/metadata/fdhandle.c +++ /dev/null @@ -1,144 +0,0 @@ - -#include "fdhandle.h" -#include "utils/mono-lazy-init.h" -#include "utils/mono-coop-mutex.h" - -static GHashTable *fds; -static MonoCoopMutex fds_mutex; -static MonoFDHandleCallback fds_callback[MONO_FDTYPE_COUNT]; -static mono_lazy_init_t fds_init = MONO_LAZY_INIT_STATUS_NOT_INITIALIZED; - -#ifndef DISABLE_ASSERT_MESSAGES -static const gchar *types_str[] = { - "File", - "Console", - "Pipe", - "Socket", - NULL -}; -#endif - -static void -fds_remove (gpointer data) -{ - MonoFDHandle* fdhandle; - - fdhandle = (MonoFDHandle*) data; - g_assert (fdhandle); - - g_assert (fds_callback [fdhandle->type].close); - fds_callback [fdhandle->type].close (fdhandle); - - mono_refcount_dec (fdhandle); -} - -static void -initialize (void) -{ - fds = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, fds_remove); - mono_coop_mutex_init (&fds_mutex); -} - -void -mono_fdhandle_register (MonoFDType type, MonoFDHandleCallback *callback) -{ - mono_lazy_initialize (&fds_init, initialize); - memcpy (&fds_callback [type], callback, sizeof (MonoFDHandleCallback)); -} - -static void -fdhandle_destroy (gpointer data) -{ - MonoFDHandle* fdhandle; - - fdhandle = (MonoFDHandle*) data; - g_assert (fdhandle); - - g_assert (fds_callback [fdhandle->type].destroy); - fds_callback [fdhandle->type].destroy (fdhandle); -} - -void -mono_fdhandle_init (MonoFDHandle *fdhandle, MonoFDType type, gint fd) -{ - mono_refcount_init (fdhandle, fdhandle_destroy); - fdhandle->type = type; - fdhandle->fd = fd; -} - -void -mono_fdhandle_insert (MonoFDHandle *fdhandle) -{ - mono_coop_mutex_lock (&fds_mutex); - - if (g_hash_table_lookup_extended (fds, GINT_TO_POINTER(fdhandle->fd), NULL, NULL)) - g_error("%s: duplicate %s fd %d", __func__, types_str [fdhandle->type], fdhandle->fd); - - g_hash_table_insert (fds, GINT_TO_POINTER(fdhandle->fd), fdhandle); - - mono_coop_mutex_unlock (&fds_mutex); -} - -gboolean -mono_fdhandle_try_insert (MonoFDHandle *fdhandle) -{ - mono_coop_mutex_lock (&fds_mutex); - - if (g_hash_table_lookup_extended (fds, GINT_TO_POINTER(fdhandle->fd), NULL, NULL)) { - /* we raced between 2 invocations of mono_fdhandle_try_insert */ - mono_coop_mutex_unlock (&fds_mutex); - - return FALSE; - } - - g_hash_table_insert (fds, GINT_TO_POINTER(fdhandle->fd), fdhandle); - - mono_coop_mutex_unlock (&fds_mutex); - - return TRUE; -} - -gboolean -mono_fdhandle_lookup_and_ref (gint fd, MonoFDHandle **fdhandle) -{ - mono_coop_mutex_lock (&fds_mutex); - - if (!g_hash_table_lookup_extended (fds, GINT_TO_POINTER(fd), NULL, (gpointer*) fdhandle)) { - mono_coop_mutex_unlock (&fds_mutex); - return FALSE; - } - - mono_refcount_inc (*fdhandle); - - mono_coop_mutex_unlock (&fds_mutex); - - return TRUE; -} - -void -mono_fdhandle_unref (MonoFDHandle *fdhandle) -{ - mono_refcount_dec (fdhandle); -} - -gboolean -mono_fdhandle_close (gint fd) -{ - MonoFDHandle *fdhandle; - gboolean removed; - - mono_coop_mutex_lock (&fds_mutex); - - if (!g_hash_table_lookup_extended (fds, GINT_TO_POINTER(fd), NULL, (gpointer*) &fdhandle)) { - mono_coop_mutex_unlock (&fds_mutex); - - return FALSE; - } - - removed = g_hash_table_remove (fds, GINT_TO_POINTER(fdhandle->fd)); - g_assert (removed); - - mono_coop_mutex_unlock (&fds_mutex); - - return TRUE; -} diff --git a/src/mono/mono/metadata/fdhandle.h b/src/mono/mono/metadata/fdhandle.h deleted file mode 100644 index e3d7e5b9b98d7..0000000000000 --- a/src/mono/mono/metadata/fdhandle.h +++ /dev/null @@ -1,50 +0,0 @@ - -#ifndef __MONO_METADATA_FDHANDLE_H__ -#define __MONO_METADATA_FDHANDLE_H__ - -#include -#include - -#include "utils/refcount.h" - -typedef enum { - MONO_FDTYPE_FILE, - MONO_FDTYPE_CONSOLE, - MONO_FDTYPE_PIPE, - MONO_FDTYPE_SOCKET, - MONO_FDTYPE_COUNT -} MonoFDType; - -typedef struct { - MonoRefCount ref; - MonoFDType type; - gint fd; -} MonoFDHandle; - -typedef struct { - void (*close) (MonoFDHandle *fdhandle); - void (*destroy) (MonoFDHandle *fdhandle); -} MonoFDHandleCallback; - -void -mono_fdhandle_register (MonoFDType type, MonoFDHandleCallback *callback); - -void -mono_fdhandle_init (MonoFDHandle *fdhandle, MonoFDType type, gint fd); - -void -mono_fdhandle_insert (MonoFDHandle *fdhandle); - -gboolean -mono_fdhandle_try_insert (MonoFDHandle *fdhandle); - -gboolean -mono_fdhandle_lookup_and_ref (gint fd, MonoFDHandle **fdhandle); - -void -mono_fdhandle_unref (MonoFDHandle *fdhandle); - -gboolean -mono_fdhandle_close (gint fd); - -#endif /* __MONO_METADATA_FDHANDLE_H__ */ diff --git a/src/mono/mono/metadata/icall-decl.h b/src/mono/mono/metadata/icall-decl.h index 1fcf0a699342c..0c7334de6d445 100644 --- a/src/mono/mono/metadata/icall-decl.h +++ b/src/mono/mono/metadata/icall-decl.h @@ -22,7 +22,6 @@ #include "mono/utils/mono-digest.h" #include "mono/utils/mono-forward-internal.h" #include "w32event.h" -#include "w32file.h" #include "mono/utils/mono-proclib.h" /* From MonoProperty.cs */ diff --git a/src/mono/mono/metadata/icall.c b/src/mono/mono/metadata/icall.c index bdb83dad31a54..a1f7c143e0780 100644 --- a/src/mono/mono/metadata/icall.c +++ b/src/mono/mono/metadata/icall.c @@ -50,7 +50,6 @@ #include #include #include -#include #include #include #include @@ -76,7 +75,6 @@ #include #include #include -#include #include #include #include @@ -89,7 +87,6 @@ #include #include #include -#include #include #include #include diff --git a/src/mono/mono/metadata/object-internals.h b/src/mono/mono/metadata/object-internals.h index a188ceb0e105c..ae5b76aa7d9fe 100644 --- a/src/mono/mono/metadata/object-internals.h +++ b/src/mono/mono/metadata/object-internals.h @@ -2119,11 +2119,11 @@ mono_gc_wbarrier_value_copy_internal (void* dest, const void* src, int count, Mo void mono_gc_wbarrier_object_copy_internal (MonoObject* obj, MonoObject *src); -MONO_COMPONENT_API char * -mono_runtime_get_managed_cmd_line (void); +MONO_COMPONENT_API int +mono_runtime_get_main_args_argc_raw (void); -char * -mono_runtime_get_cmd_line (int argc, char **argv); +MONO_COMPONENT_API char** +mono_runtime_get_main_args_argv_raw (void); #ifdef HOST_WASM int diff --git a/src/mono/mono/metadata/object.c b/src/mono/mono/metadata/object.c index 0d1bca654824a..e3cba6b8e6cce 100644 --- a/src/mono/mono/metadata/object.c +++ b/src/mono/mono/metadata/object.c @@ -39,8 +39,6 @@ #include #include "mono/metadata/profiler-private.h" #include -#include -#include #include #include #include @@ -104,12 +102,6 @@ static MonoCoopMutex ldstr_section; static MonoMethod *create_proxy_for_type_method; static MonoGHashTable *ldstr_table; -static GString * -quote_escape_and_append_string (char *src_str, GString *target_str); - -static GString * -format_cmd_line (int argc, char **argv, gboolean add_host); - /** * mono_runtime_object_init: * \param this_obj the object to initialize @@ -3894,6 +3886,26 @@ mono_runtime_get_main_args_handle (MonoError *error) HANDLE_FUNCTION_RETURN_REF (MonoArray, array); } +/** + * mono_runtime_get_main_args_argc_raw: + * \returns number of arguments from the command line + */ +int +mono_runtime_get_main_args_argc_raw () +{ + return num_main_args; +} + +/** + * mono_runtime_get_main_args_argc_raw: + * \returns array of strings from the command line + */ +char** +mono_runtime_get_main_args_argv_raw () +{ + return main_args; +} + static void free_main_args (void) { @@ -7796,122 +7808,6 @@ mono_vtype_get_field_addr (gpointer vtype, MonoClassField *field) return ((char*)vtype) + m_field_get_offset (field) - MONO_ABI_SIZEOF (MonoObject); } -static GString * -quote_escape_and_append_string (char *src_str, GString *target_str) -{ -#ifdef HOST_WIN32 - char quote_char = '\"'; - char escape_chars[] = "\"\\"; -#else - char quote_char = '\''; - char escape_chars[] = "\'\\"; -#endif - - gboolean need_quote = FALSE; - gboolean need_escape = FALSE; - - for (char *pos = src_str; *pos; ++pos) { - if (isspace (*pos)) - need_quote = TRUE; - if (strchr (escape_chars, *pos)) - need_escape = TRUE; - } - - if (need_quote) - target_str = g_string_append_c (target_str, quote_char); - - if (need_escape) { - for (char *pos = src_str; *pos; ++pos) { - if (strchr (escape_chars, *pos)) - target_str = g_string_append_c (target_str, '\\'); - target_str = g_string_append_c (target_str, *pos); - } - } else { - target_str = g_string_append (target_str, src_str); - } - - if (need_quote) - target_str = g_string_append_c (target_str, quote_char); - - return target_str; -} - -static GString * -format_cmd_line (int argc, char **argv, gboolean add_host) -{ - size_t total_size = 0; - char *host_path = NULL; - GString *cmd_line = NULL; - - if (add_host) { -#if !defined(HOST_WIN32) && defined(HAVE_GETPID) - host_path = mono_w32process_get_path (getpid ()); -#elif defined(HOST_WIN32) - gunichar2 *host_path_ucs2 = NULL; - guint32 host_path_ucs2_len = 0; - if (mono_get_module_filename (NULL, &host_path_ucs2, &host_path_ucs2_len)) { - host_path = g_utf16_to_utf8 (host_path_ucs2, -1, NULL, NULL, NULL); - g_free (host_path_ucs2); - } -#endif - } - - if (host_path) - // quote + string + quote - total_size += strlen (host_path) + 2; - - for (int i = 0; i < argc; ++i) { - if (argv [i]) { - if (total_size > 0) { - // add space - total_size++; - } - // quote + string + quote - total_size += strlen (argv [i]) + 2; - } - } - - // String will grow if needed, so not over allocating - // to handle case of escaped characters in arguments, if - // that happens string will automatically grow. - cmd_line = g_string_sized_new (total_size + 1); - - if (cmd_line) { - if (host_path) - cmd_line = quote_escape_and_append_string (host_path, cmd_line); - - for (int i = 0; i < argc; ++i) { - if (argv [i]) { - if (cmd_line->len > 0) { - // add space - cmd_line = g_string_append_c (cmd_line, ' '); - } - cmd_line = quote_escape_and_append_string (argv [i], cmd_line); - } - } - } - - g_free (host_path); - - return cmd_line; -} - -char * -mono_runtime_get_cmd_line (int argc, char **argv) -{ - MONO_REQ_GC_NEUTRAL_MODE; - GString *cmd_line = format_cmd_line (num_main_args, main_args, FALSE); - return cmd_line ? g_string_free (cmd_line, FALSE) : NULL; -} - -char * -mono_runtime_get_managed_cmd_line (void) -{ - MONO_REQ_GC_NEUTRAL_MODE; - GString *cmd_line = format_cmd_line (num_main_args, main_args, TRUE); - return cmd_line ? g_string_free (cmd_line, FALSE) : NULL; -} - #if NEVER_DEFINED /* * The following section is purely to declare prototypes and diff --git a/src/mono/mono/metadata/sgen-mono-ilgen.c b/src/mono/mono/metadata/sgen-mono-ilgen.c index e45be6ccd7282..535c8055ef627 100644 --- a/src/mono/mono/metadata/sgen-mono-ilgen.c +++ b/src/mono/mono/metadata/sgen-mono-ilgen.c @@ -32,7 +32,6 @@ #include "utils/mono-logger-internals.h" #include "utils/mono-threads-coop.h" #include "utils/mono-threads.h" -#include "metadata/w32handle.h" #include "icall-decl.h" #define OPDEF(a,b,c,d,e,f,g,h,i,j) \ diff --git a/src/mono/mono/metadata/threads.c b/src/mono/mono/metadata/threads.c index cb695dc7d42e4..80e493c23ac26 100644 --- a/src/mono/mono/metadata/threads.c +++ b/src/mono/mono/metadata/threads.c @@ -2057,31 +2057,6 @@ ves_icall_System_Threading_Thread_Join_internal (MonoThreadObjectHandle thread_h return FALSE; } -#define MANAGED_WAIT_FAILED 0x7fffffff - -static gint32 -map_native_wait_result_to_managed (MonoW32HandleWaitRet val, gsize numobjects) -{ - if (val >= MONO_W32HANDLE_WAIT_RET_SUCCESS_0 && val < MONO_W32HANDLE_WAIT_RET_SUCCESS_0 + numobjects) { - return WAIT_OBJECT_0 + (val - MONO_W32HANDLE_WAIT_RET_SUCCESS_0); - } else if (val >= MONO_W32HANDLE_WAIT_RET_ABANDONED_0 && val < MONO_W32HANDLE_WAIT_RET_ABANDONED_0 + numobjects) { - return WAIT_ABANDONED_0 + (val - MONO_W32HANDLE_WAIT_RET_ABANDONED_0); - } else if (val == MONO_W32HANDLE_WAIT_RET_ALERTED) { - return WAIT_IO_COMPLETION; - } else if (val == MONO_W32HANDLE_WAIT_RET_TIMEOUT) { - return WAIT_TIMEOUT; - } else if (val == MONO_W32HANDLE_WAIT_RET_TOO_MANY_POSTS) { - return WAIT_TOO_MANY_POSTS; - } else if (val == MONO_W32HANDLE_WAIT_RET_NOT_OWNED_BY_CALLER) { - return WAIT_NOT_OWNED_BY_CALLER; - } else if (val == MONO_W32HANDLE_WAIT_RET_FAILED) { - /* WAIT_FAILED in waithandle.cs is different from WAIT_FAILED in Win32 API */ - return MANAGED_WAIT_FAILED; - } else { - g_error ("%s: unknown val value %d", __func__, val); - } -} - gint32 ves_icall_System_Threading_Interlocked_Increment_Int (gint32 *location) { return mono_atomic_inc_i32 (location); diff --git a/src/mono/mono/metadata/w32file-unix.c b/src/mono/mono/metadata/w32file-unix.c deleted file mode 100644 index ee1e2fe89d557..0000000000000 --- a/src/mono/mono/metadata/w32file-unix.c +++ /dev/null @@ -1,748 +0,0 @@ -/** - * \file - */ - -#include -#include - -#include -#include -#include -#include -#include -#include -#ifdef HAVE_SYS_STATVFS_H -#include -#endif -#if defined(HAVE_SYS_STATFS_H) -#include -#endif -#if defined(HAVE_SYS_PARAM_H) && defined(HAVE_SYS_MOUNT_H) -#include -#include -#endif -#include -#include -#ifdef HAVE_UTIME_H -#include -#endif -#ifdef __linux__ -#include -#include -#include -#endif -#ifdef _AIX -#include -#include -#endif -#include -#ifdef HAVE_DIRENT_H -# include -#endif -#if HOST_DARWIN -#include -#endif - -#include "w32file.h" -#include "w32error.h" -#include "fdhandle.h" -#include "utils/mono-error-internals.h" -#include "utils/mono-logger-internals.h" -#include "utils/mono-os-mutex.h" -#include "utils/mono-threads.h" -#include "utils/mono-threads-api.h" -#include "utils/strenc-internals.h" -#include "utils/strenc.h" -#include "utils/refcount.h" -#include "icall-decl.h" -#include "utils/mono-errno.h" - -#define INVALID_HANDLE_VALUE ((gpointer)-1) - -typedef struct { - guint64 device; - guint64 inode; - guint32 sharemode; - guint32 access; - guint32 handle_refs; - guint32 timestamp; -} FileShare; - -/* Currently used for both FILE, CONSOLE and PIPE handle types. - * This may have to change in future. */ -typedef struct { - MonoFDHandle fdhandle; - gchar *filename; - FileShare *share_info; /* Pointer into shared mem */ - guint32 security_attributes; - guint32 fileaccess; - guint32 sharemode; - guint32 attrs; -} FileHandle; - -/* - * If SHM is disabled, this will point to a hash of FileShare structures, otherwise - * it will be NULL. We use this instead of _wapi_fileshare_layout to avoid allocating a - * 4MB array. - */ -static GHashTable *file_share_table; -static MonoCoopMutex file_share_mutex; - -static FileHandle* -file_data_create (MonoFDType type, gint fd) -{ - FileHandle *filehandle; - - filehandle = g_new0 (FileHandle, 1); - mono_fdhandle_init ((MonoFDHandle*) filehandle, type, fd); - - return filehandle; -} - -static gint -_wapi_unlink (const gchar *pathname); - -static void -file_share_release (FileShare *share_info); - -static void -file_data_close (MonoFDHandle *fdhandle) -{ - FileHandle* filehandle; - - filehandle = (FileHandle*) fdhandle; - g_assert (filehandle); - - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_FILE, "%s: closing fd %d", __func__, ((MonoFDHandle*) filehandle)->fd); - - if (((MonoFDHandle*) filehandle)->type == MONO_FDTYPE_FILE && (filehandle->attrs & FILE_FLAG_DELETE_ON_CLOSE)) { - _wapi_unlink (filehandle->filename); - } - - if (((MonoFDHandle*) filehandle)->type != MONO_FDTYPE_CONSOLE || ((MonoFDHandle*) filehandle)->fd > 2) { - if (filehandle->share_info) { - file_share_release (filehandle->share_info); - filehandle->share_info = NULL; - } - - MONO_ENTER_GC_SAFE; - close (((MonoFDHandle*) filehandle)->fd); - MONO_EXIT_GC_SAFE; - } -} - -static void -file_data_destroy (MonoFDHandle *fdhandle) -{ - FileHandle *filehandle; - - filehandle = (FileHandle*) fdhandle; - g_assert (filehandle); - - if (filehandle->filename) - g_free (filehandle->filename); - - g_free (filehandle); -} - -static void -file_share_release (FileShare *share_info) -{ - /* Prevent new entries racing with us */ - mono_coop_mutex_lock (&file_share_mutex); - - g_assert (share_info->handle_refs > 0); - share_info->handle_refs -= 1; - - if (share_info->handle_refs == 0) { - g_hash_table_remove (file_share_table, share_info); - // g_free (share_info); - } - - mono_coop_mutex_unlock (&file_share_mutex); -} - -static gint -file_share_equal (gconstpointer ka, gconstpointer kb) -{ - const FileShare *s1 = (const FileShare *)ka; - const FileShare *s2 = (const FileShare *)kb; - - return (s1->device == s2->device && s1->inode == s2->inode) ? 1 : 0; -} - -static guint -file_share_hash (gconstpointer data) -{ - const FileShare *s = (const FileShare *)data; - - return s->inode; -} - -static gboolean -file_share_get (guint64 device, guint64 inode, guint32 new_sharemode, guint32 new_access, - guint32 *old_sharemode, guint32 *old_access, FileShare **share_info) -{ - FileShare *file_share; - gboolean exists = FALSE; - - /* Prevent new entries racing with us */ - mono_coop_mutex_lock (&file_share_mutex); - - FileShare tmp; - - /* - * Instead of allocating a 4MB array, we use a hash table to keep track of this - * info. This is needed even if SHM is disabled, to track sharing inside - * the current process. - */ - if (!file_share_table) - file_share_table = g_hash_table_new_full (file_share_hash, file_share_equal, NULL, g_free); - - tmp.device = device; - tmp.inode = inode; - - file_share = (FileShare *)g_hash_table_lookup (file_share_table, &tmp); - if (file_share) { - *old_sharemode = file_share->sharemode; - *old_access = file_share->access; - *share_info = file_share; - - g_assert (file_share->handle_refs > 0); - file_share->handle_refs += 1; - - exists = TRUE; - } else { - file_share = g_new0 (FileShare, 1); - - file_share->device = device; - file_share->inode = inode; - file_share->sharemode = new_sharemode; - file_share->access = new_access; - file_share->handle_refs = 1; - *share_info = file_share; - - g_hash_table_insert (file_share_table, file_share, file_share); - } - - mono_coop_mutex_unlock (&file_share_mutex); - - return(exists); -} - -static gint -_wapi_open (const gchar *pathname, gint flags, mode_t mode) -{ - gint fd; - - MONO_ENTER_GC_SAFE; - fd = open (pathname, flags, mode); - MONO_EXIT_GC_SAFE; - - return(fd); -} - -static gint -_wapi_access (const gchar *pathname, gint mode) -{ - gint ret; - - MONO_ENTER_GC_SAFE; - ret = access (pathname, mode); - MONO_EXIT_GC_SAFE; - - return ret; -} - -static gint -_wapi_unlink (const gchar *pathname) -{ - gint ret; - - MONO_ENTER_GC_SAFE; - ret = unlink (pathname); - MONO_EXIT_GC_SAFE; - - return ret; -} - -static gchar* -_wapi_dirname (const gchar *filename) -{ - gchar *new_filename = g_strdup (filename), *ret; - - ret = g_path_get_dirname (new_filename); - g_free (new_filename); - - return ret; -} - -static void -_wapi_set_last_error_from_errno (void) -{ - mono_w32error_set_last (mono_w32error_unix_to_win32 (errno)); -} - -static void _wapi_set_last_path_error_from_errno (const gchar *dir, - const gchar *path) -{ - if (errno == ENOENT) { - /* Check the path - if it's a missing directory then - * we need to set PATH_NOT_FOUND not FILE_NOT_FOUND - */ - gchar *dirname; - - - if (dir == NULL) { - dirname = _wapi_dirname (path); - } else { - dirname = g_strdup (dir); - } - - if (_wapi_access (dirname, F_OK) == 0) { - mono_w32error_set_last (ERROR_FILE_NOT_FOUND); - } else { - mono_w32error_set_last (ERROR_PATH_NOT_FOUND); - } - - g_free (dirname); - } else { - _wapi_set_last_error_from_errno (); - } -} - -static gboolean -file_write (FileHandle *filehandle, gpointer buffer, guint32 numbytes, guint32 *byteswritten) -{ - gint ret; - MonoThreadInfo *info = mono_thread_info_current (); - - if(byteswritten!=NULL) { - *byteswritten=0; - } - - if(!(filehandle->fileaccess & GENERIC_WRITE) && !(filehandle->fileaccess & GENERIC_ALL)) { - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_FILE, "%s: fd %d doesn't have GENERIC_WRITE access: %u", __func__, ((MonoFDHandle*) filehandle)->fd, filehandle->fileaccess); - - mono_w32error_set_last (ERROR_ACCESS_DENIED); - return(FALSE); - } - - do { - MONO_ENTER_GC_SAFE; - ret = write (((MonoFDHandle*) filehandle)->fd, buffer, numbytes); - MONO_EXIT_GC_SAFE; - } while (ret == -1 && errno == EINTR && - !mono_thread_info_is_interrupt_state (info)); - - if (ret == -1) { - if (errno == EINTR) { - ret = 0; - } else { - _wapi_set_last_error_from_errno (); - - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_FILE, "%s: write of fd %d error: %s", __func__, ((MonoFDHandle*) filehandle)->fd, g_strerror(errno)); - - return(FALSE); - } - } - if (byteswritten != NULL) { - *byteswritten = ret; - } - return(TRUE); -} - -static gboolean -console_write (FileHandle *filehandle, gpointer buffer, guint32 numbytes, guint32 *byteswritten) -{ - gint ret; - MonoThreadInfo *info = mono_thread_info_current (); - - if(byteswritten!=NULL) { - *byteswritten=0; - } - - if(!(filehandle->fileaccess & (GENERIC_WRITE | GENERIC_ALL))) { - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_FILE, "%s: fd %d doesn't have GENERIC_WRITE access: %u", __func__, ((MonoFDHandle*) filehandle)->fd, filehandle->fileaccess); - - mono_w32error_set_last (ERROR_ACCESS_DENIED); - return(FALSE); - } - - do { - MONO_ENTER_GC_SAFE; - ret = write(((MonoFDHandle*) filehandle)->fd, buffer, numbytes); - MONO_EXIT_GC_SAFE; - } while (ret == -1 && errno == EINTR && - !mono_thread_info_is_interrupt_state (info)); - - if (ret == -1) { - if (errno == EINTR) { - ret = 0; - } else { - _wapi_set_last_error_from_errno (); - - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_FILE, "%s: write of fd %d error: %s", __func__, ((MonoFDHandle*) filehandle)->fd, g_strerror(errno)); - - return(FALSE); - } - } - if(byteswritten!=NULL) { - *byteswritten=ret; - } - - return(TRUE); -} - -static gboolean -pipe_write (FileHandle *filehandle, gpointer buffer, guint32 numbytes, guint32 *byteswritten) -{ - gint ret; - MonoThreadInfo *info = mono_thread_info_current (); - - if(byteswritten!=NULL) { - *byteswritten=0; - } - - if(!(filehandle->fileaccess & (GENERIC_WRITE | GENERIC_ALL))) { - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_FILE, "%s: fd %d doesn't have GENERIC_WRITE access: %u", __func__, ((MonoFDHandle*) filehandle)->fd, filehandle->fileaccess); - - mono_w32error_set_last (ERROR_ACCESS_DENIED); - return(FALSE); - } - - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_FILE, "%s: writing up to %" G_GUINT32_FORMAT " bytes to pipe %d", __func__, numbytes, ((MonoFDHandle*) filehandle)->fd); - - do { - MONO_ENTER_GC_SAFE; - ret = write (((MonoFDHandle*) filehandle)->fd, buffer, numbytes); - MONO_EXIT_GC_SAFE; - } while (ret == -1 && errno == EINTR && - !mono_thread_info_is_interrupt_state (info)); - - if (ret == -1) { - if (errno == EINTR) { - ret = 0; - } else { - _wapi_set_last_error_from_errno (); - - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_FILE, "%s: write of fd %d error: %s", __func__,((MonoFDHandle*) filehandle)->fd, g_strerror(errno)); - - return(FALSE); - } - } - if(byteswritten!=NULL) { - *byteswritten=ret; - } - - return(TRUE); -} - -static gint convert_flags(guint32 fileaccess, guint32 createmode) -{ - gint flags=0; - - switch(fileaccess) { - case GENERIC_READ: - flags=O_RDONLY; - break; - case GENERIC_WRITE: - flags=O_WRONLY; - break; - case GENERIC_READ|GENERIC_WRITE: - flags=O_RDWR; - break; - default: - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_FILE, "%s: Unknown access type 0x%" PRIx32, __func__, - fileaccess); - break; - } - - switch(createmode) { - case CREATE_NEW: - flags|=O_CREAT|O_EXCL; - break; - case CREATE_ALWAYS: - flags|=O_CREAT|O_TRUNC; - break; - case OPEN_EXISTING: - break; - case OPEN_ALWAYS: - flags|=O_CREAT; - break; - case TRUNCATE_EXISTING: - flags|=O_TRUNC; - break; - default: - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_FILE, "%s: Unknown create mode 0x%" PRIx32, __func__, - createmode); - break; - } - - return(flags); -} - -static gboolean share_allows_open (struct stat *statbuf, guint32 sharemode, - guint32 fileaccess, - FileShare **share_info) -{ - gboolean file_already_shared; - guint32 file_existing_share, file_existing_access; - - file_already_shared = file_share_get (statbuf->st_dev, statbuf->st_ino, sharemode, fileaccess, &file_existing_share, &file_existing_access, share_info); - - if (file_already_shared) { - /* The reference to this share info was incremented - * when we looked it up, so be careful to put it back - * if we conclude we can't use this file. - */ - if ((file_existing_share == FILE_SHARE_NONE) || (sharemode == FILE_SHARE_NONE)) { - /* Quick and easy, no possibility to share */ - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_FILE, "%s: Share mode prevents open: requested access: 0x%" PRIx32 ", file has sharing = NONE", __func__, fileaccess); - - file_share_release (*share_info); - *share_info = NULL; - - return(FALSE); - } - - if (((file_existing_share == FILE_SHARE_READ) && - (fileaccess != GENERIC_READ)) || - ((file_existing_share == FILE_SHARE_WRITE) && - (fileaccess != GENERIC_WRITE))) { - /* New access mode doesn't match up */ - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_FILE, "%s: Share mode prevents open: requested access: 0x%" PRIx32 ", file has sharing: 0x%" PRIx32, __func__, fileaccess, file_existing_share); - - file_share_release (*share_info); - *share_info = NULL; - - return(FALSE); - } - } else { - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_FILE, "%s: New file!", __func__); - } - - return(TRUE); -} - -void -mono_w32file_init (void) -{ - MonoFDHandleCallback file_data_callbacks; - memset (&file_data_callbacks, 0, sizeof (file_data_callbacks)); - file_data_callbacks.close = file_data_close; - file_data_callbacks.destroy = file_data_destroy; - - mono_fdhandle_register (MONO_FDTYPE_FILE, &file_data_callbacks); - mono_fdhandle_register (MONO_FDTYPE_CONSOLE, &file_data_callbacks); - mono_fdhandle_register (MONO_FDTYPE_PIPE, &file_data_callbacks); - - mono_coop_mutex_init (&file_share_mutex); -} - -gpointer -mono_w32file_create(const gunichar2 *name, guint32 fileaccess, guint32 sharemode, guint32 createmode, guint32 attrs) -{ - FileHandle *filehandle; - MonoFDType type; - gint flags=convert_flags(fileaccess, createmode); - /*mode_t perms=convert_perms(sharemode);*/ - /* we don't use sharemode, because that relates to sharing of - * the file when the file is open and is already handled by - * other code, perms instead are the on-disk permissions and - * this is a sane default. - */ - mode_t perms=0666; - gchar *filename; - gint fd, ret; - struct stat statbuf; - ERROR_DECL (error); - - if (attrs & FILE_ATTRIBUTE_TEMPORARY) - perms = 0600; - - if (attrs & FILE_ATTRIBUTE_ENCRYPTED){ - mono_w32error_set_last (ERROR_ENCRYPTION_FAILED); - return INVALID_HANDLE_VALUE; - } - - if (name == NULL) { - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_FILE, "%s: name is NULL", __func__); - - mono_w32error_set_last (ERROR_INVALID_NAME); - return(INVALID_HANDLE_VALUE); - } - - filename = mono_unicode_to_external_checked (name, error); - if (filename == NULL) { - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_FILE, "%s: unicode conversion returned NULL; %s", __func__, mono_error_get_message (error)); - - mono_error_cleanup (error); - mono_w32error_set_last (ERROR_INVALID_NAME); - return(INVALID_HANDLE_VALUE); - } - - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_FILE, "%s: Opening %s with share 0x%" PRIx32 " and access 0x%" PRIx32, __func__, - filename, sharemode, fileaccess); - - fd = _wapi_open (filename, flags, perms); - - /* If we were trying to open a directory with write permissions - * (e.g. O_WRONLY or O_RDWR), this call will fail with - * EISDIR. However, this is a bit bogus because calls to - * manipulate the directory (e.g. mono_w32file_set_times) will still work on - * the directory because they use other API calls - * (e.g. utime()). Hence, if we failed with the EISDIR error, try - * to open the directory again without write permission. - */ - if (fd == -1 && errno == EISDIR) - { - /* Try again but don't try to make it writable */ - fd = _wapi_open (filename, flags & ~(O_RDWR|O_WRONLY), perms); - } - - if (fd == -1) { - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_FILE, "%s: Error opening file %s: %s", __func__, filename, g_strerror(errno)); - _wapi_set_last_path_error_from_errno (NULL, filename); - g_free (filename); - - return(INVALID_HANDLE_VALUE); - } - - MONO_ENTER_GC_SAFE; - ret = fstat (fd, &statbuf); - MONO_EXIT_GC_SAFE; - if (ret == -1) { - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_FILE, "%s: fstat error of file %s: %s", __func__, filename, g_strerror (errno)); - _wapi_set_last_error_from_errno (); - MONO_ENTER_GC_SAFE; - close (fd); - MONO_EXIT_GC_SAFE; - - return(INVALID_HANDLE_VALUE); - } - -#ifndef S_ISFIFO -#define S_ISFIFO(m) ((m & S_IFIFO) != 0) -#endif - if (S_ISFIFO (statbuf.st_mode)) { - type = MONO_FDTYPE_PIPE; - /* maintain invariant that pipes have no filename */ - g_free (filename); - filename = NULL; - } else if (S_ISCHR (statbuf.st_mode)) { - type = MONO_FDTYPE_CONSOLE; - } else { - type = MONO_FDTYPE_FILE; - } - - filehandle = file_data_create (type, fd); - filehandle->filename = filename; - filehandle->fileaccess = fileaccess; - filehandle->sharemode = sharemode; - filehandle->attrs = attrs; - - if (!share_allows_open (&statbuf, filehandle->sharemode, filehandle->fileaccess, &filehandle->share_info)) { - mono_w32error_set_last (ERROR_SHARING_VIOLATION); - MONO_ENTER_GC_SAFE; - close (((MonoFDHandle*) filehandle)->fd); - MONO_EXIT_GC_SAFE; - - mono_fdhandle_unref ((MonoFDHandle*) filehandle); - return (INVALID_HANDLE_VALUE); - } - if (!filehandle->share_info) { - /* No space, so no more files can be opened */ - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_FILE, "%s: No space in the share table", __func__); - - mono_w32error_set_last (ERROR_TOO_MANY_OPEN_FILES); - MONO_ENTER_GC_SAFE; - close (((MonoFDHandle*) filehandle)->fd); - MONO_EXIT_GC_SAFE; - - mono_fdhandle_unref ((MonoFDHandle*) filehandle); - return(INVALID_HANDLE_VALUE); - } - -#ifdef HAVE_POSIX_FADVISE - if (attrs & FILE_FLAG_SEQUENTIAL_SCAN) { - MONO_ENTER_GC_SAFE; - posix_fadvise (((MonoFDHandle*) filehandle)->fd, 0, 0, POSIX_FADV_SEQUENTIAL); - MONO_EXIT_GC_SAFE; - } - if (attrs & FILE_FLAG_RANDOM_ACCESS) { - MONO_ENTER_GC_SAFE; - posix_fadvise (((MonoFDHandle*) filehandle)->fd, 0, 0, POSIX_FADV_RANDOM); - MONO_EXIT_GC_SAFE; - } -#endif - -#ifdef F_RDAHEAD - if (attrs & FILE_FLAG_SEQUENTIAL_SCAN) { - MONO_ENTER_GC_SAFE; - fcntl(((MonoFDHandle*) filehandle)->fd, F_RDAHEAD, 1); - MONO_EXIT_GC_SAFE; - } -#endif - - mono_fdhandle_insert ((MonoFDHandle*) filehandle); - - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_FILE, "%s: returning handle %p", __func__, GINT_TO_POINTER(((MonoFDHandle*) filehandle)->fd)); - - return GINT_TO_POINTER(((MonoFDHandle*) filehandle)->fd); -} - - -gboolean -mono_w32file_close (gpointer handle) -{ - if (!mono_fdhandle_close (GPOINTER_TO_INT (handle))) { - mono_w32error_set_last (ERROR_INVALID_HANDLE); - return FALSE; - } - - return TRUE; -} - -static gboolean -mono_w32file_read_or_write (gpointer handle, gpointer buffer, guint32 numbytes, guint32 *bytesread, gint32 *win32error) -{ - MONO_REQ_GC_UNSAFE_MODE; - - FileHandle *filehandle; - gboolean ret = FALSE; - - gboolean const ref = mono_fdhandle_lookup_and_ref(GPOINTER_TO_INT(handle), (MonoFDHandle**) &filehandle); - if (!ref) { - mono_w32error_set_last (ERROR_INVALID_HANDLE); - goto exit; - } - - switch (((MonoFDHandle*) filehandle)->type) { - case MONO_FDTYPE_FILE: - ret = (file_write) (filehandle, buffer, numbytes, bytesread); - break; - case MONO_FDTYPE_CONSOLE: - ret = (console_write) (filehandle, buffer, numbytes, bytesread); - break; - case MONO_FDTYPE_PIPE: - ret = (pipe_write) (filehandle, buffer, numbytes, bytesread); - break; - default: - mono_w32error_set_last (ERROR_INVALID_HANDLE); - break; - } - -exit: - if (ref) - mono_fdhandle_unref ((MonoFDHandle*) filehandle); - if (!ret) - *win32error = mono_w32error_get_last (); - return ret; -} - -gboolean -mono_w32file_write (gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten, gint32 *win32error) -{ - return mono_w32file_read_or_write (handle, (gpointer)buffer, numbytes, byteswritten, win32error); -} diff --git a/src/mono/mono/metadata/w32file-win32.c b/src/mono/mono/metadata/w32file-win32.c deleted file mode 100644 index 077450b52377c..0000000000000 --- a/src/mono/mono/metadata/w32file-win32.c +++ /dev/null @@ -1,80 +0,0 @@ -/** - * \file - * Windows File IO internal calls. - * - * Copyright 2016 Microsoft - * Licensed under the MIT license. See LICENSE file in the project root for full license information. - */ -#include -#include - -#include -#include -#include -#include "icall-decl.h" - -void -mono_w32file_init (void) -{ -} - -gpointer -mono_w32file_create(const gunichar2 *name, guint32 fileaccess, guint32 sharemode, guint32 createmode, guint32 attrs) -{ - gpointer res; - MONO_ENTER_GC_SAFE; - res = CreateFileW (name, fileaccess, sharemode, NULL, createmode, attrs, NULL); - MONO_EXIT_GC_SAFE; - return res; -} - -gboolean -mono_w32file_close (gpointer handle) -{ - gboolean res; - MONO_ENTER_GC_SAFE; - res = CloseHandle (handle); - MONO_EXIT_GC_SAFE; - return res; -} - -static void -win32_io_interrupt_handler (gpointer ignored) -{ -} - -gboolean -mono_w32file_write (gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten, gint32 *win32error) -{ - gboolean res; - MonoThreadInfo *info = mono_thread_info_current (); - gboolean alerted = FALSE; - - if (info) { - mono_thread_info_install_interrupt (win32_io_interrupt_handler, NULL, &alerted); - if (alerted) { - SetLastError (ERROR_OPERATION_ABORTED); - *win32error = ERROR_OPERATION_ABORTED; - return FALSE; - } - mono_win32_enter_blocking_io_call (info, handle); - } - - MONO_ENTER_GC_SAFE; - if (info && mono_thread_info_is_interrupt_state (info)) { - res = FALSE; - SetLastError (ERROR_OPERATION_ABORTED); - } else { - res = WriteFile (handle, buffer, numbytes, (PDWORD)byteswritten, NULL); - } - if (!res) - *win32error = GetLastError (); - MONO_EXIT_GC_SAFE; - - if (info) { - mono_win32_leave_blocking_io_call (info, handle); - mono_thread_info_uninstall_interrupt (&alerted); - } - - return res; -} diff --git a/src/mono/mono/metadata/w32file.h b/src/mono/mono/metadata/w32file.h deleted file mode 100644 index 86e1c05f397e1..0000000000000 --- a/src/mono/mono/metadata/w32file.h +++ /dev/null @@ -1,157 +0,0 @@ -/** - * \file - * File IO internal calls - * - * Authors: - * Dick Porter (dick@ximian.com) - * Dan Lewis (dihlewis@yahoo.co.uk) - * - * (C) 2001 Ximian, Inc. - * Copyright 2012 Xamarin Inc (http://www.xamarin.com) - * Licensed under the MIT license. See LICENSE file in the project root for full license information. - */ - -#ifndef _MONO_METADATA_W32FILE_H_ -#define _MONO_METADATA_W32FILE_H_ - -#include -#include - -#include -#include -#include - -/* This is a copy of System.IO.FileAttributes */ -typedef enum { - FileAttributes_ReadOnly=0x00001, - FileAttributes_Hidden=0x00002, - FileAttributes_System=0x00004, - FileAttributes_Directory=0x00010, - FileAttributes_Archive=0x00020, - FileAttributes_Device=0x00040, - FileAttributes_Normal=0x00080, - FileAttributes_Temporary=0x00100, - FileAttributes_SparseFile=0x00200, - FileAttributes_ReparsePoint=0x00400, - FileAttributes_Compressed=0x00800, - FileAttributes_Offline=0x01000, - FileAttributes_NotContentIndexed=0x02000, - FileAttributes_Encrypted=0x04000, - FileAttributes_MonoExecutable= (int) 0x80000000 -} MonoFileAttributes; - -#if defined (TARGET_IOS) || defined (TARGET_ANDROID) - -extern gint64 -mono_filesize_from_fd (int fd); - -#endif - -#if !defined(HOST_WIN32) - -#define GENERIC_READ 0x80000000 -#define GENERIC_WRITE 0x40000000 -#define GENERIC_EXECUTE 0x20000000 -#define GENERIC_ALL 0x10000000 - -#define FILE_SHARE_NONE 0x00000000 -#define FILE_SHARE_READ 0x00000001 -#define FILE_SHARE_WRITE 0x00000002 -#define FILE_SHARE_DELETE 0x00000004 - -#define CREATE_NEW 1 -#define CREATE_ALWAYS 2 -#define OPEN_EXISTING 3 -#define OPEN_ALWAYS 4 -#define TRUNCATE_EXISTING 5 - -#define FILE_ATTRIBUTE_READONLY 0x00000001 -#define FILE_ATTRIBUTE_HIDDEN 0x00000002 -#define FILE_ATTRIBUTE_SYSTEM 0x00000004 -#define FILE_ATTRIBUTE_DIRECTORY 0x00000010 -#define FILE_ATTRIBUTE_ARCHIVE 0x00000020 -#define FILE_ATTRIBUTE_ENCRYPTED 0x00000040 -#define FILE_ATTRIBUTE_NORMAL 0x00000080 -#define FILE_ATTRIBUTE_TEMPORARY 0x00000100 -#define FILE_ATTRIBUTE_SPARSE_FILE 0x00000200 -#define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400 -#define FILE_ATTRIBUTE_COMPRESSED 0x00000800 -#define FILE_ATTRIBUTE_OFFLINE 0x00001000 -#define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000 -#define FILE_FLAG_OPEN_NO_RECALL 0x00100000 -#define FILE_FLAG_OPEN_REPARSE_POINT 0x00200000 -#define FILE_FLAG_POSIX_SEMANTICS 0x01000000 -#define FILE_FLAG_BACKUP_SEMANTICS 0x02000000 -#define FILE_FLAG_DELETE_ON_CLOSE 0x04000000 -#define FILE_FLAG_SEQUENTIAL_SCAN 0x08000000 -#define FILE_FLAG_RANDOM_ACCESS 0x10000000 -#define FILE_FLAG_NO_BUFFERING 0x20000000 -#define FILE_FLAG_OVERLAPPED 0x40000000 -#define FILE_FLAG_WRITE_THROUGH 0x80000000 - -#define REPLACEFILE_WRITE_THROUGH 0x00000001 -#define REPLACEFILE_IGNORE_MERGE_ERRORS 0x00000002 - -#define MAX_PATH 260 - -#define INVALID_SET_FILE_POINTER ((guint32) 0xFFFFFFFF) -#define INVALID_FILE_SIZE ((guint32) 0xFFFFFFFF) -#define INVALID_FILE_ATTRIBUTES ((guint32) 0xFFFFFFFF) - -#define FILE_TYPE_UNKNOWN 0x0000 -#define FILE_TYPE_DISK 0x0001 -#define FILE_TYPE_CHAR 0x0002 -#define FILE_TYPE_PIPE 0x0003 -#define FILE_TYPE_REMOTE 0x8000 - -#define FILE_BEGIN 0 -#define FILE_CURRENT 1 -#define FILE_END 2 - -#define DRIVE_UNKNOWN 0 -#define DRIVE_NO_ROOT_DIR 1 -#define DRIVE_REMOVABLE 2 -#define DRIVE_FIXED 3 -#define DRIVE_REMOTE 4 -#define DRIVE_CDROM 5 -#define DRIVE_RAMDISK 6 - -typedef struct { - guint16 wYear; - guint16 wMonth; - guint16 wDayOfWeek; - guint16 wDay; - guint16 wHour; - guint16 wMinute; - guint16 wSecond; - guint16 wMilliseconds; -} SYSTEMTIME; - -typedef struct { -#if G_BYTE_ORDER == G_BIG_ENDIAN - guint32 dwHighDateTime; - guint32 dwLowDateTime; -#else - guint32 dwLowDateTime; - guint32 dwHighDateTime; -#endif -} FILETIME; - -#endif /* !defined(HOST_WIN32) */ - -void -mono_w32file_init (void); - -MONO_COMPONENT_API -gpointer -mono_w32file_create(const gunichar2 *name, guint32 fileaccess, guint32 sharemode, guint32 createmode, guint32 attrs); - -MONO_COMPONENT_API -gboolean -mono_w32file_close (gpointer handle); - -MONO_COMPONENT_API -gboolean -mono_w32file_write (gpointer handle, gconstpointer buffer, guint32 numbytes, guint32 *byteswritten, gint32 *win32error); - -#endif /* _MONO_METADATA_W32FILE_H_ */ diff --git a/src/mono/mono/metadata/w32process-unix-bsd.c b/src/mono/mono/metadata/w32process-unix-bsd.c deleted file mode 100644 index 31c672a4bed0f..0000000000000 --- a/src/mono/mono/metadata/w32process-unix-bsd.c +++ /dev/null @@ -1,126 +0,0 @@ -/** - * \file - */ - -#include "w32process.h" -#include "w32process-unix-internals.h" - -#ifdef USE_BSD_BACKEND - -#include -#include -#include -#include -#if !defined(__OpenBSD__) -#include -#endif -#if defined(__FreeBSD__) -#include /* struct kinfo_proc */ -#endif - -#include -#include "utils/mono-logger-internals.h" -#include "icall-decl.h" - -gchar* -mono_w32process_get_name (pid_t pid) -{ - gint mib [6]; - gsize size; - struct kinfo_proc *pi; - gchar *ret = NULL; - -#if defined(__FreeBSD__) - mib [0] = CTL_KERN; - mib [1] = KERN_PROC; - mib [2] = KERN_PROC_PID; - mib [3] = pid; - if (sysctl(mib, 4, NULL, &size, NULL, 0) < 0) { - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: sysctl() failed: %d", __func__, errno); - return NULL; - } - - if ((pi = g_malloc (size)) == NULL) - return NULL; - - if (sysctl (mib, 4, pi, &size, NULL, 0) < 0) { - if (errno == ENOMEM) { - g_free (pi); - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Didn't allocate enough memory for kproc info", __func__); - } - return NULL; - } - - ret = strlen (pi->ki_comm) > 0 ? g_strdup (pi->ki_comm) : NULL; - - g_free (pi); -#elif defined(__OpenBSD__) - mib [0] = CTL_KERN; - mib [1] = KERN_PROC; - mib [2] = KERN_PROC_PID; - mib [3] = pid; - mib [4] = sizeof(struct kinfo_proc); - mib [5] = 0; - -retry: - if (sysctl(mib, 6, NULL, &size, NULL, 0) < 0) { - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: sysctl() failed: %d", __func__, errno); - return NULL; - } - - if ((pi = g_malloc (size)) == NULL) - return NULL; - - mib[5] = (int)(size / sizeof(struct kinfo_proc)); - - if ((sysctl (mib, 6, pi, &size, NULL, 0) < 0) || - (size != sizeof (struct kinfo_proc))) { - if (errno == ENOMEM) { - g_free (pi); - goto retry; - } - return NULL; - } - - ret = strlen (pi->p_comm) > 0 ? g_strdup (pi->p_comm) : NULL; - - g_free (pi); -#endif - - return ret; -} - -gchar* -mono_w32process_get_path (pid_t pid) -{ -#if defined (__OpenBSD__) - // No KERN_PROC_PATHNAME on OpenBSD - return mono_w32process_get_name (pid); -#else - gsize path_len = PATH_MAX + 1; - gchar path [PATH_MAX + 1]; - gint mib [4]; - mib [0] = CTL_KERN; -#if defined (__NetBSD__) - mib [1] = KERN_PROC_ARGS; - mib [2] = pid; - mib [3] = KERN_PROC_PATHNAME; -#else // FreeBSD - mib [1] = KERN_PROC; - mib [2] = KERN_PROC_PATHNAME; - mib [3] = pid; -#endif - if (sysctl (mib, 4, path, &path_len, NULL, 0) < 0) { - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: sysctl() failed: %d", __func__, errno); - return NULL; - } else { - return g_strdup (path); - } -#endif -} - -#else - -MONO_EMPTY_SOURCE_FILE (w32process_unix_bsd); - -#endif diff --git a/src/mono/mono/metadata/w32process-unix-default.c b/src/mono/mono/metadata/w32process-unix-default.c deleted file mode 100644 index ef83bb22007f7..0000000000000 --- a/src/mono/mono/metadata/w32process-unix-default.c +++ /dev/null @@ -1,132 +0,0 @@ -/** - * \file - */ - -#include "w32process.h" -#include "w32process-unix-internals.h" - -#ifdef USE_DEFAULT_BACKEND - -#include - -#ifdef HOST_SOLARIS -/* procfs.h cannot be included if this define is set, but it seems to work fine if it is undefined */ -#if _FILE_OFFSET_BITS == 64 -#undef _FILE_OFFSET_BITS -#include -#define _FILE_OFFSET_BITS 64 -#else -#include -#endif -#endif - -#ifdef _AIX -/* like solaris, just different */ -#include -/* fallback for procfs-less i */ -#include -#include -#endif - -#include "utils/mono-logger-internals.h" -#include "icall-decl.h" - -#ifndef MAXPATHLEN -#define MAXPATHLEN 242 -#endif - -/* XXX: why don't we just use proclib? */ -gchar* -mono_w32process_get_name (pid_t pid) -{ - FILE *fp; - gchar *filename; - gchar *ret = NULL; - -#if defined(HOST_SOLARIS) || (defined(_AIX) && !defined(__PASE__)) - filename = g_strdup_printf ("/proc/%d/psinfo", pid); - if ((fp = fopen (filename, "r")) != NULL) { - struct psinfo info; - int nread; - - nread = fread (&info, sizeof (info), 1, fp); - if (nread == 1) { - ret = g_strdup (info.pr_fname); - } - - fclose (fp); - } - g_free (filename); -#elif defined(__PASE__) - /* AIX has a procfs, but it's not available on i */ - struct procentry64 proc; - pid_t newpid; - - newpid = pid; - if (getprocs64(&proc, sizeof (proc), NULL, NULL, &newpid, 1) == 1) { - ret = g_strdup (proc.pi_comm); - } -#else - gchar buf[256]; - memset (buf, '\0', sizeof(buf)); - filename = g_strdup_printf ("/proc/%d/exe", pid); -#if defined(HAVE_READLINK) - if (readlink (filename, buf, 255) > 0) { - ret = g_strdup (buf); - } -#endif - g_free (filename); - - if (ret != NULL) { - return(ret); - } - - filename = g_strdup_printf ("/proc/%d/cmdline", pid); - if ((fp = fopen (filename, "r")) != NULL) { - if (fgets (buf, 256, fp) != NULL) { - ret = g_strdup (buf); - } - - fclose (fp); - } - g_free (filename); - - if (ret != NULL) { - return(ret); - } - - filename = g_strdup_printf ("/proc/%d/stat", pid); - if ((fp = fopen (filename, "r")) != NULL) { - if (fgets (buf, 256, fp) != NULL) { - char *start, *end; - - start = strchr (buf, '('); - if (start != NULL) { - end = strchr (start + 1, ')'); - - if (end != NULL) { - ret = g_strndup (start + 1, - end - start - 1); - } - } - } - - fclose (fp); - } - g_free (filename); -#endif - - return ret; -} - -gchar* -mono_w32process_get_path (pid_t pid) -{ - return mono_w32process_get_name (pid); -} - -#else - -MONO_EMPTY_SOURCE_FILE (w32process_unix_default); - -#endif diff --git a/src/mono/mono/metadata/w32process-unix-haiku.c b/src/mono/mono/metadata/w32process-unix-haiku.c deleted file mode 100644 index 4422705548279..0000000000000 --- a/src/mono/mono/metadata/w32process-unix-haiku.c +++ /dev/null @@ -1,36 +0,0 @@ -/** - * \file - */ - -#include "w32process.h" -#include "w32process-unix-internals.h" - -#ifdef USE_HAIKU_BACKEND - -/* KernelKit.h doesn't include the right headers? */ -#include - -gchar* -mono_w32process_get_name (pid_t pid) -{ - image_info imageInfo; - int32 cookie = 0; - - if (get_next_image_info ((team_id) pid, &cookie, &imageInfo) != B_OK) - return NULL; - - return g_strdup (imageInfo.name); -} - -gchar* -mono_w32process_get_path (pid_t pid) -{ - return mono_w32process_get_name (pid); -} - - -#else - -MONO_EMPTY_SOURCE_FILE (w32process_unix_haiku); - -#endif diff --git a/src/mono/mono/metadata/w32process-unix-internals.h b/src/mono/mono/metadata/w32process-unix-internals.h deleted file mode 100644 index e1cbf72e7f57e..0000000000000 --- a/src/mono/mono/metadata/w32process-unix-internals.h +++ /dev/null @@ -1,30 +0,0 @@ -/** - * \file - */ - -#ifndef _MONO_METADATA_W32PROCESS_UNIX_INTERNALS_H_ -#define _MONO_METADATA_W32PROCESS_UNIX_INTERNALS_H_ - -#include -#include - -/* - * FOR EXCLUSIVE USE BY w32process-unix.c - */ - -#if defined(HOST_DARWIN) -#define USE_OSX_BACKEND -#elif (defined(__OpenBSD__) || defined(__FreeBSD__)) && defined(HAVE_LINK_H) -#define USE_BSD_BACKEND -#elif defined(__HAIKU__) -#define USE_HAIKU_BACKEND -/* Define header for team_info */ -#include -#else -#define USE_DEFAULT_BACKEND -#endif - -gchar* -mono_w32process_get_name (pid_t pid); - -#endif /* _MONO_METADATA_W32PROCESS_UNIX_INTERNALS_H_ */ diff --git a/src/mono/mono/metadata/w32process-unix-osx.c b/src/mono/mono/metadata/w32process-unix-osx.c deleted file mode 100644 index 9fc88b8deae7d..0000000000000 --- a/src/mono/mono/metadata/w32process-unix-osx.c +++ /dev/null @@ -1,125 +0,0 @@ -/** - * \file - */ - -#include "w32process.h" -#include "w32process-unix-internals.h" - -#ifdef USE_OSX_BACKEND - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -/* sys/resource.h (for rusage) is required when using osx 10.3 (but not 10.4) */ -#ifdef __APPLE__ -#include -#include -#ifdef HAVE_LIBPROC_H -/* proc_name */ -#include -#endif -#endif - -#include "utils/mono-logger-internals.h" -#include "icall-decl.h" - -gchar* -mono_w32process_get_name (pid_t pid) -{ - gchar *ret = NULL; - -#if defined (__mono_ppc__) || !defined (TARGET_OSX) - size_t size; - struct kinfo_proc *pi; - gint mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid }; - - if (sysctl(mib, 4, NULL, &size, NULL, 0) < 0) - return(ret); - - if ((pi = g_malloc (size)) == NULL) - return(ret); - - if (sysctl (mib, 4, pi, &size, NULL, 0) < 0) { - if (errno == ENOMEM) { - g_free (pi); - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Didn't allocate enough memory for kproc info", __func__); - } - return(ret); - } - - if (strlen (pi->kp_proc.p_comm) > 0) - ret = g_strdup (pi->kp_proc.p_comm); - - g_free (pi); -#else - gchar buf[256]; - gint res; - - /* No proc name on OSX < 10.5 nor ppc nor iOS */ - memset (buf, '\0', sizeof(buf)); - res = proc_name (pid, buf, sizeof(buf)); - if (res == 0) { - mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: proc_name failed, error (%d) \"%s\"", __func__, errno, g_strerror (errno)); - return NULL; - } - - // Fixes proc_name triming values to 15 characters #32539 - if (strlen (buf) >= MAXCOMLEN - 1) { - gchar path_buf [PROC_PIDPATHINFO_MAXSIZE]; - gchar *name_buf; - gint path_len; - - memset (path_buf, '\0', sizeof(path_buf)); - path_len = proc_pidpath (pid, path_buf, sizeof(path_buf)); - - if (path_len > 0 && path_len < sizeof(path_buf)) { - name_buf = path_buf + path_len; - for(;name_buf > path_buf; name_buf--) { - if (name_buf [0] == '/') { - name_buf++; - break; - } - } - - if (memcmp (buf, name_buf, MAXCOMLEN - 1) == 0) - ret = g_strdup (name_buf); - } - } - - if (ret == NULL && strlen (buf) > 0) - ret = g_strdup (buf); -#endif - - return ret; -} - -gchar* -mono_w32process_get_path (pid_t pid) -{ -#if defined(__mono_ppc__) || !defined(TARGET_OSX) - return mono_w32process_get_name (pid); -#else - gchar buf [PROC_PIDPATHINFO_MAXSIZE]; - gint res; - - res = proc_pidpath (pid, buf, sizeof (buf)); - if (res <= 0) - return NULL; - if (buf [0] == '\0') - return NULL; - return g_strdup (buf); -#endif -} - -#else - -MONO_EMPTY_SOURCE_FILE (w32process_unix_osx); - -#endif diff --git a/src/mono/mono/metadata/w32process.h b/src/mono/mono/metadata/w32process.h deleted file mode 100644 index dc3046e882696..0000000000000 --- a/src/mono/mono/metadata/w32process.h +++ /dev/null @@ -1,29 +0,0 @@ -/** - * \file - * System.Diagnostics.Process support - * - * Author: - * Dick Porter (dick@ximian.com) - * - * (C) 2002 Ximian, Inc. - */ - -#ifndef _MONO_METADATA_W32PROCESS_H_ -#define _MONO_METADATA_W32PROCESS_H_ - -#include -#include - -#include - -#if HAVE_SYS_TYPES_H -#include -#endif - -#ifndef HOST_WIN32 - -gchar* -mono_w32process_get_path (pid_t pid); - -#endif -#endif /* _MONO_METADATA_W32PROCESS_H_ */ diff --git a/src/mono/mono/mini/driver.c b/src/mono/mono/mini/driver.c index 4166aa23176ad..f4ce3d927e35a 100644 --- a/src/mono/mono/mini/driver.c +++ b/src/mono/mono/mini/driver.c @@ -39,12 +39,10 @@ #include #include #include -#include #include #include #include #include -#include #include "mono/utils/mono-counters.h" #include "mono/utils/mono-hwcap.h" #include "mono/utils/mono-logger-internals.h" @@ -2556,7 +2554,6 @@ mono_main (int argc, char* argv[]) #endif mono_set_defaults (mini_verbose_level, opt); - mono_set_os_args (argc, argv); domain = mini_init (argv [i], forced_version); diff --git a/src/mono/mono/utils/mono-forward-internal.h b/src/mono/mono/utils/mono-forward-internal.h index 6b096bd0b433d..d4d8fa9bf1c6f 100644 --- a/src/mono/mono/utils/mono-forward-internal.h +++ b/src/mono/mono/utils/mono-forward-internal.h @@ -7,7 +7,7 @@ #ifndef _MONO_UTILS_FORWARD_INTERNAL_ #define _MONO_UTILS_FORWARD_INTERNAL_ -#include "mono/utils/mono-forward.h" +#include typedef struct MonoAotModule MonoAotModule; typedef struct MonoHandleStack MonoHandleStack; diff --git a/src/mono/mono/utils/mono-threads.h b/src/mono/mono/utils/mono-threads.h index d0efd7d77c1c6..36cc46affe0e8 100644 --- a/src/mono/mono/utils/mono-threads.h +++ b/src/mono/mono/utils/mono-threads.h @@ -539,7 +539,7 @@ mono_thread_info_self_interrupt (void); void mono_thread_info_clear_self_interrupt (void); -gboolean +MONO_COMPONENT_API gboolean mono_thread_info_is_interrupt_state (THREAD_INFO_TYPE *info); void