2 * handles.c: Generic and internal operations on handles
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002-2011 Novell, Inc.
8 * Copyright 2011 Xamarin Inc
9 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
21 #include <sys/types.h>
22 #ifdef HAVE_SYS_SOCKET_H
23 # include <sys/socket.h>
28 #ifdef HAVE_SYS_MMAN_H
29 # include <sys/mman.h>
35 #ifdef HAVE_SYS_RESOURCE_H
36 # include <sys/resource.h>
39 #include <mono/io-layer/wapi.h>
40 #include <mono/io-layer/wapi-private.h>
41 #include <mono/io-layer/handles-private.h>
42 #include <mono/io-layer/shared.h>
43 #include <mono/io-layer/process-private.h>
44 #include <mono/io-layer/io-trace.h>
46 #include <mono/utils/mono-os-mutex.h>
47 #include <mono/utils/mono-proclib.h>
48 #include <mono/utils/mono-threads.h>
49 #include <mono/utils/mono-once.h>
50 #include <mono/utils/mono-logger-internals.h>
53 static void (*_wapi_handle_ops_get_close_func (WapiHandleType type))(gpointer, gpointer);
55 static WapiHandleCapability handle_caps[WAPI_HANDLE_COUNT] = { (WapiHandleCapability)0 };
56 static struct _WapiHandleOps *handle_ops[WAPI_HANDLE_COUNT]={
64 #ifndef DISABLE_SOCKETS
70 &_wapi_namedmutex_ops,
72 &_wapi_namedevent_ops,
75 static void _wapi_shared_details (gpointer handle_info);
77 static void (*handle_details[WAPI_HANDLE_COUNT])(gpointer) = {
80 _wapi_console_details,
81 _wapi_shared_details, /* thread */
85 NULL, /* Nothing useful to see in a socket handle */
86 NULL, /* Nothing useful to see in a find handle */
87 _wapi_shared_details, /* process */
89 _wapi_shared_details, /* namedmutex */
90 _wapi_shared_details, /* namedsem */
91 _wapi_shared_details, /* namedevent */
94 const char *_wapi_handle_typename[] = {
113 * We can hold _WAPI_PRIVATE_MAX_SLOTS * _WAPI_HANDLE_INITIAL_COUNT handles.
114 * If 4M handles are not enough... Oh, well... we will crash.
116 #define SLOT_INDEX(x) (x / _WAPI_HANDLE_INITIAL_COUNT)
117 #define SLOT_OFFSET(x) (x % _WAPI_HANDLE_INITIAL_COUNT)
119 struct _WapiHandleUnshared *_wapi_private_handles [_WAPI_PRIVATE_MAX_SLOTS];
120 static guint32 _wapi_private_handle_count = 0;
121 static guint32 _wapi_private_handle_slot_count = 0;
123 struct _WapiHandleSharedLayout *_wapi_shared_layout = NULL;
126 * If SHM is disabled, this will point to a hash of _WapiFileShare structures, otherwise
127 * it will be NULL. We use this instead of _wapi_fileshare_layout to avoid allocating a
130 static GHashTable *file_share_hash;
131 static mono_mutex_t file_share_hash_mutex;
133 #define file_share_hash_lock() mono_os_mutex_lock (&file_share_hash_mutex)
134 #define file_share_hash_unlock() mono_os_mutex_unlock (&file_share_hash_mutex)
136 guint32 _wapi_fd_reserve;
139 * This is an internal handle which is used for handling waiting for multiple handles.
140 * Threads which wait for multiple handles wait on this one handle, and when a handle
141 * is signalled, this handle is signalled too.
143 gpointer _wapi_global_signal_handle;
145 /* Point to the mutex/cond inside _wapi_global_signal_handle */
146 mono_mutex_t *_wapi_global_signal_mutex;
147 pthread_cond_t *_wapi_global_signal_cond;
150 gboolean _wapi_has_shut_down = FALSE;
152 /* Use this instead of getpid(), to cope with linuxthreads. It's a
153 * function rather than a variable lookup because we need to get at
154 * this before share_init() might have been called.
156 static pid_t _wapi_pid;
157 static mono_once_t pid_init_once = MONO_ONCE_INIT;
159 static void _wapi_handle_unref_full (gpointer handle, gboolean ignore_private_busy_handles);
161 static void pid_init (void)
163 _wapi_pid = getpid ();
166 pid_t _wapi_getpid (void)
168 mono_once (&pid_init_once, pid_init);
174 static mono_mutex_t scan_mutex;
176 static void handle_cleanup (void)
180 /* Every shared handle we were using ought really to be closed
181 * by now, but to make sure just blow them all away. The
182 * exiting finalizer thread in particular races us to the
183 * program exit and doesn't always win, so it can be left
184 * cluttering up the shared file. Anything else left over is
187 for(i = SLOT_INDEX (0); _wapi_private_handles[i] != NULL; i++) {
188 for(j = SLOT_OFFSET (0); j < _WAPI_HANDLE_INITIAL_COUNT; j++) {
189 struct _WapiHandleUnshared *handle_data = &_wapi_private_handles[i][j];
190 gpointer handle = GINT_TO_POINTER (i*_WAPI_HANDLE_INITIAL_COUNT+j);
192 for(k = handle_data->ref; k > 0; k--) {
193 _wapi_handle_unref_full (handle, TRUE);
198 _wapi_shm_semaphores_remove ();
200 g_free (_wapi_shared_layout);
202 if (file_share_hash) {
203 g_hash_table_destroy (file_share_hash);
204 mono_os_mutex_destroy (&file_share_hash_mutex);
207 for (i = 0; i < _WAPI_PRIVATE_MAX_SLOTS; ++i)
208 g_free (_wapi_private_handles [i]);
212 wapi_getdtablesize (void)
214 return eg_getdtablesize ();
220 * Initialize the io-layer.
225 g_assert ((sizeof (handle_ops) / sizeof (handle_ops[0]))
226 == WAPI_HANDLE_COUNT);
228 _wapi_fd_reserve = wapi_getdtablesize ();
230 /* This is needed by the code in _wapi_handle_new_internal */
231 _wapi_fd_reserve = (_wapi_fd_reserve + (_WAPI_HANDLE_INITIAL_COUNT - 1)) & ~(_WAPI_HANDLE_INITIAL_COUNT - 1);
235 * The entries in _wapi_private_handles reserved for fds are allocated lazily to
239 _wapi_private_handles [idx++] = g_new0 (struct _WapiHandleUnshared,
240 _WAPI_HANDLE_INITIAL_COUNT);
243 _wapi_private_handle_count += _WAPI_HANDLE_INITIAL_COUNT;
244 _wapi_private_handle_slot_count ++;
245 } while(_wapi_fd_reserve > _wapi_private_handle_count);
247 _wapi_shm_semaphores_init ();
249 _wapi_shared_layout = (_WapiHandleSharedLayout *)g_malloc0 (sizeof (_WapiHandleSharedLayout));
250 g_assert (_wapi_shared_layout != NULL);
253 mono_os_mutex_init (&scan_mutex);
255 _wapi_global_signal_handle = _wapi_handle_new (WAPI_HANDLE_EVENT, NULL);
257 _wapi_global_signal_cond = &_WAPI_PRIVATE_HANDLES (GPOINTER_TO_UINT (_wapi_global_signal_handle)).signal_cond;
258 _wapi_global_signal_mutex = &_WAPI_PRIVATE_HANDLES (GPOINTER_TO_UINT (_wapi_global_signal_handle)).signal_mutex;
260 wapi_processes_init ();
266 g_assert (_wapi_has_shut_down == FALSE);
268 _wapi_has_shut_down = TRUE;
270 _wapi_error_cleanup ();
271 _wapi_thread_cleanup ();
272 wapi_processes_cleanup ();
276 static void _wapi_handle_init_shared (struct _WapiHandleUnshared *handle,
278 gpointer handle_specific)
280 g_assert (_wapi_has_shut_down == FALSE);
283 handle->signalled = FALSE;
286 if (handle_specific != NULL) {
287 memcpy (&handle->u, handle_specific, sizeof (handle->u));
291 static size_t _wapi_handle_struct_size (WapiHandleType type)
296 case WAPI_HANDLE_FILE: case WAPI_HANDLE_CONSOLE: case WAPI_HANDLE_PIPE:
297 type_size = sizeof (struct _WapiHandle_file);
299 case WAPI_HANDLE_THREAD:
300 type_size = sizeof (struct _WapiHandle_thread);
302 case WAPI_HANDLE_SEM:
303 type_size = sizeof (struct _WapiHandle_sem);
305 case WAPI_HANDLE_MUTEX:
306 type_size = sizeof (struct _WapiHandle_mutex);
308 case WAPI_HANDLE_EVENT:
309 type_size = sizeof (struct _WapiHandle_event);
311 case WAPI_HANDLE_SOCKET:
312 type_size = sizeof (struct _WapiHandle_socket);
314 case WAPI_HANDLE_FIND:
315 type_size = sizeof (struct _WapiHandle_find);
317 case WAPI_HANDLE_PROCESS:
318 type_size = sizeof (struct _WapiHandle_process);
320 case WAPI_HANDLE_NAMEDMUTEX:
321 type_size = sizeof (struct _WapiHandle_namedmutex);
323 case WAPI_HANDLE_NAMEDSEM:
324 type_size = sizeof (struct _WapiHandle_namedsem);
326 case WAPI_HANDLE_NAMEDEVENT:
327 type_size = sizeof (struct _WapiHandle_namedevent);
331 g_error ("Unknown WapiHandleType: %d\n", type);
337 static void _wapi_handle_init (struct _WapiHandleUnshared *handle,
338 WapiHandleType type, gpointer handle_specific)
343 g_assert (_wapi_has_shut_down == FALSE);
346 handle->signalled = FALSE;
349 if (!_WAPI_SHARED_HANDLE(type)) {
350 thr_ret = pthread_cond_init (&handle->signal_cond, NULL);
351 g_assert (thr_ret == 0);
353 thr_ret = mono_os_mutex_init (&handle->signal_mutex);
354 g_assert (thr_ret == 0);
356 if (handle_specific != NULL) {
357 type_size = _wapi_handle_struct_size (type);
358 memcpy (&handle->u, handle_specific,
364 static guint32 _wapi_handle_new_shared (WapiHandleType type,
365 gpointer handle_specific)
368 static guint32 last = 1;
371 g_assert (_wapi_has_shut_down == FALSE);
373 /* Leave the first slot empty as a guard */
375 /* FIXME: expandable array */
376 for(offset = last; offset <_WAPI_HANDLE_INITIAL_COUNT; offset++) {
377 struct _WapiHandleUnshared *handle = &_wapi_shared_layout->handles[offset];
379 if(handle->type == WAPI_HANDLE_UNUSED) {
380 thr_ret = _wapi_handle_lock_shared_handles ();
381 g_assert (thr_ret == 0);
383 if (InterlockedCompareExchange ((gint32 *)&handle->type, type, WAPI_HANDLE_UNUSED) == WAPI_HANDLE_UNUSED) {
386 _wapi_handle_init_shared (handle, type,
389 _wapi_handle_unlock_shared_handles ();
393 /* Someone else beat us to it, just
398 _wapi_handle_unlock_shared_handles ();
403 /* Try again from the beginning */
408 /* Will need to expand the array. The caller will sort it out */
414 * _wapi_handle_new_internal:
415 * @type: Init handle to this type
417 * Search for a free handle and initialize it. Return the handle on
418 * success and 0 on failure. This is only called from
419 * _wapi_handle_new, and scan_mutex must be held.
421 static guint32 _wapi_handle_new_internal (WapiHandleType type,
422 gpointer handle_specific)
425 static guint32 last = 0;
426 gboolean retry = FALSE;
428 g_assert (_wapi_has_shut_down == FALSE);
430 /* A linear scan should be fast enough. Start from the last
431 * allocation, assuming that handles are allocated more often
432 * than they're freed. Leave the space reserved for file
436 if (last < _wapi_fd_reserve) {
437 last = _wapi_fd_reserve;
444 for(i = SLOT_INDEX (count); i < _wapi_private_handle_slot_count; i++) {
445 if (_wapi_private_handles [i]) {
446 for (k = SLOT_OFFSET (count); k < _WAPI_HANDLE_INITIAL_COUNT; k++) {
447 struct _WapiHandleUnshared *handle = &_wapi_private_handles [i][k];
449 if(handle->type == WAPI_HANDLE_UNUSED) {
452 _wapi_handle_init (handle, type, handle_specific);
460 if(retry && last > _wapi_fd_reserve) {
461 /* Try again from the beginning */
462 last = _wapi_fd_reserve;
466 /* Will need to expand the array. The caller will sort it out */
472 _wapi_handle_new (WapiHandleType type, gpointer handle_specific)
474 guint32 handle_idx = 0;
478 g_assert (_wapi_has_shut_down == FALSE);
480 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Creating new handle of type %s", __func__,
481 _wapi_handle_typename[type]);
483 g_assert(!_WAPI_FD_HANDLE(type));
485 thr_ret = mono_os_mutex_lock (&scan_mutex);
486 g_assert (thr_ret == 0);
488 while ((handle_idx = _wapi_handle_new_internal (type, handle_specific)) == 0) {
489 /* Try and expand the array, and have another go */
490 int idx = SLOT_INDEX (_wapi_private_handle_count);
491 if (idx >= _WAPI_PRIVATE_MAX_SLOTS) {
495 _wapi_private_handles [idx] = g_new0 (struct _WapiHandleUnshared,
496 _WAPI_HANDLE_INITIAL_COUNT);
498 _wapi_private_handle_count += _WAPI_HANDLE_INITIAL_COUNT;
499 _wapi_private_handle_slot_count ++;
502 thr_ret = mono_os_mutex_unlock (&scan_mutex);
503 g_assert (thr_ret == 0);
505 if (handle_idx == 0) {
506 /* We ran out of slots */
507 handle = _WAPI_HANDLE_INVALID;
511 /* Make sure we left the space for fd mappings */
512 g_assert (handle_idx >= _wapi_fd_reserve);
514 handle = GUINT_TO_POINTER (handle_idx);
516 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Allocated new handle %p", __func__, handle);
518 if (_WAPI_SHARED_HANDLE(type)) {
519 /* Add the shared section too */
522 ref = _wapi_handle_new_shared (type, handle_specific);
524 ref = _wapi_handle_new_shared (type, handle_specific);
526 /* FIXME: grow the arrays */
527 handle = _WAPI_HANDLE_INVALID;
532 _WAPI_PRIVATE_HANDLES(handle_idx).u.shared.offset = ref;
533 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: New shared handle at offset 0x%x", __func__,
541 gpointer _wapi_handle_new_from_offset (WapiHandleType type, guint32 offset)
543 guint32 handle_idx = 0;
544 gpointer handle = INVALID_HANDLE_VALUE;
546 struct _WapiHandleUnshared *shared;
548 g_assert (_wapi_has_shut_down == FALSE);
550 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Creating new handle of type %s to offset %d", __func__,
551 _wapi_handle_typename[type], offset);
553 g_assert(!_WAPI_FD_HANDLE(type));
554 g_assert(_WAPI_SHARED_HANDLE(type));
555 g_assert(offset != 0);
557 shared = &_wapi_shared_layout->handles[offset];
559 thr_ret = mono_os_mutex_lock (&scan_mutex);
560 g_assert (thr_ret == 0);
562 for (i = SLOT_INDEX (0); i < _wapi_private_handle_slot_count; i++) {
563 if (_wapi_private_handles [i]) {
564 for (k = SLOT_OFFSET (0); k < _WAPI_HANDLE_INITIAL_COUNT; k++) {
565 struct _WapiHandleUnshared *handle_data = &_wapi_private_handles [i][k];
567 if (handle_data->type == type &&
568 handle_data->u.shared.offset == offset) {
569 handle = GUINT_TO_POINTER (i * _WAPI_HANDLE_INITIAL_COUNT + k);
570 goto first_pass_done;
577 thr_ret = mono_os_mutex_unlock (&scan_mutex);
578 g_assert (thr_ret == 0);
580 if (handle != INVALID_HANDLE_VALUE) {
581 _wapi_handle_ref (handle);
583 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Returning old handle %p referencing 0x%x",
584 __func__, handle, offset);
588 /* Prevent entries expiring under us as we search */
589 thr_ret = _wapi_handle_lock_shared_handles ();
590 g_assert (thr_ret == 0);
592 if (shared->type == WAPI_HANDLE_UNUSED) {
593 /* Someone deleted this handle while we were working */
594 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Handle at 0x%x unused", __func__, offset);
598 if (shared->type != type) {
599 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Wrong type at %d 0x%x! Found %s wanted %s",
600 __func__, offset, offset,
601 _wapi_handle_typename[shared->type],
602 _wapi_handle_typename[type]);
606 thr_ret = mono_os_mutex_lock (&scan_mutex);
607 g_assert (thr_ret == 0);
609 while ((handle_idx = _wapi_handle_new_internal (type, NULL)) == 0) {
610 /* Try and expand the array, and have another go */
611 int idx = SLOT_INDEX (_wapi_private_handle_count);
612 _wapi_private_handles [idx] = g_new0 (struct _WapiHandleUnshared,
613 _WAPI_HANDLE_INITIAL_COUNT);
615 _wapi_private_handle_count += _WAPI_HANDLE_INITIAL_COUNT;
616 _wapi_private_handle_slot_count ++;
619 thr_ret = mono_os_mutex_unlock (&scan_mutex);
620 g_assert (thr_ret == 0);
622 /* Make sure we left the space for fd mappings */
623 g_assert (handle_idx >= _wapi_fd_reserve);
625 handle = GUINT_TO_POINTER (handle_idx);
627 _WAPI_PRIVATE_HANDLES(handle_idx).u.shared.offset = offset;
628 InterlockedIncrement ((gint32 *)&shared->ref);
630 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Allocated new handle %p referencing 0x%x (shared refs %d)", __func__, handle, offset, shared->ref);
633 _wapi_handle_unlock_shared_handles ();
639 init_handles_slot (int idx)
643 thr_ret = mono_os_mutex_lock (&scan_mutex);
644 g_assert (thr_ret == 0);
646 if (_wapi_private_handles [idx] == NULL) {
647 _wapi_private_handles [idx] = g_new0 (struct _WapiHandleUnshared,
648 _WAPI_HANDLE_INITIAL_COUNT);
649 g_assert (_wapi_private_handles [idx]);
652 thr_ret = mono_os_mutex_unlock (&scan_mutex);
653 g_assert (thr_ret == 0);
656 gpointer _wapi_handle_new_fd (WapiHandleType type, int fd,
657 gpointer handle_specific)
659 struct _WapiHandleUnshared *handle;
662 g_assert (_wapi_has_shut_down == FALSE);
664 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Creating new handle of type %s", __func__,
665 _wapi_handle_typename[type]);
667 g_assert(_WAPI_FD_HANDLE(type));
668 g_assert(!_WAPI_SHARED_HANDLE(type));
670 if (fd >= _wapi_fd_reserve) {
671 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: fd %d is too big", __func__, fd);
673 return(GUINT_TO_POINTER (_WAPI_HANDLE_INVALID));
676 /* Initialize the array entries on demand */
677 if (_wapi_private_handles [SLOT_INDEX (fd)] == NULL)
678 init_handles_slot (SLOT_INDEX (fd));
680 handle = &_WAPI_PRIVATE_HANDLES(fd);
682 if (handle->type != WAPI_HANDLE_UNUSED) {
683 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: fd %d is already in use!", __func__, fd);
684 /* FIXME: clean up this handle? We can't do anything
685 * with the fd, cos thats the new one
689 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Assigning new fd handle %d", __func__, fd);
691 /* Prevent file share entries racing with us, when the file
692 * handle is only half initialised
694 thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_FILESHARE);
695 g_assert(thr_ret == 0);
697 _wapi_handle_init (handle, type, handle_specific);
699 thr_ret = _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_FILESHARE);
701 return(GUINT_TO_POINTER(fd));
705 _wapi_lookup_handle (gpointer handle, WapiHandleType type,
706 gpointer *handle_specific)
708 struct _WapiHandleUnshared *handle_data;
709 guint32 handle_idx = GPOINTER_TO_UINT(handle);
711 if (!_WAPI_PRIVATE_VALID_SLOT (handle_idx)) {
715 /* Initialize the array entries on demand */
716 if (_wapi_private_handles [SLOT_INDEX (handle_idx)] == NULL)
717 init_handles_slot (SLOT_INDEX (handle_idx));
719 handle_data = &_WAPI_PRIVATE_HANDLES(handle_idx);
721 if (handle_data->type != type) {
725 if (handle_specific == NULL) {
729 if (_WAPI_SHARED_HANDLE(type)) {
730 struct _WapiHandle_shared_ref *ref;
731 struct _WapiHandleUnshared *shared_handle_data;
733 ref = &handle_data->u.shared;
734 shared_handle_data = &_wapi_shared_layout->handles[ref->offset];
736 if (shared_handle_data->type != type) {
737 /* The handle must have been deleted on us
742 *handle_specific = &shared_handle_data->u;
744 *handle_specific = &handle_data->u;
751 _wapi_handle_foreach (WapiHandleType type,
752 gboolean (*on_each)(gpointer test, gpointer user),
755 struct _WapiHandleUnshared *handle_data = NULL;
760 thr_ret = mono_os_mutex_lock (&scan_mutex);
761 g_assert (thr_ret == 0);
763 for (i = SLOT_INDEX (0); i < _wapi_private_handle_slot_count; i++) {
764 if (_wapi_private_handles [i]) {
765 for (k = SLOT_OFFSET (0); k < _WAPI_HANDLE_INITIAL_COUNT; k++) {
766 handle_data = &_wapi_private_handles [i][k];
768 if (handle_data->type == type) {
769 ret = GUINT_TO_POINTER (i * _WAPI_HANDLE_INITIAL_COUNT + k);
770 if (on_each (ret, user_data) == TRUE)
777 thr_ret = mono_os_mutex_unlock (&scan_mutex);
778 g_assert (thr_ret == 0);
781 /* This might list some shared handles twice if they are already
782 * opened by this process, and the check function returns FALSE the
783 * first time. Shared handles that are created during the search are
784 * unreffed if the check function returns FALSE, so callers must not
785 * rely on the handle persisting (unless the check function returns
787 * The caller owns the returned handle.
789 gpointer _wapi_search_handle (WapiHandleType type,
790 gboolean (*check)(gpointer test, gpointer user),
792 gpointer *handle_specific,
793 gboolean search_shared)
795 struct _WapiHandleUnshared *handle_data = NULL;
796 struct _WapiHandleUnshared *shared = NULL;
799 gboolean found = FALSE;
802 thr_ret = mono_os_mutex_lock (&scan_mutex);
803 g_assert (thr_ret == 0);
805 for (i = SLOT_INDEX (0); !found && i < _wapi_private_handle_slot_count; i++) {
806 if (_wapi_private_handles [i]) {
807 for (k = SLOT_OFFSET (0); k < _WAPI_HANDLE_INITIAL_COUNT; k++) {
808 handle_data = &_wapi_private_handles [i][k];
810 if (handle_data->type == type) {
811 ret = GUINT_TO_POINTER (i * _WAPI_HANDLE_INITIAL_COUNT + k);
812 if (check (ret, user_data) == TRUE) {
813 _wapi_handle_ref (ret);
816 if (_WAPI_SHARED_HANDLE (type)) {
817 shared = &_wapi_shared_layout->handles[i];
827 thr_ret = mono_os_mutex_unlock (&scan_mutex);
828 g_assert (thr_ret == 0);
830 if (!found && search_shared && _WAPI_SHARED_HANDLE (type)) {
831 /* Not found yet, so search the shared memory too */
832 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Looking at other shared handles...", __func__);
834 for (i = 0; i < _WAPI_HANDLE_INITIAL_COUNT; i++) {
835 shared = &_wapi_shared_layout->handles[i];
837 if (shared->type == type) {
838 /* Tell new_from_offset to not
839 * timestamp this handle, because
840 * otherwise it will ping every handle
841 * in the list and they will never
844 ret = _wapi_handle_new_from_offset (type, i);
845 if (ret == INVALID_HANDLE_VALUE) {
846 /* This handle was deleted
847 * while we were looking at it
852 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Opened tmp handle %p (type %s) from offset %d", __func__, ret, _wapi_handle_typename[type], i);
854 /* It's possible that the shared part
855 * of this handle has now been blown
856 * away (after new_from_offset
857 * successfully opened it,) if its
858 * timestamp is too old. The check
859 * function needs to be aware of this,
860 * and cope if the handle has
863 if (check (ret, user_data) == TRUE) {
864 /* Timestamp this handle, but make
865 * sure it still exists first
867 thr_ret = _wapi_handle_lock_shared_handles ();
868 g_assert (thr_ret == 0);
870 if (shared->type == type) {
872 handle_data = &_WAPI_PRIVATE_HANDLES(GPOINTER_TO_UINT(ret));
874 _wapi_handle_unlock_shared_handles ();
877 /* It's been deleted,
881 _wapi_handle_unlock_shared_handles ();
885 /* This isn't the handle we're looking
886 * for, so drop the reference we took
887 * in _wapi_handle_new_from_offset ()
889 _wapi_handle_unref (ret);
899 if(handle_specific != NULL) {
900 if (_WAPI_SHARED_HANDLE(type)) {
901 g_assert(shared->type == type);
903 *handle_specific = &shared->u;
905 *handle_specific = &handle_data->u;
913 /* Returns the offset of the metadata array, or -1 on error, or 0 for
914 * not found (0 is not a valid offset)
916 gint32 _wapi_search_handle_namespace (WapiHandleType type,
919 struct _WapiHandleUnshared *shared_handle_data;
924 g_assert(_WAPI_SHARED_HANDLE(type));
926 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Lookup for handle named [%s] type %s", __func__,
927 utf8_name, _wapi_handle_typename[type]);
929 thr_ret = _wapi_handle_lock_shared_handles ();
930 g_assert (thr_ret == 0);
932 for(i = 1; i < _WAPI_HANDLE_INITIAL_COUNT; i++) {
933 WapiSharedNamespace *sharedns;
935 shared_handle_data = &_wapi_shared_layout->handles[i];
937 /* Check mutex, event, semaphore, timer, job and
938 * file-mapping object names. So far only mutex,
939 * semaphore and event are implemented.
941 if (!_WAPI_SHARED_NAMESPACE (shared_handle_data->type)) {
945 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: found a shared namespace handle at 0x%x (type %s)", __func__, i, _wapi_handle_typename[shared_handle_data->type]);
947 sharedns=(WapiSharedNamespace *)&shared_handle_data->u;
949 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: name is [%s]", __func__, sharedns->name);
951 if (strcmp (sharedns->name, utf8_name) == 0) {
952 if (shared_handle_data->type != type) {
953 /* Its the wrong type, so fail now */
954 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle 0x%x matches name but is wrong type: %s", __func__, i, _wapi_handle_typename[shared_handle_data->type]);
958 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle 0x%x matches name and type", __func__, i);
966 _wapi_handle_unlock_shared_handles ();
971 void _wapi_handle_ref (gpointer handle)
973 guint32 idx = GPOINTER_TO_UINT(handle);
974 struct _WapiHandleUnshared *handle_data;
976 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
977 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Attempting to ref invalid private handle %p", __func__, handle);
981 if (_wapi_handle_type (handle) == WAPI_HANDLE_UNUSED) {
982 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Attempting to ref unused handle %p", __func__, handle);
986 handle_data = &_WAPI_PRIVATE_HANDLES(idx);
988 InterlockedIncrement ((gint32 *)&handle_data->ref);
991 g_message ("%s: %s handle %p ref now %d", __func__,
992 _wapi_handle_typename[_WAPI_PRIVATE_HANDLES (idx).type],
994 _WAPI_PRIVATE_HANDLES(idx).ref);
998 /* The handle must not be locked on entry to this function */
999 static void _wapi_handle_unref_full (gpointer handle, gboolean ignore_private_busy_handles)
1001 guint32 idx = GPOINTER_TO_UINT(handle);
1002 gboolean destroy = FALSE, early_exit = FALSE;
1005 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
1009 if (_wapi_handle_type (handle) == WAPI_HANDLE_UNUSED) {
1010 g_warning ("%s: Attempting to unref unused handle %p",
1015 /* Possible race condition here if another thread refs the
1016 * handle between here and setting the type to UNUSED. I
1017 * could lock a mutex, but I'm not sure that allowing a handle
1018 * reference to reach 0 isn't an application bug anyway.
1020 destroy = (InterlockedDecrement ((gint32 *)&_WAPI_PRIVATE_HANDLES(idx).ref) ==0);
1023 g_message ("%s: %s handle %p ref now %d (destroy %s)", __func__,
1024 _wapi_handle_typename[_WAPI_PRIVATE_HANDLES (idx).type],
1026 _WAPI_PRIVATE_HANDLES(idx).ref, destroy?"TRUE":"FALSE");
1030 /* Need to copy the handle info, reset the slot in the
1031 * array, and _only then_ call the close function to
1032 * avoid race conditions (eg file descriptors being
1033 * closed, and another file being opened getting the
1034 * same fd racing the memset())
1036 struct _WapiHandleUnshared handle_data;
1037 struct _WapiHandleUnshared shared_handle_data;
1038 WapiHandleType type = _WAPI_PRIVATE_HANDLES(idx).type;
1039 void (*close_func)(gpointer, gpointer) = _wapi_handle_ops_get_close_func (type);
1040 gboolean is_shared = _WAPI_SHARED_HANDLE(type);
1043 /* If this is a shared handle we need to take
1044 * the shared lock outside of the scan_mutex
1045 * lock to avoid deadlocks
1047 thr_ret = _wapi_handle_lock_shared_handles ();
1048 g_assert (thr_ret == 0);
1051 thr_ret = mono_os_mutex_lock (&scan_mutex);
1053 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Destroying handle %p", __func__, handle);
1055 memcpy (&handle_data, &_WAPI_PRIVATE_HANDLES(idx),
1056 sizeof (struct _WapiHandleUnshared));
1058 memset (&_WAPI_PRIVATE_HANDLES(idx).u, '\0',
1059 sizeof(_WAPI_PRIVATE_HANDLES(idx).u));
1061 _WAPI_PRIVATE_HANDLES(idx).type = WAPI_HANDLE_UNUSED;
1064 /* Destroy the mutex and cond var. We hope nobody
1065 * tried to grab them between the handle unlock and
1066 * now, but pthreads doesn't have a
1067 * "unlock_and_destroy" atomic function.
1069 thr_ret = mono_os_mutex_destroy (&_WAPI_PRIVATE_HANDLES(idx).signal_mutex);
1070 /*WARNING gross hack to make cleanup not crash when exiting without the whole runtime teardown.*/
1071 if (thr_ret == EBUSY && ignore_private_busy_handles) {
1075 g_error ("Error destroying handle %p mutex due to %d\n", handle, thr_ret);
1077 thr_ret = pthread_cond_destroy (&_WAPI_PRIVATE_HANDLES(idx).signal_cond);
1078 if (thr_ret == EBUSY && ignore_private_busy_handles)
1080 else if (thr_ret != 0)
1081 g_error ("Error destroying handle %p cond var due to %d\n", handle, thr_ret);
1084 struct _WapiHandleUnshared *shared = &_wapi_shared_layout->handles[handle_data.u.shared.offset];
1086 memcpy (&shared_handle_data, shared,
1087 sizeof (struct _WapiHandleUnshared));
1089 /* It's possible that this handle is already
1090 * pointing at a deleted shared section
1093 g_message ("%s: %s handle %p shared refs before dec %d", __func__, _wapi_handle_typename[type], handle, shared->ref);
1096 if (shared->ref > 0) {
1098 if (shared->ref == 0) {
1099 memset (shared, '\0', sizeof (struct _WapiHandleUnshared));
1104 thr_ret = mono_os_mutex_unlock (&scan_mutex);
1105 g_assert (thr_ret == 0);
1110 _wapi_handle_unlock_shared_handles ();
1113 if (close_func != NULL) {
1115 close_func (handle, &shared_handle_data.u);
1117 close_func (handle, &handle_data.u);
1123 void _wapi_handle_unref (gpointer handle)
1125 _wapi_handle_unref_full (handle, FALSE);
1128 void _wapi_handle_register_capabilities (WapiHandleType type,
1129 WapiHandleCapability caps)
1131 handle_caps[type] = caps;
1134 gboolean _wapi_handle_test_capabilities (gpointer handle,
1135 WapiHandleCapability caps)
1137 guint32 idx = GPOINTER_TO_UINT(handle);
1138 WapiHandleType type;
1140 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
1144 type = _WAPI_PRIVATE_HANDLES(idx).type;
1146 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: testing 0x%x against 0x%x (%d)", __func__,
1147 handle_caps[type], caps, handle_caps[type] & caps);
1149 return((handle_caps[type] & caps) != 0);
1152 static void (*_wapi_handle_ops_get_close_func (WapiHandleType type))(gpointer, gpointer)
1154 if (handle_ops[type] != NULL &&
1155 handle_ops[type]->close != NULL) {
1156 return (handle_ops[type]->close);
1162 void _wapi_handle_ops_close (gpointer handle, gpointer data)
1164 guint32 idx = GPOINTER_TO_UINT(handle);
1165 WapiHandleType type;
1167 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
1171 type = _WAPI_PRIVATE_HANDLES(idx).type;
1173 if (handle_ops[type] != NULL &&
1174 handle_ops[type]->close != NULL) {
1175 handle_ops[type]->close (handle, data);
1179 void _wapi_handle_ops_signal (gpointer handle)
1181 guint32 idx = GPOINTER_TO_UINT(handle);
1182 WapiHandleType type;
1184 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
1188 type = _WAPI_PRIVATE_HANDLES(idx).type;
1190 if (handle_ops[type] != NULL && handle_ops[type]->signal != NULL) {
1191 handle_ops[type]->signal (handle);
1195 gboolean _wapi_handle_ops_own (gpointer handle)
1197 guint32 idx = GPOINTER_TO_UINT(handle);
1198 WapiHandleType type;
1200 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
1204 type = _WAPI_PRIVATE_HANDLES(idx).type;
1206 if (handle_ops[type] != NULL && handle_ops[type]->own_handle != NULL) {
1207 return(handle_ops[type]->own_handle (handle));
1213 gboolean _wapi_handle_ops_isowned (gpointer handle)
1215 guint32 idx = GPOINTER_TO_UINT(handle);
1216 WapiHandleType type;
1218 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
1222 type = _WAPI_PRIVATE_HANDLES(idx).type;
1224 if (handle_ops[type] != NULL && handle_ops[type]->is_owned != NULL) {
1225 return(handle_ops[type]->is_owned (handle));
1231 guint32 _wapi_handle_ops_special_wait (gpointer handle, guint32 timeout, gboolean alertable)
1233 guint32 idx = GPOINTER_TO_UINT(handle);
1234 WapiHandleType type;
1236 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
1237 return(WAIT_FAILED);
1240 type = _WAPI_PRIVATE_HANDLES(idx).type;
1242 if (handle_ops[type] != NULL &&
1243 handle_ops[type]->special_wait != NULL) {
1244 return(handle_ops[type]->special_wait (handle, timeout, alertable));
1246 return(WAIT_FAILED);
1250 void _wapi_handle_ops_prewait (gpointer handle)
1252 guint32 idx = GPOINTER_TO_UINT (handle);
1253 WapiHandleType type;
1255 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
1259 type = _WAPI_PRIVATE_HANDLES (idx).type;
1261 if (handle_ops[type] != NULL &&
1262 handle_ops[type]->prewait != NULL) {
1263 handle_ops[type]->prewait (handle);
1270 * @handle: The handle to release
1272 * Closes and invalidates @handle, releasing any resources it
1273 * consumes. When the last handle to a temporary or non-persistent
1274 * object is closed, that object can be deleted. Closing the same
1275 * handle twice is an error.
1277 * Return value: %TRUE on success, %FALSE otherwise.
1279 gboolean CloseHandle(gpointer handle)
1281 if (handle == NULL) {
1282 /* Problem: because we map file descriptors to the
1283 * same-numbered handle we can't tell the difference
1284 * between a bogus handle and the handle to stdin.
1285 * Assume that it's the console handle if that handle
1288 if (_WAPI_PRIVATE_HANDLES (0).type != WAPI_HANDLE_CONSOLE) {
1289 SetLastError (ERROR_INVALID_PARAMETER);
1293 if (handle == _WAPI_HANDLE_INVALID){
1294 SetLastError (ERROR_INVALID_PARAMETER);
1298 _wapi_handle_unref (handle);
1303 /* Lots more to implement here, but this is all we need at the moment */
1304 gboolean DuplicateHandle (gpointer srcprocess, gpointer src,
1305 gpointer targetprocess, gpointer *target,
1306 guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, guint32 options G_GNUC_UNUSED)
1308 if (srcprocess != _WAPI_PROCESS_CURRENT ||
1309 targetprocess != _WAPI_PROCESS_CURRENT) {
1310 /* Duplicating other process's handles is not supported */
1311 SetLastError (ERROR_INVALID_HANDLE);
1315 if (src == _WAPI_PROCESS_CURRENT) {
1316 *target = _wapi_process_duplicate ();
1317 } else if (src == _WAPI_THREAD_CURRENT) {
1318 g_assert_not_reached ();
1320 _wapi_handle_ref (src);
1327 gboolean _wapi_handle_count_signalled_handles (guint32 numhandles,
1333 guint32 count, i, iter=0;
1336 WapiHandleType type;
1338 /* Lock all the handles, with backoff */
1340 thr_ret = _wapi_handle_lock_shared_handles ();
1341 g_assert (thr_ret == 0);
1343 for(i=0; i<numhandles; i++) {
1344 gpointer handle = handles[i];
1345 guint32 idx = GPOINTER_TO_UINT(handle);
1347 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: attempting to lock %p", __func__, handle);
1349 type = _WAPI_PRIVATE_HANDLES(idx).type;
1351 thr_ret = _wapi_handle_trylock_handle (handle);
1356 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: attempt failed for %p: %s", __func__,
1357 handle, strerror (thr_ret));
1359 thr_ret = _wapi_handle_unlock_shared_handles ();
1360 g_assert (thr_ret == 0);
1363 handle = handles[i];
1364 idx = GPOINTER_TO_UINT(handle);
1366 thr_ret = _wapi_handle_unlock_handle (handle);
1367 g_assert (thr_ret == 0);
1370 /* If iter ever reaches 100 the nanosleep will
1371 * return EINVAL immediately, but we have a
1372 * design flaw if that happens.
1376 g_warning ("%s: iteration overflow!",
1381 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Backing off for %d ms", __func__,
1383 _wapi_handle_spin (10 * iter);
1389 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Locked all handles", __func__);
1394 for(i=0; i<numhandles; i++) {
1395 gpointer handle = handles[i];
1396 guint32 idx = GPOINTER_TO_UINT(handle);
1398 type = _WAPI_PRIVATE_HANDLES(idx).type;
1400 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Checking handle %p", __func__, handle);
1402 if(((_wapi_handle_test_capabilities (handle, WAPI_HANDLE_CAP_OWN)==TRUE) &&
1403 (_wapi_handle_ops_isowned (handle) == TRUE)) ||
1404 (_WAPI_SHARED_HANDLE(type) &&
1405 WAPI_SHARED_HANDLE_DATA(handle).signalled == TRUE) ||
1406 (!_WAPI_SHARED_HANDLE(type) &&
1407 _WAPI_PRIVATE_HANDLES(idx).signalled == TRUE)) {
1410 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Handle %p signalled", __func__,
1418 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: %d event handles signalled", __func__, count);
1420 if ((waitall == TRUE && count == numhandles) ||
1421 (waitall == FALSE && count > 0)) {
1427 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Returning %d", __func__, ret);
1434 void _wapi_handle_unlock_handles (guint32 numhandles, gpointer *handles)
1439 thr_ret = _wapi_handle_unlock_shared_handles ();
1440 g_assert (thr_ret == 0);
1442 for(i=0; i<numhandles; i++) {
1443 gpointer handle = handles[i];
1445 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unlocking handle %p", __func__, handle);
1447 thr_ret = _wapi_handle_unlock_handle (handle);
1448 g_assert (thr_ret == 0);
1453 signal_handle_and_unref (gpointer handle)
1455 pthread_cond_t *cond;
1456 mono_mutex_t *mutex;
1461 /* If we reach here, then interrupt token is set to the flag value, which
1462 * means that the target thread is either
1463 * - before the first CAS in timedwait, which means it won't enter the wait.
1464 * - it is after the first CAS, so it is already waiting, or it will enter
1465 * the wait, and it will be interrupted by the broadcast. */
1466 idx = GPOINTER_TO_UINT (handle);
1467 cond = &_WAPI_PRIVATE_HANDLES (idx).signal_cond;
1468 mutex = &_WAPI_PRIVATE_HANDLES (idx).signal_mutex;
1470 mono_os_mutex_lock (mutex);
1471 mono_os_cond_broadcast (cond);
1472 mono_os_mutex_unlock (mutex);
1474 _wapi_handle_unref (handle);
1478 _wapi_handle_timedwait_signal_handle (gpointer handle, guint32 timeout, gboolean poll, gboolean *alerted)
1480 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: waiting for %p (type %s)", __func__, handle,
1481 _wapi_handle_typename[_wapi_handle_type (handle)]);
1486 if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
1487 if (WAPI_SHARED_HANDLE_DATA(handle).signalled == TRUE) {
1490 if (timeout != INFINITE) {
1491 if (timeout < 100) {
1492 /* FIXME: Real timeout is less than 100ms time, but is it really worth calculating to the exact ms? */
1493 _wapi_handle_spin (100);
1494 return WAPI_SHARED_HANDLE_DATA(handle).signalled ? 0 : ETIMEDOUT;
1498 _wapi_handle_spin (100);
1501 guint32 idx = GPOINTER_TO_UINT(handle);
1503 pthread_cond_t *cond;
1504 mono_mutex_t *mutex;
1507 mono_thread_info_install_interrupt (signal_handle_and_unref, handle, alerted);
1510 _wapi_handle_ref (handle);
1513 cond = &_WAPI_PRIVATE_HANDLES (idx).signal_cond;
1514 mutex = &_WAPI_PRIVATE_HANDLES (idx).signal_mutex;
1517 res = mono_os_cond_timedwait (cond, mutex, timeout);
1519 /* This is needed when waiting for process handles */
1522 * pthread_cond_(timed)wait() can return 0 even if the condition was not
1523 * signalled. This happens at least on Darwin. We surface this, i.e., we
1524 * get spurious wake-ups.
1526 * http://pubs.opengroup.org/onlinepubs/007908775/xsh/pthread_cond_wait.html
1528 res = mono_os_cond_timedwait (cond, mutex, timeout);
1530 if (timeout < 100) {
1531 /* Real timeout is less than 100ms time */
1532 res = mono_os_cond_timedwait (cond, mutex, timeout);
1534 res = mono_os_cond_timedwait (cond, mutex, 100);
1536 /* Mask the fake timeout, this will cause
1537 * another poll if the cond was not really signaled
1539 if (res == ETIMEDOUT)
1546 mono_thread_info_uninstall_interrupt (alerted);
1548 /* if it is alerted, then the handle is unref in the interrupt callback */
1549 _wapi_handle_unref (handle);
1558 _wapi_free_share_info (_WapiFileShare *share_info)
1560 file_share_hash_lock ();
1561 g_hash_table_remove (file_share_hash, share_info);
1562 file_share_hash_unlock ();
1563 /* The hashtable dtor frees share_info */
1567 wapi_share_info_equal (gconstpointer ka, gconstpointer kb)
1569 const _WapiFileShare *s1 = (const _WapiFileShare *)ka;
1570 const _WapiFileShare *s2 = (const _WapiFileShare *)kb;
1572 return (s1->device == s2->device && s1->inode == s2->inode) ? 1 : 0;
1576 wapi_share_info_hash (gconstpointer data)
1578 const _WapiFileShare *s = (const _WapiFileShare *)data;
1583 gboolean _wapi_handle_get_or_set_share (guint64 device, guint64 inode,
1584 guint32 new_sharemode,
1586 guint32 *old_sharemode,
1587 guint32 *old_access,
1588 struct _WapiFileShare **share_info)
1590 struct _WapiFileShare *file_share;
1592 gboolean exists = FALSE;
1594 /* Prevents entries from expiring under us as we search
1596 thr_ret = _wapi_handle_lock_shared_handles ();
1597 g_assert (thr_ret == 0);
1599 /* Prevent new entries racing with us */
1600 thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_FILESHARE);
1601 g_assert (thr_ret == 0);
1606 * Instead of allocating a 4MB array, we use a hash table to keep track of this
1607 * info. This is needed even if SHM is disabled, to track sharing inside
1608 * the current process.
1610 if (!file_share_hash) {
1611 file_share_hash = g_hash_table_new_full (wapi_share_info_hash, wapi_share_info_equal, NULL, g_free);
1612 mono_os_mutex_init_recursive (&file_share_hash_mutex);
1615 tmp.device = device;
1618 file_share_hash_lock ();
1620 file_share = (_WapiFileShare *)g_hash_table_lookup (file_share_hash, &tmp);
1622 *old_sharemode = file_share->sharemode;
1623 *old_access = file_share->access;
1624 *share_info = file_share;
1626 InterlockedIncrement ((gint32 *)&file_share->handle_refs);
1629 file_share = g_new0 (_WapiFileShare, 1);
1631 file_share->device = device;
1632 file_share->inode = inode;
1633 file_share->opened_by_pid = _wapi_getpid ();
1634 file_share->sharemode = new_sharemode;
1635 file_share->access = new_access;
1636 file_share->handle_refs = 1;
1637 *share_info = file_share;
1639 g_hash_table_insert (file_share_hash, file_share, file_share);
1642 file_share_hash_unlock ();
1644 thr_ret = _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_FILESHARE);
1646 _wapi_handle_unlock_shared_handles ();
1651 /* If we don't have the info in /proc, check if the process that
1652 * opened this share info is still there (it's not a perfect method,
1655 static void _wapi_handle_check_share_by_pid (struct _WapiFileShare *share_info)
1657 #if defined(__native_client__)
1658 g_assert_not_reached ();
1659 #elif defined(HAVE_KILL)
1660 if (kill (share_info->opened_by_pid, 0) == -1 &&
1663 /* It's gone completely (or there's a new process
1664 * owned by someone else) so mark this share info as
1667 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Didn't find it, destroying entry", __func__);
1669 _wapi_free_share_info (share_info);
1675 /* Scan /proc/<pids>/fd/ for open file descriptors to the file in
1676 * question. If there are none, reset the share info.
1678 * This implementation is Linux-specific; legacy systems will have to
1679 * implement their own ways of finding out if a particular file is
1680 * open by a process.
1682 void _wapi_handle_check_share (struct _WapiFileShare *share_info, int fd)
1684 gboolean found = FALSE, proc_fds = FALSE;
1687 /* Prevents entries from expiring under us if we remove this
1690 thr_ret = _wapi_handle_lock_shared_handles ();
1691 g_assert (thr_ret == 0);
1693 /* Prevent new entries racing with us */
1694 thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_FILESHARE);
1695 g_assert (thr_ret == 0);
1697 /* If there is no /proc, there's nothing more we can do here */
1698 if (access ("/proc", F_OK) == -1) {
1699 _wapi_handle_check_share_by_pid (share_info);
1703 /* If there's another handle that thinks it owns this fd, then even
1704 * if the fd has been closed behind our back consider it still owned.
1705 * See bugs 75764 and 75891
1707 for (i = 0; i < _wapi_fd_reserve; i++) {
1708 if (_wapi_private_handles [SLOT_INDEX (i)]) {
1709 struct _WapiHandleUnshared *handle = &_WAPI_PRIVATE_HANDLES(i);
1712 handle->type == WAPI_HANDLE_FILE) {
1713 struct _WapiHandle_file *file_handle = &handle->u.file;
1715 if (file_handle->share_info == share_info) {
1716 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle 0x%x has this file open!",
1725 if (proc_fds == FALSE) {
1726 _wapi_handle_check_share_by_pid (share_info);
1727 } else if (found == FALSE) {
1728 /* Blank out this entry, as it is stale */
1729 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Didn't find it, destroying entry", __func__);
1731 _wapi_free_share_info (share_info);
1735 thr_ret = _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_FILESHARE);
1737 _wapi_handle_unlock_shared_handles ();
1741 // Other implementations (non-Linux)
1743 void _wapi_handle_check_share (struct _WapiFileShare *share_info, int fd)
1747 /* Prevents entries from expiring under us if we remove this
1749 thr_ret = _wapi_handle_lock_shared_handles ();
1750 g_assert (thr_ret == 0);
1752 /* Prevent new entries racing with us */
1753 thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_FILESHARE);
1754 g_assert (thr_ret == 0);
1756 _wapi_handle_check_share_by_pid (share_info);
1758 thr_ret = _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_FILESHARE);
1759 _wapi_handle_unlock_shared_handles ();
1763 void _wapi_handle_dump (void)
1765 struct _WapiHandleUnshared *handle_data;
1769 thr_ret = mono_os_mutex_lock (&scan_mutex);
1770 g_assert (thr_ret == 0);
1772 for(i = SLOT_INDEX (0); i < _wapi_private_handle_slot_count; i++) {
1773 if (_wapi_private_handles [i]) {
1774 for (k = SLOT_OFFSET (0); k < _WAPI_HANDLE_INITIAL_COUNT; k++) {
1775 handle_data = &_wapi_private_handles [i][k];
1777 if (handle_data->type == WAPI_HANDLE_UNUSED) {
1781 g_print ("%3x [%7s] %s %d ",
1782 i * _WAPI_HANDLE_INITIAL_COUNT + k,
1783 _wapi_handle_typename[handle_data->type],
1784 handle_data->signalled?"Sg":"Un",
1786 if (handle_details[handle_data->type])
1787 handle_details[handle_data->type](&handle_data->u);
1793 thr_ret = mono_os_mutex_unlock (&scan_mutex);
1794 g_assert (thr_ret == 0);
1797 static void _wapi_shared_details (gpointer handle_info)
1799 struct _WapiHandle_shared_ref *shared = (struct _WapiHandle_shared_ref *)handle_info;
1801 g_print ("offset: 0x%x", shared->offset);