From a5a872efca241e55a960d9e902afd180021bede5 Mon Sep 17 00:00:00 2001
From: Alexandre Julliard <julliard@winehq.org>
Date: Thu, 22 Mar 2001 19:35:27 +0000
Subject: [PATCH] Use the new send_fd mechanism for the set_console_fd server
 request.

---
 include/server.h    |  6 +++---
 scheduler/process.c | 14 ++++++--------
 server/console.c    | 34 +++++++---------------------------
 server/trace.c      |  4 ++--
 win32/console.c     | 27 +++++++++++++--------------
 5 files changed, 31 insertions(+), 54 deletions(-)

diff --git a/include/server.h b/include/server.h
index 1493d1a8bc9..87904b10b64 100644
--- a/include/server.h
+++ b/include/server.h
@@ -757,8 +757,8 @@ struct set_console_fd_request
 {
     REQUEST_HEADER;                 /* request header */
     IN  handle_t     handle;        /* handle to the console */
-    IN  handle_t     handle_in;     /* handle of file to use as input */
-    IN  handle_t     handle_out;    /* handle of file to use as output */
+    IN  int          fd_in;         /* file descriptor to use as input */
+    IN  int          fd_out;        /* file descriptor to use as output */
     IN  int          pid;           /* pid of xterm (hack) */
 };
 
@@ -1611,7 +1611,7 @@ union generic_request
     struct async_result_request async_result;
 };
 
-#define SERVER_PROTOCOL_VERSION 42
+#define SERVER_PROTOCOL_VERSION 43
 
 /* ### make_requests end ### */
 /* Everything above this line is generated automatically by tools/make_requests */
diff --git a/scheduler/process.c b/scheduler/process.c
index 39ab851ac77..6d3ad5ecaff 100644
--- a/scheduler/process.c
+++ b/scheduler/process.c
@@ -229,20 +229,18 @@ void PROCESS_CallUserSignalProc( UINT uCode, HMODULE hModule )
  */
 static void set_console_handles( HANDLE console )
 {
-    HANDLE in = FILE_DupUnixHandle( 0, GENERIC_READ );
-    HANDLE out = FILE_DupUnixHandle( 1, GENERIC_WRITE );
+    wine_server_send_fd( 0 );
+    wine_server_send_fd( 1 );
 
     SERVER_START_REQ( set_console_fd )
     {
-        req->handle     = console;
-        req->handle_in  = in;
-        req->handle_out = out;
-        req->pid = 0;
+        req->handle = console;
+        req->fd_in  = 0;
+        req->fd_out = 1;
+        req->pid    = 0;
         SERVER_CALL();
     }
     SERVER_END_REQ;
-    NtClose( in );
-    NtClose( out );
 }
 
 
diff --git a/server/console.c b/server/console.c
index 34190704e5c..60324e95844 100644
--- a/server/console.c
+++ b/server/console.c
@@ -477,36 +477,16 @@ DECL_HANDLER(get_console_info)
 /* set a console fd */
 DECL_HANDLER(set_console_fd)
 {
-    struct object *obj_in, *obj_out;
-    int fd_in, fd_out;
+    int fd_out, fd_in  = thread_get_inflight_fd( current, req->fd_in );
 
-    if (!(obj_in = get_handle_obj( current->process, req->handle_in, GENERIC_READ, NULL )))
-        return;
-    if ((fd_in = dup(obj_in->ops->get_fd( obj_in ))) == -1)
-    {
-        release_object( obj_in );
-        return;
-    }
-    release_object( obj_in );
+    if (req->fd_out == req->fd_in) fd_out = dup( fd_in );
+    else fd_out = thread_get_inflight_fd( current, req->fd_out );
 
-    if (!(obj_out = get_handle_obj( current->process, req->handle_out, GENERIC_WRITE, NULL )))
-    {
-        close( fd_in );
-        return;
-    }
-    if ((fd_out = dup(obj_out->ops->get_fd( obj_out ))) == -1)
-    {
-        release_object( obj_out );
-        close( fd_in );
-        return;
-    }
-    release_object( obj_out );
+    if (fd_in == -1 || fd_out == -1) set_error( STATUS_INVALID_HANDLE );
+    else if (set_console_fd( req->handle, fd_in, fd_out, req->pid )) return;
 
-    if (!set_console_fd( req->handle, fd_in, fd_out, req->pid ))
-    {
-        close( fd_out );
-        close( fd_in );
-    }
+    if (fd_in != -1) close( fd_in );
+    if (fd_out != -1) close( fd_out );
 }
 
 /* get a console mode (input or output) */
diff --git a/server/trace.c b/server/trace.c
index 30f5b8509c4..367c2580e1e 100644
--- a/server/trace.c
+++ b/server/trace.c
@@ -869,8 +869,8 @@ static void dump_open_console_reply( const struct open_console_request *req )
 static void dump_set_console_fd_request( const struct set_console_fd_request *req )
 {
     fprintf( stderr, " handle=%d,", req->handle );
-    fprintf( stderr, " handle_in=%d,", req->handle_in );
-    fprintf( stderr, " handle_out=%d,", req->handle_out );
+    fprintf( stderr, " fd_in=%d,", req->fd_in );
+    fprintf( stderr, " fd_out=%d,", req->fd_out );
     fprintf( stderr, " pid=%d", req->pid );
 }
 
diff --git a/win32/console.c b/win32/console.c
index fb1ec7f25fa..960df02772c 100644
--- a/win32/console.c
+++ b/win32/console.c
@@ -42,7 +42,6 @@
 #include "wine/winuser16.h"
 #include "wine/keyboard16.h"
 #include "thread.h"
-#include "file.h"
 #include "winerror.h"
 #include "wincon.h"
 #include "heap.h"
@@ -580,7 +579,6 @@ static BOOL CONSOLE_make_complex(HANDLE handle)
 	char buf[256];
 	char c = '\0';
 	int i,xpid,master,slave;
-        HANDLE pty_handle;
 
         if (CONSOLE_GetPid( handle )) return TRUE; /* already complex */
 
@@ -605,36 +603,37 @@ static BOOL CONSOLE_make_complex(HANDLE handle)
 		ERR("error creating AllocConsole xterm\n");
 		exit(1);
 	}
-        pty_handle = FILE_DupUnixHandle( slave, GENERIC_READ | GENERIC_WRITE );
         close( master );
-        close( slave );
-        if (!pty_handle) return FALSE;
 
 	/* most xterms like to print their window ID when used with -S;
 	 * read it and continue before the user has a chance...
 	 */
         for (i = 0; i < 10000; i++)
         {
-            BOOL ok = ReadFile( pty_handle, &c, 1, NULL, NULL );
-            if (!ok && !c) usleep(100); /* wait for xterm to be created */
-            else if (c == '\n') break;
+            if (read( slave, &c, 1 ) == 1)
+            {
+                if (c == '\n') break;
+            }
+            else usleep(100); /* wait for xterm to be created */
         }
         if (i == 10000)
         {
             ERR("can't read xterm WID\n");
-            CloseHandle( pty_handle );
+            close( slave );
             return FALSE;
 	}
+
+        wine_server_send_fd( slave );
         SERVER_START_REQ( set_console_fd )
         {
-            req->handle     = handle;
-            req->handle_in  = pty_handle;
-            req->handle_out = pty_handle;
-            req->pid = xpid;
+            req->handle = handle;
+            req->fd_in  = slave;
+            req->fd_out = slave;
+            req->pid    = xpid;
             SERVER_CALL();
+            close( slave );
         }
         SERVER_END_REQ;
-        CloseHandle( pty_handle );
 
 	/* enable mouseclicks */
 	strcpy( buf, "\033[?1002h" );
-- 
GitLab