[PATCH] Cygwin: pty: Use autoload feature for pseudo console system calls.
- 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
Re: [PATCH 0/3] Some fixes for PTY with pseudo console support (3)
On 9/15/2019 12:05 AM, Takashi Yano wrote: > [PATCH 1/3] Fix bad file descriptor error in some environment. > The bad file descriptor problem reported in: > https://cygwin.com/ml/cygwin-patches/2019-q3/msg00104.html > was recurring. Fixed again. > > [PATCH 2/3] Use system NLS function instead of PTY's own one. > Since calling system __loadlocale() caused execution error, > PTY used its own NLS function. The cause of the error has been > found, the corresponding code has been rewritten using system > function. > > [PATCH 3/3] Change the timing of clearing screen. > The code which clears screen is moved from reset_switch_to_pcon() > to fixup_after_exec() because it seems not too early even at this > timing. > > Takashi Yano (3): >Cygwin: pty: Fix bad file descriptor error in some environment. >Cygwin: pty: Use system NLS function instead of PTY's own one. >Cygwin: pty: Change the timing of clearing screen. > > winsup/cygwin/fhandler.h | 1 + > winsup/cygwin/fhandler_tty.cc | 527 -- > winsup/cygwin/tty.cc | 2 +- > winsup/cygwin/tty.h | 2 +- > 4 files changed, 120 insertions(+), 412 deletions(-) Pushed. Thanks. Ken
Re: [PATCH] Cygwin: pty: Correct typos that do not fit the coding style.
On 9/15/2019 12:36 AM, Takashi Yano wrote: > --- > winsup/cygwin/fhandler_tty.cc | 26 +- > 1 file changed, 13 insertions(+), 13 deletions(-) > > diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc > index 5c27510be..5072c6243 100644 > --- a/winsup/cygwin/fhandler_tty.cc > +++ b/winsup/cygwin/fhandler_tty.cc > @@ -45,7 +45,7 @@ details. */ > #endif /* ENABLE_VIRTUAL_TERMINAL_INPUT */ > > extern "C" int sscanf (const char *, const char *, ...); > -extern "C" int ttyname_r(int, char*, size_t); > +extern "C" int ttyname_r (int, char*, size_t); > > #define close_maybe(h) \ > do { \ > @@ -2147,7 +2147,7 @@ fhandler_pty_master::close () > else if (obi.HandleCount == (getPseudoConsole () ? 2 : 1)) > /* Helper process has inherited one. */ > { > - termios_printf("Closing last master of pty%d", get_minor ()); > + termios_printf ("Closing last master of pty%d", get_minor ()); > /* Close Pseudo Console */ > if (getPseudoConsole ()) > { > @@ -2446,9 +2446,9 @@ get_locale_from_env (char *locale) > char lang[ENCODING_LEN + 1] = {0, }, country[ENCODING_LEN + 1] = {0, }; > env = getenv ("LC_ALL"); > if (env == NULL || !*env) > -env = getenv("LC_CTYPE"); > +env = getenv ("LC_CTYPE"); > if (env == NULL || !*env) > -env = getenv("LANG"); > +env = getenv ("LANG"); > if (env == NULL || !*env) > { > if (GetLocaleInfo (LOCALE_CUSTOM_UI_DEFAULT, > @@ -2476,7 +2476,7 @@ get_locale_from_env (char *locale) >LOCALE_SISO3166CTRYNAME, >country, sizeof (country)); > if (strlen (lang) && strlen (country)) > - __small_sprintf (lang + strlen(lang), "_%s.UTF-8", country); > + __small_sprintf (lang + strlen (lang), "_%s.UTF-8", country); > else > strcpy (lang , "C.UTF-8"); > env = lang; > @@ -2492,7 +2492,7 @@ get_langinfo (char *locale_out, char *charset_out) > get_locale_from_env (new_locale); > > __locale_t loc; > - memset(&loc, 0, sizeof (loc)); > + memset (&loc, 0, sizeof (loc)); > const char *locale = __loadlocale (&loc, LC_CTYPE, new_locale); > if (!locale) > locale = "C"; > @@ -2565,8 +2565,8 @@ get_langinfo (char *locale_out, char *charset_out) > return 0; > > /* Set results */ > - strcpy(locale_out, new_locale); > - strcpy(charset_out, charset); > + strcpy (locale_out, new_locale); > + strcpy (charset_out, charset); > return lcid; > } > > @@ -2670,7 +2670,7 @@ fhandler_pty_slave::fixup_after_attach (bool > native_maybe, int fd_set) > get_ttyp ()->pcon_pid = myself->pid; > get_ttyp ()->switch_to_pcon_out = true; > } > - init_console_handler(false); > + init_console_handler (false); > } > else if (fd == 0 && native_maybe) > /* Read from unattached pseudo console cause freeze, > @@ -2754,7 +2754,7 @@ fhandler_pty_slave::fixup_after_exec () > { \ > void *api = hook_api (module, #name, (void *) name##_Hooked); \ > name##_Orig = (__typeof__ (name) *) api; \ > - /*if (api) system_printf(#name " hooked.");*/ \ > + /*if (api) system_printf (#name " hooked.");*/ \ > } > DO_HOOK (NULL, WriteFile); > DO_HOOK (NULL, WriteConsoleA); > @@ -3118,7 +3118,7 @@ fhandler_pty_master::setup_pseudoconsole () > if (res != S_OK) > { > system_printf ("CreatePseudoConsole() failed. %08x\n", > - GetLastError()); > + GetLastError ()); > CloseHandle (from_master); > CloseHandle (to_slave); > from_master = from_master_cyg; > @@ -3230,7 +3230,7 @@ fhandler_pty_master::setup () > termios_printf ("can't set output_handle(%p) to non-blocking mode", > get_output_handle ()); > > - char pipename[sizeof("pty-to-master-cyg")]; > + char pipename[sizeof ("pty-to-master-cyg")]; > __small_sprintf (pipename, "pty%d-to-master", unit); > res = fhandler_pipe::create (&sec_none, &from_slave, &to_master, > fhandler_pty_common::pipesize, pipename, 0); > @@ -3406,7 +3406,7 @@ fhandler_pty_common::process_opost_output (HANDLE h, > const void *ptr, ssize_t& l > break; > else > { > - set_errno(EAGAIN); > + set_errno (EAGAIN); > len = -1; > return TRUE; > } Pushed. Thanks. Ken
Re: [PATCH] Cygwin: pty: Use autoload feature for pseudo console system calls.
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.
[PATCH] winsup/cygwin/times.cc (times): follow Linux and allow for a NULL buf argument
Adresses a problem reported on the Cygwin list. --- winsup/cygwin/times.cc | 17 +++-- 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/winsup/cygwin/times.cc b/winsup/cygwin/times.cc index 8908d44f1..909cae1f1 100644 --- a/winsup/cygwin/times.cc +++ b/winsup/cygwin/times.cc @@ -72,12 +72,17 @@ times (struct tms *buf) /* ticks is in in 100ns, convert to clock ticks. */ tc = (clock_t) (ticks.QuadPart * CLOCKS_PER_SEC / NS100PERSEC); - buf->tms_stime = __to_clock_t (&kut.KernelTime, 0); - buf->tms_utime = __to_clock_t (&kut.UserTime, 0); - timeval_to_filetime (&myself->rusage_children.ru_stime, &kut.KernelTime); - buf->tms_cstime = __to_clock_t (&kut.KernelTime, 1); - timeval_to_filetime (&myself->rusage_children.ru_utime, &kut.UserTime); - buf->tms_cutime = __to_clock_t (&kut.UserTime, 1); + /* Linux allows a NULL buf and just returns tc in that case, so +mimic that */ + if (buf) + { + buf->tms_stime = __to_clock_t (&kut.KernelTime, 0); + buf->tms_utime = __to_clock_t (&kut.UserTime, 0); + timeval_to_filetime (&myself->rusage_children.ru_stime, &kut.KernelTime); + buf->tms_cstime = __to_clock_t (&kut.KernelTime, 1); + timeval_to_filetime (&myself->rusage_children.ru_utime, &kut.UserTime); + buf->tms_cutime = __to_clock_t (&kut.UserTime, 1); + } } __except (EFAULT) { -- 2.23.0 Achim. -- +<[Q+ Matrix-12 WAVE#46+305 Neuron microQkb Andromeda XTk Blofeld]>+ SD adaptation for Waldorf rackAttack V1.04R1: http://Synth.Stromeko.net/Downloads.html#WaldorfSDada
Re: [PATCH] winsup/cygwin/times.cc (times): follow Linux and allow for a NULL buf argument
On 9/15/2019 12:28 PM, Achim Gratz wrote: > > Adresses a problem reported on the Cygwin list. > > --- > winsup/cygwin/times.cc | 17 +++-- > 1 file changed, 11 insertions(+), 6 deletions(-) > > diff --git a/winsup/cygwin/times.cc b/winsup/cygwin/times.cc > index 8908d44f1..909cae1f1 100644 > --- a/winsup/cygwin/times.cc > +++ b/winsup/cygwin/times.cc > @@ -72,12 +72,17 @@ times (struct tms *buf) > /* ticks is in in 100ns, convert to clock ticks. */ > tc = (clock_t) (ticks.QuadPart * CLOCKS_PER_SEC / NS100PERSEC); > > - buf->tms_stime = __to_clock_t (&kut.KernelTime, 0); > - buf->tms_utime = __to_clock_t (&kut.UserTime, 0); > - timeval_to_filetime (&myself->rusage_children.ru_stime, > &kut.KernelTime); > - buf->tms_cstime = __to_clock_t (&kut.KernelTime, 1); > - timeval_to_filetime (&myself->rusage_children.ru_utime, &kut.UserTime); > - buf->tms_cutime = __to_clock_t (&kut.UserTime, 1); > + /* Linux allows a NULL buf and just returns tc in that case, so > + mimic that */ > + if (buf) > + { > + buf->tms_stime = __to_clock_t (&kut.KernelTime, 0); > + buf->tms_utime = __to_clock_t (&kut.UserTime, 0); > + timeval_to_filetime (&myself->rusage_children.ru_stime, > &kut.KernelTime); > + buf->tms_cstime = __to_clock_t (&kut.KernelTime, 1); > + timeval_to_filetime (&myself->rusage_children.ru_utime, > &kut.UserTime); > + buf->tms_cutime = __to_clock_t (&kut.UserTime, 1); > + } > } > __except (EFAULT) > { Pushed, with a slight tweak to the commit message. Thanks. Ken