1 /**
2  * \file
3  * namespace for w32handles
4  *
5  * Author:
6  *	Ludovic Henry (luhenry@microsoft.com)
7  *
8  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
9  */
10 
11 #include <config.h>
12 
13 #ifndef HOST_WIN32
14 
15 #include "w32handle-namespace.h"
16 
17 #include "w32mutex.h"
18 #include "w32semaphore.h"
19 #include "w32event.h"
20 #include "mono/utils/mono-logger-internals.h"
21 #include "mono/utils/mono-coop-mutex.h"
22 
23 static MonoCoopMutex lock;
24 
25 void
mono_w32handle_namespace_init(void)26 mono_w32handle_namespace_init (void)
27 {
28 	mono_coop_mutex_init (&lock);
29 }
30 
31 void
mono_w32handle_namespace_lock(void)32 mono_w32handle_namespace_lock (void)
33 {
34 	mono_coop_mutex_lock (&lock);
35 }
36 
37 void
mono_w32handle_namespace_unlock(void)38 mono_w32handle_namespace_unlock (void)
39 {
40 	mono_coop_mutex_unlock (&lock);
41 }
42 
43 static gboolean
has_namespace(MonoW32Type type)44 has_namespace (MonoW32Type type)
45 {
46 	switch (type) {
47 	case MONO_W32TYPE_NAMEDMUTEX:
48 	case MONO_W32TYPE_NAMEDSEM:
49 	case MONO_W32TYPE_NAMEDEVENT:
50 		return TRUE;
51 	default:
52 		return FALSE;
53 	}
54 }
55 
56 typedef struct {
57 	gpointer ret;
58 	MonoW32Type type;
59 	const gchar *name;
60 } NamespaceSearchHandleData;
61 
62 static gboolean
mono_w32handle_namespace_search_handle_callback(MonoW32Handle * handle_data,gpointer user_data)63 mono_w32handle_namespace_search_handle_callback (MonoW32Handle *handle_data, gpointer user_data)
64 {
65 	NamespaceSearchHandleData *search_data;
66 	MonoW32HandleNamespace *sharedns;
67 
68 	if (!has_namespace (handle_data->type))
69 		return FALSE;
70 
71 	search_data = (NamespaceSearchHandleData*) user_data;
72 
73 	switch (handle_data->type) {
74 	case MONO_W32TYPE_NAMEDMUTEX: sharedns = mono_w32mutex_get_namespace ((MonoW32HandleNamedMutex*) handle_data->specific); break;
75 	case MONO_W32TYPE_NAMEDSEM:   sharedns = mono_w32semaphore_get_namespace ((MonoW32HandleNamedSemaphore*) handle_data->specific); break;
76 	case MONO_W32TYPE_NAMEDEVENT: sharedns = mono_w32event_get_namespace ((MonoW32HandleNamedEvent*) handle_data->specific); break;
77 	default:
78 		g_assert_not_reached ();
79 	}
80 
81 	if (strcmp (sharedns->name, search_data->name) == 0) {
82 		if (handle_data->type != search_data->type) {
83 			/* Its the wrong type, so fail now */
84 			mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_HANDLE, "%s: handle %p matches name but is wrong type: %s",
85 				__func__, handle_data, mono_w32handle_get_typename (handle_data->type));
86 			search_data->ret = INVALID_HANDLE_VALUE;
87 		} else {
88 			mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_HANDLE, "%s: handle %p matches name and type",
89 				__func__, handle_data);
90 
91 			/* we do not want the handle to be destroyed before we return it  */
92 			search_data->ret = mono_w32handle_duplicate (handle_data);
93 		}
94 
95 		return TRUE;
96 	}
97 
98 	return FALSE;
99 }
100 
101 gpointer
mono_w32handle_namespace_search_handle(MonoW32Type type,const gchar * name)102 mono_w32handle_namespace_search_handle (MonoW32Type type, const gchar *name)
103 {
104 	NamespaceSearchHandleData search_data;
105 
106 	if (!has_namespace (type))
107 		g_error ("%s: type %s does not have a namespace", __func__, type);
108 
109 	mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_HANDLE, "%s: Lookup for handle named [%s] type %s",
110 		__func__, name, mono_w32handle_get_typename (type));
111 
112 	search_data.ret = NULL;
113 	search_data.type = type;
114 	search_data.name = name;
115 	mono_w32handle_foreach (mono_w32handle_namespace_search_handle_callback, &search_data);
116 	return search_data.ret;
117 }
118 
119 #endif
120