Re: Command line processing in dcrt0.cc does not match Microsoft parsing rules

2019-09-04 Thread Brian Inglis
On 2019-09-04 17:46, Stephen Provine wrote:
> On 2019-09-04 10:20, Brian Inglis wrote:
>> and ask if you really expect anyone else to use or reproduce this insanity,
>> rather than a sane POSIX parser?
> 
> I know it's insanity, but it's insanity that almost all Windows programs 
> inherit and
> implement consistently enough because they use standard libraries or functions
> to do the parsing. The Go command line parser used to use CommandLineToArgvW
> and only switched away from it due to performance (it's in shell32.dll and 
> that takes
> a long time to load). I don't know how accurate their manual reproduction is, 
> but
> they seemed to study the sources I sent pretty carefully.
> 
> Anyway, my specific problem is that I have Go code with an array of arguments 
> that
> I want to pass verbatim (no glob expansion) to a bash script. I've figured 
> out how to
> override Go's default code for building the command line string, but it's not 
> clear how
> to correctly construct the command line string. If the POSIX rules are being 
> followed,
> I'd expect the following to work:
> 
> bash.exe script.sh arg1 "*" arg3
> 
> But it always expands the "*" to all the files in the current directory. I've 
> also tried \* and
> '*', but same problem. So how do I build a command line string that takes 
> each argument
> literally with no processing?

As standard on Unix systems, just add another level of quoting for each level of
interpretation, as bash will process that command line, then bash will process
the script command line.

How are you running the command line; I get the same results under cmd or
mintty/bash:

$ bash -nvx script.sh arg1 "*" arg3
#!/bin/bash
# script.sh - echo args

argc=$#
argv=("$0" "$@")
echo argc $argc argv[0] "${argv[0]}"

for ((a = 1; a <= $argc; ++a))
do
echo argv[$a] "${argv[$a]}"
done

C:\ > bash script.sh arg1 "*" arg3
argc 3 argv[0] script.sh
argv[1] arg1
argv[2] *
argv[3] arg3

C:\ > bash -c 'script.sh arg1 "*" arg3'
argc 3 argv[0] /mnt/c/Users/bwi/bin/script.sh
argv[1] arg1
argv[2] *
argv[3] arg3

$ bash script.sh arg1 "*" arg3
argc 3 argv[0] script.sh
argv[1] arg1
argv[2] *
argv[3] arg3

$ bash -c 'script.sh arg1 "*" arg3'
argc 3 argv[0] /home/bwi/bin/script.sh
argv[1] arg1
argv[2] *
argv[3] arg3

$ cmd /c bash script.sh arg1 "\*" arg3
argc 3 argv[0] script.sh
argv[1] arg1
argv[2] *
argv[3] arg3

$ cmd /c bash -c 'script.sh arg1 "*" arg3'
argc 3 argv[0] /mnt/c/Users/bwi/bin/script.sh
argv[1] arg1
argv[2] *
argv[3] arg3

but with un-double-quoted (and backslash escaped) * I get a list of the current
directory files from all of these commands.

Invoking bash with options -vx or set -vx in script.sh will let you see what is
happening on stderr. Many errors cause non-interactive shell scripts to exit, so
check for child process error return codes (often 128+errno). If you are not
careful within script.sh, many unquoted uses of $2 may expand the *. Double
quotes allow command and parameter substitution, and history expansion, but
suppress pathname expansion. You should refer to each parameter within script.sh
as "$1" "$2" "$3", or you might need to quote some or each argument character
and enclose the * in double quotes e.g. \""\*"\" to pass thru the Go command
line interface.
Can you not tell the interface to verbatim passthru the string for execution?

You may check any of the POSIX shell, dash/ash/sh shell, ksh Korn shell, or bash
shell man pages or docs for more details on variations between shells and
extensions to POSIX operation.

-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

This email may be disturbing to some readers as it contains
too much technical detail. Reader discretion is advised.

--
Problem reports:   http://cygwin.com/problems.html
FAQ:   http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple



[PATCH 1/1] Cygwin: pty: Fix select() with pseudo console support.

2019-09-04 Thread Takashi Yano
- select() did not work correctly when both read and except are
  polled simultaneously for the same fd and the r/w pipe is switched
  to pseudo console side. This patch fixes this isseu.
---
 winsup/cygwin/fhandler.h  |  15 +++
 winsup/cygwin/fhandler_tty.cc |  13 ++-
 winsup/cygwin/select.cc   | 192 --
 winsup/cygwin/select.h|   2 +
 4 files changed, 207 insertions(+), 15 deletions(-)

diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index e8c165100..e72e11f7a 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -2102,6 +2102,7 @@ class fhandler_pty_common: public fhandler_termios
   {
 return get_ttyp ()->hPseudoConsole;
   }
+  bool to_be_read_from_pcon (void);
 
  protected:
   BOOL process_opost_output (HANDLE h,
@@ -2150,6 +2151,8 @@ class fhandler_pty_slave: public fhandler_pty_common
   void fixup_after_exec ();
 
   select_record *select_read (select_stuff *);
+  select_record *select_write (select_stuff *);
+  select_record *select_except (select_stuff *);
   virtual char const *ttyname () { return pc.dev.name (); }
   int __reg2 fstat (struct stat *buf);
   int __reg3 facl (int, int, struct acl *);
@@ -2177,9 +2180,21 @@ class fhandler_pty_slave: public fhandler_pty_common
   void push_to_pcon_screenbuffer (const char *ptr, size_t len);
   void mask_switch_to_pcon (bool mask)
   {
+if (!mask && get_ttyp ()->pcon_pid &&
+   get_ttyp ()->pcon_pid != myself->pid &&
+   kill (get_ttyp ()->pcon_pid, 0) == 0)
+  return;
 get_ttyp ()->mask_switch_to_pcon = mask;
   }
   void fixup_after_attach (bool native_maybe);
+  pid_t get_pcon_pid (void)
+  {
+return get_ttyp ()->pcon_pid;
+  }
+  bool is_line_input (void)
+  {
+return get_ttyp ()->ti.c_lflag & ICANON;
+  }
 };
 
 #define __ptsname(buf, unit) __small_sprintf ((buf), "/dev/pty%d", (unit))
diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index a6844832b..78c9c9128 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -1223,6 +1223,13 @@ fhandler_pty_slave::write (const void *ptr, size_t len)
   return towrite;
 }
 
+bool
+fhandler_pty_common::to_be_read_from_pcon (void)
+{
+  return get_ttyp ()->switch_to_pcon &&
+(!get_ttyp ()->mask_switch_to_pcon || ALWAYS_USE_PCON);
+}
+
 void __reg3
 fhandler_pty_slave::read (void *ptr, size_t& len)
 {
@@ -1351,8 +1358,7 @@ fhandler_pty_slave::read (void *ptr, size_t& len)
}
  goto out;
}
-  if (get_ttyp ()->switch_to_pcon &&
- (!get_ttyp ()->mask_switch_to_pcon || ALWAYS_USE_PCON))
+  if (to_be_read_from_pcon ())
{
  if (!try_reattach_pcon ())
{
@@ -2129,8 +2135,7 @@ fhandler_pty_master::write (const void *ptr, size_t len)
 
   /* Write terminal input to to_slave pipe instead of output_handle
  if current application is native console application. */
-  if (get_ttyp ()->switch_to_pcon &&
-  (!get_ttyp ()->mask_switch_to_pcon || ALWAYS_USE_PCON))
+  if (to_be_read_from_pcon ())
 {
   char *buf;
   size_t nlen;
diff --git a/winsup/cygwin/select.cc b/winsup/cygwin/select.cc
index d29f3d2f4..4efc302df 100644
--- a/winsup/cygwin/select.cc
+++ b/winsup/cygwin/select.cc
@@ -667,9 +667,6 @@ peek_pipe (select_record *s, bool from_select)
fhm->flush_to_slave ();
  }
  break;
-   case DEV_PTYS_MAJOR:
- ((fhandler_pty_slave *) fh)->reset_switch_to_pcon ();
- break;
default:
  if (fh->get_readahead_valid ())
{
@@ -713,6 +710,7 @@ peek_pipe (select_record *s, bool from_select)
 }
 
 out:
+  h = fh->get_output_handle_cyg ();
   if (s->write_selected && dev != FH_PIPER)
 {
   gotone += s->write_ready =  pipe_data_available (s->fd, fh, h, true);
@@ -1176,33 +1174,173 @@ static int
 verify_tty_slave (select_record *me, fd_set *readfds, fd_set *writefds,
   fd_set *exceptfds)
 {
-  if (IsEventSignalled (me->h))
+  fhandler_pty_slave *ptys = (fhandler_pty_slave *) me->fh;
+  if (me->read_selected && !ptys->to_be_read_from_pcon () &&
+  IsEventSignalled (ptys->input_available_event))
 me->read_ready = true;
   return set_bits (me, readfds, writefds, exceptfds);
 }
 
 static int
-pty_slave_startup (select_record *s, select_stuff *)
+peek_pty_slave (select_record *s, bool from_select)
 {
+  int gotone = 0;
   fhandler_base *fh = (fhandler_base *) s->fh;
-  ((fhandler_pty_slave *) fh)->mask_switch_to_pcon (true);
+  fhandler_pty_slave *ptys = (fhandler_pty_slave *) fh;
+
+  ptys->reset_switch_to_pcon ();
+
+  if (s->read_selected)
+{
+  if (s->read_ready)
+   {
+ select_printf ("%s, already ready for read", fh->get_name ());
+ gotone = 1;
+ goto out;
+   }
+
+  if (fh->bg_check (SIGTTIN, true) <= bg_eof)
+   {
+ gotone = s->read_ready = true;
+ goto out;
+   }
+
+  if (ptys->to_be_read_from_pcon ())

[PATCH 0/1] Cygwin: pty: Fix select() with pseudo console support.

2019-09-04 Thread Takashi Yano
- select() did not work correctly when both read and except are
  polled simultaneously for the same fd and the r/w pipe is switched
  to pseudo console side. This patch fixes this isseu.

Takashi Yano (1):
  Cygwin: pty: Fix select() with pseudo console support.

 winsup/cygwin/fhandler.h  |  15 +++
 winsup/cygwin/fhandler_tty.cc |  13 ++-
 winsup/cygwin/select.cc   | 192 --
 winsup/cygwin/select.h|   2 +
 4 files changed, 207 insertions(+), 15 deletions(-)

-- 
2.21.0



Re: createevent access denied

2019-09-04 Thread Brian Inglis
On 2019-09-04 19:29, Cary Lewis wrote:
> I am trying to write to the windows events sub system using createevent,
> but I am getting access denied.
> 
> Is there a cygwin utility that provides a way to create a windows event?

Cygwin package util-linux contains logger which should create Windows events
with Unix fields.

Unless you have the Cygwin syslog or syslog-ng daemon installed and running as a
service, in which case it will write to /dev/log socket, which will be logged to
/var/log/messages, or whatever file or processing is configured.

Search the mail archive for previous topics including syslog in the subject.

-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

This email may be disturbing to some readers as it contains
too much technical detail. Reader discretion is advised.

--
Problem reports:   http://cygwin.com/problems.html
FAQ:   http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple



Re: [PATCH 2/2] Cygwin: pty: Disable clear screen on new pty if TERM=dumb or emacs*.

2019-09-04 Thread Brian Inglis
On 2019-09-04 19:13, Takashi Yano wrote:
> On Wed, 4 Sep 2019 11:22:42 -0600
> Brian Inglis wrote:
>> That output seems to be generated by a shell or program running in the pty.
>> If the recipient can not handle escape sequences, then the shell or program 
>> in
>> the pty should be configured by setting e.g. TERM=dumb when launching the
>> terminal, or in the shell environment.
> 
> No. Output is almost same even if setting TERM=dumb.
> See attached log.
> 
> Consider what should be the output of pseudo console if console
> application like below is executed.
> 
> #include 
> 
> int main()
> {
> HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
> COORD p = {20, 10};
> DWORD n;
> SetConsoleCursorPosition(h, p);
> SetConsoleTextAttribute(h, FOREGROUND_RED);
> WriteConsole(h, "R", 1, , 0);
> SetConsoleTextAttribute(h, FOREGROUND_GREEN);
> WriteConsole(h, "G", 1, , 0);
> SetConsoleTextAttribute(h, FOREGROUND_BLUE);
> WriteConsole(h, "B", 1, , 0);
> SetConsoleTextAttribute(h, 
> FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
> WriteConsole(h, "W", 1, , 0);
> WriteConsole(h, "\r\n", 2, , 0);
> return 0;
> }
> 
> Setting cursor posision and text color cannot be realized
> without ANSI escape sequences.
> 
> Indeed, the output of the pseudo console by the program above is:
> ^[[?25l^[[11;21H^[[?25h^[[?25l^[[31mR^[[32mG^[[34mB^[[mW^M
> ^[[?25h

So how do you tell the pseudo-console to generate only text not escape sequences
the recipient may not be prepared to deal with?

-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

This email may be disturbing to some readers as it contains
too much technical detail. Reader discretion is advised.


createevent access denied

2019-09-04 Thread Cary Lewis
I am trying to write to the windows events sub system using createevent,
but I am getting access denied.

Is there a cygwin utility that provides a way to create a windows event?

--
Problem reports:   http://cygwin.com/problems.html
FAQ:   http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple



Re: [PATCH 2/2] Cygwin: pty: Disable clear screen on new pty if TERM=dumb or emacs*.

2019-09-04 Thread Takashi Yano
On Wed, 4 Sep 2019 11:22:42 -0600
Brian Inglis wrote:
> That output seems to be generated by a shell or program running in the pty.
> If the recipient can not handle escape sequences, then the shell or program in
> the pty should be configured by setting e.g. TERM=dumb when launching the
> terminal, or in the shell environment.

No. Output is almost same even if setting TERM=dumb.
See attached log.

Consider what should be the output of pseudo console if console
application like below is executed.

#include 

int main()
{
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
COORD p = {20, 10};
DWORD n;
SetConsoleCursorPosition(h, p);
SetConsoleTextAttribute(h, FOREGROUND_RED);
WriteConsole(h, "R", 1, , 0);
SetConsoleTextAttribute(h, FOREGROUND_GREEN);
WriteConsole(h, "G", 1, , 0);
SetConsoleTextAttribute(h, FOREGROUND_BLUE);
WriteConsole(h, "B", 1, , 0);
SetConsoleTextAttribute(h, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
WriteConsole(h, "W", 1, , 0);
WriteConsole(h, "\r\n", 2, , 0);
return 0;
}

Setting cursor posision and text color cannot be realized
without ANSI escape sequences.

Indeed, the output of the pseudo console by the program above is:
^[[?25l^[[11;21H^[[?25h^[[?25l^[[31mR^[[32mG^[[34mB^[[mW^M
^[[?25h

-- 
Takashi Yano 


pcon-output-dumb.log
Description: Binary data


[PATCH v2 1/1] Cygwin: pty: Disable clear screen on new pty if TERM=dumb or emacs*.

2019-09-04 Thread Takashi Yano
- Pseudo console support introduced by commit
  169d65a5774acc76ce3f3feeedcbae7405aa9b57 shows garbage ^[[H^[[J in
  some of emacs screens. These screens do not handle ANSI escape
  sequences. Therefore, clear screen is disabled on these screens.
---
 winsup/cygwin/fhandler_tty.cc | 19 ++-
 winsup/cygwin/tty.cc  |  1 +
 winsup/cygwin/tty.h   |  1 +
 3 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index fadff59a3..a6844832b 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -962,6 +962,19 @@ skip_console_setting:
 void
 fhandler_pty_slave::reset_switch_to_pcon (void)
 {
+  if (get_ttyp ()->need_clear_screen)
+{
+  const char *term = getenv ("TERM");
+  if (term && strcmp (term, "dumb") && !strstr (term, "emacs"))
+   {
+ /* FIXME: Clearing sequence may not be "^[[H^[[J"
+depending on the terminal type. */
+ DWORD n;
+ WriteFile (get_output_handle_cyg (), "\033[H\033[J", 6, , NULL);
+   }
+  get_ttyp ()->need_clear_screen = false;
+}
+
   if (ALWAYS_USE_PCON)
 return;
   if (isHybrid)
@@ -2798,14 +2811,10 @@ fhandler_pty_slave::fixup_after_attach (bool 
native_maybe)
  /* Clear screen to synchronize pseudo console screen buffer
 with real terminal. This is necessary because pseudo
 console screen buffer is empty at start. */
- /* FIXME: Clearing sequence may not be "^[[H^[[J"
-depending on the terminal type. */
- DWORD n;
  if (get_ttyp ()->num_pcon_attached_slaves == 0
  && !ALWAYS_USE_PCON)
/* Assume this is the first process using this pty slave. */
-   WriteFile (get_output_handle_cyg (),
-  "\033[H\033[J", 6, , NULL);
+   get_ttyp ()->need_clear_screen = true;
 
  get_ttyp ()->num_pcon_attached_slaves ++;
}
diff --git a/winsup/cygwin/tty.cc b/winsup/cygwin/tty.cc
index 9244267c0..c94aee3ba 100644
--- a/winsup/cygwin/tty.cc
+++ b/winsup/cygwin/tty.cc
@@ -243,6 +243,7 @@ tty::init ()
   pcon_pid = 0;
   num_pcon_attached_slaves = 0;
   TermCodePage = 20127; /* ASCII */
+  need_clear_screen = false;
 }
 
 HANDLE
diff --git a/winsup/cygwin/tty.h b/winsup/cygwin/tty.h
index d59b2027d..c2b0490d0 100644
--- a/winsup/cygwin/tty.h
+++ b/winsup/cygwin/tty.h
@@ -104,6 +104,7 @@ private:
   pid_t pcon_pid;
   int num_pcon_attached_slaves;
   UINT TermCodePage;
+  bool need_clear_screen;
 
 public:
   HANDLE from_master () const { return _from_master; }
-- 
2.21.0



[PATCH v2 0/1] Disable clear screen on new pty if TERM=dumb or emacs*.

2019-09-04 Thread Takashi Yano
- Pseudo console support introduced by commit
  169d65a5774acc76ce3f3feeedcbae7405aa9b57 shows garbage ^[[H^[[J in
  some of emacs screens. These screens do not handle ANSI escape
  sequences. Therefore, clear screen is disabled on these screens.

v2:
Remove check for TERM in fixup_after_attach().

Takashi Yano (1):
  Cygwin: pty: Disable clear screen on new pty if TERM=dumb or emacs*.

 winsup/cygwin/fhandler_tty.cc | 19 ++-
 winsup/cygwin/tty.cc  |  1 +
 winsup/cygwin/tty.h   |  1 +
 3 files changed, 16 insertions(+), 5 deletions(-)

-- 
2.21.0



RE: Command line processing in dcrt0.cc does not match Microsoft parsing rules

2019-09-04 Thread Stephen Provine via cygwin
On 2019-09-04 10:20, Brian Inglis wrote:
> and ask if you really expect anyone else to use or reproduce this insanity,
> rather than a sane POSIX parser?

I know it's insanity, but it's insanity that almost all Windows programs 
inherit and
implement consistently enough because they use standard libraries or functions
to do the parsing. The Go command line parser used to use CommandLineToArgvW
and only switched away from it due to performance (it's in shell32.dll and that 
takes
a long time to load). I don't know how accurate their manual reproduction is, 
but
they seemed to study the sources I sent pretty carefully.

Anyway, my specific problem is that I have Go code with an array of arguments 
that
I want to pass verbatim (no glob expansion) to a bash script. I've figured out 
how to
override Go's default code for building the command line string, but it's not 
clear how
to correctly construct the command line string. If the POSIX rules are being 
followed,
I'd expect the following to work:

bash.exe script.sh arg1 "*" arg3

But it always expands the "*" to all the files in the current directory. I've 
also tried \* and
'*', but same problem. So how do I build a command line string that takes each 
argument
literally with no processing?

Thanks,
Stephen


--
Problem reports:   http://cygwin.com/problems.html
FAQ:   http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple



Re: WARNING: Couldn't compute FAST_CWD pointer...

2019-09-04 Thread cygwinautoreply
>  1 [main] bash 11276 find_fast_cwd: WARNING: Couldn't compute FAST_CWD
>pointer.  Please report this problem to the public mailing list
>cygwin@cygwin.com

>Wouldn't you need additional information?

>The OS was upgraded from Win 7 to 10 recently... should I just reinstall
>Cygwin?


https://cygwin.com/faq.html#faq.using.fixing-find_fast_cwd-warnings

--
Problem reports:   http://cygwin.com/problems.html
FAQ:   http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple



WARNING: Couldn't compute FAST_CWD pointer...

2019-09-04 Thread Jonathan Beit-Aharon
  1 [main] bash 11276 find_fast_cwd: WARNING: Couldn't compute FAST_CWD
pointer.  Please report this problem to the public mailing list
cygwin@cygwin.com

Wouldn't you need additional information?

The OS was upgraded from Win 7 to 10 recently... should I just reinstall
Cygwin?

--
Problem reports:   http://cygwin.com/problems.html
FAQ:   http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple



Re: [PATCH 2/2] Cygwin: pty: Disable clear screen on new pty if TERM=dumb or emacs*.

2019-09-04 Thread Brian Inglis
On 2019-09-03 21:45, Takashi Yano wrote:
> On Wed, 4 Sep 2019 12:34:31 +0900
> Takashi Yano wrote:
>> Attached is the raw output from pseudo console when the screen shows
>> the simple text below.
>>
>>  from here 
>> [yano@Express5800-S70 ~]$ cmd
>> Microsoft Windows [Version 10.0.18362.329]
>> (c) 2019 Microsoft Corporation. All rights reserved.
>>
>> C:\cygwin\home\yano>exit
>> [yano@Express5800-S70 ~]$ exit
>> exit
>>  to here 
>>
>> You will noticed that the screen is cleared if you execute
>> cat pcon-output.log
>> in a terminal which support ANSI escape sequences.
> 
> This pcon-output.log was recorded in screen of 80x24 size.
> Please try above in 80x24 terminal.

That output seems to be generated by a shell or program running in the pty.
If the recipient can not handle escape sequences, then the shell or program in
the pty should be configured by setting e.g. TERM=dumb when launching the
terminal, or in the shell environment.

-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

This email may be disturbing to some readers as it contains
too much technical detail. Reader discretion is advised.


Re: Cygwin glade v3.20.3 crashing with a segment fault.

2019-09-04 Thread Brian Inglis
On 2019-09-03 05:00, tvonderhaar wrote:
> Has anyone gotten Cygwin glade v3.20.3 to run without it crashing with a 
> segment
> fault. I got the latest updates to make sure it had not been fixed. No luck. 
> The
> strace is very long. I had attached a 7z compressed version of it but my 
> E-Mail
> was reject as too long at greater that 1MB even though it is actually 940kb. 
> It
> is hard to believe that the overhead of this message is over 60kb.  The strace
> indicates to me that the program wrote outside of it writeable memory space.

Compressed binaries have to be encoded in attachments which expands the contents
by about 1/3.
Copy the strace output and delete all the standard startup before your program
gets going, then copy that and delete what you can that is irrelevant before the
segv, then check the size is less than 1MB, and attach.
If still too big, can you post it in a Github gist, on a Google drive, or other
external site you have access to?

-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

This email may be disturbing to some readers as it contains
too much technical detail. Reader discretion is advised.

--
Problem reports:   http://cygwin.com/problems.html
FAQ:   http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple



Re: netinet/* in Cygwin

2019-09-04 Thread Brian Inglis
On 2019-09-04 00:36, Lukasz Swierczewski wrote:
> I have question.
> I need netinet/* in my C project.
> For example:
> #include 
> #include 
> #include 
> #include 
> Is this available in Cygwin?

$ cygcheck -p usr/include/netinet/
Found 6 matches for usr/include/netinet/
cygwin-devel-3.0.6-1 - cygwin-devel: Core development files
cygwin-devel-3.0.7-1 - cygwin-devel: Core development files
cygwin-devel-3.1.0-0.3 - cygwin-devel: Core development files
cygwin32-2.10.0-1 - cygwin32: Cygwin 32bit toolchain
cygwin32-2.6.0-1 - cygwin32: Cygwin 32bit toolchain (installed binaries and
support files)
cygwin32-2.8.2-1 - cygwin32: Cygwin 32bit toolchain

$ zfgrep netinet /etc/setup/cygwin-devel.lst.gz
usr/include/netinet/
usr/include/netinet/in.h
usr/include/netinet/in_systm.h
usr/include/netinet/ip.h
usr/include/netinet/ip6.h
usr/include/netinet/ip_icmp.h
usr/include/netinet/tcp.h
usr/include/netinet/udp.h

Install cygwin-devel package.

> There are problems as standard ...
> What do you need to do to compile with these libs?

In many cases, nothing, as the functions are built into libc in the cygwin dll;
otherwise for other libraries, add -l... to your build command line as you would
on Unix.

-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

This email may be disturbing to some readers as it contains
too much technical detail. Reader discretion is advised.

--
Problem reports:   http://cygwin.com/problems.html
FAQ:   http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple



Re: Command line processing in dcrt0.cc does not match Microsoft parsing rules

2019-09-04 Thread Brian Inglis
On 2019-09-03 10:38, Stephen Provine wrote:
> On 2019-08-30 21:58, Brian Inglis wrote:
>> Not being in the same Cygwin process group and lacking the appropriate 
>> interface
>> info indicates that the invoker was not Cygwin.
> 
> Should I interpret this to mean the "winshell" parameter is not an accurate
> statement of what I thought it was for and because there is no way to reliably
> determine if the calling process was from Cygwin or not, behavior like I 
> suggest
> is actually impossible?

Reread the rules in the article you quoted, carefully, then read:

http://www.windowsinspired.com/how-a-windows-programs-splits-its-command-line-into-individual-arguments/
[also see linked articles about cmd and batch file command line parsing]

and ask if you really expect anyone else to use or reproduce this insanity,
rather than a sane POSIX parser?
Once again MS "persists in reinventing the square wheel", badly [from Henry
Spencer's Commandments].

What does the Go command line parser actually accept, does it really invert the
parse_cmdline or CommandLineToArgvW rules, and which?

That winshell parameter is set in dcrt0.cc calling build_argv, based on whether
the parent process was Cygwin and an argv array is available preset by the
Cygwin parent, or not and globs are allowed to be expanded, such that the
command line args, quotes, and wildcards have to be handled by the program
according to POSIX shell command line quoting, field splitting, and pathname
expansion rules, respecting $IFS:

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html

The similar flag in spawn.cc based on the exe or interpreter exe being under a
Cygwin exec mount in realpath.iscygexec() decides whether the argv array can be
passed a la Unix to a Cygwin child, or a Windows command line needs to be built
with Windows argument double quoting and escaping where required.

-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

This email may be disturbing to some readers as it contains
too much technical detail. Reader discretion is advised.

--
Problem reports:   http://cygwin.com/problems.html
FAQ:   http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple



Re: [PATCH 2/2] Cygwin: pty: Disable clear screen on new pty if TERM=dumb or emacs*.

2019-09-04 Thread Takashi Yano
On Wed, 4 Sep 2019 17:19:05 +0200
Corinna Vinschen wrote:
> > But the first check (the one in fixup_after_attach()) could be dropped, 
> > couldn't it?
> 
> IIUC the second test first checks for need_clear_screen but then the
> TERM might have changed in the meantime which in turn requires to change
> the behaviour again.  But yeah, this sound like the first patch is not
> actually required at all.

I was convinced. I will revise the patch.

-- 
Takashi Yano 


Re: [PATCH 2/2] Cygwin: pty: Disable clear screen on new pty if TERM=dumb or emacs*.

2019-09-04 Thread Corinna Vinschen
On Sep  4 15:11, Ken Brown wrote:
> On 9/4/2019 10:42 AM, Takashi Yano wrote:
> > On Wed, 4 Sep 2019 15:55:03 +0200
> > Corinna Vinschen wrote:
> >> The code in fixup_after_attach() is the only code snippet setting
> >> need_clear_screen = true.  And that code also requires term != "dump" &&
> >> term == "*emacs*" to set need_clear_screen.
> > 
> > term != "*emacs*"
> > 
> >> The code in reset_switch_to_pcon() requires that the need_clear_screen
> >> flag is true regardless of checking TERM.  So this code depends on the
> >> successful TERM check from fixup_after_attach anyway.
> >>
> >> What am I missing?
> > 
> > Two checking results may not be the same. Indeed, emacs changes
> > TERM between two checks.
> > 
> > fixup_after_attach() is called from fixup_after_exec(),
> > which is called before executing the program code.
> > reset_switch_to_pcon () is mainly called from PTY slave I/O functions.
> > This is usually from the program code.
> 
> But the first check (the one in fixup_after_attach()) could be dropped, 
> couldn't it?

IIUC the second test first checks for need_clear_screen but then the
TERM might have changed in the meantime which in turn requires to change
the behaviour again.  But yeah, this sound like the first patch is not
actually required at all.


Corinna

-- 
Corinna Vinschen
Cygwin Maintainer


signature.asc
Description: PGP signature


Re: [PATCH 2/2] Cygwin: pty: Disable clear screen on new pty if TERM=dumb or emacs*.

2019-09-04 Thread Ken Brown
On 9/4/2019 10:42 AM, Takashi Yano wrote:
> On Wed, 4 Sep 2019 15:55:03 +0200
> Corinna Vinschen wrote:
>> The code in fixup_after_attach() is the only code snippet setting
>> need_clear_screen = true.  And that code also requires term != "dump" &&
>> term == "*emacs*" to set need_clear_screen.
> 
> term != "*emacs*"
> 
>> The code in reset_switch_to_pcon() requires that the need_clear_screen
>> flag is true regardless of checking TERM.  So this code depends on the
>> successful TERM check from fixup_after_attach anyway.
>>
>> What am I missing?
> 
> Two checking results may not be the same. Indeed, emacs changes
> TERM between two checks.
> 
> fixup_after_attach() is called from fixup_after_exec(),
> which is called before executing the program code.
> reset_switch_to_pcon () is mainly called from PTY slave I/O functions.
> This is usually from the program code.

But the first check (the one in fixup_after_attach()) could be dropped, 
couldn't it?

Ken


Re: [PATCH 2/2] Cygwin: pty: Disable clear screen on new pty if TERM=dumb or emacs*.

2019-09-04 Thread Takashi Yano
On Wed, 4 Sep 2019 15:55:03 +0200
Corinna Vinschen wrote:
> The code in fixup_after_attach() is the only code snippet setting
> need_clear_screen = true.  And that code also requires term != "dump" &&
> term == "*emacs*" to set need_clear_screen.

term != "*emacs*"

> The code in reset_switch_to_pcon() requires that the need_clear_screen
> flag is true regardless of checking TERM.  So this code depends on the
> successful TERM check from fixup_after_attach anyway.
> 
> What am I missing?

Two checking results may not be the same. Indeed, emacs changes
TERM between two checks.

fixup_after_attach() is called from fixup_after_exec(),
which is called before executing the program code.
reset_switch_to_pcon () is mainly called from PTY slave I/O functions.
This is usually from the program code.

The behaviour of the patch is as follows.

First check : True  True  False False
Second check: True  False True  False
Clear screen: Yes   NoNo   No

# True: neither dumb nor emacs*
#  False: either dumb or emacs*


> + if (get_ttyp ()->num_pcon_attached_slaves == 0 &&
> + term && strcmp (term, "dumb") &&
> +   term && !strstr (term, "emacs") &&
> + !ALWAYS_USE_PCON)
> 
> You're checking term for != NULL twice.

Oh my!

-- 
Takashi Yano 


Re: [PATCH v2 0/1] Cygwin: pty: Add a workaround for ^C handling.

2019-09-04 Thread Corinna Vinschen
On Sep  4 22:47, Takashi Yano wrote:
> - Pseudo console support introduced by commit
>   169d65a5774acc76ce3f3feeedcbae7405aa9b57 sometimes cause random
>   crash or freeze by pressing ^C while cygwin and non-cygwin
>   processes are executed simultaneously in the same pty. This
>   patch is a workaround for this issue.
> 
> v2:
> Make the behaviour of pty and console identical.
> 
> Takashi Yano (1):
>   Cygwin: pty: Add a workaround for ^C handling.
> 
>  winsup/cygwin/fork.cc  | 1 -
>  winsup/cygwin/spawn.cc | 6 ++
>  2 files changed, 6 insertions(+), 1 deletion(-)
> 
> -- 
> 2.21.0

Pushed.


Thanks,
Corinna

-- 
Corinna Vinschen
Cygwin Maintainer


signature.asc
Description: PGP signature


[newlib-cygwin] Cygwin: pty: Add a workaround for ^C handling.

2019-09-04 Thread Corinna Vinschen
https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=d4045fdbef60d8e7e0d11dfe38b048ea2cb8708b

commit d4045fdbef60d8e7e0d11dfe38b048ea2cb8708b
Author: Takashi Yano 
Date:   Wed Sep 4 22:47:42 2019 +0900

Cygwin: pty: Add a workaround for ^C handling.

- Pseudo console support introduced by commit
  169d65a5774acc76ce3f3feeedcbae7405aa9b57 sometimes cause random
  crash or freeze by pressing ^C while cygwin and non-cygwin
  processes are executed simultaneously in the same pty. This
  patch is a workaround for this issue.

Diff:
---
 winsup/cygwin/fork.cc  | 1 -
 winsup/cygwin/spawn.cc | 6 ++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/winsup/cygwin/fork.cc b/winsup/cygwin/fork.cc
index a3a7e75..0a929df 100644
--- a/winsup/cygwin/fork.cc
+++ b/winsup/cygwin/fork.cc
@@ -213,7 +213,6 @@ frok::child (volatile char * volatile here)
  - terminate the current fork call even if the child is initialized. */
   sync_with_parent ("performed fork fixups and dynamic dll loading", true);
 
-  init_console_handler (myself->ctty > 0);
   ForceCloseHandle1 (fork_info->forker_finished, forker_finished);
 
   pthread::atforkchild ();
diff --git a/winsup/cygwin/spawn.cc b/winsup/cygwin/spawn.cc
index 4bb28c4..15cba36 100644
--- a/winsup/cygwin/spawn.cc
+++ b/winsup/cygwin/spawn.cc
@@ -635,6 +635,12 @@ child_info_spawn::worker (const char *prog_arg, const char 
*const *argv,
   if (ptys)
ptys->fixup_after_attach (!iscygwin ());
 
+  if (!iscygwin ())
+   {
+ init_console_handler (myself->ctty > 0);
+ myself->ctty = 0;
+   }
+
 loop:
   /* When ruid != euid we create the new process under the current original
 account and impersonate in child, this way maintaining the different


Re: [PATCH v2 1/1] Cygwin: pty: Limit API hook to the program linked with the APIs.

2019-09-04 Thread Corinna Vinschen
On Sep  4 22:46, Takashi Yano wrote:
> - API hook used for pseudo console support causes slow down.
>   This patch limits API hook to only program which is linked
>   with the corresponding APIs. Normal cygwin program is not
>   linked with such APIs (such as WriteFile, etc...) directly,
>   therefore, no slow down occurs. However, console access by
>   cygwin.dll itself cannot switch the r/w pipe to pseudo console
>   side. Therefore, the code to switch it forcely to pseudo
>   console side is added to smallprint.cc and strace.cc.
> ---
>  winsup/cygwin/fhandler_tty.cc | 106 +++---
>  winsup/cygwin/smallprint.cc   |   2 +
>  winsup/cygwin/strace.cc   |  26 +
>  winsup/cygwin/winsup.h|   3 +
>  4 files changed, 66 insertions(+), 71 deletions(-)

Pushed.


Thanks,
Corinna

-- 
Corinna Vinschen
Cygwin Maintainer


signature.asc
Description: PGP signature


[newlib-cygwin] Cygwin: pty: Limit API hook to the program linked with the APIs.

2019-09-04 Thread Corinna Vinschen
https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=83b2d576c835dad6b8e2ea53b55a25e7bfcdcde7

commit 83b2d576c835dad6b8e2ea53b55a25e7bfcdcde7
Author: Takashi Yano 
Date:   Wed Sep 4 22:46:51 2019 +0900

Cygwin: pty: Limit API hook to the program linked with the APIs.

- API hook used for pseudo console support causes slow down.
  This patch limits API hook to only program which is linked
  with the corresponding APIs. Normal cygwin program is not
  linked with such APIs (such as WriteFile, etc...) directly,
  therefore, no slow down occurs. However, console access by
  cygwin.dll itself cannot switch the r/w pipe to pseudo console
  side. Therefore, the code to switch it forcely to pseudo
  console side is added to smallprint.cc and strace.cc.

Diff:
---
 winsup/cygwin/fhandler_tty.cc | 106 +++---
 winsup/cygwin/smallprint.cc   |   2 +
 winsup/cygwin/strace.cc   |  26 +--
 winsup/cygwin/winsup.h|   3 ++
 4 files changed, 66 insertions(+), 71 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 262c41b..fadff59 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -88,6 +88,19 @@ set_switch_to_pcon (void)
   }
 }
 
+void
+set_ishybrid_and_switch_to_pcon (HANDLE h)
+{
+  DWORD dummy;
+  if (!isHybrid
+  && GetFileType (h) == FILE_TYPE_CHAR
+  && GetConsoleMode (h, ))
+{
+  isHybrid = true;
+  set_switch_to_pcon ();
+}
+}
+
 #define DEF_HOOK(name) static __typeof__ (name) *name##_Orig
 DEF_HOOK (WriteFile);
 DEF_HOOK (WriteConsoleA);
@@ -100,6 +113,7 @@ DEF_HOOK (WriteConsoleOutputW);
 DEF_HOOK (WriteConsoleOutputCharacterA);
 DEF_HOOK (WriteConsoleOutputCharacterW);
 DEF_HOOK (WriteConsoleOutputAttribute);
+DEF_HOOK (SetConsoleTextAttribute);
 DEF_HOOK (WriteConsoleInputA);
 DEF_HOOK (WriteConsoleInputW);
 DEF_HOOK (ReadConsoleInputA);
@@ -107,140 +121,137 @@ DEF_HOOK (ReadConsoleInputW);
 DEF_HOOK (PeekConsoleInputA);
 DEF_HOOK (PeekConsoleInputW);
 
-#define CHK_CONSOLE_ACCESS(h) \
-{ \
-  DWORD dummy; \
-  if (!isHybrid \
-  && GetFileType (h) == FILE_TYPE_CHAR \
-  && GetConsoleMode (h, )) \
-{ \
-  isHybrid = true; \
-  set_switch_to_pcon (); \
-} \
-}
 static BOOL WINAPI
 WriteFile_Hooked
  (HANDLE h, LPCVOID p, DWORD l, LPDWORD n, LPOVERLAPPED o)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteFile_Orig (h, p, l, n, o);
 }
 static BOOL WINAPI
 WriteConsoleA_Hooked
  (HANDLE h, LPCVOID p, DWORD l, LPDWORD n, LPVOID o)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteConsoleA_Orig (h, p, l, n, o);
 }
 static BOOL WINAPI
 WriteConsoleW_Hooked
  (HANDLE h, LPCVOID p, DWORD l, LPDWORD n, LPVOID o)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteConsoleW_Orig (h, p, l, n, o);
 }
 static BOOL WINAPI
 ReadFile_Hooked
  (HANDLE h, LPVOID p, DWORD l, LPDWORD n, LPOVERLAPPED o)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return ReadFile_Orig (h, p, l, n, o);
 }
 static BOOL WINAPI
 ReadConsoleA_Hooked
  (HANDLE h, LPVOID p, DWORD l, LPDWORD n, LPVOID o)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return ReadConsoleA_Orig (h, p, l, n, o);
 }
 static BOOL WINAPI
 ReadConsoleW_Hooked
  (HANDLE h, LPVOID p, DWORD l, LPDWORD n, LPVOID o)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return ReadConsoleW_Orig (h, p, l, n, o);
 }
 static BOOL WINAPI
 WriteConsoleOutputA_Hooked
  (HANDLE h, CONST CHAR_INFO *p, COORD s, COORD c, PSMALL_RECT r)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteConsoleOutputA_Orig (h, p, s, c, r);
 }
 static BOOL WINAPI
 WriteConsoleOutputW_Hooked
  (HANDLE h, CONST CHAR_INFO *p, COORD s, COORD c, PSMALL_RECT r)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteConsoleOutputW_Orig (h, p, s, c, r);
 }
 static BOOL WINAPI
 WriteConsoleOutputCharacterA_Hooked
  (HANDLE h, LPCSTR p, DWORD l, COORD c, LPDWORD n)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteConsoleOutputCharacterA_Orig (h, p, l, c, n);
 }
 static BOOL WINAPI
 WriteConsoleOutputCharacterW_Hooked
  (HANDLE h, LPCWSTR p, DWORD l, COORD c, LPDWORD n)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteConsoleOutputCharacterW_Orig (h, p, l, c, n);
 }
 static BOOL WINAPI
 WriteConsoleOutputAttribute_Hooked
  (HANDLE h, CONST WORD *a, DWORD l, COORD c, LPDWORD n)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteConsoleOutputAttribute_Orig (h, a, l, c, n);
 }
 static BOOL WINAPI
+SetConsoleTextAttribute_Hooked
+ (HANDLE h, WORD a)
+{
+  set_ishybrid_and_switch_to_pcon (h);
+  return 

Re: [PATCH 2/2] Cygwin: pty: Disable clear screen on new pty if TERM=dumb or emacs*.

2019-09-04 Thread Corinna Vinschen
On Sep  4 21:49, Takashi Yano wrote:
> On Wed, 4 Sep 2019 12:47:38 +0200
> Corinna Vinschen wrote:
> > Why do you check the TERMs again here?  After all, need_clear_screen
> > is only true if one of these terms are used.
> 
> Because, emacs seems to set environment TERM after fixup_after_exec()
> is called. At the first check, TERM has the value of the terminal
> in which emacs is executed. The first check is just in case.

I still don't get it.

The code in fixup_after_attach() is the only code snippet setting
need_clear_screen = true.  And that code also requires term != "dump" &&
term == "*emacs*" to set need_clear_screen.

The code in reset_switch_to_pcon() requires that the need_clear_screen
flag is true regardless of checking TERM.  So this code depends on the
successful TERM check from fixup_after_attach anyway.

What am I missing?

While at it, in fixup_after_attach():

+ if (get_ttyp ()->num_pcon_attached_slaves == 0 &&
+ term && strcmp (term, "dumb") &&
+ term && !strstr (term, "emacs") &&
+ !ALWAYS_USE_PCON)

You're checking term for != NULL twice.


Thanks,
Corinna

-- 
Corinna Vinschen
Cygwin Maintainer


signature.asc
Description: PGP signature


[PATCH v2 1/1] Cygwin: pty: Add a workaround for ^C handling.

2019-09-04 Thread Takashi Yano
- Pseudo console support introduced by commit
  169d65a5774acc76ce3f3feeedcbae7405aa9b57 sometimes cause random
  crash or freeze by pressing ^C while cygwin and non-cygwin
  processes are executed simultaneously in the same pty. This
  patch is a workaround for this issue.
---
 winsup/cygwin/fork.cc  | 1 -
 winsup/cygwin/spawn.cc | 6 ++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/winsup/cygwin/fork.cc b/winsup/cygwin/fork.cc
index a3a7e7505..0a929dffd 100644
--- a/winsup/cygwin/fork.cc
+++ b/winsup/cygwin/fork.cc
@@ -213,7 +213,6 @@ frok::child (volatile char * volatile here)
  - terminate the current fork call even if the child is initialized. */
   sync_with_parent ("performed fork fixups and dynamic dll loading", true);
 
-  init_console_handler (myself->ctty > 0);
   ForceCloseHandle1 (fork_info->forker_finished, forker_finished);
 
   pthread::atforkchild ();
diff --git a/winsup/cygwin/spawn.cc b/winsup/cygwin/spawn.cc
index 4bb28c47b..15cba3610 100644
--- a/winsup/cygwin/spawn.cc
+++ b/winsup/cygwin/spawn.cc
@@ -635,6 +635,12 @@ child_info_spawn::worker (const char *prog_arg, const char 
*const *argv,
   if (ptys)
ptys->fixup_after_attach (!iscygwin ());
 
+  if (!iscygwin ())
+   {
+ init_console_handler (myself->ctty > 0);
+ myself->ctty = 0;
+   }
+
 loop:
   /* When ruid != euid we create the new process under the current original
 account and impersonate in child, this way maintaining the different
-- 
2.21.0



[PATCH v2 0/1] Cygwin: pty: Add a workaround for ^C handling.

2019-09-04 Thread Takashi Yano
- Pseudo console support introduced by commit
  169d65a5774acc76ce3f3feeedcbae7405aa9b57 sometimes cause random
  crash or freeze by pressing ^C while cygwin and non-cygwin
  processes are executed simultaneously in the same pty. This
  patch is a workaround for this issue.

v2:
Make the behaviour of pty and console identical.

Takashi Yano (1):
  Cygwin: pty: Add a workaround for ^C handling.

 winsup/cygwin/fork.cc  | 1 -
 winsup/cygwin/spawn.cc | 6 ++
 2 files changed, 6 insertions(+), 1 deletion(-)

-- 
2.21.0



[PATCH v2 1/1] Cygwin: pty: Limit API hook to the program linked with the APIs.

2019-09-04 Thread Takashi Yano
- API hook used for pseudo console support causes slow down.
  This patch limits API hook to only program which is linked
  with the corresponding APIs. Normal cygwin program is not
  linked with such APIs (such as WriteFile, etc...) directly,
  therefore, no slow down occurs. However, console access by
  cygwin.dll itself cannot switch the r/w pipe to pseudo console
  side. Therefore, the code to switch it forcely to pseudo
  console side is added to smallprint.cc and strace.cc.
---
 winsup/cygwin/fhandler_tty.cc | 106 +++---
 winsup/cygwin/smallprint.cc   |   2 +
 winsup/cygwin/strace.cc   |  26 +
 winsup/cygwin/winsup.h|   3 +
 4 files changed, 66 insertions(+), 71 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 262c41bfe..fadff59a3 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -88,6 +88,19 @@ set_switch_to_pcon (void)
   }
 }
 
+void
+set_ishybrid_and_switch_to_pcon (HANDLE h)
+{
+  DWORD dummy;
+  if (!isHybrid
+  && GetFileType (h) == FILE_TYPE_CHAR
+  && GetConsoleMode (h, ))
+{
+  isHybrid = true;
+  set_switch_to_pcon ();
+}
+}
+
 #define DEF_HOOK(name) static __typeof__ (name) *name##_Orig
 DEF_HOOK (WriteFile);
 DEF_HOOK (WriteConsoleA);
@@ -100,6 +113,7 @@ DEF_HOOK (WriteConsoleOutputW);
 DEF_HOOK (WriteConsoleOutputCharacterA);
 DEF_HOOK (WriteConsoleOutputCharacterW);
 DEF_HOOK (WriteConsoleOutputAttribute);
+DEF_HOOK (SetConsoleTextAttribute);
 DEF_HOOK (WriteConsoleInputA);
 DEF_HOOK (WriteConsoleInputW);
 DEF_HOOK (ReadConsoleInputA);
@@ -107,140 +121,137 @@ DEF_HOOK (ReadConsoleInputW);
 DEF_HOOK (PeekConsoleInputA);
 DEF_HOOK (PeekConsoleInputW);
 
-#define CHK_CONSOLE_ACCESS(h) \
-{ \
-  DWORD dummy; \
-  if (!isHybrid \
-  && GetFileType (h) == FILE_TYPE_CHAR \
-  && GetConsoleMode (h, )) \
-{ \
-  isHybrid = true; \
-  set_switch_to_pcon (); \
-} \
-}
 static BOOL WINAPI
 WriteFile_Hooked
  (HANDLE h, LPCVOID p, DWORD l, LPDWORD n, LPOVERLAPPED o)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteFile_Orig (h, p, l, n, o);
 }
 static BOOL WINAPI
 WriteConsoleA_Hooked
  (HANDLE h, LPCVOID p, DWORD l, LPDWORD n, LPVOID o)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteConsoleA_Orig (h, p, l, n, o);
 }
 static BOOL WINAPI
 WriteConsoleW_Hooked
  (HANDLE h, LPCVOID p, DWORD l, LPDWORD n, LPVOID o)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteConsoleW_Orig (h, p, l, n, o);
 }
 static BOOL WINAPI
 ReadFile_Hooked
  (HANDLE h, LPVOID p, DWORD l, LPDWORD n, LPOVERLAPPED o)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return ReadFile_Orig (h, p, l, n, o);
 }
 static BOOL WINAPI
 ReadConsoleA_Hooked
  (HANDLE h, LPVOID p, DWORD l, LPDWORD n, LPVOID o)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return ReadConsoleA_Orig (h, p, l, n, o);
 }
 static BOOL WINAPI
 ReadConsoleW_Hooked
  (HANDLE h, LPVOID p, DWORD l, LPDWORD n, LPVOID o)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return ReadConsoleW_Orig (h, p, l, n, o);
 }
 static BOOL WINAPI
 WriteConsoleOutputA_Hooked
  (HANDLE h, CONST CHAR_INFO *p, COORD s, COORD c, PSMALL_RECT r)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteConsoleOutputA_Orig (h, p, s, c, r);
 }
 static BOOL WINAPI
 WriteConsoleOutputW_Hooked
  (HANDLE h, CONST CHAR_INFO *p, COORD s, COORD c, PSMALL_RECT r)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteConsoleOutputW_Orig (h, p, s, c, r);
 }
 static BOOL WINAPI
 WriteConsoleOutputCharacterA_Hooked
  (HANDLE h, LPCSTR p, DWORD l, COORD c, LPDWORD n)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteConsoleOutputCharacterA_Orig (h, p, l, c, n);
 }
 static BOOL WINAPI
 WriteConsoleOutputCharacterW_Hooked
  (HANDLE h, LPCWSTR p, DWORD l, COORD c, LPDWORD n)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteConsoleOutputCharacterW_Orig (h, p, l, c, n);
 }
 static BOOL WINAPI
 WriteConsoleOutputAttribute_Hooked
  (HANDLE h, CONST WORD *a, DWORD l, COORD c, LPDWORD n)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteConsoleOutputAttribute_Orig (h, a, l, c, n);
 }
 static BOOL WINAPI
+SetConsoleTextAttribute_Hooked
+ (HANDLE h, WORD a)
+{
+  set_ishybrid_and_switch_to_pcon (h);
+  return SetConsoleTextAttribute_Orig (h, a);
+}
+static BOOL WINAPI
 WriteConsoleInputA_Hooked
  (HANDLE h, CONST INPUT_RECORD *r, DWORD l, LPDWORD n)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteConsoleInputA_Orig (h, r, l, n);
 }
 static BOOL WINAPI
 WriteConsoleInputW_Hooked
  (HANDLE h, CONST 

[PATCH v2 0/1] Cygwin: pty: Limit API hook to the program linked with the APIs.

2019-09-04 Thread Takashi Yano
- API hook used for pseudo console support causes slow down.
  This patch limits API hook to only program which is linked
  with the corresponding APIs. Normal cygwin program is not
  linked with such APIs (such as WriteFile, etc...) directly,
  therefore, no slow down occurs. However, console access by
  cygwin.dll itself cannot switch the r/w pipe to pseudo console
  side. Therefore, the code to switch it forcely to pseudo
  console side is added to smallprint.cc and strace.cc.

v2:
Unify set_ishybrid_and_switch_to_pcon() and CHK_CONSOLE_ACCESS()
because they have exactly the same functionality.

Takashi Yano (1):
  Cygwin: pty: Limit API hook to the program linked with the APIs.

 winsup/cygwin/fhandler_tty.cc | 106 +++---
 winsup/cygwin/smallprint.cc   |   2 +
 winsup/cygwin/strace.cc   |  26 +
 winsup/cygwin/winsup.h|   3 +
 4 files changed, 66 insertions(+), 71 deletions(-)

-- 
2.21.0



Re: [PATCH 1/2] Cygwin: pty: Add a workaround for ^C handling.

2019-09-04 Thread Corinna Vinschen
On Sep  4 22:30, Takashi Yano wrote:
> On Wed, 4 Sep 2019 12:42:22 +0200
> Corinna Vinschen wrote:
> > If this workaround works, what about making it the standard behaviour,
> > rather than pseudo-console only?  Would there be a downside?
> 
> I am not sure why, but console does not have this issue.
> However, I do not notice any downside.
> 
> If making it standard, the patch will be very simple as follows.
> 
> 
> diff --git a/winsup/cygwin/fork.cc b/winsup/cygwin/fork.cc
> index a3a7e7505..0a929dffd 100644
> --- a/winsup/cygwin/fork.cc
> +++ b/winsup/cygwin/fork.cc
> @@ -213,7 +213,6 @@ frok::child (volatile char * volatile here)
>   - terminate the current fork call even if the child is initialized. */
>sync_with_parent ("performed fork fixups and dynamic dll loading", true);
> 
> -  init_console_handler (myself->ctty > 0);
>ForceCloseHandle1 (fork_info->forker_finished, forker_finished);
> 
>pthread::atforkchild ();
> diff --git a/winsup/cygwin/spawn.cc b/winsup/cygwin/spawn.cc
> index 4bb28c47b..15cba3610 100644
> --- a/winsup/cygwin/spawn.cc
> +++ b/winsup/cygwin/spawn.cc
> @@ -635,6 +635,12 @@ child_info_spawn::worker (const char *prog_arg, const 
> char *const *argv,
>if (ptys)
> ptys->fixup_after_attach (!iscygwin ());
> 
> +  if (!iscygwin ())
> +   {
> + init_console_handler (myself->ctty > 0);
> + myself->ctty = 0;
> +   }
> +
>  loop:
>/* When ruid != euid we create the new process under the current 
> original
>  account and impersonate in child, this way maintaining the different

Sounds good to me.


Thanks,
Corinna

-- 
Corinna Vinschen
Cygwin Maintainer


signature.asc
Description: PGP signature


Re: [PATCH 4/4] Cygwin: pty: Limit API hook to the program linked with the APIs.

2019-09-04 Thread Corinna Vinschen
Hi Takashi,

On Sep  4 21:39, Takashi Yano wrote:
> Hi Corinna,
> 
> On Wed, 4 Sep 2019 12:03:51 +0200
> Corinna Vinschen wrote:
> > I'll push the other 3 patches from this series.  For this patch,
> > I wonder why you create set_ishybrid_and_switch_to_pcon while
> > at the same time define a macro CHK_CONSOLE_ACCESS with identical
> > functionality.
> 
> Yah, indeed!
> 
> > Suggestion: Only define set_ishybrid_and_switch_to_pcon() as
> > inline function (probably in winsup.h) and use only this througout.
> 
> This function uses static variable isHybrid (sorry camelback again)
> and static function set_switch_to_pcon() defined in fhandler_tty.cc.
> 
> To make it inline, a lot of changes will be necessary. How about
> non-inline function?

That will add extra function calls, but, yeah, sure.  We can streamline
this later.


Thanks,
Corinna

-- 
Corinna Vinschen
Cygwin Maintainer


signature.asc
Description: PGP signature


Re: [PATCH 1/2] Cygwin: pty: Add a workaround for ^C handling.

2019-09-04 Thread Takashi Yano
On Wed, 4 Sep 2019 12:42:22 +0200
Corinna Vinschen wrote:
> If this workaround works, what about making it the standard behaviour,
> rather than pseudo-console only?  Would there be a downside?

I am not sure why, but console does not have this issue.
However, I do not notice any downside.

If making it standard, the patch will be very simple as follows.


diff --git a/winsup/cygwin/fork.cc b/winsup/cygwin/fork.cc
index a3a7e7505..0a929dffd 100644
--- a/winsup/cygwin/fork.cc
+++ b/winsup/cygwin/fork.cc
@@ -213,7 +213,6 @@ frok::child (volatile char * volatile here)
  - terminate the current fork call even if the child is initialized. */
   sync_with_parent ("performed fork fixups and dynamic dll loading", true);

-  init_console_handler (myself->ctty > 0);
   ForceCloseHandle1 (fork_info->forker_finished, forker_finished);

   pthread::atforkchild ();
diff --git a/winsup/cygwin/spawn.cc b/winsup/cygwin/spawn.cc
index 4bb28c47b..15cba3610 100644
--- a/winsup/cygwin/spawn.cc
+++ b/winsup/cygwin/spawn.cc
@@ -635,6 +635,12 @@ child_info_spawn::worker (const char *prog_arg, const char 
*const *argv,
   if (ptys)
ptys->fixup_after_attach (!iscygwin ());

+  if (!iscygwin ())
+   {
+ init_console_handler (myself->ctty > 0);
+ myself->ctty = 0;
+   }
+
 loop:
   /* When ruid != euid we create the new process under the current original
 account and impersonate in child, this way maintaining the different

-- 
Takashi Yano 


Re: [PATCH 2/2] Cygwin: pty: Disable clear screen on new pty if TERM=dumb or emacs*.

2019-09-04 Thread Takashi Yano
On Wed, 4 Sep 2019 12:47:38 +0200
Corinna Vinschen wrote:
> Why do you check the TERMs again here?  After all, need_clear_screen
> is only true if one of these terms are used.

Because, emacs seems to set environment TERM after fixup_after_exec()
is called. At the first check, TERM has the value of the terminal
in which emacs is executed. The first check is just in case.

-- 
Takashi Yano 


Re: [PATCH 4/4] Cygwin: pty: Limit API hook to the program linked with the APIs.

2019-09-04 Thread Takashi Yano
Hi Corinna,

On Wed, 4 Sep 2019 12:03:51 +0200
Corinna Vinschen wrote:
> I'll push the other 3 patches from this series.  For this patch,
> I wonder why you create set_ishybrid_and_switch_to_pcon while
> at the same time define a macro CHK_CONSOLE_ACCESS with identical
> functionality.

Yah, indeed!

> Suggestion: Only define set_ishybrid_and_switch_to_pcon() as
> inline function (probably in winsup.h) and use only this througout.

This function uses static variable isHybrid (sorry camelback again)
and static function set_switch_to_pcon() defined in fhandler_tty.cc.

To make it inline, a lot of changes will be necessary. How about
non-inline function?

-- 
Takashi Yano 


Bugreport: openldap 2.4.48-1 ldapsearch coredump

2019-09-04 Thread Alexander Voropay
Hi!

Ildapsearch coredumps on (semi) complicated filter on MS AD LDAP
(Filtered request to find all non-blocked users w/o e-mail address).

(I've changed our domain name and output, sorry, DN is in UNICODE)
===
$ ldapsearch -H ldaps://SPSDCMAIN.office.local -x -W -D
"User.NM@office.local" -b "dc=office,dc=local"
'(&(objectClass=user)(objectCategory=user)(postOfficeBox=*)(!(userAccountControl:1.2.840.113556.1.4.803:=2))(!(mail=*)))'
sAMAccountName
Enter LDAP Password:
# extended LDIF
#
# LDAPv3
# base  with scope subtree
# filter:
(&(objectClass=user)(objectCategory=user)(postOfficeBox=*)(!(userAccountControl:1.2.840.113556.1.4.803:=2))(!(mail=*)))
# requesting: sAMAccountName
#

# \D0\A1\D0\BC\D1\83\x\87, Rezerved, ServicesAccount
 s, Users, SPB, office.local
dn:: Q0490KHQvNGxxx
Segmentation fault (core dumped)
===

The _same_ request works on the RHEL 7 ldapsearch "openldap-2.4.44-21.el7_6"


My system:

$ uname -a
CYGWIN_NT-10.0 USER.NM 3.0.7(0.338/5/3) 2019-04-30 18:08 x86_64 Cygwin

Windows 10 Professional Ver 10.0 Build 17134

 3415k 2019/04/30 C:\cygwin64\bin\cygwin1.dll
Cygwin DLL version info:
DLL version: 3.0.7
DLL epoch: 19
DLL old termios: 5
DLL malloc env: 28
Cygwin conv: 181
API major: 0
API minor: 338
Shared data: 5
DLL identifier: cygwin1
Mount registry: 3
Cygwin registry name: Cygwin
Installations name: Installations
Cygdrive default prefix:
Build date:
Shared id: cygwin1S5

openldap  2.4.48-1   OK
openldap-devel2.4.48-1   OK



--
-=AV=-

--
Problem reports:   http://cygwin.com/problems.html
FAQ:   http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple



Re: [PATCH 2/2] Cygwin: pty: Disable clear screen on new pty if TERM=dumb or emacs*.

2019-09-04 Thread Corinna Vinschen
On Sep  4 10:46, Takashi Yano wrote:
> - Pseudo console support introduced by commit
>   169d65a5774acc76ce3f3feeedcbae7405aa9b57 shows garbage ^[[H^[[J in
>   some of emacs screens. These screens do not handle ANSI escape
>   sequences. Therefore, clear screen is disabled on these screens.
> ---
>  winsup/cygwin/fhandler_tty.cc | 26 +++---
>  winsup/cygwin/tty.cc  |  1 +
>  winsup/cygwin/tty.h   |  1 +
>  3 files changed, 21 insertions(+), 7 deletions(-)
> 
> diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
> index 283558985..a74c3eecf 100644
> --- a/winsup/cygwin/fhandler_tty.cc
> +++ b/winsup/cygwin/fhandler_tty.cc
> @@ -972,6 +972,19 @@ skip_console_setting:
>  void
>  fhandler_pty_slave::reset_switch_to_pcon (void)
>  {
> +  if (get_ttyp ()->need_clear_screen)
> +{
> +  const char *term = getenv ("TERM");
> +  if (term && strcmp (term, "dumb") && !strstr (term, "emacs"))

Why do you check the TERMs again here?  After all, need_clear_screen
is only true if one of these terms are used.


Thanks,
Corinna

-- 
Corinna Vinschen
Cygwin Maintainer


signature.asc
Description: PGP signature


Re: [PATCH 1/2] Cygwin: pty: Add a workaround for ^C handling.

2019-09-04 Thread Corinna Vinschen
On Sep  4 10:46, Takashi Yano wrote:
> - Pseudo console support introduced by commit
>   169d65a5774acc76ce3f3feeedcbae7405aa9b57 sometimes cause random
>   crash or freeze by pressing ^C while cygwin and non-cygwin
>   processes are executed simultaneously in the same pty. This
>   patch is a workaround for this issue.

If this workaround works, what about making it the standard behaviour,
rather than pseudo-console only?  Would there be a downside?


Thanks,
Corinna

-- 
Corinna Vinschen
Cygwin Maintainer


signature.asc
Description: PGP signature


[newlib-cygwin] Cygwin: pty: Fix state management for pseudo console support.

2019-09-04 Thread Corinna Vinschen
https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=583102e7c9255ae25310154995f855e4f573f81c

commit 583102e7c9255ae25310154995f855e4f573f81c
Author: Takashi Yano 
Date:   Wed Sep 4 10:45:35 2019 +0900

Cygwin: pty: Fix state management for pseudo console support.

- Pseudo console support introduced by commit
  169d65a5774acc76ce3f3feeedcbae7405aa9b57 has some bugs which
  cause mismatch between state variables and real pseudo console
  state regarding console attaching and r/w pipe switching. This
  patch fixes this issue by redesigning the state management.

Diff:
---
 winsup/cygwin/dtable.cc   |  38 +---
 winsup/cygwin/fhandler.h  |   6 +-
 winsup/cygwin/fhandler_console.cc |  25 ++-
 winsup/cygwin/fhandler_tty.cc | 385 +-
 winsup/cygwin/fork.cc |  24 ++-
 winsup/cygwin/spawn.cc|  65 ---
 6 files changed, 289 insertions(+), 254 deletions(-)

diff --git a/winsup/cygwin/dtable.cc b/winsup/cygwin/dtable.cc
index ba5d162..4e9b6ed 100644
--- a/winsup/cygwin/dtable.cc
+++ b/winsup/cygwin/dtable.cc
@@ -147,18 +147,16 @@ dtable::get_debugger_info ()
 void
 dtable::stdio_init ()
 {
-  bool need_fixup_handle = false;
-  fhandler_pty_slave *ptys = NULL;
-  bool is_pty[3] = {false, false, false};
-  for (int fd = 0; fd < 3; fd ++)
+  int chk_order[] = {1, 0, 2};
+  for (int i = 0; i < 3; i ++)
 {
+  int fd = chk_order[i];
   fhandler_base *fh = cygheap->fdtab[fd];
   if (fh && fh->get_major () == DEV_PTYS_MAJOR)
{
- ptys = (fhandler_pty_slave *) fh;
+ fhandler_pty_slave *ptys = (fhandler_pty_slave *) fh;
  if (ptys->getPseudoConsole ())
{
- is_pty[fd] = true;
  bool attached = !!fhandler_console::get_console_process_id
(ptys->getHelperProcessId (), true);
  if (!attached)
@@ -167,15 +165,12 @@ dtable::stdio_init ()
 by some reason. This happens if the executable is
 a windows GUI binary, such as mintty. */
  FreeConsole ();
- AttachConsole (ptys->getHelperProcessId ());
- need_fixup_handle = true;
+ if (AttachConsole (ptys->getHelperProcessId ()))
+   break;
}
- ptys->reset_switch_to_pcon ();
}
}
 }
-  if (need_fixup_handle)
-goto fixup_handle;
 
   if (myself->cygstarted || ISSTATE (myself, PID_CYGPARENT))
 {
@@ -185,27 +180,6 @@ dtable::stdio_init ()
   return;
 }
 
-fixup_handle:
-  if (need_fixup_handle)
-{
-  HANDLE h;
-  h = CreateFile ("CONIN$", GENERIC_READ, FILE_SHARE_READ,
-  NULL, OPEN_EXISTING, 0, 0);
-  if (is_pty[0])
-   {
- SetStdHandle (STD_INPUT_HANDLE, h);
- ptys->set_handle (h);
-   }
-  h = CreateFile ("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE,
-  NULL, OPEN_EXISTING, 0, 0);
-  if (is_pty[1])
-   SetStdHandle (STD_OUTPUT_HANDLE, h);
-  if (is_pty[2])
-   SetStdHandle (STD_ERROR_HANDLE, h);
-  if (is_pty[1] || is_pty[2])
-   ptys->set_output_handle (h);
-}
-
   HANDLE in = GetStdHandle (STD_INPUT_HANDLE);
   HANDLE out = GetStdHandle (STD_OUTPUT_HANDLE);
   HANDLE err = GetStdHandle (STD_ERROR_HANDLE);
diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index c75e40c..e8c1651 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -2106,19 +2106,22 @@ class fhandler_pty_common: public fhandler_termios
  protected:
   BOOL process_opost_output (HANDLE h,
 const void *ptr, ssize_t& len, bool is_echo);
-  bool check_switch_to_pcon (void);
 };
 
 class fhandler_pty_slave: public fhandler_pty_common
 {
   HANDLE inuse;// used to indicate that a tty is in use
   HANDLE output_handle_cyg, io_handle_cyg;
+  DWORD pid_restore;
 
   /* Helper functions for fchmod and fchown. */
   bool fch_open_handles (bool chown);
   int fch_set_sd (security_descriptor , bool chown);
   void fch_close_handles ();
 
+  bool try_reattach_pcon ();
+  void restore_reattach_pcon ();
+
  public:
   /* Constructor */
   fhandler_pty_slave (int);
@@ -2172,7 +2175,6 @@ class fhandler_pty_slave: public fhandler_pty_common
   void set_switch_to_pcon (void);
   void reset_switch_to_pcon (void);
   void push_to_pcon_screenbuffer (const char *ptr, size_t len);
-  bool has_master_opened (void);
   void mask_switch_to_pcon (bool mask)
   {
 get_ttyp ()->mask_switch_to_pcon = mask;
diff --git a/winsup/cygwin/fhandler_console.cc 
b/winsup/cygwin/fhandler_console.cc
index 997c50d..1b034f4 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -3136,16 +3136,29 @@ DWORD
 fhandler_console::get_console_process_id (DWORD pid, bool match)
 {
   DWORD tmp;
-  int num = GetConsoleProcessList (, 1);
-  

Re: [PATCH v5 0/1] Fix PTY state management in pseudo console support.

2019-09-04 Thread Corinna Vinschen
On Sep  4 10:45, Takashi Yano wrote:
> Pseudo console support in test release TEST: Cygwin 3.1.0-0.3,
> introduced by commit 169d65a5774acc76ce3f3feeedcbae7405aa9b57,
> has some bugs which cause mismatch between state variables and
> real pseudo console state regarding console attaching and r/w
> pipe switching. This patch fixes this issue by redesigning the
> state management.
> 
> v5:
> Revise based on
> https://cygwin.com/ml/cygwin-patches/2019-q3/msg00111.html
> 
> v4:
> Small bug fix again.
> 
> v3:
> Fix the first issue (Bad file descriptor) reported in
> https://cygwin.com/ml/cygwin-patches/2019-q3/msg00104.html
> 
> v2:
> Small bug fixed from v1.
> 
> Takashi Yano (1):
>   Cygwin: pty: Fix state management for pseudo console support.
> 
>  winsup/cygwin/dtable.cc   |  38 +--
>  winsup/cygwin/fhandler.h  |   6 +-
>  winsup/cygwin/fhandler_console.cc |  25 +-
>  winsup/cygwin/fhandler_tty.cc | 385 --
>  winsup/cygwin/fork.cc |  24 +-
>  winsup/cygwin/spawn.cc|  65 ++---
>  6 files changed, 289 insertions(+), 254 deletions(-)
> 
> -- 
> 2.21.0

Pushed.


Thanks,
Corinna

-- 
Corinna Vinschen
Cygwin Maintainer


signature.asc
Description: PGP signature


[newlib-cygwin] Cygwin: pty: Move function hook_api() into hookapi.cc.

2019-09-04 Thread Corinna Vinschen
https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=ffbb9b49711f6a8e3c4f83b226ff9476327dcb61

commit ffbb9b49711f6a8e3c4f83b226ff9476327dcb61
Author: Takashi Yano 
Date:   Wed Sep 4 10:44:25 2019 +0900

Cygwin: pty: Move function hook_api() into hookapi.cc.

- PTY uses Win32 API hook for pseudo console suppot. The function
  hook_api() is used for this purpose and defined in fhandler_tty.cc
  previously. This patch moves it into hookapi.cc.

Diff:
---
 winsup/cygwin/fhandler_tty.cc | 44 ---
 winsup/cygwin/hookapi.cc  | 34 +
 winsup/cygwin/winsup.h|  1 +
 3 files changed, 35 insertions(+), 44 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 94ef2f8..f76f7b2 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -75,50 +75,6 @@ static bool pcon_attached[NTTYS];
 static bool isHybrid;
 
 #if USE_API_HOOK
-/* Hook WIN32 API */
-static
-void *hook_api (const char *mname, const char *name, const void *fn)
-{
-  HMODULE hm = GetModuleHandle (mname);
-  PIMAGE_NT_HEADERS pExeNTHdr = PIMAGE_NT_HEADERS (PBYTE (hm)
-  + PIMAGE_DOS_HEADER (hm)->e_lfanew);
-  DWORD importRVA = pExeNTHdr->OptionalHeader.DataDirectory
-[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
-  PIMAGE_IMPORT_DESCRIPTOR pdfirst =
-(PIMAGE_IMPORT_DESCRIPTOR) ((char *) hm + importRVA);
-  for (PIMAGE_IMPORT_DESCRIPTOR pd = pdfirst; pd->FirstThunk; pd++)
-{
-  if (pd->OriginalFirstThunk == 0)
-   continue;
-  PIMAGE_THUNK_DATA pt =
-   (PIMAGE_THUNK_DATA) ((char *) hm + pd->FirstThunk);
-  PIMAGE_THUNK_DATA pn =
-   (PIMAGE_THUNK_DATA) ((char *) hm + pd->OriginalFirstThunk);
-  for (PIMAGE_THUNK_DATA pi = pt; pn->u1.Ordinal; pi++, pn++)
-   {
- if (IMAGE_SNAP_BY_ORDINAL (pn->u1.Ordinal))
-   continue;
- PIMAGE_IMPORT_BY_NAME pimp =
-   (PIMAGE_IMPORT_BY_NAME) ((char *) hm + pn->u1.AddressOfData);
- if (strcmp (name, (char *) pimp->Name) != 0)
-   continue;
-#ifdef __x86_64__
-#define THUNK_FUNC_TYPE ULONGLONG
-#else
-#define THUNK_FUNC_TYPE DWORD
-#endif
- DWORD ofl = PAGE_READWRITE;
- if (!VirtualProtect (pi, sizeof (THUNK_FUNC_TYPE), ofl, ))
-   return NULL;
- void *origfn = (void *) pi->u1.Function;
- pi->u1.Function = (THUNK_FUNC_TYPE) fn;
- VirtualProtect (pi, sizeof (THUNK_FUNC_TYPE), ofl, );
- return origfn;
-   }
-}
-  return NULL;
-}
-
 static void
 set_switch_to_pcon (void)
 {
diff --git a/winsup/cygwin/hookapi.cc b/winsup/cygwin/hookapi.cc
index 4078e65..dcd9b1d 100644
--- a/winsup/cygwin/hookapi.cc
+++ b/winsup/cygwin/hookapi.cc
@@ -428,6 +428,40 @@ hook_or_detect_cygwin (const char *name, const void *fn, 
WORD& subsys, HANDLE h)
   return fh.origfn;
 }
 
+/* Hook a function in any DLL such as kernel32.dll */
+/* The DLL must be loaded in advance. */
+/* Used in fhandler_tty.cc */
+void *hook_api (const char *mname, const char *name, const void *fn)
+{
+  HMODULE hm = GetModuleHandle (mname);
+  PIMAGE_NT_HEADERS pExeNTHdr =
+rva (PIMAGE_NT_HEADERS, hm, PIMAGE_DOS_HEADER (hm)->e_lfanew);
+  DWORD importRVA = pExeNTHdr->OptionalHeader.DataDirectory
+[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
+  PIMAGE_IMPORT_DESCRIPTOR pdfirst =
+rva (PIMAGE_IMPORT_DESCRIPTOR, hm, importRVA);
+  for (PIMAGE_IMPORT_DESCRIPTOR pd = pdfirst; pd->FirstThunk; pd++)
+{
+  if (pd->OriginalFirstThunk == 0)
+   continue;
+  PIMAGE_THUNK_DATA pt = rva (PIMAGE_THUNK_DATA, hm, pd->FirstThunk);
+  PIMAGE_THUNK_DATA pn =
+   rva (PIMAGE_THUNK_DATA, hm, pd->OriginalFirstThunk);
+  for (PIMAGE_THUNK_DATA pi = pt; pn->u1.Ordinal; pi++, pn++)
+   {
+ if (IMAGE_SNAP_BY_ORDINAL (pn->u1.Ordinal))
+   continue;
+ PIMAGE_IMPORT_BY_NAME pimp =
+   rva (PIMAGE_IMPORT_BY_NAME, hm, pn->u1.AddressOfData);
+ if (strcmp (name, (char *) pimp->Name) != 0)
+   continue;
+ void *origfn = putmem (pi, fn);
+ return origfn;
+   }
+}
+  return NULL;
+}
+
 void
 ld_preload ()
 {
diff --git a/winsup/cygwin/winsup.h b/winsup/cygwin/winsup.h
index 95ab41e..ab7b3bb 100644
--- a/winsup/cygwin/winsup.h
+++ b/winsup/cygwin/winsup.h
@@ -199,6 +199,7 @@ ino_t __reg2 hash_path_name (ino_t hash, const char *name);
 void __reg2 nofinalslash (const char *src, char *dst);
 
 void __reg3 *hook_or_detect_cygwin (const char *, const void *, WORD&, HANDLE 
h = NULL);
+void __reg3 *hook_api (const char *mname, const char *name, const void *fn);
 
 /* Time related */
 void __stdcall totimeval (struct timeval *, PLARGE_INTEGER, int, int);


[newlib-cygwin] Cygwin: pty: Speed up a little hooked Win32 API for pseudo console.

2019-09-04 Thread Corinna Vinschen
https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=bddb018e10e90ea300537d5a13999558b7dce476

commit bddb018e10e90ea300537d5a13999558b7dce476
Author: Takashi Yano 
Date:   Wed Sep 4 10:44:24 2019 +0900

Cygwin: pty: Speed up a little hooked Win32 API for pseudo console.

- Some Win32 APIs are hooked in pty code for pseudo console support.
  This causes slow down. This patch improves speed a little.

Diff:
---
 winsup/cygwin/fhandler_tty.cc | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 4dbe96b..94ef2f8 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -155,7 +155,9 @@ DEF_HOOK (PeekConsoleInputW);
 #define CHK_CONSOLE_ACCESS(h) \
 { \
   DWORD dummy; \
-  if (!isHybrid && GetConsoleMode (h, )) \
+  if (!isHybrid \
+  && GetFileType (h) == FILE_TYPE_CHAR \
+  && GetConsoleMode (h, )) \
 { \
   isHybrid = true; \
   set_switch_to_pcon (); \


[newlib-cygwin] Cygwin: pty: Code cleanup

2019-09-04 Thread Corinna Vinschen
https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=9adb260181fa148fad0e91c55e41ea6d75eb8eb8

commit 9adb260181fa148fad0e91c55e41ea6d75eb8eb8
Author: Takashi Yano 
Date:   Wed Sep 4 10:44:23 2019 +0900

Cygwin: pty: Code cleanup

- Cleanup the code which is commented out by #if 0 regarding pseudo
  console.
- Remove #if 1 for experimental code which seems to be stable.

Diff:
---
 winsup/cygwin/fhandler_tty.cc | 28 
 1 file changed, 28 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index dd5ab52..4dbe96b 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -855,26 +855,6 @@ fhandler_pty_slave::cleanup ()
 int
 fhandler_pty_slave::close ()
 {
-#if 0
-  if (getPseudoConsole ())
-{
-  INPUT_RECORD inp[128];
-  DWORD n;
-  PeekFunc =
-   PeekConsoleInputA_Orig ? PeekConsoleInputA_Orig : PeekConsoleInput;
-  PeekFunc (get_handle (), inp, 128, );
-  bool pipe_empty = true;
-  while (n-- > 0)
-   if (inp[n].EventType == KEY_EVENT && inp[n].Event.KeyEvent.bKeyDown)
- pipe_empty = false;
-  if (pipe_empty)
-   {
- /* Flush input buffer */
- size_t len = UINT_MAX;
- read (NULL, len);
-   }
-}
-#endif
   termios_printf ("closing last open %s handle", ttyname ());
   if (inuse && !CloseHandle (inuse))
 termios_printf ("CloseHandle (inuse), %E");
@@ -1524,7 +1504,6 @@ fhandler_pty_slave::read (void *ptr, size_t& len)
 out:
   termios_printf ("%d = read(%p, %lu)", totalread, ptr, len);
   len = (size_t) totalread;
-#if 1 /* Experimenta code */
   /* Push slave read as echo to pseudo console screen buffer. */
   if (getPseudoConsole () && ptr0 && (get_ttyp ()->ti.c_lflag & ECHO))
 {
@@ -1532,7 +1511,6 @@ out:
   push_to_pcon_screenbuffer (ptr0, len);
   release_output_mutex ();
 }
-#endif
   mask_switch_to_pcon (false);
 }
 
@@ -2748,10 +2726,6 @@ restart:
   if (p)
 *p = L'-';
   LCID lcid = LocaleNameToLCID (lc, 0);
-#if 0
-  if (lcid == (LCID) -1)
-return lcid;
-#endif
   if (!lcid && !strcmp (charset, "ASCII"))
 return 0;
 
@@ -2842,7 +2816,6 @@ fhandler_pty_slave::fixup_after_attach (bool native_maybe)
}
}
 
-#if 1 /* Experimental code */
  /* Clear screen to synchronize pseudo console screen buffer
 with real terminal. This is necessary because pseudo
 console screen buffer is empty at start. */
@@ -2854,7 +2827,6 @@ fhandler_pty_slave::fixup_after_attach (bool native_maybe)
/* Assume this is the first process using this pty slave. */
WriteFile (get_output_handle_cyg (),
   "\033[H\033[J", 6, , NULL);
-#endif
 
  pcon_attached[get_minor ()] = true;
  get_ttyp ()->num_pcon_attached_slaves ++;


Re: [PATCH 4/4] Cygwin: pty: Limit API hook to the program linked with the APIs.

2019-09-04 Thread Corinna Vinschen
Hi Takashi,

On Sep  4 10:44, Takashi Yano wrote:
> - API hook used for pseudo console support causes slow down.
>   This patch limits API hook to only program which is linked
>   with the corresponding APIs. Normal cygwin program is not
>   linked with such APIs (such as WriteFile, etc...) directly,
>   therefore, no slow down occurs. However, console access by
>   cygwin.dll itself cannot switch the r/w pipe to pseudo console
>   side. Therefore, the code to switch it forcely to pseudo
>   console side is added to smallprint.cc and strace.cc.

I'll push the other 3 patches from this series.  For this patch,
I wonder why you create set_ishybrid_and_switch_to_pcon while
at the same time define a macro CHK_CONSOLE_ACCESS with identical
functionality.

Suggestion: Only define set_ishybrid_and_switch_to_pcon() as
inline function (probably in winsup.h) and use only this througout.


Thanks,
Corinna

-- 
Corinna Vinschen
Cygwin Maintainer


signature.asc
Description: PGP signature


netinet/* in Cygwin

2019-09-04 Thread Lukasz Swierczewski

Hello,

I have question.

I need netinet/* in my C project.

For example:

#include 
#include 
#include 
#include 

Is this available in Cygwin?
There are problems as standard ...
What do you need to do to compile with these libs?

--
Best Regards
Lukasz Swierczewski

--
Problem reports:   http://cygwin.com/problems.html
FAQ:   http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info:  http://cygwin.com/ml/#unsubscribe-simple