Merge pull request #1923 from lukaszunity/start-managed-process-fix
[mono.git] / mono / io-layer / processes.c
index ce7aee14c884732d669200fc8bcbc3a2599d718b..5f0805878cf7173c6c492460d6254159a2c3e859 100644 (file)
 #endif
 #endif
 
-#if defined(PLATFORM_MACOSX) || defined(__OpenBSD__)
+#if defined(PLATFORM_MACOSX) || defined(__OpenBSD__) || defined(__FreeBSD__)
 #include <sys/proc.h>
 #include <sys/sysctl.h>
 #  if !defined(__OpenBSD__)
 #    include <sys/utsname.h>
 #  endif
+#  if defined(__FreeBSD__)
+#    include <sys/user.h>  /* struct kinfo_proc */
+#  endif
 #endif
 
 #ifdef PLATFORM_SOLARIS
@@ -115,7 +118,7 @@ static guint32 process_wait (gpointer handle, guint32 timeout, gboolean alertabl
 static void process_close (gpointer handle, gpointer data);
 static gboolean is_pid_valid (pid_t pid);
 
-#if !(defined(PLATFORM_MACOSX) || defined(__OpenBSD__) || defined(__HAIKU__))
+#if !(defined(PLATFORM_MACOSX) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__HAIKU__))
 static FILE *
 open_process_map (int pid, const char *mode);
 #endif
@@ -175,7 +178,9 @@ is_pid_valid (pid_t pid)
 {
        gboolean result = FALSE;
 
-#if defined(PLATFORM_MACOSX) || defined(__OpenBSD__)
+#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__)
@@ -812,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);
                        }
                        
@@ -839,7 +844,6 @@ gboolean CreateProcess (const gunichar2 *appname, const gunichar2 *cmdline,
        } else {
                if (!is_executable (prog)) {
                        DEBUG ("%s: Executable permisson not set on %s", __func__, prog);
-                       g_free (prog);
                        SetLastError (ERROR_ACCESS_DENIED);
                        goto free_strings;
                }
@@ -1449,7 +1453,7 @@ static GSList *load_modules (void)
        
        return(ret);
 }
-#elif defined(__OpenBSD__)
+#elif defined(__OpenBSD__) || defined(__FreeBSD__)
 #include <link.h>
 static int load_modules_callback (struct dl_phdr_info *info, size_t size, void *ptr)
 {
@@ -1671,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);
 
@@ -1699,10 +1704,11 @@ 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;
 }
 
-#if !(defined(PLATFORM_MACOSX) || defined(__OpenBSD__) || defined(__HAIKU__))
+#if !(defined(PLATFORM_MACOSX) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__HAIKU__))
 static FILE *
 open_process_map (int pid, const char *mode)
 {
@@ -1725,11 +1731,13 @@ 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)
 {
        WapiHandle_process *process_handle;
-#if !defined(__OpenBSD__) && !defined(PLATFORM_MACOSX)
+#if !defined(__OpenBSD__) && !defined(PLATFORM_MACOSX) && !defined(__FreeBSD__)
        FILE *fp;
 #endif
        GSList *mods = NULL;
@@ -1753,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) {
@@ -1764,7 +1773,7 @@ gboolean EnumProcessModules (gpointer process, gpointer *modules,
                proc_name = process_handle->proc_name;
        }
        
-#if defined(PLATFORM_MACOSX) || defined(__OpenBSD__) || defined(__HAIKU__)
+#if defined(PLATFORM_MACOSX) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__HAIKU__)
        mods = load_modules ();
        if (!proc_name) {
                modules[0] = NULL;
@@ -1819,7 +1828,7 @@ gboolean EnumProcessModules (gpointer process, gpointer *modules,
 static char *
 get_process_name_from_proc (pid_t pid)
 {
-#if defined(__OpenBSD__)
+#if defined(__OpenBSD__) || defined(__FreeBSD__)
        int mib [6];
        size_t size;
        struct kinfo_proc *pi;
@@ -1877,6 +1886,30 @@ get_process_name_from_proc (pid_t pid)
 
        free(pi);
 #endif
+#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;
@@ -1886,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);
@@ -1903,8 +1938,13 @@ retry:
                return(ret);
        }
 
+#if defined(__OpenBSD__)
        if (strlen (pi->p_comm) > 0)
                ret = g_strdup (pi->p_comm);
+#elif defined(__FreeBSD__)
+       if (strlen (pi->ki_comm) > 0)
+               ret = g_strdup (pi->ki_comm);
+#endif
 
        free(pi);
 #elif defined(__HAIKU__)
@@ -2011,7 +2051,7 @@ get_module_name (gpointer process, gpointer module,
        char *procname_ext = NULL;
        glong len;
        gsize bytes;
-#if !defined(__OpenBSD__) && !defined(PLATFORM_MACOSX)
+#if !defined(__OpenBSD__) && !defined(PLATFORM_MACOSX) && !defined(__FreeBSD__)
        FILE *fp;
 #endif
        GSList *mods = NULL;
@@ -2045,7 +2085,7 @@ get_module_name (gpointer process, gpointer module,
        }
 
        /* Look up the address in /proc/<pid>/maps */
-#if defined(PLATFORM_MACOSX) || defined(__OpenBSD__) || defined(__HAIKU__)
+#if defined(PLATFORM_MACOSX) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__HAIKU__)
        mods = load_modules ();
 #else
        fp = open_process_map (pid, "r");
@@ -2151,7 +2191,7 @@ GetModuleInformation (gpointer process, gpointer module,
 {
        WapiHandle_process *process_handle;
        pid_t pid;
-#if !defined(__OpenBSD__) && !defined(PLATFORM_MACOSX)
+#if !defined(__OpenBSD__) && !defined(PLATFORM_MACOSX) && !defined(__FreeBSD__)
        FILE *fp;
 #endif
        GSList *mods = NULL;
@@ -2182,7 +2222,7 @@ GetModuleInformation (gpointer process, gpointer module,
                proc_name = g_strdup (process_handle->proc_name);
        }
 
-#if defined(PLATFORM_MACOSX) || defined(__OpenBSD__) || defined(__HAIKU__)
+#if defined(PLATFORM_MACOSX) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__HAIKU__)
        mods = load_modules ();
 #else
        /* Look up the address in /proc/<pid>/maps */
@@ -2275,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;
@@ -2312,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