Merge pull request #3786 from lambdageek/dev-42584-take2
[mono.git] / mono / io-layer / wapi.c
index e8e8dbcd6f8400579404e316356bff96cebfd8cb..f1674179d9a194f0d2dcb4d992acbf5bb6926d3c 100644 (file)
@@ -1,21 +1,22 @@
 
 #include "wapi.h"
 
-#include "handles-private.h"
+#include "io-trace.h"
+#include "io.h"
 #include "process-private.h"
-#include "thread-private.h"
+#include "socket-private.h"
 
 #include "mono/utils/mono-lazy-init.h"
+#include "mono/utils/w32handle.h"
 
 gboolean _wapi_has_shut_down = FALSE;
 
 void
 wapi_init (void)
 {
-       _wapi_handle_init ();
-       _wapi_shm_semaphores_init ();
        _wapi_io_init ();
-       wapi_processes_init ();
+       _wapi_processes_init ();
+       _wapi_socket_init ();
 }
 
 void
@@ -25,10 +26,8 @@ wapi_cleanup (void)
        _wapi_has_shut_down = TRUE;
 
        _wapi_error_cleanup ();
-       _wapi_thread_cleanup ();
        wapi_processes_cleanup ();
        _wapi_io_cleanup ();
-       _wapi_handle_cleanup ();
 }
 
 /* Use this instead of getpid(), to cope with linuxthreads.  It's a
@@ -44,8 +43,61 @@ _wapi_pid_init (void)
 }
 
 pid_t
-_wapi_getpid (void)
+wapi_getpid (void)
 {
        mono_lazy_initialize (&_wapi_pid_init_lazy, _wapi_pid_init);
        return _wapi_pid;
 }
+
+/* Lots more to implement here, but this is all we need at the moment */
+gboolean
+DuplicateHandle (gpointer srcprocess, gpointer src, gpointer targetprocess, gpointer *target,
+       guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, guint32 options G_GNUC_UNUSED)
+{
+       if (srcprocess != _WAPI_PROCESS_CURRENT || targetprocess != _WAPI_PROCESS_CURRENT) {
+               /* Duplicating other process's handles is not supported */
+               SetLastError (ERROR_INVALID_HANDLE);
+               return FALSE;
+       }
+
+       if (src == _WAPI_PROCESS_CURRENT) {
+               *target = _wapi_process_duplicate ();
+       } else {
+               mono_w32handle_ref (src);
+               *target = src;
+       }
+
+       return TRUE;
+}
+
+/**
+ * CloseHandle:
+ * @handle: The handle to release
+ *
+ * Closes and invalidates @handle, releasing any resources it
+ * consumes.  When the last handle to a temporary or non-persistent
+ * object is closed, that object can be deleted.  Closing the same
+ * handle twice is an error.
+ *
+ * Return value: %TRUE on success, %FALSE otherwise.
+ */
+gboolean CloseHandle(gpointer handle)
+{
+       if (handle == INVALID_HANDLE_VALUE){
+               SetLastError (ERROR_INVALID_PARAMETER);
+               return FALSE;
+       }
+       if (handle == (gpointer)0 && mono_w32handle_get_type (handle) != MONO_W32HANDLE_CONSOLE) {
+               /* Problem: because we map file descriptors to the
+                * same-numbered handle we can't tell the difference
+                * between a bogus handle and the handle to stdin.
+                * Assume that it's the console handle if that handle
+                * exists...
+                */
+               SetLastError (ERROR_INVALID_PARAMETER);
+               return FALSE;
+       }
+
+       mono_w32handle_unref (handle);
+       return TRUE;
+}