merge r67228-r67235, r67237, r67251 and r67256-67259 to trunk (they are
[mono.git] / mono / io-layer / shared.c
index ee9375c7e29849aad615e359838548bdab9e12f5..a5ba9316c3291a8d756a097af31bd1a4197588b7 100644 (file)
@@ -4,7 +4,7 @@
  * Author:
  *     Dick Porter (dick@ximian.com)
  *
- * (C) 2002 Ximian, Inc.
+ * (C) 2002-2006 Novell, Inc.
  */
 
 
@@ -20,6 +20,7 @@
 #include <string.h>
 #include <sys/ipc.h>
 #include <sys/sem.h>
+#include <sys/utsname.h>
 
 #include <mono/io-layer/wapi.h>
 #include <mono/io-layer/wapi-private.h>
 
 #undef DEBUG
 
-static guchar *_wapi_shm_file (_wapi_shm_t type)
+static gchar *_wapi_shm_file (_wapi_shm_t type)
 {
-       static guchar file[_POSIX_PATH_MAX];
-       guchar *name = NULL, *filename, *dir, *wapi_dir;
+       static gchar file[_POSIX_PATH_MAX];
+       gchar *name = NULL, *filename, *dir, *wapi_dir;
        gchar machine_name[256];
+       const gchar *fake_name;
+       struct utsname ubuf;
+       int ret;
+       int len;
+       
+       ret = uname (&ubuf);
+       if (ret == -1) {
+               ubuf.machine[0] = '\0';
+               ubuf.sysname[0] = '\0';
+       } else {
+               g_strdelimit (ubuf.sysname, "/", '_');
+               g_strdelimit (ubuf.machine, "/", '_');
+       }
 
-       if (gethostname(machine_name, sizeof(machine_name)) != 0)
-               machine_name[0] = '\0';
+       fake_name = g_getenv ("MONO_SHARED_HOSTNAME");
+       if (fake_name == NULL) {
+               if (gethostname(machine_name, sizeof(machine_name)) != 0)
+                       machine_name[0] = '\0';
+       } else {
+               len = MIN (strlen (fake_name), sizeof (machine_name) - 1);
+               strncpy (machine_name, fake_name, len);
+               machine_name [len] = '\0';
+       }
        
        switch (type) {
        case WAPI_SHM_DATA:
-               name = g_strdup_printf ("shared_data-%s-%d-%d",
-                                       machine_name, _WAPI_HANDLE_VERSION, 0);
+               name = g_strdup_printf ("shared_data-%s-%s-%s-%d-%d-%d",
+                                       machine_name, ubuf.sysname,
+                                       ubuf.machine,
+                                       (int) sizeof(struct _WapiHandleShared),
+                                       _WAPI_HANDLE_VERSION, 0);
                break;
                
        case WAPI_SHM_FILESHARE:
-               name = g_strdup_printf ("shared_fileshare-%s-%d-%d",
-                                       machine_name, _WAPI_HANDLE_VERSION, 0);
+               name = g_strdup_printf ("shared_fileshare-%s-%s-%s-%d-%d-%d",
+                                       machine_name, ubuf.sysname,
+                                       ubuf.machine,
+                                       (int) sizeof(struct _WapiFileShare),
+                                       _WAPI_HANDLE_VERSION, 0);
                break;
        }
 
@@ -76,14 +103,22 @@ static guchar *_wapi_shm_file (_wapi_shm_t type)
        return(file);
 }
 
-static int _wapi_shm_file_open (const guchar *filename, guint32 wanted_size)
+static int _wapi_shm_file_open (const gchar *filename, guint32 wanted_size)
 {
        int fd;
        struct stat statbuf;
-       int ret;
+       int ret, tries = 0;
        gboolean created = FALSE;
        
 try_again:
+       if (tries++ > 10) {
+               /* Just give up */
+               return (-1);
+       } else if (tries > 5) {
+               /* Break out of a loop */
+               unlink (filename);
+       }
+       
        /* No O_CREAT yet, because we need to initialise the file if
         * we have to create it.
         */
@@ -171,6 +206,7 @@ try_again:
                        return(-1);
                } else {
                        /* We didn't create it, so just try opening it again */
+                       _wapi_handle_spin (100);
                        goto try_again;
                }
        }
@@ -190,7 +226,7 @@ gpointer _wapi_shm_attach (_wapi_shm_t type)
        gpointer shm_seg;
        int fd;
        struct stat statbuf;
-       guchar *filename=_wapi_shm_file (type);
+       gchar *filename=_wapi_shm_file (type);
        guint32 size;
        
        switch(type) {
@@ -220,10 +256,13 @@ gpointer _wapi_shm_attach (_wapi_shm_t type)
        shm_seg = mmap (NULL, statbuf.st_size, PROT_READ|PROT_WRITE,
                        MAP_SHARED, fd, 0);
        if (shm_seg == MAP_FAILED) {
-               g_critical ("%s: mmap error: %s", __func__,
-                           g_strerror (errno));
-               close (fd);
-               return(NULL);
+               shm_seg = mmap (NULL, statbuf.st_size, PROT_READ|PROT_WRITE,
+                       MAP_PRIVATE, fd, 0);
+               if (shm_seg == MAP_FAILED) {
+                       g_critical ("%s: mmap error: %s", __func__, g_strerror (errno));
+                       close (fd);
+                       return(NULL);
+               }
        }
                
        close (fd);
@@ -232,9 +271,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
@@ -251,11 +292,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
@@ -269,7 +330,12 @@ again:
                 */
                while ((_wapi_sem_id = semget (key, _WAPI_SHARED_SEM_COUNT,
                                               IPC_CREAT | IPC_EXCL | 0600)) == -1) {
-                       if (errno != EEXIST) {
+                       if (errno == ENOMEM) {
+                               g_critical ("%s: semget error: %s", __func__,
+                                           g_strerror (errno));
+                       } else if (errno == ENOSPC) {
+                               g_critical ("%s: semget error: %s.  Try deleting some semaphores with ipcs and ipcrm", __func__, g_strerror (errno));
+                       } else if (errno != EEXIST) {
                                if (retries > 3)
                                        g_warning ("%s: semget error: %s key 0x%x - trying again", __func__,
                                                        g_strerror (errno), key);
@@ -296,7 +362,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
@@ -304,12 +370,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;
                }
        }
        
@@ -326,11 +392,82 @@ 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 (%d)", __func__, _wapi_getpid ());
+#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 (%d)", __func__, semctl (_wapi_sem_id, _WAPI_SHARED_SEM_PROCESS_COUNT, GETVAL), _wapi_getpid ());
+#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 (%d)", __func__,
+                  _wapi_getpid ());
+#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! (%d)", __func__,
+                          _wapi_getpid ());
+#endif
+
+               semctl (_wapi_sem_id, 0, IPC_RMID);
+               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)
@@ -346,11 +483,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;
        }
@@ -375,11 +529,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;
        }
@@ -409,11 +580,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;
        }