* 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.
This commit is contained in:
Christopher Faylor
2011-10-15 22:37:30 +00:00
parent a9cc13a8e0
commit 23771fa1f7
31 changed files with 10454 additions and 7870 deletions

View File

@@ -539,7 +539,7 @@ void
fhandler_pty_slave::open_setup (int flags)
{
set_flags ((flags & ~O_TEXT) | O_BINARY);
myself->set_ctty (get_ttyp (), flags, this);
myself->set_ctty (this, flags);
cygheap->manage_console_count ("fhandler_pty_slave::open_setup", 1);
report_tty_counts (this, "opened", "");
}
@@ -614,7 +614,7 @@ fhandler_pty_slave::write (const void *ptr, size_t len)
if (bg <= bg_eof)
return (ssize_t) bg;
termios_printf ("tty%d, write(%x, %d)", get_unit (), ptr, len);
termios_printf ("pty%d, write(%x, %d)", get_unit (), ptr, len);
push_process_state process_state (PID_TTYOU);
@@ -891,15 +891,16 @@ out:
}
int
fhandler_pty_slave::dup (fhandler_base *child)
fhandler_pty_slave::dup (fhandler_base *child, int flags)
{
myself->set_ctty (this, flags);
cygheap->manage_console_count ("fhandler_pty_slave::dup", 1);
report_tty_counts (child, "duped slave", "");
return 0;
}
int
fhandler_pty_master::dup (fhandler_base *child)
fhandler_pty_master::dup (fhandler_base *child, int)
{
report_tty_counts (child, "duped master", "");
return 0;
@@ -947,7 +948,7 @@ int
fhandler_pty_slave::ioctl (unsigned int cmd, void *arg)
{
termios_printf ("ioctl (%x)", cmd);
int res = ioctl_termios (cmd, (int) arg);
int res = fhandler_termios::ioctl (cmd, arg);
if (res <= 0)
return res;
@@ -1184,37 +1185,34 @@ errout:
/*******************************************************
fhandler_pty_master
*/
fhandler_pty_master::fhandler_pty_master ()
: fhandler_pty_common (), pktmode (0), need_nl (0), dwProcessId (0)
fhandler_pty_master::fhandler_pty_master (int unit)
: fhandler_pty_common (), pktmode (0), master_ctl (NULL),
master_thread (NULL), from_master (NULL), to_master (NULL),
dwProcessId (0), need_nl (0)
{
if (unit >= 0)
dev ().parse (DEV_TTYM_MAJOR, unit);
else if (!setup ())
dev ().parse (FH_ERROR);
}
int
fhandler_pty_master::open (int flags, mode_t)
{
/* Note that allocate returns with the tty lock set if it was successful. */
int unit = cygwin_shared->tty.allocate ();
if (unit < 0)
return 0;
dev().parse (DEV_TTYM_MAJOR, unit);
if (!setup ())
{
lock_ttys::release ();
return 0;
}
lock_ttys::release ();
set_flags ((flags & ~O_TEXT) | O_BINARY);
set_open_status ();
dwProcessId = GetCurrentProcessId ();
char buf[sizeof ("opened pty master for ttyNNNNNNNNNNN")];
__small_sprintf (buf, "opened pty master for tty%d", get_unit ());
report_tty_counts (this, buf, "");
return 1;
}
void
fhandler_pty_master::open_setup (int flags)
{
set_flags ((flags & ~O_TEXT) | O_BINARY);
char buf[sizeof ("opened pty master for ptyNNNNNNNNNNN")];
__small_sprintf (buf, "opened pty master for pty%d", get_unit ());
report_tty_counts (this, buf, "");
}
_off64_t
fhandler_pty_common::lseek (_off64_t, int)
{
@@ -1225,7 +1223,7 @@ fhandler_pty_common::lseek (_off64_t, int)
int
fhandler_pty_common::close ()
{
termios_printf ("tty%d <%p,%p> closing", get_unit (), get_handle (), get_output_handle ());
termios_printf ("pty%d <%p,%p> closing", get_unit (), get_handle (), get_output_handle ());
if (!ForceCloseHandle (input_mutex))
termios_printf ("CloseHandle (input_mutex<%p>), %E", input_mutex);
if (!ForceCloseHandle (output_mutex))
@@ -1245,6 +1243,8 @@ void
fhandler_pty_master::cleanup ()
{
report_tty_counts (this, "closing master", "");
if (archetype)
from_master = to_master = NULL;
}
int
@@ -1272,11 +1272,14 @@ fhandler_pty_master::close ()
CloseHandle (master_ctl);
master_thread->detach ();
}
if (!ForceCloseHandle (from_master))
termios_printf ("error closing from_master %p, %E", from_master);
if (!ForceCloseHandle (to_master))
termios_printf ("error closing from_master %p, %E", to_master);
}
if (!ForceCloseHandle (from_master))
termios_printf ("error closing from_master %p, %E", from_master);
if (!ForceCloseHandle (to_master))
termios_printf ("error closing from_master %p, %E", to_master);
from_master = to_master = NULL;
fhandler_pty_common::close ();
if (hExeced || get_ttyp ()->master_pid != myself->pid)
@@ -1287,6 +1290,15 @@ fhandler_pty_master::close ()
return 0;
}
/* This is just to catch error conditions. Since the constructor
ctually opens some handles, and stat() does not open an fd, they need
to be closed when the fhandler goes away. */
fhandler_pty_master::~fhandler_pty_master ()
{
if (from_master && to_master)
close_with_arch ();
}
ssize_t __stdcall
fhandler_pty_master::write (const void *ptr, size_t len)
{
@@ -1361,7 +1373,7 @@ fhandler_pty_master::tcflush (int queue)
int
fhandler_pty_master::ioctl (unsigned int cmd, void *arg)
{
int res = ioctl_termios (cmd, (int) arg);
int res = fhandler_termios::ioctl (cmd, arg);
if (res <= 0)
return res;
@@ -1408,7 +1420,7 @@ fhandler_pty_master::ptsname ()
{
static char buf[TTY_NAME_MAX];
__small_sprintf (buf, "/dev/tty%d", get_unit ());
__small_sprintf (buf, "/dev/pty%d", get_unit ());
return buf;
}
@@ -1587,32 +1599,27 @@ fhandler_pty_master::setup ()
security_descriptor sd;
SECURITY_ATTRIBUTES sa = { sizeof (SECURITY_ATTRIBUTES), NULL, TRUE };
tty& t = *cygwin_shared->tty[get_unit ()];
_tc = (tty_min *)&t;
/* Find an unallocated pty to use. */
int unit = cygwin_shared->tty.allocate (from_master, get_output_handle ());
if (unit < 0)
return false;
ProtectHandle1 (get_output_handle (), to_pty);
tty& t = *cygwin_shared->tty[unit];
_tc = (tty_min *) &t;
tcinit (true); /* Set termios information. Force initialization. */
const char *errstr = NULL;
DWORD pipe_mode = PIPE_NOWAIT;
/* Create communication pipes */
char pipename[sizeof("ptyNNNN-from-master")];
__small_sprintf (pipename, "pty%d-from-master", get_unit ());
res = fhandler_pipe::create_selectable (&sec_none, from_master,
get_output_handle (), 128 * 1024,
pipename);
if (res)
{
errstr = "input pipe";
goto err;
}
ProtectHandle1 (get_output_handle (), to_pty);
if (!SetNamedPipeHandleState (get_output_handle (), &pipe_mode, NULL, NULL))
termios_printf ("can't set output_handle(%p) to non-blocking mode",
get_output_handle ());
__small_sprintf (pipename, "pty%d-to-master", get_unit ());
char pipename[sizeof("ptyNNNN-from-master")];
__small_sprintf (pipename, "pty%d-to-master", unit);
res = fhandler_pipe::create_selectable (&sec_none, get_io_handle (),
to_master, 128 * 1024, pipename);
if (res)
@@ -1622,7 +1629,6 @@ fhandler_pty_master::setup ()
}
ProtectHandle1 (get_io_handle (), from_pty);
need_nl = 0;
/* Create security attribute. Default permissions are 0620. */
sd.malloc (sizeof (SECURITY_DESCRIPTOR));
@@ -1642,18 +1648,18 @@ fhandler_pty_master::setup ()
goto err;
char buf[MAX_PATH];
errstr = shared_name (buf, OUTPUT_MUTEX, t.get_unit ());
errstr = shared_name (buf, OUTPUT_MUTEX, unit);
if (!(output_mutex = CreateMutex (&sa, FALSE, buf)))
goto err;
errstr = shared_name (buf, INPUT_MUTEX, t.get_unit ());
errstr = shared_name (buf, INPUT_MUTEX, unit);
if (!(input_mutex = CreateMutex (&sa, FALSE, buf)))
goto err;
/* Create master control pipe which allows the master to duplicate
the pty pipe handles to processes which deserve it. */
__small_sprintf (buf, "\\\\.\\pipe\\cygwin-%S-pty%d-master-ctl",
&installation_key, get_unit ());
&installation_key, unit);
master_ctl = CreateNamedPipe (buf, PIPE_ACCESS_DUPLEX,
PIPE_WAIT | PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE, 1, 4096, 4096,
@@ -1676,7 +1682,9 @@ fhandler_pty_master::setup ()
t.winsize.ws_row = 25;
t.master_pid = myself->pid;
termios_printf ("tty%d opened - from_pty %p, to_pty %p", t.get_unit (),
dev ().parse (DEV_TTYM_MAJOR, unit);
termios_printf ("this %p, pty%d opened - from_pty %p, to_pty %p", this, unit,
get_io_handle (), get_output_handle ());
return true;
@@ -1690,8 +1698,7 @@ err:
close_maybe (from_master);
close_maybe (to_master);
close_maybe (master_ctl);
termios_printf ("tty%d open failed - failed to create %s", t.get_unit (),
errstr);
termios_printf ("pty%d open failed - failed to create %s", unit, errstr);
return false;
}