Author: dick
Date: 2005-04-25 11:29:19 -0400 (Mon, 25 Apr 2005)
New Revision: 43548

Modified:
   trunk/mono/mono/io-layer/ChangeLog
   trunk/mono/mono/io-layer/error.c
   trunk/mono/mono/io-layer/handles-private.h
   trunk/mono/mono/io-layer/handles.c
   trunk/mono/mono/io-layer/io.c
   trunk/mono/mono/io-layer/mutexes.c
   trunk/mono/mono/io-layer/sockets.c
   trunk/mono/mono/io-layer/wapi-private.h
Log:

2005-04-25  Dick Porter  <[EMAIL PROTECTED]>

        * sockets.c: 
        * io.c: 
        * wapi-private.h (struct _WapiHandleOps): 
        * handles.c (_wapi_handle_unref): When destroying handles, save
        the handle data and call the close function only after the array
        slot has been cleared.  This prevents race conditions with file
        descriptors, fixing bug 74713.

        * mutexes.c: Delete the handle close functions, they didn't do
        anything anyway

        * error.c (_wapi_get_win32_file_error): Add a mapping for EINTR so
        the "Unknown error" g_warning doesn't get displayed.




Modified: trunk/mono/mono/io-layer/ChangeLog
===================================================================
--- trunk/mono/mono/io-layer/ChangeLog  2005-04-25 14:50:48 UTC (rev 43547)
+++ trunk/mono/mono/io-layer/ChangeLog  2005-04-25 15:29:19 UTC (rev 43548)
@@ -1,5 +1,21 @@
 2005-04-25  Dick Porter  <[EMAIL PROTECTED]>
 
+       * sockets.c: 
+       * io.c: 
+       * wapi-private.h (struct _WapiHandleOps): 
+       * handles.c (_wapi_handle_unref): When destroying handles, save
+       the handle data and call the close function only after the array
+       slot has been cleared.  This prevents race conditions with file
+       descriptors, fixing bug 74713.
+
+       * mutexes.c: Delete the handle close functions, they didn't do
+       anything anyway
+
+       * error.c (_wapi_get_win32_file_error): Add a mapping for EINTR so
+       the "Unknown error" g_warning doesn't get displayed.
+
+2005-04-25  Dick Porter  <[EMAIL PROTECTED]>
+
        * wapi-private.h: 
        * threads.c: Make thread handles process-private for now to take
        some of the space pressure off the shared memory, while I work on

Modified: trunk/mono/mono/io-layer/error.c
===================================================================
--- trunk/mono/mono/io-layer/error.c    2005-04-25 14:50:48 UTC (rev 43547)
+++ trunk/mono/mono/io-layer/error.c    2005-04-25 15:29:19 UTC (rev 43548)
@@ -203,6 +203,10 @@
                ret = ERROR_INVALID_HANDLE;
                break;
                
+       case EINTR:
+               ret = ERROR_IO_PENDING;         /* best match I could find */
+               break;
+               
        default:
                g_message ("Unknown errno: %s\n", strerror (err));
                ret = ERROR_GEN_FAILURE;

Modified: trunk/mono/mono/io-layer/handles-private.h
===================================================================
--- trunk/mono/mono/io-layer/handles-private.h  2005-04-25 14:50:48 UTC (rev 
43547)
+++ trunk/mono/mono/io-layer/handles-private.h  2005-04-25 15:29:19 UTC (rev 
43548)
@@ -62,7 +62,7 @@
                                                WapiHandleCapability caps);
 extern gboolean _wapi_handle_test_capabilities (gpointer handle,
                                                WapiHandleCapability caps);
-extern void _wapi_handle_ops_close (gpointer handle);
+extern void _wapi_handle_ops_close (gpointer handle, gpointer data);
 extern void _wapi_handle_ops_signal (gpointer handle);
 extern gboolean _wapi_handle_ops_own (gpointer handle);
 extern gboolean _wapi_handle_ops_isowned (gpointer handle);

Modified: trunk/mono/mono/io-layer/handles.c
===================================================================
--- trunk/mono/mono/io-layer/handles.c  2005-04-25 14:50:48 UTC (rev 43547)
+++ trunk/mono/mono/io-layer/handles.c  2005-04-25 15:29:19 UTC (rev 43548)
@@ -33,6 +33,8 @@
 #undef DEBUG
 #undef DEBUG_REFS
 
+static void (*_wapi_handle_ops_get_close_func (WapiHandleType type))(gpointer, 
gpointer);
+
 static WapiHandleCapability handle_caps[WAPI_HANDLE_COUNT]={0};
 static struct _WapiHandleOps *handle_ops[WAPI_HANDLE_COUNT]={
        NULL,
@@ -914,13 +916,22 @@
 #endif
        
        if(destroy==TRUE) {
+               /* Need to copy the handle info, reset the slot in the
+                * array, and _only then_ call the close function to
+                * avoid race conditions (eg file descriptors being
+                * closed, and another file being opened getting the
+                * same fd racing the memset())
+                */
+               struct _WapiHandleUnshared handle_data;
                WapiHandleType type = _WAPI_PRIVATE_HANDLES(idx).type;
+               void (*close_func)(gpointer, gpointer) = 
_wapi_handle_ops_get_close_func (type);
 
 #ifdef DEBUG
                g_message ("%s: Destroying handle %p", __func__, handle);
 #endif
                
-               _wapi_handle_ops_close (handle);
+               memcpy (&handle_data, &_WAPI_PRIVATE_HANDLES(idx),
+                       sizeof (struct _WapiHandleUnshared));
 
                memset (&_WAPI_PRIVATE_HANDLES(idx).u, '\0',
                        sizeof(_WAPI_PRIVATE_HANDLES(idx).u));
@@ -943,6 +954,10 @@
                /* The garbage collector will take care of shared data
                 * if this is a shared handle
                 */
+               
+               if (close_func != NULL) {
+                       close_func (handle, &handle_data.u);
+               }
        }
 }
 
@@ -968,8 +983,18 @@
        return((handle_caps[type] & caps) != 0);
 }
 
-void _wapi_handle_ops_close (gpointer handle)
+static void (*_wapi_handle_ops_get_close_func (WapiHandleType type))(gpointer, 
gpointer)
 {
+       if (handle_ops[type] != NULL &&
+           handle_ops[type]->close != NULL) {
+               return (handle_ops[type]->close);
+       }
+
+       return (NULL);
+}
+
+void _wapi_handle_ops_close (gpointer handle, gpointer data)
+{
        guint32 idx = GPOINTER_TO_UINT(handle);
        WapiHandleType type;
 
@@ -977,7 +1002,7 @@
 
        if (handle_ops[type] != NULL &&
            handle_ops[type]->close != NULL) {
-               handle_ops[type]->close (handle);
+               handle_ops[type]->close (handle, data);
        }
 }
 
@@ -1664,14 +1689,14 @@
                                struct _WapiHandleSharedMetadata *shared_meta;
                                
 #ifdef DEBUG
-                               g_message ("%s: (%d) handle 0x%x is SHARED", 
__func__,
-                                          getpid (), i);
+                               g_message ("%s: (%d) handle 0x%x is SHARED 
(%s)", __func__,
+                                          getpid (), i * 
_WAPI_HANDLE_INITIAL_COUNT + k, _wapi_handle_typename[handle->type]);
 #endif
 
                                shared_meta = 
&_wapi_shared_layout->metadata[handle->u.shared.offset];
 
 #ifdef DEBUG
-                               g_message ("%s: (%d) Updating timstamp of 
handle 0x%x",
+                               g_message ("%s: (%d) Updating timestamp of 
handle 0x%x",
                                           __func__, getpid(),
                                           handle->u.shared.offset);
 #endif
@@ -1682,7 +1707,7 @@
                                
 #ifdef DEBUG
                                g_message ("%s: (%d) handle 0x%x is FILE", 
__func__,
-                                          getpid (), i);
+                                          getpid (), i * 
_WAPI_HANDLE_INITIAL_COUNT + k);
 #endif
                                
                                g_assert (file_handle->share_info != NULL);

Modified: trunk/mono/mono/io-layer/io.c
===================================================================
--- trunk/mono/mono/io-layer/io.c       2005-04-25 14:50:48 UTC (rev 43547)
+++ trunk/mono/mono/io-layer/io.c       2005-04-25 15:29:19 UTC (rev 43548)
@@ -33,7 +33,7 @@
 static gboolean _wapi_lock_file_region (int fd, off_t offset, off_t length);
 static gboolean _wapi_unlock_file_region (int fd, off_t offset, off_t length);
 
-static void file_close (gpointer handle);
+static void file_close (gpointer handle, gpointer data);
 static WapiFileType file_getfiletype(void);
 static gboolean file_read(gpointer handle, gpointer buffer,
                          guint32 numbytes, guint32 *bytesread,
@@ -77,7 +77,7 @@
                 file->attrs);
 }
 
-static void console_close (gpointer handle);
+static void console_close (gpointer handle, gpointer data);
 static WapiFileType console_getfiletype(void);
 static gboolean console_read(gpointer handle, gpointer buffer,
                             guint32 numbytes, guint32 *bytesread,
@@ -110,7 +110,7 @@
        NULL,                   /* is_owned */
 };
 
-static void pipe_close (gpointer handle);
+static void pipe_close (gpointer handle, gpointer data);
 static WapiFileType pipe_getfiletype (void);
 static gboolean pipe_read (gpointer handle, gpointer buffer, guint32 numbytes,
                           guint32 *bytesread, WapiOverlapped *overlapped);
@@ -232,22 +232,13 @@
 
 /* Handle ops.
  */
-static void file_close (gpointer handle)
+static void file_close (gpointer handle, gpointer data)
 {
-       struct _WapiHandle_file *file_handle;
-       gboolean ok;
+       struct _WapiHandle_file *file_handle = (struct _WapiHandle_file *)data;
        
-       ok=_wapi_lookup_handle (handle, WAPI_HANDLE_FILE,
-                               (gpointer *)&file_handle);
-       if(ok==FALSE) {
-               g_warning ("%s: error looking up file handle %p", __func__,
-                          handle);
-               SetLastError (ERROR_INVALID_HANDLE);
-               return;
-       }
-       
 #ifdef DEBUG
-       g_message("%s: closing file handle %p", __func__, handle);
+       g_message("%s: closing file handle %p [%s]", __func__, handle,
+                 file_handle->filename);
 #endif
 
        g_free (file_handle->filename);
@@ -915,20 +906,10 @@
        return(TRUE);
 }
 
-static void console_close (gpointer handle)
+static void console_close (gpointer handle, gpointer data)
 {
-       struct _WapiHandle_file *console_handle;
-       gboolean ok;
+       struct _WapiHandle_file *console_handle = (struct _WapiHandle_file 
*)data;
        
-       ok=_wapi_lookup_handle (handle, WAPI_HANDLE_CONSOLE,
-                               (gpointer *)&console_handle);
-       if(ok==FALSE) {
-               g_warning ("%s: error looking up console handle %p", __func__,
-                          handle);
-               SetLastError (ERROR_INVALID_HANDLE);
-               return;
-       }
-       
 #ifdef DEBUG
        g_message("%s: closing console handle %p", __func__, handle);
 #endif
@@ -1054,20 +1035,8 @@
        return(TRUE);
 }
 
-static void pipe_close (gpointer handle)
+static void pipe_close (gpointer handle, gpointer data G_GNUC_UNUSED)
 {
-       struct _WapiHandle_file *pipe_handle;
-       gboolean ok;
-       
-       ok=_wapi_lookup_handle (handle, WAPI_HANDLE_PIPE,
-                               (gpointer *)&pipe_handle);
-       if(ok==FALSE) {
-               g_warning ("%s: error looking up pipe handle %p", __func__,
-                          handle);
-               SetLastError (ERROR_INVALID_HANDLE);
-               return;
-       }
-       
 #ifdef DEBUG
        g_message("%s: closing pipe handle %p", __func__, handle);
 #endif

Modified: trunk/mono/mono/io-layer/mutexes.c
===================================================================
--- trunk/mono/mono/io-layer/mutexes.c  2005-04-25 14:50:48 UTC (rev 43547)
+++ trunk/mono/mono/io-layer/mutexes.c  2005-04-25 15:29:19 UTC (rev 43548)
@@ -22,18 +22,16 @@
 
 #undef DEBUG
 
-static void mutex_close (gpointer handle);
 static void mutex_signal(gpointer handle);
 static gboolean mutex_own (gpointer handle);
 static gboolean mutex_is_owned (gpointer handle);
 
-static void namedmutex_close (gpointer handle);
 static void namedmutex_signal (gpointer handle);
 static gboolean namedmutex_own (gpointer handle);
 static gboolean namedmutex_is_owned (gpointer handle);
 
 struct _WapiHandleOps _wapi_mutex_ops = {
-       mutex_close,            /* close */
+       NULL,                   /* close */
        mutex_signal,           /* signal */
        mutex_own,              /* own */
        mutex_is_owned,         /* is_owned */
@@ -53,7 +51,7 @@
 }
 
 struct _WapiHandleOps _wapi_namedmutex_ops = {
-       namedmutex_close,       /* close */
+       NULL,                   /* close */
        namedmutex_signal,      /* signal */
        namedmutex_own,         /* own */
        namedmutex_is_owned,    /* is_owned */
@@ -94,26 +92,6 @@
                                            WAPI_HANDLE_CAP_OWN);
 }
 
-static void mutex_close (gpointer handle)
-{
-       struct _WapiHandle_mutex *mutex_handle;
-       gboolean ok;
-       
-       ok=_wapi_lookup_handle (handle, WAPI_HANDLE_MUTEX,
-                               (gpointer *)&mutex_handle);
-       if(ok==FALSE) {
-               g_warning ("%s: error looking up mutex handle %p", __func__,
-                          handle);
-               return;
-       }
-       
-#ifdef DEBUG
-       g_message("%s: closing mutex handle %p", __func__, handle);
-#endif
-
-       /* If its a shared one, clear the name */
-}
-
 static void mutex_signal(gpointer handle)
 {
        ReleaseMutex(handle);
@@ -186,14 +164,9 @@
        }
 }
 
-static void namedmutex_close (gpointer handle)
-{
-       /**/
-}
-
 static void namedmutex_signal (gpointer handle)
 {
-       /**/
+       ReleaseMutex(handle);
 }
 
 static gboolean namedmutex_own (gpointer handle)

Modified: trunk/mono/mono/io-layer/sockets.c
===================================================================
--- trunk/mono/mono/io-layer/sockets.c  2005-04-25 14:50:48 UTC (rev 43547)
+++ trunk/mono/mono/io-layer/sockets.c  2005-04-25 15:29:19 UTC (rev 43548)
@@ -41,7 +41,7 @@
 static pthread_key_t error_key;
 static mono_once_t error_key_once=MONO_ONCE_INIT;
 
-static void socket_close (gpointer handle);
+static void socket_close (gpointer handle, gpointer data);
 
 struct _WapiHandleOps _wapi_socket_ops = {
        socket_close,           /* close */
@@ -57,7 +57,7 @@
        /* No capabilities to register */
 }
 
-static void socket_close (gpointer handle)
+static void socket_close (gpointer handle, gpointer data G_GNUC_UNUSED)
 {
        int ret;
 
@@ -138,7 +138,7 @@
                gpointer handle;
 
                handle = g_ptr_array_index (sockets, i);
-               _wapi_handle_ops_close (handle);
+               _wapi_handle_ops_close (handle, NULL);
        }
 
        g_ptr_array_free (sockets, FALSE);

Modified: trunk/mono/mono/io-layer/wapi-private.h
===================================================================
--- trunk/mono/mono/io-layer/wapi-private.h     2005-04-25 14:50:48 UTC (rev 
43547)
+++ trunk/mono/mono/io-layer/wapi-private.h     2005-04-25 15:29:19 UTC (rev 
43548)
@@ -68,7 +68,7 @@
 
 struct _WapiHandleOps 
 {
-       void (*close)(gpointer handle);
+       void (*close)(gpointer handle, gpointer data);
 
        /* SignalObjectAndWait */
        void (*signal)(gpointer signal);

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to