4 #include "process-private.h"
5 #include "thread-private.h"
8 #include "mono/utils/mono-lazy-init.h"
9 #include "mono/utils/w32handle.h"
11 gboolean _wapi_has_shut_down = FALSE;
16 _wapi_shm_semaphores_init ();
18 _wapi_processes_init ();
20 _wapi_semaphore_init ();
29 g_assert (_wapi_has_shut_down == FALSE);
30 _wapi_has_shut_down = TRUE;
32 _wapi_error_cleanup ();
33 _wapi_thread_cleanup ();
34 wapi_processes_cleanup ();
38 /* Use this instead of getpid(), to cope with linuxthreads. It's a
39 * function rather than a variable lookup because we need to get at
40 * this before share_init() might have been called. */
41 static mono_lazy_init_t _wapi_pid_init_lazy = MONO_LAZY_INIT_STATUS_NOT_INITIALIZED;
42 static pid_t _wapi_pid;
47 _wapi_pid = getpid ();
53 mono_lazy_initialize (&_wapi_pid_init_lazy, _wapi_pid_init);
58 _WAPI_SHARED_NAMESPACE (MonoW32HandleType type)
61 case MONO_W32HANDLE_NAMEDMUTEX:
62 case MONO_W32HANDLE_NAMEDSEM:
63 case MONO_W32HANDLE_NAMEDEVENT:
72 MonoW32HandleType type;
74 } _WapiSearchHandleNamespaceData;
76 static gboolean mono_w32handle_search_namespace_callback (gpointer handle, gpointer data, gpointer user_data)
78 _WapiSearchHandleNamespaceData *search_data;
79 MonoW32HandleType type;
80 WapiSharedNamespace *sharedns;
82 type = mono_w32handle_get_type (handle);
83 if (!_WAPI_SHARED_NAMESPACE (type))
86 search_data = (_WapiSearchHandleNamespaceData*) user_data;
89 case MONO_W32HANDLE_NAMEDMUTEX: sharedns = &((struct _WapiHandle_namedmutex*) data)->sharedns; break;
90 case MONO_W32HANDLE_NAMEDSEM: sharedns = &((struct _WapiHandle_namedsem*) data)->sharedns; break;
91 case MONO_W32HANDLE_NAMEDEVENT: sharedns = &((struct _WapiHandle_namedevent*) data)->sharedns; break;
93 g_assert_not_reached ();
96 if (strcmp (sharedns->name, search_data->utf8_name) == 0) {
97 if (type != search_data->type) {
98 /* Its the wrong type, so fail now */
99 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p matches name but is wrong type: %s",
100 __func__, handle, mono_w32handle_ops_typename (type));
101 search_data->ret = INVALID_HANDLE_VALUE;
103 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p matches name and type",
105 search_data->ret = handle;
114 /* Returns the offset of the metadata array, or INVALID_HANDLE_VALUE on error, or NULL for
117 gpointer _wapi_search_handle_namespace (MonoW32HandleType type, gchar *utf8_name)
119 _WapiSearchHandleNamespaceData search_data;
121 g_assert(_WAPI_SHARED_NAMESPACE(type));
123 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Lookup for handle named [%s] type %s",
124 __func__, utf8_name, mono_w32handle_ops_typename (type));
126 search_data.ret = NULL;
127 search_data.type = type;
128 search_data.utf8_name = utf8_name;
129 mono_w32handle_foreach (mono_w32handle_search_namespace_callback, &search_data);
130 return search_data.ret;
133 /* Lots more to implement here, but this is all we need at the moment */
135 DuplicateHandle (gpointer srcprocess, gpointer src, gpointer targetprocess, gpointer *target,
136 guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, guint32 options G_GNUC_UNUSED)
138 if (srcprocess != _WAPI_PROCESS_CURRENT || targetprocess != _WAPI_PROCESS_CURRENT) {
139 /* Duplicating other process's handles is not supported */
140 SetLastError (ERROR_INVALID_HANDLE);
144 if (src == _WAPI_PROCESS_CURRENT) {
145 *target = _wapi_process_duplicate ();
146 } else if (src == _WAPI_THREAD_CURRENT) {
147 g_assert_not_reached ();
149 mono_w32handle_ref (src);
158 * @handle: The handle to release
160 * Closes and invalidates @handle, releasing any resources it
161 * consumes. When the last handle to a temporary or non-persistent
162 * object is closed, that object can be deleted. Closing the same
163 * handle twice is an error.
165 * Return value: %TRUE on success, %FALSE otherwise.
167 gboolean CloseHandle(gpointer handle)
169 if (handle == INVALID_HANDLE_VALUE){
170 SetLastError (ERROR_INVALID_PARAMETER);
173 if (handle == (gpointer)0 && mono_w32handle_get_type (handle) != MONO_W32HANDLE_CONSOLE) {
174 /* Problem: because we map file descriptors to the
175 * same-numbered handle we can't tell the difference
176 * between a bogus handle and the handle to stdin.
177 * Assume that it's the console handle if that handle
180 SetLastError (ERROR_INVALID_PARAMETER);
184 mono_w32handle_unref (handle);