newlib/winsup/cygwin/fhandler_termios.cc
Christopher Faylor 23771fa1f7 * cygerrno.h (__set_errno): Modify debugging output to make searching strace
logs easier.  Throughout, change /dev/tty* to /dev/pty*.  Throughout, add flags
argument to fhandler_*::dup methods.
* devices.in: Rename (temporarily?) /dev/ttyN to /dev/ptyN.  Add /dev/ptymN
devices for pty masters.
* devices.cc: Regenerate.
* devices.h (MAX_CONSOLES): Set to max number supported by devices.in.
(fh_devices::FH_PTMX): Rename from FH_PTYM.
(device::operator int): Return by reference.
* dtable.cc (fh_alloc): Take pc as an argument rather than just the device.
This makes debugging easier since more information is available.  Actually
implement handling for already-allocated pty master devices.  Make different
decisions when generating fhandler for not-opened devices.  Add kludge to deal
with opening /dev/tty.
(cnew_no_ctor): New macro.
(build_fh_pc): Make debugging output more verbose.  Use new clone() fhandler
interface to duplicate archetypes.  Reset last term opened.
(dtable::dup_worker): Use Use new clone() fhandler interface to duplicate
archetypes.  Pass flags to child dup handler.
(dtable::dup3): Set O_NOCTTY flag if newfd is not stdin/stdout/stderr.
* fhandler.cc (fhandler_base::reset): Rename from operator =() and reduce
functionality and sense of copy direction.
(fhandler_base::open_with_arch): Use published interface to query io_handle().
Use new copyto() fhandler method to copy from/to found archetype.
* fhandler.h: Throughout, delete size(), add copyout, clone, and fhandler_*
(void *) methods.
(fhandler_base::reset): Rename from operator =().
(fhandler_termios::is_dev_tty): Delete.
(fhandler_termios): change "protected" region to "private".
(fhandler_termios::is_dev_tty): Delete.
(fhandler_termios): Rearrange protected/public.
(fhandler_termios::fhandler_termios): Remember last fhandler_termios "opened".
(fhandler_termios::~fhandler_termios): Forget last fhandler_termios opened.
(ioctl): Rename from ioctl_termios.  Take a void * argument.  Reflect argument
change in pinfo::set_ctty.
(fhandler_console::dup): Declare new function.  Set ctty here if appropriate.
(fhandler_pty_master::from_master): Privatize.
(fhandler_pty_master::to_master): Ditto.
(fhandler_pty_master::dwProcessId): Ditto.
(fhandler_pty_master::fhandler_pty_master): Add an `int' argument.
(fhandler_pty_master::open_setup): Declare new function.
(fhandler_pty_master::~fhandler_pty_master): Declare new method.
(fhandler_nodevice): Remove commented out function declaration.
* fhandler_console.cc: Use get_ttyp() instead of tc() throughout.
(fhandler_console::dup): Define new function to set controlling ctty on dup, as
appropriate.
(fhandler_console::ioctl): Reflect ioctl_termios name change.
(fhandler_console::setup): Rename from get_tty_stuff.
(fhandler_console::open_setup): Reflect argument change in pinfo::set_ctty.
(fhandler_console::fhandler_console): Set _tc here.
* fhandler_termios.cc (handler_termios::ioctl): Rename.  Take a void * arg like
other ioctl functions.
* fhandler_tty.cc (fhandler_pty_slave::dup): Call myself->set_ctty to
potentially reset the controlling terminal.
(fhandler_pty_slave::ioctl): Reflect name/arg change for ioctl_termios.
(fhandler_pty_slave::fhandler_pty_slave): Take a "unit" argument.  Call setup()
here so that we will know the unit number of this fhandler as soon as possible.
Set the unit as appropriate.
(handler_pty_master::open): Move most stuff to constructor and open_setup.
(handler_pty_slave::open_setup): Reflect argument change in pinfo::set_ctty.
(handler_pty_master::open_setup): Define new function.
(fhandler_pty_master::cleanup): Clear handles as a flag that the destructor
does not have to do "close" operations.
(fhandler_pty_master::close): Ditto.
(fhandler_pty_master::~fhandler_pty_master): Define new method.
(fhandler_pty_master::ioctl): Reflect name/arg change for ioctl_termios.
(fhandler_pty_master::setup): Allocate tty here.  Rely on handles being
returned from allocated test rather than opening them here.  Avoid setting
_need_nl here since it is already zeroed in the constructor.  Set up device
information with DEV_TTYM_MAJOR.
* path.h (path_conv &operator =): Take a const argument.
(path_conv::dup): Ditto.
(pathconv_arg::PC_OPEN): New enum.
(pathconv_arg::PC_CTTY): Ditto.
(path_types::PATH_CTTY): Ditto.
(path_types::PATH_OPEN): Ditto.
(path_conv::isopen): New method.
(path_conv::isctty_capable): Ditto.
* path.cc (path_conv::check): Set PATH_OPEN and PATH_CTTY as appropriate.
* pipe.cc (fhandler_pipe::open): Use copyto to copy pipe handle.
* syscall.cc (open): Reinstate fd > 2 check to disallow resetting ctty on
non-std* handles.
* tty.cc (tty_list::allocate): Pass out handles for allocated tty.  use
`not_allocated' to find unallocated ttys.  Avoid keeping the lock since the
allocation of the tty should be sufficient to prevent multiple access.
(tty::not_allocated): Clarify comment.  Rename.  Return handles when an unused
tty is found.  Simply test for existing tty.
(tty::exists): Rewrite to use `not_allocated'.
* tty.h (NTTYS): Reset down to actual number supported by devices.in.
(tty::not_allocated): Declare new function.
(tty_list::allocate): Pass out read/write tty handles.  Zero them when not
found.
* fhandler_proc.cc: Reflect name change from FH_PTYM -> FH_PTMX.
* pinfo.h (pinfo::set_ctty): Reduce/reorder arguments passed in.
* pinfo.cc (pinfo::set_ctty): Ditto.  Just use tc() built into the passed-in
fhandler_termios pointer.  Return true if ctty is assigned.
* syscalls.cc (open): Call build_fh_pc with PC_OPEN flag.  Set PC_CTTY if
appropriate.
(stat_worker): Remove is_dev_tty () stuff.
2011-10-15 22:37:30 +00:00

441 lines
9.8 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"
fhandler_termios *fhandler_termios::last;
/* Common functions shared by tty/console */
void
fhandler_termios::tcinit (bool is_pty_master)
{
/* Initial termios values */
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);
if (tc ()->is_console)
tc ()->kill_pgrp (__SIGSETPGRP);
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);
}
int
tty_min::is_orphaned_process_group (int pgid)
{
/* An orphaned process group is a process group in which the parent
of every member is either itself a member of the group or is not
a member of the group's session. */
termios_printf ("checking pgid %d, my sid %d, my parent %d", pgid, myself->sid, myself->ppid);
winpids pids ((DWORD) 0);
for (unsigned i = 0; i < pids.npids; i++)
{
_pinfo *p = pids[i];
termios_printf ("checking pid %d - has pgid %d\n", p->pid, p->pgid);
if (!p || !p->exists () || p->pgid != pgid)
continue;
pinfo ppid (p->ppid);
if (!ppid)
continue;
termios_printf ("ppid->pgid %d, ppid->sid %d", ppid->pgid, ppid->sid);
if (ppid->pgid != pgid && ppid->sid == myself->sid)
return 0;
}
return 1;
}
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;
}
int sigs_ignored =
((void *) global_sigs[sig].sa_handler == (void *) SIG_IGN) ||
(_main_tls->sigmask & SIGTOMASK (sig));
/* If the process is ignoring SIGTT*, then background IO is OK. If
the process is not ignoring SIGTT*, then the sig is to be sent to
all processes in the process group (unless the process group of the
process is orphaned, in which case we return EIO). */
if (sigs_ignored)
return bg_ok; /* Just allow the IO */
else if (tc ()->is_orphaned_process_group (myself->pgid))
{
termios_printf ("process group is orphaned");
set_errno (EIO); /* This is an IO error */
return bg_error;
}
else
{
/* Don't raise a SIGTT* signal if we have already been
interrupted by another signal. */
if (WaitForSingleObject (signal_arrived, 0) != WAIT_OBJECT_0)
{
siginfo_t si = {0};
si.si_signo = sig;
si.si_code = SI_KERNEL;
kill_pgrp (myself->pgid, si);
}
return bg_signalled;
}
}
#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);
}
int
fhandler_termios::tcgetsid ()
{
if (myself->ctty != -1 && myself->ctty == tc ()->ntty)
return tc ()->getsid ();
set_errno (ENOTTY);
return -1;
}
int
fhandler_termios::ioctl (int cmd, void *varg)
{
if (cmd != TIOCSCTTY)
return 1; /* Not handled by this function */
int arg = (int) varg;
if (arg != 0 && arg != 1)
{
set_errno (EINVAL);
return -1;
}
termios_printf ("myself->ctty %d, myself->sid %d, myself->pid %d, arg %d, tc()->getsid () %d\n",
myself->ctty, myself->sid, myself->pid, arg, tc ()->getsid ());
if (myself->ctty > 0 || myself->sid != myself->pid || (!arg && tc ()->getsid () > 0))
{
set_errno (EPERM);
return -1;
}
myself->ctty = -1;
myself->set_ctty (this, 0);
return 0;
}