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

Backport some fixes to 3.4 #205

Merged
merged 6 commits into from
Apr 7, 2024
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
34 changes: 24 additions & 10 deletions winsup/cygwin/fhandler/console.cc
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,12 @@ fhandler_console::cons_master_thread (handle_set_t *p, tty *ttyp)
}

WaitForSingleObject (p->input_mutex, mutex_timeout);
/* Ensure accessing input recored is not disabled. */
if (con.disable_master_thread)
{
ReleaseMutex (p->input_mutex);
continue;
}
total_read = 0;
switch (cygwait (p->input_handle, (DWORD) 0))
{
Expand Down Expand Up @@ -1005,10 +1011,14 @@ fhandler_console::read (void *pv, size_t& buflen)

push_process_state process_state (PID_TTYIN);

int copied_chars = 0;
size_t copied_chars = 0;

DWORD timeout = is_nonblocking () ? 0 : INFINITE;
DWORD timeout = is_nonblocking () ? 0 :
(get_ttyp ()->ti.c_lflag & ICANON ? INFINITE :
(get_ttyp ()->ti.c_cc[VMIN] == 0 ? 0 :
(get_ttyp ()->ti.c_cc[VTIME]*100 ? : INFINITE)));

read_more:
while (!input_ready && !get_cons_readahead_valid ())
{
int bgres;
Expand All @@ -1031,6 +1041,11 @@ fhandler_console::read (void *pv, size_t& buflen)
pthread::static_cancel_self ();
/*NOTREACHED*/
case WAIT_TIMEOUT:
if (copied_chars)
{
buflen = copied_chars;
return;
}
set_sig_errno (EAGAIN);
buflen = (size_t) -1;
return;
Expand Down Expand Up @@ -1076,19 +1091,20 @@ fhandler_console::read (void *pv, size_t& buflen)
}

/* Check console read-ahead buffer filled from terminal requests */
while (con.cons_rapoi && *con.cons_rapoi && buflen)
{
buf[copied_chars++] = *con.cons_rapoi++;
buflen --;
}
while (con.cons_rapoi && *con.cons_rapoi && buflen > copied_chars)
buf[copied_chars++] = *con.cons_rapoi++;

copied_chars +=
get_readahead_into_buffer (buf + copied_chars, buflen);
get_readahead_into_buffer (buf + copied_chars, buflen - copied_chars);

if (!con_ra.ralen)
input_ready = false;
release_input_mutex ();

if (buflen > copied_chars && !(get_ttyp ()->ti.c_lflag & ICANON)
&& copied_chars < get_ttyp ()->ti.c_cc[VMIN])
goto read_more;

#undef buf

buflen = copied_chars;
Expand Down Expand Up @@ -4328,8 +4344,6 @@ fhandler_console::need_console_handler ()
void
fhandler_console::set_disable_master_thread (bool x, fhandler_console *cons)
{
if (con.disable_master_thread == x)
return;
if (cons == NULL)
{
if (cygheap->ctty && cygheap->ctty->get_major () == DEV_CONS_MAJOR)
Expand Down
10 changes: 10 additions & 0 deletions winsup/cygwin/fhandler/pipe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,16 @@ fhandler_pipe::get_query_hdl_per_process (WCHAR *name,

for (LONG i = (LONG) n_process - 1; i >= 0; i--)
{
/* Non-cygwin app may call ReadFile() which makes NtQueryObject()
for ObjectNameInformation block. Therefore, stop to try to get
query_hdl for non-cygwin apps. */
pid_t cygpid;
if (!(cygpid = cygwin_pid (proc_pids[i])))
continue;
pinfo p (cygpid);
if (p && ISSTATE (p, PID_NOTCYGWIN))
continue;

HANDLE proc = OpenProcess (PROCESS_DUP_HANDLE
| PROCESS_QUERY_INFORMATION,
0, proc_pids[i]);
Expand Down
21 changes: 19 additions & 2 deletions winsup/cygwin/fhandler/pty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,8 @@ fhandler_pty_slave::open (int flags, mode_t)
errmsg = "can't call master, %E";
goto err;
}
CloseHandle (repl.to_slave_nat); /* not used. */
CloseHandle (repl.to_slave); /* not used. */
from_master_nat_local = repl.from_master_nat;
from_master_local = repl.from_master;
to_master_nat_local = repl.to_master_nat;
Expand Down Expand Up @@ -1218,6 +1220,10 @@ fhandler_pty_slave::reset_switch_to_nat_pipe (void)
if (!CallNamedPipe (pipe, &req, sizeof req,
&repl, sizeof repl, &len, 500))
return; /* What can we do? */
CloseHandle (repl.from_master); /* not used. */
CloseHandle (repl.to_master); /* not used. */
CloseHandle (repl.to_slave_nat); /* not used. */
CloseHandle (repl.to_slave); /* not used. */
CloseHandle (get_handle_nat ());
set_handle_nat (repl.from_master_nat);
CloseHandle (get_output_handle_nat ());
Expand Down Expand Up @@ -3932,10 +3938,20 @@ fhandler_pty_slave::transfer_input (tty::xfer_dir dir, HANDLE from, tty *ttyp,
if (!CallNamedPipe (pipe, &req, sizeof req,
&repl, sizeof repl, &len, 500))
return; /* What can we do? */
CloseHandle (repl.from_master_nat); /* not used. */
CloseHandle (repl.from_master); /* not used. */
CloseHandle (repl.to_master_nat); /* not used. */
CloseHandle (repl.to_master); /* not used. */
if (dir == tty::to_nat)
to = repl.to_slave_nat;
{
CloseHandle (repl.to_slave); /* not used. */
to = repl.to_slave_nat;
}
else
to = repl.to_slave;
{
CloseHandle (repl.to_slave_nat); /* not used. */
to = repl.to_slave;
}
}

UINT cp_from = 0, cp_to = 0;
Expand Down Expand Up @@ -4066,6 +4082,7 @@ fhandler_pty_slave::transfer_input (tty::xfer_dir dir, HANDLE from, tty *ttyp,
transfered = true;;
}
}
CloseHandle (to);

/* Fix input_available_event which indicates availability in cyg pipe. */
if (dir == tty::to_nat) /* all data is transfered to nat pipe,
Expand Down
Loading