2 * handles.c: Generic and internal operations on handles
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002-2006 Novell, Inc.
16 #include <sys/types.h>
17 #ifdef HAVE_SYS_SOCKET_H
18 # include <sys/socket.h>
23 #ifdef HAVE_SYS_MMAN_H
24 # include <sys/mman.h>
31 #include <mono/utils/gc_wrapper.h>
33 #include <mono/io-layer/wapi.h>
34 #include <mono/io-layer/wapi-private.h>
35 #include <mono/io-layer/handles-private.h>
36 #include <mono/io-layer/mono-mutex.h>
37 #include <mono/io-layer/misc-private.h>
38 #include <mono/io-layer/shared.h>
39 #include <mono/io-layer/collection.h>
40 #include <mono/io-layer/process-private.h>
41 #include <mono/io-layer/critical-section-private.h>
46 static void (*_wapi_handle_ops_get_close_func (WapiHandleType type))(gpointer, gpointer);
48 static WapiHandleCapability handle_caps[WAPI_HANDLE_COUNT]={0};
49 static struct _WapiHandleOps *handle_ops[WAPI_HANDLE_COUNT]={
57 #ifndef DISABLE_SOCKETS
63 &_wapi_namedmutex_ops,
65 &_wapi_namedevent_ops,
68 static void _wapi_shared_details (gpointer handle_info);
70 static void (*handle_details[WAPI_HANDLE_COUNT])(gpointer) = {
73 _wapi_console_details,
74 _wapi_shared_details, /* thread */
78 NULL, /* Nothing useful to see in a socket handle */
79 NULL, /* Nothing useful to see in a find handle */
80 _wapi_shared_details, /* process */
82 _wapi_shared_details, /* namedmutex */
83 _wapi_shared_details, /* namedsem */
84 _wapi_shared_details, /* namedevent */
87 const char *_wapi_handle_typename[] = {
106 * We can hold _WAPI_PRIVATE_MAX_SLOTS * _WAPI_HANDLE_INITIAL_COUNT handles.
107 * If 4M handles are not enough... Oh, well... we will crash.
109 #define SLOT_INDEX(x) (x / _WAPI_HANDLE_INITIAL_COUNT)
110 #define SLOT_OFFSET(x) (x % _WAPI_HANDLE_INITIAL_COUNT)
112 struct _WapiHandleUnshared *_wapi_private_handles [_WAPI_PRIVATE_MAX_SLOTS];
113 static guint32 _wapi_private_handle_count = 0;
114 static guint32 _wapi_private_handle_slot_count = 0;
116 struct _WapiHandleSharedLayout *_wapi_shared_layout = NULL;
117 struct _WapiFileShareLayout *_wapi_fileshare_layout = NULL;
119 guint32 _wapi_fd_reserve;
122 * This is an internal handle which is used for handling waiting for multiple handles.
123 * Threads which wait for multiple handles wait on this one handle, and when a handle
124 * is signalled, this handle is signalled too.
126 static gpointer _wapi_global_signal_handle;
128 /* Point to the mutex/cond inside _wapi_global_signal_handle */
129 mono_mutex_t *_wapi_global_signal_mutex;
130 pthread_cond_t *_wapi_global_signal_cond;
133 gboolean _wapi_has_shut_down = FALSE;
135 /* Use this instead of getpid(), to cope with linuxthreads. It's a
136 * function rather than a variable lookup because we need to get at
137 * this before share_init() might have been called.
139 static pid_t _wapi_pid;
140 static mono_once_t pid_init_once = MONO_ONCE_INIT;
142 static gpointer _wapi_handle_real_new (WapiHandleType type, gpointer handle_specific);
144 static void pid_init (void)
146 _wapi_pid = getpid ();
149 pid_t _wapi_getpid (void)
151 mono_once (&pid_init_once, pid_init);
157 static mono_mutex_t scan_mutex = MONO_MUTEX_INITIALIZER;
159 static void handle_cleanup (void)
163 _wapi_process_signal_self ();
165 /* Every shared handle we were using ought really to be closed
166 * by now, but to make sure just blow them all away. The
167 * exiting finalizer thread in particular races us to the
168 * program exit and doesn't always win, so it can be left
169 * cluttering up the shared file. Anything else left over is
172 for(i = SLOT_INDEX (0); _wapi_private_handles[i] != NULL; i++) {
173 for(j = SLOT_OFFSET (0); j < _WAPI_HANDLE_INITIAL_COUNT; j++) {
174 struct _WapiHandleUnshared *handle_data = &_wapi_private_handles[i][j];
175 int type = handle_data->type;
178 if (_WAPI_SHARED_HANDLE (type)) {
179 gpointer handle = GINT_TO_POINTER (i*_WAPI_HANDLE_INITIAL_COUNT+j);
181 if (type == WAPI_HANDLE_THREAD) {
182 /* Special-case thread handles
183 * because they need extra
184 * cleanup. This also avoids
185 * a race condition between
186 * the application exit and
187 * the finalizer thread - if
188 * it finishes up between now
189 * and actual app termination
190 * it will find all its handle
191 * details have been blown
192 * away, so this sets those
195 _wapi_thread_set_termination_details (handle, 0);
198 for(k = handle_data->ref; k > 0; k--) {
200 g_message ("%s: unreffing %s handle %p", __func__, _wapi_handle_typename[type], handle);
203 _wapi_handle_unref (handle);
209 _wapi_shm_semaphores_remove ();
212 void _wapi_cleanup ()
214 g_assert (_wapi_has_shut_down == FALSE);
216 _wapi_has_shut_down = TRUE;
218 _wapi_critical_section_cleanup ();
219 _wapi_error_cleanup ();
220 _wapi_thread_cleanup ();
223 static mono_once_t shared_init_once = MONO_ONCE_INIT;
224 static void shared_init (void)
226 g_assert ((sizeof (handle_ops) / sizeof (handle_ops[0]))
227 == WAPI_HANDLE_COUNT);
229 _wapi_fd_reserve = getdtablesize();
231 /* This is needed by the code in _wapi_handle_new_internal */
232 _wapi_fd_reserve = (_wapi_fd_reserve + (_WAPI_HANDLE_INITIAL_COUNT - 1)) & ~(_WAPI_HANDLE_INITIAL_COUNT - 1);
236 * The entries in _wapi_private_handles reserved for fds are allocated lazily to
240 _wapi_private_handles [idx++] = g_new0 (struct _WapiHandleUnshared,
241 _WAPI_HANDLE_INITIAL_COUNT);
244 _wapi_private_handle_count += _WAPI_HANDLE_INITIAL_COUNT;
245 _wapi_private_handle_slot_count ++;
246 } while(_wapi_fd_reserve > _wapi_private_handle_count);
248 _wapi_shm_semaphores_init ();
250 _wapi_shared_layout = _wapi_shm_attach (WAPI_SHM_DATA);
251 g_assert (_wapi_shared_layout != NULL);
253 _wapi_fileshare_layout = _wapi_shm_attach (WAPI_SHM_FILESHARE);
254 g_assert (_wapi_fileshare_layout != NULL);
256 #if !defined (DISABLE_SHARED_HANDLES)
257 if (!g_getenv ("MONO_DISABLE_SHM"))
258 _wapi_collection_init ();
261 /* Can't call wapi_handle_new as it calls us recursively */
262 _wapi_global_signal_handle = _wapi_handle_real_new (WAPI_HANDLE_EVENT, NULL);
264 _wapi_global_signal_cond = &_WAPI_PRIVATE_HANDLES (GPOINTER_TO_UINT (_wapi_global_signal_handle)).signal_cond;
265 _wapi_global_signal_mutex = &_WAPI_PRIVATE_HANDLES (GPOINTER_TO_UINT (_wapi_global_signal_handle)).signal_mutex;
267 /* Using g_atexit here instead of an explicit function call in
268 * a cleanup routine lets us cope when a third-party library
269 * calls exit (eg if an X client loses the connection to its
272 g_atexit (handle_cleanup);
275 static void _wapi_handle_init_shared (struct _WapiHandleShared *handle,
277 gpointer handle_specific)
279 g_assert (_wapi_has_shut_down == FALSE);
282 handle->timestamp = (guint32)(time (NULL) & 0xFFFFFFFF);
283 handle->signalled = FALSE;
284 handle->handle_refs = 1;
286 if (handle_specific != NULL) {
287 memcpy (&handle->u, handle_specific, sizeof (handle->u));
291 static void _wapi_handle_init (struct _WapiHandleUnshared *handle,
292 WapiHandleType type, gpointer handle_specific)
296 g_assert (_wapi_has_shut_down == FALSE);
299 handle->signalled = FALSE;
302 if (!_WAPI_SHARED_HANDLE(type)) {
303 thr_ret = pthread_cond_init (&handle->signal_cond, NULL);
304 g_assert (thr_ret == 0);
306 thr_ret = mono_mutex_init (&handle->signal_mutex, NULL);
307 g_assert (thr_ret == 0);
309 if (handle_specific != NULL) {
310 memcpy (&handle->u, handle_specific,
316 static guint32 _wapi_handle_new_shared (WapiHandleType type,
317 gpointer handle_specific)
320 static guint32 last = 1;
323 g_assert (_wapi_has_shut_down == FALSE);
325 /* Leave the first slot empty as a guard */
327 /* FIXME: expandable array */
328 for(offset = last; offset <_WAPI_HANDLE_INITIAL_COUNT; offset++) {
329 struct _WapiHandleShared *handle = &_wapi_shared_layout->handles[offset];
331 if(handle->type == WAPI_HANDLE_UNUSED) {
332 thr_ret = _wapi_handle_lock_shared_handles ();
333 g_assert (thr_ret == 0);
335 if (InterlockedCompareExchange ((gint32 *)&handle->type, type, WAPI_HANDLE_UNUSED) == WAPI_HANDLE_UNUSED) {
338 _wapi_handle_init_shared (handle, type,
341 _wapi_handle_unlock_shared_handles ();
345 /* Someone else beat us to it, just
350 _wapi_handle_unlock_shared_handles ();
355 /* Try again from the beginning */
360 /* Will need to expand the array. The caller will sort it out */
366 * _wapi_handle_new_internal:
367 * @type: Init handle to this type
369 * Search for a free handle and initialize it. Return the handle on
370 * success and 0 on failure. This is only called from
371 * _wapi_handle_new, and scan_mutex must be held.
373 static guint32 _wapi_handle_new_internal (WapiHandleType type,
374 gpointer handle_specific)
377 static guint32 last = 0;
378 gboolean retry = FALSE;
380 g_assert (_wapi_has_shut_down == FALSE);
382 /* A linear scan should be fast enough. Start from the last
383 * allocation, assuming that handles are allocated more often
384 * than they're freed. Leave the space reserved for file
388 if (last < _wapi_fd_reserve) {
389 last = _wapi_fd_reserve;
396 for(i = SLOT_INDEX (count); i < _wapi_private_handle_slot_count; i++) {
397 if (_wapi_private_handles [i]) {
398 for (k = SLOT_OFFSET (count); k < _WAPI_HANDLE_INITIAL_COUNT; k++) {
399 struct _WapiHandleUnshared *handle = &_wapi_private_handles [i][k];
401 if(handle->type == WAPI_HANDLE_UNUSED) {
404 _wapi_handle_init (handle, type, handle_specific);
412 if(retry && last > _wapi_fd_reserve) {
413 /* Try again from the beginning */
414 last = _wapi_fd_reserve;
418 /* Will need to expand the array. The caller will sort it out */
423 static gpointer _wapi_handle_real_new (WapiHandleType type, gpointer handle_specific)
425 guint32 handle_idx = 0;
430 g_message ("%s: Creating new handle of type %s", __func__,
431 _wapi_handle_typename[type]);
434 g_assert(!_WAPI_FD_HANDLE(type));
436 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
437 (void *)&scan_mutex);
438 thr_ret = mono_mutex_lock (&scan_mutex);
439 g_assert (thr_ret == 0);
441 while ((handle_idx = _wapi_handle_new_internal (type, handle_specific)) == 0) {
442 /* Try and expand the array, and have another go */
443 int idx = SLOT_INDEX (_wapi_private_handle_count);
444 if (idx >= _WAPI_PRIVATE_MAX_SLOTS) {
448 _wapi_private_handles [idx] = g_new0 (struct _WapiHandleUnshared,
449 _WAPI_HANDLE_INITIAL_COUNT);
451 _wapi_private_handle_count += _WAPI_HANDLE_INITIAL_COUNT;
452 _wapi_private_handle_slot_count ++;
455 thr_ret = mono_mutex_unlock (&scan_mutex);
456 g_assert (thr_ret == 0);
457 pthread_cleanup_pop (0);
459 if (handle_idx == 0) {
460 /* We ran out of slots */
461 handle = _WAPI_HANDLE_INVALID;
465 /* Make sure we left the space for fd mappings */
466 g_assert (handle_idx >= _wapi_fd_reserve);
468 handle = GUINT_TO_POINTER (handle_idx);
471 g_message ("%s: Allocated new handle %p", __func__, handle);
474 if (_WAPI_SHARED_HANDLE(type)) {
475 /* Add the shared section too */
478 ref = _wapi_handle_new_shared (type, handle_specific);
480 _wapi_handle_collect ();
481 ref = _wapi_handle_new_shared (type, handle_specific);
483 /* FIXME: grow the arrays */
484 handle = _WAPI_HANDLE_INVALID;
489 _WAPI_PRIVATE_HANDLES(handle_idx).u.shared.offset = ref;
491 g_message ("%s: New shared handle at offset 0x%x", __func__,
500 gpointer _wapi_handle_new (WapiHandleType type, gpointer handle_specific)
502 g_assert (_wapi_has_shut_down == FALSE);
504 mono_once (&shared_init_once, shared_init);
506 return _wapi_handle_real_new (type, handle_specific);
509 gpointer _wapi_handle_new_from_offset (WapiHandleType type, guint32 offset,
512 guint32 handle_idx = 0;
513 gpointer handle = INVALID_HANDLE_VALUE;
515 struct _WapiHandleShared *shared;
516 guint32 now = (guint32)(time (NULL) & 0xFFFFFFFF);
518 g_assert (_wapi_has_shut_down == FALSE);
520 mono_once (&shared_init_once, shared_init);
523 g_message ("%s: Creating new handle of type %s to offset %d", __func__,
524 _wapi_handle_typename[type], offset);
527 g_assert(!_WAPI_FD_HANDLE(type));
528 g_assert(_WAPI_SHARED_HANDLE(type));
529 g_assert(offset != 0);
531 shared = &_wapi_shared_layout->handles[offset];
533 /* Bump up the timestamp for this offset */
534 InterlockedExchange ((gint32 *)&shared->timestamp, now);
537 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
538 (void *)&scan_mutex);
539 thr_ret = mono_mutex_lock (&scan_mutex);
540 g_assert (thr_ret == 0);
542 for (i = SLOT_INDEX (0); i < _wapi_private_handle_slot_count; i++) {
543 if (_wapi_private_handles [i]) {
544 for (k = SLOT_OFFSET (0); k < _WAPI_HANDLE_INITIAL_COUNT; k++) {
545 struct _WapiHandleUnshared *handle_data = &_wapi_private_handles [i][k];
547 if (handle_data->type == type &&
548 handle_data->u.shared.offset == offset) {
549 handle = GUINT_TO_POINTER (i * _WAPI_HANDLE_INITIAL_COUNT + k);
550 goto first_pass_done;
557 thr_ret = mono_mutex_unlock (&scan_mutex);
558 g_assert (thr_ret == 0);
559 pthread_cleanup_pop (0);
561 if (handle != INVALID_HANDLE_VALUE) {
562 _wapi_handle_ref (handle);
565 g_message ("%s: Returning old handle %p referencing 0x%x",
566 __func__, handle, offset);
571 /* Prevent entries expiring under us as we search */
572 thr_ret = _wapi_handle_lock_shared_handles ();
573 g_assert (thr_ret == 0);
575 if (shared->type == WAPI_HANDLE_UNUSED) {
576 /* Someone deleted this handle while we were working */
578 g_message ("%s: Handle at 0x%x unused", __func__, offset);
583 if (shared->type != type) {
585 g_message ("%s: Wrong type at %d 0x%x! Found %s wanted %s",
586 __func__, offset, offset,
587 _wapi_handle_typename[shared->type],
588 _wapi_handle_typename[type]);
593 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
594 (void *)&scan_mutex);
595 thr_ret = mono_mutex_lock (&scan_mutex);
596 g_assert (thr_ret == 0);
598 while ((handle_idx = _wapi_handle_new_internal (type, NULL)) == 0) {
599 /* Try and expand the array, and have another go */
600 int idx = SLOT_INDEX (_wapi_private_handle_count);
601 _wapi_private_handles [idx] = g_new0 (struct _WapiHandleUnshared,
602 _WAPI_HANDLE_INITIAL_COUNT);
604 _wapi_private_handle_count += _WAPI_HANDLE_INITIAL_COUNT;
605 _wapi_private_handle_slot_count ++;
608 thr_ret = mono_mutex_unlock (&scan_mutex);
609 g_assert (thr_ret == 0);
610 pthread_cleanup_pop (0);
612 /* Make sure we left the space for fd mappings */
613 g_assert (handle_idx >= _wapi_fd_reserve);
615 handle = GUINT_TO_POINTER (handle_idx);
617 _WAPI_PRIVATE_HANDLES(handle_idx).u.shared.offset = offset;
618 InterlockedIncrement ((gint32 *)&shared->handle_refs);
621 g_message ("%s: Allocated new handle %p referencing 0x%x (shared refs %d)", __func__, handle, offset, shared->handle_refs);
625 _wapi_handle_unlock_shared_handles ();
631 init_handles_slot (int idx)
635 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
636 (void *)&scan_mutex);
637 thr_ret = mono_mutex_lock (&scan_mutex);
638 g_assert (thr_ret == 0);
640 if (_wapi_private_handles [idx] == NULL) {
641 _wapi_private_handles [idx] = g_new0 (struct _WapiHandleUnshared,
642 _WAPI_HANDLE_INITIAL_COUNT);
643 g_assert (_wapi_private_handles [idx]);
646 thr_ret = mono_mutex_unlock (&scan_mutex);
647 g_assert (thr_ret == 0);
648 pthread_cleanup_pop (0);
651 gpointer _wapi_handle_new_fd (WapiHandleType type, int fd,
652 gpointer handle_specific)
654 struct _WapiHandleUnshared *handle;
657 g_assert (_wapi_has_shut_down == FALSE);
659 mono_once (&shared_init_once, shared_init);
662 g_message ("%s: Creating new handle of type %s", __func__,
663 _wapi_handle_typename[type]);
666 g_assert(_WAPI_FD_HANDLE(type));
667 g_assert(!_WAPI_SHARED_HANDLE(type));
669 if (fd >= _wapi_fd_reserve) {
671 g_message ("%s: fd %d is too big", __func__, fd);
674 return(GUINT_TO_POINTER (_WAPI_HANDLE_INVALID));
677 /* Initialize the array entries on demand */
678 if (_wapi_private_handles [SLOT_INDEX (fd)] == NULL)
679 init_handles_slot (SLOT_INDEX (fd));
681 handle = &_WAPI_PRIVATE_HANDLES(fd);
683 if (handle->type != WAPI_HANDLE_UNUSED) {
685 g_message ("%s: fd %d is already in use!", __func__, fd);
687 /* FIXME: clean up this handle? We can't do anything
688 * with the fd, cos thats the new one
693 g_message ("%s: Assigning new fd handle %d", __func__, fd);
696 /* Prevent file share entries racing with us, when the file
697 * handle is only half initialised
699 thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_FILESHARE);
700 g_assert(thr_ret == 0);
702 _wapi_handle_init (handle, type, handle_specific);
704 thr_ret = _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_FILESHARE);
706 return(GUINT_TO_POINTER(fd));
709 gboolean _wapi_lookup_handle (gpointer handle, WapiHandleType type,
710 gpointer *handle_specific)
712 struct _WapiHandleUnshared *handle_data;
713 guint32 handle_idx = GPOINTER_TO_UINT(handle);
715 if (!_WAPI_PRIVATE_VALID_SLOT (handle_idx)) {
719 /* Initialize the array entries on demand */
720 if (_wapi_private_handles [SLOT_INDEX (handle_idx)] == NULL)
721 init_handles_slot (SLOT_INDEX (handle_idx));
723 handle_data = &_WAPI_PRIVATE_HANDLES(handle_idx);
725 if (handle_data->type != type) {
729 if (handle_specific == NULL) {
733 if (_WAPI_SHARED_HANDLE(type)) {
734 struct _WapiHandle_shared_ref *ref;
735 struct _WapiHandleShared *shared_handle_data;
737 ref = &handle_data->u.shared;
738 shared_handle_data = &_wapi_shared_layout->handles[ref->offset];
740 if (shared_handle_data->type != type) {
741 /* The handle must have been deleted on us
746 *handle_specific = &shared_handle_data->u;
748 *handle_specific = &handle_data->u;
755 _wapi_handle_foreach (WapiHandleType type,
756 gboolean (*on_each)(gpointer test, gpointer user),
759 struct _WapiHandleUnshared *handle_data = NULL;
764 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
765 (void *)&scan_mutex);
766 thr_ret = mono_mutex_lock (&scan_mutex);
767 g_assert (thr_ret == 0);
769 for (i = SLOT_INDEX (0); i < _wapi_private_handle_slot_count; i++) {
770 if (_wapi_private_handles [i]) {
771 for (k = SLOT_OFFSET (0); k < _WAPI_HANDLE_INITIAL_COUNT; k++) {
772 handle_data = &_wapi_private_handles [i][k];
774 if (handle_data->type == type) {
775 ret = GUINT_TO_POINTER (i * _WAPI_HANDLE_INITIAL_COUNT + k);
776 if (on_each (ret, user_data) == TRUE)
783 thr_ret = mono_mutex_unlock (&scan_mutex);
784 g_assert (thr_ret == 0);
785 pthread_cleanup_pop (0);
788 /* This might list some shared handles twice if they are already
789 * opened by this process, and the check function returns FALSE the
790 * first time. Shared handles that are created during the search are
791 * unreffed if the check function returns FALSE, so callers must not
792 * rely on the handle persisting (unless the check function returns
795 gpointer _wapi_search_handle (WapiHandleType type,
796 gboolean (*check)(gpointer test, gpointer user),
798 gpointer *handle_specific,
799 gboolean search_shared)
801 struct _WapiHandleUnshared *handle_data = NULL;
802 struct _WapiHandleShared *shared = NULL;
805 gboolean found = FALSE;
808 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
809 (void *)&scan_mutex);
810 thr_ret = mono_mutex_lock (&scan_mutex);
811 g_assert (thr_ret == 0);
813 for (i = SLOT_INDEX (0); !found && i < _wapi_private_handle_slot_count; i++) {
814 if (_wapi_private_handles [i]) {
815 for (k = SLOT_OFFSET (0); k < _WAPI_HANDLE_INITIAL_COUNT; k++) {
816 handle_data = &_wapi_private_handles [i][k];
818 if (handle_data->type == type) {
819 ret = GUINT_TO_POINTER (i * _WAPI_HANDLE_INITIAL_COUNT + k);
820 if (check (ret, user_data) == TRUE) {
821 _wapi_handle_ref (ret);
824 if (_WAPI_SHARED_HANDLE (type)) {
825 shared = &_wapi_shared_layout->handles[i];
835 thr_ret = mono_mutex_unlock (&scan_mutex);
836 g_assert (thr_ret == 0);
837 pthread_cleanup_pop (0);
839 if (!found && search_shared && _WAPI_SHARED_HANDLE (type)) {
840 /* Not found yet, so search the shared memory too */
842 g_message ("%s: Looking at other shared handles...", __func__);
845 for (i = 0; i < _WAPI_HANDLE_INITIAL_COUNT; i++) {
846 shared = &_wapi_shared_layout->handles[i];
848 if (shared->type == type) {
849 /* Tell new_from_offset to not
850 * timestamp this handle, because
851 * otherwise it will ping every handle
852 * in the list and they will never
855 ret = _wapi_handle_new_from_offset (type, i,
857 if (ret == INVALID_HANDLE_VALUE) {
858 /* This handle was deleted
859 * while we were looking at it
865 g_message ("%s: Opened tmp handle %p (type %s) from offset %d", __func__, ret, _wapi_handle_typename[type], i);
868 /* It's possible that the shared part
869 * of this handle has now been blown
870 * away (after new_from_offset
871 * successfully opened it,) if its
872 * timestamp is too old. The check
873 * function needs to be aware of this,
874 * and cope if the handle has
877 if (check (ret, user_data) == TRUE) {
878 /* Timestamp this handle, but make
879 * sure it still exists first
881 thr_ret = _wapi_handle_lock_shared_handles ();
882 g_assert (thr_ret == 0);
884 if (shared->type == type) {
885 guint32 now = (guint32)(time (NULL) & 0xFFFFFFFF);
886 InterlockedExchange ((gint32 *)&shared->timestamp, now);
889 handle_data = &_WAPI_PRIVATE_HANDLES(GPOINTER_TO_UINT(ret));
891 _wapi_handle_unlock_shared_handles ();
894 /* It's been deleted,
898 _wapi_handle_unlock_shared_handles ();
902 /* This isn't the handle we're looking
903 * for, so drop the reference we took
904 * in _wapi_handle_new_from_offset ()
906 _wapi_handle_unref (ret);
916 if(handle_specific != NULL) {
917 if (_WAPI_SHARED_HANDLE(type)) {
918 g_assert(shared->type == type);
920 *handle_specific = &shared->u;
922 *handle_specific = &handle_data->u;
930 /* Returns the offset of the metadata array, or -1 on error, or 0 for
931 * not found (0 is not a valid offset)
933 gint32 _wapi_search_handle_namespace (WapiHandleType type,
936 struct _WapiHandleShared *shared_handle_data;
941 g_assert(_WAPI_SHARED_HANDLE(type));
944 g_message ("%s: Lookup for handle named [%s] type %s", __func__,
945 utf8_name, _wapi_handle_typename[type]);
948 /* Do a handle collection before starting to look, so that any
949 * stale cruft gets removed
951 _wapi_handle_collect ();
953 thr_ret = _wapi_handle_lock_shared_handles ();
954 g_assert (thr_ret == 0);
956 for(i = 1; i < _WAPI_HANDLE_INITIAL_COUNT; i++) {
957 WapiSharedNamespace *sharedns;
959 shared_handle_data = &_wapi_shared_layout->handles[i];
961 /* Check mutex, event, semaphore, timer, job and
962 * file-mapping object names. So far only mutex,
963 * semaphore and event are implemented.
965 if (!_WAPI_SHARED_NAMESPACE (shared_handle_data->type)) {
970 g_message ("%s: found a shared namespace handle at 0x%x (type %s)", __func__, i, _wapi_handle_typename[shared_handle_data->type]);
973 sharedns=(WapiSharedNamespace *)&shared_handle_data->u;
976 g_message ("%s: name is [%s]", __func__, sharedns->name);
979 if (strcmp (sharedns->name, utf8_name) == 0) {
980 if (shared_handle_data->type != type) {
981 /* Its the wrong type, so fail now */
983 g_message ("%s: handle 0x%x matches name but is wrong type: %s", __func__, i, _wapi_handle_typename[shared_handle_data->type]);
989 g_message ("%s: handle 0x%x matches name and type", __func__, i);
998 _wapi_handle_unlock_shared_handles ();
1003 void _wapi_handle_ref (gpointer handle)
1005 guint32 idx = GPOINTER_TO_UINT(handle);
1006 guint32 now = (guint32)(time (NULL) & 0xFFFFFFFF);
1007 struct _WapiHandleUnshared *handle_data;
1009 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
1013 if (_wapi_handle_type (handle) == WAPI_HANDLE_UNUSED) {
1014 g_warning ("%s: Attempting to ref unused handle %p", __func__,
1019 handle_data = &_WAPI_PRIVATE_HANDLES(idx);
1021 InterlockedIncrement ((gint32 *)&handle_data->ref);
1023 /* It's possible for processes to exit before getting around
1024 * to updating timestamps in the collection thread, so if a
1025 * shared handle is reffed do the timestamp here as well just
1028 if (_WAPI_SHARED_HANDLE(handle_data->type)) {
1029 struct _WapiHandleShared *shared_data = &_wapi_shared_layout->handles[handle_data->u.shared.offset];
1031 InterlockedExchange ((gint32 *)&shared_data->timestamp, now);
1035 g_message ("%s: %s handle %p ref now %d", __func__,
1036 _wapi_handle_typename[_WAPI_PRIVATE_HANDLES (idx).type],
1038 _WAPI_PRIVATE_HANDLES(idx).ref);
1042 /* The handle must not be locked on entry to this function */
1043 void _wapi_handle_unref (gpointer handle)
1045 guint32 idx = GPOINTER_TO_UINT(handle);
1046 gboolean destroy = FALSE;
1049 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
1053 if (_wapi_handle_type (handle) == WAPI_HANDLE_UNUSED) {
1054 g_warning ("%s: Attempting to unref unused handle %p",
1059 /* Possible race condition here if another thread refs the
1060 * handle between here and setting the type to UNUSED. I
1061 * could lock a mutex, but I'm not sure that allowing a handle
1062 * reference to reach 0 isn't an application bug anyway.
1064 destroy = (InterlockedDecrement ((gint32 *)&_WAPI_PRIVATE_HANDLES(idx).ref) ==0);
1067 g_message ("%s: %s handle %p ref now %d (destroy %s)", __func__,
1068 _wapi_handle_typename[_WAPI_PRIVATE_HANDLES (idx).type],
1070 _WAPI_PRIVATE_HANDLES(idx).ref, destroy?"TRUE":"FALSE");
1074 /* Need to copy the handle info, reset the slot in the
1075 * array, and _only then_ call the close function to
1076 * avoid race conditions (eg file descriptors being
1077 * closed, and another file being opened getting the
1078 * same fd racing the memset())
1080 struct _WapiHandleUnshared handle_data;
1081 struct _WapiHandleShared shared_handle_data;
1082 WapiHandleType type = _WAPI_PRIVATE_HANDLES(idx).type;
1083 void (*close_func)(gpointer, gpointer) = _wapi_handle_ops_get_close_func (type);
1084 gboolean is_shared = _WAPI_SHARED_HANDLE(type);
1087 /* If this is a shared handle we need to take
1088 * the shared lock outside of the scan_mutex
1089 * lock to avoid deadlocks
1091 thr_ret = _wapi_handle_lock_shared_handles ();
1092 g_assert (thr_ret == 0);
1095 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup, (void *)&scan_mutex);
1096 thr_ret = mono_mutex_lock (&scan_mutex);
1099 g_message ("%s: Destroying handle %p", __func__, handle);
1102 memcpy (&handle_data, &_WAPI_PRIVATE_HANDLES(idx),
1103 sizeof (struct _WapiHandleUnshared));
1105 memset (&_WAPI_PRIVATE_HANDLES(idx).u, '\0',
1106 sizeof(_WAPI_PRIVATE_HANDLES(idx).u));
1108 _WAPI_PRIVATE_HANDLES(idx).type = WAPI_HANDLE_UNUSED;
1111 /* Destroy the mutex and cond var. We hope nobody
1112 * tried to grab them between the handle unlock and
1113 * now, but pthreads doesn't have a
1114 * "unlock_and_destroy" atomic function.
1116 thr_ret = mono_mutex_destroy (&_WAPI_PRIVATE_HANDLES(idx).signal_mutex);
1117 g_assert (thr_ret == 0);
1119 thr_ret = pthread_cond_destroy (&_WAPI_PRIVATE_HANDLES(idx).signal_cond);
1120 g_assert (thr_ret == 0);
1122 struct _WapiHandleShared *shared = &_wapi_shared_layout->handles[handle_data.u.shared.offset];
1124 memcpy (&shared_handle_data, shared,
1125 sizeof (struct _WapiHandleShared));
1127 /* It's possible that this handle is already
1128 * pointing at a deleted shared section
1131 g_message ("%s: %s handle %p shared refs before dec %d", __func__, _wapi_handle_typename[type], handle, shared->handle_refs);
1134 if (shared->handle_refs > 0) {
1135 shared->handle_refs--;
1136 if (shared->handle_refs == 0) {
1137 memset (shared, '\0', sizeof (struct _WapiHandleShared));
1142 thr_ret = mono_mutex_unlock (&scan_mutex);
1143 g_assert (thr_ret == 0);
1144 pthread_cleanup_pop (0);
1147 _wapi_handle_unlock_shared_handles ();
1150 if (close_func != NULL) {
1152 close_func (handle, &shared_handle_data.u);
1154 close_func (handle, &handle_data.u);
1160 void _wapi_handle_register_capabilities (WapiHandleType type,
1161 WapiHandleCapability caps)
1163 handle_caps[type] = caps;
1166 gboolean _wapi_handle_test_capabilities (gpointer handle,
1167 WapiHandleCapability caps)
1169 guint32 idx = GPOINTER_TO_UINT(handle);
1170 WapiHandleType type;
1172 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
1176 type = _WAPI_PRIVATE_HANDLES(idx).type;
1179 g_message ("%s: testing 0x%x against 0x%x (%d)", __func__,
1180 handle_caps[type], caps, handle_caps[type] & caps);
1183 return((handle_caps[type] & caps) != 0);
1186 static void (*_wapi_handle_ops_get_close_func (WapiHandleType type))(gpointer, gpointer)
1188 if (handle_ops[type] != NULL &&
1189 handle_ops[type]->close != NULL) {
1190 return (handle_ops[type]->close);
1196 void _wapi_handle_ops_close (gpointer handle, gpointer data)
1198 guint32 idx = GPOINTER_TO_UINT(handle);
1199 WapiHandleType type;
1201 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
1205 type = _WAPI_PRIVATE_HANDLES(idx).type;
1207 if (handle_ops[type] != NULL &&
1208 handle_ops[type]->close != NULL) {
1209 handle_ops[type]->close (handle, data);
1213 void _wapi_handle_ops_signal (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]->signal != NULL) {
1225 handle_ops[type]->signal (handle);
1229 gboolean _wapi_handle_ops_own (gpointer handle)
1231 guint32 idx = GPOINTER_TO_UINT(handle);
1232 WapiHandleType type;
1234 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
1238 type = _WAPI_PRIVATE_HANDLES(idx).type;
1240 if (handle_ops[type] != NULL && handle_ops[type]->own_handle != NULL) {
1241 return(handle_ops[type]->own_handle (handle));
1247 gboolean _wapi_handle_ops_isowned (gpointer handle)
1249 guint32 idx = GPOINTER_TO_UINT(handle);
1250 WapiHandleType type;
1252 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
1256 type = _WAPI_PRIVATE_HANDLES(idx).type;
1258 if (handle_ops[type] != NULL && handle_ops[type]->is_owned != NULL) {
1259 return(handle_ops[type]->is_owned (handle));
1265 guint32 _wapi_handle_ops_special_wait (gpointer handle, guint32 timeout)
1267 guint32 idx = GPOINTER_TO_UINT(handle);
1268 WapiHandleType type;
1270 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
1271 return(WAIT_FAILED);
1274 type = _WAPI_PRIVATE_HANDLES(idx).type;
1276 if (handle_ops[type] != NULL &&
1277 handle_ops[type]->special_wait != NULL) {
1278 return(handle_ops[type]->special_wait (handle, timeout));
1280 return(WAIT_FAILED);
1284 void _wapi_handle_ops_prewait (gpointer handle)
1286 guint32 idx = GPOINTER_TO_UINT (handle);
1287 WapiHandleType type;
1289 if (!_WAPI_PRIVATE_VALID_SLOT (idx)) {
1293 type = _WAPI_PRIVATE_HANDLES (idx).type;
1295 if (handle_ops[type] != NULL &&
1296 handle_ops[type]->prewait != NULL) {
1297 handle_ops[type]->prewait (handle);
1304 * @handle: The handle to release
1306 * Closes and invalidates @handle, releasing any resources it
1307 * consumes. When the last handle to a temporary or non-persistent
1308 * object is closed, that object can be deleted. Closing the same
1309 * handle twice is an error.
1311 * Return value: %TRUE on success, %FALSE otherwise.
1313 gboolean CloseHandle(gpointer handle)
1315 if (handle == NULL) {
1316 /* Problem: because we map file descriptors to the
1317 * same-numbered handle we can't tell the difference
1318 * between a bogus handle and the handle to stdin.
1319 * Assume that it's the console handle if that handle
1322 if (_WAPI_PRIVATE_HANDLES (0).type != WAPI_HANDLE_CONSOLE) {
1323 SetLastError (ERROR_INVALID_PARAMETER);
1327 if (handle == _WAPI_HANDLE_INVALID){
1328 SetLastError (ERROR_INVALID_PARAMETER);
1332 _wapi_handle_unref (handle);
1337 /* Lots more to implement here, but this is all we need at the moment */
1338 gboolean DuplicateHandle (gpointer srcprocess, gpointer src,
1339 gpointer targetprocess, gpointer *target,
1340 guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, guint32 options G_GNUC_UNUSED)
1342 if (srcprocess != _WAPI_PROCESS_CURRENT ||
1343 targetprocess != _WAPI_PROCESS_CURRENT) {
1344 /* Duplicating other process's handles is not supported */
1345 SetLastError (ERROR_INVALID_HANDLE);
1349 if (src == _WAPI_PROCESS_CURRENT) {
1350 *target = _wapi_process_duplicate ();
1351 } else if (src == _WAPI_THREAD_CURRENT) {
1352 *target = _wapi_thread_duplicate ();
1354 _wapi_handle_ref (src);
1361 gboolean _wapi_handle_count_signalled_handles (guint32 numhandles,
1367 guint32 count, i, iter=0;
1370 WapiHandleType type;
1372 /* Lock all the handles, with backoff */
1374 thr_ret = _wapi_handle_lock_shared_handles ();
1375 g_assert (thr_ret == 0);
1377 for(i=0; i<numhandles; i++) {
1378 gpointer handle = handles[i];
1379 guint32 idx = GPOINTER_TO_UINT(handle);
1382 g_message ("%s: attempting to lock %p", __func__, handle);
1385 type = _WAPI_PRIVATE_HANDLES(idx).type;
1387 thr_ret = _wapi_handle_trylock_handle (handle);
1393 g_message ("%s: attempt failed for %p: %s", __func__,
1394 handle, strerror (thr_ret));
1397 thr_ret = _wapi_handle_unlock_shared_handles ();
1398 g_assert (thr_ret == 0);
1401 handle = handles[i];
1402 idx = GPOINTER_TO_UINT(handle);
1404 thr_ret = _wapi_handle_unlock_handle (handle);
1405 g_assert (thr_ret == 0);
1408 /* If iter ever reaches 100 the nanosleep will
1409 * return EINVAL immediately, but we have a
1410 * design flaw if that happens.
1414 g_warning ("%s: iteration overflow!",
1420 g_message ("%s: Backing off for %d ms", __func__,
1423 _wapi_handle_spin (10 * iter);
1430 g_message ("%s: Locked all handles", __func__);
1436 for(i=0; i<numhandles; i++) {
1437 gpointer handle = handles[i];
1438 guint32 idx = GPOINTER_TO_UINT(handle);
1440 type = _WAPI_PRIVATE_HANDLES(idx).type;
1443 g_message ("%s: Checking handle %p", __func__, handle);
1446 if(((_wapi_handle_test_capabilities (handle, WAPI_HANDLE_CAP_OWN)==TRUE) &&
1447 (_wapi_handle_ops_isowned (handle) == TRUE)) ||
1448 (_WAPI_SHARED_HANDLE(type) &&
1449 WAPI_SHARED_HANDLE_DATA(handle).signalled == TRUE) ||
1450 (!_WAPI_SHARED_HANDLE(type) &&
1451 _WAPI_PRIVATE_HANDLES(idx).signalled == TRUE)) {
1455 g_message ("%s: Handle %p signalled", __func__,
1465 g_message ("%s: %d event handles signalled", __func__, count);
1468 if ((waitall == TRUE && count == numhandles) ||
1469 (waitall == FALSE && count > 0)) {
1476 g_message ("%s: Returning %d", __func__, ret);
1484 void _wapi_handle_unlock_handles (guint32 numhandles, gpointer *handles)
1489 thr_ret = _wapi_handle_unlock_shared_handles ();
1490 g_assert (thr_ret == 0);
1492 for(i=0; i<numhandles; i++) {
1493 gpointer handle = handles[i];
1496 g_message ("%s: unlocking handle %p", __func__, handle);
1499 thr_ret = _wapi_handle_unlock_handle (handle);
1500 g_assert (thr_ret == 0);
1504 static int timedwait_signal_poll_cond (pthread_cond_t *cond, mono_mutex_t *mutex, struct timespec *timeout, gboolean alertable)
1506 struct timespec fake_timeout;
1511 ret=mono_cond_timedwait (cond, mutex, timeout);
1513 ret=mono_cond_wait (cond, mutex);
1515 _wapi_calc_timeout (&fake_timeout, 100);
1517 if (timeout != NULL && ((fake_timeout.tv_sec > timeout->tv_sec) ||
1518 (fake_timeout.tv_sec == timeout->tv_sec &&
1519 fake_timeout.tv_nsec > timeout->tv_nsec))) {
1520 /* Real timeout is less than 100ms time */
1521 ret=mono_cond_timedwait (cond, mutex, timeout);
1523 ret=mono_cond_timedwait (cond, mutex, &fake_timeout);
1525 /* Mask the fake timeout, this will cause
1526 * another poll if the cond was not really signaled
1528 if (ret==ETIMEDOUT) {
1537 int _wapi_handle_wait_signal (gboolean poll)
1539 return _wapi_handle_timedwait_signal_handle (_wapi_global_signal_handle, NULL, TRUE, poll);
1542 int _wapi_handle_timedwait_signal (struct timespec *timeout, gboolean poll)
1544 return _wapi_handle_timedwait_signal_handle (_wapi_global_signal_handle, timeout, TRUE, poll);
1547 int _wapi_handle_wait_signal_handle (gpointer handle, gboolean alertable)
1550 g_message ("%s: waiting for %p", __func__, handle);
1553 return _wapi_handle_timedwait_signal_handle (handle, NULL, alertable, FALSE);
1556 int _wapi_handle_timedwait_signal_handle (gpointer handle,
1557 struct timespec *timeout, gboolean alertable, gboolean poll)
1560 g_message ("%s: waiting for %p (type %s)", __func__, handle,
1561 _wapi_handle_typename[_wapi_handle_type (handle)]);
1564 if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
1565 if (WAPI_SHARED_HANDLE_DATA(handle).signalled == TRUE) {
1568 if (timeout != NULL) {
1569 struct timespec fake_timeout;
1570 _wapi_calc_timeout (&fake_timeout, 100);
1572 if ((fake_timeout.tv_sec > timeout->tv_sec) ||
1573 (fake_timeout.tv_sec == timeout->tv_sec &&
1574 fake_timeout.tv_nsec > timeout->tv_nsec)) {
1575 /* FIXME: Real timeout is less than
1576 * 100ms time, but is it really worth
1577 * calculating to the exact ms?
1579 _wapi_handle_spin (100);
1581 if (WAPI_SHARED_HANDLE_DATA(handle).signalled == TRUE) {
1588 _wapi_handle_spin (100);
1592 guint32 idx = GPOINTER_TO_UINT(handle);
1594 pthread_cond_t *cond;
1595 mono_mutex_t *mutex;
1597 if (alertable && !wapi_thread_set_wait_handle (handle))
1600 cond = &_WAPI_PRIVATE_HANDLES (idx).signal_cond;
1601 mutex = &_WAPI_PRIVATE_HANDLES (idx).signal_mutex;
1604 /* This is needed when waiting for process handles */
1605 res = timedwait_signal_poll_cond (cond, mutex, timeout, alertable);
1608 res = mono_cond_timedwait (cond, mutex, timeout);
1610 res = mono_cond_wait (cond, mutex);
1614 wapi_thread_clear_wait_handle (handle);
1620 gboolean _wapi_handle_get_or_set_share (dev_t device, ino_t inode,
1621 guint32 new_sharemode,
1623 guint32 *old_sharemode,
1624 guint32 *old_access,
1625 struct _WapiFileShare **share_info)
1627 struct _WapiFileShare *file_share;
1628 guint32 now = (guint32)(time(NULL) & 0xFFFFFFFF);
1629 int thr_ret, i, first_unused = -1;
1630 gboolean exists = FALSE;
1632 /* Prevents entries from expiring under us as we search
1634 thr_ret = _wapi_handle_lock_shared_handles ();
1635 g_assert (thr_ret == 0);
1637 /* Prevent new entries racing with us */
1638 thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_FILESHARE);
1639 g_assert (thr_ret == 0);
1641 /* If a linear scan gets too slow we'll have to fit a hash
1642 * table onto the shared mem backing store
1645 for (i = 0; i <= _wapi_fileshare_layout->hwm; i++) {
1646 file_share = &_wapi_fileshare_layout->share_info[i];
1648 /* Make a note of an unused slot, in case we need to
1651 if (first_unused == -1 && file_share->handle_refs == 0) {
1656 if (file_share->handle_refs == 0) {
1660 if (file_share->device == device &&
1661 file_share->inode == inode) {
1662 *old_sharemode = file_share->sharemode;
1663 *old_access = file_share->access;
1664 *share_info = file_share;
1666 /* Increment the reference count while we
1667 * still have sole access to the shared area.
1668 * This makes the increment atomic wrt
1671 InterlockedIncrement ((gint32 *)&file_share->handle_refs);
1679 if (i == _WAPI_FILESHARE_SIZE && first_unused == -1) {
1682 if (first_unused == -1) {
1683 file_share = &_wapi_fileshare_layout->share_info[++i];
1684 _wapi_fileshare_layout->hwm = i;
1686 file_share = &_wapi_fileshare_layout->share_info[first_unused];
1689 file_share->device = device;
1690 file_share->inode = inode;
1691 file_share->opened_by_pid = _wapi_getpid ();
1692 file_share->sharemode = new_sharemode;
1693 file_share->access = new_access;
1694 file_share->handle_refs = 1;
1695 *share_info = file_share;
1699 if (*share_info != NULL) {
1700 InterlockedExchange ((gint32 *)&(*share_info)->timestamp, now);
1703 thr_ret = _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_FILESHARE);
1705 _wapi_handle_unlock_shared_handles ();
1710 /* If we don't have the info in /proc, check if the process that
1711 * opened this share info is still there (it's not a perfect method,
1714 static void _wapi_handle_check_share_by_pid (struct _WapiFileShare *share_info)
1716 if (kill (share_info->opened_by_pid, 0) == -1 &&
1719 /* It's gone completely (or there's a new process
1720 * owned by someone else) so mark this share info as
1724 g_message ("%s: Didn't find it, destroying entry", __func__);
1727 memset (share_info, '\0', sizeof(struct _WapiFileShare));
1732 /* Scan /proc/<pids>/fd/ for open file descriptors to the file in
1733 * question. If there are none, reset the share info.
1735 * This implementation is Linux-specific; legacy systems will have to
1736 * implement their own ways of finding out if a particular file is
1737 * open by a process.
1739 void _wapi_handle_check_share (struct _WapiFileShare *share_info, int fd)
1741 gboolean found = FALSE, proc_fds = FALSE;
1742 pid_t self = _wapi_getpid ();
1746 /* 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 /* If there is no /proc, there's nothing more we can do here */
1757 if (access ("/proc", F_OK) == -1) {
1758 _wapi_handle_check_share_by_pid (share_info);
1762 /* If there's another handle that thinks it owns this fd, then even
1763 * if the fd has been closed behind our back consider it still owned.
1764 * See bugs 75764 and 75891
1766 for (i = 0; i < _wapi_fd_reserve; i++) {
1767 if (_wapi_private_handles [SLOT_INDEX (i)]) {
1768 struct _WapiHandleUnshared *handle = &_WAPI_PRIVATE_HANDLES(i);
1771 handle->type == WAPI_HANDLE_FILE) {
1772 struct _WapiHandle_file *file_handle = &handle->u.file;
1774 if (file_handle->share_info == share_info) {
1776 g_message ("%s: handle 0x%x has this file open!",
1786 for (i = 0; i < _WAPI_HANDLE_INITIAL_COUNT; i++) {
1787 struct _WapiHandleShared *shared;
1788 struct _WapiHandle_process *process_handle;
1790 shared = &_wapi_shared_layout->handles[i];
1792 if (shared->type == WAPI_HANDLE_PROCESS) {
1794 struct dirent *fd_entry;
1795 char subdir[_POSIX_PATH_MAX];
1797 process_handle = &shared->u.process;
1798 pid = process_handle->id;
1800 /* Look in /proc/<pid>/fd/ but ignore
1801 * /proc/<our pid>/fd/<fd>, as we have the
1804 g_snprintf (subdir, _POSIX_PATH_MAX, "/proc/%d/fd",
1807 fd_dir = opendir (subdir);
1808 if (fd_dir == NULL) {
1813 g_message ("%s: Looking in %s", __func__, subdir);
1818 while ((fd_entry = readdir (fd_dir)) != NULL) {
1819 char path[_POSIX_PATH_MAX];
1820 struct stat link_stat;
1822 if (!strcmp (fd_entry->d_name, ".") ||
1823 !strcmp (fd_entry->d_name, "..") ||
1825 fd == atoi (fd_entry->d_name))) {
1829 g_snprintf (path, _POSIX_PATH_MAX,
1830 "/proc/%d/fd/%s", pid,
1833 stat (path, &link_stat);
1834 if (link_stat.st_dev == share_info->device &&
1835 link_stat.st_ino == share_info->inode) {
1837 g_message ("%s: Found it at %s",
1849 if (proc_fds == FALSE) {
1850 _wapi_handle_check_share_by_pid (share_info);
1851 } else if (found == FALSE) {
1852 /* Blank out this entry, as it is stale */
1854 g_message ("%s: Didn't find it, destroying entry", __func__);
1857 memset (share_info, '\0', sizeof(struct _WapiFileShare));
1861 thr_ret = _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_FILESHARE);
1863 _wapi_handle_unlock_shared_handles ();
1867 // Other implementations (non-Linux)
1869 void _wapi_handle_check_share (struct _WapiFileShare *share_info, int fd)
1873 /* Prevents entries from expiring under us if we remove this
1875 thr_ret = _wapi_handle_lock_shared_handles ();
1876 g_assert (thr_ret == 0);
1878 /* Prevent new entries racing with us */
1879 thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_FILESHARE);
1880 g_assert (thr_ret == 0);
1882 _wapi_handle_check_share_by_pid (share_info);
1884 thr_ret = _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_FILESHARE);
1885 _wapi_handle_unlock_shared_handles ();
1889 void _wapi_handle_dump (void)
1891 struct _WapiHandleUnshared *handle_data;
1895 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
1896 (void *)&scan_mutex);
1897 thr_ret = mono_mutex_lock (&scan_mutex);
1898 g_assert (thr_ret == 0);
1900 for(i = SLOT_INDEX (0); i < _wapi_private_handle_slot_count; i++) {
1901 if (_wapi_private_handles [i]) {
1902 for (k = SLOT_OFFSET (0); k < _WAPI_HANDLE_INITIAL_COUNT; k++) {
1903 handle_data = &_wapi_private_handles [i][k];
1905 if (handle_data->type == WAPI_HANDLE_UNUSED) {
1909 g_print ("%3x [%7s] %s %d ",
1910 i * _WAPI_HANDLE_INITIAL_COUNT + k,
1911 _wapi_handle_typename[handle_data->type],
1912 handle_data->signalled?"Sg":"Un",
1914 handle_details[handle_data->type](&handle_data->u);
1920 thr_ret = mono_mutex_unlock (&scan_mutex);
1921 g_assert (thr_ret == 0);
1922 pthread_cleanup_pop (0);
1925 static void _wapi_shared_details (gpointer handle_info)
1927 struct _WapiHandle_shared_ref *shared = (struct _WapiHandle_shared_ref *)handle_info;
1929 g_print ("offset: 0x%x", shared->offset);
1932 void _wapi_handle_update_refs (void)
1936 guint32 now = (guint32)(time (NULL) & 0xFFFFFFFF);
1938 thr_ret = _wapi_handle_lock_shared_handles ();
1939 g_assert (thr_ret == 0);
1941 /* Prevent file share entries racing with us */
1942 thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_FILESHARE);
1943 g_assert(thr_ret == 0);
1945 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
1946 (void *)&scan_mutex);
1947 thr_ret = mono_mutex_lock (&scan_mutex);
1949 for(i = SLOT_INDEX (0); i < _wapi_private_handle_slot_count; i++) {
1950 if (_wapi_private_handles [i]) {
1951 for (k = SLOT_OFFSET (0); k < _WAPI_HANDLE_INITIAL_COUNT; k++) {
1952 struct _WapiHandleUnshared *handle = &_wapi_private_handles [i][k];
1954 if (_WAPI_SHARED_HANDLE(handle->type)) {
1955 struct _WapiHandleShared *shared_data;
1958 g_message ("%s: (%d) handle 0x%x is SHARED (%s)", __func__, _wapi_getpid (), i * _WAPI_HANDLE_INITIAL_COUNT + k, _wapi_handle_typename[handle->type]);
1961 shared_data = &_wapi_shared_layout->handles[handle->u.shared.offset];
1964 g_message ("%s: (%d) Updating timestamp of handle 0x%x", __func__, _wapi_getpid (), handle->u.shared.offset);
1967 InterlockedExchange ((gint32 *)&shared_data->timestamp, now);
1968 } else if (handle->type == WAPI_HANDLE_FILE) {
1969 struct _WapiHandle_file *file_handle = &handle->u.file;
1972 g_message ("%s: (%d) handle 0x%x is FILE", __func__, _wapi_getpid (), i * _WAPI_HANDLE_INITIAL_COUNT + k);
1975 g_assert (file_handle->share_info != NULL);
1978 g_message ("%s: (%d) Inc refs on fileshare 0x%x", __func__, _wapi_getpid (), (file_handle->share_info - &_wapi_fileshare_layout->share_info[0]) / sizeof(struct _WapiFileShare));
1981 InterlockedExchange ((gint32 *)&file_handle->share_info->timestamp, now);
1987 thr_ret = mono_mutex_unlock (&scan_mutex);
1988 g_assert (thr_ret == 0);
1989 pthread_cleanup_pop (0);
1991 thr_ret = _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_FILESHARE);
1993 _wapi_handle_unlock_shared_handles ();