Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix console mode restoring #87

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 31 additions & 16 deletions winsup/cygwin/fhandler/console.cc
Original file line number Diff line number Diff line change
Expand Up @@ -916,8 +916,7 @@ fhandler_console::cleanup_for_non_cygwin_app (handle_set_t *p)
/* Cleaning-up console mode for non-cygwin app. */
/* conmode can be tty::restore when non-cygwin app is
exec'ed from login shell. */
tty::cons_mode conmode =
(con.owner == GetCurrentProcessId ()) ? tty::restore : tty::cygwin;
tty::cons_mode conmode = cons_mode_on_close (p);
set_output_mode (conmode, ti, p);
set_input_mode (conmode, ti, p);
set_disable_master_thread (con.owner == GetCurrentProcessId ());
Expand Down Expand Up @@ -1976,22 +1975,13 @@ fhandler_console::close ()

acquire_output_mutex (mutex_timeout);

if (shared_console_info[unit] && !myself->cygstarted
if (shared_console_info[unit] && myself->ppid == 1
&& (dev_t) myself->ctty == get_device ())
{
/* Restore console mode if this is the last closure. */
OBJECT_BASIC_INFORMATION obi;
NTSTATUS status;
status = NtQueryObject (get_handle (), ObjectBasicInformation,
&obi, sizeof obi, NULL);
if (NT_SUCCESS (status)
&& obi.HandleCount == (con.owner == GetCurrentProcessId () ? 2 : 3))
{
/* Cleaning-up console mode for cygwin apps. */
set_output_mode (tty::restore, &get_ttyp ()->ti, &handle_set);
set_input_mode (tty::restore, &get_ttyp ()->ti, &handle_set);
set_disable_master_thread (true, this);
}
tty::cons_mode conmode = cons_mode_on_close (&handle_set);
set_output_mode (conmode, &get_ttyp ()->ti, &handle_set);
set_input_mode (conmode, &get_ttyp ()->ti, &handle_set);
set_disable_master_thread (true, this);
}

if (shared_console_info[unit] && con.owner == GetCurrentProcessId ())
Expand Down Expand Up @@ -4690,3 +4680,28 @@ fhandler_console::fstat (struct stat *st)
}
return 0;
}

tty::cons_mode
fhandler_console::cons_mode_on_close (handle_set_t *p)
{
const _minor_t unit = p->unit;

if (myself->ppid != 1) /* Execed from normal cygwin process. */
return tty::cygwin;

if (!process_alive (con.owner)) /* The Master process already died. */
return tty::restore;
if (con.owner == GetCurrentProcessId ()) /* Master process */
return tty::restore;

PROCESS_BASIC_INFORMATION pbi;
NTSTATUS status =
NtQueryInformationProcess (GetCurrentProcess (), ProcessBasicInformation,
&pbi, sizeof (pbi), NULL);
if (NT_SUCCESS (status)
&& con.owner == (DWORD) pbi.InheritedFromUniqueProcessId)
/* The parent is the stub process. */
return tty::restore;

return tty::native; /* Otherwise */
}
1 change: 1 addition & 0 deletions winsup/cygwin/local_includes/fhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -2391,6 +2391,7 @@ class fhandler_console: public fhandler_termios

void setup_pcon_hand_over ();
static void pcon_hand_over_proc ();
static tty::cons_mode cons_mode_on_close (handle_set_t *);

friend tty_min * tty_list::get_cttyp ();
};
Expand Down