From 8700c4341b2e23362526b5d28222020c601dd66e Mon Sep 17 00:00:00 2001
From: Alexandre Julliard <julliard@winehq.org>
Date: Thu, 2 Nov 2006 20:52:05 +0100
Subject: [PATCH] server: Get rid of the server-side file descriptor cache
 management.

---
 include/wine/server_protocol.h | 21 +-------------
 server/console.c               |  2 +-
 server/debugger.c              | 18 ++++++------
 server/fd.c                    | 17 -----------
 server/handle.c                | 52 ++--------------------------------
 server/handle.h                |  5 +---
 server/named_pipe.c            |  3 --
 server/protocol.def            | 13 ---------
 server/request.h               |  2 --
 server/trace.c                 | 28 ++----------------
 server/winstation.c            |  6 ++--
 11 files changed, 20 insertions(+), 147 deletions(-)

diff --git a/include/wine/server_protocol.h b/include/wine/server_protocol.h
index b3926512b23..577c797183a 100644
--- a/include/wine/server_protocol.h
+++ b/include/wine/server_protocol.h
@@ -551,7 +551,6 @@ struct close_handle_request
 struct close_handle_reply
 {
     struct reply_header __header;
-    int          fd;
 };
 
 
@@ -856,20 +855,6 @@ struct get_handle_fd_reply
 #define FD_FLAG_REMOVABLE          0x20
 
 
-struct set_handle_fd_request
-{
-    struct request_header __header;
-    obj_handle_t handle;
-    int          fd;
-};
-struct set_handle_fd_reply
-{
-    struct reply_header __header;
-    int          cur_fd;
-};
-
-
-
 struct flush_file_request
 {
     struct request_header __header;
@@ -2533,7 +2518,6 @@ struct disconnect_named_pipe_request
 struct disconnect_named_pipe_reply
 {
     struct reply_header __header;
-    int            fd;
 };
 
 
@@ -3807,7 +3791,6 @@ enum request
     REQ_open_file_object,
     REQ_alloc_file_handle,
     REQ_get_handle_fd,
-    REQ_set_handle_fd,
     REQ_flush_file,
     REQ_lock_file,
     REQ_unlock_file,
@@ -4028,7 +4011,6 @@ union generic_request
     struct open_file_object_request open_file_object_request;
     struct alloc_file_handle_request alloc_file_handle_request;
     struct get_handle_fd_request get_handle_fd_request;
-    struct set_handle_fd_request set_handle_fd_request;
     struct flush_file_request flush_file_request;
     struct lock_file_request lock_file_request;
     struct unlock_file_request unlock_file_request;
@@ -4247,7 +4229,6 @@ union generic_reply
     struct open_file_object_reply open_file_object_reply;
     struct alloc_file_handle_reply alloc_file_handle_reply;
     struct get_handle_fd_reply get_handle_fd_reply;
-    struct set_handle_fd_reply set_handle_fd_reply;
     struct flush_file_reply flush_file_reply;
     struct lock_file_reply lock_file_reply;
     struct unlock_file_reply unlock_file_reply;
@@ -4425,6 +4406,6 @@ union generic_reply
     struct query_symlink_reply query_symlink_reply;
 };
 
-#define SERVER_PROTOCOL_VERSION 257
+#define SERVER_PROTOCOL_VERSION 258
 
 #endif /* __WINE_WINE_SERVER_PROTOCOL_H */
diff --git a/server/console.c b/server/console.c
index 65902586a06..5f448270c4b 100644
--- a/server/console.c
+++ b/server/console.c
@@ -1279,7 +1279,7 @@ DECL_HANDLER(alloc_console)
                 release_object( console );
                 goto the_end;
             }
-            close_handle( renderer, in, NULL );
+            close_handle( renderer, in );
         }
         free_console( process );
     }
diff --git a/server/debugger.c b/server/debugger.c
index fa4371db595..72de05fc19f 100644
--- a/server/debugger.c
+++ b/server/debugger.c
@@ -140,7 +140,7 @@ static int fill_create_process_event( struct debug_event *event, void *arg )
     /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
     if (!(handle = alloc_handle( debugger, thread, THREAD_ALL_ACCESS, 0 )))
     {
-        close_handle( debugger, event->data.info.create_process.process, NULL );
+        close_handle( debugger, event->data.info.create_process.process );
         return 0;
     }
     event->data.info.create_process.thread = handle;
@@ -150,8 +150,8 @@ static int fill_create_process_event( struct debug_event *event, void *arg )
         /* the doc says write access too, but this doesn't seem a good idea */
         !(handle = alloc_handle( debugger, exe_module->file, GENERIC_READ, 0 )))
     {
-        close_handle( debugger, event->data.info.create_process.process, NULL );
-        close_handle( debugger, event->data.info.create_process.thread, NULL );
+        close_handle( debugger, event->data.info.create_process.process );
+        close_handle( debugger, event->data.info.create_process.thread );
         return 0;
     }
     event->data.info.create_process.file       = handle;
@@ -287,17 +287,17 @@ static void debug_event_destroy( struct object *obj )
         switch(event->data.code)
         {
         case CREATE_THREAD_DEBUG_EVENT:
-            close_handle( debugger, event->data.info.create_thread.handle, NULL );
+            close_handle( debugger, event->data.info.create_thread.handle );
             break;
         case CREATE_PROCESS_DEBUG_EVENT:
             if (event->data.info.create_process.file)
-                close_handle( debugger, event->data.info.create_process.file, NULL );
-            close_handle( debugger, event->data.info.create_process.thread, NULL );
-            close_handle( debugger, event->data.info.create_process.process, NULL );
+                close_handle( debugger, event->data.info.create_process.file );
+            close_handle( debugger, event->data.info.create_process.thread );
+            close_handle( debugger, event->data.info.create_process.process );
             break;
         case LOAD_DLL_DEBUG_EVENT:
             if (event->data.info.load_dll.handle)
-                close_handle( debugger, event->data.info.load_dll.handle, NULL );
+                close_handle( debugger, event->data.info.load_dll.handle );
             break;
         }
     }
@@ -661,7 +661,7 @@ DECL_HANDLER(get_exception_status)
     if ((event = (struct debug_event *)get_handle_obj( current->process, req->handle,
                                                        0, &debug_event_ops )))
     {
-        close_handle( current->process, req->handle, NULL );
+        close_handle( current->process, req->handle );
         if (event->state == EVENT_CONTINUED)
         {
             if (current->context == &event->context)
diff --git a/server/fd.c b/server/fd.c
index 25a2fe0ba1b..25b9a7c4d59 100644
--- a/server/fd.c
+++ b/server/fd.c
@@ -1957,23 +1957,6 @@ DECL_HANDLER(get_handle_fd)
     }
 }
 
-/* set the cached file descriptor of a handle */
-DECL_HANDLER(set_handle_fd)
-{
-    struct fd *fd;
-
-    reply->cur_fd = -1;
-    if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
-    {
-        struct device *device = fd->inode ? fd->inode->device : NULL;
-
-        /* only cache the fd on non-removable devices */
-        if (!device || !device->removable)
-            reply->cur_fd = set_handle_unix_fd( current->process, req->handle, req->fd );
-        release_object( fd );
-    }
-}
-
 /* get ready to unmount a Unix device */
 DECL_HANDLER(unmount_device)
 {
diff --git a/server/handle.c b/server/handle.c
index 9736ad2dea1..887fbca9200 100644
--- a/server/handle.c
+++ b/server/handle.c
@@ -42,7 +42,6 @@ struct handle_entry
 {
     struct object *ptr;       /* object */
     unsigned int   access;    /* access rights */
-    int            fd;        /* file descriptor (in client process) */
 };
 
 struct handle_table
@@ -218,7 +217,6 @@ static obj_handle_t alloc_entry( struct handle_table *table, void *obj, unsigned
     table->free = i + 1;
     entry->ptr    = grab_object( obj );
     entry->access = access;
-    entry->fd     = -1;
     return index_to_handle(i);
 }
 
@@ -319,7 +317,6 @@ struct handle_table *copy_handle_table( struct process *process, struct process
         for (i = 0; i <= table->last; i++, ptr++)
         {
             if (!ptr->ptr) continue;
-            ptr->fd = -1;
             if (ptr->access & RESERVED_INHERIT) grab_object( ptr->ptr );
             else ptr->ptr = NULL; /* don't inherit this entry */
         }
@@ -331,7 +328,7 @@ struct handle_table *copy_handle_table( struct process *process, struct process
 
 /* close a handle and decrement the refcount of the associated object */
 /* return 1 if OK, 0 on error */
-int close_handle( struct process *process, obj_handle_t handle, int *fd )
+int close_handle( struct process *process, obj_handle_t handle )
 {
     struct handle_table *table;
     struct handle_entry *entry;
@@ -350,9 +347,6 @@ int close_handle( struct process *process, obj_handle_t handle, int *fd )
         return 0;
     }
     entry->ptr = NULL;
-    if (fd) *fd = entry->fd;
-    else if (entry->fd != -1) return 1;  /* silently ignore close attempt if we cannot close the fd */
-    entry->fd = -1;
     table = handle_is_global(handle) ? global_table : process->handles;
     if (entry < table->entries + table->free) table->free = entry - table->entries;
     if (entry == table->entries + table->last) shrink_handle_table( table );
@@ -410,46 +404,6 @@ unsigned int get_handle_access( struct process *process, obj_handle_t handle )
     return entry->access;
 }
 
-/* retrieve the cached fd for a given handle */
-int get_handle_unix_fd( struct process *process, obj_handle_t handle, unsigned int access )
-{
-    struct handle_entry *entry;
-
-    if (!(entry = get_handle( process, handle ))) return -1;
-    if ((entry->access & access) != access)
-    {
-        set_error( STATUS_ACCESS_DENIED );
-        return -1;
-    }
-    return entry->fd;
-}
-
-/* set the cached fd for a handle if not set already, and return the current value */
-int set_handle_unix_fd( struct process *process, obj_handle_t handle, int fd )
-{
-    struct handle_entry *entry;
-
-    if (handle_is_global( handle )) return -1;  /* no fd cache for global handles */
-    if (!(entry = get_handle( process, handle ))) return -1;
-    /* if no current fd set it, otherwise return current fd */
-    if (entry->fd == -1) entry->fd = fd;
-    return entry->fd;
-}
-
-/* remove the cached fd and return it */
-int flush_cached_fd( struct process *process, obj_handle_t handle )
-{
-    struct handle_entry *entry = get_handle( process, handle );
-    int fd = -1;
-
-    if (entry)
-    {
-        fd = entry->fd;
-        entry->fd = -1;
-    }
-    return fd;
-}
-
 /* find the first inherited handle of the given type */
 /* this is needed for window stations and desktops (don't ask...) */
 obj_handle_t find_inherited_handle( struct process *process, const struct object_ops *ops )
@@ -547,7 +501,7 @@ unsigned int get_handle_table_count( struct process *process )
 /* close a handle */
 DECL_HANDLER(close_handle)
 {
-    close_handle( current->process, req->handle, &reply->fd );
+    close_handle( current->process, req->handle );
 }
 
 /* set a handle information */
@@ -579,7 +533,7 @@ DECL_HANDLER(dup_handle)
         if (req->options & DUP_HANDLE_CLOSE_SOURCE)
         {
             unsigned int err = get_error();  /* don't overwrite error from the above calls */
-            reply->closed = close_handle( src, req->src_handle, NULL );
+            reply->closed = close_handle( src, req->src_handle );
             set_error( err );
         }
         release_object( src );
diff --git a/server/handle.h b/server/handle.h
index 94040b3f4fe..db19590b63f 100644
--- a/server/handle.h
+++ b/server/handle.h
@@ -36,12 +36,10 @@ struct unicode_str;
 /* that the thing pointed to starts with a struct object... */
 extern obj_handle_t alloc_handle( struct process *process, void *obj,
                                   unsigned int access, unsigned int attr );
-extern int close_handle( struct process *process, obj_handle_t handle, int *fd );
+extern int close_handle( struct process *process, obj_handle_t handle );
 extern struct object *get_handle_obj( struct process *process, obj_handle_t handle,
                                       unsigned int access, const struct object_ops *ops );
 extern unsigned int get_handle_access( struct process *process, obj_handle_t handle );
-extern int get_handle_unix_fd( struct process *process, obj_handle_t handle, unsigned int access );
-extern int set_handle_unix_fd( struct process *process, obj_handle_t handle, int fd );
 extern obj_handle_t duplicate_handle( struct process *src, obj_handle_t src_handle, struct process *dst,
                                       unsigned int access, unsigned int attr, unsigned int options );
 extern obj_handle_t open_object( const struct namespace *namespace, const struct unicode_str *name,
@@ -50,6 +48,5 @@ extern obj_handle_t find_inherited_handle( struct process *process, const struct
 extern struct handle_table *alloc_handle_table( struct process *process, int count );
 extern struct handle_table *copy_handle_table( struct process *process, struct process *parent );
 extern unsigned int get_handle_table_count( struct process *process);
-extern int flush_cached_fd( struct process *process, obj_handle_t handle );
 
 #endif  /* __WINE_SERVER_HANDLE_H */
diff --git a/server/named_pipe.c b/server/named_pipe.c
index 9edd1061c51..bb0e7417dbb 100644
--- a/server/named_pipe.c
+++ b/server/named_pipe.c
@@ -884,7 +884,6 @@ DECL_HANDLER(disconnect_named_pipe)
 {
     struct pipe_server *server;
 
-    reply->fd = -1;
     server = get_pipe_server_obj( current->process, req->handle, 0 );
     if (!server)
         return;
@@ -901,7 +900,6 @@ DECL_HANDLER(disconnect_named_pipe)
            around - client loses all waiting data */
         server->state = ps_disconnected_server;
         do_disconnect( server );
-        reply->fd = flush_cached_fd( current->process, req->handle );
         break;
 
     case ps_wait_disconnect:
@@ -909,7 +907,6 @@ DECL_HANDLER(disconnect_named_pipe)
         assert( server->fd );
         do_disconnect( server );
         server->state = ps_wait_connect;
-        reply->fd = flush_cached_fd( current->process, req->handle );
         break;
 
     case ps_idle_server:
diff --git a/server/protocol.def b/server/protocol.def
index c1392491939..a4a2376d66c 100644
--- a/server/protocol.def
+++ b/server/protocol.def
@@ -460,8 +460,6 @@ enum apc_type { APC_NONE, APC_USER, APC_TIMER, APC_ASYNC_IO };
 /* Close a handle for the current process */
 @REQ(close_handle)
     obj_handle_t handle;       /* handle to close */
-@REPLY
-    int          fd;           /* associated fd to close */
 @END
 
 
@@ -673,15 +671,6 @@ enum event_op { PULSE_EVENT, SET_EVENT, RESET_EVENT };
                                          * only handle available data (don't wait) */
 #define FD_FLAG_REMOVABLE          0x20 /* is it on a removable device? */
 
-/* Set the cached file descriptor of a handle */
-@REQ(set_handle_fd)
-    obj_handle_t handle;       /* handle we are interested in */
-    int          fd;           /* file descriptor */
-@REPLY
-    int          cur_fd;       /* current file descriptor */
-@END
-
-
 /* Flush a file buffers */
 @REQ(flush_file)
     obj_handle_t handle;        /* handle to the file */
@@ -1805,8 +1794,6 @@ enum message_type
 /* Disconnect a named pipe */
 @REQ(disconnect_named_pipe)
     obj_handle_t   handle;
-@REPLY
-    int            fd;           /* associated fd to close */
 @END
 
 
diff --git a/server/request.h b/server/request.h
index fd805e3bb99..8cfeccd0f05 100644
--- a/server/request.h
+++ b/server/request.h
@@ -148,7 +148,6 @@ DECL_HANDLER(create_file);
 DECL_HANDLER(open_file_object);
 DECL_HANDLER(alloc_file_handle);
 DECL_HANDLER(get_handle_fd);
-DECL_HANDLER(set_handle_fd);
 DECL_HANDLER(flush_file);
 DECL_HANDLER(lock_file);
 DECL_HANDLER(unlock_file);
@@ -368,7 +367,6 @@ static const req_handler req_handlers[REQ_NB_REQUESTS] =
     (req_handler)req_open_file_object,
     (req_handler)req_alloc_file_handle,
     (req_handler)req_get_handle_fd,
-    (req_handler)req_set_handle_fd,
     (req_handler)req_flush_file,
     (req_handler)req_lock_file,
     (req_handler)req_unlock_file,
diff --git a/server/trace.c b/server/trace.c
index 6ed28909c47..9931ea0ab86 100644
--- a/server/trace.c
+++ b/server/trace.c
@@ -871,11 +871,6 @@ static void dump_close_handle_request( const struct close_handle_request *req )
     fprintf( stderr, " handle=%p", req->handle );
 }
 
-static void dump_close_handle_reply( const struct close_handle_reply *req )
-{
-    fprintf( stderr, " fd=%d", req->fd );
-}
-
 static void dump_set_handle_info_request( const struct set_handle_info_request *req )
 {
     fprintf( stderr, " handle=%p,", req->handle );
@@ -1112,17 +1107,6 @@ static void dump_get_handle_fd_reply( const struct get_handle_fd_reply *req )
     fprintf( stderr, " flags=%d", req->flags );
 }
 
-static void dump_set_handle_fd_request( const struct set_handle_fd_request *req )
-{
-    fprintf( stderr, " handle=%p,", req->handle );
-    fprintf( stderr, " fd=%d", req->fd );
-}
-
-static void dump_set_handle_fd_reply( const struct set_handle_fd_reply *req )
-{
-    fprintf( stderr, " cur_fd=%d", req->cur_fd );
-}
-
 static void dump_flush_file_request( const struct flush_file_request *req )
 {
     fprintf( stderr, " handle=%p", req->handle );
@@ -2293,11 +2277,6 @@ static void dump_disconnect_named_pipe_request( const struct disconnect_named_pi
     fprintf( stderr, " handle=%p", req->handle );
 }
 
-static void dump_disconnect_named_pipe_reply( const struct disconnect_named_pipe_reply *req )
-{
-    fprintf( stderr, " fd=%d", req->fd );
-}
-
 static void dump_get_named_pipe_info_request( const struct get_named_pipe_info_request *req )
 {
     fprintf( stderr, " handle=%p", req->handle );
@@ -3322,7 +3301,6 @@ static const dump_func req_dumpers[REQ_NB_REQUESTS] = {
     (dump_func)dump_open_file_object_request,
     (dump_func)dump_alloc_file_handle_request,
     (dump_func)dump_get_handle_fd_request,
-    (dump_func)dump_set_handle_fd_request,
     (dump_func)dump_flush_file_request,
     (dump_func)dump_lock_file_request,
     (dump_func)dump_unlock_file_request,
@@ -3520,7 +3498,7 @@ static const dump_func reply_dumpers[REQ_NB_REQUESTS] = {
     (dump_func)0,
     (dump_func)0,
     (dump_func)dump_get_apc_reply,
-    (dump_func)dump_close_handle_reply,
+    (dump_func)0,
     (dump_func)dump_set_handle_info_reply,
     (dump_func)dump_dup_handle_reply,
     (dump_func)dump_open_process_reply,
@@ -3539,7 +3517,6 @@ static const dump_func reply_dumpers[REQ_NB_REQUESTS] = {
     (dump_func)dump_open_file_object_reply,
     (dump_func)dump_alloc_file_handle_reply,
     (dump_func)dump_get_handle_fd_reply,
-    (dump_func)dump_set_handle_fd_reply,
     (dump_func)dump_flush_file_reply,
     (dump_func)dump_lock_file_reply,
     (dump_func)0,
@@ -3639,7 +3616,7 @@ static const dump_func reply_dumpers[REQ_NB_REQUESTS] = {
     (dump_func)dump_open_named_pipe_reply,
     (dump_func)0,
     (dump_func)0,
-    (dump_func)dump_disconnect_named_pipe_reply,
+    (dump_func)0,
     (dump_func)dump_get_named_pipe_info_reply,
     (dump_func)dump_create_window_reply,
     (dump_func)0,
@@ -3756,7 +3733,6 @@ static const char * const req_names[REQ_NB_REQUESTS] = {
     "open_file_object",
     "alloc_file_handle",
     "get_handle_fd",
-    "set_handle_fd",
     "flush_file",
     "lock_file",
     "unlock_file",
diff --git a/server/winstation.c b/server/winstation.c
index e7d2ba64b67..44c66a5dc75 100644
--- a/server/winstation.c
+++ b/server/winstation.c
@@ -353,7 +353,7 @@ void close_thread_desktop( struct thread *thread )
     obj_handle_t handle = thread->desktop;
 
     thread->desktop = 0;
-    if (handle) close_handle( thread->process, handle, NULL );
+    if (handle) close_handle( thread->process, handle );
     clear_error();  /* ignore errors */
 }
 
@@ -395,7 +395,7 @@ DECL_HANDLER(close_winstation)
     if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
                                                            0, &winstation_ops )))
     {
-        if (!close_handle( current->process, req->handle, NULL )) set_error( STATUS_ACCESS_DENIED );
+        if (!close_handle( current->process, req->handle )) set_error( STATUS_ACCESS_DENIED );
         release_object( winstation );
     }
 }
@@ -474,7 +474,7 @@ DECL_HANDLER(close_desktop)
     if ((desktop = (struct desktop *)get_handle_obj( current->process, req->handle,
                                                      0, &desktop_ops )))
     {
-        if (!close_handle( current->process, req->handle, NULL )) set_error( STATUS_DEVICE_BUSY );
+        if (!close_handle( current->process, req->handle )) set_error( STATUS_DEVICE_BUSY );
         release_object( desktop );
     }
 }
-- 
GitLab