Merge pull request #1923 from lukaszunity/start-managed-process-fix
[mono.git] / mono / io-layer / processes.c
index 163dbd8e37ec2887e87fb1e155a36c93218d6d1c..5f0805878cf7173c6c492460d6254159a2c3e859 100644 (file)
@@ -178,7 +178,9 @@ is_pid_valid (pid_t pid)
 {
        gboolean result = FALSE;
 
-#if defined(PLATFORM_MACOSX) || defined(__OpenBSD__) || defined(__FreeBSD__)
+#if defined(HOST_WATCHOS)
+       result = TRUE; // TODO: Rewrite using sysctl
+#elif defined(PLATFORM_MACOSX) || defined(__OpenBSD__) || defined(__FreeBSD__)
        if (((kill(pid, 0) == 0) || (errno == EPERM)) && pid != 0)
                result = TRUE;
 #elif defined(__HAIKU__)
@@ -815,11 +817,11 @@ gboolean CreateProcess (const gunichar2 *appname, const gunichar2 *cmdline,
 
                if (newapp != NULL) {
                        if (appname != NULL) {
-                               newcmd = utf16_concat (newapp, utf16_space,
+                               newcmd = utf16_concat (utf16_quote, newapp, utf16_quote, utf16_space,
                                                       appname, utf16_space,
                                                       cmdline, NULL);
                        } else {
-                               newcmd = utf16_concat (newapp, utf16_space,
+                               newcmd = utf16_concat (utf16_quote, newapp, utf16_quote, utf16_space,
                                                       cmdline, NULL);
                        }
                        
@@ -1673,6 +1675,7 @@ static gboolean match_procname_to_modulename (char *procname, char *modulename)
        if (procname == NULL || modulename == NULL)
                return (FALSE);
 
+       DEBUG ("%s: procname=\"%s\", modulename=\"%s\"", __func__, procname, modulename);
        pname = mono_path_resolve_symlinks (procname);
        mname = mono_path_resolve_symlinks (modulename);
 
@@ -1701,6 +1704,7 @@ static gboolean match_procname_to_modulename (char *procname, char *modulename)
        g_free (pname);
        g_free (mname);
 
+       DEBUG ("%s: result is %d", __func__, result);
        return result;
 }
 
@@ -1727,6 +1731,8 @@ open_process_map (int pid, const char *mode)
 }
 #endif
 
+static char *get_process_name_from_proc (pid_t pid);
+
 gboolean EnumProcessModules (gpointer process, gpointer *modules,
                             guint32 size, guint32 *needed)
 {
@@ -1755,6 +1761,7 @@ gboolean EnumProcessModules (gpointer process, gpointer *modules,
 
        if (WAPI_IS_PSEUDO_PROCESS_HANDLE (process)) {
                pid = WAPI_HANDLE_TO_PID (process);
+               proc_name = get_process_name_from_proc (pid);
        } else {
                process_handle = lookup_process_handle (process);
                if (!process_handle) {
@@ -1879,7 +1886,31 @@ get_process_name_from_proc (pid_t pid)
 
        free(pi);
 #endif
-#elif defined(__OpenBSD__) || defined(__FreeBSD__)
+#elif 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) {
+               DEBUG ("%s: sysctl() failed: %d", __func__, errno);
+               return(ret);
+       }
+
+       if ((pi = malloc(size)) == NULL)
+               return(ret);
+
+       if (sysctl (mib, 4, pi, &size, NULL, 0) < 0) {
+               if (errno == ENOMEM) {
+                       free(pi);
+                       DEBUG ("%s: Didn't allocate enough memory for kproc info", __func__);
+               }
+               return(ret);
+       }
+
+       if (strlen (pi->ki_comm) > 0)
+               ret = g_strdup (pi->ki_comm);
+       free(pi);
+#elif defined(__OpenBSD__)
        mib [0] = CTL_KERN;
        mib [1] = KERN_PROC;
        mib [2] = KERN_PROC_PID;
@@ -1888,8 +1919,10 @@ get_process_name_from_proc (pid_t pid)
        mib [5] = 0;
 
 retry:
-       if (sysctl(mib, 6, NULL, &size, NULL, 0) < 0)
+       if (sysctl(mib, 6, NULL, &size, NULL, 0) < 0) {
+               DEBUG ("%s: sysctl() failed: %d", __func__, errno);
                return(ret);
+       }
 
        if ((pi = malloc(size)) == NULL)
                return(ret);
@@ -2282,6 +2315,7 @@ SetProcessWorkingSetSize (gpointer process, size_t min, size_t max)
 gboolean
 TerminateProcess (gpointer process, gint32 exitCode)
 {
+#if defined(HAVE_KILL)
        WapiHandle_process *process_handle;
        int signo;
        int ret;
@@ -2319,6 +2353,10 @@ TerminateProcess (gpointer process, gint32 exitCode)
        }
        
        return (ret == 0);
+#else
+       g_error ("kill() is not supported by this platform");
+       return FALSE;
+#endif
 }
 
 guint32