This is the mail archive of the
cygwin-patches
mailing list for the Cygwin project.
Re: [PATCH] Cygwin: pty: Use autoload feature for pseudo console system calls.
- From: Ken Brown <kbrown at cornell dot edu>
- To: "cygwin-patches at cygwin dot com" <cygwin-patches at cygwin dot com>
- Date: Sun, 15 Sep 2019 15:39:42 +0000
- Subject: Re: [PATCH] Cygwin: pty: Use autoload feature for pseudo console system calls.
- Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=cornell.edu; dmarc=pass action=none header.from=cornell.edu; dkim=pass header.d=cornell.edu; arc=none
- Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=aLofUbKgypGG7q54v2Gg6gyR9d8jFSzt+BZKXYUfVJ8=; b=SQPHtfjQoMdHw9qaUBOqvgA/PZoQTtQXfX/dL/MFgbZI3nzlJ0c3ktoUMVyOTF/lssJ5RhH+1WqgJCm04xVxt1GrVTtd/BOnavkHjIz+7a+p18To5HeJYdaiwojvilSpRxIUU9kHINq8WcDCIWKPhrMt0hrQQgEyEWxS5MgxO0lPA4xZj4COt6CbewTXWURMDo1wWVtJYgyJDQ5q/9TPgxUr+eDp7ZDtCrInTpJ2sxwYlTOVuCBnXhh6iQ94xO47ZxIKdQU122lz9evHJGXfy+EcMGsg7c9L1Lr68fP9hRGjrIN10UVPLtPYyGn9J4PRlez0jHNf6BAU6HF3Mru9UA==
- Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JPnLlFXv13H+Py3n7RFRcwTlGnYyKslySfXeK4k3VqXT1Tld4PaLB3kmoOuVHBaViM3a84dSQIEntHS6FePhykZzMeJ58o66ZEog2TZ+fnQhtIbkh2jIfrHkUEloGkR3DkmcfCiEQbPzZpBLUm7fCXhj4M8xvkIl0R9ghcuadDyBllnIK6h3McciT17jyShSEO7WoL55TNkQz42Pd+cZsqSjMpFso2zXESWdKbJ4AdcFBNo3cCW6OfCD7CwRz9h/5QX8H2LvFY1Rz1z6iogf80zer6TvwpTOeYYfcH0axUhuGeychXmk+TcWXyfUxWUIRi3lfGYPN/w/u36I1sDRuQ==
- References: <20190915105544.1918-1-takashi.yano@nifty.ne.jp>
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.