2006-12-11 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mono / io-layer / shared.c
index 14849bd2f969657e26e72994cb632de756767b02..b1f764436f37149821e8cb3b95f1d1cb292ae5a3 100644 (file)
@@ -4,42 +4,7 @@
  * Author:
  *     Dick Porter (dick@ximian.com)
  *
- * (C) 2002 Ximian, Inc.
- */
-
-/*
- * Code to support inter-process sharing of handles.
- *
- * I thought of using an mmap()ed file for this.  If linuxthreads
- * supported PTHREAD_PROCESS_SHARED I would have done; however without
- * that pthread support the only other inter-process IPC
- * synchronisation option is a sysV semaphore, and if I'm going to use
- * that I may as well take advantage of sysV shared memory too.
- * Actually, semaphores seem to be buggy, or I was using them
- * incorrectly :-).  I've replaced the sysV semaphore with a shared
- * integer controlled with Interlocked functions.  And I've since
- * replaced that with a separate process to serialise access to the
- * shared memory, to avoid the possibility of DOS by leaving the
- * shared memory locked, and also to allow the shared memory to be
- * cleaned up.
- *
- * mmap() files have the advantage of avoiding namespace collisions,
- * but have the disadvantage of needing cleaning up, and also msync().
- * sysV shared memory has a really stupid way of getting random key
- * IDs, which can lead to collisions.
- *
- * Having tried sysv shm, I tested mmap() and found that MAP_SHARED
- * makes msync() irrelevent, and both types need cleaning up.  Seeing
- * as mmap() doesn't suffer from the bonkers method of allocating
- * segments, it seems to be the best method.
- *
- * This shared memory is needed because w32 processes do not have the
- * POSIX parent-child relationship, so a process handle is available
- * to any other process to find out exit status.  Handles are
- * destroyed when the last reference to them is closed.  New handles
- * can be created for long lasting items such as processes or threads,
- * and also for named synchronisation objects so long as these haven't
- * been deleted by having the last referencing handle closed.
+ * (C) 2002-2006 Novell, Inc.
  */
 
 
 #include <sys/stat.h>
 #include <errno.h>
 #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>
 #include <mono/io-layer/shared.h>
-#include <mono/io-layer/daemon-private.h>
+#include <mono/io-layer/handles-private.h>
 
 #undef DEBUG
 
-/* Define this to make it easier to run valgrind on the daemon.  Then
- * the first process to start will turn into a daemon without forking
- * (the debug utility mono/handles/hps is ideal for this.)
- */
-#undef VALGRINDING
-
-guchar *_wapi_shm_file (_wapi_shm_t type, guint32 segment)
+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];
-
-       if (gethostname(machine_name, sizeof(machine_name)) != 0)
-               machine_name[0] = '\0';
+       const gchar *fake_name;
+       struct utsname ubuf;
+       int ret;
+       int len;
        
-       /* Change the filename whenever the format of the contents
-        * changes
-        */
-       if(type==WAPI_SHM_DATA) {
-               name=g_strdup_printf ("shared_data-%s-%d-%d",
-                                     machine_name, _WAPI_HANDLE_VERSION, segment);
-       } else if (type==WAPI_SHM_SCRATCH) {
-               name=g_strdup_printf ("shared_scratch-%s-%d-%d",
-                                     machine_name, _WAPI_HANDLE_VERSION, segment);
+       ret = uname (&ubuf);
+       if (ret == -1) {
+               ubuf.machine[0] = '\0';
+               ubuf.sysname[0] = '\0';
        } else {
-               g_assert_not_reached ();
+               g_strdelimit (ubuf.sysname, "/", '_');
+               g_strdelimit (ubuf.machine, "/", '_');
+       }
+
+       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-%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-%s-%s-%d-%d-%d",
+                                       machine_name, ubuf.sysname,
+                                       ubuf.machine,
+                                       (int) sizeof(struct _WapiFileShare),
+                                       _WAPI_HANDLE_VERSION, 0);
+               break;
        }
 
        /* I don't know how nfs affects mmap.  If mmap() of files on
         * nfs mounts breaks, then there should be an option to set
         * the directory.
         */
-       wapi_dir=getenv ("MONO_SHARED_DIR");
-       if(wapi_dir==NULL) {
-               filename=g_build_filename (g_get_home_dir (), ".wapi", name,
-                                          NULL);
+       wapi_dir = getenv ("MONO_SHARED_DIR");
+       if (wapi_dir == NULL) {
+               filename = g_build_filename (g_get_home_dir (), ".wapi", name,
+                                            NULL);
        } else {
-               filename=g_build_filename (wapi_dir, ".wapi", name, NULL);
+               filename = g_build_filename (wapi_dir, ".wapi", name, NULL);
        }
        g_free (name);
 
        g_snprintf (file, _POSIX_PATH_MAX, "%s", filename);
+       g_free (filename);
                
        /* No need to check if the dir already exists or check
         * mkdir() errors, because on any error the open() call will
         * report the problem.
         */
-       dir=g_path_get_dirname (file);
+       dir = g_path_get_dirname (file);
        mkdir (dir, 0755);
        g_free (dir);
        
        return(file);
 }
 
-gpointer _wapi_shm_file_expand (gpointer mem, _wapi_shm_t type,
-                               guint32 segment, guint32 old_len,
-                               guint32 new_len)
-{
-       int fd;
-       gpointer new_mem;
-       guchar *filename=_wapi_shm_file (type, segment);
-       int ret;
-
-       if(old_len>=new_len) {
-               return(mem);
-       }
-       
-       munmap (mem, old_len);
-       
-       fd=open (filename, O_RDWR, 0600);
-       if(fd==-1) {
-               g_critical (G_GNUC_PRETTY_FUNCTION
-                           ": shared file [%s] open error: %s", filename,
-                           g_strerror (errno));
-               return(NULL);
-       }
-
-       if(lseek (fd, new_len-1, SEEK_SET)==-1) {
-               g_critical (G_GNUC_PRETTY_FUNCTION
-                           ": shared file [%s] lseek error: %s", filename,
-                           g_strerror (errno));
-               return(NULL);
-       }
-       
-       do {
-               ret=write (fd, "", 1);
-       }
-       while (ret==-1 && errno==EINTR);
-
-       if(ret==-1) {
-               g_critical (G_GNUC_PRETTY_FUNCTION
-                           ": shared file [%s] write error: %s", filename,
-                           g_strerror (errno));
-               return(NULL);
-       }
-
-       close (fd);
-       
-       new_mem=_wapi_shm_file_map (type, segment, NULL, NULL);
-       
-       return(new_mem);
-}
-
-static int _wapi_shm_file_open (const guchar *filename, _wapi_shm_t type,
-                               gboolean *created)
+static int _wapi_shm_file_open (const gchar *filename, guint32 wanted_size)
 {
        int fd;
        struct stat statbuf;
-       guint32 wanted_size = 0;
-       int ret;
+       int ret, tries = 0;
+       gboolean created = FALSE;
        
-       if(created) {
-               *created=FALSE;
-       }
-       
-       if(type==WAPI_SHM_DATA) {
-               wanted_size=sizeof(struct _WapiHandleShared_list);
-       } else if (type==WAPI_SHM_SCRATCH) {
-               wanted_size=sizeof(struct _WapiHandleScratch) + 
-                               (_WAPI_SHM_SCRATCH_SIZE - MONO_ZERO_ARRAY_LENGTH);
-       } else {
-               g_assert_not_reached ();
+try_again:
+       if (tries++ > 10) {
+               /* Just give up */
+               return (-1);
+       } else if (tries > 5) {
+               /* Break out of a loop */
+               unlink (filename);
        }
        
-try_again:
        /* No O_CREAT yet, because we need to initialise the file if
         * we have to create it.
         */
-       fd=open (filename, O_RDWR, 0600);
-       if(fd==-1 && errno==ENOENT) {
+       fd = open (filename, O_RDWR, 0600);
+       if (fd == -1 && errno == ENOENT) {
                /* OK, its up to us to create it.  O_EXCL to avoid a
                 * race condition where two processes can
                 * simultaneously try and create the file
                 */
-               fd=open (filename, O_CREAT|O_EXCL|O_RDWR, 0600);
-               if(fd==-1 && errno==EEXIST) {
+               fd = open (filename, O_CREAT|O_EXCL|O_RDWR, 0600);
+               if (fd == -1 && errno == EEXIST) {
                        /* It's possible that the file was created in
                         * between finding it didn't exist, and trying
                         * to create it.  Just try opening it again
                         */
                        goto try_again;
-               } else if (fd==-1) {
-                       g_critical (G_GNUC_PRETTY_FUNCTION
-                                   ": shared file [%s] open error: %s",
-                                   filename, g_strerror (errno));
+               } else if (fd == -1) {
+                       g_critical ("%s: shared file [%s] open error: %s",
+                                   __func__, filename, g_strerror (errno));
                        return(-1);
                } else {
                        /* We created the file, so we need to expand
-                        * the file and inform the caller so it can
-                        * fork the handle daemon too.
+                        * the file.
                         *
                         * (wanted_size-1, because we're about to
                         * write the other byte to actually expand the
                         * file.)
                         */
-                       if(lseek (fd, wanted_size-1, SEEK_SET)==-1) {
-                               g_critical (G_GNUC_PRETTY_FUNCTION ": shared file [%s] lseek error: %s", filename, g_strerror (errno));
+                       if (lseek (fd, wanted_size-1, SEEK_SET) == -1) {
+                               g_critical ("%s: shared file [%s] lseek error: %s", __func__, filename, g_strerror (errno));
                                close (fd);
                                unlink (filename);
                                return(-1);
                        }
                        
                        do {
-                               ret=write (fd, "", 1);
-                       }
-                       while (ret==-1 && errno==EINTR);
+                               ret = write (fd, "", 1);
+                       } while (ret == -1 && errno == EINTR);
                                
-                       if(ret==-1) {
-                               g_critical (G_GNUC_PRETTY_FUNCTION ": shared file [%s] write error: %s", filename, g_strerror (errno));
+                       if (ret == -1) {
+                               g_critical ("%s: shared file [%s] write error: %s", __func__, filename, g_strerror (errno));
                                close (fd);
                                unlink (filename);
                                return(-1);
                        }
                        
-                       if(created) {
-                               *created=TRUE;
-                       }
+                       created = TRUE;
 
                        /* The contents of the file is set to all
                         * zero, because it is opened up with lseek,
@@ -245,44 +173,40 @@ try_again:
                         * initialisation here
                         */
                }
-       } else if(fd==-1) {
-               g_critical (G_GNUC_PRETTY_FUNCTION
-                           ": shared file [%s] open error: %s", filename,
-                           g_strerror (errno));
+       } else if (fd == -1) {
+               g_critical ("%s: shared file [%s] open error: %s", __func__,
+                           filename, g_strerror (errno));
                return(-1);
        }
        
-       /* From now on, we need to delete the file before exiting on
-        * error if we created it (ie, if *created==TRUE)
-        */
-
        /* Use stat to find the file size (instead of hard coding it)
         * because we can expand the file later if needed (for more
         * handles or scratch space.)
         */
-       if(fstat (fd, &statbuf)==-1) {
-               g_critical (G_GNUC_PRETTY_FUNCTION ": fstat error: %s",
+       if (fstat (fd, &statbuf) == -1) {
+               g_critical ("%s: fstat error: %s", __func__,
                            g_strerror (errno));
-               if(created && *created==TRUE) {
+               if (created == TRUE) {
                        unlink (filename);
                }
                close (fd);
                return(-1);
        }
 
-       if(statbuf.st_size < wanted_size) {
+       if (statbuf.st_size < wanted_size) {
                close (fd);
-               if(created && *created==TRUE) {
+               if (created == TRUE) {
 #ifdef HAVE_LARGE_FILE_SUPPORT
                        /* Keep gcc quiet... */
-                       g_critical (G_GNUC_PRETTY_FUNCTION ": shared file [%s] is not big enough! (found %lld, need %d bytes)", filename, statbuf.st_size, wanted_size);
+                       g_critical ("%s: shared file [%s] is not big enough! (found %lld, need %d bytes)", __func__, filename, statbuf.st_size, wanted_size);
 #else
-                       g_critical (G_GNUC_PRETTY_FUNCTION ": shared file [%s] is not big enough! (found %ld, need %d bytes)", filename, statbuf.st_size, wanted_size);
+                       g_critical ("%s: shared file [%s] is not big enough! (found %ld, need %d bytes)", __func__, filename, statbuf.st_size, wanted_size);
 #endif
                        unlink (filename);
                        return(-1);
                } else {
                        /* We didn't create it, so just try opening it again */
+                       _wapi_handle_spin (100);
                        goto try_again;
                }
        }
@@ -290,244 +214,392 @@ try_again:
        return(fd);
 }
 
-gpointer _wapi_shm_file_map (_wapi_shm_t type, guint32 segment,
-                            gboolean *created, off_t *size)
+/*
+ * _wapi_shm_attach:
+ * @success: Was it a success
+ *
+ * Attach to the shared memory file or create it if it did not exist.
+ * Returns the memory area the file was mmapped to.
+ */
+gpointer _wapi_shm_attach (_wapi_shm_t type)
 {
        gpointer shm_seg;
        int fd;
        struct stat statbuf;
-       guchar *filename=_wapi_shm_file (type, segment);
+       gchar *filename=_wapi_shm_file (type);
+       guint32 size;
        
-       fd=_wapi_shm_file_open (filename, type, created);
-       if(fd==-1) {
-               g_critical (G_GNUC_PRETTY_FUNCTION
-                           ": shared file [%s] open error", filename);
-               return(NULL);
+       switch(type) {
+       case WAPI_SHM_DATA:
+               size = sizeof(struct _WapiHandleSharedLayout);
+               break;
+               
+       case WAPI_SHM_FILESHARE:
+               size = sizeof(struct _WapiFileShareLayout);
+               break;
        }
        
-       if(fstat (fd, &statbuf)==-1) {
-               g_critical (G_GNUC_PRETTY_FUNCTION ": fstat error: %s",
-                           g_strerror (errno));
-               close (fd);
+       fd = _wapi_shm_file_open (filename, size);
+       if (fd == -1) {
+               g_critical ("%s: shared file [%s] open error", __func__,
+                           filename);
                return(NULL);
        }
-       if(size) {
-               *size=statbuf.st_size;
-       }
        
-       shm_seg=mmap (NULL, statbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED,
-                     fd, 0);
-       if(shm_seg==MAP_FAILED) {
-               g_critical (G_GNUC_PRETTY_FUNCTION ": mmap error: %s",
+       if (fstat (fd, &statbuf)==-1) {
+               g_critical ("%s: fstat error: %s", __func__,
                            g_strerror (errno));
                close (fd);
                return(NULL);
        }
+       
+       shm_seg = mmap (NULL, statbuf.st_size, PROT_READ|PROT_WRITE,
+                       MAP_SHARED, fd, 0);
+       if (shm_seg == MAP_FAILED) {
+               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);
        return(shm_seg);
 }
 
-/*
- * _wapi_shm_attach:
- * @success: Was it a success
- *
- * Attach to the shared memory file or create it if it did not
- * exist. If it was created and daemon was FALSE a new daemon is
- * forked into existence. Returns the memory area the file was mmapped
- * to.
- */
-gboolean _wapi_shm_attach (struct _WapiHandleShared_list **data,
-                          struct _WapiHandleScratch **scratch)
+void _wapi_shm_semaphores_init ()
 {
-       gboolean data_created=FALSE, scratch_created=FALSE;
-       off_t data_size, scratch_size;
-       int tries, closing_tries=0;
-
-map_again:
-       *data=_wapi_shm_file_map (WAPI_SHM_DATA, 0, &data_created, &data_size);
-       if(*data==NULL) {
-               return(FALSE);
-       }
+       key_t key;
+       key_t oldkey;
+       int thr_ret;
+       struct _WapiHandleSharedLayout *tmp_shared;
        
-       *scratch=_wapi_shm_file_map (WAPI_SHM_SCRATCH, 0, &scratch_created,
-                                    &scratch_size);
-       if(*scratch==NULL) {
-               if(data_created) {
-                       _wapi_shm_destroy ();
-               }
-               return(FALSE);
+       /* 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
+        */
+       union semun {
+               int val;
+               struct semid_ds *buf;
+               ushort *array;
+       } defs;
+       ushort def_vals[_WAPI_SHARED_SEM_COUNT];
+       int i;
+       int retries = 0;
+       
+       for (i = 0; i < _WAPI_SHARED_SEM_COUNT; i++) {
+               def_vals[i] = 1;
        }
 
-       if(scratch_created)
-               (*scratch)->data_len = scratch_size - 
-                               (sizeof(struct _WapiHandleScratch) - MONO_ZERO_ARRAY_LENGTH);
+       /* 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;
+       
+       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 = tmp_shared->sem_key;
+
+       if (oldkey == 0) {
+#ifdef DEBUG
+               g_message ("%s: Creating with new key (0x%x)", __func__, key);
+#endif
 
-       if(data_created==FALSE && (*data)->daemon_running==DAEMON_CLOSING) {
-               /* Daemon is closing down, give it a few ms and try
-                * again.
+               /* The while loop attempts to make some sense of the
+                * bonkers 'think of a random number' method of
+                * picking a key without collision with other
+                * applications
                 */
-               
-               struct timespec sleepytime;
+               while ((_wapi_sem_id = semget (key, _WAPI_SHARED_SEM_COUNT,
+                                              IPC_CREAT | IPC_EXCL | 0600)) == -1) {
+                       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);
+                       }
                        
-               /* Something must have gone wrong, so delete the
-                * shared segments and try again.
-                */
-               _wapi_shm_destroy ();
-               
-               munmap (*data, data_size);
-               munmap (*scratch, scratch_size);
-               
-               if(closing_tries++ == 5) {
-                       /* Still can't get going, so bail out */
-                       g_warning ("The handle daemon is stuck closing");
-                       return(FALSE);
+                       key++;
+#ifdef DEBUG
+                       g_message ("%s: Got (%s), trying with new key (0x%x)",
+                                  __func__, g_strerror (errno), key);
+#endif
                }
+               /* Got a semaphore array, so initialise it and install
+                * the key into the shared memory
+                */
                
-               sleepytime.tv_sec=0;
-               sleepytime.tv_nsec=10000000;    /* 10ms */
-                       
-               nanosleep (&sleepytime, NULL);
-               goto map_again;
-       }
-       
-       if(data_created==TRUE) {
-#ifdef VALGRINDING
-               /* _wapi_daemon_main() does not return */
-               _wapi_daemon_main (*data, *scratch);
-                       
-               /* But just in case... */
-               (*data)->daemon_running=DAEMON_DIED_AT_STARTUP;
-               exit (-1);
-#else
-               pid_t pid;
-                       
-               pid=fork ();
-               if(pid==-1) {
-                       g_critical (G_GNUC_PRETTY_FUNCTION ": fork error: %s",
-                                   strerror (errno));
-                       _wapi_shm_destroy ();
-                       return(FALSE);
-               } else if (pid==0) {
-                       int i;
-                       
-                       /* child */
-                       setsid ();
-                       
-                       /* FIXME: Set process title to something
-                        * informative
+               if (semctl (_wapi_sem_id, 0, SETALL, defs) == -1) {
+                       if (retries > 3)
+                               g_warning ("%s: semctl init error: %s - trying again", __func__, g_strerror (errno));
+
+                       /* Something went horribly wrong, so try
+                        * getting a new set from scratch
                         */
+                       semctl (_wapi_sem_id, 0, IPC_RMID);
+                       goto again;
+               }
 
-                       /* Start the daemon with a clean sheet of file
-                        * descriptors
+               if (InterlockedCompareExchange (&tmp_shared->sem_key,
+                                               key, 0) != 0) {
+                       /* Someone else created one and installed the
+                        * key while we were working, so delete the
+                        * array we created and fall through to the
+                        * 'key already known' case.
                         */
-                       for(i=3; i<getdtablesize (); i++) {
-                               close (i);
-                       }
-                       
-                       /* _wapi_daemon_main() does not return */
-                       _wapi_daemon_main (*data, *scratch);
-                       
-                       /* But just in case... */
-                       (*data)->daemon_running=DAEMON_DIED_AT_STARTUP;
-                       exit (-1);
+                       semctl (_wapi_sem_id, 0, IPC_RMID);
+                       oldkey = tmp_shared->sem_key;
+               } else {
+                       /* We've installed this semaphore set's key into
+                        * the shared memory
+                        */
+                       goto done;
                }
-               /* parent carries on */
+       }
+       
 #ifdef DEBUG
-               g_message (G_GNUC_PRETTY_FUNCTION ": Daemon pid %d", pid);
+       g_message ("%s: Trying with old key 0x%x", __func__, oldkey);
 #endif
-#endif /* !VALGRINDING */
-       }
-               
-       for(tries=0; (*data)->daemon_running==DAEMON_STARTING && tries < 100;
-           tries++) {
-               /* wait for the daemon to sort itself out.  To be
-                * completely safe, we should have a timeout before
-                * giving up.
+
+       _wapi_sem_id = semget (oldkey, _WAPI_SHARED_SEM_COUNT, 0600);
+       if (_wapi_sem_id == -1) {
+               if (retries > 3)
+                       g_warning ("%s: semget error opening old key 0x%x (%s) - trying again",
+                                       __func__, oldkey,g_strerror (errno));
+
+               /* Someone must have deleted the semaphore set, so
+                * blow away the bad key and try again
                 */
-               struct timespec sleepytime;
-                       
-               sleepytime.tv_sec=0;
-               sleepytime.tv_nsec=10000000;    /* 10ms */
-                       
-               nanosleep (&sleepytime, NULL);
+               InterlockedCompareExchange (&tmp_shared->sem_key, 0, oldkey);
+               
+               goto again;
        }
-       if(tries==100 && (*data)->daemon_running==DAEMON_STARTING) {
-               /* Daemon didnt get going */
-               struct timespec sleepytime;
-                       
-               /* Something must have gone wrong, so delete the
-                * shared segments and try 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);
+
+       g_assert (proc_count > 0);
+       if (proc_count == 1) {
+               /* Just us, so blow away the semaphores and the shared
+                * files
                 */
-               _wapi_shm_destroy ();
+#ifdef DEBUG
+               g_message ("%s: Removing semaphores! (%d)", __func__,
+                          _wapi_getpid ());
+#endif
 
-               /* Daemon didn't get going, give it a few ms and try
-                * again.
+               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...
                 */
-               
-               munmap (*data, data_size);
-               munmap (*scratch, scratch_size);
-               
-               if(closing_tries++ == 5) {
-                       /* Still can't get going, so bail out */
-                       g_warning ("The handle daemon didnt start up properly");
-                       return(FALSE);
-               }
-               
-               sleepytime.tv_sec=0;
-               sleepytime.tv_nsec=10000000;    /* 10ms */
-                       
-               nanosleep (&sleepytime, NULL);
-               goto map_again;
+               _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_PROCESS_COUNT_LOCK);
        }
+}
+
+int _wapi_shm_sem_lock (int sem)
+{
+       struct sembuf ops;
+       int ret;
        
-       if((*data)->daemon_running==DAEMON_DIED_AT_STARTUP) {
-               /* Oh dear, the daemon had an error starting up */
-               if(data_created==TRUE) {
-                       _wapi_shm_destroy ();
+#ifdef DEBUG
+       g_message ("%s: locking sem %d", __func__, sem);
+#endif
+
+       ops.sem_num = 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;
                }
-               g_warning ("Handle daemon failed to start");
-               return(FALSE);
+               
+               /* Turn this into a pthreads-style return value */
+               ret = errno;
        }
+       
+#ifdef DEBUG
+       g_message ("%s: returning %d (%s)", __func__, ret, g_strerror (ret));
+#endif
+       
+       return(ret);
+}
 
-       /* Do some sanity checking on the shared memory we
-        * attached
-        */
-       if(((*data)->daemon_running!=DAEMON_RUNNING) ||
-#ifdef NEED_LINK_UNLINK
-          (strncmp ((*data)->daemon, "/tmp/mono-handle-daemon-",
-                    24)!=0)) {
-#else
-          (strncmp ((*data)->daemon+1, "mono-handle-daemon-", 19)!=0)) {
+int _wapi_shm_sem_trylock (int sem)
+{
+       struct sembuf ops;
+       int ret;
+       
+#ifdef DEBUG
+       g_message ("%s: trying to lock sem %d", __func__, sem);
 #endif
-               g_warning ("Shared memory sanity check failed.");
-               g_warning("status: %d", (*data)->daemon_running);
-#ifdef NEED_LINK_UNLINK
-               g_warning("daemon: [%s]", (*data)->daemon);
-#else
-               g_warning("daemon: [%s]", (*data)->daemon+1);
+       
+       ops.sem_num = 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
-               return(FALSE);
-       }
+
+                       _wapi_shm_semaphores_init ();
+                       goto retry;
+               }
                
-       /* From now on, it's up to the daemon to delete the shared
-        * memory segment
-        */
+               /* Turn this into a pthreads-style return value */
+               ret = errno;
+       }
+       
+       if (ret == EAGAIN) {
+               /* But pthreads uses this code instead */
+               ret = EBUSY;
+       }
+       
+#ifdef DEBUG
+       g_message ("%s: returning %d (%s)", __func__, ret, g_strerror (ret));
+#endif
        
-       return(TRUE);
+       return(ret);
 }
 
-void _wapi_shm_destroy (void)
+int _wapi_shm_sem_unlock (int sem)
 {
-#ifndef DISABLE_SHARED_HANDLES
+       struct sembuf ops;
+       int ret;
+       
 #ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": unlinking shared data");
+       g_message ("%s: unlocking sem %d", __func__, sem);
 #endif
-       /* Only delete the first segments.  The daemon will destroy
-        * any others when it exits
-        */
-       unlink (_wapi_shm_file (WAPI_SHM_DATA, 0));
-       unlink (_wapi_shm_file (WAPI_SHM_SCRATCH, 0));
-#endif /* DISABLE_SHARED_HANDLES */
+       
+       ops.sem_num = 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;
+       }
+       
+#ifdef DEBUG
+       g_message ("%s: returning %d (%s)", __func__, ret, g_strerror (ret));
+#endif
+
+       return(ret);
 }