2007-07-22 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / io-layer / processes.c
index 90723143a09bead7b1de0c6af2117c7a94936fb4..928105c8f44cc827f27950ac337c0302e066e35e 100644 (file)
@@ -18,6 +18,7 @@
 #include <unistd.h>
 #include <signal.h>
 #include <sys/wait.h>
+#include <fcntl.h>
 
 #include <mono/io-layer/wapi.h>
 #include <mono/io-layer/wapi-private.h>
@@ -80,6 +81,10 @@ static gboolean process_set_termination_details (gpointer handle, int status)
                process_handle->exitstatus = WEXITSTATUS(status);
        }
        _wapi_time_t_to_filetime (time(NULL), &process_handle->exit_time);
+
+       /* Don't set process_handle->waited here, it needs to only
+        * happen in the parent when wait() has been called.
+        */
        
 #ifdef DEBUG
        g_message ("%s: Setting handle %p signalled", __func__, handle);
@@ -108,17 +113,17 @@ static gboolean waitfor_pid (gpointer test, gpointer user_data)
        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);
        }
+
+       if (process->waited) {
+               /* We've already done this one */
+               return(FALSE);
+       }
        
        do {
                ret = waitpid (process->id, &status, WNOHANG);
@@ -138,6 +143,8 @@ static gboolean waitfor_pid (gpointer test, gpointer user_data)
        g_message ("%s: Process %d finished", __func__, ret);
 #endif
 
+       process->waited = TRUE;
+       
        *(int *)user_data = status;
        
        return (TRUE);
@@ -185,8 +192,16 @@ static guint32 process_wait (gpointer handle, guint32 timeout)
 #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);
+       }
        
-       if (_wapi_handle_issignalled (handle)) {
+       if (process_handle->waited) {
                /* We've already done this one */
 #ifdef DEBUG
                g_message ("%s: Process %p already signalled", __func__,
@@ -195,14 +210,6 @@ static guint32 process_wait (gpointer handle, guint32 timeout)
 
                return (WAIT_OBJECT_0);
        }
-
-       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;
        
@@ -211,9 +218,15 @@ static guint32 process_wait (gpointer handle, guint32 timeout)
 #endif
 
        if (timeout == INFINITE) {
-               while ((ret = waitpid (pid, &status, 0)) != pid) {
-                       if (ret == (pid_t)-1 && errno != EINTR) {
-                               return(WAIT_FAILED);
+               if (pid == _wapi_getpid ()) {
+                       do {
+                               Sleep (10000);
+                       } while(1);
+               } else {
+                       while ((ret = waitpid (pid, &status, 0)) != pid) {
+                               if (ret == (pid_t)-1 && errno != EINTR) {
+                                       return(WAIT_FAILED);
+                               }
                        }
                }
        } else if (timeout == 0) {
@@ -224,18 +237,47 @@ static guint32 process_wait (gpointer handle, guint32 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);
-                       }
+               if (pid == _wapi_getpid ()) {
+                       Sleep (timeout);
+                       return(WAIT_TIMEOUT);
+               } else {
+                       do {
+                               ret = waitpid (pid, &status, WNOHANG);
+#ifdef DEBUG
+                               g_message ("%s: waitpid returns: %d, timeout is %d", __func__, ret, timeout);
+#endif
+                               
+                               if (ret == pid) {
+                                       break;
+                               } else if (ret == (pid_t)-1 &&
+                                          errno != EINTR) {
+#ifdef DEBUG
+                                       g_message ("%s: waitpid failure: %s",
+                                                  __func__,
+                                                  g_strerror (errno));
+#endif
+
+                                       if (errno == ECHILD &&
+                                           process_handle->waited) {
+                                               /* The background
+                                                * process reaper must
+                                                * have got this one
+                                                */
+#ifdef DEBUG
+                                               g_message ("%s: Process %p already reaped", __func__, handle);
+#endif
 
-                       _wapi_handle_spin (100);
-                       timeout -= 100;
-               } while (timeout > 0);
+                                               return(WAIT_OBJECT_0);
+                                       } else {
+                                               return(WAIT_FAILED);
+                                       }
+                               }
 
+                               _wapi_handle_spin (100);
+                               timeout -= 100;
+                       } while (timeout > 0);
+               }
+               
                if (timeout <= 0) {
                        return(WAIT_TIMEOUT);
                }
@@ -251,7 +293,8 @@ static guint32 process_wait (gpointer handle, guint32 timeout)
                SetLastError (ERROR_OUTOFMEMORY);
                return (WAIT_FAILED);
        }
-
+       process_handle->waited = TRUE;
+       
        return(WAIT_OBJECT_0);
 }
 
@@ -268,6 +311,8 @@ static void process_set_defaults (struct _WapiHandle_process *process_handle)
        process_handle->min_working_set = 204800;
        process_handle->max_working_set = 1413120;
 
+       process_handle->waited = FALSE;
+       
        _wapi_time_t_to_filetime (time (NULL), &process_handle->create_time);
 }
 
@@ -415,6 +460,114 @@ gboolean ShellExecuteEx (WapiShellExecuteInfo *sei)
        return (ret);
 }
 
+static gboolean
+is_managed_binary (const gchar *filename)
+{
+       int original_errno = errno;
+#if defined(HAVE_LARGE_FILE_SUPPORT) && defined(O_LARGEFILE)
+       int file = open (filename, O_RDONLY | O_LARGEFILE);
+#else
+       int file = open (filename, O_RDONLY);
+#endif
+       off_t new_offset;
+       unsigned char buffer[8];
+       off_t file_size, optional_header_offset;
+       off_t pe_header_offset;
+       gboolean managed = FALSE;
+       int num_read;
+       guint32 first_word, second_word;
+       
+       /* If we are unable to open the file, then we definitely
+        * can't say that it is managed. The child mono process
+        * probably wouldn't be able to open it anyway.
+        */
+       if (file < 0) {
+               errno = original_errno;
+               return FALSE;
+       }
+
+       /* Retrieve the length of the file for future sanity checks. */
+       file_size = lseek (file, 0, SEEK_END);
+       lseek (file, 0, SEEK_SET);
+
+       /* We know we need to read a header field at offset 60. */
+       if (file_size < 64)
+               goto leave;
+
+       num_read = read (file, buffer, 2);
+
+       if ((num_read != 2) || (buffer[0] != 'M') || (buffer[1] != 'Z'))
+               goto leave;
+
+       new_offset = lseek (file, 60, SEEK_SET);
+
+       if (new_offset != 60)
+               goto leave;
+       
+       num_read = read (file, buffer, 4);
+
+       if (num_read != 4)
+               goto leave;
+       pe_header_offset =  buffer[0]
+               | (buffer[1] <<  8)
+               | (buffer[2] << 16)
+               | (buffer[3] << 24);
+       
+       if (pe_header_offset + 24 > file_size)
+               goto leave;
+
+       new_offset = lseek (file, pe_header_offset, SEEK_SET);
+
+       if (new_offset != pe_header_offset)
+               goto leave;
+
+       num_read = read (file, buffer, 4);
+
+       if ((num_read != 4) || (buffer[0] != 'P') || (buffer[1] != 'E') || (buffer[2] != 0) || (buffer[3] != 0))
+               goto leave;
+
+       /*
+        * Verify that the header we want in the optional header data
+        * is present in this binary.
+        */
+       new_offset = lseek (file, pe_header_offset + 20, SEEK_SET);
+
+       if (new_offset != pe_header_offset + 20)
+               goto leave;
+
+       num_read = read (file, buffer, 2);
+
+       if ((num_read != 2) || ((buffer[0] | (buffer[1] << 8)) < 216))
+               goto leave;
+
+       /* Read the CLR header address and size fields. These will be
+        * zero if the binary is not managed.
+        */
+       optional_header_offset = pe_header_offset + 24;
+       new_offset = lseek (file, optional_header_offset + 208, SEEK_SET);
+
+       if (new_offset != optional_header_offset + 208)
+               goto leave;
+
+       num_read = read (file, buffer, 8);
+       
+       /* We are not concerned with endianness, only with
+        * whether it is zero or not.
+        */
+       first_word = *(guint32 *)&buffer[0];
+       second_word = *(guint32 *)&buffer[4];
+       
+       if ((num_read != 8) || (first_word == 0) || (second_word == 0))
+               goto leave;
+       
+       managed = TRUE;
+
+leave:
+       close (file);
+       errno = original_errno;
+       return managed;
+}
+
 gboolean CreateProcess (const gunichar2 *appname, const gunichar2 *cmdline,
                        WapiSecurityAttributes *process_attrs G_GNUC_UNUSED,
                        WapiSecurityAttributes *thread_attrs G_GNUC_UNUSED,
@@ -423,7 +576,7 @@ gboolean CreateProcess (const gunichar2 *appname, const gunichar2 *cmdline,
                        WapiStartupInfo *startup,
                        WapiProcessInformation *process_info)
 {
-       gchar *cmd=NULL, *prog = NULL, *full_prog = NULL, *args = NULL, *args_after_prog = NULL, *dir = NULL, **env_strings = NULL, **argv;
+       gchar *cmd=NULL, *prog = NULL, *full_prog = NULL, *args = NULL, *args_after_prog = NULL, *dir = NULL, **env_strings = NULL, **argv = NULL;
        guint32 i, env_count = 0;
        gboolean ret = FALSE;
        gpointer handle;
@@ -536,7 +689,6 @@ gboolean CreateProcess (const gunichar2 *appname, const gunichar2 *cmdline,
                                g_message ("%s: Couldn't find executable %s",
                                           __func__, prog);
 #endif
-                               g_free (prog);
                                g_free (unquoted);
                                SetLastError (ERROR_FILE_NOT_FOUND);
                                goto cleanup;
@@ -548,7 +700,6 @@ gboolean CreateProcess (const gunichar2 *appname, const gunichar2 *cmdline,
                        char *curdir = g_get_current_dir ();
 
                        prog = g_strdup_printf ("%s/%s", curdir, unquoted);
-                       g_free (unquoted);
                        g_free (curdir);
 
                        /* And make sure it's executable */
@@ -557,11 +708,12 @@ gboolean CreateProcess (const gunichar2 *appname, const gunichar2 *cmdline,
                                g_message ("%s: Couldn't find executable %s",
                                           __func__, prog);
 #endif
-                               g_free (prog);
+                               g_free (unquoted);
                                SetLastError (ERROR_FILE_NOT_FOUND);
                                goto cleanup;
                        }
                }
+               g_free (unquoted);
 
                args_after_prog = args;
        } else {
@@ -651,7 +803,6 @@ gboolean CreateProcess (const gunichar2 *appname, const gunichar2 *cmdline,
                        
                        /* Executable existing ? */
                        if (access (prog, X_OK) != 0) {
-                               g_free (prog);
 #ifdef DEBUG
                                g_message ("%s: Couldn't find executable %s",
                                           __func__, token);
@@ -699,6 +850,32 @@ gboolean CreateProcess (const gunichar2 *appname, const gunichar2 *cmdline,
                   args_after_prog);
 #endif
        
+       /* Check for CLR binaries; if found, we will try to invoke
+        * them using the same mono binary that started us.
+        */
+       if (is_managed_binary (prog) && (appname == NULL)) {
+               gsize bytes_ignored;
+
+               appname = mono_unicode_from_external ("mono", &bytes_ignored);
+
+               if (appname != NULL) {
+                       cmdline = utf16_concat (appname, utf16_space, cmdline, NULL);
+                       
+                       g_free ((gunichar2 *)appname);
+                       
+                       if (cmdline != NULL) {
+                               gboolean return_value = CreateProcess (
+                                       NULL, cmdline, process_attrs,
+                                       thread_attrs, inherit_handles, create_flags, new_environ,
+                                       cwd, startup, process_info);
+                               
+                               g_free ((gunichar2 *)cmdline);
+                               
+                               return return_value;
+                       }
+               }
+       }
+
        if (args_after_prog != NULL && *args_after_prog) {
                gchar *qprog;
 
@@ -826,15 +1003,19 @@ gboolean CreateProcess (const gunichar2 *appname, const gunichar2 *cmdline,
        } 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);
+               if (_wapi_shm_disabled == FALSE) {
+                       /* 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
+                        * (), but we can't do this if we're not using
+                        * the shared memory
+                        */
+                       thr_ret = _wapi_handle_lock_shared_handles ();
+                       g_assert (thr_ret == 0);
        
-               _wapi_handle_unlock_shared_handles ();
+                       _wapi_handle_unlock_shared_handles ();
+               }
                
                /* should we detach from the process group? */
 
@@ -907,6 +1088,9 @@ cleanup:
                g_free (cmd);
        }
        if (full_prog != NULL) {
+               g_free (full_prog);
+       }
+       if (prog != NULL) {
                g_free (prog);
        }
        if (args != NULL) {
@@ -918,7 +1102,15 @@ cleanup:
        if(env_strings != NULL) {
                g_strfreev (env_strings);
        }
+       if (argv != NULL) {
+               g_strfreev (argv);
+       }
        
+#ifdef DEBUG
+       g_message ("%s: returning handle %p for pid %d", __func__, handle,
+                  pid);
+#endif
+
        return(ret);
 }
                
@@ -960,6 +1152,8 @@ static void process_set_current (void)
        const char *handle_env;
        struct _WapiHandle_process process_handle = {0};
        
+       mono_once (&process_ops_once, process_ops_init);
+       
        handle_env = g_getenv ("_WAPI_PROCESS_HANDLE_OFFSET");
        g_unsetenv ("_WAPI_PROCESS_HANDLE_OFFSET");