fix macro syntax
[mono.git] / mono / io-layer / processes.c
index bd8d58ef59c1a9a03a7033c5e7aee8adbfc227b3..cc25304b2708667097150eab976200dc907c3789 100644 (file)
@@ -4,7 +4,7 @@
  * Author:
  *     Dick Porter (dick@ximian.com)
  *
- * (C) 2002 Ximian, Inc.
+ * (C) 2002-2006 Novell, Inc.
  */
 
 #include <config.h>
@@ -17,6 +17,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 #include <signal.h>
+#include <sys/wait.h>
 
 #include <mono/io-layer/wapi.h>
 #include <mono/io-layer/wapi-private.h>
 #include <mono/io-layer/process-private.h>
 #include <mono/io-layer/threads.h>
 #include <mono/utils/strenc.h>
+#include <mono/io-layer/timefuncs-private.h>
 
 /* The process' environment strings */
 extern char **environ;
 
 #undef DEBUG
 
-static void process_close_shared (gpointer handle);
+static guint32 process_wait (gpointer handle, guint32 timeout);
 
 struct _WapiHandleOps _wapi_process_ops = {
-       process_close_shared,           /* close_shared */
-       NULL,                           /* close_private */
+       NULL,                           /* close_shared */
        NULL,                           /* signal */
        NULL,                           /* own */
        NULL,                           /* is_owned */
+       process_wait,                   /* special_wait */
+       NULL                            /* prewait */   
 };
 
 static mono_once_t process_current_once=MONO_ONCE_INIT;
@@ -50,35 +53,270 @@ static mono_once_t process_ops_once=MONO_ONCE_INIT;
 static void process_ops_init (void)
 {
        _wapi_handle_register_capabilities (WAPI_HANDLE_PROCESS,
-                                           WAPI_HANDLE_CAP_WAIT);
+                                           WAPI_HANDLE_CAP_WAIT |
+                                           WAPI_HANDLE_CAP_SPECIAL_WAIT);
 }
 
-static void process_close_shared (gpointer handle G_GNUC_UNUSED)
+static gboolean process_set_termination_details (gpointer handle, int status)
 {
        struct _WapiHandle_process *process_handle;
        gboolean ok;
+       int thr_ret;
+       
+       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
+                                 (gpointer *)&process_handle);
+       if (ok == FALSE) {
+               g_warning ("%s: error looking up process handle %p",
+                          __func__, handle);
+               return(FALSE);
+       }
+       
+       thr_ret = _wapi_handle_lock_shared_handles ();
+       g_assert (thr_ret == 0);
 
-       ok=_wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
-                               (gpointer *)&process_handle, NULL);
-       if(ok==FALSE) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error looking up process handle %p", handle);
-               return;
+       if (WIFSIGNALED(status)) {
+               process_handle->exitstatus = 128 + WTERMSIG(status);
+       } else {
+               process_handle->exitstatus = WEXITSTATUS(status);
+       }
+       _wapi_time_t_to_filetime (time(NULL), &process_handle->exit_time);
+       
+       _wapi_shared_handle_set_signal_state (handle, TRUE);
+
+       _wapi_handle_unlock_shared_handles ();
+
+       return (ok);
+}
+
+/* See if any child processes have terminated and wait() for them,
+ * updating process handle info.  This function is called from the
+ * collection thread every few seconds.
+ */
+static gboolean waitfor_pid (gpointer test, gpointer user_data G_GNUC_UNUSED)
+{
+       struct _WapiHandle_process *process;
+       gboolean ok;
+       int status;
+       pid_t ret;
+       
+       if (_wapi_handle_issignalled (test)) {
+               /* We've already done this one */
+               return (FALSE);
+       }
+       
+       ok = _wapi_lookup_handle (test, WAPI_HANDLE_PROCESS,
+                                 (gpointer *)&process);
+       if (ok == FALSE) {
+               /* The handle must have been too old and was reaped */
+               return (FALSE);
+       }
+       
+       do {
+               ret = waitpid (process->id, &status, WNOHANG);
+       } while (errno == EINTR);
+       
+       if (ret <= 0) {
+               /* Process not ready for wait */
+#ifdef DEBUG
+               g_message ("%s: Process %d not ready for waiting for: %s",
+                          __func__, process->id, g_strerror (errno));
+#endif
+
+               return (FALSE);
+       }
+       
+#ifdef DEBUG
+       g_message ("%s: Process %d finished", __func__, ret);
+#endif
+
+       process_set_termination_details (test, status);
+       
+       /* return FALSE to keep searching */
+       return (FALSE);
+}
+
+void _wapi_process_reap (void)
+{
+#ifdef DEBUG
+       g_message ("%s: Reaping child processes", __func__);
+#endif
+
+       _wapi_search_handle (WAPI_HANDLE_PROCESS, waitfor_pid, NULL, NULL,
+                            FALSE);
+}
+
+/* Limitations: This can only wait for processes that are our own
+ * children.  Fixing this means resurrecting a daemon helper to manage
+ * processes.
+ */
+static guint32 process_wait (gpointer handle, guint32 timeout)
+{
+       struct _WapiHandle_process *process_handle;
+       gboolean ok;
+       pid_t pid, ret;
+       int status;
+       
+#ifdef DEBUG
+       g_message ("%s: Waiting for process %p", __func__, handle);
+#endif
+
+       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
+                                 (gpointer *)&process_handle);
+       if (ok == FALSE) {
+               g_warning ("%s: error looking up process handle %p", __func__,
+                          handle);
+               return(WAIT_FAILED);
+       }
+       
+       pid = process_handle->id;
+       
+#ifdef DEBUG
+       g_message ("%s: PID is %d", __func__, pid);
+#endif
+
+       if (timeout == INFINITE) {
+               while ((ret = waitpid (pid, &status, 0)) != pid) {
+                       if (ret == (pid_t)-1 && errno != EINTR) {
+                               return(WAIT_FAILED);
+                       }
+               }
+       } else if (timeout == 0) {
+               /* Just poll */
+               ret = waitpid (pid, &status, WNOHANG);
+               if (ret != pid) {
+                       return (WAIT_TIMEOUT);
+               }
+       } else {
+               /* Poll in a loop */
+               do {
+                       ret = waitpid (pid, &status, WNOHANG);
+                       if (ret == pid) {
+                               break;
+                       } else if (ret == (pid_t)-1 && errno != EINTR) {
+                               return(WAIT_FAILED);
+                       }
+
+                       _wapi_handle_spin (100);
+                       timeout -= 100;
+               } while (timeout > 0);
+
+               if (timeout <= 0) {
+                       return(WAIT_TIMEOUT);
+               }
        }
 
+       /* Process must have exited */
 #ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION
-                  ": closing process handle %p with id %d", handle,
-                  process_handle->id);
+       g_message ("%s: Wait done", __func__);
 #endif
 
-       if(process_handle->proc_name!=0) {
-               _wapi_handle_scratch_delete (process_handle->proc_name);
-               process_handle->proc_name=0;
+       ok = process_set_termination_details (handle, status);
+       if (ok == FALSE) {
+               SetLastError (ERROR_OUTOFMEMORY);
+               return (WAIT_FAILED);
+       }
+
+       return(WAIT_OBJECT_0);
+}
+
+void _wapi_process_signal_self ()
+{
+       if (current_process != NULL) {
+               process_set_termination_details (current_process, 0);
        }
 }
 
-gboolean CreateProcess (const gunichar2 *appname, gunichar2 *cmdline,
+       
+static void process_set_defaults (struct _WapiHandle_process *process_handle)
+{
+       /* These seem to be the defaults on w2k */
+       process_handle->min_working_set = 204800;
+       process_handle->max_working_set = 1413120;
+
+       _wapi_time_t_to_filetime (time (NULL), &process_handle->create_time);
+}
+
+/* Implemented as just a wrapper around CreateProcess () */
+gboolean ShellExecuteEx (WapiShellExecuteInfo *sei)
+{
+       gboolean ret;
+       WapiProcessInformation process_info;
+       gunichar2 *args;
+       gchar *u8file, *u8params, *u8args;
+       
+       if (sei == NULL) {
+               /* w2k just segfaults here, but we can do better than
+                * that
+                */
+               SetLastError (ERROR_INVALID_PARAMETER);
+               return (FALSE);
+       }
+
+       if (sei->lpFile == NULL) {
+               /* w2k returns TRUE for this, for some reason. */
+               return (TRUE);
+       }
+       
+       /* Put both executable and parameters into the second argument
+        * to CreateProcess (), so it searches $PATH.  The conversion
+        * into and back out of utf8 is because there is no
+        * g_strdup_printf () equivalent for gunichar2 :-(
+        */
+       u8file = g_utf16_to_utf8 (sei->lpFile, -1, NULL, NULL, NULL);
+       if (u8file == NULL) {
+               SetLastError (ERROR_INVALID_DATA);
+               return (FALSE);
+       }
+       
+       if (sei->lpParameters != NULL) {
+               u8params = g_utf16_to_utf8 (sei->lpParameters, -1, NULL, NULL, NULL);
+               if (u8params == NULL) {
+                       SetLastError (ERROR_INVALID_DATA);
+                       g_free (u8file);
+                       return (FALSE);
+               }
+       
+               u8args = g_strdup_printf ("%s %s", u8file, u8params);
+               if (u8args == NULL) {
+                       SetLastError (ERROR_INVALID_DATA);
+                       g_free (u8params);
+                       g_free (u8file);
+                       return (FALSE);
+               }
+       
+               args = g_utf8_to_utf16 (u8args, -1, NULL, NULL, NULL);
+       
+               g_free (u8file);
+               g_free (u8params);
+               g_free (u8args);
+       } else {
+               args = g_utf8_to_utf16 (u8file, -1, NULL, NULL, NULL);
+       }
+               
+       if (args == NULL) {
+               SetLastError (ERROR_INVALID_DATA);
+               return (FALSE);
+       }
+       
+       ret = CreateProcess (NULL, args, NULL, NULL, TRUE,
+                            CREATE_UNICODE_ENVIRONMENT, NULL,
+                            sei->lpDirectory, NULL, &process_info);
+       g_free (args);
+       
+       if (!ret) {
+               return (FALSE);
+       }
+       
+       if (sei->fMask & SEE_MASK_NOCLOSEPROCESS) {
+               sei->hProcess = process_info.hProcess;
+       } else {
+               CloseHandle (process_info.hProcess);
+       }
+       
+       return (ret);
+}
+
+gboolean CreateProcess (const gunichar2 *appname, const gunichar2 *cmdline,
                        WapiSecurityAttributes *process_attrs G_GNUC_UNUSED,
                        WapiSecurityAttributes *thread_attrs G_GNUC_UNUSED,
                        gboolean inherit_handles, guint32 create_flags,
@@ -86,13 +324,15 @@ gboolean CreateProcess (const gunichar2 *appname, gunichar2 *cmdline,
                        WapiStartupInfo *startup,
                        WapiProcessInformation *process_info)
 {
-       gchar *cmd=NULL, *prog = NULL, *full_prog = NULL, *args=NULL, *args_after_prog=NULL, *dir=NULL;
-       guint32 env=0, stored_dir=0, stored_prog=0, i;
-       gboolean ret=FALSE;
-       gpointer stdin_handle, stdout_handle, stderr_handle;
-       guint32 pid, tid;
-       gpointer process_handle, thread_handle;
-       struct _WapiHandle_process *process_handle_data;
+       gchar *cmd=NULL, *prog = NULL, *full_prog = NULL, *args = NULL, *args_after_prog = NULL, *dir = NULL, **env_strings = NULL, **argv;
+       guint32 i, env_count = 0;
+       gboolean ret = FALSE;
+       gpointer handle;
+       struct _WapiHandle_process process_handle = {0}, *process_handle_data;
+       GError *gerr = NULL;
+       int in_fd, out_fd, err_fd;
+       pid_t pid;
+       int thr_ret;
        
        mono_once (&process_ops_once, process_ops_init);
        
@@ -122,129 +362,82 @@ gboolean CreateProcess (const gunichar2 *appname, gunichar2 *cmdline,
         * beginning to understand just why windows apps are generally
         * so crap, with an API like this :-(
         */
-       if(appname!=NULL) {
-               cmd=mono_unicode_to_external (appname);
-               if(cmd==NULL) {
+       if (appname != NULL) {
+               cmd = mono_unicode_to_external (appname);
+               if (cmd == NULL) {
 #ifdef DEBUG
-                       g_message (G_GNUC_PRETTY_FUNCTION
-                                  ": unicode conversion returned NULL");
+                       g_message ("%s: unicode conversion returned NULL",
+                                  __func__);
 #endif
 
-                       SetLastError(ERROR_PATH_NOT_FOUND);
+                       SetLastError (ERROR_PATH_NOT_FOUND);
                        goto cleanup;
                }
 
                /* Turn all the slashes round the right way */
-               for(i=0; i<strlen (cmd); i++) {
-                       if(cmd[i]=='\\') {
-                               cmd[i]='/';
+               for (i = 0; i < strlen (cmd); i++) {
+                       if (cmd[i] == '\\') {
+                               cmd[i] = '/';
                        }
                }
        }
        
-       if(cmdline!=NULL) {
-               args=mono_unicode_to_external (cmdline);
-               if(args==NULL) {
+       if (cmdline != NULL) {
+               args = mono_unicode_to_external (cmdline);
+               if (args == NULL) {
 #ifdef DEBUG
-                       g_message (G_GNUC_PRETTY_FUNCTION
-                                  ": unicode conversion returned NULL");
+                       g_message ("%s: unicode conversion returned NULL", __func__);
 #endif
 
-                       SetLastError(ERROR_PATH_NOT_FOUND);
+                       SetLastError (ERROR_PATH_NOT_FOUND);
                        goto cleanup;
                }
        }
 
-       if(cwd!=NULL) {
-               dir=mono_unicode_to_external (cwd);
-               if(dir==NULL) {
+       if (cwd != NULL) {
+               dir = mono_unicode_to_external (cwd);
+               if (dir == NULL) {
 #ifdef DEBUG
-                       g_message (G_GNUC_PRETTY_FUNCTION
-                                  ": unicode conversion returned NULL");
+                       g_message ("%s: unicode conversion returned NULL", __func__);
 #endif
 
-                       SetLastError(ERROR_PATH_NOT_FOUND);
+                       SetLastError (ERROR_PATH_NOT_FOUND);
                        goto cleanup;
                }
 
                /* Turn all the slashes round the right way */
-               for(i=0; i<strlen (dir); i++) {
-                       if(dir[i]=='\\') {
-                               dir[i]='/';
+               for (i = 0; i < strlen (dir); i++) {
+                       if (dir[i] == '\\') {
+                               dir[i] = '/';
                        }
                }
        } else {
-               dir=g_get_current_dir ();
+               dir = g_get_current_dir ();
        }
-       stored_dir=_wapi_handle_scratch_store (dir, strlen (dir));
        
-       
-       /* new_environ is a block of NULL-terminated strings, which
-        * is itself NULL-terminated. Of course, passing an array of
-        * string pointers would have made things too easy :-(
-        *
-        * If new_environ is not NULL it specifies the entire set of
-        * environment variables in the new process.  Otherwise the
-        * new process inherits the same environment.
-        */
-       if(new_environ!=NULL) {
-               gchar **strings;
-               guint32 count=0;
-               gunichar2 *new_environp;
-
-               /* Count the number of strings */
-               for(new_environp=(gunichar2 *)new_environ; *new_environp;
-                   new_environp++) {
-                       count++;
-                       while(*new_environp) {
-                               new_environp++;
-                       }
-               }
-               strings=g_new0 (gchar *, count + 1); /* +1 -> last one is NULL */
-               
-               /* Copy each environ string into 'strings' turning it
-                * into utf8 (or the requested encoding) at the same
-                * time
-                */
-               count=0;
-               for(new_environp=(gunichar2 *)new_environ; *new_environp;
-                   new_environp++) {
-                       strings[count]=mono_unicode_to_external (new_environp);
-                       count++;
-                       while(*new_environp) {
-                               new_environp++;
-                       }
-               }
-
-               env=_wapi_handle_scratch_store_string_array (strings);
-
-               g_strfreev (strings);
-       } else {
-               /* Use the existing environment */
-               env=_wapi_handle_scratch_store_string_array (environ);
-       }
 
        /* We can't put off locating the executable any longer :-( */
-       if(cmd!=NULL) {
+       if (cmd != NULL) {
                gchar *unquoted;
-               if(g_ascii_isalpha (cmd[0]) && (cmd[1]==':')) {
+               if (g_ascii_isalpha (cmd[0]) && (cmd[1] == ':')) {
                        /* Strip off the drive letter.  I can't
                         * believe that CP/M holdover is still
                         * visible...
                         */
                        g_memmove (cmd, cmd+2, strlen (cmd)-2);
-                       cmd[strlen (cmd)-2]='\0';
+                       cmd[strlen (cmd)-2] = '\0';
                }
 
                unquoted = g_shell_unquote (cmd, NULL);
-               if(unquoted[0]=='/') {
+               if (unquoted[0] == '/') {
                        /* Assume full path given */
-                       prog=g_strdup (unquoted);
+                       prog = g_strdup (unquoted);
 
                        /* Executable existing ? */
-                       if(access (prog, X_OK)!=0) {
+                       if (access (prog, X_OK) != 0) {
 #ifdef DEBUG
-                               g_message (G_GNUC_PRETTY_FUNCTION ": Couldn't find executable %s", prog);
+                               g_message ("%s: Couldn't find executable %s",
+                                          __func__, prog);
 #endif
                                g_free (prog);
                                g_free (unquoted);
@@ -255,16 +448,27 @@ gboolean CreateProcess (const gunichar2 *appname, gunichar2 *cmdline,
                        /* Search for file named by cmd in the current
                         * directory
                         */
-                       char *curdir=g_get_current_dir ();
+                       char *curdir = g_get_current_dir ();
 
-                       prog=g_strdup_printf ("%s/%s", curdir, unquoted);
+                       prog = g_strdup_printf ("%s/%s", curdir, unquoted);
                        g_free (unquoted);
                        g_free (curdir);
+
+                       /* And make sure it's executable */
+                       if (access (prog, X_OK) != 0) {
+#ifdef DEBUG
+                               g_message ("%s: Couldn't find executable %s",
+                                          __func__, prog);
+#endif
+                               g_free (prog);
+                               SetLastError (ERROR_FILE_NOT_FOUND);
+                               goto cleanup;
+                       }
                }
 
-               args_after_prog=args;
+               args_after_prog = args;
        } else {
-               gchar *token=NULL;
+               gchar *token = NULL;
                char quote;
                
                /* Dig out the first token from args, taking quotation
@@ -272,24 +476,24 @@ gboolean CreateProcess (const gunichar2 *appname, gunichar2 *cmdline,
                 */
 
                /* First, strip off all leading whitespace */
-               args=g_strchug (args);
+               args = g_strchug (args);
                
                /* args_after_prog points to the contents of args
                 * after token has been set (otherwise argv[0] is
                 * duplicated)
                 */
-               args_after_prog=args;
+               args_after_prog = args;
 
                /* Assume the opening quote will always be the first
                 * character
                 */
-               if(args[0]=='\"' || args [0] == '\'') {
+               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])) {
+                       for (i = 1; args[i] != '\0' && args[i] != quote; i++);
+                       if (g_ascii_isspace (args[i+1])) {
                                /* We found the first token */
-                               token=g_strndup (args+1, i-1);
-                               args_after_prog=args+i;
+                               token = g_strndup (args+1, i-1);
+                               args_after_prog = args + i;
                        } else {
                                /* Quotation mark appeared in the
                                 * middle of the token.  Just give the
@@ -299,59 +503,61 @@ gboolean CreateProcess (const gunichar2 *appname, gunichar2 *cmdline,
                        }
                }
                
-               if(token==NULL) {
+               if (token == NULL) {
                        /* No quote mark, or malformed */
-                       for(i=0; args[i]!='\0'; i++) {
-                               if(g_ascii_isspace (args[i])) {
-                                       token=g_strndup (args, i);
-                                       args_after_prog=args+i+1;
+                       for (i = 0; args[i] != '\0'; i++) {
+                               if (g_ascii_isspace (args[i])) {
+                                       token = g_strndup (args, i);
+                                       args_after_prog = args + i + 1;
                                        break;
                                }
                        }
                }
 
-               if(token==NULL && args[0]!='\0') {
+               if (token == NULL && args[0] != '\0') {
                        /* Must be just one token in the string */
-                       token=g_strdup (args);
-                       args_after_prog=NULL;
+                       token = g_strdup (args);
+                       args_after_prog = NULL;
                }
                
-               if(token==NULL) {
+               if (token == NULL) {
                        /* Give up */
 #ifdef DEBUG
-                       g_message (G_GNUC_PRETTY_FUNCTION
-                                  ": Couldn't find what to exec");
+                       g_message ("%s: Couldn't find what to exec", __func__);
 #endif
 
-                       SetLastError(ERROR_PATH_NOT_FOUND);
+                       SetLastError (ERROR_PATH_NOT_FOUND);
                        goto cleanup;
                }
                
-               /* Turn all the slashes round the right way. Only for the prg. name */
-               for(i=0; i < strlen (token); i++) {
-                       if (token[i]=='\\') {
-                               token[i]='/';
+               /* Turn all the slashes round the right way. Only for
+                * the prg. name
+                */
+               for (i = 0; i < strlen (token); i++) {
+                       if (token[i] == '\\') {
+                               token[i] = '/';
                        }
                }
 
-               if(g_ascii_isalpha (token[0]) && (token[1]==':')) {
+               if (g_ascii_isalpha (token[0]) && (token[1] == ':')) {
                        /* Strip off the drive letter.  I can't
                         * believe that CP/M holdover is still
                         * visible...
                         */
                        g_memmove (token, token+2, strlen (token)-2);
-                       token[strlen (token)-2]='\0';
+                       token[strlen (token)-2] = '\0';
                }
 
-               if(token[0]=='/') {
+               if (token[0] == '/') {
                        /* Assume full path given */
-                       prog=g_strdup (token);
+                       prog = g_strdup (token);
                        
                        /* Executable existing ? */
-                       if(access (prog, X_OK)!=0) {
+                       if (access (prog, X_OK) != 0) {
                                g_free (prog);
 #ifdef DEBUG
-                               g_message (G_GNUC_PRETTY_FUNCTION ": Couldn't find executable %s", token);
+                               g_message ("%s: Couldn't find executable %s",
+                                          __func__, token);
 #endif
                                g_free (token);
                                SetLastError (ERROR_FILE_NOT_FOUND);
@@ -359,7 +565,7 @@ gboolean CreateProcess (const gunichar2 *appname, gunichar2 *cmdline,
                        }
 
                } else {
-                       char *curdir=g_get_current_dir ();
+                       char *curdir = g_get_current_dir ();
 
                        /* FIXME: Need to record the directory
                         * containing the current process, and check
@@ -367,18 +573,18 @@ gboolean CreateProcess (const gunichar2 *appname, gunichar2 *cmdline,
                         * place to look
                         */
 
-                       prog=g_strdup_printf ("%s/%s", curdir, token);
+                       prog = g_strdup_printf ("%s/%s", curdir, token);
                        g_free (curdir);
 
                        /* I assume X_OK is the criterion to use,
                         * rather than F_OK
                         */
-                       if(access (prog, X_OK)!=0) {
+                       if (access (prog, X_OK) != 0) {
                                g_free (prog);
-                               prog=g_find_program_in_path (token);
-                               if(prog==NULL) {
+                               prog = g_find_program_in_path (token);
+                               if (prog == NULL) {
 #ifdef DEBUG
-                                       g_message (G_GNUC_PRETTY_FUNCTION ": Couldn't find executable %s", token);
+                                       g_message ("%s: Couldn't find executable %s", __func__, token);
 #endif
 
                                        g_free (token);
@@ -392,82 +598,228 @@ gboolean CreateProcess (const gunichar2 *appname, gunichar2 *cmdline,
        }
 
 #ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": Exec prog [%s] args [%s]", prog,
+       g_message ("%s: Exec prog [%s] args [%s]", __func__, prog,
                   args_after_prog);
 #endif
        
-       if(args_after_prog!=NULL && *args_after_prog) {
+       if (args_after_prog != NULL && *args_after_prog) {
                gchar *qprog;
 
                qprog = g_shell_quote (prog);
-               full_prog=g_strconcat (qprog, " ", args_after_prog, NULL);
+               full_prog = g_strconcat (qprog, " ", args_after_prog, NULL);
                g_free (qprog);
        } else {
-               full_prog=g_shell_quote (prog);
+               full_prog = g_shell_quote (prog);
        }
-       
-       stored_prog=_wapi_handle_scratch_store (full_prog, strlen (full_prog));
 
-       if(startup!=NULL && startup->dwFlags & STARTF_USESTDHANDLES) {
-               stdin_handle=startup->hStdInput;
-               stdout_handle=startup->hStdOutput;
-               stderr_handle=startup->hStdError;
+       ret = g_shell_parse_argv (full_prog, NULL, &argv, &gerr);
+       if (ret == FALSE) {
+               /* FIXME: Could do something with the GError here
+                */
+       }
+
+       if (startup != NULL && startup->dwFlags & STARTF_USESTDHANDLES) {
+               in_fd = GPOINTER_TO_UINT (startup->hStdInput);
+               out_fd = GPOINTER_TO_UINT (startup->hStdOutput);
+               err_fd = GPOINTER_TO_UINT (startup->hStdError);
        } else {
-               stdin_handle=GetStdHandle (STD_INPUT_HANDLE);
-               stdout_handle=GetStdHandle (STD_OUTPUT_HANDLE);
-               stderr_handle=GetStdHandle (STD_ERROR_HANDLE);
-       }
-       
-       ret=_wapi_handle_process_fork (stored_prog, env, stored_dir,
-                                      inherit_handles, create_flags,
-                                      stdin_handle, stdout_handle,
-                                      stderr_handle, &process_handle,
-                                      &thread_handle, &pid, &tid);
-       
-       if(ret==TRUE && process_info!=NULL) {
-               process_info->hProcess=process_handle;
-               process_info->hThread=thread_handle;
-               process_info->dwProcessId=pid;
-               process_info->dwThreadId=tid;
-               /* Wait for possible execve failure */
-               if (WaitForSingleObjectEx (process_handle, 500, FALSE) != WAIT_TIMEOUT) {
-                       _wapi_lookup_handle (GUINT_TO_POINTER (process_handle),
-                                            WAPI_HANDLE_PROCESS,
-                                            (gpointer *) &process_handle_data,
-                                            NULL);
-               
-                       if (process_handle_data && process_handle_data->exec_errno != 0) {
-                               ret = FALSE;
-                               SetLastError (ERROR_PATH_NOT_FOUND);
+               in_fd = GPOINTER_TO_UINT (GetStdHandle (STD_INPUT_HANDLE));
+               out_fd = GPOINTER_TO_UINT (GetStdHandle (STD_OUTPUT_HANDLE));
+               err_fd = GPOINTER_TO_UINT (GetStdHandle (STD_ERROR_HANDLE));
+       }
+       
+       g_strlcpy (process_handle.proc_name, prog,
+                  _WAPI_PROC_NAME_MAX_LEN - 1);
+
+       process_set_defaults (&process_handle);
+       
+       handle = _wapi_handle_new (WAPI_HANDLE_PROCESS, &process_handle);
+       if (handle == _WAPI_HANDLE_INVALID) {
+               g_warning ("%s: error creating process handle", __func__);
+
+               SetLastError (ERROR_PATH_NOT_FOUND);
+               goto cleanup;
+       }
+
+       /* Hold another reference so the process has somewhere to
+        * store its exit data even if we drop this handle
+        */
+       _wapi_handle_ref (handle);
+       
+       /* new_environ is a block of NULL-terminated strings, which
+        * is itself NULL-terminated. Of course, passing an array of
+        * string pointers would have made things too easy :-(
+        *
+        * If new_environ is not NULL it specifies the entire set of
+        * environment variables in the new process.  Otherwise the
+        * new process inherits the same environment.
+        */
+       if (new_environ != NULL) {
+               gunichar2 *new_environp;
+
+               /* Count the number of strings */
+               for (new_environp = (gunichar2 *)new_environ; *new_environp;
+                    new_environp++) {
+                       env_count++;
+                       while (*new_environp) {
+                               new_environp++;
                        }
                }
-       } else if (ret==FALSE) {
-               /* FIXME: work out a better error code
+
+               /* +2: one for the process handle value, and the last
+                * one is NULL
                 */
-               SetLastError (ERROR_PATH_NOT_FOUND);
+               env_strings = g_new0 (gchar *, env_count + 2);
+               
+               /* Copy each environ string into 'strings' turning it
+                * into utf8 (or the requested encoding) at the same
+                * time
+                */
+               env_count = 0;
+               for (new_environp = (gunichar2 *)new_environ; *new_environp;
+                    new_environp++) {
+                       env_strings[env_count] = mono_unicode_to_external (new_environp);
+                       env_count++;
+                       while (*new_environp) {
+                               new_environp++;
+                       }
+               }
+       } else {
+               for (i = 0; environ[i] != NULL; i++) {
+                       env_count++;
+               }
+
+               /* +2: one for the process handle value, and the last
+                * one is NULL
+                */
+               env_strings = g_new0 (gchar *, env_count + 2);
+               
+               /* Copy each environ string into 'strings' turning it
+                * into utf8 (or the requested encoding) at the same
+                * time
+                */
+               env_count = 0;
+               for (i = 0; environ[i] != NULL; i++) {
+                       env_strings[env_count] = g_strdup (environ[i]);
+                       env_count++;
+               }
+       }
+       /* pass process handle info to the child, so it doesn't have
+        * to do an expensive search over the whole list
+        */
+       if (env_strings != NULL) {
+               struct _WapiHandleUnshared *handle_data;
+               struct _WapiHandle_shared_ref *ref;
+               
+               handle_data = &_WAPI_PRIVATE_HANDLES(GPOINTER_TO_UINT(handle));
+               ref = &handle_data->u.shared;
+               
+               env_strings[env_count] = g_strdup_printf ("_WAPI_PROCESS_HANDLE_OFFSET=%d", ref->offset);
+       }
+
+       thr_ret = _wapi_handle_lock_shared_handles ();
+       g_assert (thr_ret == 0);
+       
+       pid = fork ();
+       if (pid == -1) {
+               /* Error */
+               SetLastError (ERROR_OUTOFMEMORY);
+               _wapi_handle_unref (handle);
+               goto cleanup;
+       } else if (pid == 0) {
+               /* Child */
+               
+               /* Wait for the parent to finish setting up the
+                * handle.  The semaphore lock is safe because the
+                * sem_undo structures of a semaphore aren't inherited
+                * across a fork ()
+                */
+               thr_ret = _wapi_handle_lock_shared_handles ();
+               g_assert (thr_ret == 0);
+       
+               _wapi_handle_unlock_shared_handles ();
+               
+               /* should we detach from the process group? */
+
+               /* Connect stdin, stdout and stderr */
+               dup2 (in_fd, 0);
+               dup2 (out_fd, 1);
+               dup2 (err_fd, 2);
+
+               if (inherit_handles != TRUE) {
+                       /* FIXME: do something here */
+               }
+               
+               /* Close all file descriptors */
+               for (i = getdtablesize () - 1; i > 2; i--) {
+                       close (i);
+               }
+
+#ifdef DEBUG
+               g_message ("%s: exec()ing [%s] in dir [%s]", __func__, cmd,
+                          dir);
+               for (i = 0; argv[i] != NULL; i++) {
+                       g_message ("arg %d: [%s]", i, argv[i]);
+               }
+               
+               for (i = 0; env_strings[i] != NULL; i++) {
+                       g_message ("env %d: [%s]", i, env_strings[i]);
+               }
+#endif
+
+               /* set cwd */
+               if (chdir (dir) == -1) {
+                       /* set error */
+                       exit (-1);
+               }
+               
+               /* exec */
+               execve (argv[0], argv, env_strings);
+               
+               /* set error */
+               exit (-1);
+       }
+       /* parent */
+       
+       ret = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
+                                  (gpointer *)&process_handle_data);
+       if (ret == FALSE) {
+               g_warning ("%s: error looking up process handle %p", __func__,
+                          handle);
+               _wapi_handle_unref (handle);
+               goto cleanup;
+       }
+       
+       process_handle_data->id = pid;
+       
+       if (process_info != NULL) {
+               process_info->hProcess = handle;
+               process_info->dwProcessId = pid;
+
+               /* FIXME: we might need to handle the thread info some
+                * day
+                */
+               process_info->hThread = NULL;
+               process_info->dwThreadId = 0;
        }
 
 cleanup:
-       if(cmd!=NULL) {
+       _wapi_handle_unlock_shared_handles ();
+
+       if (cmd != NULL) {
                g_free (cmd);
        }
-       if(full_prog!=NULL) {
+       if (full_prog != NULL) {
                g_free (prog);
        }
-       if(stored_prog!=0) {
-               _wapi_handle_scratch_delete (stored_prog);
-       }
-       if(args!=NULL) {
+       if (args != NULL) {
                g_free (args);
        }
-       if(dir!=NULL) {
+       if (dir != NULL) {
                g_free (dir);
        }
-       if(stored_dir!=0) {
-               _wapi_handle_scratch_delete (stored_dir);
-       }
-       if(env!=0) {
-               _wapi_handle_scratch_delete_string_array (env);
+       if(env_strings != NULL) {
+               g_strfreev (env_strings);
        }
        
        return(ret);
@@ -481,16 +833,17 @@ static void process_set_name (struct _WapiHandle_process *process_handle)
        utf8_progname=mono_utf8_from_external (progname);
 
 #ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": using [%s] as prog name",
-                  progname);
+       g_message ("%s: using [%s] as prog name", __func__, progname);
 #endif
 
        if(utf8_progname!=NULL) {
                slash=strrchr (utf8_progname, '/');
                if(slash!=NULL) {
-                       process_handle->proc_name=_wapi_handle_scratch_store (slash+1, strlen (slash+1));
+                       g_strlcpy (process_handle->proc_name, slash+1,
+                                  _WAPI_PROC_NAME_MAX_LEN - 1);
                } else {
-                       process_handle->proc_name=_wapi_handle_scratch_store (utf8_progname, strlen (utf8_progname));
+                       g_strlcpy (process_handle->proc_name, utf8_progname,
+                                  _WAPI_PROC_NAME_MAX_LEN - 1);
                }
 
                g_free (utf8_progname);
@@ -499,90 +852,92 @@ static void process_set_name (struct _WapiHandle_process *process_handle)
 
 extern void _wapi_time_t_to_filetime (time_t timeval, WapiFileTime *filetime);
 
+#if !GLIB_CHECK_VERSION (2,4,0)
+#define g_setenv(a,b,c) setenv(a,b,c)
+#define g_unsetenv(a) unsetenv(a)
+#endif
+
 static void process_set_current (void)
 {
-       struct _WapiHandle_process *process_handle;
-       gboolean ok;
-       pid_t pid=getpid ();
-       char *handle_env;
+       pid_t pid = _wapi_getpid ();
+       const char *handle_env;
+       struct _WapiHandle_process process_handle = {0};
        
-       handle_env=getenv ("_WAPI_PROCESS_HANDLE");
-       if(handle_env==NULL) {
+       handle_env = g_getenv ("_WAPI_PROCESS_HANDLE_OFFSET");
+       g_unsetenv ("_WAPI_PROCESS_HANDLE_OFFSET");
+       
+       if (handle_env != NULL) {
+               struct _WapiHandle_process *process_handlep;
+               guchar *procname = NULL;
+               gboolean ok;
+               
+               current_process = _wapi_handle_new_from_offset (WAPI_HANDLE_PROCESS, atoi (handle_env), TRUE);
+               
 #ifdef DEBUG
-               g_message (G_GNUC_PRETTY_FUNCTION
-                          ": Need to create my own process handle");
+               g_message ("%s: Found my process handle: %p (offset %d 0x%x)",
+                          __func__, current_process, atoi (handle_env),
+                          atoi (handle_env));
 #endif
 
-               current_process=_wapi_handle_new (WAPI_HANDLE_PROCESS);
-               if(current_process==_WAPI_HANDLE_INVALID) {
-                       g_warning (G_GNUC_PRETTY_FUNCTION
-                                  ": error creating process handle");
-                       return;
-               }
-
-               ok=_wapi_lookup_handle (current_process, WAPI_HANDLE_PROCESS,
-                                       (gpointer *)&process_handle, NULL);
-               if(ok==FALSE) {
-                       g_warning (G_GNUC_PRETTY_FUNCTION
-                                  ": error looking up process handle %p",
-                                  current_process);
-                       return;
-               }
+               ok = _wapi_lookup_handle (current_process, WAPI_HANDLE_PROCESS,
+                                         (gpointer *)&process_handlep);
+               if (ok) {
+                       /* This test will probably break on linuxthreads, but
+                        * that should be ancient history on all distros we
+                        * care about by now
+                        */
+                       if (process_handlep->id == pid) {
+                               procname = process_handlep->proc_name;
+                               if (!strcmp (procname, "mono")) {
+                                       /* Set a better process name */
+#ifdef DEBUG
+                                       g_message ("%s: Setting better process name", __func__);
+#endif
+                                       
+                                       process_set_name (process_handlep);
+                               } else {
+#ifdef DEBUG
+                                       g_message ("%s: Leaving process name: %s", __func__, procname);
+#endif
+                               }
 
-               process_handle->id=pid;
-               
-               /* These seem to be the defaults on w2k */
-               process_handle->min_working_set=204800;
-               process_handle->max_working_set=1413120;
+                               return;
+                       }
 
-               _wapi_time_t_to_filetime (time (NULL), &process_handle->create_time);
+                       /* Wrong pid, so drop this handle and fall through to
+                        * create a new one
+                        */
+                       _wapi_handle_unref (current_process);
+               }
+       }
 
-               process_set_name (process_handle);
-               
-               /* Make sure the new handle has a reference so it wont go away
-                * until this process exits
-                */
-               _wapi_handle_ref (current_process);
-       } else {
-               guchar *procname;
-               
-               current_process=GUINT_TO_POINTER (atoi (handle_env));
+       /* We get here if the handle wasn't specified in the
+        * environment, or if the process ID was wrong, or if the
+        * handle lookup failed (eg if the parent process forked and
+        * quit immediately, and deleted the shared data before the
+        * child got a chance to attach it.)
+        */
 
 #ifdef DEBUG
-               g_message (G_GNUC_PRETTY_FUNCTION
-                          ": Found my process handle: %p", current_process);
+       g_message ("%s: Need to create my own process handle", __func__);
 #endif
 
-               ok=_wapi_lookup_handle (current_process, WAPI_HANDLE_PROCESS,
-                                       (gpointer *)&process_handle, NULL);
-               if(ok==FALSE) {
-                       g_warning (G_GNUC_PRETTY_FUNCTION
-                                  ": error looking up process handle %p",
-                                  current_process);
-                       return;
-               }
+       process_handle.id = pid;
 
-               procname=_wapi_handle_scratch_lookup (process_handle->proc_name);
-               if(procname!=NULL) {
-                       if(!strcmp (procname, "mono")) {
-                               /* Set a better process name */
-#ifdef DEBUG
-                               g_message (G_GNUC_PRETTY_FUNCTION ": Setting better process name");
-#endif
+       process_set_defaults (&process_handle);
+       process_set_name (&process_handle);
 
-                               _wapi_handle_scratch_delete (process_handle->proc_name);
-                               process_set_name (process_handle);
-                       } else {
-#ifdef DEBUG
-                               g_message (G_GNUC_PRETTY_FUNCTION
-                                          ": Leaving process name: %s",
-                                          procname);
-#endif
-                       }
-                       
-                       g_free (procname);
-               }
+       current_process = _wapi_handle_new (WAPI_HANDLE_PROCESS,
+                                           &process_handle);
+       if (current_process == _WAPI_HANDLE_INVALID) {
+               g_warning ("%s: error creating process handle", __func__);
+               return;
        }
+               
+       /* Make sure the new handle has a reference so it wont go away
+        * until this process exits
+        */
+       _wapi_handle_ref (current_process);
 }
 
 /* Returns a pseudo handle that doesn't need to be closed afterwards */
@@ -593,36 +948,97 @@ gpointer GetCurrentProcess (void)
        return((gpointer)-1);
 }
 
-guint32 GetCurrentProcessId (void)
+guint32 GetProcessId (gpointer handle)
 {
-       struct _WapiHandle_process *current_process_handle;
+       struct _WapiHandle_process *process_handle;
        gboolean ok;
        
+       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
+                                 (gpointer *)&process_handle);
+       if (ok == FALSE) {
+               SetLastError (ERROR_INVALID_HANDLE);
+               return (0);
+       }
+       
+       return (process_handle->id);
+}
+
+guint32 GetCurrentProcessId (void)
+{
        mono_once (&process_current_once, process_set_current);
                
-       ok=_wapi_lookup_handle (current_process, WAPI_HANDLE_PROCESS,
-                               (gpointer *)&current_process_handle, NULL);
-       if(ok==FALSE) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error looking up current process handle %p",
-                          current_process);
-               /* No failure return is defined.  PID 0 is invalid.
-                * This should only be reached when something else has
-                * gone badly wrong anyway.
+       return (GetProcessId (current_process));
+}
+
+/* Returns the process id as a convenience to the functions that call this */
+static pid_t signal_process_if_gone (gpointer handle)
+{
+       struct _WapiHandle_process *process_handle;
+       gboolean ok;
+       
+       /* Make sure the process is signalled if it has exited - if
+        * the parent process didn't wait for it then it won't be
+        */
+       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
+                                 (gpointer *)&process_handle);
+       if (ok == FALSE) {
+               /* It's possible that the handle has vanished during
+                * the _wapi_search_handle before it gets here, so
+                * don't spam the console with warnings.
                 */
-               return(0);
+/*             g_warning ("%s: error looking up process handle %p",
+  __func__, handle);*/
+               
+               return (0);
        }
        
-       return(current_process_handle->id);
+#ifdef DEBUG
+       g_message ("%s: looking at process %d", __func__, process_handle->id);
+#endif
+
+       if (kill (process_handle->id, 0) == -1 &&
+           (errno == ESRCH ||
+            errno == EPERM)) {
+               /* The process is dead, (EPERM tells us a new process
+                * has that ID, but as it's owned by someone else it
+                * can't be the one listed in our shared memory file)
+                */
+               _wapi_shared_handle_set_signal_state (handle, TRUE);
+       }
+
+       return (process_handle->id);
 }
 
 static gboolean process_enum (gpointer handle, gpointer user_data)
 {
-       GPtrArray *processes=user_data;
+       GArray *processes=user_data;
+       pid_t pid = signal_process_if_gone (handle);
+       int i;
+       
+       if (pid == 0) {
+               return (FALSE);
+       }
        
        /* Ignore processes that have already exited (ie they are signalled) */
-       if(_wapi_handle_issignalled (handle)==FALSE) {
-               g_ptr_array_add (processes, handle);
+       if (_wapi_handle_issignalled (handle) == FALSE) {
+#ifdef DEBUG
+               g_message ("%s: process %d added to array", __func__, pid);
+#endif
+
+               /* This ensures that duplicates aren't returned (see
+                * the comment above _wapi_search_handle () for why
+                * it's needed
+                */
+               for (i = 0; i < processes->len; i++) {
+                       if (g_array_index (processes, pid_t, i) == pid) {
+                               /* We've already got this one, return
+                                * FALSE to keep searching
+                                */
+                               return (FALSE);
+                       }
+               }
+               
+               g_array_append_val (processes, pid);
        }
        
        /* Return false to keep searching */
@@ -631,60 +1047,47 @@ static gboolean process_enum (gpointer handle, gpointer user_data)
 
 gboolean EnumProcesses (guint32 *pids, guint32 len, guint32 *needed)
 {
-       GPtrArray *processes=g_ptr_array_new ();
-       guint32 fit, i;
+       GArray *processes = g_array_new (FALSE, FALSE, sizeof(pid_t));
+       guint32 fit, i, j;
        
        mono_once (&process_current_once, process_set_current);
        
        _wapi_search_handle (WAPI_HANDLE_PROCESS, process_enum, processes,
-                            NULL, NULL);
+                            NULL, TRUE);
        
        fit=len/sizeof(guint32);
-       for(i=0; i<fit && i<processes->len; i++) {
-               struct _WapiHandle_process *process_handle;
-               gboolean ok;
-
-               ok=_wapi_lookup_handle (g_ptr_array_index (processes, i),
-                                       WAPI_HANDLE_PROCESS,
-                                       (gpointer *)&process_handle, NULL);
-               if(ok==FALSE) {
-                       g_warning (G_GNUC_PRETTY_FUNCTION ": error looking up process handle %p", g_ptr_array_index (processes, i));
-                       g_ptr_array_free (processes, FALSE);
-                       return(FALSE);
-               }
-
-               pids[i]=process_handle->id;
+       for (i = 0, j = 0; j < fit && i < processes->len; i++) {
+               pids[j++] = g_array_index (processes, pid_t, i);
        }
 
-       g_ptr_array_free (processes, FALSE);
+       g_array_free (processes, TRUE);
        
-       *needed=i*sizeof(guint32);
+       *needed = j * sizeof(guint32);
        
        return(TRUE);
 }
 
 static gboolean process_open_compare (gpointer handle, gpointer user_data)
 {
-       struct _WapiHandle_process *process_handle;
-       gboolean ok;
-       pid_t pid;
-       
-       ok=_wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
-                               (gpointer *)&process_handle, NULL);
-       if(ok==FALSE) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error looking up process handle %p", handle);
+       pid_t wanted_pid;
+       pid_t checking_pid = signal_process_if_gone (handle);
+
+       if (checking_pid == 0) {
                return(FALSE);
        }
-
-       pid=GPOINTER_TO_UINT (user_data);
+       
+       wanted_pid = GPOINTER_TO_UINT (user_data);
 
        /* It's possible to have more than one process handle with the
         * same pid, but only the one running process can be
         * unsignalled
         */
-       if(process_handle->id==pid &&
-          _wapi_handle_issignalled (handle)==FALSE) {
+       if (checking_pid == wanted_pid &&
+           _wapi_handle_issignalled (handle) == FALSE) {
+               /* If the handle is blown away in the window between
+                * returning TRUE here and _wapi_search_handle pinging
+                * the timestamp, the search will continue
+                */
                return(TRUE);
        } else {
                return(FALSE);
@@ -698,14 +1101,19 @@ gpointer OpenProcess (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUS
        
        mono_once (&process_current_once, process_set_current);
 
-       handle=_wapi_search_handle (WAPI_HANDLE_PROCESS, process_open_compare,
-                                   GUINT_TO_POINTER (pid), NULL, NULL);
-       if(handle==0) {
 #ifdef DEBUG
-               g_message (G_GNUC_PRETTY_FUNCTION ": Can't find pid %d", pid);
+       g_message ("%s: looking for process %d", __func__, pid);
+#endif
+
+       handle = _wapi_search_handle (WAPI_HANDLE_PROCESS,
+                                     process_open_compare,
+                                     GUINT_TO_POINTER (pid), NULL, TRUE);
+       if (handle == 0) {
+#ifdef DEBUG
+               g_message ("%s: Can't find pid %d", __func__, pid);
 #endif
 
-               /* Set an error code */
+               SetLastError (ERROR_PROC_NOT_FOUND);
        
                return(NULL);
        }
@@ -727,21 +1135,22 @@ gboolean GetExitCodeProcess (gpointer process, guint32 *code)
        }
        
        ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
-                               (gpointer *)&process_handle, NULL);
+                               (gpointer *)&process_handle);
        if(ok==FALSE) {
 #ifdef DEBUG
-               g_message (G_GNUC_PRETTY_FUNCTION ": Can't find process %p",
-                          process);
+               g_message ("%s: Can't find process %p", __func__, process);
 #endif
                
                return(FALSE);
        }
        
-       /* A process handle is only signalled if the process has exited */
-       if(_wapi_handle_issignalled (process)==TRUE) {
-               *code=process_handle->exitstatus;
+       /* A process handle is only signalled if the process has exited
+        * and has been waited for */
+       if (_wapi_handle_issignalled (process) == TRUE ||
+           process_wait (process, 0) == WAIT_OBJECT_0) {
+               *code = process_handle->exitstatus;
        } else {
-               *code=STILL_ACTIVE;
+               *code = STILL_ACTIVE;
        }
        
        return(TRUE);
@@ -763,11 +1172,10 @@ gboolean GetProcessTimes (gpointer process, WapiFileTime *create_time,
        }
        
        ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
-                               (gpointer *)&process_handle, NULL);
+                               (gpointer *)&process_handle);
        if(ok==FALSE) {
 #ifdef DEBUG
-               g_message (G_GNUC_PRETTY_FUNCTION ": Can't find process %p",
-                          process);
+               g_message ("%s: Can't find process %p", __func__, process);
 #endif
                
                return(FALSE);
@@ -820,9 +1228,8 @@ guint32 GetModuleBaseName (gpointer process, gpointer module,
        mono_once (&process_current_once, process_set_current);
 
 #ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION
-                  ": Getting module base name, process handle %p module %p",
-                  process, module);
+       g_message ("%s: Getting module base name, process handle %p module %p",
+                  __func__, process, module);
 #endif
 
        if(basename==NULL || size==0) {
@@ -830,11 +1237,10 @@ guint32 GetModuleBaseName (gpointer process, gpointer module,
        }
        
        ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
-                               (gpointer *)&process_handle, NULL);
+                               (gpointer *)&process_handle);
        if(ok==FALSE) {
 #ifdef DEBUG
-               g_message (G_GNUC_PRETTY_FUNCTION ": Can't find process %p",
-                          process);
+               g_message ("%s: Can't find process %p", __func__, process);
 #endif
                
                return(FALSE);
@@ -846,49 +1252,46 @@ guint32 GetModuleBaseName (gpointer process, gpointer module,
                 */
                pid_t pid;
                gunichar2 *procname;
-               guchar *procname_utf8;
+               guchar *procname_utf8 = NULL;
                glong len, bytes;
                
 #ifdef DEBUG
-               g_message (G_GNUC_PRETTY_FUNCTION
-                          ": Returning main module name");
+               g_message ("%s: Returning main module name", __func__);
 #endif
 
                pid=process_handle->id;
-               procname_utf8=_wapi_handle_scratch_lookup (process_handle->proc_name);
+               procname_utf8 = process_handle->proc_name;
        
 #ifdef DEBUG
-               g_message (G_GNUC_PRETTY_FUNCTION ": Process name is [%s]",
+               g_message ("%s: Process name is [%s]", __func__,
                           procname_utf8);
 #endif
 
-               procname=g_utf8_to_utf16 (procname_utf8, -1, NULL, &len, NULL);
-               if(procname==NULL) {
+               procname = g_utf8_to_utf16 (procname_utf8, -1, NULL, &len,
+                                           NULL);
+               if (procname == NULL) {
                        /* bugger */
-                       g_free (procname_utf8);
                        return(0);
                }
 
                /* Add the terminator, and convert chars to bytes */
-               bytes=(len+1)*2;
+               bytes = (len + 1) * 2;
                
-               if(size<bytes) {
+               if (size < bytes) {
 #ifdef DEBUG
-                       g_message (G_GNUC_PRETTY_FUNCTION ": Size %d smaller than needed (%ld); truncating", size, bytes);
+                       g_message ("%s: Size %d smaller than needed (%ld); truncating", __func__, size, bytes);
 #endif
 
                        memcpy (basename, procname, size);
                } else {
 #ifdef DEBUG
-                       g_message (G_GNUC_PRETTY_FUNCTION
-                                  ": Size %d larger than needed (%ld)",
-                                  size, bytes);
+                       g_message ("%s: Size %d larger than needed (%ld)",
+                                  __func__, size, bytes);
 #endif
 
                        memcpy (basename, procname, bytes);
                }
                
-               g_free (procname_utf8);
                g_free (procname);
 
                return(len);
@@ -912,11 +1315,10 @@ gboolean GetProcessWorkingSetSize (gpointer process, size_t *min, size_t *max)
        }
        
        ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
-                               (gpointer *)&process_handle, NULL);
+                               (gpointer *)&process_handle);
        if(ok==FALSE) {
 #ifdef DEBUG
-               g_message (G_GNUC_PRETTY_FUNCTION ": Can't find process %p",
-                          process);
+               g_message ("%s: Can't find process %p", __func__, process);
 #endif
                
                return(FALSE);
@@ -936,11 +1338,10 @@ gboolean SetProcessWorkingSetSize (gpointer process, size_t min, size_t max)
        mono_once (&process_current_once, process_set_current);
 
        ok=_wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
-                               (gpointer *)&process_handle, NULL);
+                               (gpointer *)&process_handle);
        if(ok==FALSE) {
 #ifdef DEBUG
-               g_message (G_GNUC_PRETTY_FUNCTION ": Can't find process %p",
-                          process);
+               g_message ("%s: Can't find process %p", __func__, process);
 #endif
                
                return(FALSE);
@@ -958,22 +1359,38 @@ TerminateProcess (gpointer process, gint32 exitCode)
 {
        struct _WapiHandle_process *process_handle;
        gboolean ok;
-       gint signo;
-       gint err;
+       int signo;
+       int ret;
 
        ok = _wapi_lookup_handle (process, WAPI_HANDLE_PROCESS,
-                                 (gpointer *) &process_handle, NULL);
+                                 (gpointer *) &process_handle);
 
        if (ok == FALSE) {
 #ifdef DEBUG
-               g_message (G_GNUC_PRETTY_FUNCTION ": Can't find process %p",
-                          process);
+               g_message ("%s: Can't find process %p", __func__, process);
 #endif
                SetLastError (ERROR_INVALID_HANDLE);
                return FALSE;
        }
 
        signo = (exitCode == -1) ? SIGKILL : SIGTERM;
-       return _wapi_handle_process_kill (process_handle->id, signo, &err);
+       ret = kill (process_handle->id, signo);
+       if (ret == -1) {
+               switch (errno) {
+               case EINVAL:
+                       SetLastError (ERROR_INVALID_PARAMETER);
+                       break;
+               case EPERM:
+                       SetLastError (ERROR_ACCESS_DENIED);
+                       break;
+               case ESRCH:
+                       SetLastError (ERROR_PROC_NOT_FOUND);
+                       break;
+               default:
+                       SetLastError (ERROR_GEN_FAILURE);
+               }
+       }
+       
+       return (ret == 0);
 }