4
0
mirror of git://sourceware.org/git/newlib-cygwin.git synced 2025-01-15 19:09:58 +08:00
newlib-cygwin/winsup/cygwin/fhandler_termios.cc
Christopher Faylor 44d2fc0a45 * autoload.cc: Call _api_fatal in asm.
* child_info.h: Redefine CURR_CHILD_INFO_MAGIC.
(child_info_fork::abort): Rename from handle_failure.  Change arguments.
* cygtls.h (_local_storage::ttybuf): New field.
* dcrt0.cc (vapi_fatal): Split api_fatal.  Add "in forked process" to message
when appropriate.
(api_fatal): Use vapi_fatal.
* devices.h: Make multiple inclusion safe.
(fh_devices): Add FH_CONS* stuff.  Reorder slightly.
(device): Eliminate anonymous union.  Add more ways to access minor/major.
(device::setunit): Accommodate no-longer-anonymous union.
(device::is_fs): Ditto.
(device::is_fs_special): Ditto.
(device::major): New function.
(device::minor): Ditto.
(device::is_device): New function.
(device::not_device): Ditto.
(device::operator int): New operator.
(device::operator fh_devices): Ditto.
(device::operator bool): Ditto.
(device::operator DWORD): Ditto.
(device::operator =): Ditto.
(isproc_dev): New function.
(isprocsys_dev): Ditto.
(iscons_dev): Ditto.
(istty_slave_dev): Ditto.
* devices.in: Add new "/dev/cons*" strings.  Accommodate no-longer-anonymous
union throughout.
(BRACK): Use more precise method for initialization.
* devices.cc: Regenerate.
* dtable.cc (dtable::stdio_init): Use get_cttyp instead of get_tty.
(dtable::find_archetype): Use new DWORD operator in device to test archetypes.
(dtable::init_std_file_from_handle): Use different method to initialize 'dev'.
Adapt to different ctty handling and accommodate /dev/cons*.
(fh_alloc): Accommodate no-longer-anonymous union.  Adapt to new /dev/cons*.
(build_fh_pc): Make debugging output more useful.
* exceptions.cc (ctrl_c_handler): Use get_cttyp instead of get_tty.
* external.cc (fillout_pinfo): Accommodate new cons* stuff.
* fhandler.cc (fhandler_base::read): Eliminate is_slow() test.
* fhandler.h (fhandler_base::*): Adapt to changes in device.h.
(fhandler_*::is_slow): Delete.
( fhandler_proc::get_proc_fhandler): Return fh_devices type.
* fhandler_console.cc (open_shared_console): New function.
(console_unit): New class.
(console_unit::console_unit): New constructor.
(enum_windows): New function.  Declare as friend to console_unit.
(fhandler_console::set_unit): New function.
(fhandler_console::get_tty_stuff): Call set_unit to set the unit number and
determine if initialization is needed.  Eliminate flags parameter.
(tty_list::get_cttyp): Rename (sorta) from get_tty.  Return pointer to correct
tty_min.
(fhandler_console::open): Adapt to elimination of argument to get_tty_stuff.
(fhandler_console::output_tcsetattr): Properly detect error condition.
(fhandler_console::fixup_after_fork_exec): Adapt to get_tty_stuff() setting tc
automatically.
* fhandler_proc.cc: Use FH_BAD rather than 0 throughout where using fh_devices
enum.
(fhandler_proc::get_proc_fhandler): Return fh_devices.  Adapt to devices.h
changes.
* fhandler_process.cc: Adapt to devices.h changes.  Use FH_BAD rather than 0
throughout where using fh_devices enum.
* fhandler_procnet.cc: Ditto.
* fhandler_procsys.cc: Ditto.
* fhandler_procsysvipc.cc: Ditto.
* fhandler_tape.cc (fhandler_dev_tape::fhandler_dev_tape): Ditto.
* fhandler_termios.cc (handler_termios::bg_check): Use tc->ttyname() rather
than assuming that we can construct a tty.
* fhandler_tty.cc (fhandler_tty_master::fhandler_tty_master): Just return
get_minor() of dev.
(fhandler_pty_master::process_slave_output): Add slightly more debugging info.
(fhandler_tty_slave::fhandler_tty_slave): Change name from ntty to unit.
(fhandler_pty_master::open): Ditto.
(fhandler_tty_slave::ioctl): Adapt to change which causes ctty to represent a
complete device.
(fhandler_tty_master::init_console): Add debugging for failure path.
(fhandler_pty_master::setup): Use get_unit() to retrieve unit number rather
than relying on raw ntty.
(fhandler_pty_master::setup): Ditto.
* fhandler_virtual.h (virt_tab_t): Redefine fhandler as fh_devices.
* fork.cc: Remove obsolete vfork stuff.
(frok::child): Don't assume that a ctty == 0 is valid.
* mount.cc (mount_info::conv_to_win32_path): Adapt to device struct changes.
(mount_info::conv_to_win32_path): Ditto.
* path.cc (path_conv::check): Retrive major/minor numbers via a method rather
than accessing them directly from device.  Rely on dev operators to
set/retrieve device information as required by device struct change.
* path.h (isproc_dev): Move to devices.h.
(isprocsys_dev): Ditto.
(isvirtual_dev): Ditto.
(path_conv:{isdevice,isfifo,isspecial,iscygdrive,issocket,get_devn,get_unitn}):
Use device methods to access/manipulate devices.
* pinfo.cc (pinfo::exit): Don't assume that ctty == 0 is valid.  Use iscons_dev
to determine if a device is a console.
(_pinfo::_ctty): Use device::parse to generate tty/cons name.
(_pinfo::set_ctty): Don't assume that ctty == 0 is valid.  Remove redundant
info from debugging.
* shared.cc (offsets): Remove console offset.
* shared_info.h (shared_locations): Ditto.
* syscalls.cc (umask): Use device methods to manipulate device information.
(ctermid): Use device::parse to generate term device name.
* tlsoffsets.h: Regenerate.
* tty.cc (ttyslot): Return minor number of ctty since ctty now represents a
full device.
(tty::create_master): Set ctty to a complete device.
(tty_list::attach): Rework to detect new /dev/cons* stuff.
(tty_list::terminate): Adapt to changes to ctty.
(tty_list::init): Adapt to change to setntty - pass in device major number.
(tty::exists): Use get_unit() to retrive tty unit number.
(tty::open_mutex): Ditto.
(tty::open_inuse): Ditto.
(tty::create_inuse): Ditto.
(tty::get_event): Ditto.
(tty_min::ttyname): Define new function.
* tty.h (tty_min::ntty): Redefine as fh_devices.
(tty::exists): Use get_unit() to retrive tty unit number.
(tty::open_mutex): Ditto.
(tty::open_inuse): Ditto.
(tty::create_inuse): Ditto.
(tty::get_event): Ditto.
(tty_min::ttyname): Declare new function.
(tty::getntty): Declare as const.
(tty_list::operator []): Assure that only minor part of argument is used.
* dll_init.cc (dll_list::alloc): Detect mismatch of data segments early issuing
an explicit error message if necessary.
* heap.cc (heap_init): Adapt to changes from fork->handle_failure to
fork->abort.
* pinfo.h (EXITCODE_FORK_FAILED): New enum.  (from Ryan Johnson)
* sigproc.cc (child_info_fork::abort): Rename from handle_failure.  Change
arguments to allow passing in a printf-like message.
* winsup.h (api_fatal): Delete macro definition.
(api_fatal): Redefine from __api_fatal.
(vapi_fatal): Declare new function.
* include/sys/strace.h (strace_vprintf): Define new macro.
* ntdll.h (_SYSTEM_INFORMATION_CLASS): Add SystemHandleInformation.
2011-05-28 18:17:09 +00:00

381 lines
8.0 KiB
C++

/* fhandler_termios.cc
Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009,
2010, 2011 Red Hat, Inc.
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#include "winsup.h"
#include <stdlib.h>
#include <ctype.h>
#include "cygerrno.h"
#include "path.h"
#include "fhandler.h"
#include "sigproc.h"
#include "pinfo.h"
#include "tty.h"
#include "cygtls.h"
#include "ntdll.h"
/* Common functions shared by tty/console */
void
fhandler_termios::tcinit (tty_min *this_tc, bool is_pty_master)
{
/* Initial termios values */
tc = this_tc;
if (is_pty_master || !tc->initialized ())
{
tc->ti.c_iflag = BRKINT | ICRNL | IXON;
tc->ti.c_oflag = OPOST | ONLCR;
tc->ti.c_cflag = B38400 | CS8 | CREAD;
tc->ti.c_lflag = ISIG | ICANON | ECHO | IEXTEN;
tc->ti.c_cc[VDISCARD] = CFLUSH;
tc->ti.c_cc[VEOL] = CEOL;
tc->ti.c_cc[VEOL2] = CEOL2;
tc->ti.c_cc[VEOF] = CEOF;
tc->ti.c_cc[VERASE] = CERASE;
tc->ti.c_cc[VINTR] = CINTR;
tc->ti.c_cc[VKILL] = CKILL;
tc->ti.c_cc[VLNEXT] = CLNEXT;
tc->ti.c_cc[VMIN] = 1;
tc->ti.c_cc[VQUIT] = CQUIT;
tc->ti.c_cc[VREPRINT] = CRPRNT;
tc->ti.c_cc[VSTART] = CSTART;
tc->ti.c_cc[VSTOP] = CSTOP;
tc->ti.c_cc[VSUSP] = CSUSP;
tc->ti.c_cc[VSWTC] = CSWTCH;
tc->ti.c_cc[VTIME] = 0;
tc->ti.c_cc[VWERASE] = CWERASE;
tc->ti.c_ispeed = tc->ti.c_ospeed = B38400;
tc->pgid = is_pty_master ? 0 : myself->pgid;
tc->initialized (true);
}
}
int
fhandler_termios::tcsetpgrp (const pid_t pgid)
{
termios_printf ("tty %d pgid %d, sid %d, tsid %d", tc->ntty, pgid,
myself->sid, tc->getsid ());
if (myself->sid != tc->getsid ())
{
set_errno (EPERM);
return -1;
}
int res;
while (1)
{
res = bg_check (-SIGTTOU);
switch (res)
{
case bg_ok:
tc->setpgid (pgid);
init_console_handler (tc->gethwnd ());
res = 0;
break;
case bg_signalled:
if (_my_tls.call_signal_handler ())
continue;
set_errno (EINTR);
/* fall through intentionally */
default:
res = -1;
break;
}
break;
}
return res;
}
int
fhandler_termios::tcgetpgrp ()
{
if (myself->ctty != -1 && myself->ctty == tc->ntty)
return tc->pgid;
set_errno (ENOTTY);
return -1;
}
int
fhandler_pty_master::tcgetpgrp ()
{
return tc->pgid;
}
void
tty_min::kill_pgrp (int sig)
{
int killself = 0;
winpids pids ((DWORD) PID_MAP_RW);
siginfo_t si = {0};
si.si_signo = sig;
si.si_code = SI_KERNEL;
for (unsigned i = 0; i < pids.npids; i++)
{
_pinfo *p = pids[i];
if (!p->exists () || p->ctty != ntty || p->pgid != pgid)
continue;
if (p == myself)
killself++;
else
sig_send (p, si);
}
if (killself)
sig_send (myself, si);
}
bg_check_types
fhandler_termios::bg_check (int sig)
{
if (!myself->pgid || tc->getpgid () == myself->pgid ||
myself->ctty != tc->ntty ||
((sig == SIGTTOU) && !(tc->ti.c_lflag & TOSTOP)))
return bg_ok;
if (sig < 0)
sig = -sig;
termios_printf ("bg I/O pgid %d, tpgid %d, %s, ntty %s", myself->pgid, tc->getpgid (),
myctty (), tc->ttyname ());
if (tc->getsid () == 0)
{
/* The pty has been closed by the master. Return an EOF
indication. FIXME: There is nothing to stop somebody
from reallocating this pty. I think this is the case
which is handled by unlockpt on a Unix system. */
termios_printf ("closed by master");
return bg_eof;
}
/* If the process group is no more or if process is ignoring or blocks 'sig',
return with error */
int pgid_gone = !pid_exists (myself->pgid);
int sigs_ignored =
((void *) global_sigs[sig].sa_handler == (void *) SIG_IGN) ||
(_main_tls->sigmask & SIGTOMASK (sig));
if (pgid_gone)
goto setEIO;
else if (!sigs_ignored)
/* nothing */;
else if (sig == SIGTTOU)
return bg_ok; /* Just allow the output */
else
goto setEIO; /* This is an output error */
/* Don't raise a SIGTT* signal if we have already been interrupted
by another signal. */
if (!IsEventSignalled (signal_arrived))
{
siginfo_t si = {0};
si.si_signo = sig;
si.si_code = SI_KERNEL;
kill_pgrp (myself->pgid, si);
}
return bg_signalled;
setEIO:
set_errno (EIO);
return bg_error;
}
#define set_input_done(x) input_done = input_done || (x)
inline void
fhandler_termios::echo_erase (int force)
{
if (force || tc->ti.c_lflag & ECHO)
doecho ("\b \b", 3);
}
line_edit_status
fhandler_termios::line_edit (const char *rptr, int nread, termios& ti)
{
line_edit_status ret = line_edit_ok;
char c;
int input_done = 0;
bool sawsig = false;
int iscanon = ti.c_lflag & ICANON;
while (nread-- > 0)
{
c = *rptr++;
termios_printf ("char %c", c);
/* Check for special chars */
if (c == '\r')
{
if (ti.c_iflag & IGNCR)
continue;
if (ti.c_iflag & ICRNL)
{
c = '\n';
set_input_done (iscanon);
}
}
else if (c == '\n')
{
if (ti.c_iflag & INLCR)
c = '\r';
else
set_input_done (iscanon);
}
if (ti.c_iflag & ISTRIP)
c &= 0x7f;
if (ti.c_lflag & ISIG)
{
int sig;
if (CCEQ (ti.c_cc[VINTR], c))
sig = SIGINT;
else if (CCEQ (ti.c_cc[VQUIT], c))
sig = SIGQUIT;
else if (CCEQ (ti.c_cc[VSUSP], c))
sig = SIGTSTP;
else
goto not_a_sig;
termios_printf ("got interrupt %d, sending signal %d", c, sig);
eat_readahead (-1);
tc->kill_pgrp (sig);
ti.c_lflag &= ~FLUSHO;
sawsig = true;
goto restart_output;
}
not_a_sig:
if (ti.c_iflag & IXON)
{
if (CCEQ (ti.c_cc[VSTOP], c))
{
if (!tc->output_stopped)
{
tc->output_stopped = 1;
acquire_output_mutex (INFINITE);
}
continue;
}
else if (CCEQ (ti.c_cc[VSTART], c))
{
restart_output:
tc->output_stopped = 0;
release_output_mutex ();
continue;
}
else if ((ti.c_iflag & IXANY) && tc->output_stopped)
goto restart_output;
}
if (iscanon && ti.c_lflag & IEXTEN && CCEQ (ti.c_cc[VDISCARD], c))
{
ti.c_lflag ^= FLUSHO;
continue;
}
if (!iscanon)
/* nothing */;
else if (CCEQ (ti.c_cc[VERASE], c))
{
if (eat_readahead (1))
echo_erase ();
continue;
}
else if (CCEQ (ti.c_cc[VWERASE], c))
{
int ch;
do
if (!eat_readahead (1))
break;
else
echo_erase ();
while ((ch = peek_readahead (1)) >= 0 && !isspace (ch));
continue;
}
else if (CCEQ (ti.c_cc[VKILL], c))
{
int nchars = eat_readahead (-1);
if (ti.c_lflag & ECHO)
while (nchars--)
echo_erase (1);
continue;
}
else if (CCEQ (ti.c_cc[VREPRINT], c))
{
if (ti.c_lflag & ECHO)
{
doecho ("\n\r", 2);
doecho (rabuf, ralen);
}
continue;
}
else if (CCEQ (ti.c_cc[VEOF], c))
{
termios_printf ("EOF");
accept_input ();
ret = line_edit_input_done;
continue;
}
else if (CCEQ (ti.c_cc[VEOL], c) ||
CCEQ (ti.c_cc[VEOL2], c) ||
c == '\n')
{
set_input_done (1);
termios_printf ("EOL");
}
if (ti.c_iflag & IUCLC && isupper (c))
c = cyg_tolower (c);
put_readahead (c);
if (ti.c_lflag & ECHO)
doecho (&c, 1);
if (!iscanon || input_done)
{
int status = accept_input ();
if (status != 1)
{
ret = status ? line_edit_error : line_edit_pipe_full;
eat_readahead (1);
break;
}
ret = line_edit_input_done;
input_done = 0;
}
}
if (!iscanon && ralen > 0)
ret = line_edit_input_done;
if (sawsig)
ret = line_edit_signalled;
return ret;
}
_off64_t
fhandler_termios::lseek (_off64_t, int)
{
set_errno (ESPIPE);
return -1;
}
void
fhandler_termios::sigflush ()
{
/* FIXME: Checking get_ttyp() for NULL is not right since it should not
be NULL while this is alive. However, we can conceivably close a
ctty while exiting and that will zero this. */
if (get_ttyp () && !(get_ttyp ()->ti.c_lflag & NOFLSH))
tcflush (TCIFLUSH);
}