2000-02-18 03:38:33 +08:00
|
|
|
/* fhandler_tty.cc
|
|
|
|
|
|
|
|
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>
|
2012-03-08 17:36:11 +08:00
|
|
|
#include <sys/param.h>
|
2016-01-07 01:41:36 +08:00
|
|
|
#include <cygwin/acl.h>
|
2006-07-03 23:29:10 +08:00
|
|
|
#include <cygwin/kd.h>
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "cygerrno.h"
|
2001-07-27 03:22:24 +08:00
|
|
|
#include "security.h"
|
2001-10-01 12:10:07 +08:00
|
|
|
#include "path.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 "fhandler.h"
|
2000-08-12 13:35:42 +08:00
|
|
|
#include "dtable.h"
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "sigproc.h"
|
2000-08-12 13:47:12 +08:00
|
|
|
#include "pinfo.h"
|
2010-04-20 03:52:43 +08:00
|
|
|
#include "ntdll.h"
|
2000-09-03 12:16:35 +08:00
|
|
|
#include "cygheap.h"
|
2000-09-08 00:23:51 +08:00
|
|
|
#include "shared_info.h"
|
2002-08-02 00:20:31 +08:00
|
|
|
#include "cygthread.h"
|
2006-06-02 23:41:34 +08:00
|
|
|
#include "child_info.h"
|
2011-07-23 02:50:42 +08:00
|
|
|
#include <asm/socket.h>
|
2012-06-18 04:50:24 +08:00
|
|
|
#include "cygwait.h"
|
2019-08-28 02:04:02 +08:00
|
|
|
#include "tls_pbuf.h"
|
2019-11-07 00:29:29 +08:00
|
|
|
#include "registry.h"
|
2019-08-28 02:04:02 +08:00
|
|
|
|
|
|
|
#ifndef PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE
|
|
|
|
#define PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE 0x00020016
|
|
|
|
#endif /* PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE */
|
|
|
|
|
|
|
|
extern "C" int sscanf (const char *, const char *, ...);
|
2019-09-15 12:36:23 +08:00
|
|
|
extern "C" int ttyname_r (int, char*, size_t);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2019-09-15 18:55:44 +08:00
|
|
|
extern "C" {
|
|
|
|
HRESULT WINAPI CreatePseudoConsole (COORD, HANDLE, HANDLE, DWORD, HPCON *);
|
|
|
|
HRESULT WINAPI ResizePseudoConsole (HPCON, COORD);
|
|
|
|
VOID WINAPI ClosePseudoConsole (HPCON);
|
|
|
|
}
|
|
|
|
|
2010-04-20 03:52:43 +08:00
|
|
|
#define close_maybe(h) \
|
|
|
|
do { \
|
|
|
|
if (h && h != INVALID_HANDLE_VALUE) \
|
|
|
|
CloseHandle (h); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/* pty master control pipe messages */
|
|
|
|
struct pipe_request {
|
|
|
|
DWORD pid;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct pipe_reply {
|
|
|
|
HANDLE from_master;
|
2019-08-28 02:04:02 +08:00
|
|
|
HANDLE from_master_cyg;
|
2010-04-20 03:52:43 +08:00
|
|
|
HANDLE to_master;
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
HANDLE to_master_cyg;
|
2010-04-20 03:52:43 +08:00
|
|
|
DWORD error;
|
|
|
|
};
|
|
|
|
|
2019-09-04 09:45:35 +08:00
|
|
|
static int pcon_attached_to = -1;
|
2019-08-28 02:04:02 +08:00
|
|
|
static bool isHybrid;
|
2019-09-08 21:23:23 +08:00
|
|
|
static bool do_not_reset_switch_to_pcon;
|
2020-01-10 19:46:26 +08:00
|
|
|
static bool freeconsole_on_close = true;
|
2019-08-28 02:04:02 +08:00
|
|
|
|
2020-02-09 22:47:30 +08:00
|
|
|
void
|
|
|
|
clear_pcon_attached_to (void)
|
|
|
|
{
|
|
|
|
pcon_attached_to = -1;
|
|
|
|
}
|
|
|
|
|
2019-08-28 02:04:02 +08:00
|
|
|
static void
|
|
|
|
set_switch_to_pcon (void)
|
|
|
|
{
|
|
|
|
cygheap_fdenum cfd (false);
|
2019-09-14 05:48:14 +08:00
|
|
|
int fd;
|
|
|
|
while ((fd = cfd.next ()) >= 0)
|
2019-08-28 02:04:02 +08:00
|
|
|
if (cfd->get_major () == DEV_PTYS_MAJOR)
|
|
|
|
{
|
|
|
|
fhandler_base *fh = cfd;
|
|
|
|
fhandler_pty_slave *ptys = (fhandler_pty_slave *) fh;
|
2019-11-16 07:27:24 +08:00
|
|
|
if (ptys->get_pseudo_console ())
|
2020-02-09 22:46:01 +08:00
|
|
|
{
|
|
|
|
ptys->set_switch_to_pcon (fd);
|
|
|
|
ptys->trigger_redraw_screen ();
|
|
|
|
DWORD mode =
|
|
|
|
ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
|
|
|
|
SetConsoleMode (ptys->get_handle (), mode);
|
|
|
|
}
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 21:22:27 +08:00
|
|
|
static void
|
|
|
|
force_attach_to_pcon (HANDLE h)
|
|
|
|
{
|
|
|
|
bool attach_done = false;
|
|
|
|
for (int n = 0; n < 2; n ++)
|
|
|
|
{
|
|
|
|
/* First time, attach to the pty whose handle value is match.
|
|
|
|
Second time, try to attach to any pty. */
|
|
|
|
cygheap_fdenum cfd (false);
|
|
|
|
while (cfd.next () >= 0)
|
|
|
|
if (cfd->get_major () == DEV_PTYS_MAJOR)
|
|
|
|
{
|
|
|
|
fhandler_base *fh = cfd;
|
|
|
|
fhandler_pty_slave *ptys = (fhandler_pty_slave *) fh;
|
2019-11-16 07:27:24 +08:00
|
|
|
if (!ptys->get_pseudo_console ())
|
2019-09-18 22:29:21 +08:00
|
|
|
continue;
|
2019-09-05 21:22:27 +08:00
|
|
|
if (n != 0
|
|
|
|
|| h == ptys->get_handle ()
|
|
|
|
|| h == ptys->get_output_handle ())
|
|
|
|
{
|
|
|
|
if (fhandler_console::get_console_process_id
|
2019-11-16 07:27:24 +08:00
|
|
|
(ptys->get_helper_process_id (), true))
|
2019-09-05 21:22:27 +08:00
|
|
|
attach_done = true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FreeConsole ();
|
2019-11-16 07:27:24 +08:00
|
|
|
if (AttachConsole (ptys->get_helper_process_id ()))
|
2019-09-05 21:22:27 +08:00
|
|
|
{
|
|
|
|
pcon_attached_to = ptys->get_minor ();
|
|
|
|
attach_done = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pcon_attached_to = -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-09-14 05:48:14 +08:00
|
|
|
else if (cfd->get_major () == DEV_CONS_MAJOR)
|
|
|
|
{
|
|
|
|
fhandler_base *fh = cfd;
|
|
|
|
fhandler_console *cons = (fhandler_console *) fh;
|
|
|
|
if (n != 0
|
|
|
|
|| h == cons->get_handle ()
|
|
|
|
|| h == cons->get_output_handle ())
|
|
|
|
{
|
|
|
|
/* If the process is running on a console,
|
|
|
|
the parent process should be attached
|
|
|
|
to the same console. */
|
2019-09-18 22:29:17 +08:00
|
|
|
DWORD attach_wpid;
|
|
|
|
if (myself->ppid == 1)
|
|
|
|
attach_wpid = ATTACH_PARENT_PROCESS;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pinfo p (myself->ppid);
|
|
|
|
attach_wpid = p->dwProcessId;
|
|
|
|
}
|
2019-09-14 05:48:14 +08:00
|
|
|
FreeConsole ();
|
2019-09-18 22:29:17 +08:00
|
|
|
if (AttachConsole (attach_wpid))
|
2019-09-14 05:48:14 +08:00
|
|
|
{
|
|
|
|
pcon_attached_to = -1;
|
|
|
|
attach_done = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pcon_attached_to = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-09-05 21:22:27 +08:00
|
|
|
if (attach_done)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-04 21:46:51 +08:00
|
|
|
void
|
|
|
|
set_ishybrid_and_switch_to_pcon (HANDLE h)
|
|
|
|
{
|
2019-09-05 21:22:27 +08:00
|
|
|
if (GetFileType (h) == FILE_TYPE_CHAR)
|
2019-09-04 21:46:51 +08:00
|
|
|
{
|
2019-09-05 21:22:27 +08:00
|
|
|
force_attach_to_pcon (h);
|
|
|
|
DWORD dummy;
|
|
|
|
if (!isHybrid && (GetConsoleMode (h, &dummy)
|
|
|
|
|| GetLastError () != ERROR_INVALID_HANDLE))
|
|
|
|
{
|
|
|
|
isHybrid = true;
|
|
|
|
set_switch_to_pcon ();
|
|
|
|
}
|
2019-09-04 21:46:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 02:04:02 +08:00
|
|
|
#define DEF_HOOK(name) static __typeof__ (name) *name##_Orig
|
|
|
|
DEF_HOOK (WriteFile);
|
|
|
|
DEF_HOOK (WriteConsoleA);
|
|
|
|
DEF_HOOK (WriteConsoleW);
|
|
|
|
DEF_HOOK (ReadFile);
|
|
|
|
DEF_HOOK (ReadConsoleA);
|
|
|
|
DEF_HOOK (ReadConsoleW);
|
|
|
|
DEF_HOOK (WriteConsoleOutputA);
|
|
|
|
DEF_HOOK (WriteConsoleOutputW);
|
|
|
|
DEF_HOOK (WriteConsoleOutputCharacterA);
|
|
|
|
DEF_HOOK (WriteConsoleOutputCharacterW);
|
|
|
|
DEF_HOOK (WriteConsoleOutputAttribute);
|
2019-09-06 21:01:27 +08:00
|
|
|
DEF_HOOK (SetConsoleCursorPosition);
|
2019-09-04 21:46:51 +08:00
|
|
|
DEF_HOOK (SetConsoleTextAttribute);
|
2019-08-28 02:04:02 +08:00
|
|
|
DEF_HOOK (WriteConsoleInputA);
|
|
|
|
DEF_HOOK (WriteConsoleInputW);
|
|
|
|
DEF_HOOK (ReadConsoleInputA);
|
|
|
|
DEF_HOOK (ReadConsoleInputW);
|
|
|
|
DEF_HOOK (PeekConsoleInputA);
|
|
|
|
DEF_HOOK (PeekConsoleInputW);
|
2019-09-18 22:29:18 +08:00
|
|
|
/* CreateProcess() is hooked for GDB etc. */
|
|
|
|
DEF_HOOK (CreateProcessA);
|
|
|
|
DEF_HOOK (CreateProcessW);
|
2019-08-28 02:04:02 +08:00
|
|
|
|
|
|
|
static BOOL WINAPI
|
|
|
|
WriteFile_Hooked
|
|
|
|
(HANDLE h, LPCVOID p, DWORD l, LPDWORD n, LPOVERLAPPED o)
|
|
|
|
{
|
2019-09-04 21:46:51 +08:00
|
|
|
set_ishybrid_and_switch_to_pcon (h);
|
2019-08-28 02:04:02 +08:00
|
|
|
return WriteFile_Orig (h, p, l, n, o);
|
|
|
|
}
|
|
|
|
static BOOL WINAPI
|
|
|
|
WriteConsoleA_Hooked
|
|
|
|
(HANDLE h, LPCVOID p, DWORD l, LPDWORD n, LPVOID o)
|
|
|
|
{
|
2019-09-04 21:46:51 +08:00
|
|
|
set_ishybrid_and_switch_to_pcon (h);
|
2019-08-28 02:04:02 +08:00
|
|
|
return WriteConsoleA_Orig (h, p, l, n, o);
|
|
|
|
}
|
|
|
|
static BOOL WINAPI
|
|
|
|
WriteConsoleW_Hooked
|
|
|
|
(HANDLE h, LPCVOID p, DWORD l, LPDWORD n, LPVOID o)
|
|
|
|
{
|
2019-09-04 21:46:51 +08:00
|
|
|
set_ishybrid_and_switch_to_pcon (h);
|
2019-08-28 02:04:02 +08:00
|
|
|
return WriteConsoleW_Orig (h, p, l, n, o);
|
|
|
|
}
|
|
|
|
static BOOL WINAPI
|
|
|
|
ReadFile_Hooked
|
|
|
|
(HANDLE h, LPVOID p, DWORD l, LPDWORD n, LPOVERLAPPED o)
|
|
|
|
{
|
2019-09-04 21:46:51 +08:00
|
|
|
set_ishybrid_and_switch_to_pcon (h);
|
2019-08-28 02:04:02 +08:00
|
|
|
return ReadFile_Orig (h, p, l, n, o);
|
|
|
|
}
|
|
|
|
static BOOL WINAPI
|
|
|
|
ReadConsoleA_Hooked
|
|
|
|
(HANDLE h, LPVOID p, DWORD l, LPDWORD n, LPVOID o)
|
|
|
|
{
|
2019-09-04 21:46:51 +08:00
|
|
|
set_ishybrid_and_switch_to_pcon (h);
|
2019-08-28 02:04:02 +08:00
|
|
|
return ReadConsoleA_Orig (h, p, l, n, o);
|
|
|
|
}
|
|
|
|
static BOOL WINAPI
|
|
|
|
ReadConsoleW_Hooked
|
|
|
|
(HANDLE h, LPVOID p, DWORD l, LPDWORD n, LPVOID o)
|
|
|
|
{
|
2019-09-04 21:46:51 +08:00
|
|
|
set_ishybrid_and_switch_to_pcon (h);
|
2019-08-28 02:04:02 +08:00
|
|
|
return ReadConsoleW_Orig (h, p, l, n, o);
|
|
|
|
}
|
|
|
|
static BOOL WINAPI
|
|
|
|
WriteConsoleOutputA_Hooked
|
|
|
|
(HANDLE h, CONST CHAR_INFO *p, COORD s, COORD c, PSMALL_RECT r)
|
|
|
|
{
|
2019-09-04 21:46:51 +08:00
|
|
|
set_ishybrid_and_switch_to_pcon (h);
|
2019-08-28 02:04:02 +08:00
|
|
|
return WriteConsoleOutputA_Orig (h, p, s, c, r);
|
|
|
|
}
|
|
|
|
static BOOL WINAPI
|
|
|
|
WriteConsoleOutputW_Hooked
|
|
|
|
(HANDLE h, CONST CHAR_INFO *p, COORD s, COORD c, PSMALL_RECT r)
|
|
|
|
{
|
2019-09-04 21:46:51 +08:00
|
|
|
set_ishybrid_and_switch_to_pcon (h);
|
2019-08-28 02:04:02 +08:00
|
|
|
return WriteConsoleOutputW_Orig (h, p, s, c, r);
|
|
|
|
}
|
|
|
|
static BOOL WINAPI
|
|
|
|
WriteConsoleOutputCharacterA_Hooked
|
|
|
|
(HANDLE h, LPCSTR p, DWORD l, COORD c, LPDWORD n)
|
|
|
|
{
|
2019-09-04 21:46:51 +08:00
|
|
|
set_ishybrid_and_switch_to_pcon (h);
|
2019-08-28 02:04:02 +08:00
|
|
|
return WriteConsoleOutputCharacterA_Orig (h, p, l, c, n);
|
|
|
|
}
|
|
|
|
static BOOL WINAPI
|
|
|
|
WriteConsoleOutputCharacterW_Hooked
|
|
|
|
(HANDLE h, LPCWSTR p, DWORD l, COORD c, LPDWORD n)
|
|
|
|
{
|
2019-09-04 21:46:51 +08:00
|
|
|
set_ishybrid_and_switch_to_pcon (h);
|
2019-08-28 02:04:02 +08:00
|
|
|
return WriteConsoleOutputCharacterW_Orig (h, p, l, c, n);
|
|
|
|
}
|
|
|
|
static BOOL WINAPI
|
|
|
|
WriteConsoleOutputAttribute_Hooked
|
|
|
|
(HANDLE h, CONST WORD *a, DWORD l, COORD c, LPDWORD n)
|
|
|
|
{
|
2019-09-04 21:46:51 +08:00
|
|
|
set_ishybrid_and_switch_to_pcon (h);
|
2019-08-28 02:04:02 +08:00
|
|
|
return WriteConsoleOutputAttribute_Orig (h, a, l, c, n);
|
|
|
|
}
|
|
|
|
static BOOL WINAPI
|
2019-09-06 21:01:27 +08:00
|
|
|
SetConsoleCursorPosition_Hooked
|
|
|
|
(HANDLE h, COORD c)
|
|
|
|
{
|
|
|
|
set_ishybrid_and_switch_to_pcon (h);
|
|
|
|
return SetConsoleCursorPosition_Orig (h, c);
|
|
|
|
}
|
|
|
|
static BOOL WINAPI
|
2019-09-04 21:46:51 +08:00
|
|
|
SetConsoleTextAttribute_Hooked
|
|
|
|
(HANDLE h, WORD a)
|
|
|
|
{
|
|
|
|
set_ishybrid_and_switch_to_pcon (h);
|
|
|
|
return SetConsoleTextAttribute_Orig (h, a);
|
|
|
|
}
|
|
|
|
static BOOL WINAPI
|
2019-08-28 02:04:02 +08:00
|
|
|
WriteConsoleInputA_Hooked
|
|
|
|
(HANDLE h, CONST INPUT_RECORD *r, DWORD l, LPDWORD n)
|
|
|
|
{
|
2019-09-04 21:46:51 +08:00
|
|
|
set_ishybrid_and_switch_to_pcon (h);
|
2019-08-28 02:04:02 +08:00
|
|
|
return WriteConsoleInputA_Orig (h, r, l, n);
|
|
|
|
}
|
|
|
|
static BOOL WINAPI
|
|
|
|
WriteConsoleInputW_Hooked
|
|
|
|
(HANDLE h, CONST INPUT_RECORD *r, DWORD l, LPDWORD n)
|
|
|
|
{
|
2019-09-04 21:46:51 +08:00
|
|
|
set_ishybrid_and_switch_to_pcon (h);
|
2019-08-28 02:04:02 +08:00
|
|
|
return WriteConsoleInputW_Orig (h, r, l, n);
|
|
|
|
}
|
|
|
|
static BOOL WINAPI
|
|
|
|
ReadConsoleInputA_Hooked
|
|
|
|
(HANDLE h, PINPUT_RECORD r, DWORD l, LPDWORD n)
|
|
|
|
{
|
2019-09-04 21:46:51 +08:00
|
|
|
set_ishybrid_and_switch_to_pcon (h);
|
2019-08-28 02:04:02 +08:00
|
|
|
return ReadConsoleInputA_Orig (h, r, l, n);
|
|
|
|
}
|
|
|
|
static BOOL WINAPI
|
|
|
|
ReadConsoleInputW_Hooked
|
|
|
|
(HANDLE h, PINPUT_RECORD r, DWORD l, LPDWORD n)
|
|
|
|
{
|
2019-09-04 21:46:51 +08:00
|
|
|
set_ishybrid_and_switch_to_pcon (h);
|
2019-08-28 02:04:02 +08:00
|
|
|
return ReadConsoleInputW_Orig (h, r, l, n);
|
|
|
|
}
|
|
|
|
static BOOL WINAPI
|
|
|
|
PeekConsoleInputA_Hooked
|
|
|
|
(HANDLE h, PINPUT_RECORD r, DWORD l, LPDWORD n)
|
|
|
|
{
|
2019-09-04 21:46:51 +08:00
|
|
|
set_ishybrid_and_switch_to_pcon (h);
|
2019-08-28 02:04:02 +08:00
|
|
|
return PeekConsoleInputA_Orig (h, r, l, n);
|
|
|
|
}
|
|
|
|
static BOOL WINAPI
|
|
|
|
PeekConsoleInputW_Hooked
|
|
|
|
(HANDLE h, PINPUT_RECORD r, DWORD l, LPDWORD n)
|
|
|
|
{
|
2019-09-04 21:46:51 +08:00
|
|
|
set_ishybrid_and_switch_to_pcon (h);
|
2019-08-28 02:04:02 +08:00
|
|
|
return PeekConsoleInputW_Orig (h, r, l, n);
|
|
|
|
}
|
2019-09-18 22:29:18 +08:00
|
|
|
/* CreateProcess() is hooked for GDB etc. */
|
|
|
|
static BOOL WINAPI
|
|
|
|
CreateProcessA_Hooked
|
|
|
|
(LPCSTR n, LPSTR c, LPSECURITY_ATTRIBUTES pa, LPSECURITY_ATTRIBUTES ta,
|
|
|
|
BOOL inh, DWORD f, LPVOID e, LPCSTR d,
|
|
|
|
LPSTARTUPINFOA si, LPPROCESS_INFORMATION pi)
|
|
|
|
{
|
|
|
|
HANDLE h;
|
|
|
|
if (si->dwFlags & STARTF_USESTDHANDLES)
|
|
|
|
h = si->hStdOutput;
|
|
|
|
else
|
|
|
|
h = GetStdHandle (STD_OUTPUT_HANDLE);
|
|
|
|
set_ishybrid_and_switch_to_pcon (h);
|
|
|
|
return CreateProcessA_Orig (n, c, pa, ta, inh, f, e, d, si, pi);
|
|
|
|
}
|
|
|
|
static BOOL WINAPI
|
|
|
|
CreateProcessW_Hooked
|
|
|
|
(LPCWSTR n, LPWSTR c, LPSECURITY_ATTRIBUTES pa, LPSECURITY_ATTRIBUTES ta,
|
|
|
|
BOOL inh, DWORD f, LPVOID e, LPCWSTR d,
|
|
|
|
LPSTARTUPINFOW si, LPPROCESS_INFORMATION pi)
|
|
|
|
{
|
|
|
|
HANDLE h;
|
|
|
|
if (si->dwFlags & STARTF_USESTDHANDLES)
|
|
|
|
h = si->hStdOutput;
|
|
|
|
else
|
|
|
|
h = GetStdHandle (STD_OUTPUT_HANDLE);
|
|
|
|
set_ishybrid_and_switch_to_pcon (h);
|
|
|
|
return CreateProcessW_Orig (n, c, pa, ta, inh, f, e, d, si, pi);
|
|
|
|
}
|
2019-08-28 02:04:02 +08:00
|
|
|
|
2019-09-18 22:29:19 +08:00
|
|
|
static char *
|
|
|
|
convert_mb_str (UINT cp_to, size_t *len_to,
|
|
|
|
UINT cp_from, const char *ptr_from, size_t len_from)
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
size_t nlen;
|
|
|
|
if (cp_to != cp_from)
|
|
|
|
{
|
|
|
|
size_t wlen =
|
|
|
|
MultiByteToWideChar (cp_from, 0, ptr_from, len_from, NULL, 0);
|
|
|
|
wchar_t *wbuf = (wchar_t *)
|
|
|
|
HeapAlloc (GetProcessHeap (), 0, wlen * sizeof (wchar_t));
|
|
|
|
wlen =
|
|
|
|
MultiByteToWideChar (cp_from, 0, ptr_from, len_from, wbuf, wlen);
|
|
|
|
nlen = WideCharToMultiByte (cp_to, 0, wbuf, wlen, NULL, 0, NULL, NULL);
|
|
|
|
buf = (char *) HeapAlloc (GetProcessHeap (), 0, nlen);
|
|
|
|
nlen = WideCharToMultiByte (cp_to, 0, wbuf, wlen, buf, nlen, NULL, NULL);
|
|
|
|
HeapFree (GetProcessHeap (), 0, wbuf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Just copy */
|
|
|
|
buf = (char *) HeapAlloc (GetProcessHeap (), 0, len_from);
|
|
|
|
memcpy (buf, ptr_from, len_from);
|
|
|
|
nlen = len_from;
|
|
|
|
}
|
|
|
|
*len_to = nlen;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mb_str_free (char *ptr)
|
|
|
|
{
|
|
|
|
HeapFree (GetProcessHeap (), 0, ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2012-04-05 10:54:51 +08:00
|
|
|
bytes_available (DWORD& n, HANDLE h)
|
|
|
|
{
|
2012-05-16 09:56:41 +08:00
|
|
|
DWORD navail, nleft;
|
|
|
|
navail = nleft = 0;
|
|
|
|
bool succeeded = PeekNamedPipe (h, NULL, 0, NULL, &navail, &nleft);
|
|
|
|
if (succeeded)
|
|
|
|
/* nleft should always be the right choice unless something has written 0
|
|
|
|
bytes to the pipe. In that pathological case we return the actual number
|
|
|
|
of bytes available in the pipe. See cgf-000008 for more details. */
|
|
|
|
n = nleft ?: navail;
|
|
|
|
else
|
2012-04-05 10:54:51 +08:00
|
|
|
{
|
|
|
|
termios_printf ("PeekNamedPipe(%p) failed, %E", h);
|
|
|
|
n = 0;
|
|
|
|
}
|
2012-11-09 01:27:00 +08:00
|
|
|
debug_only_printf ("n %u, nleft %u, navail %u", n, nleft, navail);
|
2012-04-05 10:54:51 +08:00
|
|
|
return succeeded;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
fhandler_pty_common::bytes_available (DWORD &n)
|
|
|
|
{
|
2019-08-28 02:04:02 +08:00
|
|
|
return ::bytes_available (n, get_handle_cyg ());
|
2012-04-05 10:54:51 +08:00
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
static class mutex_stack
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
const char *fn;
|
|
|
|
int ln;
|
|
|
|
const char *tname;
|
|
|
|
} ostack[100];
|
|
|
|
|
2001-09-06 12:41:59 +08:00
|
|
|
static int osi;
|
2000-02-18 03:38:33 +08:00
|
|
|
#endif /*DEBUGGING*/
|
|
|
|
|
2012-04-05 10:54:51 +08:00
|
|
|
void
|
|
|
|
fhandler_pty_master::flush_to_slave ()
|
2012-08-17 07:34:45 +08:00
|
|
|
{
|
2012-04-05 10:54:51 +08:00
|
|
|
if (get_readahead_valid () && !(get_ttyp ()->ti.c_lflag & ICANON))
|
|
|
|
accept_input ();
|
2020-02-09 22:46:59 +08:00
|
|
|
WaitForSingleObject (input_mutex, INFINITE);
|
|
|
|
if (!get_ttyp ()->pcon_in_empty && !(get_ttyp ()->ti.c_lflag & ICANON))
|
|
|
|
SetEvent (input_available_event);
|
|
|
|
ReleaseMutex (input_mutex);
|
2012-04-05 10:54:51 +08:00
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
DWORD
|
2011-06-15 05:48:43 +08:00
|
|
|
fhandler_pty_common::__acquire_output_mutex (const char *fn, int ln,
|
2012-04-05 10:54:51 +08:00
|
|
|
DWORD ms)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2005-12-30 04:46:34 +08:00
|
|
|
if (strace.active ())
|
2011-06-15 05:48:43 +08:00
|
|
|
strace.prntf (_STRACE_TERMIOS, fn, "(%d): pty output_mutex (%p): waiting %d ms", ln, output_mutex, ms);
|
2013-07-31 23:19:18 +08:00
|
|
|
if (ms == INFINITE)
|
2013-05-08 08:10:08 +08:00
|
|
|
ms = 100;
|
2000-02-18 03:38:33 +08:00
|
|
|
DWORD res = WaitForSingleObject (output_mutex, ms);
|
|
|
|
if (res == WAIT_OBJECT_0)
|
|
|
|
{
|
2000-03-10 05:04:05 +08:00
|
|
|
#ifndef DEBUGGING
|
2005-12-30 05:10:50 +08:00
|
|
|
if (strace.active ())
|
2011-06-15 05:48:43 +08:00
|
|
|
strace.prntf (_STRACE_TERMIOS, fn, "(%d): pty output_mutex: acquired", ln, res);
|
2000-03-10 05:04:05 +08:00
|
|
|
#else
|
2000-02-18 03:38:33 +08:00
|
|
|
ostack[osi].fn = fn;
|
|
|
|
ostack[osi].ln = ln;
|
2018-01-24 16:34:23 +08:00
|
|
|
ostack[osi].tname = mythreadname ();
|
2000-02-18 03:38:33 +08:00
|
|
|
termios_printf ("acquired for %s:%d, osi %d", fn, ln, osi);
|
|
|
|
osi++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-06-15 05:48:43 +08:00
|
|
|
fhandler_pty_common::__release_output_mutex (const char *fn, int ln)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
if (ReleaseMutex (output_mutex))
|
|
|
|
{
|
2000-03-10 05:04:05 +08:00
|
|
|
#ifndef DEBUGGING
|
2005-12-30 05:10:50 +08:00
|
|
|
if (strace.active ())
|
2011-06-15 05:48:43 +08:00
|
|
|
strace.prntf (_STRACE_TERMIOS, fn, "(%d): pty output_mutex(%p) released", ln, output_mutex);
|
2000-03-10 05:04:05 +08:00
|
|
|
#else
|
2000-02-18 03:38:33 +08:00
|
|
|
if (osi > 0)
|
|
|
|
osi--;
|
2011-05-06 06:30:53 +08:00
|
|
|
termios_printf ("released(%p) at %s:%d, osi %d", output_mutex, fn, ln, osi);
|
2001-03-11 09:58:23 +08:00
|
|
|
termios_printf (" for %s:%d (%s)", ostack[osi].fn, ostack[osi].ln, ostack[osi].tname);
|
2000-02-18 03:38:33 +08:00
|
|
|
ostack[osi].ln = -ln;
|
|
|
|
#endif
|
|
|
|
}
|
2006-05-22 12:50:54 +08:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
else if (osi > 0)
|
|
|
|
{
|
|
|
|
system_printf ("couldn't release output mutex but we seem to own it, %E");
|
|
|
|
try_to_debug ();
|
|
|
|
}
|
|
|
|
#endif
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2011-06-15 05:48:43 +08:00
|
|
|
/* Process pty input. */
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_pty_master::doecho (const void *str, DWORD len)
|
|
|
|
{
|
2015-03-25 19:42:38 +08:00
|
|
|
ssize_t towrite = len;
|
2019-08-28 02:04:02 +08:00
|
|
|
acquire_output_mutex (INFINITE);
|
2015-03-25 19:42:38 +08:00
|
|
|
if (!process_opost_output (echo_w, str, towrite, true))
|
2015-03-05 20:57:34 +08:00
|
|
|
termios_printf ("Write to echo pipe failed, %E");
|
2019-08-28 02:04:02 +08:00
|
|
|
release_output_mutex ();
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_pty_master::accept_input ()
|
|
|
|
{
|
2002-12-17 11:49:34 +08:00
|
|
|
DWORD bytes_left;
|
|
|
|
int ret = 1;
|
2001-03-04 23:34:25 +08:00
|
|
|
|
2005-07-07 04:05:03 +08:00
|
|
|
WaitForSingleObject (input_mutex, INFINITE);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-12-17 11:49:34 +08:00
|
|
|
bytes_left = eat_readahead (-1);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-12-17 11:49:34 +08:00
|
|
|
if (!bytes_left)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2002-12-17 11:49:34 +08:00
|
|
|
termios_printf ("sending EOF to slave");
|
|
|
|
get_ttyp ()->read_retval = 0;
|
|
|
|
}
|
2020-02-09 22:46:59 +08:00
|
|
|
else if (!to_be_read_from_pcon ())
|
2002-12-17 11:49:34 +08:00
|
|
|
{
|
2020-01-27 20:14:32 +08:00
|
|
|
char *p = rabuf ();
|
2002-12-17 11:49:34 +08:00
|
|
|
DWORD rc;
|
|
|
|
DWORD written = 0;
|
2002-12-01 06:23:01 +08:00
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
paranoid_printf ("about to write %u chars to slave", bytes_left);
|
2002-12-17 11:49:34 +08:00
|
|
|
rc = WriteFile (get_output_handle (), p, bytes_left, &written, NULL);
|
|
|
|
if (!rc)
|
|
|
|
{
|
2011-10-12 07:20:38 +08:00
|
|
|
debug_printf ("error writing to pipe %p %E", get_output_handle ());
|
2002-12-17 11:49:34 +08:00
|
|
|
get_ttyp ()->read_retval = -1;
|
2002-12-20 09:38:55 +08:00
|
|
|
ret = -1;
|
2002-12-17 11:49:34 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
get_ttyp ()->read_retval = 1;
|
2001-04-27 14:27:28 +08:00
|
|
|
p += written;
|
|
|
|
bytes_left -= written;
|
|
|
|
if (bytes_left > 0)
|
|
|
|
{
|
|
|
|
debug_printf ("to_slave pipe is full");
|
2002-12-17 11:49:34 +08:00
|
|
|
puts_readahead (p, bytes_left);
|
|
|
|
ret = 0;
|
2001-04-27 14:27:28 +08:00
|
|
|
}
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2002-12-17 11:49:34 +08:00
|
|
|
|
2001-03-04 23:34:25 +08:00
|
|
|
SetEvent (input_available_event);
|
|
|
|
ReleaseMutex (input_mutex);
|
2002-12-17 11:49:34 +08:00
|
|
|
return ret;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2001-09-25 05:50:44 +08:00
|
|
|
bool
|
2000-02-18 03:38:33 +08:00
|
|
|
fhandler_pty_master::hit_eof ()
|
|
|
|
{
|
|
|
|
if (get_ttyp ()->was_opened && !get_ttyp ()->slave_alive ())
|
|
|
|
{
|
|
|
|
/* We have the only remaining open handle to this pty, and
|
|
|
|
the slave pty has been opened at least once. We treat
|
|
|
|
this as EOF. */
|
|
|
|
termios_printf ("all other handles closed");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-15 05:48:43 +08:00
|
|
|
/* Process pty output requests */
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
int
|
2000-03-12 14:29:54 +08:00
|
|
|
fhandler_pty_master::process_slave_output (char *buf, size_t len, int pktmode_on)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
size_t rlen;
|
2015-03-25 19:42:38 +08:00
|
|
|
char outbuf[OUT_BUFFER_SIZE];
|
2000-02-18 03:38:33 +08:00
|
|
|
DWORD n;
|
2015-03-05 20:57:34 +08:00
|
|
|
DWORD echo_cnt;
|
2000-03-12 12:44:37 +08:00
|
|
|
int rc = 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2012-04-05 10:54:51 +08:00
|
|
|
flush_to_slave ();
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
if (len == 0)
|
2000-03-12 12:44:37 +08:00
|
|
|
goto out;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-03-12 14:29:54 +08:00
|
|
|
for (;;)
|
|
|
|
{
|
2015-03-05 20:57:34 +08:00
|
|
|
n = echo_cnt = 0;
|
2012-04-05 10:54:51 +08:00
|
|
|
for (;;)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2015-03-05 20:57:34 +08:00
|
|
|
/* Check echo pipe first. */
|
|
|
|
if (::bytes_available (echo_cnt, echo_r) && echo_cnt > 0)
|
|
|
|
break;
|
2019-08-28 02:04:02 +08:00
|
|
|
if (!bytes_available (n))
|
2012-04-05 10:54:51 +08:00
|
|
|
goto err;
|
|
|
|
if (n)
|
|
|
|
break;
|
|
|
|
if (hit_eof ())
|
2015-03-25 19:42:38 +08:00
|
|
|
{
|
|
|
|
set_errno (EIO);
|
|
|
|
rc = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-04-05 10:54:51 +08:00
|
|
|
/* DISCARD (FLUSHO) and tcflush can finish here. */
|
|
|
|
if ((get_ttyp ()->ti.c_lflag & FLUSHO || !buf))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (is_nonblocking ())
|
2000-03-12 14:29:54 +08:00
|
|
|
{
|
2012-04-05 10:54:51 +08:00
|
|
|
set_errno (EAGAIN);
|
|
|
|
rc = -1;
|
|
|
|
goto out;
|
2000-03-12 14:29:54 +08:00
|
|
|
}
|
2012-04-05 10:54:51 +08:00
|
|
|
pthread_testcancel ();
|
2012-08-16 03:07:42 +08:00
|
|
|
if (cygwait (NULL, 10, cw_sig_eintr) == WAIT_SIGNALED
|
2012-04-05 10:54:51 +08:00
|
|
|
&& !_my_tls.call_signal_handler ())
|
2011-05-06 06:30:53 +08:00
|
|
|
{
|
2012-04-05 10:54:51 +08:00
|
|
|
set_errno (EINTR);
|
|
|
|
rc = -1;
|
|
|
|
goto out;
|
2011-05-06 06:30:53 +08:00
|
|
|
}
|
2012-04-05 10:54:51 +08:00
|
|
|
flush_to_slave ();
|
|
|
|
}
|
|
|
|
|
2015-03-25 19:42:38 +08:00
|
|
|
/* Set RLEN to the number of bytes to read from the pipe. */
|
|
|
|
rlen = len;
|
|
|
|
|
|
|
|
char *optr;
|
|
|
|
optr = buf;
|
|
|
|
if (pktmode_on && buf)
|
|
|
|
{
|
|
|
|
*optr++ = TIOCPKT_DATA;
|
|
|
|
rlen -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rlen == 0)
|
|
|
|
{
|
|
|
|
rc = optr - buf;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rlen > sizeof outbuf)
|
|
|
|
rlen = sizeof outbuf;
|
|
|
|
|
2015-03-05 20:57:34 +08:00
|
|
|
/* If echo pipe has data (something has been typed or pasted), prefer
|
|
|
|
it over slave output. */
|
|
|
|
if (echo_cnt > 0)
|
2020-01-21 10:22:02 +08:00
|
|
|
{
|
2015-03-05 20:57:34 +08:00
|
|
|
if (!ReadFile (echo_r, outbuf, rlen, &n, NULL))
|
|
|
|
{
|
|
|
|
termios_printf ("ReadFile on echo pipe failed, %E");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 02:04:02 +08:00
|
|
|
else if (!ReadFile (get_handle (), outbuf, rlen, &n, NULL))
|
2012-04-05 10:54:51 +08:00
|
|
|
{
|
|
|
|
termios_printf ("ReadFile failed, %E");
|
|
|
|
goto err;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2000-03-12 14:29:54 +08:00
|
|
|
termios_printf ("bytes read %u", n);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2003-09-19 02:54:48 +08:00
|
|
|
if (get_ttyp ()->ti.c_lflag & FLUSHO || !buf)
|
2000-09-08 00:23:51 +08:00
|
|
|
continue;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2015-03-25 19:42:38 +08:00
|
|
|
memcpy (optr, outbuf, n);
|
|
|
|
optr += n;
|
2000-03-12 14:29:54 +08:00
|
|
|
rc = optr - buf;
|
2000-03-12 12:44:37 +08:00
|
|
|
break;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-03-12 12:44:37 +08:00
|
|
|
err:
|
|
|
|
if (GetLastError () == ERROR_BROKEN_PIPE)
|
|
|
|
rc = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
rc = -1;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2000-03-12 12:44:37 +08:00
|
|
|
break;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2000-03-12 12:44:37 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
termios_printf ("returning %d", rc);
|
|
|
|
return rc;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2011-06-15 05:48:43 +08:00
|
|
|
/* pty slave stuff */
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2011-06-15 05:48:43 +08:00
|
|
|
fhandler_pty_slave::fhandler_pty_slave (int unit)
|
2019-08-28 02:04:02 +08:00
|
|
|
: fhandler_pty_common (), inuse (NULL), output_handle_cyg (NULL),
|
2019-09-14 05:48:14 +08:00
|
|
|
io_handle_cyg (NULL), pid_restore (0), fd (-1)
|
2011-05-06 06:30:53 +08:00
|
|
|
{
|
2011-06-14 07:49:00 +08:00
|
|
|
if (unit >= 0)
|
2011-10-20 22:02:54 +08:00
|
|
|
dev ().parse (DEV_PTYS_MAJOR, unit);
|
2011-05-06 06:30:53 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2019-08-28 02:04:02 +08:00
|
|
|
fhandler_pty_slave::~fhandler_pty_slave ()
|
|
|
|
{
|
|
|
|
if (!get_ttyp ())
|
2020-03-13 11:06:49 +08:00
|
|
|
{
|
|
|
|
/* Why comes here? Who clears _tc? */
|
|
|
|
if (freeconsole_on_close)
|
|
|
|
{
|
|
|
|
FreeConsole ();
|
|
|
|
pcon_attached_to = -1;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-11-16 07:27:24 +08:00
|
|
|
if (get_pseudo_console ())
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
|
|
|
int used = 0;
|
2019-09-04 09:45:35 +08:00
|
|
|
int attached = 0;
|
2019-08-28 02:04:02 +08:00
|
|
|
cygheap_fdenum cfd (false);
|
|
|
|
while (cfd.next () >= 0)
|
2019-09-04 09:45:35 +08:00
|
|
|
{
|
|
|
|
if (cfd->get_major () == DEV_PTYS_MAJOR ||
|
|
|
|
cfd->get_major () == DEV_CONS_MAJOR)
|
|
|
|
used ++;
|
|
|
|
if (cfd->get_major () == DEV_PTYS_MAJOR &&
|
|
|
|
cfd->get_minor () == pcon_attached_to)
|
|
|
|
attached ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call FreeConsole() if no tty is opened and the process
|
|
|
|
is attached to console corresponding to tty. This is
|
|
|
|
needed to make GNU screen and tmux work in Windows 10
|
|
|
|
1903. */
|
|
|
|
if (attached == 0)
|
|
|
|
pcon_attached_to = -1;
|
|
|
|
if (used == 0)
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
|
|
|
init_console_handler (false);
|
2020-01-10 19:46:26 +08:00
|
|
|
if (freeconsole_on_close)
|
2020-02-09 22:47:30 +08:00
|
|
|
{
|
|
|
|
FreeConsole ();
|
|
|
|
pcon_attached_to = -1;
|
|
|
|
}
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
int
|
2011-06-15 05:48:43 +08:00
|
|
|
fhandler_pty_slave::open (int flags, mode_t)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2019-08-28 02:04:02 +08:00
|
|
|
HANDLE pty_owner;
|
|
|
|
HANDLE from_master_local, from_master_cyg_local;
|
|
|
|
HANDLE to_master_local, to_master_cyg_local;
|
2010-04-02 23:41:27 +08:00
|
|
|
HANDLE *handles[] =
|
|
|
|
{
|
|
|
|
&from_master_local, &input_available_event, &input_mutex, &inuse,
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
&output_mutex, &to_master_local, &pty_owner, &to_master_cyg_local,
|
2019-08-28 02:04:02 +08:00
|
|
|
&from_master_cyg_local,
|
2010-04-02 23:41:27 +08:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
for (HANDLE **h = handles; *h; h++)
|
|
|
|
**h = NULL;
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
_tc = cygwin_shared->tty[get_minor ()];
|
2011-06-04 08:12:29 +08:00
|
|
|
|
|
|
|
tcinit (false);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
cygwin_shared->tty.attach (get_minor ());
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* Create synchronisation events */
|
2007-12-06 00:50:18 +08:00
|
|
|
char buf[MAX_PATH];
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2011-05-06 06:30:53 +08:00
|
|
|
const char *errmsg = NULL;
|
|
|
|
|
2010-04-20 03:52:43 +08:00
|
|
|
if (!(output_mutex = get_ttyp ()->open_output_mutex (MAXIMUM_ALLOWED)))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2010-04-02 23:41:27 +08:00
|
|
|
errmsg = "open output mutex failed, %E";
|
|
|
|
goto err;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2010-04-20 03:52:43 +08:00
|
|
|
if (!(input_mutex = get_ttyp ()->open_input_mutex (MAXIMUM_ALLOWED)))
|
2001-03-04 23:34:25 +08:00
|
|
|
{
|
2010-04-02 23:41:27 +08:00
|
|
|
errmsg = "open input mutex failed, %E";
|
|
|
|
goto err;
|
2001-03-04 23:34:25 +08:00
|
|
|
}
|
2013-04-23 17:44:36 +08:00
|
|
|
shared_name (buf, INPUT_AVAILABLE_EVENT, get_minor ());
|
2010-04-20 03:52:43 +08:00
|
|
|
if (!(input_available_event = OpenEvent (MAXIMUM_ALLOWED, TRUE, buf)))
|
2001-03-04 23:34:25 +08:00
|
|
|
{
|
2010-04-02 23:41:27 +08:00
|
|
|
errmsg = "open input event failed, %E";
|
|
|
|
goto err;
|
2001-03-04 23:34:25 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* FIXME: Needs a method to eliminate tty races */
|
|
|
|
{
|
2010-04-20 03:52:43 +08:00
|
|
|
/* Create security attribute. Default permissions are 0620. */
|
|
|
|
security_descriptor sd;
|
|
|
|
sd.malloc (sizeof (SECURITY_DESCRIPTOR));
|
2011-04-28 17:30:36 +08:00
|
|
|
RtlCreateSecurityDescriptor (sd, SECURITY_DESCRIPTOR_REVISION);
|
2010-04-20 03:52:43 +08:00
|
|
|
SECURITY_ATTRIBUTES sa = { sizeof (SECURITY_ATTRIBUTES), NULL, TRUE };
|
Reapply POSIX ACL changes.
- New, unified implementation of POSIX permission and ACL handling. The
new ACLs now store the POSIX ACL MASK/CLASS_OBJ permission mask, and
they allow to inherit the S_ISGID bit. ACL inheritance now really
works as desired, in a limited, but theoretically equivalent fashion
even for non-Cygwin processes.
To accommodate Windows default ACLs, the new code ignores SYSTEM and
Administrators group permissions when computing the MASK/CLASS_OBJ
permission mask on old ACLs, and it doesn't deny access to SYSTEM and
Administrators group based on the value of MASK/CLASS_OBJ when
creating the new ACLs.
The new code now handles the S_ISGID bit on directories as on Linux:
Setting S_ISGID on a directory causes new files and subdirs created
within to inherit its group, rather than the primary group of the user
who created the file. This only works for files and directories
created by Cygwin processes.
2015-05-29 Corinna Vinschen <corinna@vinschen.de>
Reapply POSIX ACL changes.
* utils.xml (setfacl): Show new option output.
(getfacl): Show new option output.
* sec_acl.cc (get_posix_access): Check for Cygwin "standard" ACL.
Apply umask, if so. Align comments.
* security.cc (set_created_file_access): Fix permission masking by
incoming requested file mode.
* sec_acl.cc (set_posix_access): Apply mask only in terms of execute bit
for SYSTEM and Admins group.
* sec_acl.cc (set_posix_access): Don't create DENY ACEs for USER and
GROUP entries if they are the same as USER_OBJ or GROUP_OBJ.
* fhandler.h (fhandler_pty_slave::facl): Add prototype.
* fhandler_tty.cc (fhandler_pty_slave::facl): New method.
(fhandler_pty_slave::fchown): Fix uid/gid handling.
* sec_acl.cc (set_posix_access): Drop superfluous class_idx variable.
Simplify and move around code in a few places. To improve ACL
readability, add r/w permissions to Admins ACE appended to pty ACL.
Add comment to explain Windows ACE Mask filtering being in the way of
creating a real CLASS_OBJ.
(get_posix_access): Fake CLASS_OBJ for ptys. Explain why.
* security.cc (get_object_attribute): Add S_IFCHR flag to attributes
when calling get_posix_access.
* sec_acl.cc (set_posix_access): Move merging group perms into owner
perms in case of owner == group after mask has been computed. Take
mask into account when doing so to avoid unnecessary ACCESS_DENIED_ACE.
* sec_acl.cc (get_posix_access): Only set saw_group_obj flag if we saw
the ACCESS_ALLOWED_ACE.
* fhandler_disk_file.cc (fhandler_disk_file::fchmod): Deliberatly
set GROUP_OBJ and CLASS_OBJ perms to new group perms. Add comment
to explain why.
* security.cc (set_created_file_access): Ditto.
* sec_acl.cc (set_posix_access): Replace previous patch. Return
EINVAL if uid and/or guid is invalid and not backed by an actual
Windows account.
* sec_acl.cc (set_posix_access): Workaround owner/group SIDs being NULL.
* sec_acl.cc (set_posix_access): Handle files with owner == group.
Rephrase switch statement checking against unfiltered a_type value.
(get_posix_access): Handle files with owner == group.
* sec_acl.cc (get_posix_access): Don't use GROUP_OBJ access to fix up
CLASS_OBJ mask on old-style ACLs. Fix a comment.
* sec_acl.cc (set_posix_access): Always make sure Admins have
WRITE_DAC and WRITE_OWNER permissions.
* security.h (create_object_sd_from_attribute): Drop handle parameter
from prototype.
* security.cc (create_object_sd_from_attribute): Drop handle parameter.
Just create the standard POSIXy security descriptor.
(set_object_attribute): Accommodate dropped paramter in call to
create_object_sd_from_attribute.
* fhandler_tty.cc: Ditto, throughout.
* fhandler_disk_file.cc (fhandler_disk_file::fchmod): Fix typo in
mask computation.
* fhandler.cc (fhandler_base::open_with_arch): Call open with mode
not umasked.
(fhandler_base::open): Explicitely umask mode on NFS here. Call new
set_created_file_access rather than set_file_attribute.
* fhandler_disk_file.cc (fhandler_disk_file::fchmod): Reimplement
setting permissions on filesystems supporting ACLs using the new
set_posix_access call.
(fhandler_disk_file::fchown): Ditto.
(fhandler_disk_file::mkdir): Call new set_created_file_access rather
than set_file_attribute.
* fhandler_socket.cc (fhandler_socket::bind): Don't umask here. Add
WRITE_OWNER access to allow writing group in case of SGID bit set.
Call new set_created_file_access rather than set_file_attribute.
* path.cc (symlink_worker): Call new set_created_file_access rather
than set_file_attribute.
* sec_acl.cc (searchace): Un-staticize.
(set_posix_access): New, complementary functionality to
get_posix_access.
(setacl): Implement in terms of get_posix_access/set_posix_access.
(get_posix_access): Add handling for just created files requiring
their first Cygwin ACL. Fix new_style recognition. Handle SGID
bit. For old-style ACLs, ignore SYSTEM and Administrators when
computing the {DEF_}CLASS_OBJ perms.
* security.cc (get_file_sd): Revamp comment. Change and (hopefully)
speed up inheritance processing for just created files.
(alloc_sd): Remove.
(set_security_attribute): Call set_posix_access instead of alloc_sd.
(get_object_attribute): Fix return value.
(create_object_sd_from_attribute): Call set_posix_access instead of
alloc_sd.
(set_file_attribute): Remove.
(set_created_file_access): New function implemented in terms of
get_posix_access/set_posix_access.
* security.h (set_file_attribute): Remove prototype.
(set_created_file_access): Add prototype.
(searchace): Ditto.
(set_posix_access): Ditto.
* syscalls.cc (open): Call open_with_arch with mode not umasked.
* sec_acl.cc: Change preceeding comment explaining new-style ACLs.
Describe how to generate deny ACEs in more detail. Accommodate the
fact that a NULL deny ACE is used for {DEF_}CLASS_OBJ, rather than
a special Cygwin ACE. Improve further comments.
(CYG_ACE_NEW_STYLE): Define.
(get_posix_access): Change from Cygwin ACE to NULL deny ACE. Fix
CLASS_OBJ handling to generate CLASS_OBJ and DEF_CLASS_OBJ from a single
NULL deny ACE if the inheritance flags say so.
* sec_helper.cc (well_known_cygwin_sid): Remove.
* security.h (well_known_cygwin_sid): Drop declaration.
* sec_acl.cc (CYG_ACE_ISBITS_TO_WIN): Fix typo.
(get_posix_access): Rename index variable from i to idx. Define only
once at top level.
* security.cc (add_access_allowed_ace): Drop unused parameter "offset".
Accommodate throughout.
(add_access_denied_ace): Ditto.
* sec_acl.cc: Accommodate above change throughout.
* security.h (add_access_allowed_ace): Adjust prototype to above change.
(add_access_denied_ace): Ditto.
* sec_acl.cc (get_posix_access): Handle multiple ACEs for the
owner and primary group of the file. Handle the default primary
group ACE as DEF_GROUP_OBJ entry if the directory has the S_ISGID bit
set. Add comments. Minor code rearrangements.
Preliminary read side implementation of new permission handling.
* acl.h (MAX_ACL_ENTRIES): Raise to 2730. Add comment to explain.
* sec_acl.cc: Add leading comment to explain new ACL style.
Add definitions and macros to use for bits in new Cygwin ACL.
(DENY_RWX): New mask value for all temporary deny bits.
(getace): Add bool parameter to decide when leaving all bits intact,
rather than filtering them per the already set bits.
(get_posix_access): New function, taking over functionality to read
POSIX ACL from SECURITY_DESCRIPTOR.
(getacl): Just call get_posix_access.
* sec_helper.cc (well_known_cygwin_sid): Define.
* security.cc (get_attribute_from_acl): Remove.
(get_info_from_sd): Remove.
(get_reg_sd): Call get_posix_access instead of get_info_from_sd.
(get_file_attribute): Ditto.
(get_object_attribute): Ditto.
* security.h (well_known_cygwin_sid): Declare.
(get_posix_access): Add prototype.
* Throughout, use simpler ACE macros from Windows' accctrl.h.
* getfacl.c (main): Special-case SYSTEM and Admins group. Add comments.
* setfacl.c: Align more to Linux tool.
(delacl): New function to delete acl entries only.
(modacl): Drop delete functionality. Add handling of recomputing the
mask and default mask values.
(delallacl): Rename from delacl.
(setfacl): Call delacl in Delete case. Call delallacl in DeleteAll
and DeleteDef case.
(usage): Accommodate new options. Rearrange and rephrase slightly.
(longopts): Emit 'x' in --delete case. Add --no-mask and --mask
options.
(opts): Add -x and -n options.
(main): Handle -d and -x the same. Handle -n and --mask options.
Drop handling for -r option.
* getfacl.c (usage): Align more closely to Linux version. Add new
options -c, -e, -E. Change formatting to accommodate longer options.
(longopts): Rename --noname to --numeric. Keep --noname for backward
compatibility. Add --omit-header, --all-effective and --no-effective
options.
(opts): Add -c, -e and -E option.
(main): Handle new -c, -e, and -E options.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-06-24 17:52:29 +08:00
|
|
|
if (!create_object_sd_from_attribute (myself->uid, myself->gid,
|
2015-09-04 01:29:55 +08:00
|
|
|
S_IFCHR | S_IRUSR | S_IWUSR | S_IWGRP,
|
|
|
|
sd))
|
2010-04-20 03:52:43 +08:00
|
|
|
sa.lpSecurityDescriptor = (PSECURITY_DESCRIPTOR) sd;
|
2013-05-08 08:10:08 +08:00
|
|
|
acquire_output_mutex (INFINITE);
|
2010-04-20 03:52:43 +08:00
|
|
|
inuse = get_ttyp ()->create_inuse (&sa);
|
2003-12-08 06:37:12 +08:00
|
|
|
get_ttyp ()->was_opened = true;
|
2000-02-18 03:38:33 +08:00
|
|
|
release_output_mutex ();
|
|
|
|
}
|
|
|
|
|
2019-08-28 02:04:02 +08:00
|
|
|
if (!get_ttyp ()->from_master () || !get_ttyp ()->from_master_cyg () ||
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
!get_ttyp ()->to_master () || !get_ttyp ()->to_master_cyg ())
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2011-06-15 05:48:43 +08:00
|
|
|
errmsg = "pty handles have been closed";
|
2000-02-18 03:38:33 +08:00
|
|
|
set_errno (EACCES);
|
2010-04-02 23:41:27 +08:00
|
|
|
goto err_no_errno;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2010-04-20 03:52:43 +08:00
|
|
|
/* Three case for duplicating the pipe handles:
|
|
|
|
- Either we're the master. In this case, just duplicate the handles.
|
|
|
|
- Or, we have the right to open the master process for handle duplication.
|
|
|
|
In this case, just duplicate the handles.
|
|
|
|
- Or, we have to ask the master process itself. In this case, send our
|
|
|
|
pid to the master process and check the reply. The reply contains
|
|
|
|
either the handles, or an error code which tells us why we didn't
|
|
|
|
get the handles. */
|
|
|
|
if (myself->pid == get_ttyp ()->master_pid)
|
|
|
|
{
|
|
|
|
/* This is the most common case, just calling openpty. */
|
|
|
|
termios_printf ("dup handles within myself.");
|
2011-06-15 05:48:43 +08:00
|
|
|
pty_owner = GetCurrentProcess ();
|
2010-04-20 03:52:43 +08:00
|
|
|
}
|
|
|
|
else
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2006-06-02 23:41:34 +08:00
|
|
|
pinfo p (get_ttyp ()->master_pid);
|
|
|
|
if (!p)
|
2011-06-15 05:48:43 +08:00
|
|
|
termios_printf ("*** couldn't find pty master");
|
2010-04-12 21:28:06 +08:00
|
|
|
else
|
|
|
|
{
|
2011-06-15 05:48:43 +08:00
|
|
|
pty_owner = OpenProcess (PROCESS_DUP_HANDLE, FALSE, p->dwProcessId);
|
|
|
|
if (pty_owner)
|
2011-10-10 12:05:29 +08:00
|
|
|
termios_printf ("dup handles directly since I'm the owner");
|
2002-05-28 09:55:40 +08:00
|
|
|
}
|
2010-04-20 03:52:43 +08:00
|
|
|
}
|
2011-06-15 05:48:43 +08:00
|
|
|
if (pty_owner)
|
2010-04-20 03:52:43 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
if (!DuplicateHandle (pty_owner, get_ttyp ()->from_master (),
|
2009-12-19 04:32:04 +08:00
|
|
|
GetCurrentProcess (), &from_master_local, 0, TRUE,
|
2002-02-28 22:30:38 +08:00
|
|
|
DUPLICATE_SAME_ACCESS))
|
2002-05-28 09:55:40 +08:00
|
|
|
{
|
2009-07-04 02:05:51 +08:00
|
|
|
termios_printf ("can't duplicate input from %u/%p, %E",
|
2013-04-23 17:44:36 +08:00
|
|
|
get_ttyp ()->master_pid, get_ttyp ()->from_master ());
|
2002-05-28 09:55:40 +08:00
|
|
|
__seterrno ();
|
2010-04-02 23:41:27 +08:00
|
|
|
goto err_no_msg;
|
2002-05-28 09:55:40 +08:00
|
|
|
}
|
2019-08-28 02:04:02 +08:00
|
|
|
if (!DuplicateHandle (pty_owner, get_ttyp ()->from_master_cyg (),
|
|
|
|
GetCurrentProcess (), &from_master_cyg_local, 0, TRUE,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
termios_printf ("can't duplicate input from %u/%p, %E",
|
|
|
|
get_ttyp ()->master_pid, get_ttyp ()->from_master_cyg ());
|
|
|
|
__seterrno ();
|
|
|
|
goto err_no_msg;
|
|
|
|
}
|
2013-04-23 17:44:36 +08:00
|
|
|
if (!DuplicateHandle (pty_owner, get_ttyp ()->to_master (),
|
2009-12-19 04:32:04 +08:00
|
|
|
GetCurrentProcess (), &to_master_local, 0, TRUE,
|
2002-02-28 22:30:38 +08:00
|
|
|
DUPLICATE_SAME_ACCESS))
|
2002-05-28 09:55:40 +08:00
|
|
|
{
|
2010-04-02 23:41:27 +08:00
|
|
|
errmsg = "can't duplicate output, %E";
|
|
|
|
goto err;
|
2002-05-28 09:55:40 +08:00
|
|
|
}
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
if (!DuplicateHandle (pty_owner, get_ttyp ()->to_master_cyg (),
|
|
|
|
GetCurrentProcess (), &to_master_cyg_local, 0, TRUE,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
errmsg = "can't duplicate output for cygwin, %E";
|
|
|
|
goto err;
|
|
|
|
}
|
2011-06-15 05:48:43 +08:00
|
|
|
if (pty_owner != GetCurrentProcess ())
|
|
|
|
CloseHandle (pty_owner);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2010-04-20 03:52:43 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
pipe_request req = { GetCurrentProcessId () };
|
|
|
|
pipe_reply repl;
|
|
|
|
DWORD len;
|
|
|
|
|
2011-06-15 05:48:43 +08:00
|
|
|
__small_sprintf (buf, "\\\\.\\pipe\\cygwin-%S-pty%d-master-ctl",
|
2013-04-23 17:44:36 +08:00
|
|
|
&cygheap->installation_key, get_minor ());
|
2010-04-20 03:52:43 +08:00
|
|
|
termios_printf ("dup handles via master control pipe %s", buf);
|
|
|
|
if (!CallNamedPipe (buf, &req, sizeof req, &repl, sizeof repl,
|
|
|
|
&len, 500))
|
|
|
|
{
|
|
|
|
errmsg = "can't call master, %E";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
from_master_local = repl.from_master;
|
2019-08-28 02:04:02 +08:00
|
|
|
from_master_cyg_local = repl.from_master_cyg;
|
2010-04-20 03:52:43 +08:00
|
|
|
to_master_local = repl.to_master;
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
to_master_cyg_local = repl.to_master_cyg;
|
2019-08-28 02:04:02 +08:00
|
|
|
if (!from_master_local || !from_master_cyg_local ||
|
|
|
|
!to_master_local || !to_master_cyg_local)
|
2010-04-20 03:52:43 +08:00
|
|
|
{
|
|
|
|
SetLastError (repl.error);
|
|
|
|
errmsg = "error duplicating pipes, %E";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VerifyHandle (from_master_local);
|
2019-08-28 02:04:02 +08:00
|
|
|
VerifyHandle (from_master_cyg_local);
|
2010-04-20 03:52:43 +08:00
|
|
|
VerifyHandle (to_master_local);
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
VerifyHandle (to_master_cyg_local);
|
2002-02-28 22:30:38 +08:00
|
|
|
|
2011-06-15 05:48:43 +08:00
|
|
|
termios_printf ("duplicated from_master %p->%p from pty_owner",
|
2013-04-23 17:44:36 +08:00
|
|
|
get_ttyp ()->from_master (), from_master_local);
|
2019-08-28 02:04:02 +08:00
|
|
|
termios_printf ("duplicated from_master_cyg %p->%p from pty_owner",
|
|
|
|
get_ttyp ()->from_master_cyg (), from_master_cyg_local);
|
2011-06-15 05:48:43 +08:00
|
|
|
termios_printf ("duplicated to_master %p->%p from pty_owner",
|
2013-04-23 17:44:36 +08:00
|
|
|
get_ttyp ()->to_master (), to_master_local);
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
termios_printf ("duplicated to_master_cyg %p->%p from pty_owner",
|
|
|
|
get_ttyp ()->to_master_cyg (), to_master_cyg_local);
|
2002-02-28 22:30:38 +08:00
|
|
|
|
2019-03-30 15:12:02 +08:00
|
|
|
set_handle (from_master_local);
|
2019-08-28 02:04:02 +08:00
|
|
|
set_handle_cyg (from_master_cyg_local);
|
2002-02-28 22:30:38 +08:00
|
|
|
set_output_handle (to_master_local);
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
set_output_handle_cyg (to_master_cyg_local);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2019-11-16 07:27:24 +08:00
|
|
|
if (!get_pseudo_console ())
|
2019-09-04 09:45:35 +08:00
|
|
|
{
|
|
|
|
fhandler_console::need_invisible ();
|
|
|
|
pcon_attached_to = -1;
|
|
|
|
}
|
|
|
|
else if (!fhandler_console::get_console_process_id
|
|
|
|
(GetCurrentProcessId (), true))
|
|
|
|
{
|
|
|
|
fhandler_console::need_invisible ();
|
|
|
|
pcon_attached_to = -1;
|
|
|
|
}
|
|
|
|
else if (fhandler_console::get_console_process_id
|
2019-11-16 07:27:24 +08:00
|
|
|
(get_helper_process_id (), true))
|
2019-09-04 09:45:35 +08:00
|
|
|
/* Attached to pcon of this pty */
|
|
|
|
{
|
|
|
|
pcon_attached_to = get_minor ();
|
|
|
|
init_console_handler (true);
|
|
|
|
}
|
|
|
|
|
2001-04-24 10:07:58 +08:00
|
|
|
set_open_status ();
|
2000-02-18 03:38:33 +08:00
|
|
|
return 1;
|
2010-04-02 23:41:27 +08:00
|
|
|
|
|
|
|
err:
|
2011-10-10 12:05:29 +08:00
|
|
|
if (GetLastError () == ERROR_FILE_NOT_FOUND)
|
|
|
|
set_errno (ENXIO);
|
|
|
|
else
|
|
|
|
__seterrno ();
|
2010-04-02 23:41:27 +08:00
|
|
|
err_no_errno:
|
|
|
|
termios_printf (errmsg);
|
|
|
|
err_no_msg:
|
|
|
|
for (HANDLE **h = handles; *h; h++)
|
|
|
|
if (**h && **h != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle (**h);
|
|
|
|
return 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2011-05-06 06:30:53 +08:00
|
|
|
void
|
2011-06-15 05:48:43 +08:00
|
|
|
fhandler_pty_slave::open_setup (int flags)
|
2011-05-06 06:30:53 +08:00
|
|
|
{
|
|
|
|
set_flags ((flags & ~O_TEXT) | O_BINARY);
|
2011-10-16 06:37:30 +08:00
|
|
|
myself->set_ctty (this, flags);
|
2011-05-06 06:30:53 +08:00
|
|
|
report_tty_counts (this, "opened", "");
|
2013-10-24 23:26:21 +08:00
|
|
|
fhandler_base::open_setup (flags);
|
2011-05-06 06:30:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-06-15 05:48:43 +08:00
|
|
|
fhandler_pty_slave::cleanup ()
|
2003-03-04 12:07:34 +08:00
|
|
|
{
|
2011-11-14 09:29:49 +08:00
|
|
|
/* This used to always call fhandler_pty_common::close when we were execing
|
|
|
|
but that caused multiple closes of the handles associated with this pty.
|
|
|
|
Since close_all_files is not called until after the cygwin process has
|
|
|
|
synced or before a non-cygwin process has exited, it should be safe to
|
|
|
|
just close this normally. cgf 2006-05-20 */
|
2006-05-20 12:59:53 +08:00
|
|
|
report_tty_counts (this, "closed", "");
|
2013-10-23 04:41:09 +08:00
|
|
|
fhandler_base::cleanup ();
|
2011-05-06 06:30:53 +08:00
|
|
|
}
|
2003-12-28 01:41:17 +08:00
|
|
|
|
2011-05-06 06:30:53 +08:00
|
|
|
int
|
2011-06-15 05:48:43 +08:00
|
|
|
fhandler_pty_slave::close ()
|
2011-05-06 06:30:53 +08:00
|
|
|
{
|
2003-12-30 09:57:16 +08:00
|
|
|
termios_printf ("closing last open %s handle", ttyname ());
|
2006-06-02 23:41:34 +08:00
|
|
|
if (inuse && !CloseHandle (inuse))
|
|
|
|
termios_printf ("CloseHandle (inuse), %E");
|
2011-12-10 00:02:56 +08:00
|
|
|
if (!ForceCloseHandle (input_available_event))
|
|
|
|
termios_printf ("CloseHandle (input_available_event<%p>), %E", input_available_event);
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
if (!ForceCloseHandle (get_output_handle_cyg ()))
|
|
|
|
termios_printf ("CloseHandle (get_output_handle_cyg ()<%p>), %E",
|
|
|
|
get_output_handle_cyg ());
|
2019-08-28 02:04:02 +08:00
|
|
|
if (!ForceCloseHandle (get_handle_cyg ()))
|
|
|
|
termios_printf ("CloseHandle (get_handle_cyg ()<%p>), %E",
|
|
|
|
get_handle_cyg ());
|
2019-11-16 07:27:24 +08:00
|
|
|
if (!get_pseudo_console () &&
|
2019-09-04 09:45:35 +08:00
|
|
|
(unsigned) myself->ctty == FHDEV (DEV_PTYS_MAJOR, get_minor ()))
|
2012-02-08 00:54:14 +08:00
|
|
|
fhandler_console::free_console (); /* assumes that we are the last pty closer */
|
2015-05-28 22:06:49 +08:00
|
|
|
fhandler_pty_common::close ();
|
|
|
|
if (!ForceCloseHandle (output_mutex))
|
|
|
|
termios_printf ("CloseHandle (output_mutex<%p>), %E", output_mutex);
|
2019-09-04 09:45:35 +08:00
|
|
|
if (pcon_attached_to == get_minor ())
|
2019-08-28 02:04:02 +08:00
|
|
|
get_ttyp ()->num_pcon_attached_slaves --;
|
2015-05-28 22:06:49 +08:00
|
|
|
return 0;
|
2003-03-04 12:07:34 +08:00
|
|
|
}
|
|
|
|
|
2002-02-28 22:30:38 +08:00
|
|
|
int
|
2011-06-15 05:48:43 +08:00
|
|
|
fhandler_pty_slave::init (HANDLE h, DWORD a, mode_t)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
* 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
|
|
|
int flags = 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
a &= GENERIC_READ | GENERIC_WRITE;
|
|
|
|
if (a == GENERIC_READ)
|
* 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
|
|
|
flags = O_RDONLY;
|
2000-02-18 03:38:33 +08:00
|
|
|
if (a == GENERIC_WRITE)
|
* 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
|
|
|
flags = O_WRONLY;
|
2000-02-18 03:38:33 +08:00
|
|
|
if (a == (GENERIC_READ | GENERIC_WRITE))
|
* 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
|
|
|
flags = O_RDWR;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2011-05-06 06:30:53 +08:00
|
|
|
int ret = open_with_arch (flags);
|
2010-09-21 04:25:50 +08:00
|
|
|
|
2010-09-21 06:28:57 +08:00
|
|
|
if (ret && !cygwin_finished_initializing && !being_debugged ())
|
2010-04-22 03:36:40 +08:00
|
|
|
{
|
|
|
|
/* This only occurs when called from dtable::init_std_file_from_handle
|
|
|
|
We have been started from a non-Cygwin process. So we should become
|
2011-06-15 05:48:43 +08:00
|
|
|
pty process group leader.
|
2010-04-22 03:36:40 +08:00
|
|
|
TODO: Investigate how SIGTTIN should be handled with pure-windows
|
|
|
|
programs. */
|
2011-06-04 08:12:29 +08:00
|
|
|
pinfo p (tc ()->getpgid ());
|
2010-09-21 06:28:57 +08:00
|
|
|
/* We should only grab this when the process group owner for this
|
2011-06-15 05:48:43 +08:00
|
|
|
pty is a non-cygwin process or we've been started directly
|
2010-09-21 06:28:57 +08:00
|
|
|
from a non-Cygwin process with no Cygwin ancestry. */
|
|
|
|
if (!p || ISSTATE (p, PID_NOTCYGWIN))
|
2010-09-29 11:57:24 +08:00
|
|
|
{
|
|
|
|
termios_printf ("Setting process group leader to %d since %W(%d) is not a cygwin process",
|
|
|
|
myself->pgid, p->progname, p->pid);
|
2011-06-04 08:12:29 +08:00
|
|
|
tc ()->setpgid (myself->pgid);
|
2010-09-29 11:57:24 +08:00
|
|
|
}
|
2010-04-22 03:36:40 +08:00
|
|
|
}
|
2010-04-20 03:52:43 +08:00
|
|
|
|
2011-05-06 06:30:53 +08:00
|
|
|
if (h != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle (h); /* Reopened by open */
|
2010-04-20 03:52:43 +08:00
|
|
|
|
|
|
|
return ret;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2019-09-04 09:45:35 +08:00
|
|
|
bool
|
|
|
|
fhandler_pty_slave::try_reattach_pcon (void)
|
|
|
|
{
|
|
|
|
pid_restore = 0;
|
|
|
|
|
|
|
|
/* Do not detach from the console because re-attaching will
|
|
|
|
fail if helper process is running as service account. */
|
2019-10-16 20:34:08 +08:00
|
|
|
if (get_ttyp()->attach_pcon_in_fork)
|
|
|
|
return false;
|
2019-09-04 09:45:35 +08:00
|
|
|
if (pcon_attached_to >= 0 &&
|
|
|
|
cygwin_shared->tty[pcon_attached_to]->attach_pcon_in_fork)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
pid_restore =
|
|
|
|
fhandler_console::get_console_process_id (GetCurrentProcessId (),
|
|
|
|
false);
|
|
|
|
/* If pid_restore is not set, give up. */
|
|
|
|
if (!pid_restore)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
FreeConsole ();
|
2019-11-16 07:27:24 +08:00
|
|
|
if (!AttachConsole (get_helper_process_id ()))
|
2019-09-04 09:45:35 +08:00
|
|
|
{
|
|
|
|
system_printf ("pty%d: AttachConsole(helper=%d) failed. 0x%08lx",
|
2019-11-16 07:27:24 +08:00
|
|
|
get_minor (), get_helper_process_id (), GetLastError ());
|
2019-09-04 09:45:35 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-28 02:04:02 +08:00
|
|
|
void
|
2019-09-04 09:45:35 +08:00
|
|
|
fhandler_pty_slave::restore_reattach_pcon (void)
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
2019-09-04 09:45:35 +08:00
|
|
|
if (pid_restore)
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
2019-09-04 09:45:35 +08:00
|
|
|
FreeConsole ();
|
|
|
|
if (!AttachConsole (pid_restore))
|
|
|
|
{
|
|
|
|
system_printf ("pty%d: AttachConsole(restore=%d) failed. 0x%08lx",
|
|
|
|
get_minor (), pid_restore, GetLastError ());
|
|
|
|
pcon_attached_to = -1;
|
|
|
|
}
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
2019-09-04 09:45:35 +08:00
|
|
|
pid_restore = 0;
|
|
|
|
}
|
|
|
|
|
2020-02-09 22:46:59 +08:00
|
|
|
/* This function requests transfering the input data from the input
|
|
|
|
pipe for cygwin apps to the other input pipe for native apps. */
|
|
|
|
void
|
|
|
|
fhandler_pty_slave::pull_pcon_input (void)
|
|
|
|
{
|
|
|
|
get_ttyp ()->req_transfer_input_to_pcon = true;
|
|
|
|
while (get_ttyp ()->req_transfer_input_to_pcon)
|
|
|
|
Sleep (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_pty_slave::update_pcon_input_state (bool need_lock)
|
|
|
|
{
|
|
|
|
if (need_lock)
|
|
|
|
WaitForSingleObject (input_mutex, INFINITE);
|
|
|
|
/* Flush input buffer if it is requested by master.
|
|
|
|
This happens if ^C is pressed in pseudo console side. */
|
|
|
|
if (get_ttyp ()->req_flush_pcon_input)
|
|
|
|
{
|
|
|
|
FlushConsoleInputBuffer (get_handle ());
|
|
|
|
get_ttyp ()->req_flush_pcon_input = false;
|
|
|
|
}
|
|
|
|
/* Peek console input buffer and update state. */
|
|
|
|
INPUT_RECORD inp[INREC_SIZE];
|
|
|
|
DWORD n;
|
|
|
|
BOOL (WINAPI *PeekFunc)
|
|
|
|
(HANDLE, PINPUT_RECORD, DWORD, LPDWORD);
|
|
|
|
PeekFunc =
|
|
|
|
PeekConsoleInputA_Orig ? : PeekConsoleInput;
|
|
|
|
PeekFunc (get_handle (), inp, INREC_SIZE, &n);
|
|
|
|
bool saw_accept = false;
|
|
|
|
bool pipe_empty = true;
|
|
|
|
while (n-- > 0)
|
|
|
|
if (inp[n].EventType == KEY_EVENT && inp[n].Event.KeyEvent.bKeyDown &&
|
|
|
|
inp[n].Event.KeyEvent.uChar.AsciiChar)
|
|
|
|
{
|
|
|
|
pipe_empty = false;
|
|
|
|
char c = inp[n].Event.KeyEvent.uChar.AsciiChar;
|
|
|
|
const char sigs[] = {
|
|
|
|
(char) get_ttyp ()->ti.c_cc[VINTR],
|
|
|
|
(char) get_ttyp ()->ti.c_cc[VQUIT],
|
|
|
|
(char) get_ttyp ()->ti.c_cc[VSUSP],
|
|
|
|
};
|
|
|
|
const char eols[] = {
|
|
|
|
(char) get_ttyp ()->ti.c_cc[VEOF],
|
|
|
|
(char) get_ttyp ()->ti.c_cc[VEOL],
|
|
|
|
(char) get_ttyp ()->ti.c_cc[VEOL2],
|
|
|
|
'\n',
|
|
|
|
'\r'
|
|
|
|
};
|
|
|
|
if (is_line_input () && memchr (eols, c, sizeof (eols)))
|
|
|
|
saw_accept = true;
|
|
|
|
if ((get_ttyp ()->ti.c_lflag & ISIG) &&
|
|
|
|
memchr (sigs, c, sizeof (sigs)))
|
|
|
|
saw_accept = true;
|
|
|
|
}
|
|
|
|
get_ttyp ()->pcon_in_empty = pipe_empty && !(ralen () > raixget ());
|
|
|
|
if (!get_readahead_valid () &&
|
|
|
|
(pipe_empty || (is_line_input () && !saw_accept)) &&
|
|
|
|
get_ttyp ()->read_retval == 1 && bytes_available (n) && n == 0)
|
|
|
|
ResetEvent (input_available_event);
|
|
|
|
if (need_lock)
|
|
|
|
ReleaseMutex (input_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_pty_slave::eat_readahead (int n)
|
|
|
|
{
|
|
|
|
int oralen = ralen () - raixget ();
|
|
|
|
if (n < 0)
|
|
|
|
n = ralen () - raixget ();
|
|
|
|
if (n > 0 && ralen () > raixget ())
|
|
|
|
{
|
|
|
|
const char eols[] = {
|
|
|
|
(char) get_ttyp ()->ti.c_cc[VEOF],
|
|
|
|
(char) get_ttyp ()->ti.c_cc[VEOL],
|
|
|
|
(char) get_ttyp ()->ti.c_cc[VEOL2],
|
|
|
|
'\n'
|
|
|
|
};
|
|
|
|
while (n > 0 && ralen () > raixget ())
|
|
|
|
{
|
|
|
|
if (memchr (eols, rabuf ()[ralen ()-1], sizeof (eols)))
|
|
|
|
break;
|
|
|
|
-- n;
|
|
|
|
-- ralen ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If IUTF8 is set, the terminal is in UTF-8 mode. If so, we erase
|
|
|
|
a complete UTF-8 multibyte sequence on VERASE/VWERASE. Otherwise,
|
|
|
|
if we only erase a single byte, invalid unicode chars are left in
|
|
|
|
the input. */
|
|
|
|
if (get_ttyp ()->ti.c_iflag & IUTF8)
|
|
|
|
while (ralen () > 0 &&
|
|
|
|
((unsigned char) rabuf ()[ralen ()] & 0xc0) == 0x80)
|
|
|
|
--ralen ();
|
|
|
|
|
|
|
|
if (raixget () >= ralen ())
|
|
|
|
raixget () = raixput () = ralen () = 0;
|
|
|
|
else if (raixput () > ralen ())
|
|
|
|
raixput () = ralen ();
|
|
|
|
}
|
|
|
|
|
|
|
|
return oralen;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_pty_slave::get_readahead_into_buffer (char *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
int copied_chars = 0;
|
|
|
|
|
|
|
|
while (buflen)
|
|
|
|
if ((ch = get_readahead ()) < 0)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const char eols[] = {
|
|
|
|
(char) get_ttyp ()->ti.c_cc[VEOF],
|
|
|
|
(char) get_ttyp ()->ti.c_cc[VEOL],
|
|
|
|
(char) get_ttyp ()->ti.c_cc[VEOL2],
|
|
|
|
'\n'
|
|
|
|
};
|
|
|
|
buf[copied_chars++] = (unsigned char)(ch & 0xff);
|
|
|
|
buflen--;
|
|
|
|
if (is_line_input () && memchr (eols, ch & 0xff, sizeof (eols)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return copied_chars;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
fhandler_pty_slave::get_readahead_valid (void)
|
|
|
|
{
|
|
|
|
if (is_line_input ())
|
|
|
|
{
|
|
|
|
const char eols[] = {
|
|
|
|
(char) get_ttyp ()->ti.c_cc[VEOF],
|
|
|
|
(char) get_ttyp ()->ti.c_cc[VEOL],
|
|
|
|
(char) get_ttyp ()->ti.c_cc[VEOL2],
|
|
|
|
'\n'
|
|
|
|
};
|
|
|
|
for (size_t i=raixget (); i<ralen (); i++)
|
|
|
|
if (memchr (eols, rabuf ()[i], sizeof (eols)))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return ralen () > raixget ();
|
|
|
|
}
|
|
|
|
|
2019-09-04 09:45:35 +08:00
|
|
|
void
|
2019-09-14 05:48:14 +08:00
|
|
|
fhandler_pty_slave::set_switch_to_pcon (int fd_set)
|
2019-09-04 09:45:35 +08:00
|
|
|
{
|
2019-09-14 05:48:14 +08:00
|
|
|
if (fd < 0)
|
|
|
|
fd = fd_set;
|
|
|
|
if (fd == 0 && !get_ttyp ()->switch_to_pcon_in)
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
2020-02-09 22:46:59 +08:00
|
|
|
pull_pcon_input ();
|
2019-09-14 05:48:14 +08:00
|
|
|
if (get_ttyp ()->pcon_pid == 0 ||
|
2020-02-06 18:48:17 +08:00
|
|
|
!pinfo (get_ttyp ()->pcon_pid))
|
2019-09-14 05:48:14 +08:00
|
|
|
get_ttyp ()->pcon_pid = myself->pid;
|
|
|
|
get_ttyp ()->switch_to_pcon_in = true;
|
2020-02-09 22:46:02 +08:00
|
|
|
if (isHybrid && !get_ttyp ()->switch_to_pcon_out)
|
|
|
|
{
|
|
|
|
wait_pcon_fwd ();
|
|
|
|
get_ttyp ()->switch_to_pcon_out = true;
|
|
|
|
}
|
2019-09-14 05:48:14 +08:00
|
|
|
}
|
|
|
|
else if ((fd == 1 || fd == 2) && !get_ttyp ()->switch_to_pcon_out)
|
|
|
|
{
|
2020-01-27 19:22:24 +08:00
|
|
|
wait_pcon_fwd ();
|
2019-08-28 02:04:02 +08:00
|
|
|
if (get_ttyp ()->pcon_pid == 0 ||
|
2020-02-06 18:48:17 +08:00
|
|
|
!pinfo (get_ttyp ()->pcon_pid))
|
2019-08-28 02:04:02 +08:00
|
|
|
get_ttyp ()->pcon_pid = myself->pid;
|
2019-09-14 05:48:14 +08:00
|
|
|
get_ttyp ()->switch_to_pcon_out = true;
|
2020-02-09 22:46:02 +08:00
|
|
|
if (isHybrid)
|
|
|
|
get_ttyp ()->switch_to_pcon_in = true;
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_pty_slave::reset_switch_to_pcon (void)
|
|
|
|
{
|
|
|
|
if (get_ttyp ()->pcon_pid &&
|
|
|
|
get_ttyp ()->pcon_pid != myself->pid &&
|
2020-02-06 18:48:17 +08:00
|
|
|
!!pinfo (get_ttyp ()->pcon_pid))
|
2019-08-28 02:04:02 +08:00
|
|
|
/* There is a process which is grabbing pseudo console. */
|
|
|
|
return;
|
2019-09-08 20:58:35 +08:00
|
|
|
if (isHybrid)
|
2020-02-09 22:46:59 +08:00
|
|
|
return;
|
2019-09-08 21:23:23 +08:00
|
|
|
if (do_not_reset_switch_to_pcon)
|
|
|
|
return;
|
2019-09-14 05:48:14 +08:00
|
|
|
if (get_ttyp ()->switch_to_pcon_out)
|
2020-01-21 10:22:02 +08:00
|
|
|
/* Wait for pty_master_fwd_thread() */
|
2020-01-27 19:22:24 +08:00
|
|
|
wait_pcon_fwd ();
|
2019-08-28 02:04:02 +08:00
|
|
|
get_ttyp ()->pcon_pid = 0;
|
2019-09-14 05:48:14 +08:00
|
|
|
get_ttyp ()->switch_to_pcon_in = false;
|
|
|
|
get_ttyp ()->switch_to_pcon_out = false;
|
2019-09-08 20:58:35 +08:00
|
|
|
init_console_handler (true);
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-09 22:46:01 +08:00
|
|
|
fhandler_pty_slave::push_to_pcon_screenbuffer (const char *ptr, size_t len,
|
|
|
|
bool is_echo)
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
2019-09-04 09:45:35 +08:00
|
|
|
bool attached =
|
2019-11-16 07:27:24 +08:00
|
|
|
!!fhandler_console::get_console_process_id (get_helper_process_id (), true);
|
2019-09-04 09:45:35 +08:00
|
|
|
if (!attached && pcon_attached_to == get_minor ())
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
2019-09-04 09:45:35 +08:00
|
|
|
for (DWORD t0 = GetTickCount (); GetTickCount () - t0 < 100; )
|
|
|
|
{
|
|
|
|
Sleep (1);
|
|
|
|
attached = fhandler_console::get_console_process_id
|
2019-11-16 07:27:24 +08:00
|
|
|
(get_helper_process_id (), true);
|
2019-09-04 09:45:35 +08:00
|
|
|
if (attached)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!attached)
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
2019-09-04 09:45:35 +08:00
|
|
|
system_printf ("pty%d: pcon_attach_to mismatch??????", get_minor ());
|
|
|
|
return;
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
|
|
|
}
|
2019-09-04 09:45:35 +08:00
|
|
|
|
|
|
|
/* If not attached to this pseudo console, try to attach temporarily. */
|
|
|
|
pid_restore = 0;
|
|
|
|
if (pcon_attached_to != get_minor ())
|
|
|
|
if (!try_reattach_pcon ())
|
|
|
|
goto detach;
|
|
|
|
|
2019-08-28 02:04:02 +08:00
|
|
|
char *buf;
|
|
|
|
size_t nlen;
|
|
|
|
DWORD origCP;
|
|
|
|
origCP = GetConsoleOutputCP ();
|
2019-09-15 12:05:52 +08:00
|
|
|
SetConsoleOutputCP (get_ttyp ()->term_code_page);
|
2019-08-28 02:04:02 +08:00
|
|
|
/* Just copy */
|
|
|
|
buf = (char *) HeapAlloc (GetProcessHeap (), 0, len);
|
|
|
|
memcpy (buf, (char *)ptr, len);
|
|
|
|
nlen = len;
|
|
|
|
char *p0, *p1;
|
|
|
|
p0 = p1 = buf;
|
|
|
|
/* Remove alternate screen buffer drawing */
|
|
|
|
while (p0 && p1)
|
|
|
|
{
|
|
|
|
if (!get_ttyp ()->screen_alternated)
|
|
|
|
{
|
|
|
|
/* Check switching to alternate screen buffer */
|
|
|
|
p0 = (char *) memmem (p1, nlen - (p1-buf), "\033[?1049h", 8);
|
|
|
|
if (p0)
|
|
|
|
{
|
|
|
|
//p0 += 8;
|
|
|
|
get_ttyp ()->screen_alternated = true;
|
2019-09-14 05:48:14 +08:00
|
|
|
if (get_ttyp ()->switch_to_pcon_out)
|
2019-09-08 21:23:23 +08:00
|
|
|
do_not_reset_switch_to_pcon = true;
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (get_ttyp ()->screen_alternated)
|
|
|
|
{
|
|
|
|
/* Check switching to main screen buffer */
|
|
|
|
p1 = (char *) memmem (p0, nlen - (p0-buf), "\033[?1049l", 8);
|
|
|
|
if (p1)
|
|
|
|
{
|
|
|
|
p1 += 8;
|
|
|
|
get_ttyp ()->screen_alternated = false;
|
2019-09-08 21:23:23 +08:00
|
|
|
do_not_reset_switch_to_pcon = false;
|
2019-08-28 02:04:02 +08:00
|
|
|
memmove (p0, p1, buf+nlen - p1);
|
|
|
|
nlen -= p1 - p0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
nlen = p0 - buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!nlen) /* Nothing to be synchronized */
|
|
|
|
goto cleanup;
|
2020-02-09 22:46:01 +08:00
|
|
|
if (get_ttyp ()->switch_to_pcon_out && !is_echo)
|
2019-08-28 02:04:02 +08:00
|
|
|
goto cleanup;
|
|
|
|
/* Remove ESC sequence which returns results to console
|
|
|
|
input buffer. Without this, cursor position report
|
|
|
|
is put into the input buffer as a garbage. */
|
|
|
|
/* Remove ESC sequence to report cursor position. */
|
|
|
|
while ((p0 = (char *) memmem (buf, nlen, "\033[6n", 4)))
|
|
|
|
{
|
|
|
|
memmove (p0, p0+4, nlen - (p0+4 - buf));
|
|
|
|
nlen -= 4;
|
|
|
|
}
|
|
|
|
/* Remove ESC sequence to report terminal identity. */
|
|
|
|
while ((p0 = (char *) memmem (buf, nlen, "\033[0c", 4)))
|
|
|
|
{
|
|
|
|
memmove (p0, p0+4, nlen - (p0+4 - buf));
|
|
|
|
nlen -= 4;
|
|
|
|
}
|
2019-11-13 18:49:29 +08:00
|
|
|
|
|
|
|
/* If the ESC sequence ESC[?3h or ESC[?3l which clears console screen
|
|
|
|
buffer is pushed, set need_redraw_screen to trigger redraw screen. */
|
|
|
|
p0 = buf;
|
|
|
|
while ((p0 = (char *) memmem (p0, nlen - (p0 - buf), "\033[?", 3)))
|
|
|
|
{
|
|
|
|
p0 += 3;
|
2019-12-19 00:07:33 +08:00
|
|
|
bool exist_arg_3 = false;
|
2019-12-19 19:03:30 +08:00
|
|
|
while (p0 < buf + nlen && (isdigit (*p0) || *p0 == ';'))
|
2019-11-13 18:49:29 +08:00
|
|
|
{
|
|
|
|
int arg = 0;
|
|
|
|
while (p0 < buf + nlen && isdigit (*p0))
|
|
|
|
arg = arg * 10 + (*p0 ++) - '0';
|
|
|
|
if (arg == 3)
|
2019-12-19 00:07:33 +08:00
|
|
|
exist_arg_3 = true;
|
2019-11-13 18:49:29 +08:00
|
|
|
if (p0 < buf + nlen && *p0 == ';')
|
|
|
|
p0 ++;
|
|
|
|
}
|
2019-12-19 00:07:33 +08:00
|
|
|
if (p0 < buf + nlen && exist_arg_3 && (*p0 == 'h' || *p0 == 'l'))
|
|
|
|
get_ttyp ()->need_redraw_screen = true;
|
2019-11-13 18:49:29 +08:00
|
|
|
p0 ++;
|
|
|
|
if (p0 >= buf + nlen)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-01-21 22:41:44 +08:00
|
|
|
int retry_count;
|
|
|
|
retry_count = 0;
|
2019-08-28 02:04:02 +08:00
|
|
|
DWORD dwMode, flags;
|
|
|
|
flags = ENABLE_VIRTUAL_TERMINAL_PROCESSING;
|
2020-01-21 22:41:44 +08:00
|
|
|
while (!GetConsoleMode (get_output_handle (), &dwMode))
|
|
|
|
{
|
|
|
|
termios_printf ("GetConsoleMode failed, %E");
|
2020-02-10 19:42:45 +08:00
|
|
|
int errno_save = errno;
|
2020-01-21 22:41:44 +08:00
|
|
|
/* Re-open handles */
|
|
|
|
this->open (0, 0);
|
|
|
|
/* Fix pseudo console window size */
|
|
|
|
this->ioctl (TIOCSWINSZ, &get_ttyp ()->winsize);
|
2020-02-10 19:42:45 +08:00
|
|
|
if (errno != errno_save)
|
|
|
|
set_errno (errno_save);
|
2020-01-21 22:41:44 +08:00
|
|
|
if (++retry_count > 3)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2019-08-28 02:04:02 +08:00
|
|
|
if (!(get_ttyp ()->ti.c_oflag & OPOST) ||
|
|
|
|
!(get_ttyp ()->ti.c_oflag & ONLCR))
|
|
|
|
flags |= DISABLE_NEWLINE_AUTO_RETURN;
|
|
|
|
SetConsoleMode (get_output_handle (), dwMode | flags);
|
|
|
|
char *p;
|
|
|
|
p = buf;
|
|
|
|
DWORD wLen, written;
|
|
|
|
written = 0;
|
|
|
|
BOOL (WINAPI *WriteFunc)
|
|
|
|
(HANDLE, LPCVOID, DWORD, LPDWORD, LPOVERLAPPED);
|
|
|
|
WriteFunc = WriteFile_Orig ? WriteFile_Orig : WriteFile;
|
|
|
|
while (written < nlen)
|
|
|
|
{
|
|
|
|
if (!WriteFunc (get_output_handle (), p, nlen - written, &wLen, NULL))
|
|
|
|
{
|
|
|
|
termios_printf ("WriteFile failed, %E");
|
2020-01-21 22:41:44 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
written += wLen;
|
|
|
|
p += wLen;
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Detach from pseudo console and resume. */
|
2019-09-08 21:23:23 +08:00
|
|
|
flags = ENABLE_VIRTUAL_TERMINAL_PROCESSING;
|
|
|
|
SetConsoleMode (get_output_handle (), dwMode | flags);
|
2019-08-28 02:04:02 +08:00
|
|
|
cleanup:
|
|
|
|
SetConsoleOutputCP (origCP);
|
|
|
|
HeapFree (GetProcessHeap (), 0, buf);
|
|
|
|
detach:
|
2019-09-04 09:45:35 +08:00
|
|
|
restore_reattach_pcon ();
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
2009-07-25 04:54:33 +08:00
|
|
|
ssize_t __stdcall
|
2011-06-15 05:48:43 +08:00
|
|
|
fhandler_pty_slave::write (const void *ptr, size_t len)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
ssize_t towrite = len;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2011-05-06 03:43:04 +08:00
|
|
|
bg_check_types bg = bg_check (SIGTTOU);
|
|
|
|
if (bg <= bg_eof)
|
|
|
|
return (ssize_t) bg;
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
termios_printf ("pty%d, write(%p, %lu)", get_minor (), ptr, len);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2011-05-06 03:43:04 +08:00
|
|
|
push_process_state process_state (PID_TTYOU);
|
|
|
|
|
2019-08-28 02:04:02 +08:00
|
|
|
reset_switch_to_pcon ();
|
|
|
|
|
2019-09-18 22:29:19 +08:00
|
|
|
UINT target_code_page = get_ttyp ()->switch_to_pcon_out ?
|
2019-09-15 12:05:52 +08:00
|
|
|
GetConsoleOutputCP () : get_ttyp ()->term_code_page;
|
2019-09-18 22:29:19 +08:00
|
|
|
ssize_t nlen;
|
|
|
|
char *buf = convert_mb_str (target_code_page, (size_t *) &nlen,
|
|
|
|
get_ttyp ()->term_code_page, (const char *) ptr, len);
|
2019-08-28 02:04:02 +08:00
|
|
|
|
2019-09-04 09:45:35 +08:00
|
|
|
/* If not attached to this pseudo console, try to attach temporarily. */
|
|
|
|
pid_restore = 0;
|
|
|
|
bool fallback = false;
|
2019-09-14 05:48:14 +08:00
|
|
|
if (get_ttyp ()->switch_to_pcon_out && pcon_attached_to != get_minor ())
|
2019-09-04 09:45:35 +08:00
|
|
|
if (!try_reattach_pcon ())
|
|
|
|
fallback = true;
|
|
|
|
|
2020-02-09 22:46:59 +08:00
|
|
|
if (get_ttyp ()->switch_to_pcon_out && !fallback &&
|
|
|
|
(memmem (buf, nlen, "\033[6n", 4) || memmem (buf, nlen, "\033[0c", 4)))
|
|
|
|
{
|
|
|
|
get_ttyp ()->pcon_in_empty = false;
|
|
|
|
if (!is_line_input ())
|
|
|
|
SetEvent (input_available_event);
|
|
|
|
}
|
|
|
|
|
2019-08-28 02:04:02 +08:00
|
|
|
DWORD dwMode, flags;
|
|
|
|
flags = ENABLE_VIRTUAL_TERMINAL_PROCESSING;
|
|
|
|
if (!(get_ttyp ()->ti.c_oflag & OPOST) ||
|
|
|
|
!(get_ttyp ()->ti.c_oflag & ONLCR))
|
|
|
|
flags |= DISABLE_NEWLINE_AUTO_RETURN;
|
2019-09-14 05:48:14 +08:00
|
|
|
if (get_ttyp ()->switch_to_pcon_out && !fallback)
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
|
|
|
GetConsoleMode (get_output_handle (), &dwMode);
|
|
|
|
SetConsoleMode (get_output_handle (), dwMode | flags);
|
|
|
|
}
|
2019-09-14 05:48:14 +08:00
|
|
|
HANDLE to = (get_ttyp ()->switch_to_pcon_out && !fallback) ?
|
2019-09-04 09:45:35 +08:00
|
|
|
get_output_handle () : get_output_handle_cyg ();
|
2019-08-28 02:04:02 +08:00
|
|
|
acquire_output_mutex (INFINITE);
|
|
|
|
if (!process_opost_output (to, buf, nlen, false))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2015-03-25 19:42:38 +08:00
|
|
|
DWORD err = GetLastError ();
|
|
|
|
termios_printf ("WriteFile failed, %E");
|
|
|
|
switch (err)
|
2000-09-08 00:23:51 +08:00
|
|
|
{
|
2015-03-25 19:42:38 +08:00
|
|
|
case ERROR_NO_DATA:
|
|
|
|
err = ERROR_IO_DEVICE;
|
2016-11-24 17:51:32 +08:00
|
|
|
/*FALLTHRU*/
|
2015-03-25 19:42:38 +08:00
|
|
|
default:
|
|
|
|
__seterrno_from_win_error (err);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2015-03-25 19:42:38 +08:00
|
|
|
towrite = -1;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2019-08-28 02:04:02 +08:00
|
|
|
release_output_mutex ();
|
2019-09-18 22:29:19 +08:00
|
|
|
mb_str_free (buf);
|
2019-08-28 02:04:02 +08:00
|
|
|
flags = ENABLE_VIRTUAL_TERMINAL_PROCESSING;
|
2019-09-14 05:48:14 +08:00
|
|
|
if (get_ttyp ()->switch_to_pcon_out && !fallback)
|
2019-09-08 21:23:23 +08:00
|
|
|
SetConsoleMode (get_output_handle (), dwMode | flags);
|
2019-09-04 09:45:35 +08:00
|
|
|
|
|
|
|
restore_reattach_pcon ();
|
2019-08-28 02:04:02 +08:00
|
|
|
|
|
|
|
/* Push slave output to pseudo console screen buffer */
|
2019-11-16 07:27:24 +08:00
|
|
|
if (get_pseudo_console ())
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
|
|
|
acquire_output_mutex (INFINITE);
|
2020-02-09 22:46:01 +08:00
|
|
|
push_to_pcon_screenbuffer ((char *)ptr, len, false);
|
2019-08-28 02:04:02 +08:00
|
|
|
release_output_mutex ();
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
return towrite;
|
|
|
|
}
|
|
|
|
|
2020-02-09 22:46:00 +08:00
|
|
|
void
|
|
|
|
fhandler_pty_slave::mask_switch_to_pcon_in (bool mask)
|
|
|
|
{
|
|
|
|
get_ttyp ()->mask_switch_to_pcon_in = mask;
|
|
|
|
}
|
|
|
|
|
2019-09-05 12:22:54 +08:00
|
|
|
bool
|
|
|
|
fhandler_pty_common::to_be_read_from_pcon (void)
|
|
|
|
{
|
2020-02-09 22:46:59 +08:00
|
|
|
return !get_ttyp ()->pcon_in_empty ||
|
|
|
|
(get_ttyp ()->switch_to_pcon_in && !get_ttyp ()->mask_switch_to_pcon_in);
|
2019-09-05 12:22:54 +08:00
|
|
|
}
|
|
|
|
|
2013-05-01 09:20:37 +08:00
|
|
|
void __reg3
|
2011-06-15 05:48:43 +08:00
|
|
|
fhandler_pty_slave::read (void *ptr, size_t& len)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2019-08-28 02:04:02 +08:00
|
|
|
char *ptr0 = (char *)ptr;
|
2013-04-23 17:44:36 +08:00
|
|
|
ssize_t totalread = 0;
|
2002-10-16 12:54:08 +08:00
|
|
|
int vmin = 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
int vtime = 0; /* Initialized to prevent -Wuninitialized warning */
|
2001-03-04 23:34:25 +08:00
|
|
|
size_t readlen;
|
|
|
|
DWORD bytes_in_pipe;
|
2000-02-18 03:38:33 +08:00
|
|
|
char buf[INP_BUFFER_SIZE];
|
2001-03-04 23:34:25 +08:00
|
|
|
DWORD time_to_wait;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2011-05-06 03:43:04 +08:00
|
|
|
bg_check_types bg = bg_check (SIGTTIN);
|
|
|
|
if (bg <= bg_eof)
|
|
|
|
{
|
|
|
|
len = (size_t) bg;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-28 02:04:02 +08:00
|
|
|
termios_printf ("read(%p, %lu) handle %p", ptr, len, get_handle_cyg ());
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2011-05-06 03:43:04 +08:00
|
|
|
push_process_state process_state (PID_TTYIN);
|
|
|
|
|
2019-08-28 02:04:02 +08:00
|
|
|
if (ptr) /* Indicating not tcflush(). */
|
|
|
|
{
|
2019-09-14 05:48:14 +08:00
|
|
|
mask_switch_to_pcon_in (true);
|
2020-02-09 22:46:02 +08:00
|
|
|
reset_switch_to_pcon ();
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
2020-02-09 22:46:59 +08:00
|
|
|
if (to_be_read_from_pcon ())
|
|
|
|
update_pcon_input_state (true);
|
2019-08-28 02:04:02 +08:00
|
|
|
|
2011-05-03 15:58:13 +08:00
|
|
|
if (is_nonblocking () || !ptr) /* Indicating tcflush(). */
|
2003-09-19 02:54:48 +08:00
|
|
|
time_to_wait = 0;
|
|
|
|
else if ((get_ttyp ()->ti.c_lflag & ICANON))
|
2002-10-16 12:54:08 +08:00
|
|
|
time_to_wait = INFINITE;
|
|
|
|
else
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2002-05-19 04:27:49 +08:00
|
|
|
vmin = get_ttyp ()->ti.c_cc[VMIN];
|
|
|
|
if (vmin > INP_BUFFER_SIZE)
|
|
|
|
vmin = INP_BUFFER_SIZE;
|
2000-02-18 03:38:33 +08:00
|
|
|
vtime = get_ttyp ()->ti.c_cc[VTIME];
|
2002-08-31 11:35:50 +08:00
|
|
|
if (vmin < 0)
|
|
|
|
vmin = 0;
|
|
|
|
if (vtime < 0)
|
|
|
|
vtime = 0;
|
2002-10-16 12:54:08 +08:00
|
|
|
if (!vmin && !vtime)
|
|
|
|
time_to_wait = 0;
|
|
|
|
else
|
|
|
|
time_to_wait = !vtime ? INFINITE : 100 * vtime;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2001-03-04 23:34:25 +08:00
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
while (len)
|
|
|
|
{
|
2011-12-10 00:02:56 +08:00
|
|
|
switch (cygwait (input_available_event, time_to_wait))
|
2004-11-26 12:15:10 +08:00
|
|
|
{
|
2011-05-03 15:58:13 +08:00
|
|
|
case WAIT_OBJECT_0:
|
2004-11-26 12:15:10 +08:00
|
|
|
break;
|
2012-06-18 04:50:24 +08:00
|
|
|
case WAIT_SIGNALED:
|
2011-05-03 15:58:13 +08:00
|
|
|
if (totalread > 0)
|
|
|
|
goto out;
|
|
|
|
termios_printf ("wait catched signal");
|
|
|
|
set_sig_errno (EINTR);
|
|
|
|
totalread = -1;
|
|
|
|
goto out;
|
2012-06-18 04:50:24 +08:00
|
|
|
case WAIT_CANCELED:
|
2011-05-06 03:43:04 +08:00
|
|
|
process_state.pop ();
|
2011-05-03 15:58:13 +08:00
|
|
|
pthread::static_cancel_self ();
|
|
|
|
/*NOTREACHED*/
|
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
termios_printf ("wait timed out, time_to_wait %u", time_to_wait);
|
2015-02-26 00:46:57 +08:00
|
|
|
/* No error condition when called from tcflush. */
|
|
|
|
if (!totalread && ptr)
|
2011-05-03 15:58:13 +08:00
|
|
|
{
|
|
|
|
set_sig_errno (EAGAIN);
|
|
|
|
totalread = -1;
|
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
default:
|
2002-10-16 12:54:08 +08:00
|
|
|
termios_printf ("wait for input event failed, %E");
|
2011-05-03 15:58:13 +08:00
|
|
|
if (!totalread)
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
totalread = -1;
|
|
|
|
}
|
|
|
|
goto out;
|
2002-10-16 12:54:08 +08:00
|
|
|
}
|
2011-05-03 15:58:13 +08:00
|
|
|
/* Now that we know that input is available we have to grab the
|
|
|
|
input mutex. */
|
2011-12-10 00:02:56 +08:00
|
|
|
switch (cygwait (input_mutex, 1000))
|
2001-03-11 09:58:23 +08:00
|
|
|
{
|
2011-05-03 15:58:13 +08:00
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
case WAIT_ABANDONED_0:
|
|
|
|
break;
|
2012-06-18 04:50:24 +08:00
|
|
|
case WAIT_SIGNALED:
|
2001-03-11 09:58:23 +08:00
|
|
|
if (totalread > 0)
|
2011-05-03 15:58:13 +08:00
|
|
|
goto out;
|
2012-06-18 04:50:24 +08:00
|
|
|
termios_printf ("wait for mutex caught signal");
|
2001-03-11 09:58:23 +08:00
|
|
|
set_sig_errno (EINTR);
|
2011-05-03 15:58:13 +08:00
|
|
|
totalread = -1;
|
|
|
|
goto out;
|
2012-06-18 04:50:24 +08:00
|
|
|
case WAIT_CANCELED:
|
2011-05-06 03:43:04 +08:00
|
|
|
process_state.pop ();
|
2011-05-03 15:58:13 +08:00
|
|
|
pthread::static_cancel_self ();
|
|
|
|
/*NOTREACHED*/
|
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
termios_printf ("failed to acquire input mutex after input event "
|
|
|
|
"arrived");
|
|
|
|
/* If we have a timeout, we can simply handle this failure to
|
|
|
|
grab the mutex as an EAGAIN situation. Otherwise, if this
|
|
|
|
is an infinitely blocking read, restart the loop. */
|
|
|
|
if (time_to_wait != INFINITE)
|
|
|
|
{
|
|
|
|
if (!totalread)
|
|
|
|
{
|
|
|
|
set_sig_errno (EAGAIN);
|
|
|
|
totalread = -1;
|
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
default:
|
2001-03-11 09:58:23 +08:00
|
|
|
termios_printf ("wait for input mutex failed, %E");
|
2011-05-03 15:58:13 +08:00
|
|
|
if (!totalread)
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
totalread = -1;
|
|
|
|
}
|
|
|
|
goto out;
|
2001-03-11 09:58:23 +08:00
|
|
|
}
|
2020-02-09 22:46:59 +08:00
|
|
|
if (ptr && to_be_read_from_pcon ())
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
2020-02-09 22:46:59 +08:00
|
|
|
if (get_readahead_valid ())
|
2019-09-04 09:45:35 +08:00
|
|
|
{
|
2020-02-09 22:46:59 +08:00
|
|
|
ReleaseMutex (input_mutex);
|
|
|
|
totalread = get_readahead_into_buffer ((char *) ptr, len);
|
2019-09-04 09:45:35 +08:00
|
|
|
}
|
2020-02-09 22:46:59 +08:00
|
|
|
else
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
2020-02-09 22:46:59 +08:00
|
|
|
if (!try_reattach_pcon ())
|
|
|
|
{
|
|
|
|
restore_reattach_pcon ();
|
|
|
|
goto do_read_cyg;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD dwMode;
|
|
|
|
GetConsoleMode (get_handle (), &dwMode);
|
|
|
|
DWORD flags = ENABLE_VIRTUAL_TERMINAL_INPUT;
|
|
|
|
if (dwMode != flags)
|
|
|
|
SetConsoleMode (get_handle (), flags);
|
|
|
|
/* Read get_handle() instad of get_handle_cyg() */
|
|
|
|
BOOL (WINAPI *ReadFunc)
|
|
|
|
(HANDLE, LPVOID, DWORD, LPDWORD, LPVOID);
|
|
|
|
ReadFunc = ReadConsoleA_Orig ? ReadConsoleA_Orig : ReadConsoleA;
|
|
|
|
DWORD rlen;
|
|
|
|
readlen = MIN (len, sizeof (buf));
|
|
|
|
if (!ReadFunc (get_handle (), buf, readlen, &rlen, NULL))
|
|
|
|
{
|
|
|
|
termios_printf ("read failed, %E");
|
|
|
|
SetConsoleMode (get_handle (), dwMode);
|
|
|
|
restore_reattach_pcon ();
|
|
|
|
ReleaseMutex (input_mutex);
|
|
|
|
set_errno (EIO);
|
|
|
|
totalread = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
SetConsoleMode (get_handle (), dwMode);
|
|
|
|
restore_reattach_pcon ();
|
2019-08-28 02:04:02 +08:00
|
|
|
ReleaseMutex (input_mutex);
|
2020-02-09 22:46:59 +08:00
|
|
|
|
|
|
|
ssize_t nlen;
|
|
|
|
char *nbuf = convert_mb_str (get_ttyp ()->term_code_page,
|
|
|
|
(size_t *) &nlen, GetConsoleCP (), buf, rlen);
|
|
|
|
|
|
|
|
ssize_t ret;
|
|
|
|
line_edit_status res =
|
|
|
|
line_edit (nbuf, nlen, get_ttyp ()->ti, &ret);
|
|
|
|
|
|
|
|
mb_str_free (nbuf);
|
|
|
|
|
|
|
|
if (res == line_edit_input_done || res == line_edit_ok)
|
|
|
|
totalread = get_readahead_into_buffer ((char *) ptr, len);
|
|
|
|
else if (res > line_edit_signalled)
|
|
|
|
{
|
|
|
|
set_sig_errno (EINTR);
|
|
|
|
totalread = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
update_pcon_input_state (true);
|
|
|
|
continue;
|
|
|
|
}
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
2019-09-04 09:45:35 +08:00
|
|
|
|
2020-02-09 22:46:59 +08:00
|
|
|
update_pcon_input_state (true);
|
2019-09-14 05:48:14 +08:00
|
|
|
mask_switch_to_pcon_in (false);
|
2020-02-09 22:46:59 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (!ptr && len == UINT_MAX && !get_ttyp ()->pcon_in_empty)
|
|
|
|
{
|
|
|
|
FlushConsoleInputBuffer (get_handle ());
|
|
|
|
get_ttyp ()->pcon_in_empty = true;
|
|
|
|
DWORD n;
|
|
|
|
if (bytes_available (n) && n == 0)
|
|
|
|
ResetEvent (input_available_event);
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
2019-09-04 09:45:35 +08:00
|
|
|
|
|
|
|
do_read_cyg:
|
2012-04-05 10:54:51 +08:00
|
|
|
if (!bytes_available (bytes_in_pipe))
|
2015-06-08 20:07:42 +08:00
|
|
|
{
|
|
|
|
ReleaseMutex (input_mutex);
|
|
|
|
set_errno (EIO);
|
|
|
|
totalread = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
2002-10-16 12:54:08 +08:00
|
|
|
|
2005-05-02 08:17:45 +08:00
|
|
|
if (ptr && !bytes_in_pipe && !vmin && !time_to_wait)
|
2002-10-16 12:54:08 +08:00
|
|
|
{
|
|
|
|
ReleaseMutex (input_mutex);
|
2019-09-14 05:48:14 +08:00
|
|
|
mask_switch_to_pcon_in (false);
|
2003-09-08 02:27:54 +08:00
|
|
|
len = (size_t) bytes_in_pipe;
|
2002-12-14 12:01:32 +08:00
|
|
|
return;
|
2002-10-16 12:54:08 +08:00
|
|
|
}
|
|
|
|
|
2016-06-06 21:09:34 +08:00
|
|
|
readlen = bytes_in_pipe ? MIN (len, sizeof (buf)) : 0;
|
|
|
|
if (get_ttyp ()->ti.c_lflag & ICANON && ptr)
|
|
|
|
readlen = MIN (bytes_in_pipe, readlen);
|
2002-10-16 12:54:08 +08:00
|
|
|
|
2014-11-14 02:37:15 +08:00
|
|
|
#if 0
|
|
|
|
/* Why on earth is the read length reduced to vmin, even if more bytes
|
|
|
|
are available *and* len is bigger *and* the local buf is big enough?
|
|
|
|
Disable this code for now, it looks like a remnant of old. */
|
2003-09-19 02:54:48 +08:00
|
|
|
if (ptr && vmin && readlen > (unsigned) vmin)
|
2002-10-16 12:54:08 +08:00
|
|
|
readlen = vmin;
|
2014-11-14 02:37:15 +08:00
|
|
|
#endif
|
2002-10-16 12:54:08 +08:00
|
|
|
|
|
|
|
DWORD n = 0;
|
2001-03-11 09:58:23 +08:00
|
|
|
if (readlen)
|
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
termios_printf ("reading %lu bytes (vtime %d)", readlen, vtime);
|
2019-08-28 02:04:02 +08:00
|
|
|
if (!ReadFile (get_handle_cyg (), buf, readlen, &n, NULL))
|
2001-03-11 09:58:23 +08:00
|
|
|
{
|
2001-03-04 23:34:25 +08:00
|
|
|
termios_printf ("read failed, %E");
|
2015-06-08 20:07:42 +08:00
|
|
|
ReleaseMutex (input_mutex);
|
|
|
|
set_errno (EIO);
|
|
|
|
totalread = -1;
|
|
|
|
goto out;
|
2001-03-19 02:05:01 +08:00
|
|
|
}
|
2011-12-10 00:02:56 +08:00
|
|
|
else
|
2001-03-11 09:58:23 +08:00
|
|
|
{
|
2011-12-10 00:02:56 +08:00
|
|
|
/* MSDN states that 5th prameter can be used to determine total
|
|
|
|
number of bytes in pipe, but for some reason this number doesn't
|
|
|
|
change after successful read. So we have to peek into the pipe
|
|
|
|
again to see if input is still available */
|
2012-04-05 10:54:51 +08:00
|
|
|
if (!bytes_available (bytes_in_pipe))
|
2015-06-08 20:07:42 +08:00
|
|
|
{
|
|
|
|
ReleaseMutex (input_mutex);
|
|
|
|
set_errno (EIO);
|
|
|
|
totalread = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
2011-12-10 00:02:56 +08:00
|
|
|
if (n)
|
|
|
|
{
|
2016-06-06 21:09:34 +08:00
|
|
|
if (!(!ptr && len == UINT_MAX)) /* not tcflush() */
|
|
|
|
len -= n;
|
2011-12-10 00:02:56 +08:00
|
|
|
totalread += n;
|
|
|
|
if (ptr)
|
|
|
|
{
|
|
|
|
memcpy (ptr, buf, n);
|
|
|
|
ptr = (char *) ptr + n;
|
|
|
|
}
|
2003-09-19 02:54:48 +08:00
|
|
|
}
|
2001-03-11 09:58:23 +08:00
|
|
|
}
|
|
|
|
}
|
2001-03-04 23:34:25 +08:00
|
|
|
|
2001-03-19 02:05:01 +08:00
|
|
|
if (!bytes_in_pipe)
|
|
|
|
ResetEvent (input_available_event);
|
2001-03-04 23:34:25 +08:00
|
|
|
|
|
|
|
ReleaseMutex (input_mutex);
|
2000-03-12 14:29:54 +08:00
|
|
|
|
2003-09-19 02:54:48 +08:00
|
|
|
if (!ptr)
|
|
|
|
{
|
|
|
|
if (!bytes_in_pipe)
|
|
|
|
break;
|
|
|
|
continue;
|
2004-05-29 03:50:07 +08:00
|
|
|
}
|
2003-09-19 02:54:48 +08:00
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
if (get_ttyp ()->read_retval < 0) // read error
|
|
|
|
{
|
|
|
|
set_errno (-get_ttyp ()->read_retval);
|
|
|
|
totalread = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (get_ttyp ()->read_retval == 0) //EOF
|
|
|
|
{
|
|
|
|
termios_printf ("saw EOF");
|
|
|
|
break;
|
|
|
|
}
|
2001-08-15 15:49:15 +08:00
|
|
|
if (get_ttyp ()->ti.c_lflag & ICANON || is_nonblocking ())
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
2002-10-16 12:54:08 +08:00
|
|
|
if (vmin && totalread >= vmin)
|
2001-03-11 09:58:23 +08:00
|
|
|
break;
|
2001-03-04 23:34:25 +08:00
|
|
|
|
2001-03-11 09:58:23 +08:00
|
|
|
/* vmin == 0 && vtime == 0:
|
2001-03-04 23:34:25 +08:00
|
|
|
* we've already read all input, if any, so return immediately
|
|
|
|
* vmin == 0 && vtime > 0:
|
|
|
|
* we've waited for input 10*vtime ms in WFSO(input_available_event),
|
|
|
|
* no matter whether any input arrived, we shouldn't wait any longer,
|
2001-03-11 09:58:23 +08:00
|
|
|
* so return immediately
|
2001-03-04 23:34:25 +08:00
|
|
|
* vmin > 0 && vtime == 0:
|
|
|
|
* here, totalread < vmin, so continue waiting until more data
|
|
|
|
* arrive
|
|
|
|
* vmin > 0 && vtime > 0:
|
|
|
|
* similar to the previous here, totalread < vmin, and timer
|
|
|
|
* hadn't expired -- WFSO(input_available_event) != WAIT_TIMEOUT,
|
|
|
|
* so "restart timer" and wait until more data arrive
|
|
|
|
*/
|
|
|
|
|
2001-03-11 09:58:23 +08:00
|
|
|
if (vmin == 0)
|
|
|
|
break;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2011-05-03 15:58:13 +08:00
|
|
|
out:
|
2018-07-24 13:31:58 +08:00
|
|
|
termios_printf ("%d = read(%p, %lu)", totalread, ptr, len);
|
2003-09-08 02:27:54 +08:00
|
|
|
len = (size_t) totalread;
|
2019-08-28 02:04:02 +08:00
|
|
|
/* Push slave read as echo to pseudo console screen buffer. */
|
2020-02-09 22:46:59 +08:00
|
|
|
if (get_pseudo_console () && ptr0 && totalread > 0 &&
|
|
|
|
(get_ttyp ()->ti.c_lflag & ECHO))
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
|
|
|
acquire_output_mutex (INFINITE);
|
2020-02-09 22:46:01 +08:00
|
|
|
push_to_pcon_screenbuffer (ptr0, len, true);
|
|
|
|
if (get_ttyp ()->switch_to_pcon_out)
|
|
|
|
trigger_redraw_screen ();
|
2019-08-28 02:04:02 +08:00
|
|
|
release_output_mutex ();
|
|
|
|
}
|
2019-09-14 05:48:14 +08:00
|
|
|
mask_switch_to_pcon_in (false);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2003-03-04 12:07:34 +08:00
|
|
|
int
|
2011-10-16 06:37:30 +08:00
|
|
|
fhandler_pty_slave::dup (fhandler_base *child, int flags)
|
2003-03-04 12:07:34 +08:00
|
|
|
{
|
2012-03-04 05:35:30 +08:00
|
|
|
/* This code was added in Oct 2001 for some undisclosed reason.
|
|
|
|
However, setting the controlling tty on a dup causes rxvt to
|
|
|
|
hang when the parent does a dup since the controlling pgid changes.
|
|
|
|
Specifically testing for -2 (ctty has been setsid'ed) works around
|
|
|
|
this problem. However, it's difficult to see scenarios in which you
|
|
|
|
have a dup'able fd, no controlling tty, and not having run setsid.
|
|
|
|
So, we might want to consider getting rid of the set_ctty in tty-like dup
|
|
|
|
methods entirely at some point */
|
|
|
|
if (myself->ctty != -2)
|
|
|
|
myself->set_ctty (this, flags);
|
2011-05-06 06:30:53 +08:00
|
|
|
report_tty_counts (child, "duped slave", "");
|
2003-12-11 14:12:41 +08:00
|
|
|
return 0;
|
2003-03-04 12:07:34 +08:00
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
int
|
2011-10-16 06:37:30 +08:00
|
|
|
fhandler_pty_master::dup (fhandler_base *child, int)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2006-06-02 23:41:34 +08:00
|
|
|
report_tty_counts (child, "duped master", "");
|
2000-02-18 03:38:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-06-15 05:48:43 +08:00
|
|
|
fhandler_pty_slave::tcgetattr (struct termios *t)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2019-08-28 02:04:02 +08:00
|
|
|
reset_switch_to_pcon ();
|
2000-02-18 03:38:33 +08:00
|
|
|
*t = get_ttyp ()->ti;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-06-15 05:48:43 +08:00
|
|
|
fhandler_pty_slave::tcsetattr (int, const struct termios *t)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2019-08-28 02:04:02 +08:00
|
|
|
reset_switch_to_pcon ();
|
2000-02-18 03:38:33 +08:00
|
|
|
acquire_output_mutex (INFINITE);
|
|
|
|
get_ttyp ()->ti = *t;
|
|
|
|
release_output_mutex ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-06-15 05:48:43 +08:00
|
|
|
fhandler_pty_slave::tcflush (int queue)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2003-09-19 02:54:48 +08:00
|
|
|
int ret = 0;
|
|
|
|
|
2019-08-28 02:04:02 +08:00
|
|
|
termios_printf ("tcflush(%d) handle %p", queue, get_handle_cyg ());
|
|
|
|
|
|
|
|
reset_switch_to_pcon ();
|
2003-09-19 02:54:48 +08:00
|
|
|
|
|
|
|
if (queue == TCIFLUSH || queue == TCIOFLUSH)
|
|
|
|
{
|
|
|
|
size_t len = UINT_MAX;
|
|
|
|
read (NULL, len);
|
2005-08-06 00:11:21 +08:00
|
|
|
ret = ((int) len) >= 0 ? 0 : -1;
|
2003-09-19 02:54:48 +08:00
|
|
|
}
|
|
|
|
if (queue == TCOFLUSH || queue == TCIOFLUSH)
|
|
|
|
{
|
|
|
|
/* do nothing for now. */
|
|
|
|
}
|
|
|
|
|
|
|
|
termios_printf ("%d=tcflush(%d)", ret, queue);
|
|
|
|
return ret;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-06-15 05:48:43 +08:00
|
|
|
fhandler_pty_slave::ioctl (unsigned int cmd, void *arg)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
termios_printf ("ioctl (%x)", cmd);
|
2019-08-28 02:04:02 +08:00
|
|
|
reset_switch_to_pcon ();
|
2011-10-16 06:37:30 +08:00
|
|
|
int res = fhandler_termios::ioctl (cmd, arg);
|
2011-10-12 07:20:38 +08:00
|
|
|
if (res <= 0)
|
|
|
|
return res;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-08-31 11:35:50 +08:00
|
|
|
if (myself->pgid && get_ttyp ()->getpgid () != myself->pgid
|
2013-04-23 17:44:36 +08:00
|
|
|
&& (unsigned) myself->ctty == FHDEV (DEV_PTYS_MAJOR, get_minor ())
|
2011-05-29 02:17:09 +08:00
|
|
|
&& (get_ttyp ()->ti.c_lflag & TOSTOP))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
/* background process */
|
2005-11-14 12:28:45 +08:00
|
|
|
termios_printf ("bg ioctl pgid %d, tpgid %d, %s", myself->pgid,
|
|
|
|
get_ttyp ()->getpgid (), myctty ());
|
2002-10-20 12:15:50 +08:00
|
|
|
raise (SIGTTOU);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2000-05-12 13:06:43 +08:00
|
|
|
|
2003-10-24 09:13:22 +08:00
|
|
|
int retval;
|
2000-02-18 03:38:33 +08:00
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case TIOCGWINSZ:
|
|
|
|
case TIOCSWINSZ:
|
|
|
|
break;
|
2010-10-24 02:07:08 +08:00
|
|
|
case TIOCGPGRP:
|
|
|
|
{
|
|
|
|
pid_t pid = this->tcgetpgrp ();
|
|
|
|
if (pid < 0)
|
|
|
|
retval = -1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*((pid_t *) arg) = pid;
|
|
|
|
retval = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
case TIOCSPGRP:
|
2013-04-23 17:44:36 +08:00
|
|
|
retval = this->tcsetpgrp ((pid_t) (intptr_t) arg);
|
2010-10-24 02:07:08 +08:00
|
|
|
goto out;
|
2011-07-23 02:50:42 +08:00
|
|
|
case FIONREAD:
|
|
|
|
{
|
2012-04-05 10:54:51 +08:00
|
|
|
DWORD n;
|
|
|
|
if (!bytes_available (n))
|
2011-07-23 02:50:42 +08:00
|
|
|
{
|
2011-07-25 23:19:35 +08:00
|
|
|
set_errno (EINVAL);
|
2011-07-23 02:50:42 +08:00
|
|
|
retval = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-05 10:54:51 +08:00
|
|
|
*(int *) arg = (int) n;
|
2011-07-23 02:50:42 +08:00
|
|
|
retval = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto out;
|
2000-02-18 03:38:33 +08:00
|
|
|
default:
|
2011-07-22 04:21:46 +08:00
|
|
|
return fhandler_base::ioctl (cmd, arg);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2000-05-12 13:06:43 +08:00
|
|
|
|
|
|
|
acquire_output_mutex (INFINITE);
|
|
|
|
|
|
|
|
get_ttyp ()->cmd = cmd;
|
|
|
|
get_ttyp ()->ioctl_retval = 0;
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case TIOCGWINSZ:
|
|
|
|
get_ttyp ()->arg.winsize = get_ttyp ()->winsize;
|
2002-11-04 09:10:38 +08:00
|
|
|
*(struct winsize *) arg = get_ttyp ()->arg.winsize;
|
2000-05-12 13:06:43 +08:00
|
|
|
get_ttyp ()->winsize = get_ttyp ()->arg.winsize;
|
|
|
|
break;
|
|
|
|
case TIOCSWINSZ:
|
2019-11-16 07:27:24 +08:00
|
|
|
if (get_pseudo_console ())
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
2019-09-04 09:45:35 +08:00
|
|
|
/* If not attached to this pseudo console,
|
|
|
|
try to attach temporarily. */
|
|
|
|
pid_restore = 0;
|
|
|
|
if (pcon_attached_to != get_minor ())
|
|
|
|
if (!try_reattach_pcon ())
|
|
|
|
goto cleanup;
|
2019-08-28 02:04:02 +08:00
|
|
|
|
|
|
|
COORD size;
|
|
|
|
size.X = ((struct winsize *) arg)->ws_col;
|
|
|
|
size.Y = ((struct winsize *) arg)->ws_row;
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
|
|
|
if (GetConsoleScreenBufferInfo (get_output_handle (), &csbi))
|
|
|
|
if (size.X == csbi.srWindow.Right - csbi.srWindow.Left + 1 &&
|
|
|
|
size.Y == csbi.srWindow.Bottom - csbi.srWindow.Top + 1)
|
|
|
|
goto cleanup;
|
|
|
|
if (!SetConsoleScreenBufferSize (get_output_handle (), size))
|
|
|
|
goto cleanup;
|
|
|
|
SMALL_RECT rect;
|
|
|
|
rect.Left = 0;
|
|
|
|
rect.Top = 0;
|
|
|
|
rect.Right = size.X-1;
|
|
|
|
rect.Bottom = size.Y-1;
|
|
|
|
SetConsoleWindowInfo (get_output_handle (), TRUE, &rect);
|
|
|
|
cleanup:
|
2019-09-04 09:45:35 +08:00
|
|
|
restore_reattach_pcon ();
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
2019-09-04 09:45:35 +08:00
|
|
|
|
2002-11-04 09:10:38 +08:00
|
|
|
if (get_ttyp ()->winsize.ws_row != ((struct winsize *) arg)->ws_row
|
|
|
|
|| get_ttyp ()->winsize.ws_col != ((struct winsize *) arg)->ws_col)
|
|
|
|
{
|
2004-01-21 14:28:35 +08:00
|
|
|
get_ttyp ()->arg.winsize = *(struct winsize *) arg;
|
2011-06-15 05:48:43 +08:00
|
|
|
get_ttyp ()->winsize = *(struct winsize *) arg;
|
2013-01-31 13:26:47 +08:00
|
|
|
get_ttyp ()->kill_pgrp (SIGWINCH);
|
2002-11-04 09:10:38 +08:00
|
|
|
}
|
2000-05-12 13:06:43 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
release_output_mutex ();
|
2003-10-24 09:13:22 +08:00
|
|
|
retval = get_ttyp ()->ioctl_retval;
|
2003-09-27 11:14:07 +08:00
|
|
|
if (retval < 0)
|
|
|
|
{
|
|
|
|
set_errno (-retval);
|
|
|
|
retval = -1;
|
|
|
|
}
|
2003-10-24 09:13:22 +08:00
|
|
|
|
|
|
|
out:
|
2011-12-04 05:43:27 +08:00
|
|
|
termios_printf ("%d = ioctl(%x)", retval, cmd);
|
2003-09-27 11:14:07 +08:00
|
|
|
return retval;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2013-01-21 12:34:52 +08:00
|
|
|
int __reg2
|
2013-04-23 17:44:36 +08:00
|
|
|
fhandler_pty_slave::fstat (struct stat *st)
|
2010-04-20 03:52:43 +08:00
|
|
|
{
|
|
|
|
fhandler_base::fstat (st);
|
|
|
|
|
|
|
|
bool to_close = false;
|
|
|
|
if (!input_available_event)
|
|
|
|
{
|
|
|
|
char buf[MAX_PATH];
|
2013-04-23 17:44:36 +08:00
|
|
|
shared_name (buf, INPUT_AVAILABLE_EVENT, get_minor ());
|
2010-04-20 03:52:43 +08:00
|
|
|
input_available_event = OpenEvent (READ_CONTROL, TRUE, buf);
|
|
|
|
if (input_available_event)
|
|
|
|
to_close = true;
|
|
|
|
}
|
2015-09-04 01:29:55 +08:00
|
|
|
st->st_mode = S_IFCHR;
|
2010-04-20 03:52:43 +08:00
|
|
|
if (!input_available_event
|
|
|
|
|| get_object_attribute (input_available_event, &st->st_uid, &st->st_gid,
|
|
|
|
&st->st_mode))
|
|
|
|
{
|
|
|
|
/* If we can't access the ACL, or if the tty doesn't actually exist,
|
2011-06-06 13:02:13 +08:00
|
|
|
then fake uid and gid to strict, system-like values. */
|
2010-04-20 03:52:43 +08:00
|
|
|
st->st_mode = S_IFCHR | S_IRUSR | S_IWUSR;
|
|
|
|
st->st_uid = 18;
|
|
|
|
st->st_gid = 544;
|
|
|
|
}
|
|
|
|
if (to_close)
|
|
|
|
CloseHandle (input_available_event);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
Reapply POSIX ACL changes.
- New, unified implementation of POSIX permission and ACL handling. The
new ACLs now store the POSIX ACL MASK/CLASS_OBJ permission mask, and
they allow to inherit the S_ISGID bit. ACL inheritance now really
works as desired, in a limited, but theoretically equivalent fashion
even for non-Cygwin processes.
To accommodate Windows default ACLs, the new code ignores SYSTEM and
Administrators group permissions when computing the MASK/CLASS_OBJ
permission mask on old ACLs, and it doesn't deny access to SYSTEM and
Administrators group based on the value of MASK/CLASS_OBJ when
creating the new ACLs.
The new code now handles the S_ISGID bit on directories as on Linux:
Setting S_ISGID on a directory causes new files and subdirs created
within to inherit its group, rather than the primary group of the user
who created the file. This only works for files and directories
created by Cygwin processes.
2015-05-29 Corinna Vinschen <corinna@vinschen.de>
Reapply POSIX ACL changes.
* utils.xml (setfacl): Show new option output.
(getfacl): Show new option output.
* sec_acl.cc (get_posix_access): Check for Cygwin "standard" ACL.
Apply umask, if so. Align comments.
* security.cc (set_created_file_access): Fix permission masking by
incoming requested file mode.
* sec_acl.cc (set_posix_access): Apply mask only in terms of execute bit
for SYSTEM and Admins group.
* sec_acl.cc (set_posix_access): Don't create DENY ACEs for USER and
GROUP entries if they are the same as USER_OBJ or GROUP_OBJ.
* fhandler.h (fhandler_pty_slave::facl): Add prototype.
* fhandler_tty.cc (fhandler_pty_slave::facl): New method.
(fhandler_pty_slave::fchown): Fix uid/gid handling.
* sec_acl.cc (set_posix_access): Drop superfluous class_idx variable.
Simplify and move around code in a few places. To improve ACL
readability, add r/w permissions to Admins ACE appended to pty ACL.
Add comment to explain Windows ACE Mask filtering being in the way of
creating a real CLASS_OBJ.
(get_posix_access): Fake CLASS_OBJ for ptys. Explain why.
* security.cc (get_object_attribute): Add S_IFCHR flag to attributes
when calling get_posix_access.
* sec_acl.cc (set_posix_access): Move merging group perms into owner
perms in case of owner == group after mask has been computed. Take
mask into account when doing so to avoid unnecessary ACCESS_DENIED_ACE.
* sec_acl.cc (get_posix_access): Only set saw_group_obj flag if we saw
the ACCESS_ALLOWED_ACE.
* fhandler_disk_file.cc (fhandler_disk_file::fchmod): Deliberatly
set GROUP_OBJ and CLASS_OBJ perms to new group perms. Add comment
to explain why.
* security.cc (set_created_file_access): Ditto.
* sec_acl.cc (set_posix_access): Replace previous patch. Return
EINVAL if uid and/or guid is invalid and not backed by an actual
Windows account.
* sec_acl.cc (set_posix_access): Workaround owner/group SIDs being NULL.
* sec_acl.cc (set_posix_access): Handle files with owner == group.
Rephrase switch statement checking against unfiltered a_type value.
(get_posix_access): Handle files with owner == group.
* sec_acl.cc (get_posix_access): Don't use GROUP_OBJ access to fix up
CLASS_OBJ mask on old-style ACLs. Fix a comment.
* sec_acl.cc (set_posix_access): Always make sure Admins have
WRITE_DAC and WRITE_OWNER permissions.
* security.h (create_object_sd_from_attribute): Drop handle parameter
from prototype.
* security.cc (create_object_sd_from_attribute): Drop handle parameter.
Just create the standard POSIXy security descriptor.
(set_object_attribute): Accommodate dropped paramter in call to
create_object_sd_from_attribute.
* fhandler_tty.cc: Ditto, throughout.
* fhandler_disk_file.cc (fhandler_disk_file::fchmod): Fix typo in
mask computation.
* fhandler.cc (fhandler_base::open_with_arch): Call open with mode
not umasked.
(fhandler_base::open): Explicitely umask mode on NFS here. Call new
set_created_file_access rather than set_file_attribute.
* fhandler_disk_file.cc (fhandler_disk_file::fchmod): Reimplement
setting permissions on filesystems supporting ACLs using the new
set_posix_access call.
(fhandler_disk_file::fchown): Ditto.
(fhandler_disk_file::mkdir): Call new set_created_file_access rather
than set_file_attribute.
* fhandler_socket.cc (fhandler_socket::bind): Don't umask here. Add
WRITE_OWNER access to allow writing group in case of SGID bit set.
Call new set_created_file_access rather than set_file_attribute.
* path.cc (symlink_worker): Call new set_created_file_access rather
than set_file_attribute.
* sec_acl.cc (searchace): Un-staticize.
(set_posix_access): New, complementary functionality to
get_posix_access.
(setacl): Implement in terms of get_posix_access/set_posix_access.
(get_posix_access): Add handling for just created files requiring
their first Cygwin ACL. Fix new_style recognition. Handle SGID
bit. For old-style ACLs, ignore SYSTEM and Administrators when
computing the {DEF_}CLASS_OBJ perms.
* security.cc (get_file_sd): Revamp comment. Change and (hopefully)
speed up inheritance processing for just created files.
(alloc_sd): Remove.
(set_security_attribute): Call set_posix_access instead of alloc_sd.
(get_object_attribute): Fix return value.
(create_object_sd_from_attribute): Call set_posix_access instead of
alloc_sd.
(set_file_attribute): Remove.
(set_created_file_access): New function implemented in terms of
get_posix_access/set_posix_access.
* security.h (set_file_attribute): Remove prototype.
(set_created_file_access): Add prototype.
(searchace): Ditto.
(set_posix_access): Ditto.
* syscalls.cc (open): Call open_with_arch with mode not umasked.
* sec_acl.cc: Change preceeding comment explaining new-style ACLs.
Describe how to generate deny ACEs in more detail. Accommodate the
fact that a NULL deny ACE is used for {DEF_}CLASS_OBJ, rather than
a special Cygwin ACE. Improve further comments.
(CYG_ACE_NEW_STYLE): Define.
(get_posix_access): Change from Cygwin ACE to NULL deny ACE. Fix
CLASS_OBJ handling to generate CLASS_OBJ and DEF_CLASS_OBJ from a single
NULL deny ACE if the inheritance flags say so.
* sec_helper.cc (well_known_cygwin_sid): Remove.
* security.h (well_known_cygwin_sid): Drop declaration.
* sec_acl.cc (CYG_ACE_ISBITS_TO_WIN): Fix typo.
(get_posix_access): Rename index variable from i to idx. Define only
once at top level.
* security.cc (add_access_allowed_ace): Drop unused parameter "offset".
Accommodate throughout.
(add_access_denied_ace): Ditto.
* sec_acl.cc: Accommodate above change throughout.
* security.h (add_access_allowed_ace): Adjust prototype to above change.
(add_access_denied_ace): Ditto.
* sec_acl.cc (get_posix_access): Handle multiple ACEs for the
owner and primary group of the file. Handle the default primary
group ACE as DEF_GROUP_OBJ entry if the directory has the S_ISGID bit
set. Add comments. Minor code rearrangements.
Preliminary read side implementation of new permission handling.
* acl.h (MAX_ACL_ENTRIES): Raise to 2730. Add comment to explain.
* sec_acl.cc: Add leading comment to explain new ACL style.
Add definitions and macros to use for bits in new Cygwin ACL.
(DENY_RWX): New mask value for all temporary deny bits.
(getace): Add bool parameter to decide when leaving all bits intact,
rather than filtering them per the already set bits.
(get_posix_access): New function, taking over functionality to read
POSIX ACL from SECURITY_DESCRIPTOR.
(getacl): Just call get_posix_access.
* sec_helper.cc (well_known_cygwin_sid): Define.
* security.cc (get_attribute_from_acl): Remove.
(get_info_from_sd): Remove.
(get_reg_sd): Call get_posix_access instead of get_info_from_sd.
(get_file_attribute): Ditto.
(get_object_attribute): Ditto.
* security.h (well_known_cygwin_sid): Declare.
(get_posix_access): Add prototype.
* Throughout, use simpler ACE macros from Windows' accctrl.h.
* getfacl.c (main): Special-case SYSTEM and Admins group. Add comments.
* setfacl.c: Align more to Linux tool.
(delacl): New function to delete acl entries only.
(modacl): Drop delete functionality. Add handling of recomputing the
mask and default mask values.
(delallacl): Rename from delacl.
(setfacl): Call delacl in Delete case. Call delallacl in DeleteAll
and DeleteDef case.
(usage): Accommodate new options. Rearrange and rephrase slightly.
(longopts): Emit 'x' in --delete case. Add --no-mask and --mask
options.
(opts): Add -x and -n options.
(main): Handle -d and -x the same. Handle -n and --mask options.
Drop handling for -r option.
* getfacl.c (usage): Align more closely to Linux version. Add new
options -c, -e, -E. Change formatting to accommodate longer options.
(longopts): Rename --noname to --numeric. Keep --noname for backward
compatibility. Add --omit-header, --all-effective and --no-effective
options.
(opts): Add -c, -e and -E option.
(main): Handle new -c, -e, and -E options.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-06-24 17:52:29 +08:00
|
|
|
int __reg3
|
|
|
|
fhandler_pty_slave::facl (int cmd, int nentries, aclent_t *aclbufp)
|
|
|
|
{
|
|
|
|
int res = -1;
|
|
|
|
bool to_close = false;
|
|
|
|
security_descriptor sd;
|
|
|
|
mode_t attr = S_IFCHR;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case SETACL:
|
|
|
|
if (!aclsort32 (nentries, 0, aclbufp))
|
|
|
|
set_errno (ENOTSUP);
|
|
|
|
break;
|
|
|
|
case GETACL:
|
|
|
|
if (!aclbufp)
|
|
|
|
{
|
|
|
|
set_errno (EFAULT);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*FALLTHRU*/
|
|
|
|
case GETACLCNT:
|
|
|
|
if (!input_available_event)
|
|
|
|
{
|
|
|
|
char buf[MAX_PATH];
|
|
|
|
shared_name (buf, INPUT_AVAILABLE_EVENT, get_minor ());
|
|
|
|
input_available_event = OpenEvent (READ_CONTROL, TRUE, buf);
|
|
|
|
if (input_available_event)
|
|
|
|
to_close = true;
|
|
|
|
}
|
|
|
|
if (!input_available_event
|
|
|
|
|| get_object_sd (input_available_event, sd))
|
|
|
|
{
|
|
|
|
res = get_posix_access (NULL, &attr, NULL, NULL, aclbufp, nentries);
|
|
|
|
if (aclbufp && res == MIN_ACL_ENTRIES)
|
|
|
|
{
|
|
|
|
aclbufp[0].a_perm = S_IROTH | S_IWOTH;
|
|
|
|
aclbufp[0].a_id = 18;
|
|
|
|
aclbufp[1].a_id = 544;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (cmd == GETACL)
|
|
|
|
res = get_posix_access (sd, &attr, NULL, NULL, aclbufp, nentries);
|
|
|
|
else
|
|
|
|
res = get_posix_access (sd, &attr, NULL, NULL, NULL, 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
set_errno (EINVAL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (to_close)
|
|
|
|
CloseHandle (input_available_event);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2010-04-20 03:52:43 +08:00
|
|
|
/* Helper function for fchmod and fchown, which just opens all handles
|
|
|
|
and signals success via bool return. */
|
|
|
|
bool
|
2014-08-27 19:42:17 +08:00
|
|
|
fhandler_pty_slave::fch_open_handles (bool chown)
|
2010-04-20 03:52:43 +08:00
|
|
|
{
|
|
|
|
char buf[MAX_PATH];
|
2014-08-27 19:42:17 +08:00
|
|
|
DWORD write_access = WRITE_DAC | (chown ? WRITE_OWNER : 0);
|
2010-04-20 03:52:43 +08:00
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
_tc = cygwin_shared->tty[get_minor ()];
|
|
|
|
shared_name (buf, INPUT_AVAILABLE_EVENT, get_minor ());
|
2014-08-27 19:42:17 +08:00
|
|
|
input_available_event = OpenEvent (READ_CONTROL | write_access,
|
2010-04-20 03:52:43 +08:00
|
|
|
TRUE, buf);
|
2014-08-27 19:42:17 +08:00
|
|
|
output_mutex = get_ttyp ()->open_output_mutex (write_access);
|
|
|
|
input_mutex = get_ttyp ()->open_input_mutex (write_access);
|
|
|
|
inuse = get_ttyp ()->open_inuse (write_access);
|
2010-04-20 03:52:43 +08:00
|
|
|
if (!input_available_event || !output_mutex || !input_mutex || !inuse)
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper function for fchmod and fchown, which sets the new security
|
2011-06-15 05:48:43 +08:00
|
|
|
descriptor on all objects representing the pty. */
|
2010-04-20 03:52:43 +08:00
|
|
|
int
|
2011-06-15 05:48:43 +08:00
|
|
|
fhandler_pty_slave::fch_set_sd (security_descriptor &sd, bool chown)
|
2010-04-20 03:52:43 +08:00
|
|
|
{
|
|
|
|
security_descriptor sd_old;
|
|
|
|
|
|
|
|
get_object_sd (input_available_event, sd_old);
|
2010-04-20 04:00:18 +08:00
|
|
|
if (!set_object_sd (input_available_event, sd, chown)
|
2010-04-20 03:52:43 +08:00
|
|
|
&& !set_object_sd (output_mutex, sd, chown)
|
|
|
|
&& !set_object_sd (input_mutex, sd, chown)
|
2011-06-15 05:48:43 +08:00
|
|
|
&& !set_object_sd (inuse, sd, chown))
|
2010-04-20 03:52:43 +08:00
|
|
|
return 0;
|
|
|
|
set_object_sd (input_available_event, sd_old, chown);
|
|
|
|
set_object_sd (output_mutex, sd_old, chown);
|
|
|
|
set_object_sd (input_mutex, sd_old, chown);
|
|
|
|
set_object_sd (inuse, sd_old, chown);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper function for fchmod and fchown, which closes all object handles in
|
2011-06-15 05:48:43 +08:00
|
|
|
the pty. */
|
2010-04-20 03:52:43 +08:00
|
|
|
void
|
2011-06-15 05:48:43 +08:00
|
|
|
fhandler_pty_slave::fch_close_handles ()
|
2010-04-20 03:52:43 +08:00
|
|
|
{
|
|
|
|
close_maybe (input_available_event);
|
|
|
|
close_maybe (output_mutex);
|
|
|
|
close_maybe (input_mutex);
|
|
|
|
close_maybe (inuse);
|
|
|
|
}
|
|
|
|
|
2013-05-01 09:20:37 +08:00
|
|
|
int __reg1
|
2011-06-15 05:48:43 +08:00
|
|
|
fhandler_pty_slave::fchmod (mode_t mode)
|
2010-04-20 03:52:43 +08:00
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
bool to_close = false;
|
|
|
|
security_descriptor sd;
|
2013-04-23 17:44:36 +08:00
|
|
|
uid_t uid;
|
|
|
|
gid_t gid;
|
2015-09-04 01:29:55 +08:00
|
|
|
mode_t orig_mode = S_IFCHR;
|
2010-04-20 03:52:43 +08:00
|
|
|
|
|
|
|
if (!input_available_event)
|
|
|
|
{
|
|
|
|
to_close = true;
|
2014-08-27 19:42:17 +08:00
|
|
|
if (!fch_open_handles (false))
|
2010-04-20 03:52:43 +08:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
sd.malloc (sizeof (SECURITY_DESCRIPTOR));
|
2011-04-28 17:30:36 +08:00
|
|
|
RtlCreateSecurityDescriptor (sd, SECURITY_DESCRIPTOR_REVISION);
|
2015-09-04 01:29:55 +08:00
|
|
|
if (!get_object_attribute (input_available_event, &uid, &gid, &orig_mode)
|
|
|
|
&& !create_object_sd_from_attribute (uid, gid, S_IFCHR | mode, sd))
|
2010-04-20 03:52:43 +08:00
|
|
|
ret = fch_set_sd (sd, false);
|
|
|
|
errout:
|
|
|
|
if (to_close)
|
|
|
|
fch_close_handles ();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-05-01 09:20:37 +08:00
|
|
|
int __reg2
|
2013-04-23 17:44:36 +08:00
|
|
|
fhandler_pty_slave::fchown (uid_t uid, gid_t gid)
|
2010-04-20 03:52:43 +08:00
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
bool to_close = false;
|
2015-09-04 01:29:55 +08:00
|
|
|
security_descriptor sd;
|
2013-04-23 17:44:36 +08:00
|
|
|
uid_t o_uid;
|
|
|
|
gid_t o_gid;
|
2015-09-04 01:29:55 +08:00
|
|
|
mode_t mode = S_IFCHR;
|
2010-04-20 03:52:43 +08:00
|
|
|
|
|
|
|
if (uid == ILLEGAL_UID && gid == ILLEGAL_GID)
|
|
|
|
return 0;
|
|
|
|
if (!input_available_event)
|
|
|
|
{
|
|
|
|
to_close = true;
|
2014-08-27 19:42:17 +08:00
|
|
|
if (!fch_open_handles (true))
|
2010-04-20 03:52:43 +08:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
sd.malloc (sizeof (SECURITY_DESCRIPTOR));
|
2011-04-28 17:30:36 +08:00
|
|
|
RtlCreateSecurityDescriptor (sd, SECURITY_DESCRIPTOR_REVISION);
|
2010-04-20 03:52:43 +08:00
|
|
|
if (!get_object_attribute (input_available_event, &o_uid, &o_gid, &mode))
|
|
|
|
{
|
Reapply POSIX ACL changes.
- New, unified implementation of POSIX permission and ACL handling. The
new ACLs now store the POSIX ACL MASK/CLASS_OBJ permission mask, and
they allow to inherit the S_ISGID bit. ACL inheritance now really
works as desired, in a limited, but theoretically equivalent fashion
even for non-Cygwin processes.
To accommodate Windows default ACLs, the new code ignores SYSTEM and
Administrators group permissions when computing the MASK/CLASS_OBJ
permission mask on old ACLs, and it doesn't deny access to SYSTEM and
Administrators group based on the value of MASK/CLASS_OBJ when
creating the new ACLs.
The new code now handles the S_ISGID bit on directories as on Linux:
Setting S_ISGID on a directory causes new files and subdirs created
within to inherit its group, rather than the primary group of the user
who created the file. This only works for files and directories
created by Cygwin processes.
2015-05-29 Corinna Vinschen <corinna@vinschen.de>
Reapply POSIX ACL changes.
* utils.xml (setfacl): Show new option output.
(getfacl): Show new option output.
* sec_acl.cc (get_posix_access): Check for Cygwin "standard" ACL.
Apply umask, if so. Align comments.
* security.cc (set_created_file_access): Fix permission masking by
incoming requested file mode.
* sec_acl.cc (set_posix_access): Apply mask only in terms of execute bit
for SYSTEM and Admins group.
* sec_acl.cc (set_posix_access): Don't create DENY ACEs for USER and
GROUP entries if they are the same as USER_OBJ or GROUP_OBJ.
* fhandler.h (fhandler_pty_slave::facl): Add prototype.
* fhandler_tty.cc (fhandler_pty_slave::facl): New method.
(fhandler_pty_slave::fchown): Fix uid/gid handling.
* sec_acl.cc (set_posix_access): Drop superfluous class_idx variable.
Simplify and move around code in a few places. To improve ACL
readability, add r/w permissions to Admins ACE appended to pty ACL.
Add comment to explain Windows ACE Mask filtering being in the way of
creating a real CLASS_OBJ.
(get_posix_access): Fake CLASS_OBJ for ptys. Explain why.
* security.cc (get_object_attribute): Add S_IFCHR flag to attributes
when calling get_posix_access.
* sec_acl.cc (set_posix_access): Move merging group perms into owner
perms in case of owner == group after mask has been computed. Take
mask into account when doing so to avoid unnecessary ACCESS_DENIED_ACE.
* sec_acl.cc (get_posix_access): Only set saw_group_obj flag if we saw
the ACCESS_ALLOWED_ACE.
* fhandler_disk_file.cc (fhandler_disk_file::fchmod): Deliberatly
set GROUP_OBJ and CLASS_OBJ perms to new group perms. Add comment
to explain why.
* security.cc (set_created_file_access): Ditto.
* sec_acl.cc (set_posix_access): Replace previous patch. Return
EINVAL if uid and/or guid is invalid and not backed by an actual
Windows account.
* sec_acl.cc (set_posix_access): Workaround owner/group SIDs being NULL.
* sec_acl.cc (set_posix_access): Handle files with owner == group.
Rephrase switch statement checking against unfiltered a_type value.
(get_posix_access): Handle files with owner == group.
* sec_acl.cc (get_posix_access): Don't use GROUP_OBJ access to fix up
CLASS_OBJ mask on old-style ACLs. Fix a comment.
* sec_acl.cc (set_posix_access): Always make sure Admins have
WRITE_DAC and WRITE_OWNER permissions.
* security.h (create_object_sd_from_attribute): Drop handle parameter
from prototype.
* security.cc (create_object_sd_from_attribute): Drop handle parameter.
Just create the standard POSIXy security descriptor.
(set_object_attribute): Accommodate dropped paramter in call to
create_object_sd_from_attribute.
* fhandler_tty.cc: Ditto, throughout.
* fhandler_disk_file.cc (fhandler_disk_file::fchmod): Fix typo in
mask computation.
* fhandler.cc (fhandler_base::open_with_arch): Call open with mode
not umasked.
(fhandler_base::open): Explicitely umask mode on NFS here. Call new
set_created_file_access rather than set_file_attribute.
* fhandler_disk_file.cc (fhandler_disk_file::fchmod): Reimplement
setting permissions on filesystems supporting ACLs using the new
set_posix_access call.
(fhandler_disk_file::fchown): Ditto.
(fhandler_disk_file::mkdir): Call new set_created_file_access rather
than set_file_attribute.
* fhandler_socket.cc (fhandler_socket::bind): Don't umask here. Add
WRITE_OWNER access to allow writing group in case of SGID bit set.
Call new set_created_file_access rather than set_file_attribute.
* path.cc (symlink_worker): Call new set_created_file_access rather
than set_file_attribute.
* sec_acl.cc (searchace): Un-staticize.
(set_posix_access): New, complementary functionality to
get_posix_access.
(setacl): Implement in terms of get_posix_access/set_posix_access.
(get_posix_access): Add handling for just created files requiring
their first Cygwin ACL. Fix new_style recognition. Handle SGID
bit. For old-style ACLs, ignore SYSTEM and Administrators when
computing the {DEF_}CLASS_OBJ perms.
* security.cc (get_file_sd): Revamp comment. Change and (hopefully)
speed up inheritance processing for just created files.
(alloc_sd): Remove.
(set_security_attribute): Call set_posix_access instead of alloc_sd.
(get_object_attribute): Fix return value.
(create_object_sd_from_attribute): Call set_posix_access instead of
alloc_sd.
(set_file_attribute): Remove.
(set_created_file_access): New function implemented in terms of
get_posix_access/set_posix_access.
* security.h (set_file_attribute): Remove prototype.
(set_created_file_access): Add prototype.
(searchace): Ditto.
(set_posix_access): Ditto.
* syscalls.cc (open): Call open_with_arch with mode not umasked.
* sec_acl.cc: Change preceeding comment explaining new-style ACLs.
Describe how to generate deny ACEs in more detail. Accommodate the
fact that a NULL deny ACE is used for {DEF_}CLASS_OBJ, rather than
a special Cygwin ACE. Improve further comments.
(CYG_ACE_NEW_STYLE): Define.
(get_posix_access): Change from Cygwin ACE to NULL deny ACE. Fix
CLASS_OBJ handling to generate CLASS_OBJ and DEF_CLASS_OBJ from a single
NULL deny ACE if the inheritance flags say so.
* sec_helper.cc (well_known_cygwin_sid): Remove.
* security.h (well_known_cygwin_sid): Drop declaration.
* sec_acl.cc (CYG_ACE_ISBITS_TO_WIN): Fix typo.
(get_posix_access): Rename index variable from i to idx. Define only
once at top level.
* security.cc (add_access_allowed_ace): Drop unused parameter "offset".
Accommodate throughout.
(add_access_denied_ace): Ditto.
* sec_acl.cc: Accommodate above change throughout.
* security.h (add_access_allowed_ace): Adjust prototype to above change.
(add_access_denied_ace): Ditto.
* sec_acl.cc (get_posix_access): Handle multiple ACEs for the
owner and primary group of the file. Handle the default primary
group ACE as DEF_GROUP_OBJ entry if the directory has the S_ISGID bit
set. Add comments. Minor code rearrangements.
Preliminary read side implementation of new permission handling.
* acl.h (MAX_ACL_ENTRIES): Raise to 2730. Add comment to explain.
* sec_acl.cc: Add leading comment to explain new ACL style.
Add definitions and macros to use for bits in new Cygwin ACL.
(DENY_RWX): New mask value for all temporary deny bits.
(getace): Add bool parameter to decide when leaving all bits intact,
rather than filtering them per the already set bits.
(get_posix_access): New function, taking over functionality to read
POSIX ACL from SECURITY_DESCRIPTOR.
(getacl): Just call get_posix_access.
* sec_helper.cc (well_known_cygwin_sid): Define.
* security.cc (get_attribute_from_acl): Remove.
(get_info_from_sd): Remove.
(get_reg_sd): Call get_posix_access instead of get_info_from_sd.
(get_file_attribute): Ditto.
(get_object_attribute): Ditto.
* security.h (well_known_cygwin_sid): Declare.
(get_posix_access): Add prototype.
* Throughout, use simpler ACE macros from Windows' accctrl.h.
* getfacl.c (main): Special-case SYSTEM and Admins group. Add comments.
* setfacl.c: Align more to Linux tool.
(delacl): New function to delete acl entries only.
(modacl): Drop delete functionality. Add handling of recomputing the
mask and default mask values.
(delallacl): Rename from delacl.
(setfacl): Call delacl in Delete case. Call delallacl in DeleteAll
and DeleteDef case.
(usage): Accommodate new options. Rearrange and rephrase slightly.
(longopts): Emit 'x' in --delete case. Add --no-mask and --mask
options.
(opts): Add -x and -n options.
(main): Handle -d and -x the same. Handle -n and --mask options.
Drop handling for -r option.
* getfacl.c (usage): Align more closely to Linux version. Add new
options -c, -e, -E. Change formatting to accommodate longer options.
(longopts): Rename --noname to --numeric. Keep --noname for backward
compatibility. Add --omit-header, --all-effective and --no-effective
options.
(opts): Add -c, -e and -E option.
(main): Handle new -c, -e, and -E options.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-06-24 17:52:29 +08:00
|
|
|
if (uid == ILLEGAL_UID)
|
|
|
|
uid = o_uid;
|
|
|
|
if (gid == ILLEGAL_GID)
|
|
|
|
gid = o_gid;
|
|
|
|
if (uid == o_uid && gid == o_gid)
|
2010-04-20 03:52:43 +08:00
|
|
|
ret = 0;
|
Reapply POSIX ACL changes.
- New, unified implementation of POSIX permission and ACL handling. The
new ACLs now store the POSIX ACL MASK/CLASS_OBJ permission mask, and
they allow to inherit the S_ISGID bit. ACL inheritance now really
works as desired, in a limited, but theoretically equivalent fashion
even for non-Cygwin processes.
To accommodate Windows default ACLs, the new code ignores SYSTEM and
Administrators group permissions when computing the MASK/CLASS_OBJ
permission mask on old ACLs, and it doesn't deny access to SYSTEM and
Administrators group based on the value of MASK/CLASS_OBJ when
creating the new ACLs.
The new code now handles the S_ISGID bit on directories as on Linux:
Setting S_ISGID on a directory causes new files and subdirs created
within to inherit its group, rather than the primary group of the user
who created the file. This only works for files and directories
created by Cygwin processes.
2015-05-29 Corinna Vinschen <corinna@vinschen.de>
Reapply POSIX ACL changes.
* utils.xml (setfacl): Show new option output.
(getfacl): Show new option output.
* sec_acl.cc (get_posix_access): Check for Cygwin "standard" ACL.
Apply umask, if so. Align comments.
* security.cc (set_created_file_access): Fix permission masking by
incoming requested file mode.
* sec_acl.cc (set_posix_access): Apply mask only in terms of execute bit
for SYSTEM and Admins group.
* sec_acl.cc (set_posix_access): Don't create DENY ACEs for USER and
GROUP entries if they are the same as USER_OBJ or GROUP_OBJ.
* fhandler.h (fhandler_pty_slave::facl): Add prototype.
* fhandler_tty.cc (fhandler_pty_slave::facl): New method.
(fhandler_pty_slave::fchown): Fix uid/gid handling.
* sec_acl.cc (set_posix_access): Drop superfluous class_idx variable.
Simplify and move around code in a few places. To improve ACL
readability, add r/w permissions to Admins ACE appended to pty ACL.
Add comment to explain Windows ACE Mask filtering being in the way of
creating a real CLASS_OBJ.
(get_posix_access): Fake CLASS_OBJ for ptys. Explain why.
* security.cc (get_object_attribute): Add S_IFCHR flag to attributes
when calling get_posix_access.
* sec_acl.cc (set_posix_access): Move merging group perms into owner
perms in case of owner == group after mask has been computed. Take
mask into account when doing so to avoid unnecessary ACCESS_DENIED_ACE.
* sec_acl.cc (get_posix_access): Only set saw_group_obj flag if we saw
the ACCESS_ALLOWED_ACE.
* fhandler_disk_file.cc (fhandler_disk_file::fchmod): Deliberatly
set GROUP_OBJ and CLASS_OBJ perms to new group perms. Add comment
to explain why.
* security.cc (set_created_file_access): Ditto.
* sec_acl.cc (set_posix_access): Replace previous patch. Return
EINVAL if uid and/or guid is invalid and not backed by an actual
Windows account.
* sec_acl.cc (set_posix_access): Workaround owner/group SIDs being NULL.
* sec_acl.cc (set_posix_access): Handle files with owner == group.
Rephrase switch statement checking against unfiltered a_type value.
(get_posix_access): Handle files with owner == group.
* sec_acl.cc (get_posix_access): Don't use GROUP_OBJ access to fix up
CLASS_OBJ mask on old-style ACLs. Fix a comment.
* sec_acl.cc (set_posix_access): Always make sure Admins have
WRITE_DAC and WRITE_OWNER permissions.
* security.h (create_object_sd_from_attribute): Drop handle parameter
from prototype.
* security.cc (create_object_sd_from_attribute): Drop handle parameter.
Just create the standard POSIXy security descriptor.
(set_object_attribute): Accommodate dropped paramter in call to
create_object_sd_from_attribute.
* fhandler_tty.cc: Ditto, throughout.
* fhandler_disk_file.cc (fhandler_disk_file::fchmod): Fix typo in
mask computation.
* fhandler.cc (fhandler_base::open_with_arch): Call open with mode
not umasked.
(fhandler_base::open): Explicitely umask mode on NFS here. Call new
set_created_file_access rather than set_file_attribute.
* fhandler_disk_file.cc (fhandler_disk_file::fchmod): Reimplement
setting permissions on filesystems supporting ACLs using the new
set_posix_access call.
(fhandler_disk_file::fchown): Ditto.
(fhandler_disk_file::mkdir): Call new set_created_file_access rather
than set_file_attribute.
* fhandler_socket.cc (fhandler_socket::bind): Don't umask here. Add
WRITE_OWNER access to allow writing group in case of SGID bit set.
Call new set_created_file_access rather than set_file_attribute.
* path.cc (symlink_worker): Call new set_created_file_access rather
than set_file_attribute.
* sec_acl.cc (searchace): Un-staticize.
(set_posix_access): New, complementary functionality to
get_posix_access.
(setacl): Implement in terms of get_posix_access/set_posix_access.
(get_posix_access): Add handling for just created files requiring
their first Cygwin ACL. Fix new_style recognition. Handle SGID
bit. For old-style ACLs, ignore SYSTEM and Administrators when
computing the {DEF_}CLASS_OBJ perms.
* security.cc (get_file_sd): Revamp comment. Change and (hopefully)
speed up inheritance processing for just created files.
(alloc_sd): Remove.
(set_security_attribute): Call set_posix_access instead of alloc_sd.
(get_object_attribute): Fix return value.
(create_object_sd_from_attribute): Call set_posix_access instead of
alloc_sd.
(set_file_attribute): Remove.
(set_created_file_access): New function implemented in terms of
get_posix_access/set_posix_access.
* security.h (set_file_attribute): Remove prototype.
(set_created_file_access): Add prototype.
(searchace): Ditto.
(set_posix_access): Ditto.
* syscalls.cc (open): Call open_with_arch with mode not umasked.
* sec_acl.cc: Change preceeding comment explaining new-style ACLs.
Describe how to generate deny ACEs in more detail. Accommodate the
fact that a NULL deny ACE is used for {DEF_}CLASS_OBJ, rather than
a special Cygwin ACE. Improve further comments.
(CYG_ACE_NEW_STYLE): Define.
(get_posix_access): Change from Cygwin ACE to NULL deny ACE. Fix
CLASS_OBJ handling to generate CLASS_OBJ and DEF_CLASS_OBJ from a single
NULL deny ACE if the inheritance flags say so.
* sec_helper.cc (well_known_cygwin_sid): Remove.
* security.h (well_known_cygwin_sid): Drop declaration.
* sec_acl.cc (CYG_ACE_ISBITS_TO_WIN): Fix typo.
(get_posix_access): Rename index variable from i to idx. Define only
once at top level.
* security.cc (add_access_allowed_ace): Drop unused parameter "offset".
Accommodate throughout.
(add_access_denied_ace): Ditto.
* sec_acl.cc: Accommodate above change throughout.
* security.h (add_access_allowed_ace): Adjust prototype to above change.
(add_access_denied_ace): Ditto.
* sec_acl.cc (get_posix_access): Handle multiple ACEs for the
owner and primary group of the file. Handle the default primary
group ACE as DEF_GROUP_OBJ entry if the directory has the S_ISGID bit
set. Add comments. Minor code rearrangements.
Preliminary read side implementation of new permission handling.
* acl.h (MAX_ACL_ENTRIES): Raise to 2730. Add comment to explain.
* sec_acl.cc: Add leading comment to explain new ACL style.
Add definitions and macros to use for bits in new Cygwin ACL.
(DENY_RWX): New mask value for all temporary deny bits.
(getace): Add bool parameter to decide when leaving all bits intact,
rather than filtering them per the already set bits.
(get_posix_access): New function, taking over functionality to read
POSIX ACL from SECURITY_DESCRIPTOR.
(getacl): Just call get_posix_access.
* sec_helper.cc (well_known_cygwin_sid): Define.
* security.cc (get_attribute_from_acl): Remove.
(get_info_from_sd): Remove.
(get_reg_sd): Call get_posix_access instead of get_info_from_sd.
(get_file_attribute): Ditto.
(get_object_attribute): Ditto.
* security.h (well_known_cygwin_sid): Declare.
(get_posix_access): Add prototype.
* Throughout, use simpler ACE macros from Windows' accctrl.h.
* getfacl.c (main): Special-case SYSTEM and Admins group. Add comments.
* setfacl.c: Align more to Linux tool.
(delacl): New function to delete acl entries only.
(modacl): Drop delete functionality. Add handling of recomputing the
mask and default mask values.
(delallacl): Rename from delacl.
(setfacl): Call delacl in Delete case. Call delallacl in DeleteAll
and DeleteDef case.
(usage): Accommodate new options. Rearrange and rephrase slightly.
(longopts): Emit 'x' in --delete case. Add --no-mask and --mask
options.
(opts): Add -x and -n options.
(main): Handle -d and -x the same. Handle -n and --mask options.
Drop handling for -r option.
* getfacl.c (usage): Align more closely to Linux version. Add new
options -c, -e, -E. Change formatting to accommodate longer options.
(longopts): Rename --noname to --numeric. Keep --noname for backward
compatibility. Add --omit-header, --all-effective and --no-effective
options.
(opts): Add -c, -e and -E option.
(main): Handle new -c, -e, and -E options.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-06-24 17:52:29 +08:00
|
|
|
else if (!create_object_sd_from_attribute (uid, gid, mode, sd))
|
2010-04-20 03:52:43 +08:00
|
|
|
ret = fch_set_sd (sd, true);
|
|
|
|
}
|
|
|
|
errout:
|
|
|
|
if (to_close)
|
|
|
|
fch_close_handles ();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/*******************************************************
|
|
|
|
fhandler_pty_master
|
|
|
|
*/
|
2011-10-16 06:37:30 +08:00
|
|
|
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),
|
2019-08-28 02:04:02 +08:00
|
|
|
from_slave (NULL), to_slave (NULL), echo_r (NULL), echo_w (NULL),
|
|
|
|
dwProcessId (0), to_master_cyg (NULL), from_master_cyg (NULL),
|
|
|
|
master_fwd_thread (NULL)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2011-10-16 06:37:30 +08:00
|
|
|
if (unit >= 0)
|
2011-10-20 22:02:54 +08:00
|
|
|
dev ().parse (DEV_PTYM_MAJOR, unit);
|
2011-10-16 06:37:30 +08:00
|
|
|
else if (!setup ())
|
2011-10-20 22:02:54 +08:00
|
|
|
{
|
|
|
|
dev ().parse (FH_ERROR);
|
2011-12-18 07:39:47 +08:00
|
|
|
return;
|
2011-10-20 22:02:54 +08:00
|
|
|
}
|
|
|
|
set_name ("/dev/ptmx");
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
* 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
|
|
|
fhandler_pty_master::open (int flags, mode_t)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2001-04-24 10:07:58 +08:00
|
|
|
set_open_status ();
|
2011-05-06 06:30:53 +08:00
|
|
|
dwProcessId = GetCurrentProcessId ();
|
2011-10-16 06:37:30 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2011-10-16 06:37:30 +08:00
|
|
|
void
|
|
|
|
fhandler_pty_master::open_setup (int flags)
|
|
|
|
{
|
|
|
|
set_flags ((flags & ~O_TEXT) | O_BINARY);
|
|
|
|
char buf[sizeof ("opened pty master for ptyNNNNNNNNNNN")];
|
2013-04-23 17:44:36 +08:00
|
|
|
__small_sprintf (buf, "opened pty master for pty%d", get_minor ());
|
2006-06-04 04:32:07 +08:00
|
|
|
report_tty_counts (this, buf, "");
|
2013-10-24 23:26:21 +08:00
|
|
|
fhandler_base::open_setup (flags);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
off_t
|
|
|
|
fhandler_pty_common::lseek (off_t, int)
|
2005-08-19 04:17:10 +08:00
|
|
|
{
|
|
|
|
set_errno (ESPIPE);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
int
|
2011-06-15 05:48:43 +08:00
|
|
|
fhandler_pty_common::close ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2019-08-28 02:04:02 +08:00
|
|
|
termios_printf ("pty%d <%p,%p> closing",
|
|
|
|
get_minor (), get_handle (), get_output_handle ());
|
2001-03-04 23:34:25 +08:00
|
|
|
if (!ForceCloseHandle (input_mutex))
|
|
|
|
termios_printf ("CloseHandle (input_mutex<%p>), %E", input_mutex);
|
2005-08-18 00:10:48 +08:00
|
|
|
if (!ForceCloseHandle1 (get_handle (), from_pty))
|
|
|
|
termios_printf ("CloseHandle (get_handle ()<%p>), %E", get_handle ());
|
|
|
|
if (!ForceCloseHandle1 (get_output_handle (), to_pty))
|
2019-08-28 02:04:02 +08:00
|
|
|
termios_printf ("CloseHandle (get_output_handle ()<%p>), %E",
|
|
|
|
get_output_handle ());
|
2001-03-20 02:27:37 +08:00
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-06 06:30:53 +08:00
|
|
|
void
|
|
|
|
fhandler_pty_master::cleanup ()
|
|
|
|
{
|
|
|
|
report_tty_counts (this, "closing master", "");
|
2011-10-16 06:37:30 +08:00
|
|
|
if (archetype)
|
2019-08-28 02:04:02 +08:00
|
|
|
from_master = from_master_cyg =
|
|
|
|
to_master = to_master_cyg = from_slave = to_slave = NULL;
|
2013-10-23 04:41:09 +08:00
|
|
|
fhandler_base::cleanup ();
|
2011-05-06 06:30:53 +08:00
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
int
|
|
|
|
fhandler_pty_master::close ()
|
|
|
|
{
|
2015-05-28 22:06:49 +08:00
|
|
|
OBJECT_BASIC_INFORMATION obi;
|
|
|
|
NTSTATUS status;
|
2019-08-28 02:04:02 +08:00
|
|
|
pid_t master_pid_tmp = get_ttyp ()->master_pid;
|
2015-05-28 22:06:49 +08:00
|
|
|
|
2019-08-28 02:04:02 +08:00
|
|
|
termios_printf ("closing from_master(%p)/from_master_cyg(%p)/to_master(%p)/to_master_cyg(%p) since we own them(%u)",
|
|
|
|
from_master, from_master_cyg,
|
|
|
|
to_master, to_master_cyg, dwProcessId);
|
2009-06-15 07:42:09 +08:00
|
|
|
if (cygwin_finished_initializing)
|
|
|
|
{
|
2011-05-06 06:30:53 +08:00
|
|
|
if (master_ctl && get_ttyp ()->master_pid == myself->pid)
|
2010-04-20 03:52:43 +08:00
|
|
|
{
|
|
|
|
char buf[MAX_PATH];
|
|
|
|
pipe_request req = { (DWORD) -1 };
|
|
|
|
pipe_reply repl;
|
|
|
|
DWORD len;
|
|
|
|
|
2011-06-15 05:48:43 +08:00
|
|
|
__small_sprintf (buf, "\\\\.\\pipe\\cygwin-%S-pty%d-master-ctl",
|
2013-04-23 17:44:36 +08:00
|
|
|
&cygheap->installation_key, get_minor ());
|
2015-03-05 21:58:03 +08:00
|
|
|
acquire_output_mutex (INFINITE);
|
|
|
|
if (master_ctl)
|
|
|
|
{
|
|
|
|
CallNamedPipe (buf, &req, sizeof req, &repl, sizeof repl, &len,
|
|
|
|
500);
|
|
|
|
CloseHandle (master_ctl);
|
|
|
|
master_thread->detach ();
|
|
|
|
get_ttyp ()->set_master_ctl_closed ();
|
|
|
|
master_ctl = NULL;
|
|
|
|
}
|
|
|
|
release_output_mutex ();
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
master_fwd_thread->terminate_thread ();
|
2020-01-21 10:22:02 +08:00
|
|
|
CloseHandle (get_ttyp ()->fwd_done);
|
|
|
|
get_ttyp ()->fwd_done = NULL;
|
2010-04-20 03:52:43 +08:00
|
|
|
}
|
2009-06-15 07:42:09 +08:00
|
|
|
}
|
2011-10-16 06:37:30 +08:00
|
|
|
|
2015-03-12 22:48:10 +08:00
|
|
|
/* Check if the last master handle has been closed. If so, set
|
|
|
|
input_available_event to wake up potentially waiting slaves. */
|
2015-05-28 22:06:49 +08:00
|
|
|
acquire_output_mutex (INFINITE);
|
|
|
|
status = NtQueryObject (get_output_handle (), ObjectBasicInformation,
|
|
|
|
&obi, sizeof obi, NULL);
|
|
|
|
fhandler_pty_common::close ();
|
|
|
|
release_output_mutex ();
|
|
|
|
if (!ForceCloseHandle (output_mutex))
|
|
|
|
termios_printf ("CloseHandle (output_mutex<%p>), %E", output_mutex);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
debug_printf ("NtQueryObject: %y", status);
|
2020-01-14 12:10:54 +08:00
|
|
|
else if (obi.HandleCount == 1)
|
2015-05-28 22:06:49 +08:00
|
|
|
{
|
2019-09-15 12:36:23 +08:00
|
|
|
termios_printf ("Closing last master of pty%d", get_minor ());
|
2019-08-28 02:04:02 +08:00
|
|
|
/* Close Pseudo Console */
|
2019-11-16 07:27:24 +08:00
|
|
|
if (get_pseudo_console ())
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
|
|
|
/* Terminate helper process */
|
2019-11-16 07:27:24 +08:00
|
|
|
SetEvent (get_ttyp ()->h_helper_goodbye);
|
|
|
|
WaitForSingleObject (get_ttyp ()->h_helper_process, INFINITE);
|
2020-01-14 10:50:04 +08:00
|
|
|
CloseHandle (get_ttyp ()->h_helper_goodbye);
|
|
|
|
CloseHandle (get_ttyp ()->h_helper_process);
|
2019-08-28 02:04:02 +08:00
|
|
|
/* FIXME: Pseudo console can be accessed via its handle
|
|
|
|
only in the process which created it. What else can we do? */
|
|
|
|
if (master_pid_tmp == myself->pid)
|
2020-01-14 10:50:04 +08:00
|
|
|
{
|
|
|
|
/* ClosePseudoConsole() seems to have a bug that one
|
|
|
|
internal handle remains opened. This causes handle leak.
|
|
|
|
This is a workaround for this problem. */
|
|
|
|
HPCON_INTERNAL *hp = (HPCON_INTERNAL *) get_pseudo_console ();
|
|
|
|
HANDLE tmp = hp->hConHostProcess;
|
|
|
|
/* Release pseudo console */
|
|
|
|
ClosePseudoConsole (get_pseudo_console ());
|
|
|
|
CloseHandle (tmp);
|
|
|
|
}
|
2019-09-14 05:48:14 +08:00
|
|
|
get_ttyp ()->switch_to_pcon_in = false;
|
|
|
|
get_ttyp ()->switch_to_pcon_out = false;
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
2015-06-08 20:07:42 +08:00
|
|
|
if (get_ttyp ()->getsid () > 0)
|
|
|
|
kill (get_ttyp ()->getsid (), SIGHUP);
|
2015-05-28 22:06:49 +08:00
|
|
|
SetEvent (input_available_event);
|
|
|
|
}
|
2015-03-12 22:48:10 +08:00
|
|
|
|
2011-10-16 06:37:30 +08:00
|
|
|
if (!ForceCloseHandle (from_master))
|
|
|
|
termios_printf ("error closing from_master %p, %E", from_master);
|
2019-08-28 02:04:02 +08:00
|
|
|
if (from_master_cyg != from_master) /* Avoid double close. */
|
|
|
|
if (!ForceCloseHandle (from_master_cyg))
|
|
|
|
termios_printf ("error closing from_master_cyg %p, %E", from_master_cyg);
|
2011-10-16 06:37:30 +08:00
|
|
|
if (!ForceCloseHandle (to_master))
|
2015-03-12 22:48:10 +08:00
|
|
|
termios_printf ("error closing to_master %p, %E", to_master);
|
2011-10-16 06:37:30 +08:00
|
|
|
from_master = to_master = NULL;
|
2019-08-28 02:04:02 +08:00
|
|
|
if (!ForceCloseHandle (from_slave))
|
|
|
|
termios_printf ("error closing from_slave %p, %E", from_slave);
|
|
|
|
from_slave = NULL;
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
if (!ForceCloseHandle (to_master_cyg))
|
|
|
|
termios_printf ("error closing to_master_cyg %p, %E", to_master_cyg);
|
2019-08-28 02:04:02 +08:00
|
|
|
to_master_cyg = from_master_cyg = NULL;
|
2015-03-05 20:57:34 +08:00
|
|
|
ForceCloseHandle (echo_r);
|
|
|
|
ForceCloseHandle (echo_w);
|
|
|
|
echo_r = echo_w = NULL;
|
2019-08-28 02:04:02 +08:00
|
|
|
if (to_slave)
|
|
|
|
ForceCloseHandle (to_slave);
|
|
|
|
to_slave = NULL;
|
2011-10-16 06:37:30 +08:00
|
|
|
|
2011-11-14 09:29:49 +08:00
|
|
|
if (have_execed || get_ttyp ()->master_pid != myself->pid)
|
2019-08-28 02:04:02 +08:00
|
|
|
termios_printf ("not clearing: %d, master_pid %d",
|
|
|
|
have_execed, get_ttyp ()->master_pid);
|
2011-12-10 00:02:56 +08:00
|
|
|
if (!ForceCloseHandle (input_available_event))
|
2019-08-28 02:04:02 +08:00
|
|
|
termios_printf ("CloseHandle (input_available_event<%p>), %E",
|
|
|
|
input_available_event);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-25 04:54:33 +08:00
|
|
|
ssize_t __stdcall
|
2000-02-18 03:38:33 +08:00
|
|
|
fhandler_pty_master::write (const void *ptr, size_t len)
|
|
|
|
{
|
2014-11-14 02:37:15 +08:00
|
|
|
ssize_t ret;
|
2002-12-06 00:24:52 +08:00
|
|
|
char *p = (char *) ptr;
|
2011-06-04 08:12:29 +08:00
|
|
|
termios ti = tc ()->ti;
|
2002-12-27 11:50:29 +08:00
|
|
|
|
2011-05-06 03:43:04 +08:00
|
|
|
bg_check_types bg = bg_check (SIGTTOU);
|
|
|
|
if (bg <= bg_eof)
|
|
|
|
return (ssize_t) bg;
|
|
|
|
|
|
|
|
push_process_state process_state (PID_TTYOU);
|
2019-08-28 02:04:02 +08:00
|
|
|
|
|
|
|
/* Write terminal input to to_slave pipe instead of output_handle
|
|
|
|
if current application is native console application. */
|
2019-09-05 12:22:54 +08:00
|
|
|
if (to_be_read_from_pcon ())
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
|
|
|
size_t nlen;
|
2019-09-18 22:29:19 +08:00
|
|
|
char *buf = convert_mb_str
|
|
|
|
(CP_UTF8, &nlen, get_ttyp ()->term_code_page, (const char *) ptr, len);
|
2019-08-28 02:04:02 +08:00
|
|
|
|
2020-02-09 22:46:02 +08:00
|
|
|
WaitForSingleObject (input_mutex, INFINITE);
|
|
|
|
|
2020-02-09 22:46:59 +08:00
|
|
|
if (memchr (buf, '\003', nlen)) /* ^C intr key in pcon */
|
|
|
|
get_ttyp ()->req_flush_pcon_input = true;
|
|
|
|
|
2019-08-28 02:04:02 +08:00
|
|
|
DWORD wLen;
|
|
|
|
WriteFile (to_slave, buf, nlen, &wLen, NULL);
|
2020-02-09 22:46:59 +08:00
|
|
|
get_ttyp ()->pcon_in_empty = false;
|
2019-09-14 05:48:14 +08:00
|
|
|
|
2020-02-09 22:46:59 +08:00
|
|
|
ReleaseMutex (input_mutex);
|
|
|
|
|
|
|
|
/* Use line_edit () in order to set input_available_event. */
|
|
|
|
bool is_echo = tc ()->ti.c_lflag & ECHO;
|
|
|
|
if (!get_ttyp ()->mask_switch_to_pcon_in)
|
2019-09-14 05:48:14 +08:00
|
|
|
{
|
2020-02-09 22:46:59 +08:00
|
|
|
tc ()->ti.c_lflag &= ~ECHO;
|
|
|
|
ti.c_lflag &= ~ECHO;
|
2019-09-14 05:48:14 +08:00
|
|
|
}
|
2020-02-09 22:46:59 +08:00
|
|
|
ti.c_lflag &= ~ISIG;
|
|
|
|
line_edit (buf, nlen, ti, &ret);
|
|
|
|
if (is_echo)
|
|
|
|
tc ()->ti.c_lflag |= ECHO;
|
|
|
|
get_ttyp ()->read_retval = 1;
|
|
|
|
|
|
|
|
const char sigs[] = {
|
|
|
|
(char) ti.c_cc[VINTR],
|
|
|
|
(char) ti.c_cc[VQUIT],
|
|
|
|
(char) ti.c_cc[VSUSP],
|
|
|
|
};
|
|
|
|
if (tc ()->ti.c_lflag & ISIG)
|
|
|
|
for (size_t i=0; i<sizeof (sigs); i++)
|
|
|
|
if (memchr (buf, sigs[i], nlen))
|
|
|
|
{
|
|
|
|
eat_readahead (-1);
|
|
|
|
SetEvent (input_available_event);
|
|
|
|
}
|
2020-02-09 22:46:02 +08:00
|
|
|
|
2019-09-18 22:29:19 +08:00
|
|
|
mb_str_free (buf);
|
2019-08-28 02:04:02 +08:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2020-02-04 20:25:52 +08:00
|
|
|
line_edit_status status = line_edit (p, len, ti, &ret);
|
2014-11-14 02:37:15 +08:00
|
|
|
if (status > line_edit_signalled && status != line_edit_pipe_full)
|
|
|
|
ret = -1;
|
|
|
|
return ret;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2013-05-01 09:20:37 +08:00
|
|
|
void __reg3
|
2002-12-14 12:01:32 +08:00
|
|
|
fhandler_pty_master::read (void *ptr, size_t& len)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2011-05-06 03:43:04 +08:00
|
|
|
bg_check_types bg = bg_check (SIGTTIN);
|
|
|
|
if (bg <= bg_eof)
|
|
|
|
{
|
|
|
|
len = (size_t) bg;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
push_process_state process_state (PID_TTYIN);
|
2003-09-08 02:27:54 +08:00
|
|
|
len = (size_t) process_slave_output ((char *) ptr, len, pktmode);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_pty_master::tcgetattr (struct termios *t)
|
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
*t = cygwin_shared->tty[get_minor ()]->ti;
|
2000-02-18 03:38:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-02-21 13:20:38 +08:00
|
|
|
fhandler_pty_master::tcsetattr (int, const struct termios *t)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
cygwin_shared->tty[get_minor ()]->ti = *t;
|
2000-02-18 03:38:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2003-09-19 02:54:48 +08:00
|
|
|
fhandler_pty_master::tcflush (int queue)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2003-09-19 02:54:48 +08:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
termios_printf ("tcflush(%d) handle %p", queue, get_handle ());
|
|
|
|
|
|
|
|
if (queue == TCIFLUSH || queue == TCIOFLUSH)
|
|
|
|
ret = process_slave_output (NULL, OUT_BUFFER_SIZE, 0);
|
|
|
|
else if (queue == TCIFLUSH || queue == TCIOFLUSH)
|
|
|
|
{
|
|
|
|
/* do nothing for now. */
|
|
|
|
}
|
|
|
|
|
|
|
|
termios_printf ("%d=tcflush(%d)", ret, queue);
|
|
|
|
return ret;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_pty_master::ioctl (unsigned int cmd, void *arg)
|
|
|
|
{
|
2011-10-16 06:37:30 +08:00
|
|
|
int res = fhandler_termios::ioctl (cmd, arg);
|
2011-10-12 07:20:38 +08:00
|
|
|
if (res <= 0)
|
|
|
|
return res;
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
switch (cmd)
|
|
|
|
{
|
2010-10-24 02:07:08 +08:00
|
|
|
case TIOCPKT:
|
|
|
|
pktmode = *(int *) arg;
|
|
|
|
break;
|
|
|
|
case TIOCGWINSZ:
|
|
|
|
*(struct winsize *) arg = get_ttyp ()->winsize;
|
|
|
|
break;
|
|
|
|
case TIOCSWINSZ:
|
2019-08-28 02:04:02 +08:00
|
|
|
/* FIXME: Pseudo console can be accessed via its handle
|
|
|
|
only in the process which created it. What else can we do? */
|
2019-11-16 07:27:24 +08:00
|
|
|
if (get_pseudo_console () && get_ttyp ()->master_pid == myself->pid)
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
|
|
|
COORD size;
|
|
|
|
size.X = ((struct winsize *) arg)->ws_col;
|
|
|
|
size.Y = ((struct winsize *) arg)->ws_row;
|
2019-11-16 07:27:24 +08:00
|
|
|
ResizePseudoConsole (get_pseudo_console (), size);
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
2010-10-24 02:07:08 +08:00
|
|
|
if (get_ttyp ()->winsize.ws_row != ((struct winsize *) arg)->ws_row
|
|
|
|
|| get_ttyp ()->winsize.ws_col != ((struct winsize *) arg)->ws_col)
|
|
|
|
{
|
|
|
|
get_ttyp ()->winsize = *(struct winsize *) arg;
|
2013-01-31 13:26:47 +08:00
|
|
|
get_ttyp ()->kill_pgrp (SIGWINCH);
|
2010-10-24 02:07:08 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TIOCGPGRP:
|
|
|
|
*((pid_t *) arg) = this->tcgetpgrp ();
|
|
|
|
break;
|
|
|
|
case TIOCSPGRP:
|
2013-04-23 17:44:36 +08:00
|
|
|
return this->tcsetpgrp ((pid_t) (intptr_t) arg);
|
2011-07-23 02:50:42 +08:00
|
|
|
case FIONREAD:
|
|
|
|
{
|
2012-04-05 10:54:51 +08:00
|
|
|
DWORD n;
|
2019-08-28 02:04:02 +08:00
|
|
|
if (!bytes_available (n))
|
2011-07-23 02:50:42 +08:00
|
|
|
{
|
2011-07-25 23:19:35 +08:00
|
|
|
set_errno (EINVAL);
|
2011-07-23 02:50:42 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2012-05-04 11:00:43 +08:00
|
|
|
*(int *) arg = (int) n;
|
2011-07-23 02:50:42 +08:00
|
|
|
}
|
2010-10-24 02:07:08 +08:00
|
|
|
break;
|
|
|
|
default:
|
2011-07-23 02:50:42 +08:00
|
|
|
return fhandler_base::ioctl (cmd, arg);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-08 04:05:49 +08:00
|
|
|
int
|
|
|
|
fhandler_pty_master::ptsname_r (char *buf, size_t buflen)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2011-11-08 04:05:49 +08:00
|
|
|
char tmpbuf[TTY_NAME_MAX];
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
__ptsname (tmpbuf, get_minor ());
|
2011-11-08 04:05:49 +08:00
|
|
|
if (buflen <= strlen (tmpbuf))
|
|
|
|
{
|
|
|
|
set_errno (ERANGE);
|
|
|
|
return ERANGE;
|
|
|
|
}
|
|
|
|
strcpy (buf, tmpbuf);
|
|
|
|
return 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-06-15 05:48:43 +08:00
|
|
|
fhandler_pty_common::set_close_on_exec (bool val)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2006-06-02 23:41:34 +08:00
|
|
|
// Cygwin processes will handle this specially on exec.
|
|
|
|
close_on_exec (val);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2019-08-28 02:04:02 +08:00
|
|
|
/* This table is borrowed from mintty: charset.c */
|
|
|
|
static const struct {
|
|
|
|
UINT cp;
|
|
|
|
const char *name;
|
|
|
|
}
|
|
|
|
cs_names[] = {
|
|
|
|
{ CP_UTF8, "UTF-8"},
|
|
|
|
{ CP_UTF8, "UTF8"},
|
|
|
|
{ 20127, "ASCII"},
|
|
|
|
{ 20127, "US-ASCII"},
|
|
|
|
{ 20127, "ANSI_X3.4-1968"},
|
|
|
|
{ 20866, "KOI8-R"},
|
|
|
|
{ 20866, "KOI8R"},
|
|
|
|
{ 20866, "KOI8"},
|
|
|
|
{ 21866, "KOI8-U"},
|
|
|
|
{ 21866, "KOI8U"},
|
|
|
|
{ 20932, "EUCJP"},
|
|
|
|
{ 20932, "EUC-JP"},
|
|
|
|
{ 874, "TIS620"},
|
|
|
|
{ 874, "TIS-620"},
|
|
|
|
{ 932, "SJIS"},
|
|
|
|
{ 936, "GBK"},
|
|
|
|
{ 936, "GB2312"},
|
|
|
|
{ 936, "EUCCN"},
|
|
|
|
{ 936, "EUC-CN"},
|
|
|
|
{ 949, "EUCKR"},
|
|
|
|
{ 949, "EUC-KR"},
|
|
|
|
{ 950, "BIG5"},
|
|
|
|
{ 0, "NULL"}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
get_locale_from_env (char *locale)
|
|
|
|
{
|
|
|
|
const char *env = NULL;
|
|
|
|
char lang[ENCODING_LEN + 1] = {0, }, country[ENCODING_LEN + 1] = {0, };
|
|
|
|
env = getenv ("LC_ALL");
|
|
|
|
if (env == NULL || !*env)
|
2019-09-15 12:36:23 +08:00
|
|
|
env = getenv ("LC_CTYPE");
|
2019-08-28 02:04:02 +08:00
|
|
|
if (env == NULL || !*env)
|
2019-09-15 12:36:23 +08:00
|
|
|
env = getenv ("LANG");
|
2019-08-28 02:04:02 +08:00
|
|
|
if (env == NULL || !*env)
|
|
|
|
{
|
|
|
|
if (GetLocaleInfo (LOCALE_CUSTOM_UI_DEFAULT,
|
|
|
|
LOCALE_SISO639LANGNAME,
|
|
|
|
lang, sizeof (lang)))
|
|
|
|
GetLocaleInfo (LOCALE_CUSTOM_UI_DEFAULT,
|
|
|
|
LOCALE_SISO3166CTRYNAME,
|
|
|
|
country, sizeof (country));
|
|
|
|
else if (GetLocaleInfo (LOCALE_CUSTOM_DEFAULT,
|
|
|
|
LOCALE_SISO639LANGNAME,
|
|
|
|
lang, sizeof (lang)))
|
|
|
|
GetLocaleInfo (LOCALE_CUSTOM_DEFAULT,
|
|
|
|
LOCALE_SISO3166CTRYNAME,
|
|
|
|
country, sizeof (country));
|
|
|
|
else if (GetLocaleInfo (LOCALE_USER_DEFAULT,
|
|
|
|
LOCALE_SISO639LANGNAME,
|
|
|
|
lang, sizeof (lang)))
|
|
|
|
GetLocaleInfo (LOCALE_USER_DEFAULT,
|
|
|
|
LOCALE_SISO3166CTRYNAME,
|
|
|
|
country, sizeof (country));
|
|
|
|
else if (GetLocaleInfo (LOCALE_SYSTEM_DEFAULT,
|
|
|
|
LOCALE_SISO639LANGNAME,
|
|
|
|
lang, sizeof (lang)))
|
|
|
|
GetLocaleInfo (LOCALE_SYSTEM_DEFAULT,
|
|
|
|
LOCALE_SISO3166CTRYNAME,
|
|
|
|
country, sizeof (country));
|
|
|
|
if (strlen (lang) && strlen (country))
|
2019-09-15 12:36:23 +08:00
|
|
|
__small_sprintf (lang + strlen (lang), "_%s.UTF-8", country);
|
2019-08-28 02:04:02 +08:00
|
|
|
else
|
|
|
|
strcpy (lang , "C.UTF-8");
|
|
|
|
env = lang;
|
|
|
|
}
|
|
|
|
strcpy (locale, env);
|
|
|
|
}
|
|
|
|
|
|
|
|
static LCID
|
|
|
|
get_langinfo (char *locale_out, char *charset_out)
|
|
|
|
{
|
|
|
|
/* Get locale from environment */
|
|
|
|
char new_locale[ENCODING_LEN + 1];
|
|
|
|
get_locale_from_env (new_locale);
|
|
|
|
|
2019-09-15 12:05:52 +08:00
|
|
|
__locale_t loc;
|
2019-09-15 12:36:23 +08:00
|
|
|
memset (&loc, 0, sizeof (loc));
|
2019-09-15 12:05:52 +08:00
|
|
|
const char *locale = __loadlocale (&loc, LC_CTYPE, new_locale);
|
2019-08-28 02:04:02 +08:00
|
|
|
if (!locale)
|
2019-09-15 12:05:52 +08:00
|
|
|
locale = "C";
|
|
|
|
|
|
|
|
char tmp_locale[ENCODING_LEN + 1];
|
|
|
|
char *ret = __set_locale_from_locale_alias (locale, tmp_locale);
|
|
|
|
if (ret)
|
|
|
|
locale = tmp_locale;
|
|
|
|
|
|
|
|
const char *charset;
|
|
|
|
struct lc_ctype_T *lc_ctype = (struct lc_ctype_T *) loc.lc_cat[LC_CTYPE].ptr;
|
|
|
|
if (!lc_ctype)
|
|
|
|
charset = "ASCII";
|
|
|
|
else
|
|
|
|
charset = lc_ctype->codeset;
|
2019-08-28 02:04:02 +08:00
|
|
|
|
|
|
|
/* The following code is borrowed from nl_langinfo()
|
|
|
|
in newlib/libc/locale/nl_langinfo.c */
|
|
|
|
/* Convert charset to Linux compatible codeset string. */
|
2019-09-15 12:05:52 +08:00
|
|
|
if (charset[0] == 'A'/*SCII*/)
|
|
|
|
charset = "ANSI_X3.4-1968";
|
|
|
|
else if (charset[0] == 'E')
|
|
|
|
{
|
|
|
|
if (strcmp (charset, "EUCJP") == 0)
|
|
|
|
charset = "EUC-JP";
|
|
|
|
else if (strcmp (charset, "EUCKR") == 0)
|
|
|
|
charset = "EUC-KR";
|
|
|
|
else if (strcmp (charset, "EUCCN") == 0)
|
|
|
|
charset = "GB2312";
|
|
|
|
}
|
|
|
|
else if (charset[0] == 'C'/*Pxxxx*/)
|
|
|
|
{
|
|
|
|
if (strcmp (charset + 2, "874") == 0)
|
|
|
|
charset = "TIS-620";
|
|
|
|
else if (strcmp (charset + 2, "20866") == 0)
|
|
|
|
charset = "KOI8-R";
|
|
|
|
else if (strcmp (charset + 2, "21866") == 0)
|
|
|
|
charset = "KOI8-U";
|
|
|
|
else if (strcmp (charset + 2, "101") == 0)
|
|
|
|
charset = "GEORGIAN-PS";
|
|
|
|
else if (strcmp (charset + 2, "102") == 0)
|
|
|
|
charset = "PT154";
|
|
|
|
}
|
|
|
|
else if (charset[0] == 'S'/*JIS*/)
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
|
|
|
/* Cygwin uses MSFT's implementation of SJIS, which differs
|
|
|
|
in some codepoints from the real thing, especially
|
|
|
|
0x5c: yen sign instead of backslash,
|
|
|
|
0x7e: overline instead of tilde.
|
|
|
|
We can't use the real SJIS since otherwise Win32
|
|
|
|
pathnames would become invalid. OTOH, if we return
|
|
|
|
"SJIS" here, then libiconv will do mb<->wc conversion
|
|
|
|
differently to our internal functions. Therefore we
|
|
|
|
return what we really implement, CP932. This is handled
|
|
|
|
fine by libiconv. */
|
2019-09-15 12:05:52 +08:00
|
|
|
charset = "CP932";
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t lc[ENCODING_LEN + 1];
|
|
|
|
wchar_t *p;
|
|
|
|
mbstowcs (lc, locale, ENCODING_LEN);
|
|
|
|
p = wcschr (lc, L'.');
|
|
|
|
if (p)
|
|
|
|
*p = L'\0';
|
|
|
|
p = wcschr (lc, L'_');
|
|
|
|
if (p)
|
|
|
|
*p = L'-';
|
|
|
|
LCID lcid = LocaleNameToLCID (lc, 0);
|
|
|
|
if (!lcid && !strcmp (charset, "ASCII"))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Set results */
|
2019-09-15 12:36:23 +08:00
|
|
|
strcpy (locale_out, new_locale);
|
|
|
|
strcpy (charset_out, charset);
|
2019-08-28 02:04:02 +08:00
|
|
|
return lcid;
|
|
|
|
}
|
2019-09-15 12:05:52 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_pty_slave::setup_locale (void)
|
|
|
|
{
|
|
|
|
char locale[ENCODING_LEN + 1] = "C";
|
|
|
|
char charset[ENCODING_LEN + 1] = "ASCII";
|
|
|
|
LCID lcid = get_langinfo (locale, charset);
|
|
|
|
|
|
|
|
/* Set console code page form locale */
|
2020-01-10 19:47:12 +08:00
|
|
|
if (get_pseudo_console ())
|
|
|
|
{
|
|
|
|
UINT code_page;
|
|
|
|
if (lcid == 0 || lcid == (LCID) -1)
|
|
|
|
code_page = 20127; /* ASCII */
|
|
|
|
else if (!GetLocaleInfo (lcid,
|
|
|
|
LOCALE_IDEFAULTCODEPAGE | LOCALE_RETURN_NUMBER,
|
|
|
|
(char *) &code_page, sizeof (code_page)))
|
|
|
|
code_page = 20127; /* ASCII */
|
|
|
|
SetConsoleCP (code_page);
|
|
|
|
SetConsoleOutputCP (code_page);
|
|
|
|
}
|
2019-09-15 12:05:52 +08:00
|
|
|
|
2020-01-01 14:50:36 +08:00
|
|
|
/* Set terminal code page from locale */
|
|
|
|
/* This code is borrowed from mintty: charset.c */
|
|
|
|
get_ttyp ()->term_code_page = 20127; /* Default ASCII */
|
|
|
|
char charset_u[ENCODING_LEN + 1] = {0, };
|
|
|
|
for (int i=0; charset[i] && i<ENCODING_LEN; i++)
|
|
|
|
charset_u[i] = toupper (charset[i]);
|
|
|
|
unsigned int iso;
|
|
|
|
UINT cp = 20127; /* Default for fallback */
|
|
|
|
if (sscanf (charset_u, "ISO-8859-%u", &iso) == 1 ||
|
|
|
|
sscanf (charset_u, "ISO8859-%u", &iso) == 1 ||
|
|
|
|
sscanf (charset_u, "ISO8859%u", &iso) == 1)
|
|
|
|
{
|
|
|
|
if (iso && iso <= 16 && iso !=12)
|
|
|
|
get_ttyp ()->term_code_page = 28590 + iso;
|
|
|
|
}
|
|
|
|
else if (sscanf (charset_u, "CP%u", &cp) == 1)
|
|
|
|
get_ttyp ()->term_code_page = cp;
|
|
|
|
else
|
|
|
|
for (int i=0; cs_names[i].cp; i++)
|
|
|
|
if (strcasecmp (charset_u, cs_names[i].name) == 0)
|
2019-09-15 12:05:52 +08:00
|
|
|
{
|
2020-01-01 14:50:36 +08:00
|
|
|
get_ttyp ()->term_code_page = cs_names[i].cp;
|
|
|
|
break;
|
2019-09-15 12:05:52 +08:00
|
|
|
}
|
|
|
|
}
|
2019-08-28 02:04:02 +08:00
|
|
|
|
2020-01-10 19:46:26 +08:00
|
|
|
void
|
|
|
|
fhandler_pty_slave::set_freeconsole_on_close (bool val)
|
|
|
|
{
|
|
|
|
freeconsole_on_close = val;
|
|
|
|
}
|
|
|
|
|
2020-01-27 19:22:24 +08:00
|
|
|
void
|
|
|
|
fhandler_pty_slave::wait_pcon_fwd (void)
|
|
|
|
{
|
|
|
|
acquire_output_mutex (INFINITE);
|
|
|
|
get_ttyp ()->pcon_last_time = GetTickCount ();
|
|
|
|
ResetEvent (get_ttyp ()->fwd_done);
|
|
|
|
release_output_mutex ();
|
|
|
|
cygwait (get_ttyp ()->fwd_done, INFINITE);
|
|
|
|
}
|
|
|
|
|
2020-02-09 22:46:01 +08:00
|
|
|
void
|
|
|
|
fhandler_pty_slave::trigger_redraw_screen (void)
|
|
|
|
{
|
|
|
|
/* Forcibly redraw screen based on console screen buffer. */
|
|
|
|
/* The following code triggers redrawing the screen. */
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO sbi;
|
|
|
|
GetConsoleScreenBufferInfo (get_output_handle (), &sbi);
|
|
|
|
SMALL_RECT rect = {0, 0,
|
|
|
|
(SHORT) (sbi.dwSize.X -1), (SHORT) (sbi.dwSize.Y - 1)};
|
|
|
|
COORD dest = {0, 0};
|
|
|
|
CHAR_INFO fill = {' ', 0};
|
|
|
|
ScrollConsoleScreenBuffer (get_output_handle (), &rect, NULL, dest, &fill);
|
|
|
|
get_ttyp ()->need_redraw_screen = false;
|
|
|
|
}
|
|
|
|
|
2019-08-28 02:04:02 +08:00
|
|
|
void
|
2019-09-14 05:48:14 +08:00
|
|
|
fhandler_pty_slave::fixup_after_attach (bool native_maybe, int fd_set)
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
2019-09-14 05:48:14 +08:00
|
|
|
if (fd < 0)
|
|
|
|
fd = fd_set;
|
2019-11-16 07:27:24 +08:00
|
|
|
if (get_pseudo_console ())
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
2019-11-16 07:27:24 +08:00
|
|
|
if (fhandler_console::get_console_process_id (get_helper_process_id (),
|
2019-08-28 02:04:02 +08:00
|
|
|
true))
|
|
|
|
{
|
2019-09-04 09:45:35 +08:00
|
|
|
if (pcon_attached_to != get_minor ())
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
2019-09-04 09:45:35 +08:00
|
|
|
pcon_attached_to = get_minor ();
|
2019-08-28 02:04:02 +08:00
|
|
|
init_console_handler (true);
|
2019-09-14 05:48:14 +08:00
|
|
|
}
|
|
|
|
/* Clear screen to synchronize pseudo console screen buffer
|
|
|
|
with real terminal. This is necessary because pseudo
|
|
|
|
console screen buffer is empty at start. */
|
2020-02-09 22:46:02 +08:00
|
|
|
if (get_ttyp ()->num_pcon_attached_slaves == 0)
|
2019-09-14 05:48:14 +08:00
|
|
|
/* Assume this is the first process using this pty slave. */
|
2019-11-12 21:00:23 +08:00
|
|
|
get_ttyp ()->need_redraw_screen = true;
|
2019-09-14 05:48:14 +08:00
|
|
|
|
|
|
|
get_ttyp ()->num_pcon_attached_slaves ++;
|
|
|
|
}
|
2019-08-28 02:04:02 +08:00
|
|
|
|
2020-02-09 22:46:59 +08:00
|
|
|
#if 0 /* This is for debug only. */
|
|
|
|
isHybrid = true;
|
|
|
|
get_ttyp ()->switch_to_pcon_in = true;
|
|
|
|
get_ttyp ()->switch_to_pcon_out = true;
|
|
|
|
#endif
|
|
|
|
|
2019-09-14 05:48:14 +08:00
|
|
|
if (pcon_attached_to == get_minor () && native_maybe)
|
|
|
|
{
|
|
|
|
if (fd == 0)
|
|
|
|
{
|
2020-02-09 22:46:59 +08:00
|
|
|
pull_pcon_input ();
|
2019-09-14 05:48:14 +08:00
|
|
|
DWORD mode =
|
|
|
|
ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
|
|
|
|
SetConsoleMode (get_handle (), mode);
|
|
|
|
if (get_ttyp ()->pcon_pid == 0 ||
|
2020-02-06 18:48:17 +08:00
|
|
|
!pinfo (get_ttyp ()->pcon_pid))
|
2019-09-14 05:48:14 +08:00
|
|
|
get_ttyp ()->pcon_pid = myself->pid;
|
|
|
|
get_ttyp ()->switch_to_pcon_in = true;
|
|
|
|
}
|
|
|
|
else if (fd == 1 || fd == 2)
|
|
|
|
{
|
|
|
|
DWORD mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
|
|
|
|
SetConsoleMode (get_output_handle (), mode);
|
|
|
|
if (!get_ttyp ()->switch_to_pcon_out)
|
2020-01-27 19:22:24 +08:00
|
|
|
wait_pcon_fwd ();
|
2019-09-14 05:48:14 +08:00
|
|
|
if (get_ttyp ()->pcon_pid == 0 ||
|
2020-02-06 18:48:17 +08:00
|
|
|
!pinfo (get_ttyp ()->pcon_pid))
|
2019-09-14 05:48:14 +08:00
|
|
|
get_ttyp ()->pcon_pid = myself->pid;
|
|
|
|
get_ttyp ()->switch_to_pcon_out = true;
|
2019-11-12 21:00:23 +08:00
|
|
|
|
|
|
|
if (get_ttyp ()->need_redraw_screen)
|
2020-02-09 22:46:01 +08:00
|
|
|
trigger_redraw_screen ();
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
2019-09-15 12:36:23 +08:00
|
|
|
init_console_handler (false);
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
2019-09-14 05:48:14 +08:00
|
|
|
else if (fd == 0 && native_maybe)
|
|
|
|
/* Read from unattached pseudo console cause freeze,
|
|
|
|
therefore, fallback to legacy pty. */
|
|
|
|
set_handle (get_handle_cyg ());
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-04 12:07:34 +08:00
|
|
|
void
|
2011-06-15 05:48:43 +08:00
|
|
|
fhandler_pty_slave::fixup_after_fork (HANDLE parent)
|
2003-03-04 12:07:34 +08:00
|
|
|
{
|
2019-09-14 05:48:14 +08:00
|
|
|
fixup_after_attach (false, -1);
|
2006-06-02 23:41:34 +08:00
|
|
|
// fork_fixup (parent, inuse, "inuse");
|
2011-06-15 05:48:43 +08:00
|
|
|
// fhandler_pty_common::fixup_after_fork (parent);
|
2005-11-14 12:28:45 +08:00
|
|
|
report_tty_counts (this, "inherited", "");
|
2003-03-04 12:07:34 +08:00
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
void
|
2011-06-15 05:48:43 +08:00
|
|
|
fhandler_pty_slave::fixup_after_exec ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2019-08-28 02:04:02 +08:00
|
|
|
reset_switch_to_pcon ();
|
|
|
|
|
2006-06-03 14:35:10 +08:00
|
|
|
if (!close_on_exec ())
|
2020-02-28 20:21:05 +08:00
|
|
|
fixup_after_fork (NULL); /* No parent handle required. */
|
2019-11-16 07:27:24 +08:00
|
|
|
else if (get_pseudo_console ())
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
|
|
|
int used = 0;
|
2019-09-04 09:45:35 +08:00
|
|
|
int attached = 0;
|
2019-08-28 02:04:02 +08:00
|
|
|
cygheap_fdenum cfd (false);
|
|
|
|
while (cfd.next () >= 0)
|
2019-09-04 09:45:35 +08:00
|
|
|
{
|
|
|
|
if (cfd->get_major () == DEV_PTYS_MAJOR ||
|
|
|
|
cfd->get_major () == DEV_CONS_MAJOR)
|
|
|
|
used ++;
|
|
|
|
if (cfd->get_major () == DEV_PTYS_MAJOR &&
|
|
|
|
cfd->get_minor () == pcon_attached_to)
|
|
|
|
attached ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call FreeConsole() if no tty is opened and the process
|
|
|
|
is attached to console corresponding to tty. This is
|
|
|
|
needed to make GNU screen and tmux work in Windows 10
|
|
|
|
1903. */
|
|
|
|
if (attached == 1 && get_minor () == pcon_attached_to)
|
|
|
|
pcon_attached_to = -1;
|
|
|
|
if (used == 1 /* About to close this tty */)
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
|
|
|
init_console_handler (false);
|
2020-01-10 19:46:26 +08:00
|
|
|
if (freeconsole_on_close)
|
2020-02-09 22:47:30 +08:00
|
|
|
{
|
|
|
|
FreeConsole ();
|
|
|
|
pcon_attached_to = -1;
|
|
|
|
}
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-15 12:05:52 +08:00
|
|
|
/* Set locale */
|
2020-01-01 14:50:36 +08:00
|
|
|
if (get_ttyp ()->term_code_page == 0)
|
|
|
|
setup_locale ();
|
2019-09-15 12:05:52 +08:00
|
|
|
|
2019-08-28 02:04:02 +08:00
|
|
|
/* Hook Console API */
|
2019-11-16 07:27:24 +08:00
|
|
|
if (get_pseudo_console ())
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
|
|
|
#define DO_HOOK(module, name) \
|
|
|
|
if (!name##_Orig) \
|
|
|
|
{ \
|
|
|
|
void *api = hook_api (module, #name, (void *) name##_Hooked); \
|
|
|
|
name##_Orig = (__typeof__ (name) *) api; \
|
2019-09-15 12:36:23 +08:00
|
|
|
/*if (api) system_printf (#name " hooked.");*/ \
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
2019-09-04 21:46:51 +08:00
|
|
|
DO_HOOK (NULL, WriteFile);
|
|
|
|
DO_HOOK (NULL, WriteConsoleA);
|
|
|
|
DO_HOOK (NULL, WriteConsoleW);
|
|
|
|
DO_HOOK (NULL, ReadFile);
|
|
|
|
DO_HOOK (NULL, ReadConsoleA);
|
|
|
|
DO_HOOK (NULL, ReadConsoleW);
|
|
|
|
DO_HOOK (NULL, WriteConsoleOutputA);
|
|
|
|
DO_HOOK (NULL, WriteConsoleOutputW);
|
|
|
|
DO_HOOK (NULL, WriteConsoleOutputCharacterA);
|
|
|
|
DO_HOOK (NULL, WriteConsoleOutputCharacterW);
|
|
|
|
DO_HOOK (NULL, WriteConsoleOutputAttribute);
|
2019-09-06 21:01:27 +08:00
|
|
|
DO_HOOK (NULL, SetConsoleCursorPosition);
|
2019-09-04 21:46:51 +08:00
|
|
|
DO_HOOK (NULL, SetConsoleTextAttribute);
|
|
|
|
DO_HOOK (NULL, WriteConsoleInputA);
|
|
|
|
DO_HOOK (NULL, WriteConsoleInputW);
|
|
|
|
DO_HOOK (NULL, ReadConsoleInputA);
|
|
|
|
DO_HOOK (NULL, ReadConsoleInputW);
|
|
|
|
DO_HOOK (NULL, PeekConsoleInputA);
|
|
|
|
DO_HOOK (NULL, PeekConsoleInputW);
|
2019-09-18 22:29:18 +08:00
|
|
|
/* CreateProcess() is hooked for GDB etc. */
|
|
|
|
DO_HOOK (NULL, CreateProcessA);
|
|
|
|
DO_HOOK (NULL, CreateProcessW);
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2010-04-20 03:52:43 +08:00
|
|
|
/* This thread function handles the master control pipe. It waits for a
|
|
|
|
client to connect. Then it checks if the client process has permissions
|
|
|
|
to access the tty handles. If so, it opens the client process and
|
|
|
|
duplicates the handles into that process. If that fails, it sends a reply
|
|
|
|
with at least one handle set to NULL and an error code. Last but not
|
|
|
|
least, the client is disconnected and the thread waits for the next client.
|
|
|
|
|
|
|
|
A special case is when the master side of the tty is about to be closed.
|
|
|
|
The client side is the fhandler_pty_master::close function and it sends
|
2015-12-16 04:49:19 +08:00
|
|
|
a PID -1 in that case. A check is performed that the request to leave
|
|
|
|
really comes from the master process itself.
|
2010-04-20 03:52:43 +08:00
|
|
|
|
2010-10-25 02:50:57 +08:00
|
|
|
Since there's always only one pipe instance, there's a chance that clients
|
|
|
|
have to wait to connect to the master control pipe. Therefore the client
|
|
|
|
calls to CallNamedPipe should have a big enough timeout value. For now this
|
2010-04-20 03:52:43 +08:00
|
|
|
is 500ms. Hope that's enough. */
|
|
|
|
|
|
|
|
DWORD
|
|
|
|
fhandler_pty_master::pty_master_thread ()
|
|
|
|
{
|
|
|
|
bool exit = false;
|
|
|
|
GENERIC_MAPPING map = { EVENT_QUERY_STATE, EVENT_MODIFY_STATE, 0,
|
|
|
|
EVENT_QUERY_STATE | EVENT_MODIFY_STATE };
|
|
|
|
pipe_request req;
|
|
|
|
DWORD len;
|
|
|
|
security_descriptor sd;
|
|
|
|
HANDLE token;
|
|
|
|
PRIVILEGE_SET ps;
|
|
|
|
DWORD pid;
|
2011-04-29 17:48:25 +08:00
|
|
|
NTSTATUS status;
|
2010-04-20 03:52:43 +08:00
|
|
|
|
|
|
|
termios_printf ("Entered");
|
2011-04-29 17:48:25 +08:00
|
|
|
while (!exit && (ConnectNamedPipe (master_ctl, NULL)
|
|
|
|
|| GetLastError () == ERROR_PIPE_CONNECTED))
|
2010-04-20 03:52:43 +08:00
|
|
|
{
|
2020-02-09 22:46:03 +08:00
|
|
|
pipe_reply repl = { NULL, NULL, NULL, NULL, 0 };
|
2010-04-20 03:52:43 +08:00
|
|
|
bool deimp = false;
|
2011-04-29 17:48:25 +08:00
|
|
|
NTSTATUS allow = STATUS_ACCESS_DENIED;
|
2010-04-20 03:52:43 +08:00
|
|
|
ACCESS_MASK access = EVENT_MODIFY_STATE;
|
|
|
|
HANDLE client = NULL;
|
|
|
|
|
|
|
|
if (!ReadFile (master_ctl, &req, sizeof req, &len, NULL))
|
2011-06-06 13:02:13 +08:00
|
|
|
{
|
2010-04-20 03:52:43 +08:00
|
|
|
termios_printf ("ReadFile, %E");
|
|
|
|
goto reply;
|
|
|
|
}
|
|
|
|
if (!GetNamedPipeClientProcessId (master_ctl, &pid))
|
|
|
|
pid = req.pid;
|
|
|
|
if (get_object_sd (input_available_event, sd))
|
|
|
|
{
|
|
|
|
termios_printf ("get_object_sd, %E");
|
|
|
|
goto reply;
|
|
|
|
}
|
|
|
|
cygheap->user.deimpersonate ();
|
|
|
|
deimp = true;
|
|
|
|
if (!ImpersonateNamedPipeClient (master_ctl))
|
|
|
|
{
|
|
|
|
termios_printf ("ImpersonateNamedPipeClient, %E");
|
|
|
|
goto reply;
|
|
|
|
}
|
2011-04-29 17:48:25 +08:00
|
|
|
status = NtOpenThreadToken (GetCurrentThread (), TOKEN_QUERY, TRUE,
|
|
|
|
&token);
|
|
|
|
if (!NT_SUCCESS (status))
|
2010-04-20 03:52:43 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
termios_printf ("NtOpenThreadToken, %y", status);
|
2011-04-29 17:48:25 +08:00
|
|
|
SetLastError (RtlNtStatusToDosError (status));
|
2010-04-20 03:52:43 +08:00
|
|
|
goto reply;
|
|
|
|
}
|
|
|
|
len = sizeof ps;
|
2011-04-29 17:48:25 +08:00
|
|
|
status = NtAccessCheck (sd, token, access, &map, &ps, &len, &access,
|
|
|
|
&allow);
|
|
|
|
NtClose (token);
|
|
|
|
if (!NT_SUCCESS (status))
|
2010-04-20 03:52:43 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
termios_printf ("NtAccessCheck, %y", status);
|
2011-04-29 17:48:25 +08:00
|
|
|
SetLastError (RtlNtStatusToDosError (status));
|
2010-04-20 03:52:43 +08:00
|
|
|
goto reply;
|
|
|
|
}
|
|
|
|
if (!RevertToSelf ())
|
|
|
|
{
|
|
|
|
termios_printf ("RevertToSelf, %E");
|
|
|
|
goto reply;
|
|
|
|
}
|
|
|
|
if (req.pid == (DWORD) -1) /* Request to finish thread. */
|
|
|
|
{
|
2015-12-16 04:49:19 +08:00
|
|
|
/* Check if the requesting process is the master process itself. */
|
|
|
|
if (pid == GetCurrentProcessId ())
|
2010-04-20 03:52:43 +08:00
|
|
|
exit = true;
|
|
|
|
goto reply;
|
|
|
|
}
|
2011-04-29 17:48:25 +08:00
|
|
|
if (NT_SUCCESS (allow))
|
2010-04-20 03:52:43 +08:00
|
|
|
{
|
|
|
|
client = OpenProcess (PROCESS_DUP_HANDLE, FALSE, pid);
|
|
|
|
if (!client)
|
|
|
|
{
|
|
|
|
termios_printf ("OpenProcess, %E");
|
|
|
|
goto reply;
|
|
|
|
}
|
|
|
|
if (!DuplicateHandle (GetCurrentProcess (), from_master,
|
|
|
|
client, &repl.from_master,
|
|
|
|
0, TRUE, DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
termios_printf ("DuplicateHandle (from_master), %E");
|
|
|
|
goto reply;
|
|
|
|
}
|
2019-08-28 02:04:02 +08:00
|
|
|
if (!DuplicateHandle (GetCurrentProcess (), from_master_cyg,
|
|
|
|
client, &repl.from_master_cyg,
|
|
|
|
0, TRUE, DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
termios_printf ("DuplicateHandle (from_master_cyg), %E");
|
|
|
|
goto reply;
|
|
|
|
}
|
2010-04-20 03:52:43 +08:00
|
|
|
if (!DuplicateHandle (GetCurrentProcess (), to_master,
|
|
|
|
client, &repl.to_master,
|
|
|
|
0, TRUE, DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
termios_printf ("DuplicateHandle (to_master), %E");
|
|
|
|
goto reply;
|
|
|
|
}
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
if (!DuplicateHandle (GetCurrentProcess (), to_master_cyg,
|
|
|
|
client, &repl.to_master_cyg,
|
|
|
|
0, TRUE, DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
termios_printf ("DuplicateHandle (to_master_cyg), %E");
|
|
|
|
goto reply;
|
|
|
|
}
|
2010-04-20 03:52:43 +08:00
|
|
|
}
|
|
|
|
reply:
|
|
|
|
repl.error = GetLastError ();
|
|
|
|
if (client)
|
|
|
|
CloseHandle (client);
|
|
|
|
if (deimp)
|
|
|
|
cygheap->user.reimpersonate ();
|
|
|
|
sd.free ();
|
2013-04-23 17:44:36 +08:00
|
|
|
termios_printf ("Reply: from %p, to %p, error %u",
|
2010-04-20 03:52:43 +08:00
|
|
|
repl.from_master, repl.to_master, repl.error );
|
|
|
|
if (!WriteFile (master_ctl, &repl, sizeof repl, &len, NULL))
|
|
|
|
termios_printf ("WriteFile, %E");
|
|
|
|
if (!DisconnectNamedPipe (master_ctl))
|
|
|
|
termios_printf ("DisconnectNamedPipe, %E");
|
|
|
|
}
|
|
|
|
termios_printf ("Leaving");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-09 22:46:59 +08:00
|
|
|
void
|
|
|
|
fhandler_pty_master::transfer_input_to_pcon (void)
|
|
|
|
{
|
|
|
|
WaitForSingleObject (input_mutex, INFINITE);
|
|
|
|
DWORD n;
|
|
|
|
size_t transfered = 0;
|
|
|
|
while (::bytes_available (n, from_master_cyg) && n)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
ReadFile (from_master_cyg, buf, sizeof (buf), &n, 0);
|
|
|
|
char *p = buf;
|
|
|
|
while ((p = (char *) memchr (p, '\n', n - (p - buf))))
|
|
|
|
*p = '\r';
|
|
|
|
if (WriteFile (to_slave, buf, n, &n, 0))
|
|
|
|
transfered += n;
|
|
|
|
}
|
|
|
|
DWORD bytes_left = eat_readahead (-1);
|
|
|
|
if (bytes_left)
|
|
|
|
{
|
|
|
|
if (WriteFile (to_slave, rabuf (), bytes_left, &n, NULL))
|
|
|
|
transfered += n;
|
|
|
|
}
|
|
|
|
if (transfered)
|
|
|
|
get_ttyp ()->pcon_in_empty = false;
|
|
|
|
ReleaseMutex (input_mutex);
|
|
|
|
}
|
|
|
|
|
2010-04-20 03:52:43 +08:00
|
|
|
static DWORD WINAPI
|
2011-05-06 06:30:53 +08:00
|
|
|
pty_master_thread (VOID *arg)
|
|
|
|
{
|
2010-04-20 03:52:43 +08:00
|
|
|
return ((fhandler_pty_master *) arg)->pty_master_thread ();
|
2011-05-06 06:30:53 +08:00
|
|
|
}
|
2006-06-02 23:41:34 +08:00
|
|
|
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
DWORD
|
|
|
|
fhandler_pty_master::pty_master_fwd_thread ()
|
|
|
|
{
|
|
|
|
DWORD rlen;
|
2019-08-28 02:04:02 +08:00
|
|
|
char outbuf[65536];
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
|
2019-08-28 02:04:02 +08:00
|
|
|
termios_printf ("Started.");
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
for (;;)
|
|
|
|
{
|
2020-01-27 19:22:24 +08:00
|
|
|
if (get_pseudo_console ())
|
|
|
|
{
|
|
|
|
/* The forwarding in pseudo console sometimes stops for
|
|
|
|
16-32 msec even if it already has data to transfer.
|
|
|
|
If the time without transfer exceeds 32 msec, the
|
|
|
|
forwarding is supposed to be finished. */
|
|
|
|
const int sleep_in_pcon = 16;
|
|
|
|
const int time_to_wait = sleep_in_pcon * 2 + 1/* margine */;
|
|
|
|
get_ttyp ()->pcon_last_time = GetTickCount ();
|
|
|
|
while (::bytes_available (rlen, from_slave) && rlen == 0)
|
|
|
|
{
|
|
|
|
acquire_output_mutex (INFINITE);
|
|
|
|
if (GetTickCount () - get_ttyp ()->pcon_last_time > time_to_wait)
|
|
|
|
SetEvent (get_ttyp ()->fwd_done);
|
|
|
|
release_output_mutex ();
|
2020-02-09 22:46:59 +08:00
|
|
|
/* Forcibly transfer input if it is requested by slave.
|
|
|
|
This happens when input data should be transfered
|
|
|
|
from the input pipe for cygwin apps to the input pipe
|
|
|
|
for native apps. */
|
|
|
|
if (get_ttyp ()->req_transfer_input_to_pcon)
|
|
|
|
{
|
|
|
|
transfer_input_to_pcon ();
|
|
|
|
get_ttyp ()->req_transfer_input_to_pcon = false;
|
|
|
|
}
|
2020-01-27 19:22:24 +08:00
|
|
|
Sleep (1);
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 02:04:02 +08:00
|
|
|
if (!ReadFile (from_slave, outbuf, sizeof outbuf, &rlen, NULL))
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
{
|
|
|
|
termios_printf ("ReadFile for forwarding failed, %E");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ssize_t wlen = rlen;
|
2019-08-28 02:04:02 +08:00
|
|
|
char *ptr = outbuf;
|
2019-11-16 07:27:24 +08:00
|
|
|
if (get_pseudo_console ())
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
|
|
|
/* Avoid duplicating slave output which is already sent to
|
|
|
|
to_master_cyg */
|
2019-09-14 05:48:14 +08:00
|
|
|
if (!get_ttyp ()->switch_to_pcon_out)
|
2019-08-28 02:04:02 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Avoid setting window title to "cygwin-console-helper.exe" */
|
|
|
|
int state = 0;
|
|
|
|
int start_at = 0;
|
|
|
|
for (DWORD i=0; i<rlen; i++)
|
|
|
|
if (outbuf[i] == '\033')
|
|
|
|
{
|
|
|
|
start_at = i;
|
|
|
|
state = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if ((state == 1 && outbuf[i] == ']') ||
|
|
|
|
(state == 2 && outbuf[i] == '0') ||
|
2019-09-04 09:45:35 +08:00
|
|
|
(state == 3 && outbuf[i] == ';'))
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
|
|
|
state ++;
|
|
|
|
continue;
|
|
|
|
}
|
2019-09-04 09:45:35 +08:00
|
|
|
else if (state == 4 && outbuf[i] == '\a')
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
|
|
|
memmove (&outbuf[start_at], &outbuf[i+1], rlen-i-1);
|
|
|
|
state = 0;
|
|
|
|
rlen = wlen = start_at + rlen - i - 1;
|
|
|
|
continue;
|
|
|
|
}
|
2019-09-04 09:45:35 +08:00
|
|
|
else if (outbuf[i] == '\a')
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
|
|
|
state = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-09-04 09:45:35 +08:00
|
|
|
/* Remove ESC sequence which returns results to console
|
|
|
|
input buffer. Without this, cursor position report
|
|
|
|
is put into the input buffer as a garbage. */
|
|
|
|
/* Remove ESC sequence to report cursor position. */
|
|
|
|
char *p0;
|
|
|
|
while ((p0 = (char *) memmem (outbuf, rlen, "\033[6n", 4)))
|
|
|
|
{
|
|
|
|
memmove (p0, p0+4, rlen - (p0+4 - outbuf));
|
|
|
|
rlen -= 4;
|
|
|
|
}
|
|
|
|
/* Remove ESC sequence to report terminal identity. */
|
|
|
|
while ((p0 = (char *) memmem (outbuf, rlen, "\033[0c", 4)))
|
|
|
|
{
|
|
|
|
memmove (p0, p0+4, rlen - (p0+4 - outbuf));
|
|
|
|
rlen -= 4;
|
|
|
|
}
|
|
|
|
wlen = rlen;
|
|
|
|
|
2019-08-28 02:04:02 +08:00
|
|
|
size_t nlen;
|
2019-09-18 22:29:19 +08:00
|
|
|
char *buf = convert_mb_str
|
|
|
|
(get_ttyp ()->term_code_page, &nlen, CP_UTF8, ptr, wlen);
|
|
|
|
|
2019-08-28 02:04:02 +08:00
|
|
|
ptr = buf;
|
|
|
|
wlen = rlen = nlen;
|
|
|
|
|
|
|
|
/* OPOST processing was already done in pseudo console,
|
|
|
|
so just write it to to_master_cyg. */
|
|
|
|
DWORD written;
|
|
|
|
acquire_output_mutex (INFINITE);
|
|
|
|
while (rlen>0)
|
|
|
|
{
|
|
|
|
if (!WriteFile (to_master_cyg, ptr, wlen, &written, NULL))
|
|
|
|
{
|
|
|
|
termios_printf ("WriteFile for forwarding failed, %E");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ptr += written;
|
|
|
|
wlen = (rlen -= written);
|
|
|
|
}
|
|
|
|
release_output_mutex ();
|
2019-09-18 22:29:19 +08:00
|
|
|
mb_str_free (buf);
|
2019-08-28 02:04:02 +08:00
|
|
|
continue;
|
|
|
|
}
|
2019-09-18 22:29:20 +08:00
|
|
|
size_t nlen;
|
|
|
|
char *buf = convert_mb_str
|
|
|
|
(get_ttyp ()->term_code_page, &nlen, GetConsoleOutputCP (), ptr, wlen);
|
|
|
|
|
|
|
|
ptr = buf;
|
|
|
|
wlen = rlen = nlen;
|
2019-08-28 02:04:02 +08:00
|
|
|
acquire_output_mutex (INFINITE);
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
while (rlen>0)
|
|
|
|
{
|
2019-08-28 02:04:02 +08:00
|
|
|
if (!process_opost_output (to_master_cyg, ptr, wlen, false))
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
{
|
|
|
|
termios_printf ("WriteFile for forwarding failed, %E");
|
|
|
|
break;
|
|
|
|
}
|
2019-08-28 02:04:02 +08:00
|
|
|
ptr += wlen;
|
|
|
|
wlen = (rlen -= wlen);
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
}
|
2019-08-28 02:04:02 +08:00
|
|
|
release_output_mutex ();
|
2019-09-18 22:29:20 +08:00
|
|
|
mb_str_free (buf);
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DWORD WINAPI
|
|
|
|
pty_master_fwd_thread (VOID *arg)
|
|
|
|
{
|
|
|
|
return ((fhandler_pty_master *) arg)->pty_master_fwd_thread ();
|
|
|
|
}
|
|
|
|
|
2019-08-28 02:04:02 +08:00
|
|
|
/* If master process is running as service, attaching to
|
|
|
|
pseudo console should be done in fork. If attaching
|
|
|
|
is done in spawn for inetd or sshd, it fails because
|
|
|
|
the helper process is running as privileged user while
|
|
|
|
slave process is not. This function is used to determine
|
|
|
|
if the process is running as a srvice or not. */
|
2019-11-06 20:08:43 +08:00
|
|
|
inline static bool
|
2019-08-28 02:04:02 +08:00
|
|
|
is_running_as_service (void)
|
|
|
|
{
|
2019-11-06 20:08:43 +08:00
|
|
|
return check_token_membership (well_known_service_sid)
|
|
|
|
|| cygheap->user.saved_sid () == well_known_system_sid;
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
fhandler_pty_master::setup_pseudoconsole ()
|
|
|
|
{
|
2020-01-21 21:25:13 +08:00
|
|
|
if (disable_pcon)
|
|
|
|
return false;
|
2019-11-07 00:29:29 +08:00
|
|
|
/* If the legacy console mode is enabled, pseudo console seems
|
|
|
|
not to work as expected. To determine console mode, registry
|
|
|
|
key ForceV2 in HKEY_CURRENT_USER\Console is checked. */
|
|
|
|
reg_key reg (HKEY_CURRENT_USER, KEY_READ, L"Console", NULL);
|
|
|
|
if (reg.error ())
|
|
|
|
return false;
|
|
|
|
if (reg.get_dword (L"ForceV2", 1) == 0)
|
|
|
|
{
|
|
|
|
termios_printf ("Pseudo console is disabled "
|
|
|
|
"because the legacy console mode is enabled.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-28 02:04:02 +08:00
|
|
|
/* Pseudo console supprot is realized using a tricky technic.
|
|
|
|
PTY need the pseudo console handles, however, they cannot
|
|
|
|
be retrieved by normal procedure. Therefore, run a helper
|
|
|
|
process in a pseudo console and get them from the helper.
|
|
|
|
Slave process will attach to the pseudo console in the
|
|
|
|
helper process using AttachConsole(). */
|
2020-02-11 01:45:14 +08:00
|
|
|
COORD size = {
|
|
|
|
(SHORT) get_ttyp ()->winsize.ws_col,
|
|
|
|
(SHORT) get_ttyp ()->winsize.ws_row
|
|
|
|
};
|
2019-08-28 02:04:02 +08:00
|
|
|
CreatePipe (&from_master, &to_slave, &sec_none, 0);
|
2019-09-15 18:55:44 +08:00
|
|
|
SetLastError (ERROR_SUCCESS);
|
2019-08-28 02:04:02 +08:00
|
|
|
HRESULT res = CreatePseudoConsole (size, from_master, to_master,
|
2019-11-16 07:27:24 +08:00
|
|
|
0, &get_ttyp ()->h_pseudo_console);
|
2019-09-15 18:55:44 +08:00
|
|
|
if (res != S_OK || GetLastError () == ERROR_PROC_NOT_FOUND)
|
2019-08-28 02:04:02 +08:00
|
|
|
{
|
2019-09-15 18:55:44 +08:00
|
|
|
if (res != S_OK)
|
|
|
|
system_printf ("CreatePseudoConsole() failed. %08x\n",
|
|
|
|
GetLastError ());
|
2020-02-11 01:45:14 +08:00
|
|
|
goto fallback;
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If master process is running as service, attaching to
|
|
|
|
pseudo console should be done in fork. If attaching
|
|
|
|
is done in spawn for inetd or sshd, it fails because
|
|
|
|
the helper process is running as privileged user while
|
|
|
|
slave process is not. */
|
|
|
|
if (is_running_as_service ())
|
|
|
|
get_ttyp ()->attach_pcon_in_fork = true;
|
|
|
|
|
|
|
|
STARTUPINFOEXW si_helper;
|
2020-02-11 01:45:14 +08:00
|
|
|
HANDLE hello, goodbye;
|
2019-08-28 02:04:02 +08:00
|
|
|
HANDLE hr, hw;
|
|
|
|
PROCESS_INFORMATION pi_helper;
|
|
|
|
HANDLE hpConIn, hpConOut;
|
2020-02-11 01:45:14 +08:00
|
|
|
{
|
|
|
|
SIZE_T bytesRequired;
|
|
|
|
InitializeProcThreadAttributeList (NULL, 2, 0, &bytesRequired);
|
|
|
|
ZeroMemory (&si_helper, sizeof (si_helper));
|
|
|
|
si_helper.StartupInfo.cb = sizeof (STARTUPINFOEXW);
|
|
|
|
si_helper.lpAttributeList = (PPROC_THREAD_ATTRIBUTE_LIST)
|
|
|
|
HeapAlloc (GetProcessHeap (), 0, bytesRequired);
|
|
|
|
if (si_helper.lpAttributeList == NULL)
|
|
|
|
goto cleanup_pseudo_console;
|
|
|
|
if (!InitializeProcThreadAttributeList (si_helper.lpAttributeList,
|
|
|
|
2, 0, &bytesRequired))
|
|
|
|
goto cleanup_heap;
|
|
|
|
if (!UpdateProcThreadAttribute (si_helper.lpAttributeList,
|
|
|
|
0,
|
|
|
|
PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE,
|
|
|
|
get_ttyp ()->h_pseudo_console,
|
|
|
|
sizeof (get_ttyp ()->h_pseudo_console),
|
|
|
|
NULL, NULL))
|
|
|
|
goto cleanup_heap;
|
|
|
|
/* Create events for start/stop helper process. */
|
|
|
|
hello = CreateEvent (&sec_none, true, false, NULL);
|
|
|
|
goodbye = CreateEvent (&sec_none, true, false, NULL);
|
|
|
|
/* Create a pipe for receiving pseudo console handles */
|
|
|
|
CreatePipe (&hr, &hw, &sec_none, 0);
|
|
|
|
/* Inherit only handles which are needed by helper. */
|
|
|
|
HANDLE handles_to_inherit[] = {hello, goodbye, hw};
|
|
|
|
if (!UpdateProcThreadAttribute (si_helper.lpAttributeList,
|
|
|
|
0,
|
|
|
|
PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
|
|
|
|
handles_to_inherit,
|
|
|
|
sizeof (handles_to_inherit),
|
|
|
|
NULL, NULL))
|
|
|
|
goto cleanup_event_and_pipes;
|
|
|
|
/* Create helper process */
|
|
|
|
WCHAR cmd[MAX_PATH];
|
|
|
|
path_conv helper ("/bin/cygwin-console-helper.exe");
|
|
|
|
size_t len = helper.get_wide_win32_path_len ();
|
|
|
|
helper.get_wide_win32_path (cmd);
|
|
|
|
__small_swprintf (cmd + len, L" %p %p %p", hello, goodbye, hw);
|
|
|
|
si_helper.StartupInfo.dwFlags = STARTF_USESTDHANDLES;
|
|
|
|
si_helper.StartupInfo.hStdInput = NULL;
|
|
|
|
si_helper.StartupInfo.hStdOutput = NULL;
|
|
|
|
si_helper.StartupInfo.hStdError = NULL;
|
|
|
|
if (!CreateProcessW (NULL, cmd, &sec_none, &sec_none,
|
|
|
|
TRUE, EXTENDED_STARTUPINFO_PRESENT,
|
|
|
|
NULL, NULL, &si_helper.StartupInfo, &pi_helper))
|
|
|
|
goto cleanup_event_and_pipes;
|
|
|
|
WaitForSingleObject (hello, INFINITE);
|
|
|
|
CloseHandle (hello);
|
|
|
|
CloseHandle (pi_helper.hThread);
|
|
|
|
/* Retrieve pseudo console handles */
|
|
|
|
DWORD rLen;
|
|
|
|
char buf[64];
|
|
|
|
if (!ReadFile (hr, buf, sizeof (buf), &rLen, NULL))
|
|
|
|
goto cleanup_helper_process;
|
|
|
|
buf[rLen] = '\0';
|
|
|
|
sscanf (buf, "StdHandles=%p,%p", &hpConIn, &hpConOut);
|
|
|
|
if (!DuplicateHandle (pi_helper.hProcess, hpConIn,
|
|
|
|
GetCurrentProcess (), &hpConIn, 0,
|
|
|
|
TRUE, DUPLICATE_SAME_ACCESS))
|
|
|
|
goto cleanup_helper_process;
|
|
|
|
if (!DuplicateHandle (pi_helper.hProcess, hpConOut,
|
|
|
|
GetCurrentProcess (), &hpConOut, 0,
|
|
|
|
TRUE, DUPLICATE_SAME_ACCESS))
|
|
|
|
goto cleanup_pcon_in;
|
|
|
|
CloseHandle (hr);
|
|
|
|
CloseHandle (hw);
|
|
|
|
/* Clean up */
|
|
|
|
DeleteProcThreadAttributeList (si_helper.lpAttributeList);
|
|
|
|
HeapFree (GetProcessHeap (), 0, si_helper.lpAttributeList);
|
|
|
|
}
|
2019-08-28 02:04:02 +08:00
|
|
|
/* Setting information of stuffs regarding pseudo console */
|
2019-11-16 07:27:24 +08:00
|
|
|
get_ttyp ()->h_helper_goodbye = goodbye;
|
|
|
|
get_ttyp ()->h_helper_process = pi_helper.hProcess;
|
|
|
|
get_ttyp ()->helper_process_id = pi_helper.dwProcessId;
|
2019-08-28 02:04:02 +08:00
|
|
|
CloseHandle (from_master);
|
|
|
|
CloseHandle (to_master);
|
|
|
|
from_master = hpConIn;
|
|
|
|
to_master = hpConOut;
|
2020-02-11 01:45:14 +08:00
|
|
|
ResizePseudoConsole (get_ttyp ()->h_pseudo_console, size);
|
2019-08-28 02:04:02 +08:00
|
|
|
return true;
|
2020-02-11 01:45:14 +08:00
|
|
|
|
|
|
|
cleanup_pcon_in:
|
|
|
|
CloseHandle (hpConIn);
|
|
|
|
cleanup_helper_process:
|
|
|
|
SetEvent (goodbye);
|
|
|
|
WaitForSingleObject (pi_helper.hProcess, INFINITE);
|
|
|
|
CloseHandle (pi_helper.hProcess);
|
|
|
|
goto skip_close_hello;
|
|
|
|
cleanup_event_and_pipes:
|
|
|
|
CloseHandle (hello);
|
|
|
|
skip_close_hello:
|
|
|
|
CloseHandle (goodbye);
|
|
|
|
CloseHandle (hr);
|
|
|
|
CloseHandle (hw);
|
|
|
|
cleanup_heap:
|
|
|
|
HeapFree (GetProcessHeap (), 0, si_helper.lpAttributeList);
|
|
|
|
cleanup_pseudo_console:
|
|
|
|
{
|
|
|
|
HPCON_INTERNAL *hp = (HPCON_INTERNAL *) get_ttyp ()->h_pseudo_console;
|
|
|
|
HANDLE tmp = hp->hConHostProcess;
|
|
|
|
ClosePseudoConsole (get_pseudo_console ());
|
|
|
|
CloseHandle (tmp);
|
|
|
|
}
|
|
|
|
fallback:
|
|
|
|
CloseHandle (from_master);
|
|
|
|
CloseHandle (to_slave);
|
|
|
|
from_master = from_master_cyg;
|
|
|
|
to_slave = NULL;
|
|
|
|
get_ttyp ()->h_pseudo_console = NULL;
|
|
|
|
return false;
|
2019-08-28 02:04:02 +08:00
|
|
|
}
|
|
|
|
|
2006-06-02 23:41:34 +08:00
|
|
|
bool
|
2011-06-15 05:48:43 +08:00
|
|
|
fhandler_pty_master::setup ()
|
2006-06-02 23:41:34 +08:00
|
|
|
{
|
2008-03-23 05:04:16 +08:00
|
|
|
int res;
|
2010-04-20 03:52:43 +08:00
|
|
|
security_descriptor sd;
|
|
|
|
SECURITY_ATTRIBUTES sa = { sizeof (SECURITY_ATTRIBUTES), NULL, TRUE };
|
|
|
|
|
2011-10-16 06:37:30 +08:00
|
|
|
/* Find an unallocated pty to use. */
|
2019-08-28 02:04:02 +08:00
|
|
|
int unit = cygwin_shared->tty.allocate (from_master_cyg, get_output_handle ());
|
2011-10-16 06:37:30 +08:00
|
|
|
if (unit < 0)
|
|
|
|
return false;
|
|
|
|
|
2019-08-28 02:04:02 +08:00
|
|
|
/* from_master should be used for pseudo console.
|
|
|
|
Just copy from_master_cyg here for the case that
|
|
|
|
pseudo console is not available. */
|
|
|
|
from_master = from_master_cyg;
|
|
|
|
|
2011-10-16 06:37:30 +08:00
|
|
|
ProtectHandle1 (get_output_handle (), to_pty);
|
|
|
|
|
|
|
|
tty& t = *cygwin_shared->tty[unit];
|
|
|
|
_tc = (tty_min *) &t;
|
2006-06-04 05:44:01 +08:00
|
|
|
|
2011-06-04 08:12:29 +08:00
|
|
|
tcinit (true); /* Set termios information. Force initialization. */
|
2006-06-02 23:41:34 +08:00
|
|
|
|
|
|
|
const char *errstr = NULL;
|
|
|
|
DWORD pipe_mode = PIPE_NOWAIT;
|
|
|
|
|
2008-07-27 03:35:21 +08:00
|
|
|
if (!SetNamedPipeHandleState (get_output_handle (), &pipe_mode, NULL, NULL))
|
|
|
|
termios_printf ("can't set output_handle(%p) to non-blocking mode",
|
|
|
|
get_output_handle ());
|
2006-06-02 23:41:34 +08:00
|
|
|
|
2019-09-15 12:36:23 +08:00
|
|
|
char pipename[sizeof ("ptyNNNN-to-master-cyg")];
|
2011-10-16 06:37:30 +08:00
|
|
|
__small_sprintf (pipename, "pty%d-to-master", unit);
|
2019-08-28 02:04:02 +08:00
|
|
|
res = fhandler_pipe::create (&sec_none, &from_slave, &to_master,
|
2011-11-24 02:56:57 +08:00
|
|
|
fhandler_pty_common::pipesize, pipename, 0);
|
2008-03-23 05:04:16 +08:00
|
|
|
if (res)
|
2006-06-02 23:41:34 +08:00
|
|
|
{
|
|
|
|
errstr = "output pipe";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
__small_sprintf (pipename, "pty%d-to-master-cyg", unit);
|
2019-08-28 02:04:02 +08:00
|
|
|
res = fhandler_pipe::create (&sec_none, &get_handle (), &to_master_cyg,
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
fhandler_pty_common::pipesize, pipename, 0);
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
errstr = "output pipe for cygwin";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2019-08-28 02:04:02 +08:00
|
|
|
ProtectHandle1 (from_slave, from_pty);
|
2006-06-02 23:41:34 +08:00
|
|
|
|
2015-03-05 20:57:34 +08:00
|
|
|
__small_sprintf (pipename, "pty%d-echoloop", unit);
|
|
|
|
res = fhandler_pipe::create (&sec_none, &echo_r, &echo_w,
|
|
|
|
fhandler_pty_common::pipesize, pipename, 0);
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
errstr = "echo pipe";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2010-04-20 03:52:43 +08:00
|
|
|
/* Create security attribute. Default permissions are 0620. */
|
|
|
|
sd.malloc (sizeof (SECURITY_DESCRIPTOR));
|
2011-04-28 17:30:36 +08:00
|
|
|
RtlCreateSecurityDescriptor (sd, SECURITY_DESCRIPTOR_REVISION);
|
Reapply POSIX ACL changes.
- New, unified implementation of POSIX permission and ACL handling. The
new ACLs now store the POSIX ACL MASK/CLASS_OBJ permission mask, and
they allow to inherit the S_ISGID bit. ACL inheritance now really
works as desired, in a limited, but theoretically equivalent fashion
even for non-Cygwin processes.
To accommodate Windows default ACLs, the new code ignores SYSTEM and
Administrators group permissions when computing the MASK/CLASS_OBJ
permission mask on old ACLs, and it doesn't deny access to SYSTEM and
Administrators group based on the value of MASK/CLASS_OBJ when
creating the new ACLs.
The new code now handles the S_ISGID bit on directories as on Linux:
Setting S_ISGID on a directory causes new files and subdirs created
within to inherit its group, rather than the primary group of the user
who created the file. This only works for files and directories
created by Cygwin processes.
2015-05-29 Corinna Vinschen <corinna@vinschen.de>
Reapply POSIX ACL changes.
* utils.xml (setfacl): Show new option output.
(getfacl): Show new option output.
* sec_acl.cc (get_posix_access): Check for Cygwin "standard" ACL.
Apply umask, if so. Align comments.
* security.cc (set_created_file_access): Fix permission masking by
incoming requested file mode.
* sec_acl.cc (set_posix_access): Apply mask only in terms of execute bit
for SYSTEM and Admins group.
* sec_acl.cc (set_posix_access): Don't create DENY ACEs for USER and
GROUP entries if they are the same as USER_OBJ or GROUP_OBJ.
* fhandler.h (fhandler_pty_slave::facl): Add prototype.
* fhandler_tty.cc (fhandler_pty_slave::facl): New method.
(fhandler_pty_slave::fchown): Fix uid/gid handling.
* sec_acl.cc (set_posix_access): Drop superfluous class_idx variable.
Simplify and move around code in a few places. To improve ACL
readability, add r/w permissions to Admins ACE appended to pty ACL.
Add comment to explain Windows ACE Mask filtering being in the way of
creating a real CLASS_OBJ.
(get_posix_access): Fake CLASS_OBJ for ptys. Explain why.
* security.cc (get_object_attribute): Add S_IFCHR flag to attributes
when calling get_posix_access.
* sec_acl.cc (set_posix_access): Move merging group perms into owner
perms in case of owner == group after mask has been computed. Take
mask into account when doing so to avoid unnecessary ACCESS_DENIED_ACE.
* sec_acl.cc (get_posix_access): Only set saw_group_obj flag if we saw
the ACCESS_ALLOWED_ACE.
* fhandler_disk_file.cc (fhandler_disk_file::fchmod): Deliberatly
set GROUP_OBJ and CLASS_OBJ perms to new group perms. Add comment
to explain why.
* security.cc (set_created_file_access): Ditto.
* sec_acl.cc (set_posix_access): Replace previous patch. Return
EINVAL if uid and/or guid is invalid and not backed by an actual
Windows account.
* sec_acl.cc (set_posix_access): Workaround owner/group SIDs being NULL.
* sec_acl.cc (set_posix_access): Handle files with owner == group.
Rephrase switch statement checking against unfiltered a_type value.
(get_posix_access): Handle files with owner == group.
* sec_acl.cc (get_posix_access): Don't use GROUP_OBJ access to fix up
CLASS_OBJ mask on old-style ACLs. Fix a comment.
* sec_acl.cc (set_posix_access): Always make sure Admins have
WRITE_DAC and WRITE_OWNER permissions.
* security.h (create_object_sd_from_attribute): Drop handle parameter
from prototype.
* security.cc (create_object_sd_from_attribute): Drop handle parameter.
Just create the standard POSIXy security descriptor.
(set_object_attribute): Accommodate dropped paramter in call to
create_object_sd_from_attribute.
* fhandler_tty.cc: Ditto, throughout.
* fhandler_disk_file.cc (fhandler_disk_file::fchmod): Fix typo in
mask computation.
* fhandler.cc (fhandler_base::open_with_arch): Call open with mode
not umasked.
(fhandler_base::open): Explicitely umask mode on NFS here. Call new
set_created_file_access rather than set_file_attribute.
* fhandler_disk_file.cc (fhandler_disk_file::fchmod): Reimplement
setting permissions on filesystems supporting ACLs using the new
set_posix_access call.
(fhandler_disk_file::fchown): Ditto.
(fhandler_disk_file::mkdir): Call new set_created_file_access rather
than set_file_attribute.
* fhandler_socket.cc (fhandler_socket::bind): Don't umask here. Add
WRITE_OWNER access to allow writing group in case of SGID bit set.
Call new set_created_file_access rather than set_file_attribute.
* path.cc (symlink_worker): Call new set_created_file_access rather
than set_file_attribute.
* sec_acl.cc (searchace): Un-staticize.
(set_posix_access): New, complementary functionality to
get_posix_access.
(setacl): Implement in terms of get_posix_access/set_posix_access.
(get_posix_access): Add handling for just created files requiring
their first Cygwin ACL. Fix new_style recognition. Handle SGID
bit. For old-style ACLs, ignore SYSTEM and Administrators when
computing the {DEF_}CLASS_OBJ perms.
* security.cc (get_file_sd): Revamp comment. Change and (hopefully)
speed up inheritance processing for just created files.
(alloc_sd): Remove.
(set_security_attribute): Call set_posix_access instead of alloc_sd.
(get_object_attribute): Fix return value.
(create_object_sd_from_attribute): Call set_posix_access instead of
alloc_sd.
(set_file_attribute): Remove.
(set_created_file_access): New function implemented in terms of
get_posix_access/set_posix_access.
* security.h (set_file_attribute): Remove prototype.
(set_created_file_access): Add prototype.
(searchace): Ditto.
(set_posix_access): Ditto.
* syscalls.cc (open): Call open_with_arch with mode not umasked.
* sec_acl.cc: Change preceeding comment explaining new-style ACLs.
Describe how to generate deny ACEs in more detail. Accommodate the
fact that a NULL deny ACE is used for {DEF_}CLASS_OBJ, rather than
a special Cygwin ACE. Improve further comments.
(CYG_ACE_NEW_STYLE): Define.
(get_posix_access): Change from Cygwin ACE to NULL deny ACE. Fix
CLASS_OBJ handling to generate CLASS_OBJ and DEF_CLASS_OBJ from a single
NULL deny ACE if the inheritance flags say so.
* sec_helper.cc (well_known_cygwin_sid): Remove.
* security.h (well_known_cygwin_sid): Drop declaration.
* sec_acl.cc (CYG_ACE_ISBITS_TO_WIN): Fix typo.
(get_posix_access): Rename index variable from i to idx. Define only
once at top level.
* security.cc (add_access_allowed_ace): Drop unused parameter "offset".
Accommodate throughout.
(add_access_denied_ace): Ditto.
* sec_acl.cc: Accommodate above change throughout.
* security.h (add_access_allowed_ace): Adjust prototype to above change.
(add_access_denied_ace): Ditto.
* sec_acl.cc (get_posix_access): Handle multiple ACEs for the
owner and primary group of the file. Handle the default primary
group ACE as DEF_GROUP_OBJ entry if the directory has the S_ISGID bit
set. Add comments. Minor code rearrangements.
Preliminary read side implementation of new permission handling.
* acl.h (MAX_ACL_ENTRIES): Raise to 2730. Add comment to explain.
* sec_acl.cc: Add leading comment to explain new ACL style.
Add definitions and macros to use for bits in new Cygwin ACL.
(DENY_RWX): New mask value for all temporary deny bits.
(getace): Add bool parameter to decide when leaving all bits intact,
rather than filtering them per the already set bits.
(get_posix_access): New function, taking over functionality to read
POSIX ACL from SECURITY_DESCRIPTOR.
(getacl): Just call get_posix_access.
* sec_helper.cc (well_known_cygwin_sid): Define.
* security.cc (get_attribute_from_acl): Remove.
(get_info_from_sd): Remove.
(get_reg_sd): Call get_posix_access instead of get_info_from_sd.
(get_file_attribute): Ditto.
(get_object_attribute): Ditto.
* security.h (well_known_cygwin_sid): Declare.
(get_posix_access): Add prototype.
* Throughout, use simpler ACE macros from Windows' accctrl.h.
* getfacl.c (main): Special-case SYSTEM and Admins group. Add comments.
* setfacl.c: Align more to Linux tool.
(delacl): New function to delete acl entries only.
(modacl): Drop delete functionality. Add handling of recomputing the
mask and default mask values.
(delallacl): Rename from delacl.
(setfacl): Call delacl in Delete case. Call delallacl in DeleteAll
and DeleteDef case.
(usage): Accommodate new options. Rearrange and rephrase slightly.
(longopts): Emit 'x' in --delete case. Add --no-mask and --mask
options.
(opts): Add -x and -n options.
(main): Handle -d and -x the same. Handle -n and --mask options.
Drop handling for -r option.
* getfacl.c (usage): Align more closely to Linux version. Add new
options -c, -e, -E. Change formatting to accommodate longer options.
(longopts): Rename --noname to --numeric. Keep --noname for backward
compatibility. Add --omit-header, --all-effective and --no-effective
options.
(opts): Add -c, -e and -E option.
(main): Handle new -c, -e, and -E options.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-06-24 17:52:29 +08:00
|
|
|
if (!create_object_sd_from_attribute (myself->uid, myself->gid,
|
2015-09-04 01:29:55 +08:00
|
|
|
S_IFCHR | S_IRUSR | S_IWUSR | S_IWGRP,
|
|
|
|
sd))
|
2010-04-20 03:52:43 +08:00
|
|
|
sa.lpSecurityDescriptor = (PSECURITY_DESCRIPTOR) sd;
|
2006-06-02 23:41:34 +08:00
|
|
|
|
2010-04-20 03:52:43 +08:00
|
|
|
/* Carefully check that the input_available_event didn't already exist.
|
|
|
|
This is a measure to make sure that the event security descriptor
|
|
|
|
isn't occupied by a malicious process. We must make sure that the
|
|
|
|
event's security descriptor is what we expect it to be. */
|
|
|
|
if (!(input_available_event = t.get_event (errstr = INPUT_AVAILABLE_EVENT,
|
|
|
|
&sa, TRUE))
|
|
|
|
|| GetLastError () == ERROR_ALREADY_EXISTS)
|
2006-06-02 23:41:34 +08:00
|
|
|
goto err;
|
|
|
|
|
2007-12-06 00:50:18 +08:00
|
|
|
char buf[MAX_PATH];
|
2011-10-16 06:37:30 +08:00
|
|
|
errstr = shared_name (buf, OUTPUT_MUTEX, unit);
|
2010-04-20 03:52:43 +08:00
|
|
|
if (!(output_mutex = CreateMutex (&sa, FALSE, buf)))
|
2006-06-02 23:41:34 +08:00
|
|
|
goto err;
|
|
|
|
|
2011-10-16 06:37:30 +08:00
|
|
|
errstr = shared_name (buf, INPUT_MUTEX, unit);
|
2010-04-20 03:52:43 +08:00
|
|
|
if (!(input_mutex = CreateMutex (&sa, FALSE, buf)))
|
2006-06-02 23:41:34 +08:00
|
|
|
goto err;
|
|
|
|
|
2010-09-28 18:13:19 +08:00
|
|
|
/* Create master control pipe which allows the master to duplicate
|
|
|
|
the pty pipe handles to processes which deserve it. */
|
2011-06-15 05:48:43 +08:00
|
|
|
__small_sprintf (buf, "\\\\.\\pipe\\cygwin-%S-pty%d-master-ctl",
|
2012-02-14 19:27:43 +08:00
|
|
|
&cygheap->installation_key, unit);
|
2013-04-23 17:44:36 +08:00
|
|
|
master_ctl = CreateNamedPipe (buf, PIPE_ACCESS_DUPLEX
|
|
|
|
| FILE_FLAG_FIRST_PIPE_INSTANCE,
|
2010-09-28 18:13:19 +08:00
|
|
|
PIPE_WAIT | PIPE_TYPE_MESSAGE
|
2013-04-23 17:44:36 +08:00
|
|
|
| PIPE_READMODE_MESSAGE
|
2015-12-15 23:02:35 +08:00
|
|
|
| PIPE_REJECT_REMOTE_CLIENTS,
|
2013-04-23 17:44:36 +08:00
|
|
|
1, 4096, 4096, 0, &sec_all_nih);
|
2010-09-28 18:13:19 +08:00
|
|
|
if (master_ctl == INVALID_HANDLE_VALUE)
|
2010-04-20 03:52:43 +08:00
|
|
|
{
|
2010-09-28 18:13:19 +08:00
|
|
|
errstr = "pty master control pipe";
|
|
|
|
goto err;
|
|
|
|
}
|
2011-12-04 05:43:27 +08:00
|
|
|
master_thread = new cygthread (::pty_master_thread, this, "ptym");
|
2010-09-28 18:13:19 +08:00
|
|
|
if (!master_thread)
|
|
|
|
{
|
|
|
|
errstr = "pty master control thread";
|
|
|
|
goto err;
|
2010-04-20 03:52:43 +08:00
|
|
|
}
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
master_fwd_thread = new cygthread (::pty_master_fwd_thread, this, "ptymf");
|
|
|
|
if (!master_fwd_thread)
|
|
|
|
{
|
|
|
|
errstr = "pty master forwarding thread";
|
|
|
|
goto err;
|
|
|
|
}
|
2020-01-21 10:22:02 +08:00
|
|
|
get_ttyp ()->fwd_done = CreateEvent (&sec_none, true, false, NULL);
|
2010-04-20 03:52:43 +08:00
|
|
|
|
2020-02-11 01:45:14 +08:00
|
|
|
t.winsize.ws_col = 80;
|
|
|
|
t.winsize.ws_row = 25;
|
|
|
|
|
2019-08-28 02:04:02 +08:00
|
|
|
setup_pseudoconsole ();
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
t.set_from_master (from_master);
|
2019-08-28 02:04:02 +08:00
|
|
|
t.set_from_master_cyg (from_master_cyg);
|
2013-04-23 17:44:36 +08:00
|
|
|
t.set_to_master (to_master);
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
t.set_to_master_cyg (to_master_cyg);
|
2006-06-02 23:41:34 +08:00
|
|
|
t.master_pid = myself->pid;
|
|
|
|
|
2011-10-20 22:02:54 +08:00
|
|
|
dev ().parse (DEV_PTYM_MAJOR, unit);
|
2011-10-16 06:37:30 +08:00
|
|
|
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
termios_printf ("this %p, pty%d opened - from_pty <%p,%p>, to_pty %p",
|
2019-08-28 02:04:02 +08:00
|
|
|
this, unit, from_slave, get_handle (),
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
get_output_handle ());
|
2006-06-02 23:41:34 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
err:
|
|
|
|
__seterrno ();
|
2019-08-28 02:04:02 +08:00
|
|
|
close_maybe (from_slave);
|
2019-03-30 15:12:02 +08:00
|
|
|
close_maybe (get_handle ());
|
2006-06-02 23:41:34 +08:00
|
|
|
close_maybe (get_output_handle ());
|
|
|
|
close_maybe (input_available_event);
|
|
|
|
close_maybe (output_mutex);
|
|
|
|
close_maybe (input_mutex);
|
2006-06-03 14:35:10 +08:00
|
|
|
close_maybe (from_master);
|
2019-08-28 02:04:02 +08:00
|
|
|
close_maybe (from_master_cyg);
|
2006-06-03 14:35:10 +08:00
|
|
|
close_maybe (to_master);
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
close_maybe (to_master_cyg);
|
2015-03-05 20:57:34 +08:00
|
|
|
close_maybe (echo_r);
|
|
|
|
close_maybe (echo_w);
|
2010-04-20 03:52:43 +08:00
|
|
|
close_maybe (master_ctl);
|
2011-10-16 06:37:30 +08:00
|
|
|
termios_printf ("pty%d open failed - failed to create %s", unit, errstr);
|
2006-06-02 23:41:34 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_pty_master::fixup_after_fork (HANDLE parent)
|
|
|
|
{
|
|
|
|
DWORD wpid = GetCurrentProcessId ();
|
2011-06-15 05:48:43 +08:00
|
|
|
fhandler_pty_master *arch = (fhandler_pty_master *) archetype;
|
2006-06-02 23:41:34 +08:00
|
|
|
if (arch->dwProcessId != wpid)
|
|
|
|
{
|
|
|
|
tty& t = *get_ttyp ();
|
|
|
|
if (myself->pid == t.master_pid)
|
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
t.set_from_master (arch->from_master);
|
2019-08-28 02:04:02 +08:00
|
|
|
t.set_from_master_cyg (arch->from_master_cyg);
|
2013-04-23 17:44:36 +08:00
|
|
|
t.set_to_master (arch->to_master);
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
t.set_to_master_cyg (arch->to_master_cyg);
|
2006-06-02 23:41:34 +08:00
|
|
|
}
|
|
|
|
arch->dwProcessId = wpid;
|
|
|
|
}
|
2006-06-03 15:17:53 +08:00
|
|
|
from_master = arch->from_master;
|
2019-08-28 02:04:02 +08:00
|
|
|
from_master_cyg = arch->from_master_cyg;
|
2006-06-03 15:17:53 +08:00
|
|
|
to_master = arch->to_master;
|
Fix OPOST for non-Cygwin pty slaves
* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg. This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch->to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-04-22 19:22:59 +08:00
|
|
|
to_master_cyg = arch->to_master_cyg;
|
2019-08-28 02:04:02 +08:00
|
|
|
#if 0 /* Not sure if this is necessary. */
|
|
|
|
from_slave = arch->from_slave;
|
|
|
|
to_slave = arch->to_slave;
|
|
|
|
#endif
|
2006-06-02 23:41:34 +08:00
|
|
|
report_tty_counts (this, "inherited master", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_pty_master::fixup_after_exec ()
|
|
|
|
{
|
2006-06-03 14:35:10 +08:00
|
|
|
if (!close_on_exec ())
|
2006-06-02 23:41:34 +08:00
|
|
|
fixup_after_fork (spawn_info->parent);
|
2006-06-03 15:17:53 +08:00
|
|
|
else
|
2019-08-28 02:04:02 +08:00
|
|
|
from_master = from_master_cyg = to_master = to_master_cyg =
|
|
|
|
from_slave = to_slave = NULL;
|
2006-06-02 23:41:34 +08:00
|
|
|
}
|
2015-03-25 19:42:38 +08:00
|
|
|
|
|
|
|
BOOL
|
|
|
|
fhandler_pty_common::process_opost_output (HANDLE h, const void *ptr, ssize_t& len, bool is_echo)
|
|
|
|
{
|
|
|
|
ssize_t towrite = len;
|
|
|
|
BOOL res = TRUE;
|
2019-08-28 02:04:02 +08:00
|
|
|
BOOL (WINAPI *WriteFunc)
|
|
|
|
(HANDLE, LPCVOID, DWORD, LPDWORD, LPOVERLAPPED);
|
|
|
|
WriteFunc = WriteFile_Orig ? WriteFile_Orig : WriteFile;
|
2015-03-25 19:42:38 +08:00
|
|
|
while (towrite)
|
|
|
|
{
|
|
|
|
if (!is_echo)
|
|
|
|
{
|
|
|
|
if (tc ()->output_stopped && is_nonblocking ())
|
|
|
|
{
|
|
|
|
if (towrite < len)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
{
|
2019-09-15 12:36:23 +08:00
|
|
|
set_errno (EAGAIN);
|
2015-03-25 19:42:38 +08:00
|
|
|
len = -1;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (tc ()->output_stopped)
|
|
|
|
cygwait (10);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(get_ttyp ()->ti.c_oflag & OPOST)) // raw output mode
|
|
|
|
{
|
|
|
|
DWORD n = MIN (OUT_BUFFER_SIZE, towrite);
|
2019-08-28 02:04:02 +08:00
|
|
|
res = WriteFunc (h, ptr, n, &n, NULL);
|
2015-03-25 19:42:38 +08:00
|
|
|
if (!res)
|
|
|
|
break;
|
|
|
|
ptr = (char *) ptr + n;
|
|
|
|
towrite -= n;
|
|
|
|
}
|
|
|
|
else // post-process output
|
|
|
|
{
|
|
|
|
char outbuf[OUT_BUFFER_SIZE + 1];
|
|
|
|
char *buf = (char *)ptr;
|
|
|
|
DWORD n = 0;
|
|
|
|
ssize_t rc = 0;
|
|
|
|
while (n < OUT_BUFFER_SIZE && rc < towrite)
|
|
|
|
{
|
|
|
|
switch (buf[rc])
|
|
|
|
{
|
|
|
|
case '\r':
|
|
|
|
if ((get_ttyp ()->ti.c_oflag & ONOCR)
|
|
|
|
&& get_ttyp ()->column == 0)
|
|
|
|
{
|
|
|
|
rc++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (get_ttyp ()->ti.c_oflag & OCRNL)
|
|
|
|
{
|
|
|
|
outbuf[n++] = '\n';
|
|
|
|
rc++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
outbuf[n++] = buf[rc++];
|
|
|
|
get_ttyp ()->column = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
if (get_ttyp ()->ti.c_oflag & ONLCR)
|
|
|
|
{
|
|
|
|
outbuf[n++] = '\r';
|
|
|
|
get_ttyp ()->column = 0;
|
|
|
|
}
|
|
|
|
if (get_ttyp ()->ti.c_oflag & ONLRET)
|
|
|
|
get_ttyp ()->column = 0;
|
|
|
|
outbuf[n++] = buf[rc++];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
outbuf[n++] = buf[rc++];
|
|
|
|
get_ttyp ()->column++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 02:04:02 +08:00
|
|
|
res = WriteFunc (h, outbuf, n, &n, NULL);
|
2015-03-25 19:42:38 +08:00
|
|
|
if (!res)
|
|
|
|
break;
|
|
|
|
ptr = (char *) ptr + rc;
|
|
|
|
towrite -= rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
len -= towrite;
|
|
|
|
return res;
|
|
|
|
}
|