[Fix] Fix process scanning on OSX.
authorTak <levi@unity3d.com>
Fri, 22 Oct 2010 10:51:56 +0000 (12:51 +0200)
committerLevi Bard <levi@unity3d.com>
Mon, 31 Jan 2011 13:05:55 +0000 (14:05 +0100)
* io-layer/process-private.h: Generalize PID masking.
* io-layer/processes.c: Fix process scanning on OSX.

License: MIT/X11

mono/io-layer/process-private.h
mono/io-layer/processes.c

index 8d1c80abde325a2b154f8ef1207ae3893b39fffb..fc955fbb56ee548e140619f499fec1a6ce36b390 100644 (file)
@@ -17,9 +17,8 @@
 #define _WAPI_PROCESS_CURRENT (gpointer)0xFFFFFFFF
 
 /* This marks a system process that we don't have a handle on */
-/* FIXME: cope with pids > 31bit? */
-#define _WAPI_PROCESS_UNHANDLED_PID_MASK 0x7FFFFFFF
-#define _WAPI_PROCESS_UNHANDLED (-1 & ~_WAPI_PROCESS_UNHANDLED_PID_MASK)
+#define _WAPI_PROCESS_UNHANDLED (1 << (8*sizeof(pid_t)-1))
+#define _WAPI_PROCESS_UNHANDLED_PID_MASK (-1 & ~_WAPI_PROCESS_UNHANDLED)
 
 extern gpointer _wapi_process_duplicate (void);
 
index 33037f1a69ffc736eee64dedd67af7d818f6400a..f1d8eb0e4c772f7bcc5d46cc20a435600c5c5ad6 100644 (file)
@@ -1590,11 +1590,12 @@ gboolean EnumProcesses (guint32 *pids, guint32 len, guint32 *needed)
        count = proclength / sizeof(struct kinfo_proc);
 #endif
        fit = len / sizeof(guint32);
-       for (i = 0, j = 0; j< fit && i < count; i++) {
+       for (i = 0, j = 0; j < fit && i < count; i++) {
 #if defined(__OpenBSD__)
                pids [j++] = result [i].p_pid;
 #else
-               pids [j++] = result [i].kp_proc.p_pid;
+               if (result[i].kp_proc.p_pid > 0) /* Pid 0 not supported */
+                       pids [j++] = result [i].kp_proc.p_pid;
 #endif
        }
        free (result);
@@ -1708,7 +1709,7 @@ gpointer OpenProcess (guint32 req_access G_GNUC_UNUSED, gboolean inherit G_GNUC_
                                      GUINT_TO_POINTER (pid), NULL, TRUE);
        if (handle == 0) {
 #if defined(PLATFORM_MACOSX) || defined(__OpenBSD__)
-               if ((kill(pid, 0) == 0) || (errno == EPERM)) {
+               if (((kill(pid, 0) == 0) || (errno == EPERM)) && pid != 0) {
 #elif defined(__HAIKU__)
                team_info teamInfo;
                if (get_team_info ((team_id)pid, &teamInfo) == B_OK) {
@@ -2222,7 +2223,7 @@ gboolean EnumProcessModules (gpointer process, gpointer *modules,
 {
        struct _WapiHandle_process *process_handle;
        gboolean ok;
-#if !defined(__OpenBSD__)
+#if !defined(__OpenBSD__) && !defined(PLATFORM_MACOSX)
        FILE *fp;
 #endif
        GSList *mods = NULL;
@@ -2310,7 +2311,15 @@ gboolean EnumProcessModules (gpointer process, gpointer *modules,
 
 static gchar *get_process_name_from_proc (pid_t pid)
 {
-#if !defined(__OpenBSD__)
+#ifdef __OpenBSD__
+       int mib [6];
+       size_t size;
+       struct kinfo_proc2 *pi;
+#elif defined(PLATFORM_MACOSX)
+       size_t size;
+       struct kinfo_proc *pi;
+       int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid };
+#else
        FILE *fp;
        gchar *filename = NULL;
        gchar buf[256];
@@ -2332,12 +2341,34 @@ static gchar *get_process_name_from_proc (pid_t pid)
        }
        g_free (filename);
 #elif defined(PLATFORM_MACOSX)
+#if (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5) && !defined(__arm__)
+       /* No proc name on OSX < 10.5 nor iOS */
        memset (buf, '\0', sizeof(buf));
-#  if !defined (__mono_ppc__) && !defined(__arm__)
        proc_name (pid, buf, sizeof(buf));
-#  endif
        if (strlen (buf) > 0)
                ret = g_strdup (buf);
+#else
+       if (sysctl(mib, 4, NULL, &size, NULL, 0) < 0)
+               return(ret);
+
+       if ((pi = malloc(size)) == NULL)
+               return(ret);
+
+       if (sysctl (mib, 4, pi, &size, NULL, 0) < 0) {
+               if (errno == ENOMEM) {
+                       free(pi);
+#ifdef DEBUG
+                       g_message ("%s: Didn't allocate enough memory for kproc info", __func__);
+#endif
+               }
+               return(ret);
+       }
+
+       if (strlen (pi->kp_proc.p_comm) > 0)
+               ret = g_strdup (pi->kp_proc.p_comm);
+
+       free(pi);
+#endif
 #elif defined(__OpenBSD__)
        int mib [6];
        size_t size;
@@ -2426,11 +2457,7 @@ retry:
        g_free (filename);
 #endif
 
-       if (ret != NULL) {
-               return(ret);
-       }
-
-       return(NULL);
+       return ret;
 }
 
 static guint32 get_module_name (gpointer process, gpointer module,
@@ -2444,7 +2471,7 @@ static guint32 get_module_name (gpointer process, gpointer module,
        gchar *procname_ext = NULL;
        glong len;
        gsize bytes;
-#if !defined(__OpenBSD__)
+#if !defined(__OpenBSD__) && !defined(PLATFORM_MACOSX)
        FILE *fp;
 #endif
        GSList *mods = NULL;
@@ -2598,7 +2625,7 @@ gboolean GetModuleInformation (gpointer process, gpointer module,
        struct _WapiHandle_process *process_handle;
        gboolean ok;
        pid_t pid;
-#if !defined(__OpenBSD__)
+#if !defined(__OpenBSD__) && !defined(PLATFORM_MACOSX)
        FILE *fp;
 #endif
        GSList *mods = NULL;