[io-layer] Fix the linux build.
[mono.git] / mono / io-layer / handles.c
index ea9cbeccbc138f262c8b23a4fc25fd1889ebebb7..4e8c5223c755f87995c105d883b370425f93cac5 100644 (file)
@@ -37,7 +37,6 @@
 #include <mono/io-layer/shared.h>
 #include <mono/io-layer/collection.h>
 #include <mono/io-layer/process-private.h>
-#include <mono/io-layer/critical-section-private.h>
 
 #include <mono/utils/mono-mutex.h>
 #undef DEBUG_REFS
@@ -131,10 +130,10 @@ struct _WapiFileShareLayout *_wapi_fileshare_layout = NULL;
  * 4MB array.
  */
 static GHashTable *file_share_hash;
-static CRITICAL_SECTION file_share_hash_mutex;
+static mono_mutex_t file_share_hash_mutex;
 
-#define file_share_hash_lock() EnterCriticalSection (&file_share_hash_mutex)
-#define file_share_hash_unlock() LeaveCriticalSection (&file_share_hash_mutex)
+#define file_share_hash_lock() mono_mutex_lock (&file_share_hash_mutex)
+#define file_share_hash_unlock() mono_mutex_unlock (&file_share_hash_mutex)
 
 guint32 _wapi_fd_reserve;
 
@@ -207,7 +206,7 @@ static void handle_cleanup (void)
 
        if (file_share_hash) {
                g_hash_table_destroy (file_share_hash);
-               DeleteCriticalSection (&file_share_hash_mutex);
+               mono_mutex_destroy (&file_share_hash_mutex);
        }
 
        for (i = 0; i < _WAPI_PRIVATE_MAX_SLOTS; ++i)
@@ -267,6 +266,7 @@ wapi_init (void)
        _wapi_global_signal_cond = &_WAPI_PRIVATE_HANDLES (GPOINTER_TO_UINT (_wapi_global_signal_handle)).signal_cond;
        _wapi_global_signal_mutex = &_WAPI_PRIVATE_HANDLES (GPOINTER_TO_UINT (_wapi_global_signal_handle)).signal_mutex;
 
+       wapi_processes_init ();
 
        /* Using g_atexit here instead of an explicit function call in
         * a cleanup routine lets us cope when a third-party library
@@ -285,6 +285,7 @@ wapi_cleanup (void)
 
        _wapi_error_cleanup ();
        _wapi_thread_cleanup ();
+       wapi_processes_cleanup ();
 }
 
 static void _wapi_handle_init_shared (struct _WapiHandleShared *handle,
@@ -303,10 +304,57 @@ static void _wapi_handle_init_shared (struct _WapiHandleShared *handle,
        }
 }
 
+static size_t _wapi_handle_struct_size (WapiHandleType type)
+{
+       size_t type_size;
+
+       switch (type) {
+               case WAPI_HANDLE_FILE: case WAPI_HANDLE_CONSOLE: case WAPI_HANDLE_PIPE:
+                       type_size = sizeof (struct _WapiHandle_file);
+                       break;
+               case WAPI_HANDLE_THREAD:
+                       type_size = sizeof (struct _WapiHandle_thread);
+                       break;
+               case WAPI_HANDLE_SEM:
+                       type_size = sizeof (struct _WapiHandle_sem);
+                       break;
+               case WAPI_HANDLE_MUTEX:
+                       type_size = sizeof (struct _WapiHandle_mutex);
+                       break;
+               case WAPI_HANDLE_EVENT:
+                       type_size = sizeof (struct _WapiHandle_event);
+                       break;
+               case WAPI_HANDLE_SOCKET:
+                       type_size = sizeof (struct _WapiHandle_socket);
+                       break;
+               case WAPI_HANDLE_FIND:
+                       type_size = sizeof (struct _WapiHandle_find);
+                       break;
+               case WAPI_HANDLE_PROCESS:
+                       type_size = sizeof (struct _WapiHandle_process);
+                       break;
+               case WAPI_HANDLE_NAMEDMUTEX:
+                       type_size = sizeof (struct _WapiHandle_namedmutex);
+                       break;
+               case WAPI_HANDLE_NAMEDSEM:
+                       type_size = sizeof (struct _WapiHandle_namedsem);
+                       break;
+               case WAPI_HANDLE_NAMEDEVENT:
+                       type_size = sizeof (struct _WapiHandle_namedevent);
+                       break;
+
+               default:
+                       g_error ("Unknown WapiHandleType: %d\n", type);
+       }
+
+       return type_size;
+}
+
 static void _wapi_handle_init (struct _WapiHandleUnshared *handle,
                               WapiHandleType type, gpointer handle_specific)
 {
        int thr_ret;
+       int type_size;
        
        g_assert (_wapi_has_shut_down == FALSE);
        
@@ -322,8 +370,9 @@ static void _wapi_handle_init (struct _WapiHandleUnshared *handle,
                g_assert (thr_ret == 0);
 
                if (handle_specific != NULL) {
+                       type_size = _wapi_handle_struct_size (type);
                        memcpy (&handle->u, handle_specific,
-                               sizeof (handle->u));
+                               type_size);
                }
        }
 }
@@ -449,8 +498,6 @@ _wapi_handle_new (WapiHandleType type, gpointer handle_specific)
 
        g_assert(!_WAPI_FD_HANDLE(type));
        
-       pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
-                             (void *)&scan_mutex);
        thr_ret = mono_mutex_lock (&scan_mutex);
        g_assert (thr_ret == 0);
                
@@ -470,7 +517,6 @@ _wapi_handle_new (WapiHandleType type, gpointer handle_specific)
        
        thr_ret = mono_mutex_unlock (&scan_mutex);
        g_assert (thr_ret == 0);
-       pthread_cleanup_pop (0);
 
        if (handle_idx == 0) {
                /* We ran out of slots */
@@ -533,8 +579,6 @@ gpointer _wapi_handle_new_from_offset (WapiHandleType type, guint32 offset,
                InterlockedExchange ((gint32 *)&shared->timestamp, now);
        }
                
-       pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
-                             (void *)&scan_mutex);
        thr_ret = mono_mutex_lock (&scan_mutex);
        g_assert (thr_ret == 0);
 
@@ -555,7 +599,6 @@ gpointer _wapi_handle_new_from_offset (WapiHandleType type, guint32 offset,
 first_pass_done:
        thr_ret = mono_mutex_unlock (&scan_mutex);
        g_assert (thr_ret == 0);
-       pthread_cleanup_pop (0);
 
        if (handle != INVALID_HANDLE_VALUE) {
                _wapi_handle_ref (handle);
@@ -583,8 +626,6 @@ first_pass_done:
                goto done;
        }
        
-       pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
-                             (void *)&scan_mutex);
        thr_ret = mono_mutex_lock (&scan_mutex);
        g_assert (thr_ret == 0);
        
@@ -600,7 +641,6 @@ first_pass_done:
                
        thr_ret = mono_mutex_unlock (&scan_mutex);
        g_assert (thr_ret == 0);
-       pthread_cleanup_pop (0);
                
        /* Make sure we left the space for fd mappings */
        g_assert (handle_idx >= _wapi_fd_reserve);
@@ -623,8 +663,6 @@ init_handles_slot (int idx)
 {
        int thr_ret;
 
-       pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
-                                                 (void *)&scan_mutex);
        thr_ret = mono_mutex_lock (&scan_mutex);
        g_assert (thr_ret == 0);
 
@@ -636,7 +674,6 @@ init_handles_slot (int idx)
 
        thr_ret = mono_mutex_unlock (&scan_mutex);
        g_assert (thr_ret == 0);
-       pthread_cleanup_pop (0);
 }
 
 gpointer _wapi_handle_new_fd (WapiHandleType type, int fd,
@@ -742,8 +779,6 @@ _wapi_handle_foreach (WapiHandleType type,
        guint32 i, k;
        int thr_ret;
 
-       pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
-                             (void *)&scan_mutex);
        thr_ret = mono_mutex_lock (&scan_mutex);
        g_assert (thr_ret == 0);
 
@@ -763,7 +798,6 @@ _wapi_handle_foreach (WapiHandleType type,
 
        thr_ret = mono_mutex_unlock (&scan_mutex);
        g_assert (thr_ret == 0);
-       pthread_cleanup_pop (0);
 }
 
 /* This might list some shared handles twice if they are already
@@ -787,8 +821,6 @@ gpointer _wapi_search_handle (WapiHandleType type,
        gboolean found = FALSE;
        int thr_ret;
 
-       pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
-                             (void *)&scan_mutex);
        thr_ret = mono_mutex_lock (&scan_mutex);
        g_assert (thr_ret == 0);
        
@@ -816,7 +848,6 @@ gpointer _wapi_search_handle (WapiHandleType type,
 
        thr_ret = mono_mutex_unlock (&scan_mutex);
        g_assert (thr_ret == 0);
-       pthread_cleanup_pop (0);
 
        if (!found && search_shared && _WAPI_SHARED_HANDLE (type)) {
                /* Not found yet, so search the shared memory too */
@@ -1059,7 +1090,6 @@ static void _wapi_handle_unref_full (gpointer handle, gboolean ignore_private_bu
                        g_assert (thr_ret == 0);
                }
                
-               pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup, (void *)&scan_mutex);
                thr_ret = mono_mutex_lock (&scan_mutex);
 
                DEBUG ("%s: Destroying handle %p", __func__, handle);
@@ -1115,7 +1145,6 @@ static void _wapi_handle_unref_full (gpointer handle, gboolean ignore_private_bu
 
                thr_ret = mono_mutex_unlock (&scan_mutex);
                g_assert (thr_ret == 0);
-               pthread_cleanup_pop (0);
 
                if (early_exit)
                        return;
@@ -1328,7 +1357,7 @@ gboolean DuplicateHandle (gpointer srcprocess, gpointer src,
        if (src == _WAPI_PROCESS_CURRENT) {
                *target = _wapi_process_duplicate ();
        } else if (src == _WAPI_THREAD_CURRENT) {
-               *target = _wapi_thread_duplicate ();
+               g_assert_not_reached ();
        } else {
                _wapi_handle_ref (src);
                *target = src;
@@ -1635,7 +1664,7 @@ gboolean _wapi_handle_get_or_set_share (dev_t device, ino_t inode,
                 */
                if (!file_share_hash) {
                        file_share_hash = g_hash_table_new_full (wapi_share_info_hash, wapi_share_info_equal, NULL, g_free);
-                       InitializeCriticalSection (&file_share_hash_mutex);
+                       mono_mutex_init_recursive (&file_share_hash_mutex);
                }
                        
                tmp.device = device;
@@ -1813,65 +1842,6 @@ void _wapi_handle_check_share (struct _WapiFileShare *share_info, int fd)
                }
        }
 
-       for (i = 0; i < _WAPI_HANDLE_INITIAL_COUNT; i++) {
-               struct _WapiHandleShared *shared;
-               struct _WapiHandle_process *process_handle;
-
-               shared = &_wapi_shared_layout->handles[i];
-               
-               if (shared->type == WAPI_HANDLE_PROCESS) {
-                       DIR *fd_dir;
-                       struct dirent *fd_entry;
-                       char subdir[_POSIX_PATH_MAX];
-
-                       process_handle = &shared->u.process;
-                       pid = process_handle->id;
-               
-                       /* Look in /proc/<pid>/fd/ but ignore
-                        * /proc/<our pid>/fd/<fd>, as we have the
-                        * file open too
-                        */
-                       g_snprintf (subdir, _POSIX_PATH_MAX, "/proc/%d/fd",
-                                   pid);
-                       
-                       fd_dir = opendir (subdir);
-                       if (fd_dir == NULL) {
-                               continue;
-                       }
-
-                       DEBUG ("%s: Looking in %s", __func__, subdir);
-                       
-                       proc_fds = TRUE;
-                       
-                       while ((fd_entry = readdir (fd_dir)) != NULL) {
-                               char path[_POSIX_PATH_MAX];
-                               struct stat link_stat;
-                               
-                               if (!strcmp (fd_entry->d_name, ".") ||
-                                   !strcmp (fd_entry->d_name, "..") ||
-                                   (pid == self &&
-                                    fd == atoi (fd_entry->d_name))) {
-                                       continue;
-                               }
-
-                               g_snprintf (path, _POSIX_PATH_MAX,
-                                           "/proc/%d/fd/%s", pid,
-                                           fd_entry->d_name);
-                               
-                               stat (path, &link_stat);
-                               if (link_stat.st_dev == share_info->device &&
-                                   link_stat.st_ino == share_info->inode) {
-                                       DEBUG ("%s:  Found it at %s",
-                                                  __func__, path);
-
-                                       found = TRUE;
-                               }
-                       }
-                       
-                       closedir (fd_dir);
-               }
-       }
-
        if (proc_fds == FALSE) {
                _wapi_handle_check_share_by_pid (share_info);
        } else if (found == FALSE) {
@@ -1916,8 +1886,6 @@ void _wapi_handle_dump (void)
        guint32 i, k;
        int thr_ret;
        
-       pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
-                             (void *)&scan_mutex);
        thr_ret = mono_mutex_lock (&scan_mutex);
        g_assert (thr_ret == 0);
        
@@ -1943,7 +1911,6 @@ void _wapi_handle_dump (void)
 
        thr_ret = mono_mutex_unlock (&scan_mutex);
        g_assert (thr_ret == 0);
-       pthread_cleanup_pop (0);
 }
 
 static void _wapi_shared_details (gpointer handle_info)
@@ -1966,8 +1933,6 @@ void _wapi_handle_update_refs (void)
        thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_FILESHARE);
        g_assert(thr_ret == 0);
 
-       pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
-                             (void *)&scan_mutex);
        thr_ret = mono_mutex_lock (&scan_mutex);
        
        for(i = SLOT_INDEX (0); i < _wapi_private_handle_slot_count; i++) {
@@ -2002,7 +1967,6 @@ void _wapi_handle_update_refs (void)
 
        thr_ret = mono_mutex_unlock (&scan_mutex);
        g_assert (thr_ret == 0);
-       pthread_cleanup_pop (0);
        
        thr_ret = _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_FILESHARE);