Merge branch 'master' into msbuilddll2
[mono.git] / mono / metadata / process.c
old mode 100644 (file)
new mode 100755 (executable)
index ab6ed49..4b1c43b
@@ -13,7 +13,7 @@
 #include <glib.h>
 #include <string.h>
 
-#include <mono/metadata/object.h>
+#include <mono/metadata/object-internals.h>
 #include <mono/metadata/process.h>
 #include <mono/metadata/assembly.h>
 #include <mono/metadata/appdomain.h>
 #include <mono/utils/strenc.h>
 #include <mono/utils/mono-proclib.h>
 #include <mono/io-layer/io-layer.h>
+#if defined (MINGW_CROSS_COMPILE) && defined (HAVE_GETPROCESSID)
+#undef HAVE_GETPROCESSID
+#endif
+#ifndef HAVE_GETPROCESSID
+#if defined(_MSC_VER) || defined(HAVE_WINTERNL_H)
+#include <winternl.h>
+#ifndef NT_SUCCESS
+#define NT_SUCCESS(status) ((NTSTATUS) (status) >= 0)
+#endif /* !NT_SUCCESS */
+#else /* ! (defined(_MSC_VER) || defined(HAVE_WINTERNL_H)) */
+#include <ddk/ntddk.h>
+#include <ddk/ntapi.h>
+#endif /* (defined(_MSC_VER) || defined(HAVE_WINTERNL_H)) */
+#endif /* !HAVE_GETPROCESSID */
 /* FIXME: fix this code to not depend so much on the inetrnals */
 #include <mono/metadata/class-internals.h>
 
-#undef DEBUG
+#define LOGDEBUG(...)  
+/* define LOGDEBUG(...) g_message(__VA_ARGS__)  */
+
 
 HANDLE ves_icall_System_Diagnostics_Process_GetProcess_internal (guint32 pid)
 {
@@ -60,11 +76,14 @@ void ves_icall_System_Diagnostics_Process_Process_free_internal (MonoObject *thi
        MONO_ARCH_SAVE_REGS;
 
 #ifdef THREAD_DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": Closing process %p, handle %p",
-                  this, process);
+       g_message ("%s: Closing process %p, handle %p", __func__, this, process);
 #endif
 
+#if defined(TARGET_WIN32) || defined(HOST_WIN32)
        CloseHandle (process);
+#else
+       CloseProcess (process);
+#endif
 }
 
 #define STASH_SYS_ASS(this) \
@@ -91,15 +110,11 @@ static void process_set_field_object (MonoObject *obj, const gchar *fieldname,
 {
        MonoClassField *field;
 
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": Setting field %s to object at %p",
-                  fieldname, data);
-#endif
+       LOGDEBUG (g_message ("%s: Setting field %s to object at %p", __func__, fieldname, data));
 
        field=mono_class_get_field_from_name (mono_object_class (obj),
                                              fieldname);
-       /* FIXME: moving GC */
-       *(MonoObject **)(((char *)obj) + field->offset)=data;
+       mono_gc_wbarrier_generic_store (((char *)obj) + field->offset, data);
 }
 
 static void process_set_field_string (MonoObject *obj, const gchar *fieldname,
@@ -108,17 +123,13 @@ static void process_set_field_string (MonoObject *obj, const gchar *fieldname,
        MonoClassField *field;
        MonoString *string;
 
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": Setting field %s to [%s]",
-                  fieldname, g_utf16_to_utf8 (val, len, NULL, NULL, NULL));
-#endif
+       LOGDEBUG (g_message ("%s: Setting field %s to [%s]", __func__, fieldname, g_utf16_to_utf8 (val, len, NULL, NULL, NULL)));
 
        string=mono_string_new_utf16 (mono_object_domain (obj), val, len);
        
        field=mono_class_get_field_from_name (mono_object_class (obj),
                                              fieldname);
-       /* FIXME: moving GC */
-       *(MonoString **)(((char *)obj) + field->offset)=string;
+       mono_gc_wbarrier_generic_store (((char *)obj) + field->offset, (MonoObject*)string);
 }
 
 static void process_set_field_int (MonoObject *obj, const gchar *fieldname,
@@ -126,10 +137,7 @@ static void process_set_field_int (MonoObject *obj, const gchar *fieldname,
 {
        MonoClassField *field;
 
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": Setting field %s to %d",
-                  fieldname, val);
-#endif
+       LOGDEBUG (g_message ("%s: Setting field %s to %d", __func__,fieldname, val));
        
        field=mono_class_get_field_from_name (mono_object_class (obj),
                                              fieldname);
@@ -141,9 +149,7 @@ static void process_set_field_intptr (MonoObject *obj, const gchar *fieldname,
 {
        MonoClassField *field;
 
-#ifdef DEBUG
-       g_message ("%s: Setting field %s to %p", __func__, fieldname, val);
-#endif
+       LOGDEBUG (g_message ("%s: Setting field %s to %p", __func__, fieldname, val));
        
        field=mono_class_get_field_from_name (mono_object_class (obj),
                                              fieldname);
@@ -155,10 +161,7 @@ static void process_set_field_bool (MonoObject *obj, const gchar *fieldname,
 {
        MonoClassField *field;
 
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": Setting field %s to %s",
-                  fieldname, val?"TRUE":"FALSE");
-#endif
+       LOGDEBUG (g_message ("%s: Setting field %s to %s", __func__, fieldname, val?"TRUE":"FALSE"));
        
        field=mono_class_get_field_from_name (mono_object_class (obj),
                                              fieldname);
@@ -190,17 +193,12 @@ static void process_module_string_read (MonoObject *filever, gpointer data,
 
        lang_key_utf8 = g_strdup_printf (key, lang_lo, lang_hi, 0x04, 0xb0);
 
-#ifdef DEBUG
-       g_message ("%s: asking for [%s]", __func__, lang_key_utf8);
-#endif
+       LOGDEBUG (g_message ("%s: asking for [%s]", __func__, lang_key_utf8));
 
        lang_key = g_utf8_to_utf16 (lang_key_utf8, -1, NULL, NULL, NULL);
 
        if (VerQueryValue (data, lang_key, (gpointer *)&buffer, &chars) && chars > 0) {
-#ifdef DEBUG
-               g_message ("%s: found %d chars of [%s]", __func__, chars,
-                          g_utf16_to_utf8 (buffer, chars, NULL, NULL, NULL));
-#endif
+               LOGDEBUG (g_message ("%s: found %d chars of [%s]", __func__, chars, g_utf16_to_utf8 (buffer, chars, NULL, NULL, NULL)));
                /* chars includes trailing null */
                process_set_field_string (filever, fieldname, buffer, chars - 1);
        } else {
@@ -267,9 +265,7 @@ static void process_get_fileversion (MonoObject *filever, gunichar2 *filename)
                        
                        if (VerQueryValue (data, query, (gpointer *)&ffi,
                            &ffi_size)) {
-#ifdef DEBUG
-                               g_message (G_GNUC_PRETTY_FUNCTION ": recording assembly: FileName [%s] FileVersionInfo [%d.%d.%d.%d]", g_utf16_to_utf8 (filename, -1, NULL, NULL, NULL), HIWORD (ffi->dwFileVersionMS), LOWORD (ffi->dwFileVersionMS), HIWORD (ffi->dwFileVersionLS), LOWORD (ffi->dwFileVersionLS));
-#endif
+                               LOGDEBUG (g_message ("%s: recording assembly: FileName [%s] FileVersionInfo [%d.%d.%d.%d]", __func__, g_utf16_to_utf8 (filename, -1, NULL, NULL, NULL), HIWORD (ffi->dwFileVersionMS), LOWORD (ffi->dwFileVersionMS), HIWORD (ffi->dwFileVersionLS), LOWORD (ffi->dwFileVersionLS)));
        
                                process_set_field_int (filever, "filemajorpart", HIWORD (ffi->dwFileVersionMS));
                                process_set_field_int (filever, "fileminorpart", LOWORD (ffi->dwFileVersionMS));
@@ -301,9 +297,7 @@ static void process_get_fileversion (MonoObject *filever, gunichar2 *filename)
                                /* use the first language ID we see
                                 */
                                if (trans_size >= 4) {
-#ifdef DEBUG
-                                       g_message("%s: %s has 0x%0x 0x%0x 0x%0x 0x%0x", __func__, g_utf16_to_utf8 (filename, -1, NULL, NULL, NULL), trans_data[0], trans_data[1], trans_data[2], trans_data[3]);
-#endif
+                                       LOGDEBUG (g_message("%s: %s has 0x%0x 0x%0x 0x%0x 0x%0x", __func__, g_utf16_to_utf8 (filename, -1, NULL, NULL, NULL), trans_data[0], trans_data[1], trans_data[2], trans_data[3]));
                                        lang = (trans_data[0]) |
                                                (trans_data[1] << 8) |
                                                (trans_data[2] << 16) |
@@ -375,8 +369,7 @@ static void process_get_fileversion (MonoObject *filever, gunichar2 *filename)
        }
 }
 
-static void process_add_module (GPtrArray *modules, HANDLE process, HMODULE mod,
-                               gunichar2 *filename, gunichar2 *modulename)
+static MonoObject* process_add_module (HANDLE process, HMODULE mod, gunichar2 *filename, gunichar2 *modulename)
 {
        MonoClass *proc_class, *filever_class;
        MonoObject *item, *filever;
@@ -415,50 +408,47 @@ static void process_add_module (GPtrArray *modules, HANDLE process, HMODULE mod,
                                  unicode_chars (modulename));
        process_set_field_object (item, "version_info", filever);
 
-       /* FIXME: moving GC */
-       g_ptr_array_add (modules, item);
+       return item;
 }
 
 /* Returns an array of System.Diagnostics.ProcessModule */
 MonoArray *ves_icall_System_Diagnostics_Process_GetModules_internal (MonoObject *this, HANDLE process)
 {
-       GPtrArray *modules_list=g_ptr_array_new ();
+       MonoArray *temp_arr = NULL;
        MonoArray *arr;
        HMODULE mods[1024];
        gunichar2 filename[MAX_PATH];
        gunichar2 modname[MAX_PATH];
        DWORD needed;
-       guint32 count;
-       guint32 i;
-       
-       MONO_ARCH_SAVE_REGS;
+       guint32 count = 0;
+       guint32 i, num_added = 0;
 
        STASH_SYS_ASS (this);
 
        if (EnumProcessModules (process, mods, sizeof(mods), &needed)) {
                count = needed / sizeof(HMODULE);
+               temp_arr = mono_array_new (mono_domain_get (), mono_get_object_class (), count);
                for (i = 0; i < count; i++) {
-                       if (GetModuleBaseName (process, mods[i], modname,
-                                              MAX_PATH) &&
-                           GetModuleFileNameEx (process, mods[i], filename,
-                                                MAX_PATH)) {
-                               process_add_module (modules_list, process,
-                                                   mods[i], filename, modname);
+                       if (GetModuleBaseName (process, mods[i], modname, MAX_PATH) &&
+                                       GetModuleFileNameEx (process, mods[i], filename, MAX_PATH)) {
+                               MonoObject *module = process_add_module (process, mods[i],
+                                               filename, modname);
+                               mono_array_setref (temp_arr, num_added++, module);
                        }
                }
        }
 
-       /* Build a MonoArray out of modules_list */
-       arr=mono_array_new (mono_domain_get (), mono_get_object_class (),
-                           modules_list->len);
-       
-       for(i=0; i<modules_list->len; i++) {
-               mono_array_setref (arr, i, g_ptr_array_index (modules_list, i));
+       if (count == num_added) {
+               arr = temp_arr;
+       } else {
+               /* shorter version of the array */
+               arr = mono_array_new (mono_domain_get (), mono_get_object_class (), num_added);
+
+               for (i = 0; i < num_added; i++)
+                       mono_array_setref (arr, i, mono_array_get (temp_arr, MonoObject*, i));
        }
-       
-       g_ptr_array_free (modules_list, TRUE);
-       
-       return(arr);
+
+       return arr;
 }
 
 void ves_icall_System_Diagnostics_FileVersionInfo_GetVersionInfo_internal (MonoObject *this, MonoString *filename)
@@ -478,7 +468,7 @@ static gchar *
 quote_path (const gchar *path)
 {
        gchar *res = g_shell_quote (path);
-#ifdef PLATFORM_WIN32
+#ifdef TARGET_WIN32
        {
        gchar *q = res;
        while (*q) {
@@ -499,7 +489,7 @@ complete_path (const gunichar2 *appname, gchar **completed)
        gchar *found;
 
        utf8appmemory = utf8app = g_utf16_to_utf8 (appname, -1, NULL, NULL, NULL);
-#ifdef PLATFORM_WIN32 // Should this happen on all platforms? 
+#ifdef TARGET_WIN32 // Should this happen on all platforms? 
        {
                // remove the quotes around utf8app.
                size_t len;
@@ -540,7 +530,7 @@ complete_path (const gunichar2 *appname, gchar **completed)
 
 #ifndef HAVE_GETPROCESSID
 /* Run-time GetProcessId detection for Windows */
-#ifdef PLATFORM_WIN32
+#ifdef TARGET_WIN32
 #define HAVE_GETPROCESSID
 
 typedef DWORD (WINAPI *GETPROCESSID_PROC) (HANDLE);
@@ -605,7 +595,7 @@ static DWORD WINAPI GetProcessId_detect (HANDLE process)
        GetProcessId = &GetProcessId_stub;
        return GetProcessId (process);
 }
-#endif /* PLATFORM_WIN32 */
+#endif /* HOST_WIN32 */
 #endif /* !HAVE_GETPROCESSID */
 
 MonoBoolean ves_icall_System_Diagnostics_Process_ShellExecuteEx_internal (MonoProcessStartInfo *proc_start_info, MonoProcInfo *process_info)
@@ -614,7 +604,7 @@ MonoBoolean ves_icall_System_Diagnostics_Process_ShellExecuteEx_internal (MonoPr
        gboolean ret;
 
        shellex.cbSize = sizeof(SHELLEXECUTEINFO);
-       shellex.fMask = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_NOCLOSEPROCESS | SEE_MASK_UNICODE;
+       shellex.fMask = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_UNICODE;
        shellex.nShow = proc_start_info->window_style;
        shellex.nShow = (shellex.nShow == 0) ? 1 : (shellex.nShow == 1 ? 0 : shellex.nShow);
        
@@ -644,22 +634,6 @@ MonoBoolean ves_icall_System_Diagnostics_Process_ShellExecuteEx_internal (MonoPr
        }
 
        ret = ShellExecuteEx (&shellex);
-       if (ret == FALSE) {
-               process_info->pid = -GetLastError ();
-       } else {
-               process_info->process_handle = shellex.hProcess;
-               process_info->thread_handle = NULL;
-               /* It appears that there's no way to get the pid from a
-                * process handle before windows xp.  Really.
-                */
-#ifdef HAVE_GETPROCESSID
-               process_info->pid = GetProcessId (shellex.hProcess);
-#else
-               process_info->pid = 0;
-#endif
-               process_info->tid = 0;
-       }
-
        return (ret);
 }
 
@@ -692,7 +666,7 @@ MonoBoolean ves_icall_System_Diagnostics_Process_CreateProcess_internal (MonoPro
                process_info->pid = -ERROR_FILE_NOT_FOUND;
                return FALSE;
        }
-#ifdef PLATFORM_WIN32
+#ifdef TARGET_WIN32
        /* Seems like our CreateProcess does not work as the windows one.
         * This hack is needed to deal with paths containing spaces */
        shell_path = NULL;
@@ -757,7 +731,7 @@ MonoBoolean ves_icall_System_Diagnostics_Process_CreateProcess_internal (MonoPro
        /* The default dir name is "".  Turn that into NULL to mean
         * "current directory"
         */
-       if(mono_string_length (proc_start_info->working_directory)==0) {
+       if(proc_start_info->working_directory == NULL || mono_string_length (proc_start_info->working_directory)==0) {
                dir=NULL;
        } else {
                dir=mono_string_chars (proc_start_info->working_directory);
@@ -872,9 +846,7 @@ gint32 ves_icall_System_Diagnostics_Process_ExitCode_internal (HANDLE process)
 
        GetExitCodeProcess (process, &code);
        
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": process exit code is %d", code);
-#endif
+       LOGDEBUG (g_message ("%s: process exit code is %d", __func__, code));
        
        return(code);
 }
@@ -900,10 +872,7 @@ MonoString *ves_icall_System_Diagnostics_Process_ProcessName_internal (HANDLE pr
                return(NULL);
        }
        
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": process name is [%s]",
-                  g_utf16_to_utf8 (name, -1, NULL, NULL, NULL));
-#endif
+       LOGDEBUG (g_message ("%s: process name is [%s]", __func__, g_utf16_to_utf8 (name, -1, NULL, NULL, NULL)));
        
        string=mono_string_new_utf16 (mono_domain_get (), name, len);
        
@@ -916,25 +885,38 @@ MonoArray *ves_icall_System_Diagnostics_Process_GetProcesses_internal (void)
        MonoArray *procs;
        gboolean ret;
        DWORD needed;
-       guint32 count, i;
-       DWORD pids[1024];
+       guint32 count;
+       guint32 *pids;
 
        MONO_ARCH_SAVE_REGS;
 
-       ret=EnumProcesses (pids, sizeof(pids), &needed);
-       if(ret==FALSE) {
-               /* FIXME: throw an exception */
-               return(NULL);
-       }
-       
-       count=needed/sizeof(DWORD);
-       procs=mono_array_new (mono_domain_get (), mono_get_int32_class (),
-                             count);
-       for(i=0; i<count; i++) {
-               mono_array_set (procs, guint32, i, pids[i]);
-       }
+       count = 512;
+       do {
+               pids = g_new0 (guint32, count);
+               ret = EnumProcesses (pids, count * sizeof (guint32), &needed);
+               if (ret == FALSE) {
+                       MonoException *exc;
+
+                       g_free (pids);
+                       pids = NULL;
+                       exc = mono_get_exception_not_supported ("This system does not support EnumProcesses");
+                       mono_raise_exception (exc);
+                       g_assert_not_reached ();
+               }
+               if (needed < (count * sizeof (guint32)))
+                       break;
+               g_free (pids);
+               pids = NULL;
+               count = (count * 3) / 2;
+       } while (TRUE);
+
+       count = needed / sizeof (guint32);
+       procs = mono_array_new (mono_domain_get (), mono_get_int32_class (), count);
+       memcpy (mono_array_addr (procs, guint32, 0), pids, needed);
+       g_free (pids);
+       pids = NULL;
        
-       return(procs);
+       return procs;
 }
 
 MonoBoolean ves_icall_System_Diagnostics_Process_GetWorkingSet_internal (HANDLE process, guint32 *min, guint32 *max)
@@ -1024,9 +1006,7 @@ ves_icall_System_Diagnostics_Process_ProcessHandle_duplicate (HANDLE process)
 
        MONO_ARCH_SAVE_REGS;
 
-#ifdef DEBUG
-       g_message ("%s: Duplicating process handle %p", __func__, process);
-#endif
+       LOGDEBUG (g_message ("%s: Duplicating process handle %p", __func__, process));
        
        DuplicateHandle (GetCurrentProcess (), process, GetCurrentProcess (),
                         &ret, THREAD_ALL_ACCESS, TRUE, 0);
@@ -1039,9 +1019,7 @@ ves_icall_System_Diagnostics_Process_ProcessHandle_close (HANDLE process)
 {
        MONO_ARCH_SAVE_REGS;
 
-#ifdef DEBUG
-       g_message ("%s: Closing process handle %p", __func__, process);
-#endif
+       LOGDEBUG (g_message ("%s: Closing process handle %p", __func__, process));
 
        CloseHandle (process);
 }