[2020-02] Fix leak in assembly-specific dllmap lookups (#21053)
[mono-project.git] / mono / metadata / w32handle-namespace.c
blobc141b28909fd63bf9bf3b441f1e0d214824f377e
1 /**
2 * \file
3 * namespace for w32handles
5 * Author:
6 * Ludovic Henry (luhenry@microsoft.com)
8 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
9 */
11 #include <config.h>
13 #ifndef HOST_WIN32
15 #include "w32handle-namespace.h"
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"
23 static MonoCoopMutex lock;
25 void
26 mono_w32handle_namespace_init (void)
28 mono_coop_mutex_init (&lock);
31 void
32 mono_w32handle_namespace_lock (void)
34 mono_coop_mutex_lock (&lock);
37 void
38 mono_w32handle_namespace_unlock (void)
40 mono_coop_mutex_unlock (&lock);
43 static gboolean
44 has_namespace (MonoW32Type type)
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;
56 typedef struct {
57 gpointer ret;
58 MonoW32Type type;
59 const gchar *name;
60 } NamespaceSearchHandleData;
62 static gboolean
63 mono_w32handle_namespace_search_handle_callback (MonoW32Handle *handle_data, gpointer user_data)
65 NamespaceSearchHandleData *search_data;
66 MonoW32HandleNamespace *sharedns;
68 if (!has_namespace (handle_data->type))
69 return FALSE;
71 search_data = (NamespaceSearchHandleData*) user_data;
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 ();
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);
91 /* we do not want the handle to be destroyed before we return it */
92 search_data->ret = mono_w32handle_duplicate (handle_data);
95 return TRUE;
98 return FALSE;
101 gpointer
102 mono_w32handle_namespace_search_handle (MonoW32Type type, const gchar *name)
104 NamespaceSearchHandleData search_data;
106 if (!has_namespace (type))
107 g_error ("%s: type %s does not have a namespace", __func__, mono_w32handle_get_typename (type));
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));
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;
119 #endif