[PATCH] Cygwin: pty: Not to change code page of parent console.

2022-05-08 Thread Takashi Yano
- The recent commit "Cygwin: pty: Fix timing of creating invisible
  console." breaks the feature added by commit 72770148, which
  prevents pty from changing code page of parent console. This patch
  restores that.
---
 winsup/cygwin/fhandler_console.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/winsup/cygwin/fhandler_console.cc 
b/winsup/cygwin/fhandler_console.cc
index 5625b7be2..933614228 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -1610,7 +1610,7 @@ fhandler_console::close ()
   if (con_ra.rabuf)
 free (con_ra.rabuf);
 
-  if (!have_execed)
+  if (!have_execed && !invisible_console)
 free_console ();
   return 0;
 }
-- 
2.36.0



[PATCH] Cygwin: pty: Add missing attach_mutex guard.

2022-05-08 Thread Takashi Yano
---
 winsup/cygwin/fhandler_tty.cc | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index f6a7a6cf9..bb18d139e 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -3422,6 +3422,7 @@ skip_create:
   HeapFree (GetProcessHeap (), 0, hp);
 }
 
+  acquire_attach_mutex (mutex_timeout);
   if (get_ttyp ()->previous_code_page)
 SetConsoleCP (get_ttyp ()->previous_code_page);
   if (get_ttyp ()->previous_output_code_page)
@@ -3452,6 +3453,7 @@ skip_create:
mode |= DISABLE_NEWLINE_AUTO_RETURN;
   SetConsoleMode (hpConOut, mode);
 }
+  release_attach_mutex ();
 
   return true;
 
-- 
2.36.0



[PATCH] Cygwin: pty: Avoid deadlock when pcon is started on console.

2022-05-08 Thread Takashi Yano
- Previously, "env SHELL=cmd script" command in console caused
  deadlock when starting cmd.exe. This patch fixes the issue.
---
 winsup/cygwin/fhandler_tty.cc | 34 +-
 winsup/cygwin/select.cc   |  2 --
 2 files changed, 17 insertions(+), 19 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 9ab681d6c..f6a7a6cf9 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -1171,11 +1171,7 @@ fhandler_pty_slave::reset_switch_to_nat_pipe (void)
  bool need_restore_handles = get_ttyp ()->pcon_activated;
  WaitForSingleObject (pipe_sw_mutex, INFINITE);
  if (get_ttyp ()->pcon_activated)
-   {
- acquire_attach_mutex (mutex_timeout);
- close_pseudoconsole (get_ttyp ());
- release_attach_mutex ();
-   }
+   close_pseudoconsole (get_ttyp ());
  else
hand_over_only (get_ttyp ());
  ReleaseMutex (pipe_sw_mutex);
@@ -3244,9 +3240,11 @@ fhandler_pty_slave::setup_pseudoconsole ()
   GetCurrentProcess (), &hpConOut,
   0, TRUE, DUPLICATE_SAME_ACCESS);
   CloseHandle (pcon_owner);
+  acquire_attach_mutex (mutex_timeout);
   FreeConsole ();
   AttachConsole (get_ttyp ()->nat_pipe_owner_pid);
   init_console_handler (false);
+  release_attach_mutex ();
   goto skip_create;
 }
 
@@ -3368,9 +3366,11 @@ fhandler_pty_slave::setup_pseudoconsole ()
   HeapFree (GetProcessHeap (), 0, si.lpAttributeList);
 
   /* Attach to pseudo console */
+  acquire_attach_mutex (mutex_timeout);
   FreeConsole ();
   AttachConsole (pi.dwProcessId);
   init_console_handler (false);
+  release_attach_mutex ();
 
   /* Terminate helper process */
   SetEvent (goodbye);
@@ -3535,8 +3535,10 @@ void
 fhandler_pty_slave::close_pseudoconsole (tty *ttyp, DWORD force_switch_to)
 {
   DWORD switch_to = get_winpid_to_hand_over (ttyp, force_switch_to);
+  acquire_attach_mutex (mutex_timeout);
   ttyp->previous_code_page = GetConsoleCP ();
   ttyp->previous_output_code_page = GetConsoleOutputCP ();
+  release_attach_mutex ();
   if (nat_pipe_owner_self (ttyp->nat_pipe_owner_pid))
 { /* I am owner of the nat pipe. */
   if (switch_to)
@@ -3578,19 +3580,23 @@ fhandler_pty_slave::close_pseudoconsole (tty *ttyp, 
DWORD force_switch_to)
  ttyp->h_pcon_conhost_process = new_conhost_process;
  ttyp->h_pcon_in = new_pcon_in;
  ttyp->h_pcon_out = new_pcon_out;
+ acquire_attach_mutex (mutex_timeout);
  FreeConsole ();
  pinfo p (myself->ppid);
  if (!p || !AttachConsole (p->dwProcessId))
AttachConsole (ATTACH_PARENT_PROCESS);
  init_console_handler (false);
+ release_attach_mutex ();
}
   else
{ /* Close pseudo console and abandon the ownership of the nat pipe. */
+ acquire_attach_mutex (mutex_timeout);
  FreeConsole ();
  pinfo p (myself->ppid);
  if (!p || !AttachConsole (p->dwProcessId))
AttachConsole (ATTACH_PARENT_PROCESS);
  init_console_handler (false);
+ release_attach_mutex ();
  /* Reconstruct pseudo console handler container here for close */
  HPCON_INTERNAL *hp =
(HPCON_INTERNAL *) HeapAlloc (GetProcessHeap (), 0,
@@ -3610,11 +3616,13 @@ fhandler_pty_slave::close_pseudoconsole (tty *ttyp, 
DWORD force_switch_to)
 }
   else
 { /* Just detach from the pseudo console if I am not owner. */
+  acquire_attach_mutex (mutex_timeout);
   FreeConsole ();
   pinfo p (myself->ppid);
   if (!p || !AttachConsole (p->dwProcessId))
AttachConsole (ATTACH_PARENT_PROCESS);
   init_console_handler (false);
+  release_attach_mutex ();
 }
 }
 
@@ -4040,11 +4048,7 @@ fhandler_pty_slave::setup_for_non_cygwin_app (bool 
nopcon, PWCHAR envblock,
 }
   bool pcon_enabled = false;
   if (!nopcon)
-{
-  acquire_attach_mutex (mutex_timeout);
-  pcon_enabled = setup_pseudoconsole ();
-  release_attach_mutex ();
-}
+pcon_enabled = setup_pseudoconsole ();
   ReleaseMutex (pipe_sw_mutex);
   /* For pcon enabled case, transfer_input() is called in master::write() */
   if (!pcon_enabled && get_ttyp ()->getpgid () == myself->pgid
@@ -4077,11 +4081,7 @@ fhandler_pty_slave::cleanup_for_non_cygwin_app 
(handle_set_t *p, tty *ttyp,
 }
   WaitForSingleObject (p->pipe_sw_mutex, INFINITE);
   if (ttyp->pcon_activated)
-{
-  acquire_attach_mutex (mutex_timeout);
-  close_pseudoconsole (ttyp, force_switch_to);
-  release_attach_mutex ();
-}
+close_pseudoconsole (ttyp, force_switch_to);
   else
 hand_over_only (ttyp, force_switch_to);
   ReleaseMutex (p->pipe_sw_mutex);
@@ -4111,6 +4111,7 @@ fhandler_pty_slave::setpgid_aux (pid_t pid)
   

[PATCH] Cygwin: pty: Change the condition to send Ctrl-C event.

2022-05-08 Thread Takashi Yano
- Previously, non-cygwin app started by "script -c "
  receives Ctrl-C twice. This patch fixes the issue.
---
 winsup/cygwin/fhandler_termios.cc | 9 +++--
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/winsup/cygwin/fhandler_termios.cc 
b/winsup/cygwin/fhandler_termios.cc
index 4eff5eab3..735423bf2 100644
--- a/winsup/cygwin/fhandler_termios.cc
+++ b/winsup/cygwin/fhandler_termios.cc
@@ -324,9 +324,6 @@ fhandler_termios::process_sigs (char c, tty* ttyp, 
fhandler_termios *fh)
   pid_t pgid = ttyp->pgid;
 
   /* The name *_nat stands for 'native' which means non-cygwin apps. */
-  pinfo leader (pgid);
-  bool cyg_leader = /* The process leader is a cygwin process. */
-leader && !(leader->process_state & PID_NOTCYGWIN);
   bool ctrl_c_event_sent = false;
   bool need_discard_input = false;
   bool pg_with_nat = false; /* The process group has non-cygwin processes. */
@@ -373,9 +370,9 @@ fhandler_termios::process_sigs (char c, tty* ttyp, 
fhandler_termios *fh)
 instead. */
  if (p->process_state & PID_NEW_PG)
GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, p->dwProcessId);
- else if ((!fh || fh->need_send_ctrl_c_event () || cyg_leader)
-  && !ctrl_c_event_sent) /* cyg_leader is needed by GDB
-with non-cygwin inferior */
+ else if ((!fh || fh->need_send_ctrl_c_event ()
+   || p->exec_dwProcessId == p->dwProcessId)
+  && !ctrl_c_event_sent)
{
  GenerateConsoleCtrlEvent (CTRL_C_EVENT, 0);
  ctrl_c_event_sent = true;
-- 
2.36.0



[PATCH] Cygwin: pty: Fix timing of creating invisible console.

2022-05-08 Thread Takashi Yano
- Previously, invisible console was created in fixup_after_exec().
  However, actually this should be done in fixup_after_fork(). this
  patch fixes the issue.
---
 winsup/cygwin/fhandler_tty.cc | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index bdde1dce6..9ab681d6c 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -2467,6 +2467,8 @@ fhandler_pty_slave::bg_check (int sig, bool dontsignal)
 void
 fhandler_pty_slave::fixup_after_fork (HANDLE parent)
 {
+  create_invisible_console ();
+
   // fork_fixup (parent, inuse, "inuse");
   // fhandler_pty_common::fixup_after_fork (parent);
   report_tty_counts (this, "inherited", "");
@@ -2475,8 +2477,6 @@ fhandler_pty_slave::fixup_after_fork (HANDLE parent)
 void
 fhandler_pty_slave::fixup_after_exec ()
 {
-  create_invisible_console ();
-
   if (!close_on_exec ())
 fixup_after_fork (NULL);   /* No parent handle required. */
 
-- 
2.36.0



[PATCH] Cygwin: pty: Fix acquiring attach_mutex timing.

2022-05-08 Thread Takashi Yano
- When temporarily attaching a console, the timing of acquiring
  attach_mutex was not appropriate. This sometimes caused master
  forwarding thread to crash on Ctrl-C in Windows 7. This patch
  fixes the issue.
---
 winsup/cygwin/fhandler_tty.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 484bf55dc..bdde1dce6 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -4171,13 +4171,13 @@ DWORD
 fhandler_pty_common::attach_console_temporarily (DWORD target_pid)
 {
   DWORD resume_pid = 0;
+  acquire_attach_mutex (mutex_timeout);
   pinfo pinfo_resume (myself->ppid);
   if (pinfo_resume)
 resume_pid = pinfo_resume->dwProcessId;
   if (!resume_pid)
 resume_pid = get_console_process_id (myself->dwProcessId, false);
   bool console_exists = fhandler_console::exists ();
-  acquire_attach_mutex (mutex_timeout);
   if (!console_exists || resume_pid)
 {
   FreeConsole ();
-- 
2.36.0



Re: load average calculation failing

2022-05-08 Thread Jon Turney

On 08/05/2022 08:01, Mark Geisert wrote:

Mark Geisert wrote (on the main Cygwin mailing list):
I've recently noticed that the 'xload' I routinely run shows zero load 
even with compute-bound processes running.  This is on both Cygwin 
pre-3.4.0 as well as 3.3.4.  A test program, shown below, indicates 
that getloadavg() is returning with 0 status, i.e. not an error but no 
elems

of the passed-in array updated.

Stepping with gdb through the test program seems weird within the 
loadavginfo::load_init method.  Single-stepping at line loadavg.cc:68 
goes to strace.h:52 and then to _sigbe ?!


I had recently updated both Cygwin and Windows 10 to latest at the 
same time so I cannot say when the failure started.  Last day or two 
at most.



[...]


I've debugged a bit further..  Within Cygwin's loadavg.cc:load_init(), 
the PdhOpenQueryW() call returns successfully.  The subsequent 
PdhAddEnglishCounterW() call is unsuccessful.  It returns status 
0x87D0 == PDH_CSTATUS_NO_MACHINE. The code (at line 68 mentioned 
above) calls debug_printf() to conditionally display the error, which is 
what leads to the strace.h and _sigbe; that's fine.


The weird PDH_CSTATUS_NO_MACHINE is the problem.  I'll try running the 
example from an elevated shell.  Or rebooting the machine.  After that 
it's consulting some oracle TBD. :-(




Thanks for looking into this.
You can find the user space version of this code I initially wrote at 
https://github.com/jon-turney/windows-loadavg, which might save you some 
time.


I can't reproduce this on W10 21H1, so I think this must be due to some 
change in later Windows...