2006-03-22 Dick Porter <dick@ximian.com>
authorDick Porter <dick@acm.org>
Fri, 24 Mar 2006 12:19:30 +0000 (12:19 -0000)
committerDick Porter <dick@acm.org>
Fri, 24 Mar 2006 12:19:30 +0000 (12:19 -0000)
        * handles.c:
        * wapi-private.h:
        * shared.h:
        * shared.c: Delete the semaphores and shared files when the last
        process has finished with them

svn path=/trunk/mono/; revision=58409

mono/io-layer/ChangeLog
mono/io-layer/handles.c
mono/io-layer/shared.c
mono/io-layer/shared.h
mono/io-layer/wapi-private.h

index 1b59d9f25ce6b78cb661333c87b59c77db69c46d..3a8de9d05de82339ed3351d28aeb0c59effc0ac9 100644 (file)
@@ -1,3 +1,11 @@
+2006-03-22  Dick Porter  <dick@ximian.com>
+
+       * handles.c:
+       * wapi-private.h:
+       * shared.h:
+       * shared.c: Delete the semaphores and shared files when the last
+       process has finished with them
+
 2006-03-15  Dick Porter  <dick@ximian.com>
 
        * events.c: 
index e827cc11535c7f770a13429b7d739eea9ed14a4f..cab0dec8b5fedecd59128a2817704068c149ab42 100644 (file)
@@ -4,7 +4,7 @@
  * Author:
  *     Dick Porter (dick@ximian.com)
  *
- * (C) 2002-2006 Ximian, Inc.
+ * (C) 2002-2006 Novell, Inc.
  */
 
 #include <config.h>
@@ -132,6 +132,11 @@ pid_t _wapi_getpid (void)
 
 static mono_mutex_t scan_mutex = MONO_MUTEX_INITIALIZER;
 
+static void handle_cleanup (void)
+{
+       _wapi_shm_semaphores_remove ();
+}
+
 static mono_once_t shared_init_once = MONO_ONCE_INIT;
 static void shared_init (void)
 {
@@ -149,11 +154,11 @@ static void shared_init (void)
 
                _wapi_private_handle_count += _WAPI_HANDLE_INITIAL_COUNT;
        } while(_wapi_fd_reserve > _wapi_private_handle_count);
+
+       _wapi_shm_semaphores_init ();
        
        _wapi_shared_layout = _wapi_shm_attach (WAPI_SHM_DATA);
        g_assert (_wapi_shared_layout != NULL);
-
-       _wapi_shm_semaphores_init ();
        
        _wapi_fileshare_layout = _wapi_shm_attach (WAPI_SHM_FILESHARE);
        g_assert (_wapi_fileshare_layout != NULL);
@@ -165,6 +170,13 @@ static void shared_init (void)
        
        thr_ret = mono_mutex_init(&_wapi_global_signal_mutex, NULL);
        g_assert (thr_ret == 0);
+
+       /* Using g_atexit here instead of an explicit function call in
+        * a cleanup routine lets us cope when a third-party library
+        * calls exit (eg if an X client loses the connection to its
+        * server.)
+        */
+       g_atexit (handle_cleanup);
 }
 
 static void _wapi_handle_init_shared (struct _WapiHandleShared *handle,
@@ -1709,3 +1721,4 @@ void _wapi_handle_update_refs (void)
 
        _wapi_handle_unlock_shared_handles ();
 }
+
index 8f7f28570c5a8d374b12824499993a0a576ce341..3e92c3436e00d0caf2c3e0cd0e7578623c84bf8e 100644 (file)
@@ -4,7 +4,7 @@
  * Author:
  *     Dick Porter (dick@ximian.com)
  *
- * (C) 2002 Ximian, Inc.
+ * (C) 2002-2006 Novell, Inc.
  */
 
 
@@ -34,7 +34,7 @@ static guchar *_wapi_shm_file (_wapi_shm_t type)
        static guchar file[_POSIX_PATH_MAX];
        guchar *name = NULL, *filename, *dir, *wapi_dir;
        gchar machine_name[256];
-       gchar *fake_name;
+       const gchar *fake_name;
        struct utsname ubuf;
        int ret;
        int len;
@@ -259,9 +259,11 @@ gpointer _wapi_shm_attach (_wapi_shm_t type)
 
 void _wapi_shm_semaphores_init ()
 {
-       key_t key = ftok (_wapi_shm_file (WAPI_SHM_DATA), 'M');
+       key_t key;
        key_t oldkey;
-
+       int thr_ret;
+       struct _WapiHandleSharedLayout *tmp_shared;
+       
        /* Yet more barmy API - this union is a well-defined parameter
         * in a syscall, yet I still have to define it here as it
         * doesn't appear in a header
@@ -278,11 +280,31 @@ void _wapi_shm_semaphores_init ()
        for (i = 0; i < _WAPI_SHARED_SEM_COUNT; i++) {
                def_vals[i] = 1;
        }
+#ifdef NEXT_VERSION_INC
+       /* Process count must start at '0' - the 1 for all the others
+        * sets the semaphore to "unlocked"
+        */
+       def_vals[_WAPI_SHARED_SEM_PROCESS_COUNT] = 0;
+#endif
+       
        defs.array = def_vals;
        
+       /* Temporarily attach the shared data so we can read the
+        * semaphore key.  We release this mapping and attach again
+        * after getting the semaphores to avoid a race condition
+        * where a terminating process can delete the shared files
+        * between a new process attaching the file and getting access
+        * to the semaphores (which increments the process count,
+        * preventing destruction of the shared data...)
+        */
+       tmp_shared = _wapi_shm_attach (WAPI_SHM_DATA);
+       g_assert (tmp_shared != NULL);
+       
+       key = ftok (_wapi_shm_file (WAPI_SHM_DATA), 'M');
+
 again:
        retries++;
-       oldkey = _wapi_shared_layout->sem_key;
+       oldkey = tmp_shared->sem_key;
 
        if (oldkey == 0) {
 #ifdef DEBUG
@@ -328,7 +350,7 @@ again:
                        goto again;
                }
 
-               if (InterlockedCompareExchange (&_wapi_shared_layout->sem_key,
+               if (InterlockedCompareExchange (&tmp_shared->sem_key,
                                                key, 0) != 0) {
                        /* Someone else created one and installed the
                         * key while we were working, so delete the
@@ -336,12 +358,12 @@ again:
                         * 'key already known' case.
                         */
                        semctl (_wapi_sem_id, 0, IPC_RMID);
-                       oldkey = _wapi_shared_layout->sem_key;
+                       oldkey = tmp_shared->sem_key;
                } else {
                        /* We've installed this semaphore set's key into
                         * the shared memory
                         */
-                       return;
+                       goto done;
                }
        }
        
@@ -358,11 +380,80 @@ again:
                /* Someone must have deleted the semaphore set, so
                 * blow away the bad key and try again
                 */
-               InterlockedCompareExchange (&_wapi_shared_layout->sem_key, 0,
-                                           oldkey);
+               InterlockedCompareExchange (&tmp_shared->sem_key, 0, oldkey);
                
                goto again;
        }
+
+  done:
+       /* Increment the usage count of this semaphore set */
+       thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
+       g_assert (thr_ret == 0);
+       
+#ifdef DEBUG
+       g_message ("%s: Incrementing the process count", __func__);
+#endif
+
+       /* We only ever _unlock_ this semaphore, letting the kernel
+        * restore (ie decrement) this unlock when this process exits.
+        * We lock another semaphore around it so we can serialise
+        * access when we're testing the value of this semaphore when
+        * we exit cleanly, so we can delete the whole semaphore set.
+        */
+       _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_PROCESS_COUNT);
+
+#ifdef DEBUG
+       g_message ("%s: Process count is now %d", __func__, semctl (_wapi_sem_id, _WAPI_SHARED_SEM_PROCESS_COUNT, GETVAL));
+#endif
+       
+       _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
+
+       munmap (tmp_shared, sizeof(struct _WapiHandleSharedLayout));
+}
+
+void _wapi_shm_semaphores_remove (void)
+{
+       int thr_ret;
+       int proc_count;
+       
+#ifdef DEBUG
+       g_message ("%s: Checking process count", __func__);
+#endif
+
+       thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
+       g_assert (thr_ret == 0);
+       
+       proc_count = semctl (_wapi_sem_id, _WAPI_SHARED_SEM_PROCESS_COUNT,
+                            GETVAL);
+#ifdef NEXT_VERSION_INC
+       g_assert (proc_count > 0);
+       if (proc_count == 1) {
+#else
+       /* Compatibility - the semaphore was initialised to '1' (which
+        * normally means 'unlocked'.  Instead of fixing that right
+        * now, which would mean a shared file version increment, just
+        * cope with the value starting too high for now.  Fix this
+        * next time I have to change the file version.
+        */
+       g_assert (proc_count > 1);
+       if (proc_count == 2) {
+#endif
+               /* Just us, so blow away the semaphores and the shared
+                * files
+                */
+#ifdef DEBUG
+               g_message ("%s: Removing semaphores!", __func__);
+#endif
+
+               semctl (_wapi_sem_id, IPC_RMID, 0);
+               unlink (_wapi_shm_file (WAPI_SHM_DATA));
+               unlink (_wapi_shm_file (WAPI_SHM_FILESHARE));
+       } else {
+               /* "else" clause, because there's no point unlocking
+                * the semaphore if we've just blown it away...
+                */
+               _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
+       }
 }
 
 int _wapi_shm_sem_lock (int sem)
@@ -378,11 +469,28 @@ int _wapi_shm_sem_lock (int sem)
        ops.sem_op = -1;
        ops.sem_flg = SEM_UNDO;
        
+  retry:
        do {
                ret = semop (_wapi_sem_id, &ops, 1);
        } while (ret == -1 && errno == EINTR);
 
        if (ret == -1) {
+               /* EINVAL covers the case when the semaphore was
+                * deleted before we started the semop
+                */
+               if (errno == EIDRM || errno == EINVAL) {
+                       /* Someone blew away this semaphore set, so
+                        * get a new one and try again
+                        */
+#ifdef DEBUG
+                       g_message ("%s: Reinitialising the semaphores!",
+                                  __func__);
+#endif
+
+                       _wapi_shm_semaphores_init ();
+                       goto retry;
+               }
+               
                /* Turn this into a pthreads-style return value */
                ret = errno;
        }
@@ -407,11 +515,28 @@ int _wapi_shm_sem_trylock (int sem)
        ops.sem_op = -1;
        ops.sem_flg = IPC_NOWAIT | SEM_UNDO;
        
+  retry:
        do {
                ret = semop (_wapi_sem_id, &ops, 1);
        } while (ret == -1 && errno == EINTR);
 
        if (ret == -1) {
+               /* EINVAL covers the case when the semaphore was
+                * deleted before we started the semop
+                */
+               if (errno == EIDRM || errno == EINVAL) {
+                       /* Someone blew away this semaphore set, so
+                        * get a new one and try again
+                        */
+#ifdef DEBUG
+                       g_message ("%s: Reinitialising the semaphores!",
+                                  __func__);
+#endif
+
+                       _wapi_shm_semaphores_init ();
+                       goto retry;
+               }
+               
                /* Turn this into a pthreads-style return value */
                ret = errno;
        }
@@ -441,11 +566,29 @@ int _wapi_shm_sem_unlock (int sem)
        ops.sem_op = 1;
        ops.sem_flg = SEM_UNDO;
        
+  retry:
        do {
                ret = semop (_wapi_sem_id, &ops, 1);
        } while (ret == -1 && errno == EINTR);
 
        if (ret == -1) {
+               /* EINVAL covers the case when the semaphore was
+                * deleted before we started the semop
+                */
+               if (errno == EIDRM || errno == EINVAL) {
+                       /* Someone blew away this semaphore set, so
+                        * get a new one and try again (we can't just
+                        * assume that the semaphore is now unlocked)
+                        */
+#ifdef DEBUG
+                       g_message ("%s: Reinitialising the semaphores!",
+                                  __func__);
+#endif
+
+                       _wapi_shm_semaphores_init ();
+                       goto retry;
+               }
+               
                /* Turn this into a pthreads-style return value */
                ret = errno;
        }
index af31fe1d9085d857c74d08808e24882c066b06fc..be256f23fc6a3cd0a009e5e2bb024e7e0cf3b85a 100644 (file)
@@ -4,7 +4,7 @@
  * Author:
  *     Dick Porter (dick@ximian.com)
  *
- * (C) 2002 Ximian, Inc.
+ * (C) 2002-2006 Novell, Inc.
  */
 
 #ifndef _WAPI_SHARED_H_
@@ -19,6 +19,7 @@ typedef enum {
 
 extern gpointer _wapi_shm_attach (_wapi_shm_t type);
 extern void _wapi_shm_semaphores_init (void);
+extern void _wapi_shm_semaphores_remove (void);
 extern int _wapi_shm_sem_lock (int sem);
 extern int _wapi_shm_sem_trylock (int sem);
 extern int _wapi_shm_sem_unlock (int sem);
index d82fbaf2564636dbc7663dede98ce641ecff1b93..918c1d4ee312b6219cdcb736a06cbd5423cf5ddc 100644 (file)
@@ -4,7 +4,7 @@
  * Author:
  *     Dick Porter (dick@ximian.com)
  *
- * (C) 2002 Ximian, Inc.
+ * (C) 2002-2006 Novell, Inc.
  */
 
 #ifndef _WAPI_PRIVATE_H_
@@ -24,6 +24,7 @@
 /* Increment this whenever an incompatible change is made to the
  * shared handle structure.
  */
+/* Next time I change this, remember to fix the process count in shared.c */
 #define _WAPI_HANDLE_VERSION 10
 
 typedef enum {
@@ -165,6 +166,8 @@ struct _WapiHandleShared
 /*#define _WAPI_SHARED_SEM_COLLECTION 1*/
 #define _WAPI_SHARED_SEM_FILESHARE 2
 #define _WAPI_SHARED_SEM_SHARED_HANDLES 3
+#define _WAPI_SHARED_SEM_PROCESS_COUNT_LOCK 6
+#define _WAPI_SHARED_SEM_PROCESS_COUNT 7
 #define _WAPI_SHARED_SEM_COUNT 8       /* Leave some future expansion space */
 
 struct _WapiHandleSharedLayout