Cygwin: pty: Communalize the code for temporary attach to console.
- This patch communalizes the code for attaching another console temporarily and resuming to the original attach state, because there were a plurality of similar codes throughout.
This commit is contained in:
parent
5f4e7b0368
commit
df24c3d3c6
|
@ -2321,6 +2321,8 @@ class fhandler_pty_common: public fhandler_termios
|
||||||
bool cygwin = false,
|
bool cygwin = false,
|
||||||
bool stub_only = false);
|
bool stub_only = false);
|
||||||
bool to_be_read_from_nat_pipe (void);
|
bool to_be_read_from_nat_pipe (void);
|
||||||
|
static DWORD attach_console_temporarily (DWORD target_pid);
|
||||||
|
static void resume_from_temporarily_attach (DWORD resume_pid);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static BOOL process_opost_output (HANDLE h, const void *ptr, ssize_t& len,
|
static BOOL process_opost_output (HANDLE h, const void *ptr, ssize_t& len,
|
||||||
|
|
|
@ -357,22 +357,10 @@ fhandler_termios::process_sigs (char c, tty* ttyp, fhandler_termios *fh)
|
||||||
which the target process is attaching before sending the
|
which the target process is attaching before sending the
|
||||||
CTRL_C_EVENT. After sending the event, reattach to the
|
CTRL_C_EVENT. After sending the event, reattach to the
|
||||||
console to which the process was previously attached. */
|
console to which the process was previously attached. */
|
||||||
bool console_exists = fhandler_console::exists ();
|
|
||||||
pinfo pinfo_resume = pinfo (myself->ppid);
|
|
||||||
DWORD resume_pid = 0;
|
DWORD resume_pid = 0;
|
||||||
if (pinfo_resume)
|
if (fh && !fh->is_console ())
|
||||||
resume_pid = pinfo_resume->dwProcessId;
|
resume_pid =
|
||||||
else
|
fhandler_pty_common::attach_console_temporarily (p->dwProcessId);
|
||||||
resume_pid = fhandler_pty_common::get_console_process_id
|
|
||||||
(myself->dwProcessId, false);
|
|
||||||
acquire_attach_mutex (mutex_timeout);
|
|
||||||
if ((!console_exists || resume_pid) && fh && !fh->is_console ())
|
|
||||||
{
|
|
||||||
FreeConsole ();
|
|
||||||
AttachConsole (p->dwProcessId);
|
|
||||||
init_console_handler (::cygheap->ctty
|
|
||||||
&& ::cygheap->ctty->is_console ());
|
|
||||||
}
|
|
||||||
if (fh && p == myself && being_debugged ())
|
if (fh && p == myself && being_debugged ())
|
||||||
{ /* Avoid deadlock in gdb on console. */
|
{ /* Avoid deadlock in gdb on console. */
|
||||||
fh->tcflush(TCIFLUSH);
|
fh->tcflush(TCIFLUSH);
|
||||||
|
@ -391,7 +379,7 @@ fhandler_termios::process_sigs (char c, tty* ttyp, fhandler_termios *fh)
|
||||||
GenerateConsoleCtrlEvent (CTRL_C_EVENT, 0);
|
GenerateConsoleCtrlEvent (CTRL_C_EVENT, 0);
|
||||||
ctrl_c_event_sent = true;
|
ctrl_c_event_sent = true;
|
||||||
}
|
}
|
||||||
if ((!console_exists || resume_pid) && fh && !fh->is_console ())
|
if (fh && !fh->is_console ())
|
||||||
{
|
{
|
||||||
/* If a process on pseudo console is killed by Ctrl-C,
|
/* If a process on pseudo console is killed by Ctrl-C,
|
||||||
this process may take over the ownership of the
|
this process may take over the ownership of the
|
||||||
|
@ -399,13 +387,8 @@ fhandler_termios::process_sigs (char c, tty* ttyp, fhandler_termios *fh)
|
||||||
before sending CTRL_C_EVENT. In this case, closing
|
before sending CTRL_C_EVENT. In this case, closing
|
||||||
pseudo console is necessary. */
|
pseudo console is necessary. */
|
||||||
fhandler_pty_slave::release_ownership_of_nat_pipe (ttyp, fh);
|
fhandler_pty_slave::release_ownership_of_nat_pipe (ttyp, fh);
|
||||||
FreeConsole ();
|
fhandler_pty_common::resume_from_temporarily_attach (resume_pid);
|
||||||
if (resume_pid && console_exists)
|
|
||||||
AttachConsole (resume_pid);
|
|
||||||
init_console_handler (::cygheap->ctty
|
|
||||||
&& ::cygheap->ctty->is_console ());
|
|
||||||
}
|
}
|
||||||
release_attach_mutex ();
|
|
||||||
need_discard_input = true;
|
need_discard_input = true;
|
||||||
}
|
}
|
||||||
if (p && p->ctty == ttyp->ntty && p->pgid == pgid)
|
if (p && p->ctty == ttyp->ntty && p->pgid == pgid)
|
||||||
|
|
|
@ -530,26 +530,13 @@ fhandler_pty_master::accept_input ()
|
||||||
DWORD target_pid = 0;
|
DWORD target_pid = 0;
|
||||||
if (pinfo_target)
|
if (pinfo_target)
|
||||||
target_pid = pinfo_target->dwProcessId;
|
target_pid = pinfo_target->dwProcessId;
|
||||||
pinfo pinfo_resume = pinfo (myself->ppid);
|
if (target_pid)
|
||||||
DWORD resume_pid;
|
|
||||||
if (pinfo_resume)
|
|
||||||
resume_pid = pinfo_resume->dwProcessId;
|
|
||||||
else
|
|
||||||
resume_pid = get_console_process_id (myself->dwProcessId, false);
|
|
||||||
bool console_exists = fhandler_console::exists ();
|
|
||||||
if (target_pid && (resume_pid || !console_exists))
|
|
||||||
{
|
{
|
||||||
/* Slave attaches to a different console than master.
|
/* Slave attaches to a different console than master.
|
||||||
Therefore reattach here. */
|
Therefore reattach here. */
|
||||||
acquire_attach_mutex (mutex_timeout);
|
DWORD resume_pid = attach_console_temporarily (target_pid);
|
||||||
FreeConsole ();
|
|
||||||
AttachConsole (target_pid);
|
|
||||||
cp_to = GetConsoleCP ();
|
cp_to = GetConsoleCP ();
|
||||||
FreeConsole ();
|
resume_from_temporarily_attach (resume_pid);
|
||||||
if (resume_pid && console_exists)
|
|
||||||
AttachConsole (resume_pid);
|
|
||||||
init_console_handler (false);
|
|
||||||
release_attach_mutex ();
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
cp_to = GetConsoleCP ();
|
cp_to = GetConsoleCP ();
|
||||||
|
@ -1230,34 +1217,19 @@ fhandler_pty_slave::reset_switch_to_nat_pipe (void)
|
||||||
HANDLE pcon_owner = OpenProcess (PROCESS_DUP_HANDLE, FALSE,
|
HANDLE pcon_owner = OpenProcess (PROCESS_DUP_HANDLE, FALSE,
|
||||||
get_ttyp ()->nat_pipe_owner_pid);
|
get_ttyp ()->nat_pipe_owner_pid);
|
||||||
if (pcon_owner)
|
if (pcon_owner)
|
||||||
{
|
|
||||||
pinfo pinfo_resume = pinfo (myself->ppid);
|
|
||||||
DWORD resume_pid;
|
|
||||||
if (pinfo_resume)
|
|
||||||
resume_pid = pinfo_resume->dwProcessId;
|
|
||||||
else
|
|
||||||
resume_pid =
|
|
||||||
get_console_process_id (myself->dwProcessId, false);
|
|
||||||
if (resume_pid)
|
|
||||||
{
|
{
|
||||||
HANDLE h_pcon_in;
|
HANDLE h_pcon_in;
|
||||||
DuplicateHandle (pcon_owner, get_ttyp ()->h_pcon_in,
|
DuplicateHandle (pcon_owner, get_ttyp ()->h_pcon_in,
|
||||||
GetCurrentProcess (), &h_pcon_in,
|
GetCurrentProcess (), &h_pcon_in,
|
||||||
0, TRUE, DUPLICATE_SAME_ACCESS);
|
0, TRUE, DUPLICATE_SAME_ACCESS);
|
||||||
acquire_attach_mutex (mutex_timeout);
|
DWORD target_pid = get_ttyp ()->nat_pipe_owner_pid;
|
||||||
FreeConsole ();
|
DWORD resume_pid = attach_console_temporarily (target_pid);
|
||||||
AttachConsole (get_ttyp ()->nat_pipe_owner_pid);
|
|
||||||
init_console_handler (false);
|
|
||||||
WaitForSingleObject (input_mutex, mutex_timeout);
|
WaitForSingleObject (input_mutex, mutex_timeout);
|
||||||
transfer_input (tty::to_cyg, h_pcon_in, get_ttyp (),
|
transfer_input (tty::to_cyg, h_pcon_in, get_ttyp (),
|
||||||
input_available_event);
|
input_available_event);
|
||||||
ReleaseMutex (input_mutex);
|
ReleaseMutex (input_mutex);
|
||||||
FreeConsole ();
|
resume_from_temporarily_attach (resume_pid);
|
||||||
AttachConsole (resume_pid);
|
|
||||||
init_console_handler (false);
|
|
||||||
release_attach_mutex ();
|
|
||||||
CloseHandle (h_pcon_in);
|
CloseHandle (h_pcon_in);
|
||||||
}
|
|
||||||
CloseHandle (pcon_owner);
|
CloseHandle (pcon_owner);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2853,26 +2825,13 @@ fhandler_pty_master::pty_master_fwd_thread (const master_fwd_thread_param_t *p)
|
||||||
DWORD target_pid = 0;
|
DWORD target_pid = 0;
|
||||||
if (pinfo_target)
|
if (pinfo_target)
|
||||||
target_pid = pinfo_target->dwProcessId;
|
target_pid = pinfo_target->dwProcessId;
|
||||||
pinfo pinfo_resume = pinfo (myself->ppid);
|
if (target_pid)
|
||||||
DWORD resume_pid;
|
|
||||||
if (pinfo_resume)
|
|
||||||
resume_pid = pinfo_resume->dwProcessId;
|
|
||||||
else
|
|
||||||
resume_pid = get_console_process_id (myself->dwProcessId, false);
|
|
||||||
bool console_exists = fhandler_console::exists ();
|
|
||||||
if (target_pid && (resume_pid || !console_exists))
|
|
||||||
{
|
{
|
||||||
/* Slave attaches to a different console than master.
|
/* Slave attaches to a different console than master.
|
||||||
Therefore reattach here. */
|
Therefore reattach here. */
|
||||||
acquire_attach_mutex (mutex_timeout);
|
DWORD resume_pid = attach_console_temporarily (target_pid);
|
||||||
FreeConsole ();
|
|
||||||
AttachConsole (target_pid);
|
|
||||||
cp_from = GetConsoleOutputCP ();
|
cp_from = GetConsoleOutputCP ();
|
||||||
FreeConsole ();
|
resume_from_temporarily_attach (resume_pid);
|
||||||
if (resume_pid && console_exists)
|
|
||||||
AttachConsole (resume_pid);
|
|
||||||
init_console_handler (false);
|
|
||||||
release_attach_mutex ();
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
cp_from = GetConsoleOutputCP ();
|
cp_from = GetConsoleOutputCP ();
|
||||||
|
@ -4130,6 +4089,7 @@ fhandler_pty_slave::setpgid_aux (pid_t pid)
|
||||||
{
|
{
|
||||||
bool attach_restore = false;
|
bool attach_restore = false;
|
||||||
HANDLE from = get_handle_nat ();
|
HANDLE from = get_handle_nat ();
|
||||||
|
DWORD resume_pid = 0;
|
||||||
if (get_ttyp ()->pcon_activated && get_ttyp ()->nat_pipe_owner_pid
|
if (get_ttyp ()->pcon_activated && get_ttyp ()->nat_pipe_owner_pid
|
||||||
&& !get_console_process_id (get_ttyp ()->nat_pipe_owner_pid, true))
|
&& !get_console_process_id (get_ttyp ()->nat_pipe_owner_pid, true))
|
||||||
{
|
{
|
||||||
|
@ -4139,22 +4099,15 @@ fhandler_pty_slave::setpgid_aux (pid_t pid)
|
||||||
GetCurrentProcess (), &from,
|
GetCurrentProcess (), &from,
|
||||||
0, TRUE, DUPLICATE_SAME_ACCESS);
|
0, TRUE, DUPLICATE_SAME_ACCESS);
|
||||||
CloseHandle (pcon_owner);
|
CloseHandle (pcon_owner);
|
||||||
FreeConsole ();
|
DWORD target_pid = get_ttyp ()->nat_pipe_owner_pid;
|
||||||
AttachConsole (get_ttyp ()->nat_pipe_owner_pid);
|
resume_pid = attach_console_temporarily (target_pid);
|
||||||
init_console_handler (false);
|
|
||||||
attach_restore = true;
|
attach_restore = true;
|
||||||
}
|
}
|
||||||
WaitForSingleObject (input_mutex, mutex_timeout);
|
WaitForSingleObject (input_mutex, mutex_timeout);
|
||||||
transfer_input (tty::to_cyg, from, get_ttyp (), input_available_event);
|
transfer_input (tty::to_cyg, from, get_ttyp (), input_available_event);
|
||||||
ReleaseMutex (input_mutex);
|
ReleaseMutex (input_mutex);
|
||||||
if (attach_restore)
|
if (attach_restore)
|
||||||
{
|
resume_from_temporarily_attach (resume_pid);
|
||||||
FreeConsole ();
|
|
||||||
pinfo p (myself->ppid);
|
|
||||||
if (!p || !AttachConsole (p->dwProcessId))
|
|
||||||
AttachConsole (ATTACH_PARENT_PROCESS);
|
|
||||||
init_console_handler (false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ReleaseMutex (pipe_sw_mutex);
|
ReleaseMutex (pipe_sw_mutex);
|
||||||
}
|
}
|
||||||
|
@ -4187,3 +4140,40 @@ fhandler_pty_slave::release_ownership_of_nat_pipe (tty *ttyp,
|
||||||
ReleaseMutex (ptym->pipe_sw_mutex);
|
ReleaseMutex (ptym->pipe_sw_mutex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DWORD
|
||||||
|
fhandler_pty_common::attach_console_temporarily (DWORD target_pid)
|
||||||
|
{
|
||||||
|
DWORD resume_pid = 0;
|
||||||
|
pinfo pinfo_resume (myself->ppid);
|
||||||
|
if (pinfo_resume)
|
||||||
|
resume_pid = pinfo_resume->dwProcessId;
|
||||||
|
if (!resume_pid)
|
||||||
|
resume_pid = get_console_process_id (myself->dwProcessId, false);
|
||||||
|
bool console_exists = fhandler_console::exists ();
|
||||||
|
acquire_attach_mutex (mutex_timeout);
|
||||||
|
if (!console_exists || resume_pid)
|
||||||
|
{
|
||||||
|
FreeConsole ();
|
||||||
|
AttachConsole (target_pid);
|
||||||
|
init_console_handler (::cygheap->ctty
|
||||||
|
&& ::cygheap->ctty->is_console ());
|
||||||
|
}
|
||||||
|
return console_exists ? resume_pid : (DWORD) -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
fhandler_pty_common::resume_from_temporarily_attach (DWORD resume_pid)
|
||||||
|
{
|
||||||
|
bool console_exists = (resume_pid != (DWORD) -1);
|
||||||
|
if (!console_exists || resume_pid)
|
||||||
|
{
|
||||||
|
FreeConsole ();
|
||||||
|
if (console_exists)
|
||||||
|
if (!resume_pid || !AttachConsole (resume_pid))
|
||||||
|
AttachConsole (ATTACH_PARENT_PROCESS);
|
||||||
|
init_console_handler (::cygheap->ctty
|
||||||
|
&& ::cygheap->ctty->is_console ());
|
||||||
|
}
|
||||||
|
release_attach_mutex ();
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue