Cygwin: pty: Implement new pseudo console support.

- In this implementation, pseudo console is created for each native
  console app. Advantages and disadvantages of this implementation
  over the previous implementation are as follows.

  Advantages:
  1) No performance degradation in pty output for cygwin process.
      https://cygwin.com/pipermail/cygwin/2020-February/243858.html
  2) Free from the problem caused by difference of behaviour of control
     sequences between real terminal and pseudo console.
      https://cygwin.com/pipermail/cygwin/2019-December/243281.html
      https://cygwin.com/pipermail/cygwin/2020-February/243855.html
  3) Free from the problem in cgdb and emacs gud.
      https://cygwin.com/pipermail/cygwin/2020-January/243601.html
      https://cygwin.com/pipermail/cygwin/2020-March/244146.html
  4) Redrawing screen on executing native console apps is not necessary.
  5) cygwin-console-helper is not necessary for the pseudo console
     support.
  6) The codes for pseudo console support are much simpler than that
     of the previous one.

  Disadvantages:
  1) The cygwin program which calls console API directly does not work.
  2) The apps which use console API cannot be debugged with gdb. This
     is because pseudo console is not activated since gdb uses
     CreateProcess() rather than exec(). Even with this limitation,
     attaching gdb to native apps, in which pseudo console is already
     activated, works.
  3) Typeahead key inputs are discarded while native console app is
     executed. Simirally, typeahead key inputs while cygwin app is
     executed are not inherited to native console app.
  4) Code page cannot be changed by chcp.com. Acctually, chcp works
     itself and changes code page of its own pseudo console.  However,
     since pseudo console is recreated for another process, it cannot
     inherit the code page.
  5) system_printf() does not work after stderr is closed. (Same with
     cygwin 3.0.7)
  6) Startup time of native console apps is about 3 times slower than
     previous implemenation.
  7) Pseudo console cannot be activated if it is already activated for
     another process on same pty.
This commit is contained in:
Takashi Yano 2020-08-19 20:25:21 +09:00 committed by Corinna Vinschen
parent b9261fa1d9
commit bb42852062
12 changed files with 340 additions and 1720 deletions

View File

@ -147,38 +147,6 @@ dtable::get_debugger_info ()
void
dtable::stdio_init ()
{
for (int i = 0; i < 3; i ++)
{
const int chk_order[] = {1, 0, 2};
int fd = chk_order[i];
fhandler_base *fh = cygheap->fdtab[fd];
if (fh && fh->get_major () == DEV_PTYS_MAJOR)
{
fhandler_pty_slave *ptys = (fhandler_pty_slave *) fh;
if (ptys->get_pseudo_console ())
{
bool attached = !!fhandler_console::get_console_process_id
(ptys->get_helper_process_id (), true);
if (attached)
break;
else
{
/* Not attached to pseudo console in fork() or spawn()
by some reason. This happens if the executable is
a windows GUI binary, such as mintty. */
FreeConsole ();
if (AttachConsole (ptys->get_helper_process_id ()))
{
ptys->fixup_after_attach (false, fd);
break;
}
}
}
}
else if (fh && fh->get_major () == DEV_CONS_MAJOR)
break;
}
if (myself->cygstarted || ISSTATE (myself, PID_CYGPARENT))
{
tty_min *t = cygwin_shared->tty.get_cttyp ();

View File

@ -326,16 +326,16 @@ class fhandler_base
virtual size_t &raixput () { return ra.raixput; };
virtual size_t &rabuflen () { return ra.rabuflen; };
virtual bool get_readahead_valid () { return raixget () < ralen (); }
bool get_readahead_valid () { return raixget () < ralen (); }
int puts_readahead (const char *s, size_t len = (size_t) -1);
virtual int put_readahead (char value);
int put_readahead (char value);
int get_readahead ();
int peek_readahead (int queryput = 0);
void set_readahead_valid (int val, int ch = -1);
virtual int get_readahead_into_buffer (char *buf, size_t buflen);
int get_readahead_into_buffer (char *buf, size_t buflen);
bool has_acls () const { return pc.has_acls (); }
@ -1905,7 +1905,7 @@ class fhandler_termios: public fhandler_base
int ioctl (int, void *);
tty_min *_tc;
tty *get_ttyp () {return (tty *) tc ();}
virtual int eat_readahead (int n);
int eat_readahead (int n);
public:
tty_min*& tc () {return _tc;}
@ -2184,7 +2184,6 @@ private:
static bool need_invisible ();
static void free_console ();
static const char *get_nonascii_key (INPUT_RECORD& input_rec, char *);
static DWORD get_console_process_id (DWORD pid, bool match);
fhandler_console (void *) {}
@ -2264,19 +2263,7 @@ class fhandler_pty_common: public fhandler_termios
return fh;
}
bool attach_pcon_in_fork (void)
{
return get_ttyp ()->attach_pcon_in_fork;
}
DWORD get_helper_process_id (void)
{
return get_ttyp ()->helper_process_id;
}
HPCON get_pseudo_console (void)
{
return get_ttyp ()->h_pseudo_console;
}
bool to_be_read_from_pcon (void);
void resize_pseudo_console (struct winsize *);
protected:
BOOL process_opost_output (HANDLE h,
@ -2287,23 +2274,15 @@ 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;
int fd;
/* Helper functions for fchmod and fchown. */
bool fch_open_handles (bool chown);
int fch_set_sd (security_descriptor &sd, bool chown);
void fch_close_handles ();
bool try_reattach_pcon ();
void restore_reattach_pcon ();
inline void free_attached_console ();
public:
/* Constructor */
fhandler_pty_slave (int);
/* Destructor */
~fhandler_pty_slave ();
void set_output_handle_cyg (HANDLE h) { output_handle_cyg = h; }
HANDLE& get_output_handle_cyg () { return output_handle_cyg; }
@ -2315,9 +2294,6 @@ class fhandler_pty_slave: public fhandler_pty_common
ssize_t __stdcall write (const void *ptr, size_t len);
void __reg3 read (void *ptr, size_t& len);
int init (HANDLE, DWORD, mode_t);
int eat_readahead (int n);
int get_readahead_into_buffer (char *buf, size_t buflen);
bool get_readahead_valid (void);
int tcsetattr (int a, const struct termios *t);
int tcgetattr (struct termios *t);
@ -2354,20 +2330,12 @@ class fhandler_pty_slave: public fhandler_pty_common
copyto (fh);
return fh;
}
void set_switch_to_pcon (int fd);
bool setup_pseudoconsole (STARTUPINFOEXW *si, bool nopcon);
void close_pseudoconsole (void);
void set_switch_to_pcon (void);
void reset_switch_to_pcon (void);
void push_to_pcon_screenbuffer (const char *ptr, size_t len, bool is_echo);
void mask_switch_to_pcon_in (bool mask);
void fixup_after_attach (bool native_maybe, int fd);
bool is_line_input (void)
{
return get_ttyp ()->ti.c_lflag & ICANON;
}
void setup_locale (void);
void set_freeconsole_on_close (bool val);
void trigger_redraw_screen (void);
void pull_pcon_input (void);
void update_pcon_input_state (bool need_lock);
};
#define __ptsname(buf, unit) __small_sprintf ((buf), "/dev/pty%d", (unit))
@ -2392,7 +2360,6 @@ public:
int process_slave_output (char *buf, size_t len, int pktmode_on);
void doecho (const void *str, DWORD len);
int accept_input ();
int put_readahead (char value);
int open (int flags, mode_t mode = 0);
void open_setup (int flags);
ssize_t __stdcall write (const void *ptr, size_t len);
@ -2431,9 +2398,7 @@ public:
copyto (fh);
return fh;
}
bool setup_pseudoconsole (void);
void transfer_input_to_pcon (void);
bool to_be_read_from_pcon (void);
};
class fhandler_dev_null: public fhandler_base

View File

@ -1206,18 +1206,6 @@ fhandler_console::close ()
if (con_ra.rabuf)
free (con_ra.rabuf);
/* If already attached to pseudo console, don't call free_console () */
cygheap_fdenum cfd (false);
while (cfd.next () >= 0)
if (cfd->get_major () == DEV_PTYM_MAJOR ||
cfd->get_major () == DEV_PTYS_MAJOR)
{
fhandler_pty_common *t =
(fhandler_pty_common *) (fhandler_base *) cfd;
if (get_console_process_id (t->get_helper_process_id (), true))
return 0;
}
if (!have_execed)
free_console ();
return 0;
@ -3611,37 +3599,6 @@ fhandler_console::need_invisible ()
return b;
}
DWORD
fhandler_console::get_console_process_id (DWORD pid, bool match)
{
DWORD tmp;
DWORD num, num_req;
num = 1;
num_req = GetConsoleProcessList (&tmp, num);
DWORD *list;
while (true)
{
list = (DWORD *)
HeapAlloc (GetProcessHeap (), 0, num_req * sizeof (DWORD));
num = num_req;
num_req = GetConsoleProcessList (list, num);
if (num_req > num)
HeapFree (GetProcessHeap (), 0, list);
else
break;
}
num = num_req;
tmp = 0;
for (DWORD i=0; i<num; i++)
if ((match && list[i] == pid) || (!match && list[i] != pid))
/* Last one is the oldest. */
/* https://github.com/microsoft/terminal/issues/95 */
tmp = list[i];
HeapFree (GetProcessHeap (), 0, list);
return tmp;
}
DWORD
fhandler_console::__acquire_input_mutex (const char *fn, int ln, DWORD ms)
{

File diff suppressed because it is too large Load Diff

View File

@ -134,36 +134,6 @@ child_info::prefork (bool detached)
int __stdcall
frok::child (volatile char * volatile here)
{
cygheap_fdenum cfd (false);
while (cfd.next () >= 0)
if (cfd->get_major () == DEV_PTYM_MAJOR)
{
fhandler_base *fh = cfd;
fhandler_pty_master *ptym = (fhandler_pty_master *) fh;
if (ptym->get_pseudo_console ())
{
debug_printf ("found a PTY master %d: helper_PID=%d",
ptym->get_minor (), ptym->get_helper_process_id ());
if (fhandler_console::get_console_process_id (
ptym->get_helper_process_id (), true))
/* Already attached */
break;
else
{
if (ptym->attach_pcon_in_fork ())
{
FreeConsole ();
if (!AttachConsole (ptym->get_helper_process_id ()))
/* Error */;
else
break;
}
}
}
}
extern void clear_pcon_attached_to (void); /* fhandler_tty.cc */
clear_pcon_attached_to ();
HANDLE& hParent = ch.parent;
sync_with_parent ("after longjmp", true);

View File

@ -1215,8 +1215,7 @@ verify_tty_slave (select_record *me, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds)
{
fhandler_pty_slave *ptys = (fhandler_pty_slave *) me->fh;
if (me->read_selected && !ptys->to_be_read_from_pcon () &&
IsEventSignalled (ptys->input_available_event))
if (me->read_selected && IsEventSignalled (ptys->input_available_event))
me->read_ready = true;
return set_bits (me, readfds, writefds, exceptfds);
}
@ -1229,8 +1228,6 @@ peek_pty_slave (select_record *s, bool from_select)
fhandler_pty_slave *ptys = (fhandler_pty_slave *) fh;
ptys->reset_switch_to_pcon ();
if (ptys->to_be_read_from_pcon ())
ptys->update_pcon_input_state (true);
if (s->read_selected)
{

View File

@ -405,7 +405,6 @@ small_printf (const char *fmt, ...)
count = __small_vsprintf (buf, fmt, ap);
va_end (ap);
set_ishybrid_and_switch_to_pcon (GetStdHandle (STD_ERROR_HANDLE));
WriteFile (GetStdHandle (STD_ERROR_HANDLE), buf, count, &done, NULL);
FlushFileBuffers (GetStdHandle (STD_ERROR_HANDLE));
}
@ -432,7 +431,6 @@ console_printf (const char *fmt, ...)
count = __small_vsprintf (buf, fmt, ap);
va_end (ap);
set_ishybrid_and_switch_to_pcon (console_handle);
WriteFile (console_handle, buf, count, &done, NULL);
FlushFileBuffers (console_handle);
}

View File

@ -177,7 +177,7 @@ find_exec (const char *name, path_conv& buf, const char *search,
/* Utility for child_info_spawn::worker. */
static HANDLE
handle (int fd, bool writing, bool iscygwin)
handle (int fd, bool writing)
{
HANDLE h;
cygheap_fdget cfd (fd);
@ -188,17 +188,30 @@ handle (int fd, bool writing, bool iscygwin)
h = INVALID_HANDLE_VALUE;
else if (!writing)
h = cfd->get_handle ();
else if (cfd->get_major () == DEV_PTYS_MAJOR && iscygwin)
{
fhandler_pty_slave *ptys = (fhandler_pty_slave *)(fhandler_base *) cfd;
h = ptys->get_output_handle_cyg ();
}
else
h = cfd->get_output_handle ();
return h;
}
static bool
is_console_app (WCHAR *filename)
{
HANDLE h;
const int id_offset = 92;
h = CreateFileW (filename, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, 0, NULL);
char buf[1024];
DWORD n;
ReadFile (h, buf, sizeof (buf), &n, 0);
CloseHandle (h);
char *p = (char *) memmem (buf, n, "PE\0\0", 4);
if (p && p + id_offset <= buf + n)
return p[id_offset] == '\003'; /* 02: GUI, 03: console */
else
return false;
}
int
iscmd (const char *argv0, const char *what)
{
@ -266,8 +279,6 @@ child_info_spawn::worker (const char *prog_arg, const char *const *argv,
{
bool rc;
int res = -1;
DWORD pid_restore = 0;
bool attach_to_console = false;
pid_t ctty_pgid = 0;
/* Search for CTTY and retrieve its PGID */
@ -587,9 +598,7 @@ child_info_spawn::worker (const char *prog_arg, const char *const *argv,
PROCESS_QUERY_LIMITED_INFORMATION))
sa = &sec_none_nih;
/* Attach to pseudo console if pty salve is used */
pid_restore = fhandler_console::get_console_process_id
(GetCurrentProcessId (), false);
fhandler_pty_slave *ptys_primary = NULL;
for (int i = 0; i < 3; i ++)
{
const int chk_order[] = {1, 0, 2};
@ -598,29 +607,11 @@ child_info_spawn::worker (const char *prog_arg, const char *const *argv,
if (fh && fh->get_major () == DEV_PTYS_MAJOR)
{
fhandler_pty_slave *ptys = (fhandler_pty_slave *) fh;
if (ptys->get_pseudo_console ())
{
DWORD helper_process_id = ptys->get_helper_process_id ();
debug_printf ("found a PTY slave %d: helper_PID=%d",
fh->get_minor (), helper_process_id);
if (fhandler_console::get_console_process_id
(helper_process_id, true))
/* Already attached */
attach_to_console = true;
else if (!attach_to_console)
{
FreeConsole ();
if (AttachConsole (helper_process_id))
attach_to_console = true;
}
ptys->fixup_after_attach (!iscygwin (), fd);
if (mode == _P_OVERLAY)
ptys->set_freeconsole_on_close (iscygwin ());
}
if (ptys_primary == NULL)
ptys_primary = ptys;
}
else if (fh && fh->get_major () == DEV_CONS_MAJOR)
{
attach_to_console = true;
fhandler_console *cons = (fhandler_console *) fh;
if (wincap.has_con_24bit_colors () && !iscygwin ())
if (fd == 1 || fd == 2)
@ -642,17 +633,28 @@ child_info_spawn::worker (const char *prog_arg, const char *const *argv,
/* Set up needed handles for stdio */
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdInput = handle ((in__stdin < 0 ? 0 : in__stdin), false,
iscygwin ());
si.hStdOutput = handle ((in__stdout < 0 ? 1 : in__stdout), true,
iscygwin ());
si.hStdError = handle (2, true, iscygwin ());
si.hStdInput = handle ((in__stdin < 0 ? 0 : in__stdin), false);
si.hStdOutput = handle ((in__stdout < 0 ? 1 : in__stdout), true);
si.hStdError = handle (2, true);
si.cb = sizeof (si);
if (!iscygwin ())
init_console_handler (myself->ctty > 0);
bool enable_pcon = false;
STARTUPINFOEXW si_pcon;
ZeroMemory (&si_pcon, sizeof (si_pcon));
STARTUPINFOW *si_tmp = &si;
if (!iscygwin () && ptys_primary && is_console_app (runpath))
if (ptys_primary->setup_pseudoconsole (&si_pcon,
mode != _P_OVERLAY && mode != _P_WAIT))
{
c_flags |= EXTENDED_STARTUPINFO_PRESENT;
si_tmp = &si_pcon.StartupInfo;
enable_pcon = true;
}
loop:
/* When ruid != euid we create the new process under the current original
account and impersonate in child, this way maintaining the different
@ -681,7 +683,7 @@ child_info_spawn::worker (const char *prog_arg, const char *const *argv,
c_flags,
envblock, /* environment */
NULL,
&si,
si_tmp,
&pi);
}
else
@ -735,7 +737,7 @@ child_info_spawn::worker (const char *prog_arg, const char *const *argv,
c_flags,
envblock, /* environment */
NULL,
&si,
si_tmp,
&pi);
if (hwst)
{
@ -748,6 +750,11 @@ child_info_spawn::worker (const char *prog_arg, const char *const *argv,
CloseDesktop (hdsk);
}
}
if (enable_pcon)
{
DeleteProcThreadAttributeList (si_pcon.lpAttributeList);
HeapFree (GetProcessHeap (), 0, si_pcon.lpAttributeList);
}
if (mode != _P_OVERLAY)
SetHandleInformation (my_wr_proc_pipe, HANDLE_FLAG_INHERIT,
@ -920,6 +927,11 @@ child_info_spawn::worker (const char *prog_arg, const char *const *argv,
}
if (sem)
__posix_spawn_sem_release (sem, 0);
if (enable_pcon)
{
WaitForSingleObject (pi.hProcess, INFINITE);
ptys_primary->close_pseudoconsole ();
}
myself.exit (EXITCODE_NOSET);
break;
case _P_WAIT:
@ -927,6 +939,8 @@ child_info_spawn::worker (const char *prog_arg, const char *const *argv,
system_call.arm ();
if (waitpid (cygpid, &res, 0) != cygpid)
res = -1;
if (enable_pcon)
ptys_primary->close_pseudoconsole ();
break;
case _P_DETACH:
res = 0; /* Lost all memory of this child. */
@ -953,21 +967,6 @@ child_info_spawn::worker (const char *prog_arg, const char *const *argv,
if (envblock)
free (envblock);
if (attach_to_console && pid_restore)
{
FreeConsole ();
AttachConsole (pid_restore);
cygheap_fdenum cfd (false);
int fd;
while ((fd = cfd.next ()) >= 0)
if (cfd->get_major () == DEV_PTYS_MAJOR)
{
fhandler_pty_slave *ptys =
(fhandler_pty_slave *) (fhandler_base *) cfd;
ptys->fixup_after_attach (false, fd);
}
}
return (int) res;
}

View File

@ -264,7 +264,6 @@ strace::vprntf (unsigned category, const char *func, const char *fmt, va_list ap
if (category & _STRACE_SYSTEM)
{
DWORD done;
set_ishybrid_and_switch_to_pcon (GetStdHandle (STD_ERROR_HANDLE));
WriteFile (GetStdHandle (STD_ERROR_HANDLE), buf, len, &done, 0);
FlushFileBuffers (GetStdHandle (STD_ERROR_HANDLE));
/* Make sure that the message shows up on the screen, too, since this is
@ -276,7 +275,6 @@ strace::vprntf (unsigned category, const char *func, const char *fmt, va_list ap
&sec_none, OPEN_EXISTING, 0, 0);
if (h != INVALID_HANDLE_VALUE)
{
set_ishybrid_and_switch_to_pcon (h);
WriteFile (h, buf, len, &done, 0);
CloseHandle (h);
}

View File

@ -234,20 +234,14 @@ tty::init ()
was_opened = false;
master_pid = 0;
is_console = false;
attach_pcon_in_fork = false;
h_pseudo_console = NULL;
column = 0;
h_pseudo_console = NULL;
switch_to_pcon_in = false;
switch_to_pcon_out = false;
screen_alternated = false;
mask_switch_to_pcon_in = false;
pcon_pid = 0;
term_code_page = 0;
need_redraw_screen = true;
pcon_last_time = 0;
pcon_in_empty = true;
req_transfer_input_to_pcon = false;
req_flush_pcon_input = false;
pcon_start = false;
}
HANDLE
@ -293,16 +287,6 @@ tty_min::ttyname ()
return d.name ();
}
void
tty::set_switch_to_pcon_out (bool v)
{
if (switch_to_pcon_out != v)
{
wait_pcon_fwd ();
switch_to_pcon_out = v;
}
}
void
tty::wait_pcon_fwd (void)
{

View File

@ -94,21 +94,13 @@ private:
HANDLE _to_master;
HANDLE _to_master_cyg;
HPCON h_pseudo_console;
HANDLE h_helper_process;
DWORD helper_process_id;
HANDLE h_helper_goodbye;
bool attach_pcon_in_fork;
bool pcon_start;
bool switch_to_pcon_in;
bool switch_to_pcon_out;
bool screen_alternated;
bool mask_switch_to_pcon_in;
pid_t pcon_pid;
UINT term_code_page;
bool need_redraw_screen;
DWORD pcon_last_time;
bool pcon_in_empty;
bool req_transfer_input_to_pcon;
bool req_flush_pcon_input;
HANDLE h_pcon_write_pipe;
public:
HANDLE from_master () const { return _from_master; }
@ -138,7 +130,6 @@ public:
void set_master_ctl_closed () {master_pid = -1;}
static void __stdcall create_master (int);
static void __stdcall init_session ();
void set_switch_to_pcon_out (bool v);
void wait_pcon_fwd (void);
friend class fhandler_pty_common;
friend class fhandler_pty_master;

View File

@ -224,9 +224,6 @@ void init_console_handler (bool);
extern bool wsock_started;
/* PTY related */
void set_ishybrid_and_switch_to_pcon (HANDLE h);
/* Printf type functions */
extern "C" void vapi_fatal (const char *, va_list ap) __attribute__ ((noreturn));
extern "C" void api_fatal (const char *, ...) __attribute__ ((noreturn));