2000-02-18 03:38:33 +08:00
|
|
|
/* fhandler_termios.cc
|
|
|
|
|
2013-01-21 12:38:31 +08:00
|
|
|
Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009, 2010,
|
2015-03-26 00:21:38 +08:00
|
|
|
2011, 2012, 2014, 2015 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. */
|
|
|
|
|
2000-08-03 00:28:18 +08:00
|
|
|
#include "winsup.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
2000-08-22 11:58:47 +08:00
|
|
|
#include "cygerrno.h"
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 08:37:18 +08:00
|
|
|
#include "path.h"
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "fhandler.h"
|
|
|
|
#include "sigproc.h"
|
|
|
|
#include "pinfo.h"
|
2000-09-08 00:23:51 +08:00
|
|
|
#include "tty.h"
|
2005-06-30 10:52:14 +08:00
|
|
|
#include "cygtls.h"
|
2012-04-18 13:57:38 +08:00
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
|
|
|
#include "child_info.h"
|
2011-04-29 16:27:11 +08:00
|
|
|
#include "ntdll.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* Common functions shared by tty/console */
|
|
|
|
|
|
|
|
void
|
2011-06-04 08:12:29 +08:00
|
|
|
fhandler_termios::tcinit (bool is_pty_master)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
/* Initial termios values */
|
|
|
|
|
2011-06-04 08:12:29 +08:00
|
|
|
if (is_pty_master || !tc ()->initialized ())
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2011-06-04 08:12:29 +08:00
|
|
|
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;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2011-06-04 08:12:29 +08:00
|
|
|
tc ()->ti.c_cc[VDISCARD] = CFLUSH;
|
2014-11-14 02:37:15 +08:00
|
|
|
tc ()->ti.c_cc[VEOL] = CEOL;
|
2011-06-04 08:12:29 +08:00
|
|
|
tc ()->ti.c_cc[VEOL2] = CEOL2;
|
2014-11-14 02:37:15 +08:00
|
|
|
tc ()->ti.c_cc[VEOF] = CEOF;
|
2011-06-04 08:12:29 +08:00
|
|
|
tc ()->ti.c_cc[VERASE] = CERASE;
|
|
|
|
tc ()->ti.c_cc[VINTR] = CINTR;
|
|
|
|
tc ()->ti.c_cc[VKILL] = CKILL;
|
|
|
|
tc ()->ti.c_cc[VLNEXT] = CLNEXT;
|
2015-03-26 00:21:38 +08:00
|
|
|
tc ()->ti.c_cc[VMIN] = CMIN;
|
2011-06-04 08:12:29 +08:00
|
|
|
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;
|
2015-03-26 00:21:38 +08:00
|
|
|
tc ()->ti.c_cc[VTIME] = CTIME;
|
2011-06-04 08:12:29 +08:00
|
|
|
tc ()->ti.c_cc[VWERASE] = CWERASE;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2011-06-04 08:12:29 +08:00
|
|
|
tc ()->ti.c_ispeed = tc ()->ti.c_ospeed = B38400;
|
|
|
|
tc ()->pgid = is_pty_master ? 0 : myself->pgid;
|
|
|
|
tc ()->initialized (true);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_termios::tcsetpgrp (const pid_t pgid)
|
|
|
|
{
|
2012-01-08 14:24:17 +08:00
|
|
|
termios_printf ("%s, pgid %d, sid %d, tsid %d", tc ()->ttyname (), pgid,
|
2011-06-04 08:12:29 +08:00
|
|
|
myself->sid, tc ()->getsid ());
|
|
|
|
if (myself->sid != tc ()->getsid ())
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
set_errno (EPERM);
|
|
|
|
return -1;
|
|
|
|
}
|
2005-06-30 10:52:14 +08:00
|
|
|
int res;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
res = bg_check (-SIGTTOU);
|
|
|
|
|
|
|
|
switch (res)
|
|
|
|
{
|
|
|
|
case bg_ok:
|
2011-06-04 08:12:29 +08:00
|
|
|
tc ()->setpgid (pgid);
|
2011-12-17 08:03:31 +08:00
|
|
|
if (tc ()->is_console && (strace.active () || !being_debugged ()))
|
2011-10-07 03:47:45 +08:00
|
|
|
tc ()->kill_pgrp (__SIGSETPGRP);
|
2005-06-30 10:52:14 +08:00
|
|
|
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;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_termios::tcgetpgrp ()
|
|
|
|
{
|
2012-03-04 05:35:30 +08:00
|
|
|
if (myself->ctty > 0 && myself->ctty == tc ()->ntty)
|
2011-06-04 08:12:29 +08:00
|
|
|
return tc ()->pgid;
|
2009-10-07 15:52:30 +08:00
|
|
|
set_errno (ENOTTY);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_pty_master::tcgetpgrp ()
|
|
|
|
{
|
2011-06-04 08:12:29 +08:00
|
|
|
return tc ()->pgid;
|
2011-05-30 14:58:00 +08:00
|
|
|
}
|
|
|
|
|
2013-01-31 13:26:47 +08:00
|
|
|
static inline bool
|
|
|
|
is_flush_sig (int sig)
|
|
|
|
{
|
|
|
|
return sig == SIGINT || sig == SIGQUIT || sig == SIGTSTP;
|
|
|
|
}
|
|
|
|
|
2002-02-23 03:33:41 +08:00
|
|
|
void
|
|
|
|
tty_min::kill_pgrp (int sig)
|
|
|
|
{
|
2011-12-08 12:09:22 +08:00
|
|
|
bool killself = false;
|
2013-01-31 13:26:47 +08:00
|
|
|
if (is_flush_sig (sig) && cygheap->ctty)
|
|
|
|
cygheap->ctty->sigflush ();
|
2003-09-17 09:15:56 +08:00
|
|
|
winpids pids ((DWORD) PID_MAP_RW);
|
2006-02-07 02:24:11 +08:00
|
|
|
siginfo_t si = {0};
|
2004-01-19 13:46:54 +08:00
|
|
|
si.si_signo = sig;
|
|
|
|
si.si_code = SI_KERNEL;
|
2013-01-31 13:26:47 +08:00
|
|
|
|
2002-02-23 03:33:41 +08:00
|
|
|
for (unsigned i = 0; i < pids.npids; i++)
|
|
|
|
{
|
|
|
|
_pinfo *p = pids[i];
|
2016-04-05 16:26:06 +08:00
|
|
|
if (!p->exists () || p->ctty != ntty || p->pgid != pgid)
|
2002-02-23 03:33:41 +08:00
|
|
|
continue;
|
|
|
|
if (p == myself)
|
2013-10-19 04:56:19 +08:00
|
|
|
killself = sig != __SIGSETPGRP && !exit_state;
|
2002-02-23 03:33:41 +08:00
|
|
|
else
|
2005-07-07 04:05:03 +08:00
|
|
|
sig_send (p, si);
|
2002-02-23 03:33:41 +08:00
|
|
|
}
|
|
|
|
if (killself)
|
2004-01-19 13:46:54 +08:00
|
|
|
sig_send (myself, si);
|
2002-02-23 03:33:41 +08:00
|
|
|
}
|
2002-05-28 09:55:40 +08:00
|
|
|
|
2011-06-04 08:12:29 +08:00
|
|
|
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)
|
2011-06-06 13:02:13 +08:00
|
|
|
continue;
|
2011-06-04 08:12:29 +08:00
|
|
|
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)
|
2011-06-06 13:02:13 +08:00
|
|
|
return 0;
|
2011-06-04 08:12:29 +08:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-01-17 22:57:09 +08:00
|
|
|
bg_check_types
|
2000-02-21 13:20:38 +08:00
|
|
|
fhandler_termios::bg_check (int sig)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2013-01-31 13:26:47 +08:00
|
|
|
if (!myself->pgid || !tc () || tc ()->getpgid () == myself->pgid ||
|
2011-06-04 08:12:29 +08:00
|
|
|
myself->ctty != tc ()->ntty ||
|
|
|
|
((sig == SIGTTOU) && !(tc ()->ti.c_lflag & TOSTOP)))
|
2001-01-17 22:57:09 +08:00
|
|
|
return bg_ok;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
if (sig < 0)
|
|
|
|
sig = -sig;
|
|
|
|
|
2012-01-08 14:24:17 +08:00
|
|
|
termios_printf ("%s, bg I/O pgid %d, tpgid %d, myctty %s", tc ()->ttyname (),
|
|
|
|
myself->pgid, tc ()->getpgid (), myctty ());
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2011-06-04 08:12:29 +08:00
|
|
|
if (tc ()->getsid () == 0)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
/* 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");
|
2001-01-17 22:57:09 +08:00
|
|
|
return bg_eof;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2014-11-29 04:46:13 +08:00
|
|
|
threadlist_t *tl_entry;
|
|
|
|
tl_entry = cygheap->find_tls (_main_tls);
|
2000-02-18 03:38:33 +08:00
|
|
|
int sigs_ignored =
|
2003-11-29 06:13:57 +08:00
|
|
|
((void *) global_sigs[sig].sa_handler == (void *) SIG_IGN) ||
|
2008-02-16 01:53:11 +08:00
|
|
|
(_main_tls->sigmask & SIGTOMASK (sig));
|
2014-11-29 04:46:13 +08:00
|
|
|
cygheap->unlock_tls (tl_entry);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2011-05-30 14:58:00 +08:00
|
|
|
/* 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 */
|
2011-06-04 08:12:29 +08:00
|
|
|
else if (tc ()->is_orphaned_process_group (myself->pgid))
|
2004-01-19 13:46:54 +08:00
|
|
|
{
|
2011-05-30 14:58:00 +08:00
|
|
|
termios_printf ("process group is orphaned");
|
|
|
|
set_errno (EIO); /* This is an IO error */
|
|
|
|
return bg_error;
|
2004-01-19 13:46:54 +08:00
|
|
|
}
|
2011-05-30 14:58:00 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Don't raise a SIGTT* signal if we have already been
|
|
|
|
interrupted by another signal. */
|
2012-07-22 06:58:20 +08:00
|
|
|
if (cygwait ((DWORD) 0) != WAIT_SIGNALED)
|
2011-05-30 14:58:00 +08:00
|
|
|
{
|
|
|
|
siginfo_t si = {0};
|
|
|
|
si.si_signo = sig;
|
|
|
|
si.si_code = SI_KERNEL;
|
|
|
|
kill_pgrp (myself->pgid, si);
|
|
|
|
}
|
|
|
|
return bg_signalled;
|
2011-06-06 13:02:13 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define set_input_done(x) input_done = input_done || (x)
|
|
|
|
|
2001-05-10 02:53:55 +08:00
|
|
|
inline void
|
|
|
|
fhandler_termios::echo_erase (int force)
|
|
|
|
{
|
2011-06-04 08:12:29 +08:00
|
|
|
if (force || tc ()->ti.c_lflag & ECHO)
|
2001-05-10 02:53:55 +08:00
|
|
|
doecho ("\b \b", 3);
|
|
|
|
}
|
|
|
|
|
2002-12-06 00:24:52 +08:00
|
|
|
line_edit_status
|
2014-11-14 02:37:15 +08:00
|
|
|
fhandler_termios::line_edit (const char *rptr, size_t nread, termios& ti,
|
|
|
|
ssize_t *bytes_read)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2002-12-06 00:24:52 +08:00
|
|
|
line_edit_status ret = line_edit_ok;
|
2000-02-18 03:38:33 +08:00
|
|
|
char c;
|
|
|
|
int input_done = 0;
|
2002-12-21 12:38:12 +08:00
|
|
|
bool sawsig = false;
|
2002-12-27 11:50:29 +08:00
|
|
|
int iscanon = ti.c_lflag & ICANON;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2014-11-14 18:57:45 +08:00
|
|
|
if (bytes_read)
|
2014-11-14 02:37:15 +08:00
|
|
|
*bytes_read = nread;
|
2000-02-18 03:38:33 +08:00
|
|
|
while (nread-- > 0)
|
|
|
|
{
|
|
|
|
c = *rptr++;
|
|
|
|
|
2014-11-14 02:37:15 +08:00
|
|
|
paranoid_printf ("char %0o", c);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-12-27 11:50:29 +08:00
|
|
|
if (ti.c_iflag & ISTRIP)
|
2000-02-18 03:38:33 +08:00
|
|
|
c &= 0x7f;
|
2002-12-27 11:50:29 +08:00
|
|
|
if (ti.c_lflag & ISIG)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
int sig;
|
2002-12-27 11:50:29 +08:00
|
|
|
if (CCEQ (ti.c_cc[VINTR], c))
|
2000-02-18 03:38:33 +08:00
|
|
|
sig = SIGINT;
|
2002-12-27 11:50:29 +08:00
|
|
|
else if (CCEQ (ti.c_cc[VQUIT], c))
|
2000-02-18 03:38:33 +08:00
|
|
|
sig = SIGQUIT;
|
2002-12-27 11:50:29 +08:00
|
|
|
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);
|
2001-03-19 02:05:01 +08:00
|
|
|
eat_readahead (-1);
|
2011-06-04 08:12:29 +08:00
|
|
|
tc ()->kill_pgrp (sig);
|
2002-12-27 11:50:29 +08:00
|
|
|
ti.c_lflag &= ~FLUSHO;
|
2002-12-21 12:38:12 +08:00
|
|
|
sawsig = true;
|
2000-02-18 03:38:33 +08:00
|
|
|
goto restart_output;
|
|
|
|
}
|
|
|
|
not_a_sig:
|
2002-12-27 11:50:29 +08:00
|
|
|
if (ti.c_iflag & IXON)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2002-12-27 11:50:29 +08:00
|
|
|
if (CCEQ (ti.c_cc[VSTOP], c))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2011-06-04 08:12:29 +08:00
|
|
|
if (!tc ()->output_stopped)
|
2012-10-12 09:19:04 +08:00
|
|
|
tc ()->output_stopped = true;
|
2000-02-18 03:38:33 +08:00
|
|
|
continue;
|
|
|
|
}
|
2002-12-27 11:50:29 +08:00
|
|
|
else if (CCEQ (ti.c_cc[VSTART], c))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
restart_output:
|
2012-10-12 09:19:04 +08:00
|
|
|
tc ()->output_stopped = false;
|
2000-02-18 03:38:33 +08:00
|
|
|
continue;
|
|
|
|
}
|
2011-06-04 08:12:29 +08:00
|
|
|
else if ((ti.c_iflag & IXANY) && tc ()->output_stopped)
|
2000-02-18 03:38:33 +08:00
|
|
|
goto restart_output;
|
|
|
|
}
|
2012-11-09 01:27:00 +08:00
|
|
|
/* 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);
|
|
|
|
}
|
2013-04-23 17:44:36 +08:00
|
|
|
|
2002-12-27 11:50:29 +08:00
|
|
|
if (iscanon && ti.c_lflag & IEXTEN && CCEQ (ti.c_cc[VDISCARD], c))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2002-12-27 11:50:29 +08:00
|
|
|
ti.c_lflag ^= FLUSHO;
|
2000-02-18 03:38:33 +08:00
|
|
|
continue;
|
|
|
|
}
|
2012-11-09 01:27:00 +08:00
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
if (!iscanon)
|
|
|
|
/* nothing */;
|
2002-12-27 11:50:29 +08:00
|
|
|
else if (CCEQ (ti.c_cc[VERASE], c))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
if (eat_readahead (1))
|
2001-05-10 02:53:55 +08:00
|
|
|
echo_erase ();
|
2000-02-18 03:38:33 +08:00
|
|
|
continue;
|
|
|
|
}
|
2002-12-27 11:50:29 +08:00
|
|
|
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
|
2001-05-10 02:53:55 +08:00
|
|
|
echo_erase ();
|
2000-02-18 03:38:33 +08:00
|
|
|
while ((ch = peek_readahead (1)) >= 0 && !isspace (ch));
|
|
|
|
continue;
|
|
|
|
}
|
2002-12-27 11:50:29 +08:00
|
|
|
else if (CCEQ (ti.c_cc[VKILL], c))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
int nchars = eat_readahead (-1);
|
2002-12-27 11:50:29 +08:00
|
|
|
if (ti.c_lflag & ECHO)
|
2001-05-10 02:53:55 +08:00
|
|
|
while (nchars--)
|
|
|
|
echo_erase (1);
|
2000-02-18 03:38:33 +08:00
|
|
|
continue;
|
|
|
|
}
|
2002-12-27 11:50:29 +08:00
|
|
|
else if (CCEQ (ti.c_cc[VREPRINT], c))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2002-12-27 11:50:29 +08:00
|
|
|
if (ti.c_lflag & ECHO)
|
2001-05-10 02:53:55 +08:00
|
|
|
{
|
|
|
|
doecho ("\n\r", 2);
|
|
|
|
doecho (rabuf, ralen);
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
continue;
|
|
|
|
}
|
2002-12-27 11:50:29 +08:00
|
|
|
else if (CCEQ (ti.c_cc[VEOF], c))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
termios_printf ("EOF");
|
2005-07-07 04:05:03 +08:00
|
|
|
accept_input ();
|
2002-12-15 03:40:41 +08:00
|
|
|
ret = line_edit_input_done;
|
2000-02-18 03:38:33 +08:00
|
|
|
continue;
|
|
|
|
}
|
2002-12-27 11:50:29 +08:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2002-12-27 11:50:29 +08:00
|
|
|
if (ti.c_iflag & IUCLC && isupper (c))
|
2000-10-19 11:12:44 +08:00
|
|
|
c = cyg_tolower (c);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-12-06 00:24:52 +08:00
|
|
|
put_readahead (c);
|
2002-12-28 14:30:47 +08:00
|
|
|
if (ti.c_lflag & ECHO)
|
|
|
|
doecho (&c, 1);
|
2014-11-14 02:37:15 +08:00
|
|
|
/* Write in chunks of 32 bytes to reduce the number of WriteFile calls
|
|
|
|
in non-canonical mode. */
|
|
|
|
if ((!iscanon && ralen >= 32) || input_done)
|
2002-12-15 03:40:41 +08:00
|
|
|
{
|
2002-12-20 09:38:55 +08:00
|
|
|
int status = accept_input ();
|
2003-01-17 13:33:52 +08:00
|
|
|
if (status != 1)
|
2002-12-17 11:49:34 +08:00
|
|
|
{
|
2002-12-20 09:38:55 +08:00
|
|
|
ret = status ? line_edit_error : line_edit_pipe_full;
|
2014-11-14 02:37:15 +08:00
|
|
|
nread += ralen;
|
2002-12-17 11:49:34 +08:00
|
|
|
break;
|
|
|
|
}
|
2002-12-15 03:40:41 +08:00
|
|
|
ret = line_edit_input_done;
|
|
|
|
input_done = 0;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2014-11-14 02:37:15 +08:00
|
|
|
/* If we didn't write all bytes in non-canonical mode, write them now. */
|
2015-02-26 23:58:04 +08:00
|
|
|
if (!iscanon && ralen > 0
|
|
|
|
&& (ret == line_edit_ok || ret == line_edit_input_done))
|
2014-11-14 02:37:15 +08:00
|
|
|
{
|
2015-02-26 23:58:04 +08:00
|
|
|
int status = accept_input ();
|
|
|
|
if (status != 1)
|
|
|
|
{
|
|
|
|
ret = status ? line_edit_error : line_edit_pipe_full;
|
|
|
|
nread += ralen;
|
2014-11-14 02:37:15 +08:00
|
|
|
}
|
2015-02-26 23:58:04 +08:00
|
|
|
else
|
|
|
|
ret = line_edit_input_done;
|
2014-11-14 02:37:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Adding one compensates for the postdecrement in the above loop. */
|
2014-11-14 18:57:45 +08:00
|
|
|
if (bytes_read)
|
2014-11-14 02:37:15 +08:00
|
|
|
*bytes_read -= (nread + 1);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-03-05 14:28:25 +08:00
|
|
|
if (sawsig)
|
2002-12-06 00:24:52 +08:00
|
|
|
ret = line_edit_signalled;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-12-06 00:24:52 +08:00
|
|
|
return ret;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2001-03-13 05:27:42 +08:00
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
off_t
|
|
|
|
fhandler_termios::lseek (off_t, int)
|
2002-03-19 12:39:01 +08:00
|
|
|
{
|
|
|
|
set_errno (ESPIPE);
|
2002-05-28 09:55:40 +08:00
|
|
|
return -1;
|
2002-03-19 12:39:01 +08:00
|
|
|
}
|
2011-04-18 03:56:25 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_termios::sigflush ()
|
|
|
|
{
|
2011-04-21 08:53:55 +08:00
|
|
|
/* 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. */
|
2013-01-31 13:26:47 +08:00
|
|
|
if ((!have_execed || have_execed_cygwin) && tc ()
|
|
|
|
&& (tc ()->getpgid () == myself->pgid)
|
|
|
|
&& !(tc ()->ti.c_lflag & NOFLSH))
|
2011-04-18 03:56:25 +08:00
|
|
|
tcflush (TCIFLUSH);
|
|
|
|
}
|
2011-10-12 07:20:38 +08:00
|
|
|
|
2012-02-28 22:03:03 +08:00
|
|
|
pid_t
|
2011-10-12 07:20:38 +08:00
|
|
|
fhandler_termios::tcgetsid ()
|
|
|
|
{
|
2012-03-04 05:35:30 +08:00
|
|
|
if (myself->ctty > 0 && myself->ctty == tc ()->ntty)
|
2011-10-12 07:20:38 +08:00
|
|
|
return tc ()->getsid ();
|
|
|
|
set_errno (ENOTTY);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-10-16 06:37:30 +08:00
|
|
|
fhandler_termios::ioctl (int cmd, void *varg)
|
2011-10-12 07:20:38 +08:00
|
|
|
{
|
|
|
|
if (cmd != TIOCSCTTY)
|
|
|
|
return 1; /* Not handled by this function */
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
int arg = (int) (intptr_t) varg;
|
2011-10-16 06:37:30 +08:00
|
|
|
|
2011-10-12 07:20:38 +08:00
|
|
|
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;
|
2011-10-16 06:37:30 +08:00
|
|
|
myself->set_ctty (this, 0);
|
2011-10-12 07:20:38 +08:00
|
|
|
return 0;
|
|
|
|
}
|