newlib-cygwin/winsup/cygwin/fhandler_termios.cc

365 lines
7.9 KiB
C++
Raw Normal View History

2000-02-18 03:38:33 +08:00
/* fhandler_termios.cc
Copyright 1999, 2000, 2001, 2002, 2003 Red Hat, Inc.
2000-02-18 03:38:33 +08:00
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 <sys/termios.h>
2000-02-18 03:38:33 +08:00
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include "cygerrno.h"
#include "security.h"
#include "fhandler.h"
#include "sigproc.h"
#include "pinfo.h"
#include "tty.h"
2000-02-18 03:38:33 +08:00
/* Common functions shared by tty/console */
void
fhandler_termios::tcinit (tty_min *this_tc, int force)
{
/* Initial termios values */
tc = this_tc;
if (force || !TTYISSETF (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 = myself->pgid;
TTYSETF (INITIALIZED);
}
}
int
fhandler_termios::tcsetpgrp (const pid_t pgid)
{
termios_printf ("tty %d pgid %d, sid %d, tsid %d", tc->ntty, pgid,
2000-02-18 03:38:33 +08:00
myself->sid, tc->getsid ());
if (myself->sid != tc->getsid ())
{
set_errno (EPERM);
return -1;
}
tc->setpgid (pgid);
return 0;
}
int
fhandler_termios::tcgetpgrp ()
{
return tc->pgid;
}
* sync.h (new_muto): Just accept an argument which denotes the name of the muto. Use this argument to construct static storage. * cygheap.cc (cygheap_init): Reflect above change. * exceptions.cc (events_init): Ditto. * malloc.cc (malloc_init): Ditto. * path.cc (cwdstuff::init): Ditto. * cygheap.h (cwdstuff): Change name of lock element to make it less generic. * path.cc (cwdstuff::get_hash): Ditto. (cwdstuff::get_initial): Ditto. (cwdstuff::set): Ditto. (cwdstuff::get): Ditto. * sigproc.cc (proc_subproc): Ditto. * debug.cc (lock_debug): Change to method. Use method rather than macro throughout. * tty.h (tty_min::kill_pgrp): Declare new method. * fhandler_termios.cc (tty_min::kill_pgrp): New method. (fhandler_termios::line_edit): Use new method for killing process. * dcrt0.cc (do_exit): Ditto. * dtable.cc (dtable::get_debugger_info): New method for inheriting dtable info from a debugger. * tty.cc (tty_init): Attempt to grab file handle info from parent debugger, if appropriate. # dtable.cc (dtable::stdio_init): Make this a method. (dtable::init_std_file_from_handle): Don't set fd unless it's not open. (dtable::build_fhandler_from_name): Move name setting to dtable::build_fhandler. (dtable::build_fhandler): Add win32 name parameter. * dcrt0.cc (dll_crt0_1): Change to use dtable stdio_init. * dtable.h (dtable): Reflect build_fhandler parameter change. * mmap.cc (mmap_record::alloc_fh): Don't set name parameter in build_fhandler. * net.cc (fdsock): Remove set_name call since it is now handled by build_fhandler. * sigproc.cc (proc_subproc): Release muto as early as possible.
2002-02-23 03:33:41 +08:00
void
tty_min::kill_pgrp (int sig)
{
int killself = 0;
winpids pids;
for (unsigned i = 0; i < pids.npids; i++)
{
_pinfo *p = pids[i];
if (!proc_exists (p) || p->ctty != ntty || p->pgid != pgid)
continue;
if (p == myself)
killself++;
else
(void) sig_send (p, sig);
}
if (killself)
sig_send (myself, sig);
}
* autoload.cc (LoadFuncEx): Define via new LoadFuncEx2 macro. (LoadFuncEx2): Adapted from LoadFuncEx. Provides control of return value for nonexistent function. (NtQueryObject): Declare. (IsDebuggerPresent): Declare via LoadFuncEx2 and always return true if not available. * debug.h (being_debugged): Just rely on IsDebuggerPresent return value. * dtable.cc (handle_to_fn): New function. (dtable::init_std_file_from_handle): Attempt to derive std handle's name via handle_to_fn. (dtable::build_fhandler_from_name): Fill in what we can in path_conv structure when given a handle and path doesn't exist. * fhandler.cc (fhandler_base::open): Don't set the file pointer here. Use pc->exists () to determine if file exists rather than calling GetFileAttributes again. * fhandler.h (fhandler_base::exec_state_isknown): New method. (fhandler_base::fstat_helper): Add extra arguments to declaration. (fhandler_base::fstat_by_handle): Declare new method. (fhandler_base::fstat_by_name): Declare new method. * fhandler_disk_file (num_entries): Make __stdcall. (fhandler_base::fstat_by_handle): Define new method. (fhandler_base::fstat_by_name): Define new method. (fhandler_base:fstat): Call fstat_by_{handle,name} as appropriate. (fhandler_disk_file::fstat_helper): Accept extra arguments for filling out stat structure. Move handle or name specific stuff to new methods above. (fhandler_disk_file::open): Use real_path->exists rather than calling GetFileAttributes again. * ntdll.h (FILE_NAME_INFORMATION): Define new structure. (OBJECT_INFORMATION_CLASS): Partially define new enum. (OBJECT_NAME_INFORMATION): Define new structure. (NtQueryInformationFile): New declaration. (NtQueryObject): New declaration. * path.cc (path_conv::fillin): Define new method. * path.h (path_conv::fillin): Declare new method. (path_conv::drive_thpe): Rename from 'get_drive_type'. (path_conv::volser): Declare new method. (path_conv::volname): Declare new method. (path_conv::root_dir): Declare new method. * syscalls.cc (fstat64): Send real path_conv to fstat as second argument.
2002-05-28 09:55:40 +08:00
2000-02-18 03:38:33 +08:00
void
tty_min::set_ctty (int ttynum, int flags)
2000-02-18 03:38:33 +08:00
{
if ((myself->ctty < 0 || myself->ctty == ttynum) && !(flags & O_NOCTTY))
{
myself->ctty = ttynum;
syscall_printf ("attached tty%d sid %d, pid %d, tty->pgid %d, tty->sid %d",
ttynum, myself->sid, myself->pid, pgid, getsid ());
2000-02-18 03:38:33 +08:00
pinfo p (getsid ());
2000-02-18 03:38:33 +08:00
if (myself->sid == myself->pid &&
(p == myself || !proc_exists (p)))
{
paranoid_printf ("resetting tty%d sid. Was %d, now %d. pgid was %d, now %d.",
ttynum, getsid (), myself->sid, getpgid (), myself->pgid);
2000-02-18 03:38:33 +08:00
/* We are the session leader */
setsid (myself->sid);
setpgid (myself->pgid);
2000-02-18 03:38:33 +08:00
}
else
myself->sid = getsid ();
if (getpgid () == 0)
setpgid (myself->pgid);
2000-02-18 03:38:33 +08:00
}
}
bg_check_types
fhandler_termios::bg_check (int sig)
2000-02-18 03:38:33 +08:00
{
if (!myself->pgid || tc->getpgid () == myself->pgid ||
myself->ctty != tc->ntty ||
((sig == SIGTTOU) && !(tc->ti.c_lflag & TOSTOP)))
return bg_ok;
2000-02-18 03:38:33 +08:00
if (sig < 0)
sig = -sig;
termios_printf ("bg I/O pgid %d, tpgid %d, ctty %d",
2000-02-18 03:38:33 +08:00
myself->pgid, tc->getpgid (), myself->ctty);
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;
2000-02-18 03:38:33 +08:00
}
/* 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);
2000-02-18 03:38:33 +08:00
int sigs_ignored =
((void *) myself->getsig (sig).sa_handler == (void *) SIG_IGN) ||
2000-02-18 03:38:33 +08:00
(myself->getsigmask () & SIGTOMASK (sig));
if (pgid_gone)
goto setEIO;
else if (!sigs_ignored)
/* nothing */;
else if (sig == SIGTTOU)
return bg_ok; /* Just allow the output */
2000-02-18 03:38:33 +08:00
else
goto setEIO; /* This is an output error */
/* Don't raise a SIGTT* signal if we have already been interrupted
by another signal. */
if (WaitForSingleObject (signal_arrived, 0) != WAIT_OBJECT_0)
kill_pgrp (myself->pgid, sig);
return bg_signalled;
2000-02-18 03:38:33 +08:00
setEIO:
set_errno (EIO);
return bg_error;
2000-02-18 03:38:33 +08:00
}
#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)
2000-02-18 03:38:33 +08:00
{
line_edit_status ret = line_edit_ok;
2000-02-18 03:38:33 +08:00
char c;
int input_done = 0;
bool sawsig = false;
int iscanon = ti.c_lflag & ICANON;
2000-02-18 03:38:33 +08:00
while (nread-- > 0)
{
c = *rptr++;
termios_printf ("char %c", c);
/* Check for special chars */
if (c == '\r')
{
if (ti.c_iflag & IGNCR)
2000-02-18 03:38:33 +08:00
continue;
if (ti.c_iflag & ICRNL)
2000-02-18 03:38:33 +08:00
{
c = '\n';
set_input_done (iscanon);
}
}
else if (c == '\n')
{
if (ti.c_iflag & INLCR)
2000-02-18 03:38:33 +08:00
c = '\r';
else
set_input_done (iscanon);
}
if (ti.c_iflag & ISTRIP)
2000-02-18 03:38:33 +08:00
c &= 0x7f;
if (ti.c_lflag & ISIG)
2000-02-18 03:38:33 +08:00
{
int sig;
if (CCEQ (ti.c_cc[VINTR], c))
2000-02-18 03:38:33 +08:00
sig = SIGINT;
else if (CCEQ (ti.c_cc[VQUIT], c))
2000-02-18 03:38:33 +08:00
sig = SIGQUIT;
else if (CCEQ (ti.c_cc[VSUSP], c))
2000-02-18 03:38:33 +08:00
sig = SIGTSTP;
else
goto not_a_sig;
termios_printf ("got interrupt %d, sending signal %d", c, sig);
eat_readahead (-1);
* sync.h (new_muto): Just accept an argument which denotes the name of the muto. Use this argument to construct static storage. * cygheap.cc (cygheap_init): Reflect above change. * exceptions.cc (events_init): Ditto. * malloc.cc (malloc_init): Ditto. * path.cc (cwdstuff::init): Ditto. * cygheap.h (cwdstuff): Change name of lock element to make it less generic. * path.cc (cwdstuff::get_hash): Ditto. (cwdstuff::get_initial): Ditto. (cwdstuff::set): Ditto. (cwdstuff::get): Ditto. * sigproc.cc (proc_subproc): Ditto. * debug.cc (lock_debug): Change to method. Use method rather than macro throughout. * tty.h (tty_min::kill_pgrp): Declare new method. * fhandler_termios.cc (tty_min::kill_pgrp): New method. (fhandler_termios::line_edit): Use new method for killing process. * dcrt0.cc (do_exit): Ditto. * dtable.cc (dtable::get_debugger_info): New method for inheriting dtable info from a debugger. * tty.cc (tty_init): Attempt to grab file handle info from parent debugger, if appropriate. # dtable.cc (dtable::stdio_init): Make this a method. (dtable::init_std_file_from_handle): Don't set fd unless it's not open. (dtable::build_fhandler_from_name): Move name setting to dtable::build_fhandler. (dtable::build_fhandler): Add win32 name parameter. * dcrt0.cc (dll_crt0_1): Change to use dtable stdio_init. * dtable.h (dtable): Reflect build_fhandler parameter change. * mmap.cc (mmap_record::alloc_fh): Don't set name parameter in build_fhandler. * net.cc (fdsock): Remove set_name call since it is now handled by build_fhandler. * sigproc.cc (proc_subproc): Release muto as early as possible.
2002-02-23 03:33:41 +08:00
tc->kill_pgrp (sig);
ti.c_lflag &= ~FLUSHO;
sawsig = true;
2000-02-18 03:38:33 +08:00
goto restart_output;
}
not_a_sig:
if (ti.c_iflag & IXON)
2000-02-18 03:38:33 +08:00
{
if (CCEQ (ti.c_cc[VSTOP], c))
2000-02-18 03:38:33 +08:00
{
if (!tc->output_stopped)
{
tc->output_stopped = 1;
acquire_output_mutex (INFINITE);
}
2000-02-18 03:38:33 +08:00
continue;
}
else if (CCEQ (ti.c_cc[VSTART], c))
2000-02-18 03:38:33 +08:00
{
restart_output:
tc->output_stopped = 0;
release_output_mutex ();
2000-02-18 03:38:33 +08:00
continue;
}
else if ((ti.c_iflag & IXANY) && tc->output_stopped)
2000-02-18 03:38:33 +08:00
goto restart_output;
}
if (iscanon && ti.c_lflag & IEXTEN && CCEQ (ti.c_cc[VDISCARD], c))
2000-02-18 03:38:33 +08:00
{
ti.c_lflag ^= FLUSHO;
2000-02-18 03:38:33 +08:00
continue;
}
if (!iscanon)
/* nothing */;
else if (CCEQ (ti.c_cc[VERASE], c))
2000-02-18 03:38:33 +08:00
{
if (eat_readahead (1))
echo_erase ();
2000-02-18 03:38:33 +08:00
continue;
}
else if (CCEQ (ti.c_cc[VWERASE], c))
2000-02-18 03:38:33 +08:00
{
int ch;
do
if (!eat_readahead (1))
break;
else
echo_erase ();
2000-02-18 03:38:33 +08:00
while ((ch = peek_readahead (1)) >= 0 && !isspace (ch));
continue;
}
else if (CCEQ (ti.c_cc[VKILL], c))
2000-02-18 03:38:33 +08:00
{
int nchars = eat_readahead (-1);
if (ti.c_lflag & ECHO)
while (nchars--)
echo_erase (1);
2000-02-18 03:38:33 +08:00
continue;
}
else if (CCEQ (ti.c_cc[VREPRINT], c))
2000-02-18 03:38:33 +08:00
{
if (ti.c_lflag & ECHO)
{
doecho ("\n\r", 2);
doecho (rabuf, ralen);
}
2000-02-18 03:38:33 +08:00
continue;
}
else if (CCEQ (ti.c_cc[VEOF], c))
2000-02-18 03:38:33 +08:00
{
termios_printf ("EOF");
(void) accept_input ();
ret = line_edit_input_done;
2000-02-18 03:38:33 +08:00
continue;
}
else if (CCEQ (ti.c_cc[VEOL], c) ||
CCEQ (ti.c_cc[VEOL2], c) ||
2000-02-18 03:38:33 +08:00
c == '\n')
{
set_input_done (1);
termios_printf ("EOL");
}
if (ti.c_iflag & IUCLC && isupper (c))
c = cyg_tolower (c);
2000-02-18 03:38:33 +08:00
put_readahead (c);
if (ti.c_lflag & ECHO)
doecho (&c, 1);
if (!iscanon || input_done)
{
int status = accept_input ();
2003-01-17 13:33:52 +08:00
if (status != 1)
{
ret = status ? line_edit_error : line_edit_pipe_full;
eat_readahead (1);
break;
}
ret = line_edit_input_done;
input_done = 0;
}
2000-02-18 03:38:33 +08:00
}
if (!iscanon && ralen > 0)
ret = line_edit_input_done;
2000-02-18 03:38:33 +08:00
2001-03-05 14:28:25 +08:00
if (sawsig)
ret = line_edit_signalled;
2000-02-18 03:38:33 +08:00
return ret;
2000-02-18 03:38:33 +08:00
}
void
fhandler_termios::fixup_after_fork (HANDLE parent)
{
fhandler_base::fixup_after_fork (parent);
fork_fixup (parent, get_output_handle (), "output_handle");
}
__off64_t
* autoload.cc (LoadFuncEx): Define via new LoadFuncEx2 macro. (LoadFuncEx2): Adapted from LoadFuncEx. Provides control of return value for nonexistent function. (NtQueryObject): Declare. (IsDebuggerPresent): Declare via LoadFuncEx2 and always return true if not available. * debug.h (being_debugged): Just rely on IsDebuggerPresent return value. * dtable.cc (handle_to_fn): New function. (dtable::init_std_file_from_handle): Attempt to derive std handle's name via handle_to_fn. (dtable::build_fhandler_from_name): Fill in what we can in path_conv structure when given a handle and path doesn't exist. * fhandler.cc (fhandler_base::open): Don't set the file pointer here. Use pc->exists () to determine if file exists rather than calling GetFileAttributes again. * fhandler.h (fhandler_base::exec_state_isknown): New method. (fhandler_base::fstat_helper): Add extra arguments to declaration. (fhandler_base::fstat_by_handle): Declare new method. (fhandler_base::fstat_by_name): Declare new method. * fhandler_disk_file (num_entries): Make __stdcall. (fhandler_base::fstat_by_handle): Define new method. (fhandler_base::fstat_by_name): Define new method. (fhandler_base:fstat): Call fstat_by_{handle,name} as appropriate. (fhandler_disk_file::fstat_helper): Accept extra arguments for filling out stat structure. Move handle or name specific stuff to new methods above. (fhandler_disk_file::open): Use real_path->exists rather than calling GetFileAttributes again. * ntdll.h (FILE_NAME_INFORMATION): Define new structure. (OBJECT_INFORMATION_CLASS): Partially define new enum. (OBJECT_NAME_INFORMATION): Define new structure. (NtQueryInformationFile): New declaration. (NtQueryObject): New declaration. * path.cc (path_conv::fillin): Define new method. * path.h (path_conv::fillin): Declare new method. (path_conv::drive_thpe): Rename from 'get_drive_type'. (path_conv::volser): Declare new method. (path_conv::volname): Declare new method. (path_conv::root_dir): Declare new method. * syscalls.cc (fstat64): Send real path_conv to fstat as second argument.
2002-05-28 09:55:40 +08:00
fhandler_termios::lseek (__off64_t, int)
{
set_errno (ESPIPE);
* autoload.cc (LoadFuncEx): Define via new LoadFuncEx2 macro. (LoadFuncEx2): Adapted from LoadFuncEx. Provides control of return value for nonexistent function. (NtQueryObject): Declare. (IsDebuggerPresent): Declare via LoadFuncEx2 and always return true if not available. * debug.h (being_debugged): Just rely on IsDebuggerPresent return value. * dtable.cc (handle_to_fn): New function. (dtable::init_std_file_from_handle): Attempt to derive std handle's name via handle_to_fn. (dtable::build_fhandler_from_name): Fill in what we can in path_conv structure when given a handle and path doesn't exist. * fhandler.cc (fhandler_base::open): Don't set the file pointer here. Use pc->exists () to determine if file exists rather than calling GetFileAttributes again. * fhandler.h (fhandler_base::exec_state_isknown): New method. (fhandler_base::fstat_helper): Add extra arguments to declaration. (fhandler_base::fstat_by_handle): Declare new method. (fhandler_base::fstat_by_name): Declare new method. * fhandler_disk_file (num_entries): Make __stdcall. (fhandler_base::fstat_by_handle): Define new method. (fhandler_base::fstat_by_name): Define new method. (fhandler_base:fstat): Call fstat_by_{handle,name} as appropriate. (fhandler_disk_file::fstat_helper): Accept extra arguments for filling out stat structure. Move handle or name specific stuff to new methods above. (fhandler_disk_file::open): Use real_path->exists rather than calling GetFileAttributes again. * ntdll.h (FILE_NAME_INFORMATION): Define new structure. (OBJECT_INFORMATION_CLASS): Partially define new enum. (OBJECT_NAME_INFORMATION): Define new structure. (NtQueryInformationFile): New declaration. (NtQueryObject): New declaration. * path.cc (path_conv::fillin): Define new method. * path.h (path_conv::fillin): Declare new method. (path_conv::drive_thpe): Rename from 'get_drive_type'. (path_conv::volser): Declare new method. (path_conv::volname): Declare new method. (path_conv::root_dir): Declare new method. * syscalls.cc (fstat64): Send real path_conv to fstat as second argument.
2002-05-28 09:55:40 +08:00
return -1;
}