4 #include "event-private.h"
7 #include "mutex-private.h"
8 #include "process-private.h"
9 #include "semaphore-private.h"
11 #include "socket-private.h"
13 #include "mono/utils/mono-lazy-init.h"
14 #include "mono/utils/w32handle.h"
16 gboolean _wapi_has_shut_down = FALSE;
21 _wapi_shm_semaphores_init ();
23 _wapi_processes_init ();
24 _wapi_semaphore_init ();
33 g_assert (_wapi_has_shut_down == FALSE);
34 _wapi_has_shut_down = TRUE;
36 _wapi_error_cleanup ();
37 wapi_processes_cleanup ();
41 /* Use this instead of getpid(), to cope with linuxthreads. It's a
42 * function rather than a variable lookup because we need to get at
43 * this before share_init() might have been called. */
44 static mono_lazy_init_t _wapi_pid_init_lazy = MONO_LAZY_INIT_STATUS_NOT_INITIALIZED;
45 static pid_t _wapi_pid;
50 _wapi_pid = getpid ();
56 mono_lazy_initialize (&_wapi_pid_init_lazy, _wapi_pid_init);
61 _WAPI_SHARED_NAMESPACE (MonoW32HandleType type)
64 case MONO_W32HANDLE_NAMEDMUTEX:
65 case MONO_W32HANDLE_NAMEDSEM:
66 case MONO_W32HANDLE_NAMEDEVENT:
75 MonoW32HandleType type;
77 } _WapiSearchHandleNamespaceData;
79 static gboolean mono_w32handle_search_namespace_callback (gpointer handle, gpointer data, gpointer user_data)
81 _WapiSearchHandleNamespaceData *search_data;
82 MonoW32HandleType type;
83 WapiSharedNamespace *sharedns;
85 type = mono_w32handle_get_type (handle);
86 if (!_WAPI_SHARED_NAMESPACE (type))
89 search_data = (_WapiSearchHandleNamespaceData*) user_data;
92 case MONO_W32HANDLE_NAMEDMUTEX: sharedns = &((struct _WapiHandle_namedmutex*) data)->sharedns; break;
93 case MONO_W32HANDLE_NAMEDSEM: sharedns = &((struct _WapiHandle_namedsem*) data)->sharedns; break;
94 case MONO_W32HANDLE_NAMEDEVENT: sharedns = &((struct _WapiHandle_namedevent*) data)->sharedns; break;
96 g_assert_not_reached ();
99 if (strcmp (sharedns->name, search_data->utf8_name) == 0) {
100 if (type != search_data->type) {
101 /* Its the wrong type, so fail now */
102 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p matches name but is wrong type: %s",
103 __func__, handle, mono_w32handle_ops_typename (type));
104 search_data->ret = INVALID_HANDLE_VALUE;
106 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p matches name and type",
108 search_data->ret = handle;
117 /* Returns the offset of the metadata array, or INVALID_HANDLE_VALUE on error, or NULL for
120 gpointer _wapi_search_handle_namespace (MonoW32HandleType type, gchar *utf8_name)
122 _WapiSearchHandleNamespaceData search_data;
124 g_assert(_WAPI_SHARED_NAMESPACE(type));
126 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Lookup for handle named [%s] type %s",
127 __func__, utf8_name, mono_w32handle_ops_typename (type));
129 search_data.ret = NULL;
130 search_data.type = type;
131 search_data.utf8_name = utf8_name;
132 mono_w32handle_foreach (mono_w32handle_search_namespace_callback, &search_data);
133 return search_data.ret;
136 /* Lots more to implement here, but this is all we need at the moment */
138 DuplicateHandle (gpointer srcprocess, gpointer src, gpointer targetprocess, gpointer *target,
139 guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, guint32 options G_GNUC_UNUSED)
141 if (srcprocess != _WAPI_PROCESS_CURRENT || targetprocess != _WAPI_PROCESS_CURRENT) {
142 /* Duplicating other process's handles is not supported */
143 SetLastError (ERROR_INVALID_HANDLE);
147 if (src == _WAPI_PROCESS_CURRENT) {
148 *target = _wapi_process_duplicate ();
150 mono_w32handle_ref (src);
159 * @handle: The handle to release
161 * Closes and invalidates @handle, releasing any resources it
162 * consumes. When the last handle to a temporary or non-persistent
163 * object is closed, that object can be deleted. Closing the same
164 * handle twice is an error.
166 * Return value: %TRUE on success, %FALSE otherwise.
168 gboolean CloseHandle(gpointer handle)
170 if (handle == INVALID_HANDLE_VALUE){
171 SetLastError (ERROR_INVALID_PARAMETER);
174 if (handle == (gpointer)0 && mono_w32handle_get_type (handle) != MONO_W32HANDLE_CONSOLE) {
175 /* Problem: because we map file descriptors to the
176 * same-numbered handle we can't tell the difference
177 * between a bogus handle and the handle to stdin.
178 * Assume that it's the console handle if that handle
181 SetLastError (ERROR_INVALID_PARAMETER);
185 mono_w32handle_unref (handle);