1 /**
2  * \file
3  * Runtime support for managed Semaphore on Unix
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 "w32semaphore.h"
12 
13 #include "w32error.h"
14 #include "w32handle-namespace.h"
15 #include "mono/utils/mono-logger-internals.h"
16 #include "mono/metadata/w32handle.h"
17 
18 #define MAX_PATH 260
19 
20 typedef struct {
21 	guint32 val;
22 	gint32 max;
23 } MonoW32HandleSemaphore;
24 
25 struct MonoW32HandleNamedSemaphore {
26 	MonoW32HandleSemaphore s;
27 	MonoW32HandleNamespace sharedns;
28 };
29 
sem_handle_signal(MonoW32Handle * handle_data)30 static void sem_handle_signal (MonoW32Handle *handle_data)
31 {
32 	MonoW32HandleSemaphore *sem_handle;
33 
34 	sem_handle = (MonoW32HandleSemaphore*) handle_data->specific;
35 
36 	mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_SEMAPHORE, "%s: signalling %s handle %p",
37 		__func__, mono_w32handle_get_typename (handle_data->type), handle_data);
38 
39 	/* No idea why max is signed, but thats the spec :-( */
40 	if (sem_handle->val + 1 > (guint32)sem_handle->max) {
41 		mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_SEMAPHORE, "%s: %s handle %p val %d count %d max %d, max value would be exceeded",
42 			__func__, mono_w32handle_get_typename (handle_data->type), handle_data, sem_handle->val, 1, sem_handle->max);
43 	} else {
44 		mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_SEMAPHORE, "%s: %s handle %p val %d count %d max %d",
45 			__func__, mono_w32handle_get_typename (handle_data->type), handle_data, sem_handle->val, 1, sem_handle->max);
46 
47 		sem_handle->val += 1;
48 		mono_w32handle_set_signal_state (handle_data, TRUE, TRUE);
49 	}
50 }
51 
sem_handle_own(MonoW32Handle * handle_data,gboolean * abandoned)52 static gboolean sem_handle_own (MonoW32Handle *handle_data, gboolean *abandoned)
53 {
54 	MonoW32HandleSemaphore *sem_handle;
55 
56 	*abandoned = FALSE;
57 
58 	sem_handle = (MonoW32HandleSemaphore*) handle_data->specific;
59 
60 	mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_SEMAPHORE, "%s: owning %s handle %p",
61 		__func__, mono_w32handle_get_typename (handle_data->type), handle_data);
62 
63 	sem_handle->val--;
64 
65 	if (sem_handle->val == 0)
66 		mono_w32handle_set_signal_state (handle_data, FALSE, FALSE);
67 
68 	return TRUE;
69 }
70 
sema_details(MonoW32Handle * handle_data)71 static void sema_details (MonoW32Handle *handle_data)
72 {
73 	MonoW32HandleSemaphore *sem = (MonoW32HandleSemaphore *)handle_data->specific;
74 	g_print ("val: %5u, max: %5d", sem->val, sem->max);
75 }
76 
namedsema_details(MonoW32Handle * handle_data)77 static void namedsema_details (MonoW32Handle *handle_data)
78 {
79 	MonoW32HandleNamedSemaphore *namedsem = (MonoW32HandleNamedSemaphore *)handle_data->specific;
80 	g_print ("val: %5u, max: %5d, name: \"%s\"", namedsem->s.val, namedsem->s.max, namedsem->sharedns.name);
81 }
82 
sema_typename(void)83 static const gchar* sema_typename (void)
84 {
85 	return "Semaphore";
86 }
87 
sema_typesize(void)88 static gsize sema_typesize (void)
89 {
90 	return sizeof (MonoW32HandleSemaphore);
91 }
92 
namedsema_typename(void)93 static const gchar* namedsema_typename (void)
94 {
95 	return "N.Semaphore";
96 }
97 
namedsema_typesize(void)98 static gsize namedsema_typesize (void)
99 {
100 	return sizeof (MonoW32HandleNamedSemaphore);
101 }
102 
103 void
mono_w32semaphore_init(void)104 mono_w32semaphore_init (void)
105 {
106 	static MonoW32HandleOps sem_ops = {
107 		NULL,			/* close */
108 		sem_handle_signal,		/* signal */
109 		sem_handle_own,		/* own */
110 		NULL,			/* is_owned */
111 		NULL,			/* special_wait */
112 		NULL,			/* prewait */
113 		sema_details,	/* details */
114 		sema_typename,	/* typename */
115 		sema_typesize,	/* typesize */
116 	};
117 
118 	static MonoW32HandleOps namedsem_ops = {
119 		NULL,			/* close */
120 		sem_handle_signal,	/* signal */
121 		sem_handle_own,		/* own */
122 		NULL,			/* is_owned */
123 		NULL,			/* special_wait */
124 		NULL,			/* prewait */
125 		namedsema_details,	/* details */
126 		namedsema_typename,	/* typename */
127 		namedsema_typesize,	/* typesize */
128 	};
129 
130 	mono_w32handle_register_ops (MONO_W32TYPE_SEM,      &sem_ops);
131 	mono_w32handle_register_ops (MONO_W32TYPE_NAMEDSEM, &namedsem_ops);
132 
133 	mono_w32handle_register_capabilities (MONO_W32TYPE_SEM,
134 		(MonoW32HandleCapability)(MONO_W32HANDLE_CAP_WAIT | MONO_W32HANDLE_CAP_SIGNAL));
135 	mono_w32handle_register_capabilities (MONO_W32TYPE_NAMEDSEM,
136 		(MonoW32HandleCapability)(MONO_W32HANDLE_CAP_WAIT | MONO_W32HANDLE_CAP_SIGNAL));
137 }
138 
139 static gpointer
sem_handle_create(MonoW32HandleSemaphore * sem_handle,MonoW32Type type,gint32 initial,gint32 max)140 sem_handle_create (MonoW32HandleSemaphore *sem_handle, MonoW32Type type, gint32 initial, gint32 max)
141 {
142 	MonoW32Handle *handle_data;
143 	gpointer handle;
144 
145 	sem_handle->val = initial;
146 	sem_handle->max = max;
147 
148 	handle = mono_w32handle_new (type, sem_handle);
149 	if (handle == INVALID_HANDLE_VALUE) {
150 		g_warning ("%s: error creating %s handle",
151 			__func__, mono_w32handle_get_typename (type));
152 		mono_w32error_set_last (ERROR_GEN_FAILURE);
153 		return NULL;
154 	}
155 
156 	if (!mono_w32handle_lookup_and_ref (handle, &handle_data))
157 		g_error ("%s: unkown handle %p", __func__, handle);
158 
159 	if (handle_data->type != type)
160 		g_error ("%s: unknown semaphore handle %p", __func__, handle);
161 
162 	mono_w32handle_lock (handle_data);
163 
164 	if (initial != 0)
165 		mono_w32handle_set_signal_state (handle_data, TRUE, FALSE);
166 
167 	mono_w32handle_unlock (handle_data);
168 
169 	mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_SEMAPHORE, "%s: created %s handle %p",
170 		__func__, mono_w32handle_get_typename (type), handle);
171 
172 	mono_w32handle_unref (handle_data);
173 
174 	return handle;
175 }
176 
177 static gpointer
sem_create(gint32 initial,gint32 max)178 sem_create (gint32 initial, gint32 max)
179 {
180 	MonoW32HandleSemaphore sem_handle;
181 	mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_SEMAPHORE, "%s: creating %s handle, initial %d max %d",
182 		__func__, mono_w32handle_get_typename (MONO_W32TYPE_SEM), initial, max);
183 	return sem_handle_create (&sem_handle, MONO_W32TYPE_SEM, initial, max);
184 }
185 
186 static gpointer
namedsem_create(gint32 initial,gint32 max,const gunichar2 * name)187 namedsem_create (gint32 initial, gint32 max, const gunichar2 *name)
188 {
189 	gpointer handle;
190 	gchar *utf8_name;
191 
192 	mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_SEMAPHORE, "%s: creating %s handle, initial %d max %d name \"%s\"",
193 		    __func__, mono_w32handle_get_typename (MONO_W32TYPE_NAMEDSEM), initial, max, (const char*)name);
194 
195 	/* w32 seems to guarantee that opening named objects can't race each other */
196 	mono_w32handle_namespace_lock ();
197 
198 	glong utf8_len = 0;
199 	utf8_name = g_utf16_to_utf8 (name, -1, NULL, &utf8_len, NULL);
200 
201 	mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_SEMAPHORE, "%s: Creating named sem name [%s] initial %d max %d", __func__, utf8_name, initial, max);
202 
203 	handle = mono_w32handle_namespace_search_handle (MONO_W32TYPE_NAMEDSEM, utf8_name);
204 	if (handle == INVALID_HANDLE_VALUE) {
205 		/* The name has already been used for a different object. */
206 		handle = NULL;
207 		mono_w32error_set_last (ERROR_INVALID_HANDLE);
208 	} else if (handle) {
209 		/* Not an error, but this is how the caller is informed that the semaphore wasn't freshly created */
210 		mono_w32error_set_last (ERROR_ALREADY_EXISTS);
211 
212 		/* mono_w32handle_namespace_search_handle already adds a ref to the handle */
213 	} else {
214 		/* A new named semaphore */
215 		MonoW32HandleNamedSemaphore namedsem_handle;
216 
217 		size_t len = utf8_len < MAX_PATH ? utf8_len : MAX_PATH;
218 		memcpy (&namedsem_handle.sharedns.name [0], utf8_name, len);
219 		namedsem_handle.sharedns.name [len] = '\0';
220 
221 		handle = sem_handle_create ((MonoW32HandleSemaphore*) &namedsem_handle, MONO_W32TYPE_NAMEDSEM, initial, max);
222 	}
223 
224 	g_free (utf8_name);
225 
226 	mono_w32handle_namespace_unlock ();
227 
228 	return handle;
229 }
230 
231 gpointer
ves_icall_System_Threading_Semaphore_CreateSemaphore_internal(gint32 initialCount,gint32 maximumCount,MonoString * name,gint32 * error)232 ves_icall_System_Threading_Semaphore_CreateSemaphore_internal (gint32 initialCount, gint32 maximumCount, MonoString *name, gint32 *error)
233 {
234 	gpointer sem;
235 
236 	if (maximumCount <= 0) {
237 		mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_SEMAPHORE, "%s: maximumCount <= 0", __func__);
238 
239 		*error = ERROR_INVALID_PARAMETER;
240 		return NULL;
241 	}
242 
243 	if (initialCount > maximumCount || initialCount < 0) {
244 		mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_SEMAPHORE, "%s: initialCount > maximumCount or < 0", __func__);
245 
246 		*error = ERROR_INVALID_PARAMETER;
247 		return NULL;
248 	}
249 
250 	/* Need to blow away any old errors here, because code tests
251 	 * for ERROR_ALREADY_EXISTS on success (!) to see if a
252 	 * semaphore was freshly created
253 	 */
254 	mono_w32error_set_last (ERROR_SUCCESS);
255 
256 	if (!name)
257 		sem = sem_create (initialCount, maximumCount);
258 	else
259 		sem = namedsem_create (initialCount, maximumCount, mono_string_chars (name));
260 
261 	*error = mono_w32error_get_last ();
262 
263 	return sem;
264 }
265 
266 MonoBoolean
ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal(gpointer handle,gint32 releaseCount,gint32 * prevcount)267 ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal (gpointer handle, gint32 releaseCount, gint32 *prevcount)
268 {
269 	MonoW32Handle *handle_data;
270 	MonoW32HandleSemaphore *sem_handle;
271 	MonoBoolean ret;
272 
273 	if (!mono_w32handle_lookup_and_ref (handle, &handle_data)) {
274 		g_warning ("%s: unkown handle %p", __func__, handle);
275 		mono_w32error_set_last (ERROR_INVALID_HANDLE);
276 		return FALSE;
277 	}
278 
279 	if (handle_data->type != MONO_W32TYPE_SEM && handle_data->type != MONO_W32TYPE_NAMEDSEM) {
280 		g_warning ("%s: unknown sem handle %p", __func__, handle);
281 		mono_w32error_set_last (ERROR_INVALID_HANDLE);
282 		mono_w32handle_unref (handle_data);
283 		return FALSE;
284 	}
285 
286 	sem_handle = (MonoW32HandleSemaphore*) handle_data->specific;
287 
288 	mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_SEMAPHORE, "%s: releasing %s handle %p",
289 		__func__, mono_w32handle_get_typename (handle_data->type), handle);
290 
291 	mono_w32handle_lock (handle_data);
292 
293 	/* Do this before checking for count overflow, because overflowing
294 	 * max is a listed technique for finding the current value */
295 	if (prevcount)
296 		*prevcount = sem_handle->val;
297 
298 	/* No idea why max is signed, but thats the spec :-( */
299 	if (sem_handle->val + releaseCount > (guint32)sem_handle->max) {
300 		mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_SEMAPHORE, "%s: %s handle %p val %d count %d max %d, max value would be exceeded",
301 			__func__, mono_w32handle_get_typename (handle_data->type), handle, sem_handle->val, releaseCount, sem_handle->max);
302 
303 		ret = FALSE;
304 	} else {
305 		mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_SEMAPHORE, "%s: %s handle %p val %d count %d max %d",
306 			__func__, mono_w32handle_get_typename (handle_data->type), handle, sem_handle->val, releaseCount, sem_handle->max);
307 
308 		sem_handle->val += releaseCount;
309 		mono_w32handle_set_signal_state (handle_data, TRUE, TRUE);
310 
311 		ret = TRUE;
312 	}
313 
314 	mono_w32handle_unlock (handle_data);
315 	mono_w32handle_unref (handle_data);
316 
317 	return ret;
318 }
319 
320 gpointer
ves_icall_System_Threading_Semaphore_OpenSemaphore_internal(MonoString * name,gint32 rights,gint32 * error)321 ves_icall_System_Threading_Semaphore_OpenSemaphore_internal (MonoString *name, gint32 rights, gint32 *error)
322 {
323 	gpointer handle;
324 	gchar *utf8_name;
325 
326 	*error = ERROR_SUCCESS;
327 
328 	/* w32 seems to guarantee that opening named objects can't race each other */
329 	mono_w32handle_namespace_lock ();
330 
331 	utf8_name = g_utf16_to_utf8 (mono_string_chars (name), -1, NULL, NULL, NULL);
332 
333 	mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_SEMAPHORE, "%s: Opening named sem [%s]", __func__, utf8_name);
334 
335 	handle = mono_w32handle_namespace_search_handle (MONO_W32TYPE_NAMEDSEM, utf8_name);
336 	if (handle == INVALID_HANDLE_VALUE) {
337 		/* The name has already been used for a different object. */
338 		*error = ERROR_INVALID_HANDLE;
339 		goto cleanup;
340 	} else if (!handle) {
341 		/* This name doesn't exist */
342 		*error = ERROR_FILE_NOT_FOUND;
343 		goto cleanup;
344 	}
345 
346 	mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_SEMAPHORE, "%s: returning named sem handle %p", __func__, handle);
347 
348 cleanup:
349 	g_free (utf8_name);
350 
351 	mono_w32handle_namespace_unlock ();
352 
353 	return handle;
354 }
355 
356 MonoW32HandleNamespace*
mono_w32semaphore_get_namespace(MonoW32HandleNamedSemaphore * semaphore)357 mono_w32semaphore_get_namespace (MonoW32HandleNamedSemaphore *semaphore)
358 {
359 	return &semaphore->sharedns;
360 }
361