2004-08-11 Dick Porter <dick@ximian.com>
authorDick Porter <dick@acm.org>
Wed, 11 Aug 2004 13:07:13 +0000 (13:07 -0000)
committerDick Porter <dick@acm.org>
Wed, 11 Aug 2004 13:07:13 +0000 (13:07 -0000)
* sockets.c:
* io.c: Returned handle values are the file descriptor the handle
encapsulates

* handles.c:
* handles-private.h:
* daemon.c: Reserve the range of handles that can have the same
values as file descriptors.  These won't be used, but the values
will be used as file, console, pipe or socket handles.  The fd to
handle mapping is done internally and is invisible to users.
Fixes bug 61828.

* wapi-private.h (_WAPI_HANDLE_VERSION): Increment, because we now
reserve a chunk of handle space.

svn path=/trunk/mono/; revision=32203

mono/io-layer/ChangeLog
mono/io-layer/daemon.c
mono/io-layer/handles-private.h
mono/io-layer/handles.c
mono/io-layer/io.c
mono/io-layer/sockets.c
mono/io-layer/wapi-private.h

index 55e249a74afc85b9ffdc8135242967e902808ecf..5dc428485eef10172ec1954b2311faa2f53fa91b 100644 (file)
@@ -1,3 +1,20 @@
+2004-08-11  Dick Porter  <dick@ximian.com>
+
+       * sockets.c: 
+       * io.c: Returned handle values are the file descriptor the handle
+       encapsulates
+
+       * handles.c: 
+       * handles-private.h: 
+       * daemon.c: Reserve the range of handles that can have the same
+       values as file descriptors.  These won't be used, but the values
+       will be used as file, console, pipe or socket handles.  The fd to
+       handle mapping is done internally and is invisible to users.
+       Fixes bug 61828.
+
+       * wapi-private.h (_WAPI_HANDLE_VERSION): Increment, because we now
+       reserve a chunk of handle space.
+
 2004-08-04  Neale Ferguson <Neale.Ferguson@SoftwareAG-usa.com>
 
        * atomic.h: add support for 64-bit S/390
index 62aa75ce1f335d8a18b7e0fcbc90aa92ad9791c5..1f25bfcda3f044043ebf5812e59ec1715096b963 100644 (file)
@@ -1491,6 +1491,8 @@ void _wapi_daemon_main(gpointer data, gpointer scratch)
        /* Note that we've got the starting segment already */
        _wapi_shared_data[0]->num_segments=1;
        _wapi_shm_mapped_segments=1;
+
+       _wapi_fd_offset_table_size=getdtablesize ();
        
        startup ();
        
index 674e01384b9636f531a4a6083ed7a8a41cf960c5..c3043461c54bc31e22809e97c6207f744bf88e35 100644 (file)
@@ -28,6 +28,9 @@ extern struct _WapiHandlePrivate_list **_wapi_private_data;
 extern pthread_mutex_t _wapi_shared_mutex;
 extern guint32 _wapi_shm_mapped_segments;
 
+extern guint32 _wapi_fd_offset_table_size;
+extern gpointer *_wapi_fd_offset_table;
+
 extern guint32 _wapi_handle_new_internal (WapiHandleType type);
 extern gpointer _wapi_handle_new (WapiHandleType type);
 extern gboolean _wapi_lookup_handle (gpointer handle, WapiHandleType type,
@@ -93,6 +96,36 @@ extern gboolean _wapi_handle_get_or_set_share (dev_t device, ino_t inode,
 extern void _wapi_handle_set_share (dev_t device, ino_t inode,
                                    guint32 sharemode, guint32 access);
 
+static inline void _wapi_handle_fd_offset_store (int fd, gpointer handle)
+{
+       g_assert (fd < _wapi_fd_offset_table_size);
+       g_assert (_wapi_fd_offset_table[fd]==NULL || handle==NULL);
+       g_assert (GPOINTER_TO_UINT (handle) >= _wapi_fd_offset_table_size || handle==NULL);
+       
+#ifdef DEBUG
+       g_message (G_GNUC_PRETTY_FUNCTION ": Assigning fd offset %d to %p", fd,
+                  handle);
+#endif
+
+       _wapi_fd_offset_table[fd]=handle;
+}
+
+static inline gpointer _wapi_handle_fd_offset_to_handle (gpointer fd_handle)
+{
+       int fd = GPOINTER_TO_INT (fd_handle);
+       
+       g_assert (fd < _wapi_fd_offset_table_size);
+       g_assert (_wapi_fd_offset_table[fd]!=NULL);
+       g_assert (GPOINTER_TO_UINT (_wapi_fd_offset_table[fd]) >= _wapi_fd_offset_table_size);
+
+#ifdef DEBUG
+       g_message (G_GNUC_PRETTY_FUNCTION ": Returning fd offset %d of %p", fd,
+                  _wapi_fd_offset_table[fd]);
+#endif
+
+       return(_wapi_fd_offset_table[fd]);
+}
+
 static inline struct _WapiHandleShared_list *_wapi_handle_get_shared_segment (guint32 segment)
 {
        struct _WapiHandleShared_list *shared;
@@ -225,6 +258,10 @@ static inline WapiHandleType _wapi_handle_type (gpointer handle)
        guint32 idx;
        guint32 segment;
        
+       if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+               handle = _wapi_handle_fd_offset_to_handle (handle);
+       }
+       
        _wapi_handle_segment (handle, &segment, &idx);
 
        if(segment>=_wapi_shm_mapped_segments)
@@ -242,6 +279,8 @@ static inline void _wapi_handle_set_signal_state (gpointer handle,
        struct _WapiHandleShared *shared_handle;
        int thr_ret;
        
+       g_assert (GPOINTER_TO_UINT (handle) >= _wapi_fd_offset_table_size);
+       
        _wapi_handle_segment (handle, &segment, &idx);
        shared_handle=&_wapi_handle_get_shared_segment (segment)->handles[idx];
        
@@ -317,6 +356,8 @@ static inline gboolean _wapi_handle_issignalled (gpointer handle)
        guint32 idx;
        guint32 segment;
        
+       g_assert (GPOINTER_TO_UINT (handle) >= _wapi_fd_offset_table_size);
+       
        _wapi_handle_segment (handle, &segment, &idx);
        
        return(_wapi_handle_get_shared_segment (segment)->handles[idx].signalled);
@@ -352,6 +393,8 @@ static inline int _wapi_handle_lock_handle (gpointer handle)
        guint32 idx;
        guint32 segment;
        
+       g_assert (GPOINTER_TO_UINT (handle) >= _wapi_fd_offset_table_size);
+       
 #ifdef DEBUG
        g_message (G_GNUC_PRETTY_FUNCTION ": locking handle %p", handle);
 #endif
@@ -369,6 +412,8 @@ static inline int _wapi_handle_unlock_handle (gpointer handle)
        guint32 segment;
        int ret;
        
+       g_assert (GPOINTER_TO_UINT (handle) >= _wapi_fd_offset_table_size);
+       
 #ifdef DEBUG
        g_message (G_GNUC_PRETTY_FUNCTION ": unlocking handle %p", handle);
 #endif
index 769235fe76d89584bcd65bc2fac0881b1a63f1fa..cbab985e62e216041430fc759cf302bf5d763a77 100644 (file)
@@ -77,6 +77,9 @@ pthread_mutex_t _wapi_shared_mutex=PTHREAD_MUTEX_INITIALIZER;
  */
 guint32 _wapi_shm_mapped_segments;
 
+guint32 _wapi_fd_offset_table_size;
+gpointer *_wapi_fd_offset_table=NULL;
+
 static void shared_init (void)
 {
        struct sockaddr_un shared_socket_address;
@@ -86,6 +89,13 @@ static void shared_init (void)
        
        _wapi_shared_data=g_new0 (struct _WapiHandleShared_list *, 1);
        _wapi_private_data=g_new0 (struct _WapiHandlePrivate_list *, 1);
+
+       /* Build this array at runtime, because I'm guessing that
+        * getdtablesize() won't be a fixed value on all systems (or
+        * why would it be a function rather than a constant define?)
+        */
+       _wapi_fd_offset_table_size=getdtablesize ();
+       _wapi_fd_offset_table=g_new0 (gpointer, _wapi_fd_offset_table_size);
        
 attach_again:
 
@@ -259,8 +269,11 @@ again:
                for(j=idx; j<_WAPI_HANDLES_PER_SEGMENT; j++) {
                        struct _WapiHandleShared *shared;
                
-                       /* Make sure we dont try and assign handle 0 */
-                       if (i==0 && j==0) {
+                       /* Make sure we dont try and assign the
+                        * handles that would clash with fds
+                        */
+                       if (i==0 && j < _wapi_fd_offset_table_size) {
+                               j = _wapi_fd_offset_table_size;
                                continue;
                        }
                        
@@ -314,7 +327,7 @@ gpointer _wapi_handle_new (WapiHandleType type)
        int thr_ret;
        
        mono_once (&shared_init_once, shared_init);
-
+       
 again:
        if(shared==TRUE) {
                new.type=WapiHandleRequestType_New;
@@ -361,6 +374,9 @@ again:
                pthread_cleanup_pop (0);
        }
                
+       /* Make sure we left the space for fd mappings */
+       g_assert (handle_idx >= _wapi_fd_offset_table_size);
+       
        if(handle_idx==0) {
                g_warning (G_GNUC_PRETTY_FUNCTION ": Ran out of handles!");
 
@@ -414,6 +430,8 @@ gboolean _wapi_lookup_handle (gpointer handle, WapiHandleType type,
        guint32 idx;
        guint32 segment;
 
+       g_assert (GPOINTER_TO_UINT (handle) >= _wapi_fd_offset_table_size);
+       
        _wapi_handle_segment (handle, &segment, &idx);
        _wapi_handle_ensure_mapped (segment);
        
@@ -592,6 +610,8 @@ gpointer _wapi_search_handle_namespace (WapiHandleType type,
 
 void _wapi_handle_ref (gpointer handle)
 {
+       g_assert (GPOINTER_TO_UINT (handle) >= _wapi_fd_offset_table_size);
+       
        if(shared==TRUE) {
                WapiHandleRequest req={0};
                WapiHandleResponse resp={0};
@@ -628,6 +648,8 @@ void _wapi_handle_unref (gpointer handle)
        gboolean destroy = FALSE;
        int thr_ret;
        
+       g_assert (GPOINTER_TO_UINT (handle) >= _wapi_fd_offset_table_size);
+       
        _wapi_handle_segment (handle, &segment, &idx);
        
        if(shared==TRUE) {
@@ -1180,6 +1202,10 @@ gboolean _wapi_handle_test_capabilities (gpointer handle,
        guint32 idx, segment;
        WapiHandleType type;
 
+       if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+               handle = _wapi_handle_fd_offset_to_handle (handle);
+       }
+       
        _wapi_handle_segment (handle, &segment, &idx);
        
        type=_wapi_handle_get_shared_segment (segment)->handles[idx].type;
@@ -1197,6 +1223,10 @@ void _wapi_handle_ops_close_shared (gpointer handle)
        guint32 idx, segment;
        WapiHandleType type;
 
+       if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+               handle = _wapi_handle_fd_offset_to_handle (handle);
+       }
+
        _wapi_handle_segment (handle, &segment, &idx);
        
        type=_wapi_handle_get_shared_segment (segment)->handles[idx].type;
@@ -1211,6 +1241,10 @@ void _wapi_handle_ops_close_private (gpointer handle)
        guint32 idx, segment;
        WapiHandleType type;
 
+       if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+               handle = _wapi_handle_fd_offset_to_handle (handle);
+       }
+
        _wapi_handle_segment (handle, &segment, &idx);
 
        type=_wapi_handle_get_shared_segment (segment)->handles[idx].type;
@@ -1232,6 +1266,10 @@ void _wapi_handle_ops_signal (gpointer handle)
        guint32 idx, segment;
        WapiHandleType type;
 
+       if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+               handle = _wapi_handle_fd_offset_to_handle (handle);
+       }
+
        _wapi_handle_segment (handle, &segment, &idx);
 
        type=_wapi_handle_get_shared_segment (segment)->handles[idx].type;
@@ -1246,6 +1284,10 @@ void _wapi_handle_ops_own (gpointer handle)
        guint32 idx, segment;
        WapiHandleType type;
 
+       if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+               handle = _wapi_handle_fd_offset_to_handle (handle);
+       }
+
        _wapi_handle_segment (handle, &segment, &idx);
        
        type=_wapi_handle_get_shared_segment (segment)->handles[idx].type;
@@ -1260,6 +1302,10 @@ gboolean _wapi_handle_ops_isowned (gpointer handle)
        guint32 idx, segment;
        WapiHandleType type;
 
+       if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+               handle = _wapi_handle_fd_offset_to_handle (handle);
+       }
+
        _wapi_handle_segment (handle, &segment, &idx);
        
        type=_wapi_handle_get_shared_segment (segment)->handles[idx].type;
@@ -1284,6 +1330,10 @@ gboolean _wapi_handle_ops_isowned (gpointer handle)
  */
 gboolean CloseHandle(gpointer handle)
 {
+       if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+               handle = _wapi_handle_fd_offset_to_handle (handle);
+       }
+
        _wapi_handle_unref (handle);
        
        return(TRUE);
@@ -1303,12 +1353,17 @@ gboolean _wapi_handle_count_signalled_handles (guint32 numhandles,
 again:
        for(i=0; i<numhandles; i++) {
                guint32 idx, segment;
+               gpointer handle = handles[i];
                
-               _wapi_handle_segment (handles[i], &segment, &idx);
+               if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+                       handle = _wapi_handle_fd_offset_to_handle (handle);
+               }
+               
+               _wapi_handle_segment (handle, &segment, &idx);
                
 #ifdef DEBUG
                g_message (G_GNUC_PRETTY_FUNCTION ": attempting to lock %p",
-                          handles[i]);
+                          handle);
 #endif
 
                ret=mono_mutex_trylock (&_wapi_handle_get_shared_segment (segment)->handles[idx].signal_mutex);
@@ -1317,11 +1372,11 @@ again:
                        struct timespec sleepytime;
                        
 #ifdef DEBUG
-                       g_message (G_GNUC_PRETTY_FUNCTION ": attempt failed for %p", handles[i]);
+                       g_message (G_GNUC_PRETTY_FUNCTION ": attempt failed for %p", handle);
 #endif
 
                        while(i--) {
-                               _wapi_handle_segment (handles[i], &segment, &idx);
+                               _wapi_handle_segment (handle, &segment, &idx);
                                thr_ret = mono_mutex_unlock (&_wapi_handle_get_shared_segment (segment)->handles[idx].signal_mutex);
                                g_assert (thr_ret == 0);
                        }
@@ -1359,24 +1414,29 @@ again:
        
        for(i=0; i<numhandles; i++) {
                guint32 idx, segment;
-
-               _wapi_handle_ref (handles[i]);
+               gpointer handle = handles[i];
+               
+               if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+                       handle = _wapi_handle_fd_offset_to_handle (handle);
+               }
+               
+               _wapi_handle_ref (handle);
                
-               _wapi_handle_segment (handles[i], &segment, &idx);
+               _wapi_handle_segment (handle, &segment, &idx);
                
 #ifdef DEBUG
                g_message (G_GNUC_PRETTY_FUNCTION ": Checking handle %p",
-                          handles[i]);
+                          handle);
 #endif
 
-               if(((_wapi_handle_test_capabilities (handles[i], WAPI_HANDLE_CAP_OWN)==TRUE) &&
-                   (_wapi_handle_ops_isowned (handles[i])==TRUE)) ||
+               if(((_wapi_handle_test_capabilities (handle, WAPI_HANDLE_CAP_OWN)==TRUE) &&
+                   (_wapi_handle_ops_isowned (handle)==TRUE)) ||
                   (_wapi_handle_get_shared_segment (segment)->handles[idx].signalled==TRUE)) {
                        count++;
                        
 #ifdef DEBUG
                        g_message (G_GNUC_PRETTY_FUNCTION
-                                  ": Handle %p signalled", handles[i]);
+                                  ": Handle %p signalled", handle);
 #endif
                        if(*lowest>i) {
                                *lowest=i;
@@ -1412,18 +1472,23 @@ void _wapi_handle_unlock_handles (guint32 numhandles, gpointer *handles)
        
        for(i=0; i<numhandles; i++) {
                guint32 idx, segment;
-
-               _wapi_handle_segment (handles[i], &segment, &idx);
+               gpointer handle = handles[i];
+               
+               if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+                       handle = _wapi_handle_fd_offset_to_handle (handle);
+               }
+               
+               _wapi_handle_segment (handle, &segment, &idx);
 
 #ifdef DEBUG
                g_message (G_GNUC_PRETTY_FUNCTION ": unlocking handle %p",
-                          handles[i]);
+                          handle);
 #endif
 
                thr_ret = mono_mutex_unlock (&_wapi_handle_get_shared_segment (segment)->handles[idx].signal_mutex);
                g_assert (thr_ret == 0);
 
-               _wapi_handle_unref (handles[i]);
+               _wapi_handle_unref (handle);
        }
 }
 
@@ -1495,6 +1560,10 @@ int _wapi_handle_wait_signal_handle (gpointer handle)
 #if defined(_POSIX_THREAD_PROCESS_SHARED) && _POSIX_THREAD_PROCESS_SHARED != -1
        guint32 idx, segment;
 
+       if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+               handle = _wapi_handle_fd_offset_to_handle (handle);
+       }
+
        _wapi_handle_segment (handle, &segment, &idx);
        
        return(mono_cond_wait (&_wapi_handle_get_shared_segment (segment)->handles[idx].signal_cond,
@@ -1504,6 +1573,10 @@ int _wapi_handle_wait_signal_handle (gpointer handle)
        struct timespec fake_timeout;
        int ret;
        
+       if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+               handle = _wapi_handle_fd_offset_to_handle (handle);
+       }
+
        _wapi_handle_segment (handle, &segment, &idx);
        _wapi_calc_timeout (&fake_timeout, 100);
        
@@ -1524,6 +1597,10 @@ int _wapi_handle_timedwait_signal_handle (gpointer handle,
 #if defined(_POSIX_THREAD_PROCESS_SHARED) && _POSIX_THREAD_PROCESS_SHARED != -1
        guint32 idx, segment;
 
+       if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+               handle = _wapi_handle_fd_offset_to_handle (handle);
+       }
+
        _wapi_handle_segment (handle, &segment, &idx);
        
        return(mono_cond_timedwait (&_wapi_handle_get_shared_segment (segment)->handles[idx].signal_cond,
@@ -1534,6 +1611,10 @@ int _wapi_handle_timedwait_signal_handle (gpointer handle,
        struct timespec fake_timeout;
        int ret;
        
+       if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+               handle = _wapi_handle_fd_offset_to_handle (handle);
+       }
+
        _wapi_handle_segment (handle, &segment, &idx);
        _wapi_calc_timeout (&fake_timeout, 100);
        
index cdc616743fa956b4c8062b90de5011d81c9c5194..e369e0cd0bd619a7f309d3fa8f6d264fb2783a58 100644 (file)
@@ -271,6 +271,9 @@ static void file_close_private (gpointer handle)
                  handle, file_private_handle->fd);
 #endif
        
+       /* Blank out the mapping, to make catching errors easier */
+       _wapi_handle_fd_offset_store (file_private_handle->fd, NULL);
+       
        close(file_private_handle->fd);
 }
 
@@ -1142,6 +1145,9 @@ static void console_close_private (gpointer handle)
                  console_private_handle->fd);
 #endif
        
+       /* Blank out the mapping, to make catching errors easier */
+       _wapi_handle_fd_offset_store (console_private_handle->fd, NULL);
+       
        close(console_private_handle->fd);
 }
 
@@ -1301,6 +1307,9 @@ static void pipe_close_private (gpointer handle)
                  pipe_private_handle->fd);
 #endif
        
+       /* Blank out the mapping, to make catching errors easier */
+       _wapi_handle_fd_offset_store (pipe_private_handle->fd, NULL);
+       
        close(pipe_private_handle->fd);
 }
 
@@ -1705,7 +1714,9 @@ gpointer CreateFile(const gunichar2 *name, guint32 fileaccess,
                close (fd);
                goto cleanup;
        }
-       cf_ret = handle;
+
+       _wapi_handle_fd_offset_store (fd, handle);
+       cf_ret = GINT_TO_POINTER (fd);
 
        file_private_handle->fd=fd;
        file_private_handle->assigned=TRUE;
@@ -2047,7 +2058,9 @@ static gpointer stdhandle_create (int fd, const guchar *name)
                           ": error looking up console handle %p", handle);
                goto cleanup;
        }
-       ret = handle;
+
+       _wapi_handle_fd_offset_store (fd, handle);
+       ret = GINT_TO_POINTER (fd);
        
        file_private_handle->fd=fd;
        file_private_handle->assigned=TRUE;
@@ -2118,7 +2131,7 @@ gpointer GetStdHandle(WapiStdHandle stdhandle)
        }
 
        /* Add a reference to this handle */
-       _wapi_handle_ref (handle);
+       _wapi_handle_ref (_wapi_handle_fd_offset_to_handle (handle));
        
        return(handle);
 }
@@ -2150,9 +2163,10 @@ gpointer GetStdHandle(WapiStdHandle stdhandle)
  * read due to an attempt to read past the end of the file), %FALSE on
  * error.
  */
-gboolean ReadFile(gpointer handle, gpointer buffer, guint32 numbytes,
+gboolean ReadFile(gpointer fd_handle, gpointer buffer, guint32 numbytes,
                  guint32 *bytesread, WapiOverlapped *overlapped)
 {
+       gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
        WapiHandleType type=_wapi_handle_type (handle);
        
        if(io_ops[type].readfile==NULL) {
@@ -2188,9 +2202,10 @@ gboolean ReadFile(gpointer handle, gpointer buffer, guint32 numbytes,
  *
  * Return value: %TRUE if the write succeeds, %FALSE on error.
  */
-gboolean WriteFile(gpointer handle, gconstpointer buffer, guint32 numbytes,
+gboolean WriteFile(gpointer fd_handle, gconstpointer buffer, guint32 numbytes,
                   guint32 *byteswritten, WapiOverlapped *overlapped)
 {
+       gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
        WapiHandleType type=_wapi_handle_type (handle);
        
        if(io_ops[type].writefile==NULL) {
@@ -2211,8 +2226,9 @@ gboolean WriteFile(gpointer handle, gconstpointer buffer, guint32 numbytes,
  *
  * Return value: %TRUE on success, %FALSE otherwise.
  */
-gboolean FlushFileBuffers(gpointer handle)
+gboolean FlushFileBuffers(gpointer fd_handle)
 {
+       gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
        WapiHandleType type=_wapi_handle_type (handle);
        
        if(io_ops[type].flushfile==NULL) {
@@ -2232,8 +2248,9 @@ gboolean FlushFileBuffers(gpointer handle)
  *
  * Return value: %TRUE on success, %FALSE otherwise.
  */
-gboolean SetEndOfFile(gpointer handle)
+gboolean SetEndOfFile(gpointer fd_handle)
 {
+       gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
        WapiHandleType type=_wapi_handle_type (handle);
        
        if(io_ops[type].setendoffile==NULL) {
@@ -2272,9 +2289,10 @@ gboolean SetEndOfFile(gpointer handle)
  * If @highmovedistance is not %NULL, the high 32 bits of the new file
  * pointer are stored there.  On failure, %INVALID_SET_FILE_POINTER.
  */
-guint32 SetFilePointer(gpointer handle, gint32 movedistance,
+guint32 SetFilePointer(gpointer fd_handle, gint32 movedistance,
                       gint32 *highmovedistance, WapiSeekMethod method)
 {
+       gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
        WapiHandleType type=_wapi_handle_type (handle);
        
        if(io_ops[type].seek==NULL) {
@@ -2296,8 +2314,9 @@ guint32 SetFilePointer(gpointer handle, gint32 movedistance,
  * %FILE_TYPE_CHAR - @handle is a character device, such as a console.
  * %FILE_TYPE_PIPE - @handle is a named or anonymous pipe.
  */
-WapiFileType GetFileType(gpointer handle)
+WapiFileType GetFileType(gpointer fd_handle)
 {
+       gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
        WapiHandleType type=_wapi_handle_type (handle);
        
        if(io_ops[type].getfiletype==NULL) {
@@ -2323,8 +2342,9 @@ WapiFileType GetFileType(gpointer handle)
  * @highsize is non-%NULL then the high 32 bits of the file size are
  * stored here.  On failure %INVALID_FILE_SIZE is returned.
  */
-guint32 GetFileSize(gpointer handle, guint32 *highsize)
+guint32 GetFileSize(gpointer fd_handle, guint32 *highsize)
 {
+       gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
        WapiHandleType type=_wapi_handle_type (handle);
        
        if(io_ops[type].getfilesize==NULL) {
@@ -2359,9 +2379,10 @@ guint32 GetFileSize(gpointer handle, guint32 *highsize)
  *
  * Return value: %TRUE on success, %FALSE otherwise.
  */
-gboolean GetFileTime(gpointer handle, WapiFileTime *create_time,
+gboolean GetFileTime(gpointer fd_handle, WapiFileTime *create_time,
                     WapiFileTime *last_access, WapiFileTime *last_write)
 {
+       gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
        WapiHandleType type=_wapi_handle_type (handle);
        
        if(io_ops[type].getfiletime==NULL) {
@@ -2395,10 +2416,11 @@ gboolean GetFileTime(gpointer handle, WapiFileTime *create_time,
  *
  * Return value: %TRUE on success, %FALSE otherwise.
  */
-gboolean SetFileTime(gpointer handle, const WapiFileTime *create_time,
+gboolean SetFileTime(gpointer fd_handle, const WapiFileTime *create_time,
                     const WapiFileTime *last_access,
                     const WapiFileTime *last_write)
 {
+       gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
        WapiHandleType type=_wapi_handle_type (handle);
        
        if(io_ops[type].setfiletime==NULL) {
@@ -3400,10 +3422,14 @@ extern gboolean SetCurrentDirectory (const gunichar2 *path)
        return result;
 }
 
-int _wapi_file_handle_to_fd (gpointer handle)
+/* When we're confident there are no more bugs in the fd->handle
+ * mapping, this can be replaced as a no-op: GPOINTER_TO_INT(fd_handle) == fd
+ */
+int _wapi_file_handle_to_fd (gpointer fd_handle)
 {
        struct _WapiHandlePrivate_file *file_private_handle;
        gboolean ok;
+       gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
        
 #ifdef DEBUG
        g_message (G_GNUC_PRETTY_FUNCTION ": looking up fd for %p", handle);
@@ -3432,6 +3458,8 @@ int _wapi_file_handle_to_fd (gpointer handle)
                   file_private_handle->fd);
 #endif
        
+       g_assert (file_private_handle->fd == GPOINTER_TO_INT (fd_handle));
+       
        return(file_private_handle->fd);
 }
 
@@ -3528,18 +3556,18 @@ gboolean CreatePipe (gpointer *readpipe, gpointer *writepipe,
        pipe_read_private_handle->assigned=TRUE;
        pipe_read_handle->fileaccess=GENERIC_READ;
        
-       *readpipe=read_handle;
+       _wapi_handle_fd_offset_store (filedes[0], read_handle);
+       *readpipe=GINT_TO_POINTER (filedes[0]);
 
        pipe_write_private_handle->fd=filedes[1];
        pipe_write_private_handle->assigned=TRUE;
        pipe_write_handle->fileaccess=GENERIC_WRITE;
        
-       *writepipe=write_handle;
+       _wapi_handle_fd_offset_store (filedes[1], write_handle);
+       *writepipe=GINT_TO_POINTER (filedes[1]);
 
 #ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION
-                  ": Returning pipe: read handle %p, write handle %p",
-                  read_handle, write_handle);
+       g_message (G_GNUC_PRETTY_FUNCTION ": Returning pipe: read handle %p (fd %d), write handle %p (fd %d)", read_handle, filedes[0], write_handle, filedes[1]);
 #endif
 
 write_cleanup:
@@ -3612,7 +3640,7 @@ guint32 GetTempPath (guint32 len, gunichar2 *buf)
 }
 
 gboolean
-_wapi_io_add_callback (gpointer handle,
+_wapi_io_add_callback (gpointer fd_handle,
                       WapiOverlappedCB callback,
                       guint64 flags G_GNUC_UNUSED)
 {
@@ -3621,6 +3649,7 @@ _wapi_io_add_callback (gpointer handle,
        gboolean ok;
        int thr_ret;
        gboolean ret = FALSE;
+       gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
        
        ok = _wapi_lookup_handle (handle, WAPI_HANDLE_FILE,
                                  (gpointer *) &file_handle,
@@ -3794,13 +3823,14 @@ static gboolean _wapi_unlock_file_region (int fd, off_t offset, off_t length)
        return(TRUE);
 }
 
-gboolean LockFile (gpointer handle, guint32 offset_low, guint32 offset_high,
+gboolean LockFile (gpointer fd_handle, guint32 offset_low, guint32 offset_high,
                   guint32 length_low, guint32 length_high)
 {
        struct _WapiHandle_file *file_handle;
        struct _WapiHandlePrivate_file *file_private_handle;
        gboolean ok;
        off_t offset, length;
+       gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
        
        ok = _wapi_lookup_handle (handle, WAPI_HANDLE_FILE,
                                  (gpointer *)&file_handle,
@@ -3846,13 +3876,15 @@ gboolean LockFile (gpointer handle, guint32 offset_low, guint32 offset_high,
                                       length));
 }
 
-gboolean UnlockFile (gpointer handle, guint32 offset_low, guint32 offset_high,
-                    guint32 length_low, guint32 length_high)
+gboolean UnlockFile (gpointer fd_handle, guint32 offset_low,
+                    guint32 offset_high, guint32 length_low,
+                    guint32 length_high)
 {
        struct _WapiHandle_file *file_handle;
        struct _WapiHandlePrivate_file *file_private_handle;
        gboolean ok;
        off_t offset, length;
+       gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
        
        ok = _wapi_lookup_handle (handle, WAPI_HANDLE_FILE,
                                  (gpointer *)&file_handle,
index 3f48f02af8e4328a8fb127ae1b8ef6f222e7f6b8..46531f2d75d6b477d170a67b697f77587240abb9 100644 (file)
@@ -97,6 +97,9 @@ static void socket_close_private (gpointer handle)
 
        g_ptr_array_remove_fast(sockets, GUINT_TO_POINTER (handle));
 
+       /* Blank out the mapping, to make catching errors easier */
+       _wapi_handle_fd_offset_store (socket_private_handle->fd, NULL);
+
        do {
                ret=close(socket_private_handle->fd);
        }
@@ -205,20 +208,21 @@ int WSAGetLastError(void)
        return(err);
 }
 
-int closesocket(guint32 handle)
+int closesocket(guint32 fd_handle)
 {
-       _wapi_handle_unref (GUINT_TO_POINTER (handle));
+       gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd_handle));
+       
+       _wapi_handle_unref (handle);
        return(0);
 }
 
-guint32 _wapi_accept(guint32 handle, struct sockaddr *addr,
-                    socklen_t *addrlen)
+guint32 _wapi_accept(guint32 fd, struct sockaddr *addr, socklen_t *addrlen)
 {
-       struct _WapiHandlePrivate_socket *socket_private_handle;
        struct _WapiHandlePrivate_socket *new_socket_private_handle;
+       gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
        gpointer new_handle;
        gboolean ok;
-       int fd;
+       int new_fd;
        int thr_ret;
        guint32 ret = INVALID_SOCKET;
        
@@ -227,21 +231,17 @@ guint32 _wapi_accept(guint32 handle, struct sockaddr *addr,
                return(INVALID_SOCKET);
        }
        
-       ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
-                               NULL, (gpointer *)&socket_private_handle);
-       if(ok==FALSE) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error looking up socket handle 0x%x", handle);
+       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(INVALID_SOCKET);
        }
        
        do {
-               fd=accept(socket_private_handle->fd, addr, addrlen);
+               new_fd=accept(fd, addr, addrlen);
        }
-       while (fd==-1 && errno==EINTR && !_wapi_thread_cur_apc_pending());
+       while (new_fd==-1 && errno==EINTR && !_wapi_thread_cur_apc_pending());
 
-       if(fd==-1) {
+       if(new_fd==-1) {
                gint errnum = errno;
 #ifdef DEBUG
                g_message(G_GNUC_PRETTY_FUNCTION ": accept error: %s",
@@ -258,6 +258,7 @@ guint32 _wapi_accept(guint32 handle, struct sockaddr *addr,
        if(new_handle==_WAPI_HANDLE_INVALID) {
                g_warning (G_GNUC_PRETTY_FUNCTION
                           ": error creating socket handle");
+               WSASetLastError (ERROR_GEN_FAILURE);
                return(INVALID_SOCKET);
        }
 
@@ -270,12 +271,15 @@ guint32 _wapi_accept(guint32 handle, struct sockaddr *addr,
                                (gpointer *)&new_socket_private_handle);
        if(ok==FALSE) {
                g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error looking up socket handle 0x%x", handle);
+                          ": error looking up new socket handle %p",
+                          new_handle);
                goto cleanup;
        }
-       ret = GPOINTER_TO_UINT (new_handle);
+
+       _wapi_handle_fd_offset_store (new_fd, new_handle);
+       ret = new_fd;
        
-       new_socket_private_handle->fd=fd;
+       new_socket_private_handle->fd=new_fd;
        
 #ifdef DEBUG
        g_message(G_GNUC_PRETTY_FUNCTION
@@ -291,27 +295,22 @@ cleanup:
        return(ret);
 }
 
-int _wapi_bind(guint32 handle, struct sockaddr *my_addr, socklen_t addrlen)
+int _wapi_bind(guint32 fd, struct sockaddr *my_addr, socklen_t addrlen)
 {
-       struct _WapiHandlePrivate_socket *socket_private_handle;
-       gboolean ok;
+       gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
        int ret;
        
        if(startup_count==0) {
                WSASetLastError(WSANOTINITIALISED);
                return(SOCKET_ERROR);
        }
-       
-       ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
-                               NULL, (gpointer *)&socket_private_handle);
-       if(ok==FALSE) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error looking up socket handle 0x%x", handle);
+
+       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
        
-       ret=bind(socket_private_handle->fd, my_addr, addrlen);
+       ret=bind(fd, my_addr, addrlen);
        if(ret==-1) {
                gint errnum = errno;
 #ifdef DEBUG
@@ -326,11 +325,10 @@ int _wapi_bind(guint32 handle, struct sockaddr *my_addr, socklen_t addrlen)
        return(ret);
 }
 
-int _wapi_connect(guint32 handle, const struct sockaddr *serv_addr,
+int _wapi_connect(guint32 fd, const struct sockaddr *serv_addr,
                  socklen_t addrlen)
 {
-       struct _WapiHandlePrivate_socket *socket_private_handle;
-       gboolean ok;
+       gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
        int ret;
        gint errnum;
        
@@ -339,17 +337,13 @@ int _wapi_connect(guint32 handle, const struct sockaddr *serv_addr,
                return(SOCKET_ERROR);
        }
        
-       ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
-                               NULL, (gpointer *)&socket_private_handle);
-       if(ok==FALSE) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error looking up socket handle 0x%x", handle);
+       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
        
        do {
-               ret=connect(socket_private_handle->fd, serv_addr, addrlen);
+               ret=connect(fd, serv_addr, addrlen);
        }
        while (ret==-1 && errno==EINTR && !_wapi_thread_cur_apc_pending());
 
@@ -361,11 +355,11 @@ int _wapi_connect(guint32 handle, const struct sockaddr *serv_addr,
                
                errnum = errno;
 
-               ret=setsockopt (socket_private_handle->fd, SOL_SOCKET,
-                               SO_BROADCAST, &true, sizeof(true));
+               ret=setsockopt (fd, SOL_SOCKET, SO_BROADCAST, &true,
+                               sizeof(true));
                if(ret==0) {
                        do {
-                               ret=connect (socket_private_handle->fd, serv_addr, addrlen);
+                               ret=connect (fd, serv_addr, addrlen);
                        }
                        while (ret==-1 && errno==EINTR && !_wapi_thread_cur_apc_pending());
                }
@@ -386,11 +380,9 @@ int _wapi_connect(guint32 handle, const struct sockaddr *serv_addr,
        return(ret);
 }
 
-int _wapi_getpeername(guint32 handle, struct sockaddr *name,
-                     socklen_t *namelen)
+int _wapi_getpeername(guint32 fd, struct sockaddr *name, socklen_t *namelen)
 {
-       struct _WapiHandlePrivate_socket *socket_private_handle;
-       gboolean ok;
+       gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
        int ret;
        
        if(startup_count==0) {
@@ -398,16 +390,12 @@ int _wapi_getpeername(guint32 handle, struct sockaddr *name,
                return(SOCKET_ERROR);
        }
        
-       ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
-                               NULL, (gpointer *)&socket_private_handle);
-       if(ok==FALSE) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error looking up socket handle 0x%x", handle);
+       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
 
-       ret=getpeername(socket_private_handle->fd, name, namelen);
+       ret=getpeername(fd, name, namelen);
        if(ret==-1) {
                gint errnum = errno;
 #ifdef DEBUG
@@ -424,11 +412,9 @@ int _wapi_getpeername(guint32 handle, struct sockaddr *name,
        return(ret);
 }
 
-int _wapi_getsockname(guint32 handle, struct sockaddr *name,
-                     socklen_t *namelen)
+int _wapi_getsockname(guint32 fd, struct sockaddr *name, socklen_t *namelen)
 {
-       struct _WapiHandlePrivate_socket *socket_private_handle;
-       gboolean ok;
+       gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
        int ret;
        
        if(startup_count==0) {
@@ -436,16 +422,12 @@ int _wapi_getsockname(guint32 handle, struct sockaddr *name,
                return(SOCKET_ERROR);
        }
        
-       ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
-                               NULL, (gpointer *)&socket_private_handle);
-       if(ok==FALSE) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error looking up socket handle 0x%x", handle);
+       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
 
-       ret=getsockname(socket_private_handle->fd, name, namelen);
+       ret=getsockname(fd, name, namelen);
        if(ret==-1) {
                gint errnum = errno;
 #ifdef DEBUG
@@ -462,11 +444,10 @@ int _wapi_getsockname(guint32 handle, struct sockaddr *name,
        return(ret);
 }
 
-int _wapi_getsockopt(guint32 handle, int level, int optname, void *optval,
+int _wapi_getsockopt(guint32 fd, int level, int optname, void *optval,
                     socklen_t *optlen)
 {
-       struct _WapiHandlePrivate_socket *socket_private_handle;
-       gboolean ok;
+       gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
        int ret;
        
        if(startup_count==0) {
@@ -474,17 +455,12 @@ int _wapi_getsockopt(guint32 handle, int level, int optname, void *optval,
                return(SOCKET_ERROR);
        }
        
-       ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
-                               NULL, (gpointer *)&socket_private_handle);
-       if(ok==FALSE) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error looking up socket handle 0x%x", handle);
+       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
        
-       ret=getsockopt(socket_private_handle->fd, level, optname, optval,
-                      optlen);
+       ret=getsockopt(fd, level, optname, optval, optlen);
        if(ret==-1) {
                gint errnum = errno;
 #ifdef DEBUG
@@ -501,10 +477,9 @@ int _wapi_getsockopt(guint32 handle, int level, int optname, void *optval,
        return(ret);
 }
 
-int _wapi_listen(guint32 handle, int backlog)
+int _wapi_listen(guint32 fd, int backlog)
 {
-       struct _WapiHandlePrivate_socket *socket_private_handle;
-       gboolean ok;
+       gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
        int ret;
        
        if(startup_count==0) {
@@ -512,16 +487,12 @@ int _wapi_listen(guint32 handle, int backlog)
                return(SOCKET_ERROR);
        }
        
-       ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
-                               NULL, (gpointer *)&socket_private_handle);
-       if(ok==FALSE) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error looking up socket handle 0x%x", handle);
+       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
        
-       ret=listen(socket_private_handle->fd, backlog);
+       ret=listen(fd, backlog);
        if(ret==-1) {
                gint errnum = errno;
 #ifdef DEBUG
@@ -538,19 +509,18 @@ int _wapi_listen(guint32 handle, int backlog)
        return(0);
 }
 
-int _wapi_recv(guint32 handle, void *buf, size_t len, int recv_flags)
+int _wapi_recv(guint32 fd, void *buf, size_t len, int recv_flags)
 {
-       return(_wapi_recvfrom(handle, buf, len, recv_flags, NULL, 0));
+       return(_wapi_recvfrom(fd, buf, len, recv_flags, NULL, 0));
 }
 
-int _wapi_recvfrom(guint32 handle, void *buf, size_t len, int recv_flags,
+int _wapi_recvfrom(guint32 fd, void *buf, size_t len, int recv_flags,
                   struct sockaddr *from, socklen_t *fromlen)
 {
 #ifndef HAVE_MSG_NOSIGNAL
        void (*old_sigpipe)(int);       // old SIGPIPE handler
 #endif
-       struct _WapiHandlePrivate_socket *socket_private_handle;
-       gboolean ok;
+       gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
        int ret;
        
        if(startup_count==0) {
@@ -558,26 +528,21 @@ int _wapi_recvfrom(guint32 handle, void *buf, size_t len, int recv_flags,
                return(SOCKET_ERROR);
        }
        
-       ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
-                               NULL, (gpointer *)&socket_private_handle);
-       if(ok==FALSE) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error looking up socket handle 0x%x", handle);
+       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
        
 #ifdef HAVE_MSG_NOSIGNAL
        do {
-               ret=recvfrom(socket_private_handle->fd, buf, len, recv_flags | MSG_NOSIGNAL, from,
+               ret=recvfrom(fd, buf, len, recv_flags | MSG_NOSIGNAL, from,
                             fromlen);
        }
        while (ret==-1 && errno==EINTR && !_wapi_thread_cur_apc_pending());
 #else
        old_sigpipe = signal(SIGPIPE, SIG_IGN);
        do {
-               ret=recvfrom(socket_private_handle->fd, buf, len, recv_flags, from,
-                            fromlen);
+               ret=recvfrom(fd, buf, len, recv_flags, from, fromlen);
        }
        while (ret==-1 && errno==EINTR && !_wapi_thread_cur_apc_pending());
        signal(SIGPIPE, old_sigpipe);
@@ -598,13 +563,12 @@ int _wapi_recvfrom(guint32 handle, void *buf, size_t len, int recv_flags,
        return(ret);
 }
 
-int _wapi_send(guint32 handle, const void *msg, size_t len, int send_flags)
+int _wapi_send(guint32 fd, const void *msg, size_t len, int send_flags)
 {
 #ifndef HAVE_MSG_NOSIGNAL
        void (*old_sigpipe)(int);       // old SIGPIPE handler
 #endif
-       struct _WapiHandlePrivate_socket *socket_private_handle;
-       gboolean ok;
+       gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
        int ret;
        
        if(startup_count==0) {
@@ -612,24 +576,20 @@ int _wapi_send(guint32 handle, const void *msg, size_t len, int send_flags)
                return(SOCKET_ERROR);
        }
        
-       ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
-                               NULL, (gpointer *)&socket_private_handle);
-       if(ok==FALSE) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error looking up socket handle 0x%x", handle);
+       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
 
 #ifdef HAVE_MSG_NOSIGNAL
        do {
-               ret=send(socket_private_handle->fd, msg, len, send_flags | MSG_NOSIGNAL);
+               ret=send(fd, msg, len, send_flags | MSG_NOSIGNAL);
        }
        while (ret==-1 && errno==EINTR && !_wapi_thread_cur_apc_pending());
 #else
        old_sigpipe = signal(SIGPIPE, SIG_IGN);
        do {
-               ret=send(socket_private_handle->fd, msg, len, send_flags);
+               ret=send(fd, msg, len, send_flags);
        }
        while (ret==-1 && errno==EINTR && !_wapi_thread_cur_apc_pending());
        signal(SIGPIPE, old_sigpipe);
@@ -649,14 +609,13 @@ int _wapi_send(guint32 handle, const void *msg, size_t len, int send_flags)
        return(ret);
 }
 
-int _wapi_sendto(guint32 handle, const void *msg, size_t len, int send_flags,
+int _wapi_sendto(guint32 fd, const void *msg, size_t len, int send_flags,
                 const struct sockaddr *to, socklen_t tolen)
 {
 #ifndef HAVE_MSG_NOSIGNAL
        void (*old_sigpipe)(int);       // old SIGPIPE handler
 #endif
-       struct _WapiHandlePrivate_socket *socket_private_handle;
-       gboolean ok;
+       gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
        int ret;
        
        if(startup_count==0) {
@@ -664,24 +623,20 @@ int _wapi_sendto(guint32 handle, const void *msg, size_t len, int send_flags,
                return(SOCKET_ERROR);
        }
        
-       ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
-                               NULL, (gpointer *)&socket_private_handle);
-       if(ok==FALSE) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error looking up socket handle 0x%x", handle);
+       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
        
 #ifdef HAVE_MSG_NOSIGNAL
        do {
-               ret=sendto(socket_private_handle->fd, msg, len, send_flags | MSG_NOSIGNAL, to, tolen);
+               ret=sendto(fd, msg, len, send_flags | MSG_NOSIGNAL, to, tolen);
        }
        while (ret==-1 && errno==EINTR && !_wapi_thread_cur_apc_pending());
 #else
        old_sigpipe = signal(SIGPIPE, SIG_IGN);
        do {
-               ret=sendto(socket_private_handle->fd, msg, len, send_flags, to, tolen);
+               ret=sendto(fd, msg, len, send_flags, to, tolen);
        }
        while (ret==-1 && errno==EINTR && !_wapi_thread_cur_apc_pending());
        signal(SIGPIPE, old_sigpipe);
@@ -701,11 +656,10 @@ int _wapi_sendto(guint32 handle, const void *msg, size_t len, int send_flags,
        return(ret);
 }
 
-int _wapi_setsockopt(guint32 handle, int level, int optname,
+int _wapi_setsockopt(guint32 fd, int level, int optname,
                     const void *optval, socklen_t optlen)
 {
-       struct _WapiHandlePrivate_socket *socket_private_handle;
-       gboolean ok;
+       gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
        int ret;
        
        if(startup_count==0) {
@@ -713,17 +667,12 @@ int _wapi_setsockopt(guint32 handle, int level, int optname,
                return(SOCKET_ERROR);
        }
        
-       ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
-                               NULL, (gpointer *)&socket_private_handle);
-       if(ok==FALSE) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error looking up socket handle 0x%x", handle);
+       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
        
-       ret=setsockopt(socket_private_handle->fd, level, optname, optval,
-                      optlen);
+       ret=setsockopt(fd, level, optname, optval, optlen);
        if(ret==-1) {
                gint errnum = errno;
 #ifdef DEBUG
@@ -740,10 +689,9 @@ int _wapi_setsockopt(guint32 handle, int level, int optname,
        return(ret);
 }
 
-int _wapi_shutdown(guint32 handle, int how)
+int _wapi_shutdown(guint32 fd, int how)
 {
-       struct _WapiHandlePrivate_socket *socket_private_handle;
-       gboolean ok;
+       gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
        int ret;
        
        if(startup_count==0) {
@@ -751,16 +699,12 @@ int _wapi_shutdown(guint32 handle, int how)
                return(SOCKET_ERROR);
        }
        
-       ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
-                               NULL, (gpointer *)&socket_private_handle);
-       if(ok==FALSE) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error looking up socket handle 0x%x", handle);
+       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
        
-       ret=shutdown(socket_private_handle->fd, how);
+       ret=shutdown(fd, how);
        if(ret==-1) {
                gint errnum = errno;
 #ifdef DEBUG
@@ -824,7 +768,9 @@ guint32 _wapi_socket(int domain, int type, int protocol, void *unused, guint32 u
                           ": error looking up socket handle %p", handle);
                goto cleanup;
        }
-       ret = GPOINTER_TO_UINT (handle);
+
+       _wapi_handle_fd_offset_store (fd, handle);
+       ret = fd;
        
        socket_private_handle->fd=fd;
        
@@ -884,13 +830,12 @@ struct hostent *_wapi_gethostbyname(const char *hostname)
 }
 
 int
-WSAIoctl (guint32 handle, gint32 command,
-               gchar *input, gint i_len,
-               gchar *output, gint o_len, glong *written,
-               void *unused1, void *unused2)
+WSAIoctl (guint32 fd, gint32 command,
+         gchar *input, gint i_len,
+         gchar *output, gint o_len, glong *written,
+         void *unused1, void *unused2)
 {
-       struct _WapiHandlePrivate_socket *socket_private_handle;
-       gboolean ok;
+       gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
        int ret;
        gchar *buffer = NULL;
 
@@ -899,12 +844,7 @@ WSAIoctl (guint32 handle, gint32 command,
                return(SOCKET_ERROR);
        }
 
-       ok = _wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
-                                       NULL, (gpointer *)&socket_private_handle);
-
-       if (ok == FALSE) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error looking up socket handle 0x%x", handle);
+       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return SOCKET_ERROR;
        }
@@ -912,7 +852,7 @@ WSAIoctl (guint32 handle, gint32 command,
        if (i_len > 0)
                buffer = g_memdup (input, i_len);
 
-       ret = ioctl (socket_private_handle->fd, command, buffer);
+       ret = ioctl (fd, command, buffer);
        if (ret == -1) {
                gint errnum = errno;
 #ifdef DEBUG
@@ -941,10 +881,9 @@ WSAIoctl (guint32 handle, gint32 command,
        return 0;
 }
 
-int ioctlsocket(guint32 handle, gint32 command, gpointer arg)
+int ioctlsocket(guint32 fd, gint32 command, gpointer arg)
 {
-       struct _WapiHandlePrivate_socket *socket_private_handle;
-       gboolean ok;
+       gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
        int ret;
        
        if(startup_count==0) {
@@ -952,11 +891,7 @@ int ioctlsocket(guint32 handle, gint32 command, gpointer arg)
                return(SOCKET_ERROR);
        }
        
-       ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
-                               NULL, (gpointer *)&socket_private_handle);
-       if(ok==FALSE) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error looking up socket handle 0x%x", handle);
+       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -976,19 +911,19 @@ int ioctlsocket(guint32 handle, gint32 command, gpointer arg)
         * connect to return EINPROGRESS, but the ioctl doesn't seem to)
         */
        if(command==FIONBIO) {
-               ret=fcntl(socket_private_handle->fd, F_GETFL, 0);
+               ret=fcntl(fd, F_GETFL, 0);
                if(ret!=-1) {
                        if(*(gboolean *)arg) {
                                ret &= ~O_NONBLOCK;
                        } else {
                                ret |= O_NONBLOCK;
                        }
-                       ret=fcntl(socket_private_handle->fd, F_SETFL, ret);
+                       ret=fcntl(fd, F_SETFL, ret);
                }
        } else
 #endif /* O_NONBLOCK */
        {
-               ret=ioctl(socket_private_handle->fd, command, arg);
+               ret=ioctl(fd, command, arg);
        }
        if(ret==-1) {
                gint errnum = errno;
@@ -1036,55 +971,40 @@ int _wapi_select(int nfds G_GNUC_UNUSED, fd_set *readfds, fd_set *writefds,
        return(ret);
 }
 
-void _wapi_FD_CLR(guint32 handle, fd_set *set)
+void _wapi_FD_CLR(guint32 fd, fd_set *set)
 {
-       struct _WapiHandlePrivate_socket *socket_private_handle;
-       gboolean ok;
+       gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
        
-       ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
-                               NULL, (gpointer *)&socket_private_handle);
-       if(ok==FALSE) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error looking up socket handle 0x%x", handle);
+       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return;
        }
 
-       FD_CLR(socket_private_handle->fd, set);
+       FD_CLR(fd, set);
 }
 
-int _wapi_FD_ISSET(guint32 handle, fd_set *set)
+int _wapi_FD_ISSET(guint32 fd, fd_set *set)
 {
-       struct _WapiHandlePrivate_socket *socket_private_handle;
-       gboolean ok;
+       gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
        
-       ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
-                               NULL, (gpointer *)&socket_private_handle);
-       if(ok==FALSE) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error looking up socket handle 0x%x", handle);
+       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(0);
        }
 
-       return(FD_ISSET(socket_private_handle->fd, set));
+       return(FD_ISSET(fd, set));
 }
 
-void _wapi_FD_SET(guint32 handle, fd_set *set)
+void _wapi_FD_SET(guint32 fd, fd_set *set)
 {
-       struct _WapiHandlePrivate_socket *socket_private_handle;
-       gboolean ok;
+       gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
        
-       ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
-                               NULL, (gpointer *)&socket_private_handle);
-       if(ok==FALSE) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error looking up socket handle 0x%x", handle);
+       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return;
        }
 
-       FD_SET(socket_private_handle->fd, set);
+       FD_SET(fd, set);
 }
 
 #ifdef USE_AIO
@@ -1119,29 +1039,22 @@ async_notifier (union sigval sig)
 }
 
 static gboolean
-do_aio_call (gboolean is_read, gpointer handle, gpointer buffer,
+do_aio_call (gboolean is_read, gpointer fd_handle, gpointer buffer,
                guint32 numbytes, guint32 *out_bytes,
                gpointer ares,
                SocketAsyncCB callback)
 {
-       struct _WapiHandlePrivate_socket *socket_private_handle;
-       gboolean ok;
-       int fd;
+       gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
+       int fd = GPOINTER_TO_UINT (fd_handle);
        struct aiocb *aio;
        int result;
        notifier_data_t *ndata;
        
-       ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
-                               NULL, (gpointer *)&socket_private_handle);
-       if (ok == FALSE) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error looking up socket handle 0x%x", (guint) handle);
+       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return FALSE;
        }
 
-       fd = socket_private_handle->fd;
-
        ndata = g_new0 (notifier_data_t, 1);
        aio = g_new0 (struct aiocb, 1);
        ndata->ares = ares;
index 59ec8defe3037214d26acb25c9f19a2a3f6477d1..1f48c5cc7684b0f0561946f7c779b408eec489b0 100644 (file)
@@ -35,7 +35,7 @@
 /* Increment this whenever an incompatible change is made to the
  * shared handle structure.
  */
-#define _WAPI_HANDLE_VERSION 1
+#define _WAPI_HANDLE_VERSION 2
 
 typedef enum {
        WAPI_HANDLE_UNUSED=0,