2004-09-28 Dick Porter <dick@ximian.com>
[mono.git] / mono / io-layer / shared.c
index e57f5e25aa83a2ca4f7f1dca99a2481db527f265..b0e828a222107e17c6c5f2dbb2ccf9ed4dc79d7b 100644 (file)
 guchar *_wapi_shm_file (_wapi_shm_t type, guint32 segment)
 {
        static guchar file[_POSIX_PATH_MAX];
-       guchar *name, *filename, *dir, *wapi_dir;
+       guchar *name = NULL, *filename, *dir, *wapi_dir;
+       gchar machine_name[256];
+
+       if (gethostname(machine_name, sizeof(machine_name)) != 0)
+               machine_name[0] = '\0';
        
        /* Change the filename whenever the format of the contents
         * changes
         */
        if(type==WAPI_SHM_DATA) {
-               name=g_strdup_printf ("shared_data-%d-%d",
-                                     _WAPI_HANDLE_VERSION, segment);
+               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-%d-%d",
-                                     _WAPI_HANDLE_VERSION, segment);
+               name=g_strdup_printf ("shared_scratch-%s-%d-%d",
+                                     machine_name, _WAPI_HANDLE_VERSION, segment);
        } else {
                g_assert_not_reached ();
        }
@@ -99,6 +103,7 @@ guchar *_wapi_shm_file (_wapi_shm_t type, guint32 segment)
        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
@@ -118,6 +123,7 @@ gpointer _wapi_shm_file_expand (gpointer mem, _wapi_shm_t type,
        int fd;
        gpointer new_mem;
        guchar *filename=_wapi_shm_file (type, segment);
+       int ret;
 
        if(old_len>=new_len) {
                return(mem);
@@ -140,12 +146,18 @@ gpointer _wapi_shm_file_expand (gpointer mem, _wapi_shm_t type,
                return(NULL);
        }
        
-       if(write (fd, "", 1)==-1) {
+       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);
@@ -158,7 +170,8 @@ static int _wapi_shm_file_open (const guchar *filename, _wapi_shm_t type,
 {
        int fd;
        struct stat statbuf;
-       guint32 wanted_size;
+       guint32 wanted_size = 0;
+       int ret;
        
        if(created) {
                *created=FALSE;
@@ -211,7 +224,12 @@ try_again:
                                return(-1);
                        }
                        
-                       if(write (fd, "", 1)==-1) {
+                       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));
                                close (fd);
                                unlink (filename);
@@ -254,17 +272,20 @@ try_again:
        }
 
        if(statbuf.st_size < wanted_size) {
+               close (fd);
+               if(created && *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);
+                       /* 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);
 #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 (G_GNUC_PRETTY_FUNCTION ": shared file [%s] is not big enough! (found %ld, need %d bytes)", filename, statbuf.st_size, wanted_size);
 #endif
-               if(created && *created==TRUE) {
                        unlink (filename);
+                       return(-1);
+               } else {
+                       /* We didn't create it, so just try opening it again */
+                       goto try_again;
                }
-               close (fd);
-               return(-1);
        }
        
        return(fd);
@@ -350,14 +371,17 @@ map_again:
                
                struct timespec sleepytime;
                        
+               /* 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) {
-                       /* Something must have gone wrong, so bail
-                        * out.  This will let the calling code delete
-                        * the shared segments and try again.
-                        */
+                       /* Still can't get going, so bail out */
+                       g_warning ("The handle daemon is stuck closing");
                        return(FALSE);
                }
                
@@ -414,22 +438,6 @@ map_again:
                g_message (G_GNUC_PRETTY_FUNCTION ": Daemon pid %d", pid);
 #endif
 #endif /* !VALGRINDING */
-       } else {
-               /* Do some sanity checking on the shared memory we
-                * attached
-                */
-               if(!((*data)->daemon_running==DAEMON_STARTING || 
-                    (*data)->daemon_running==DAEMON_RUNNING ||
-                    (*data)->daemon_running==DAEMON_DIED_AT_STARTUP) ||
-#ifdef NEED_LINK_UNLINK
-                  (strncmp ((*data)->daemon, "/tmp/mono-handle-daemon-",
-                            24)!=0)) {
-#else
-                  (strncmp ((*data)->daemon+1, "mono-handle-daemon-", 19)!=0)) {
-#endif
-                       g_warning ("Shared memory sanity check failed.");
-                       return(FALSE);
-               }
        }
                
        for(tries=0; (*data)->daemon_running==DAEMON_STARTING && tries < 100;
@@ -447,11 +455,31 @@ map_again:
        }
        if(tries==100 && (*data)->daemon_running==DAEMON_STARTING) {
                /* Daemon didnt get going */
-               if(data_created==TRUE) {
-                       _wapi_shm_destroy ();
+               struct timespec sleepytime;
+                       
+               /* Something must have gone wrong, so delete the
+                * shared segments and try again.
+                */
+               _wapi_shm_destroy ();
+
+               /* Daemon didn't get going, give it a few ms and try
+                * again.
+                */
+               
+               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);
                }
-               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;
        }
        
        if((*data)->daemon_running==DAEMON_DIED_AT_STARTUP) {
@@ -462,6 +490,26 @@ map_again:
                g_warning ("Handle daemon failed to start");
                return(FALSE);
        }
+
+       /* 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)) {
+#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);
+#endif
+               return(FALSE);
+       }
                
        /* From now on, it's up to the daemon to delete the shared
         * memory segment