[xbuild] ImportCollections.TryGetImport and other fixes
[mono.git] / mono / io-layer / processes.c
index c71c458dea74fc52521bbe7a7a05f42a5c06c248..d0501a7dbe0f9638683bdc972f12147a0acaa75f 100644 (file)
 #include <sys/resource.h>
 #endif
 
+#if defined(PLATFORM_MACOSX) || defined(__OpenBSD__)
+#include <sys/proc.h>
+#include <sys/sysctl.h>
+#  if !defined(__OpenBSD__)
+#    include <sys/utsname.h>
+#  endif
+#endif
+
 #ifdef PLATFORM_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
 #endif
 #endif
 
+#ifdef __HAIKU__
+#include <KernelKit.h>
+#endif
+
 #include <mono/io-layer/wapi.h>
 #include <mono/io-layer/wapi-private.h>
 #include <mono/io-layer/handles-private.h>
 #include <mono/io-layer/process-private.h>
 #include <mono/io-layer/threads.h>
 #include <mono/utils/strenc.h>
+#include <mono/utils/mono-path.h>
 #include <mono/io-layer/timefuncs-private.h>
 
 /* The process' environment strings */
-#ifdef __APPLE__
+#if defined(__APPLE__) && !defined (__arm__)
 /* Apple defines this in crt_externs.h but doesn't provide that header for 
  * arm-apple-darwin9.  We'll manually define the symbol on Apple as it does
  * in fact exist on all implementations (so far) 
@@ -71,7 +84,11 @@ extern char **environ;
 #undef DEBUG
 
 static guint32 process_wait (gpointer handle, guint32 timeout);
-FILE *open_process_map (int pid, const char *mode);
+
+#if !defined(__OpenBSD__)
+static FILE *
+open_process_map (int pid, const char *mode);
+#endif
 
 struct _WapiHandleOps _wapi_process_ops = {
        NULL,                           /* close_shared */
@@ -407,7 +424,6 @@ utf16_concat (const gunichar2 *first, ...)
 }
 
 #ifdef PLATFORM_MACOSX
-#include <sys/utsname.h>
 
 /* 0 = no detection; -1 = not 10.5 or higher;  1 = 10.5 or higher */
 static int osx_10_5_or_higher;
@@ -448,6 +464,19 @@ static const gunichar2 *utf16_space = utf16_space_bytes;
 static const gunichar2 utf16_quote_bytes [2] = { 0x22, 0 };
 static const gunichar2 *utf16_quote = utf16_quote_bytes;
 
+#ifdef DEBUG
+/* Useful in gdb */
+void
+print_utf16 (gunichar2 *str)
+{
+       gchar *res;
+
+       res = g_utf16_to_utf8 (str, -1, NULL, NULL, NULL);
+       g_print ("%s\n", res);
+       g_free (res);
+}
+#endif
+
 /* Implemented as just a wrapper around CreateProcess () */
 gboolean ShellExecuteEx (WapiShellExecuteInfo *sei)
 {
@@ -473,7 +502,7 @@ gboolean ShellExecuteEx (WapiShellExecuteInfo *sei)
         * into and back out of utf8 is because there is no
         * g_strdup_printf () equivalent for gunichar2 :-(
         */
-       args = utf16_concat (sei->lpFile, sei->lpParameters == NULL ? NULL : utf16_space, sei->lpParameters, NULL);
+       args = utf16_concat (utf16_quote, sei->lpFile, utf16_quote, sei->lpParameters == NULL ? NULL : utf16_space, sei->lpParameters, NULL);
        if (args == NULL){
                SetLastError (ERROR_INVALID_DATA);
                return (FALSE);
@@ -845,6 +874,7 @@ gboolean CreateProcess (const gunichar2 *appname, const gunichar2 *cmdline,
        } else {
                gchar *token = NULL;
                char quote;
+               gint token_len;
                
                /* Dig out the first token from args, taking quotation
                 * marks into account
@@ -865,10 +895,10 @@ gboolean CreateProcess (const gunichar2 *appname, const gunichar2 *cmdline,
                if (args[0] == '\"' || args [0] == '\'') {
                        quote = args [0];
                        for (i = 1; args[i] != '\0' && args[i] != quote; i++);
-                       if (g_ascii_isspace (args[i+1])) {
+                       if (args [i + 1] == '\0' || g_ascii_isspace (args[i+1])) {
                                /* We found the first token */
                                token = g_strndup (args+1, i-1);
-                               args_after_prog = args + i;
+                               args_after_prog = g_strchug (args + i + 1);
                        } else {
                                /* Quotation mark appeared in the
                                 * middle of the token.  Just give the
@@ -908,7 +938,8 @@ gboolean CreateProcess (const gunichar2 *appname, const gunichar2 *cmdline,
                /* Turn all the slashes round the right way. Only for
                 * the prg. name
                 */
-               for (i = 0; i < strlen (token); i++) {
+               token_len = strlen (token);
+               for (i = 0; i < token_len; i++) {
                        if (token[i] == '\\') {
                                token[i] = '/';
                        }
@@ -1025,8 +1056,10 @@ gboolean CreateProcess (const gunichar2 *appname, const gunichar2 *cmdline,
 
        ret = g_shell_parse_argv (full_prog, NULL, &argv, &gerr);
        if (ret == FALSE) {
-               /* FIXME: Could do something with the GError here
-                */
+               g_message ("CreateProcess: %s\n", gerr->message);
+               g_error_free (gerr);
+               gerr = NULL;
+               goto free_strings;
        }
 
        if (startup != NULL && startup->dwFlags & STARTF_USESTDHANDLES) {
@@ -1488,10 +1521,111 @@ static gboolean process_enum (gpointer handle, gpointer user_data)
 }
 #endif /* UNUSED_CODE */
 
+#if defined(PLATFORM_MACOSX) || defined(__OpenBSD__)
+
+gboolean EnumProcesses (guint32 *pids, guint32 len, guint32 *needed)
+{
+       guint32 count, fit, i, j;
+       gint32 err;
+       gboolean done;
+       size_t proclength, size;
+#if defined(__OpenBSD__)
+       struct kinfo_proc2 *result;
+       int name[6];
+       name[0] = CTL_KERN;
+       name[1] = KERN_PROC2;
+       name[2] = KERN_PROC_ALL;
+       name[3] = 0;
+       name[4] = sizeof(struct kinfo_proc2);
+       name[5] = 0;
+#else
+       struct kinfo_proc *result;
+       static const int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
+#endif
+
+       mono_once (&process_current_once, process_set_current);
+
+       result = NULL;
+       done = FALSE;
+
+       do {
+               proclength = 0;
+#if defined(__OpenBSD__)
+               size = (sizeof(name) / sizeof(*name));
+#else
+               size = (sizeof(name) / sizeof(*name)) - 1;
+#endif
+               err = sysctl ((int *)name, size, NULL, &proclength, NULL, 0);
+
+               if (err == 0) {
+                       result = malloc (proclength);
+
+                       if (result == NULL)
+                               return FALSE;
+
+#if defined(__OpenBSD__)
+                       name[5] = (int)(proclength / sizeof(struct kinfo_proc2));
+#endif
+
+                       err = sysctl ((int *) name, size, result, &proclength, NULL, 0);
+
+                       if (err == 0) 
+                               done = TRUE;
+                       else
+                               free (result);
+               }
+       } while (err == 0 && !done);
+       
+       if (err != 0) {
+               if (result != NULL) {
+                       free (result);
+                       result = NULL;
+               }
+               return(FALSE);
+       }       
+
+#if defined(__OpenBSD__)
+       count = proclength / sizeof(struct kinfo_proc2);
+#else
+       count = proclength / sizeof(struct kinfo_proc);
+#endif
+       fit = len / sizeof(guint32);
+       for (i = 0, j = 0; j< fit && i < count; i++) {
+#if defined(__OpenBSD__)
+               pids [j++] = result [i].p_pid;
+#else
+               if (result[i].kp_proc.p_pid > 0) /* Pid 0 not supported */
+                       pids [j++] = result [i].kp_proc.p_pid;
+#endif
+       }
+       free (result);
+       result = NULL;
+       *needed = j * sizeof(guint32);
+       
+       return(TRUE);
+}
+#elif defined(__HAIKU__)
+
 gboolean EnumProcesses (guint32 *pids, guint32 len, guint32 *needed)
 {
-       GArray *processes = g_array_new (FALSE, FALSE, sizeof(pid_t));
-       guint32 fit, i, j;
+       guint32 fit, i = 0;
+       int32 cookie = 0;
+       team_info teamInfo;
+
+       mono_once (&process_current_once, process_set_current);
+
+       fit = len / sizeof (guint32);
+       while (get_next_team_info (&cookie, &teamInfo) == B_OK && i < fit) {
+               pids [i++] = teamInfo.team;
+       }
+       *needed = i * sizeof (guint32);
+
+       return TRUE;
+}
+#else
+gboolean EnumProcesses (guint32 *pids, guint32 len, guint32 *needed)
+{
+       guint32 fit, i;
        DIR *dir;
        struct dirent *entry;
        
@@ -1501,32 +1635,26 @@ gboolean EnumProcesses (guint32 *pids, guint32 len, guint32 *needed)
        if (dir == NULL) {
                return(FALSE);
        }
-       while((entry = readdir (dir)) != NULL) {
-               if (isdigit (entry->d_name[0])) {
-                       char *endptr;
-                       pid_t pid = (pid_t)strtol (entry->d_name, &endptr, 10);
 
-                       if (*endptr == '\0') {
-                               /* Name was entirely numeric, so was a
-                                * process ID
-                                */
-                               g_array_append_val (processes, pid);
-                       }
-               }
-       }
-       closedir (dir);
+       i = 0;
+       fit = len / sizeof (guint32);
+       while(i < fit && (entry = readdir (dir)) != NULL) {
+               pid_t pid;
+               char *endptr;
 
-       fit=len/sizeof(guint32);
-       for (i = 0, j = 0; j < fit && i < processes->len; i++) {
-               pids[j++] = g_array_index (processes, pid_t, i);
-       }
+               if (!isdigit (entry->d_name[0]))
+                       continue;
 
-       g_array_free (processes, TRUE);
-       
-       *needed = j * sizeof(guint32);
+               pid = (pid_t) strtol (entry->d_name, &endptr, 10);
+               if (*endptr == '\0')
+                       pids [i++] = (guint32) pid;
+       }
+       closedir (dir);
+       *needed = i * sizeof(guint32);
        
        return(TRUE);
 }
+#endif
 
 static gboolean process_open_compare (gpointer handle, gpointer user_data)
 {
@@ -1555,10 +1683,20 @@ static gboolean process_open_compare (gpointer handle, gpointer user_data)
        }
 }
 
+gboolean CloseProcess(gpointer handle)
+{
+       if ((GPOINTER_TO_UINT (handle) & _WAPI_PROCESS_UNHANDLED) == _WAPI_PROCESS_UNHANDLED) {
+               /* This is a pseudo handle */
+               return(TRUE);
+       }
+
+       return CloseHandle (handle);
+}
+
 gpointer OpenProcess (guint32 req_access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, guint32 pid)
 {
        /* Find the process handle that corresponds to pid */
-       gpointer handle;
+       gpointer handle = NULL;
        
        mono_once (&process_current_once, process_set_current);
 
@@ -1570,8 +1708,15 @@ gpointer OpenProcess (guint32 req_access G_GNUC_UNUSED, gboolean inherit G_GNUC_
                                      process_open_compare,
                                      GUINT_TO_POINTER (pid), NULL, TRUE);
        if (handle == 0) {
+#if defined(PLATFORM_MACOSX) || defined(__OpenBSD__)
+               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) {
+#else
                gchar *dir = g_strdup_printf ("/proc/%d", pid);
                if (!access (dir, F_OK)) {
+#endif
                        /* Return a pseudo handle for processes we
                         * don't have handles for
                         */
@@ -1596,6 +1741,7 @@ gboolean GetExitCodeProcess (gpointer process, guint32 *code)
 {
        struct _WapiHandle_process *process_handle;
        gboolean ok;
+       guint32 pid = -1;
        
        mono_once (&process_current_once, process_set_current);
 
@@ -1603,13 +1749,27 @@ gboolean GetExitCodeProcess (gpointer process, guint32 *code)
                return(FALSE);
        }
        
+       pid = GPOINTER_TO_UINT (process) - _WAPI_PROCESS_UNHANDLED;
        if ((GPOINTER_TO_UINT (process) & _WAPI_PROCESS_UNHANDLED) == _WAPI_PROCESS_UNHANDLED) {
                /* This is a pseudo handle, so we don't know what the
-                * exit code was
+                * exit code was, but we can check whether it's alive or not
                 */
-               return(FALSE);
+#if defined(PLATFORM_MACOSX) || defined(__OpenBSD__)
+               if ((kill(pid, 0) == 0) || (errno == EPERM)) {
+#elif defined(__HAIKU__)
+               team_info teamInfo;
+               if (get_team_info ((team_id)pid, &teamInfo) == B_OK) {
+#else
+               gchar *dir = g_strdup_printf ("/proc/%d", pid);
+               if (!access (dir, F_OK)) {
+#endif
+                       *code = STILL_ACTIVE;
+                       return TRUE;
+               } else {
+                       return FALSE;
+               }
        }
-       
+
        ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
                                (gpointer *)&process_handle);
        if(ok==FALSE) {
@@ -1749,15 +1909,24 @@ static GSList *load_modules (void)
        int i = 0;
 
        for (i = 0; i < count; i++) {
+#if SIZEOF_VOID_P == 8
+               const struct mach_header_64 *hdr;
+               const struct section_64 *sec;
+#else
                const struct mach_header *hdr;
                const struct section *sec;
+#endif
                const char *name;
                intptr_t slide;
 
                slide = _dyld_get_image_vmaddr_slide (i);
                name = _dyld_get_image_name (i);
                hdr = _dyld_get_image_header (i);
+#if SIZEOF_VOID_P == 8
+               sec = getsectbynamefromheader_64 (hdr, SEG_DATA, SECT_DATA);
+#else
                sec = getsectbynamefromheader (hdr, SEG_DATA, SECT_DATA);
+#endif
 
                /* Some dynlibs do not have data sections on osx (#533893) */
                if (sec == 0) {
@@ -1784,6 +1953,98 @@ static GSList *load_modules (void)
        
        return(ret);
 }
+#elif defined(__OpenBSD__)
+#include <link.h>
+static int load_modules_callback (struct dl_phdr_info *info, size_t size, void *ptr)
+{
+       if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
+           + sizeof (info->dlpi_phnum))
+               return (-1);
+
+       struct dl_phdr_info *cpy = calloc(1, sizeof(struct dl_phdr_info));
+       if (!cpy)
+               return (-1);
+
+       memcpy(cpy, info, sizeof(*info));
+
+       g_ptr_array_add ((GPtrArray *)ptr, cpy);
+
+       return (0);
+}
+
+static GSList *load_modules (void)
+{
+       GSList *ret = NULL;
+       WapiProcModule *mod;
+       GPtrArray *dlarray = g_ptr_array_new();
+       int i;
+
+       if (dl_iterate_phdr(load_modules_callback, dlarray) < 0)
+               return (ret);
+
+       for (i = 0; i < dlarray->len; i++) {
+               struct dl_phdr_info *info = g_ptr_array_index (dlarray, i);
+
+               mod = g_new0 (WapiProcModule, 1);
+               mod->address_start = (gpointer)(info->dlpi_addr + info->dlpi_phdr[0].p_vaddr);
+               mod->address_end = (gpointer)(info->dlpi_addr +
+                                       info->dlpi_phdr[info->dlpi_phnum - 1].p_vaddr);
+               mod->perms = g_strdup ("r--p");
+               mod->address_offset = 0;
+               mod->inode = (ino_t) i;
+               mod->filename = g_strdup (info->dlpi_name); 
+
+#ifdef DEBUG
+               g_message ("%s: inode=%d, filename=%s, address_start=%p, address_end=%p", __func__,
+                                  mod->inode, mod->filename, mod->address_start, mod->address_end);
+#endif
+
+               free(info);
+
+               if (g_slist_find_custom (ret, mod, find_procmodule) == NULL) {
+                       ret = g_slist_prepend (ret, mod);
+               } else {
+                       free_procmodule (mod);
+               }
+       }
+
+       g_ptr_array_free (dlarray, TRUE);
+
+       ret = g_slist_reverse (ret);
+
+       return(ret);
+}
+#elif defined(__HAIKU__)
+
+static GSList *load_modules (void)
+{
+       GSList *ret = NULL;
+       WapiProcModule *mod;
+       int32 cookie = 0;
+       image_info imageInfo;
+
+       while (get_next_image_info (B_CURRENT_TEAM, &cookie, &imageInfo) == B_OK) {
+               mod = g_new0 (WapiProcModule, 1);
+               mod->device = imageInfo.device;
+               mod->inode = imageInfo.node;
+               mod->filename = g_strdup (imageInfo.name);
+               mod->address_start = MIN (imageInfo.text, imageInfo.data);
+               mod->address_end = MAX ((uint8_t*)imageInfo.text + imageInfo.text_size,
+                       (uint8_t*)imageInfo.data + imageInfo.data_size);
+               mod->perms = g_strdup ("r--p");
+               mod->address_offset = 0;
+
+               if (g_slist_find_custom (ret, mod, find_procmodule) == NULL) {
+                       ret = g_slist_prepend (ret, mod);
+               } else {
+                       free_procmodule (mod);
+               }
+       }
+
+       ret = g_slist_reverse (ret);
+
+       return ret;
+}
 #else
 static GSList *load_modules (FILE *fp)
 {
@@ -1908,24 +2169,35 @@ static GSList *load_modules (FILE *fp)
 static gboolean match_procname_to_modulename (gchar *procname, gchar *modulename)
 {
        char* lastsep = NULL;
+       char* pname = NULL;
+       char* mname = NULL;
+       gboolean result = FALSE;
 
        if (procname == NULL || modulename == NULL)
                return (FALSE);
 
-       if (!strcmp (procname, modulename))
-               return (TRUE);
+       pname = mono_path_resolve_symlinks (procname);
+       mname = mono_path_resolve_symlinks (modulename);
 
-       lastsep = strrchr (modulename, '/');
-       if (lastsep) {
-               if (!strcmp (lastsep+1, procname))
-                       return (TRUE);
-               return (FALSE);
+       if (!strcmp (pname, mname))
+               result = TRUE;
+
+       if (!result) {
+               lastsep = strrchr (mname, '/');
+               if (lastsep)
+                       if (!strcmp (lastsep+1, pname))
+                               result = TRUE;
        }
 
-       return (FALSE);
+       g_free (pname);
+       g_free (mname);
+
+       return result;
 }
 
-FILE *open_process_map (int pid, const char *mode)
+#if !defined(__OpenBSD__)
+static FILE *
+open_process_map (int pid, const char *mode)
 {
        FILE *fp = NULL;
        const gchar *proc_path[] = {
@@ -1944,13 +2216,16 @@ FILE *open_process_map (int pid, const char *mode)
 
        return fp;
 }
+#endif
 
 gboolean EnumProcessModules (gpointer process, gpointer *modules,
                             guint32 size, guint32 *needed)
 {
        struct _WapiHandle_process *process_handle;
        gboolean ok;
+#if !defined(__OpenBSD__) && !defined(PLATFORM_MACOSX)
        FILE *fp;
+#endif
        GSList *mods = NULL;
        WapiProcModule *module;
        guint32 count, avail = size / sizeof(gpointer);
@@ -1988,7 +2263,7 @@ gboolean EnumProcessModules (gpointer process, gpointer *modules,
                proc_name = process_handle->proc_name;
        }
        
-#ifdef PLATFORM_MACOSX
+#if defined(PLATFORM_MACOSX) || defined(__OpenBSD__) || defined(__HAIKU__)
        {
                mods = load_modules ();
 #else
@@ -2036,13 +2311,107 @@ gboolean EnumProcessModules (gpointer process, gpointer *modules,
 
 static gchar *get_process_name_from_proc (pid_t pid)
 {
+#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 *ret = NULL;
+#endif
        gchar buf[256];
-       FILE *fp;
-       
+       gchar *ret = NULL;
+
+#if defined(PLATFORM_SOLARIS)
+       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(PLATFORM_MACOSX)
+#if (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5) && !defined (__mono_ppc__) && !defined(__arm__)
+       /* No proc name on OSX < 10.5 nor ppc nor iOS */
+       memset (buf, '\0', sizeof(buf));
+       proc_name (pid, buf, sizeof(buf));
+       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;
+       struct kinfo_proc2 *pi;
+
+       mib [0] = CTL_KERN;
+       mib [1] = KERN_PROC2;
+       mib [2] = KERN_PROC_PID;
+       mib [3] = pid;
+       mib [4] = sizeof(struct kinfo_proc2);
+       mib [5] = 0;
+
+retry:
+       if (sysctl(mib, 6, NULL, &size, NULL, 0) < 0)
+               return(ret);
+
+       if ((pi = malloc(size)) == NULL)
+               return(ret);
+
+       mib[5] = (int)(size / sizeof(struct kinfo_proc2));
+
+       if ((sysctl (mib, 6, pi, &size, NULL, 0) < 0) ||
+               (size != sizeof (struct kinfo_proc2))) {
+               if (errno == ENOMEM) {
+                       free(pi);
+                       goto retry;
+               }
+               return(ret);
+       }
+
+       if (strlen (pi->p_comm) > 0)
+               ret = g_strdup (pi->p_comm);
+
+       free(pi);
+#elif defined(__HAIKU__)
+       image_info imageInfo;
+       int32 cookie = 0;
+
+       if (get_next_image_info ((team_id)pid, &cookie, &imageInfo) == B_OK) {
+               ret = g_strdup (imageInfo.name);
+       }
+#else
        memset (buf, '\0', sizeof(buf));
-       
        filename = g_strdup_printf ("/proc/%d/exe", pid);
        if (readlink (filename, buf, 255) > 0) {
                ret = g_strdup (buf);
@@ -2052,7 +2421,7 @@ static gchar *get_process_name_from_proc (pid_t pid)
        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) {
@@ -2086,32 +2455,9 @@ static gchar *get_process_name_from_proc (pid_t pid)
                fclose (fp);
        }
        g_free (filename);
-
-       if (ret != NULL) {
-               return(ret);
-       }
-
-#ifdef PLATFORM_SOLARIS
-       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);
 #endif
 
-       if (ret != NULL) {
-               return(ret);
-       }
-
-       return(NULL);
+       return ret;
 }
 
 static guint32 get_module_name (gpointer process, gpointer module,
@@ -2125,7 +2471,9 @@ static guint32 get_module_name (gpointer process, gpointer module,
        gchar *procname_ext = NULL;
        glong len;
        gsize bytes;
+#if !defined(__OpenBSD__) && !defined(PLATFORM_MACOSX)
        FILE *fp;
+#endif
        GSList *mods = NULL;
        WapiProcModule *found_module;
        guint32 count;
@@ -2165,7 +2513,7 @@ static guint32 get_module_name (gpointer process, gpointer module,
        }
 
        /* Look up the address in /proc/<pid>/maps */
-#ifdef PLATFORM_MACOSX
+#if defined(PLATFORM_MACOSX) || defined(__OpenBSD__) || defined(__HAIKU__)
        {
                mods = load_modules ();
 #else
@@ -2277,7 +2625,9 @@ gboolean GetModuleInformation (gpointer process, gpointer module,
        struct _WapiHandle_process *process_handle;
        gboolean ok;
        pid_t pid;
+#if !defined(__OpenBSD__) && !defined(PLATFORM_MACOSX)
        FILE *fp;
+#endif
        GSList *mods = NULL;
        WapiProcModule *found_module;
        guint32 count;
@@ -2315,7 +2665,7 @@ gboolean GetModuleInformation (gpointer process, gpointer module,
                proc_name = g_strdup (process_handle->proc_name);
        }
 
-#ifdef PLATFORM_MACOSX
+#if defined(PLATFORM_MACOSX) || defined(__OpenBSD__) || defined(__HAIKU__)
        {
                mods = load_modules ();
 #else