[PATCH] Cygwin: pty: Use autoload feature for pseudo console system calls.

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] Cygwin: pty: Use autoload feature for pseudo console system calls.

Takashi Yano
- The autoload feature is used rather than GetModuleHandle(),
  GetProcAddress() for CreatePseudoConsole(), ResizePseudoConsole()
  and ClosePseudoConsole().
---
 winsup/cygwin/autoload.cc     |  3 +++
 winsup/cygwin/fhandler_tty.cc | 36 +++++++++++++----------------------
 2 files changed, 16 insertions(+), 23 deletions(-)

diff --git a/winsup/cygwin/autoload.cc b/winsup/cygwin/autoload.cc
index c4d91611e..1851ab3b6 100644
--- a/winsup/cygwin/autoload.cc
+++ b/winsup/cygwin/autoload.cc
@@ -759,4 +759,7 @@ LoadDLLfunc (PdhAddEnglishCounterW, 16, pdh)
 LoadDLLfunc (PdhCollectQueryData, 4, pdh)
 LoadDLLfunc (PdhGetFormattedCounterValue, 16, pdh)
 LoadDLLfunc (PdhOpenQueryW, 12, pdh)
+LoadDLLfuncEx (CreatePseudoConsole, 20, kernel32, 1)
+LoadDLLfuncEx (ResizePseudoConsole, 8, kernel32, 1)
+LoadDLLfuncEx (ClosePseudoConsole, 4, kernel32, 1)
 }
diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 5072c6243..659e7b595 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -47,6 +47,12 @@ details. */
 extern "C" int sscanf (const char *, const char *, ...);
 extern "C" int ttyname_r (int, char*, size_t);
 
+extern "C" {
+  HRESULT WINAPI CreatePseudoConsole (COORD, HANDLE, HANDLE, DWORD, HPCON *);
+  HRESULT WINAPI ResizePseudoConsole (HPCON, COORD);
+  VOID WINAPI ClosePseudoConsole (HPCON);
+}
+
 #define close_maybe(h) \
   do { \
     if (h && h != INVALID_HANDLE_VALUE) \
@@ -2157,14 +2163,8 @@ fhandler_pty_master::close ()
   /* FIXME: Pseudo console can be accessed via its handle
      only in the process which created it. What else can we do? */
   if (master_pid_tmp == myself->pid)
-    {
-      /* Release pseudo console */
-      HMODULE hModule = GetModuleHandle ("kernel32.dll");
-      FARPROC func = GetProcAddress (hModule, "ClosePseudoConsole");
-      VOID (WINAPI *ClosePseudoConsole) (HPCON) = NULL;
-      ClosePseudoConsole = (VOID (WINAPI *) (HPCON)) func;
-      ClosePseudoConsole (getPseudoConsole ());
-    }
+    /* Release pseudo console */
+    ClosePseudoConsole (getPseudoConsole ());
   get_ttyp ()->switch_to_pcon_in = false;
   get_ttyp ()->switch_to_pcon_out = false;
  }
@@ -2348,10 +2348,6 @@ fhandler_pty_master::ioctl (unsigned int cmd, void *arg)
  only in the process which created it. What else can we do? */
       if (getPseudoConsole () && get_ttyp ()->master_pid == myself->pid)
  {
-  HMODULE hModule = GetModuleHandle ("kernel32.dll");
-  FARPROC func = GetProcAddress (hModule, "ResizePseudoConsole");
-  HRESULT (WINAPI *ResizePseudoConsole) (HPCON, COORD) = NULL;
-  ResizePseudoConsole = (HRESULT (WINAPI *) (HPCON, COORD)) func;
   COORD size;
   size.X = ((struct winsize *) arg)->ws_col;
   size.Y = ((struct winsize *) arg)->ws_row;
@@ -3103,22 +3099,16 @@ fhandler_pty_master::setup_pseudoconsole ()
      process in a pseudo console and get them from the helper.
      Slave process will attach to the pseudo console in the
      helper process using AttachConsole(). */
-  HMODULE hModule = GetModuleHandle ("kernel32.dll");
-  FARPROC func = GetProcAddress (hModule, "CreatePseudoConsole");
-  HRESULT (WINAPI *CreatePseudoConsole)
-    (COORD, HANDLE, HANDLE, DWORD, HPCON *) = NULL;
-  if (!func)
-    return false;
-  CreatePseudoConsole =
-    (HRESULT (WINAPI *) (COORD, HANDLE, HANDLE, DWORD, HPCON *)) func;
   COORD size = {80, 25};
   CreatePipe (&from_master, &to_slave, &sec_none, 0);
+  SetLastError (ERROR_SUCCESS);
   HRESULT res = CreatePseudoConsole (size, from_master, to_master,
      0, &get_ttyp ()->hPseudoConsole);
-  if (res != S_OK)
+  if (res != S_OK || GetLastError () == ERROR_PROC_NOT_FOUND)
     {
-      system_printf ("CreatePseudoConsole() failed. %08x\n",
-     GetLastError ());
+      if (res != S_OK)
+ system_printf ("CreatePseudoConsole() failed. %08x\n",
+       GetLastError ());
       CloseHandle (from_master);
       CloseHandle (to_slave);
       from_master = from_master_cyg;
--
2.21.0

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] Cygwin: pty: Use autoload feature for pseudo console system calls.

Ken Brown-6
On 9/15/2019 6:55 AM, Takashi Yano wrote:

> - The autoload feature is used rather than GetModuleHandle(),
>    GetProcAddress() for CreatePseudoConsole(), ResizePseudoConsole()
>    and ClosePseudoConsole().
> ---
>   winsup/cygwin/autoload.cc     |  3 +++
>   winsup/cygwin/fhandler_tty.cc | 36 +++++++++++++----------------------
>   2 files changed, 16 insertions(+), 23 deletions(-)
>
> diff --git a/winsup/cygwin/autoload.cc b/winsup/cygwin/autoload.cc
> index c4d91611e..1851ab3b6 100644
> --- a/winsup/cygwin/autoload.cc
> +++ b/winsup/cygwin/autoload.cc
> @@ -759,4 +759,7 @@ LoadDLLfunc (PdhAddEnglishCounterW, 16, pdh)
>   LoadDLLfunc (PdhCollectQueryData, 4, pdh)
>   LoadDLLfunc (PdhGetFormattedCounterValue, 16, pdh)
>   LoadDLLfunc (PdhOpenQueryW, 12, pdh)
> +LoadDLLfuncEx (CreatePseudoConsole, 20, kernel32, 1)
> +LoadDLLfuncEx (ResizePseudoConsole, 8, kernel32, 1)
> +LoadDLLfuncEx (ClosePseudoConsole, 4, kernel32, 1)
>   }
> diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
> index 5072c6243..659e7b595 100644
> --- a/winsup/cygwin/fhandler_tty.cc
> +++ b/winsup/cygwin/fhandler_tty.cc
> @@ -47,6 +47,12 @@ details. */
>   extern "C" int sscanf (const char *, const char *, ...);
>   extern "C" int ttyname_r (int, char*, size_t);
>  
> +extern "C" {
> +  HRESULT WINAPI CreatePseudoConsole (COORD, HANDLE, HANDLE, DWORD, HPCON *);
> +  HRESULT WINAPI ResizePseudoConsole (HPCON, COORD);
> +  VOID WINAPI ClosePseudoConsole (HPCON);
> +}
> +
>   #define close_maybe(h) \
>     do { \
>       if (h && h != INVALID_HANDLE_VALUE) \
> @@ -2157,14 +2163,8 @@ fhandler_pty_master::close ()
>    /* FIXME: Pseudo console can be accessed via its handle
>       only in the process which created it. What else can we do? */
>    if (master_pid_tmp == myself->pid)
> -    {
> -      /* Release pseudo console */
> -      HMODULE hModule = GetModuleHandle ("kernel32.dll");
> -      FARPROC func = GetProcAddress (hModule, "ClosePseudoConsole");
> -      VOID (WINAPI *ClosePseudoConsole) (HPCON) = NULL;
> -      ClosePseudoConsole = (VOID (WINAPI *) (HPCON)) func;
> -      ClosePseudoConsole (getPseudoConsole ());
> -    }
> +    /* Release pseudo console */
> +    ClosePseudoConsole (getPseudoConsole ());
>    get_ttyp ()->switch_to_pcon_in = false;
>    get_ttyp ()->switch_to_pcon_out = false;
>   }
> @@ -2348,10 +2348,6 @@ fhandler_pty_master::ioctl (unsigned int cmd, void *arg)
>   only in the process which created it. What else can we do? */
>         if (getPseudoConsole () && get_ttyp ()->master_pid == myself->pid)
>   {
> -  HMODULE hModule = GetModuleHandle ("kernel32.dll");
> -  FARPROC func = GetProcAddress (hModule, "ResizePseudoConsole");
> -  HRESULT (WINAPI *ResizePseudoConsole) (HPCON, COORD) = NULL;
> -  ResizePseudoConsole = (HRESULT (WINAPI *) (HPCON, COORD)) func;
>    COORD size;
>    size.X = ((struct winsize *) arg)->ws_col;
>    size.Y = ((struct winsize *) arg)->ws_row;
> @@ -3103,22 +3099,16 @@ fhandler_pty_master::setup_pseudoconsole ()
>        process in a pseudo console and get them from the helper.
>        Slave process will attach to the pseudo console in the
>        helper process using AttachConsole(). */
> -  HMODULE hModule = GetModuleHandle ("kernel32.dll");
> -  FARPROC func = GetProcAddress (hModule, "CreatePseudoConsole");
> -  HRESULT (WINAPI *CreatePseudoConsole)
> -    (COORD, HANDLE, HANDLE, DWORD, HPCON *) = NULL;
> -  if (!func)
> -    return false;
> -  CreatePseudoConsole =
> -    (HRESULT (WINAPI *) (COORD, HANDLE, HANDLE, DWORD, HPCON *)) func;
>     COORD size = {80, 25};
>     CreatePipe (&from_master, &to_slave, &sec_none, 0);
> +  SetLastError (ERROR_SUCCESS);
>     HRESULT res = CreatePseudoConsole (size, from_master, to_master,
>       0, &get_ttyp ()->hPseudoConsole);
> -  if (res != S_OK)
> +  if (res != S_OK || GetLastError () == ERROR_PROC_NOT_FOUND)
>       {
> -      system_printf ("CreatePseudoConsole() failed. %08x\n",
> -     GetLastError ());
> +      if (res != S_OK)
> + system_printf ("CreatePseudoConsole() failed. %08x\n",
> +       GetLastError ());
>         CloseHandle (from_master);
>         CloseHandle (to_slave);
>         from_master = from_master_cyg;

Pushed.  Thanks.

Ken

P.S. I'm building a new test release now, which I'll upload in a few hours
unless you discover something else and want me to wait.