2004-09-28 Dick Porter <dick@ximian.com>
[mono.git] / mono / io-layer / shared.c
index 24878da6d7e653aad1ddfd94e148d6b9d820b7e4..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 ();
        }
@@ -98,6 +102,9 @@ 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
         * report the problem.
@@ -105,8 +112,6 @@ guchar *_wapi_shm_file (_wapi_shm_t type, guint32 segment)
        dir=g_path_get_dirname (file);
        mkdir (dir, 0755);
        g_free (dir);
-               
-       g_snprintf (file, _POSIX_PATH_MAX, "%s", filename);
        
        return(file);
 }
@@ -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,15 +146,21 @@ 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);
+       new_mem=_wapi_shm_file_map (type, segment, NULL, NULL);
        
        return(new_mem);
 }
@@ -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;
@@ -167,7 +180,8 @@ static int _wapi_shm_file_open (const guchar *filename, _wapi_shm_t type,
        if(type==WAPI_SHM_DATA) {
                wanted_size=sizeof(struct _WapiHandleShared_list);
        } else if (type==WAPI_SHM_SCRATCH) {
-               wanted_size=sizeof(struct _WapiHandleScratch);
+               wanted_size=sizeof(struct _WapiHandleScratch) + 
+                               (_WAPI_SHM_SCRATCH_SIZE - MONO_ZERO_ARRAY_LENGTH);
        } else {
                g_assert_not_reached ();
        }
@@ -210,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);
@@ -245,7 +264,7 @@ try_again:
        if(fstat (fd, &statbuf)==-1) {
                g_critical (G_GNUC_PRETTY_FUNCTION ": fstat error: %s",
                            g_strerror (errno));
-               if(*created==TRUE) {
+               if(created && *created==TRUE) {
                        unlink (filename);
                }
                close (fd);
@@ -253,24 +272,27 @@ 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==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);
 }
 
 gpointer _wapi_shm_file_map (_wapi_shm_t type, guint32 segment,
-                            gboolean *created)
+                            gboolean *created, off_t *size)
 {
        gpointer shm_seg;
        int fd;
@@ -290,7 +312,10 @@ gpointer _wapi_shm_file_map (_wapi_shm_t type, guint32 segment,
                close (fd);
                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) {
@@ -317,20 +342,55 @@ gboolean _wapi_shm_attach (struct _WapiHandleShared_list **data,
                           struct _WapiHandleScratch **scratch)
 {
        gboolean data_created=FALSE, scratch_created=FALSE;
-       int tries;
+       off_t data_size, scratch_size;
+       int tries, closing_tries=0;
 
-       *data=_wapi_shm_file_map (WAPI_SHM_DATA, 0, &data_created);
+map_again:
+       *data=_wapi_shm_file_map (WAPI_SHM_DATA, 0, &data_created, &data_size);
        if(*data==NULL) {
                return(FALSE);
        }
        
-       *scratch=_wapi_shm_file_map (WAPI_SHM_SCRATCH, 0, &scratch_created);
+       *scratch=_wapi_shm_file_map (WAPI_SHM_SCRATCH, 0, &scratch_created,
+                                    &scratch_size);
        if(*scratch==NULL) {
                if(data_created) {
                        _wapi_shm_destroy ();
                }
                return(FALSE);
        }
+
+       if(scratch_created)
+               (*scratch)->data_len = scratch_size - 
+                               (sizeof(struct _WapiHandleScratch) - MONO_ZERO_ARRAY_LENGTH);
+
+       if(data_created==FALSE && (*data)->daemon_running==DAEMON_CLOSING) {
+               /* Daemon is closing down, give it a few ms and try
+                * 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) {
+                       /* Still can't get going, so bail out */
+                       g_warning ("The handle daemon is stuck closing");
+                       return(FALSE);
+               }
+               
+               sleepytime.tv_sec=0;
+               sleepytime.tv_nsec=10000000;    /* 10ms */
+                       
+               nanosleep (&sleepytime, NULL);
+               goto map_again;
+       }
        
        if(data_created==TRUE) {
 #ifdef VALGRINDING
@@ -378,22 +438,6 @@ gboolean _wapi_shm_attach (struct _WapiHandleShared_list **data,
                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;
@@ -411,11 +455,31 @@ gboolean _wapi_shm_attach (struct _WapiHandleShared_list **data,
        }
        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) {
@@ -426,6 +490,26 @@ gboolean _wapi_shm_attach (struct _WapiHandleShared_list **data,
                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