mirror of
git://sourceware.org/git/newlib-cygwin.git
synced 2025-01-29 02:20:21 +08:00
23771fa1f7
logs easier. Throughout, change /dev/tty* to /dev/pty*. Throughout, add flags argument to fhandler_*::dup methods. * devices.in: Rename (temporarily?) /dev/ttyN to /dev/ptyN. Add /dev/ptymN devices for pty masters. * devices.cc: Regenerate. * devices.h (MAX_CONSOLES): Set to max number supported by devices.in. (fh_devices::FH_PTMX): Rename from FH_PTYM. (device::operator int): Return by reference. * dtable.cc (fh_alloc): Take pc as an argument rather than just the device. This makes debugging easier since more information is available. Actually implement handling for already-allocated pty master devices. Make different decisions when generating fhandler for not-opened devices. Add kludge to deal with opening /dev/tty. (cnew_no_ctor): New macro. (build_fh_pc): Make debugging output more verbose. Use new clone() fhandler interface to duplicate archetypes. Reset last term opened. (dtable::dup_worker): Use Use new clone() fhandler interface to duplicate archetypes. Pass flags to child dup handler. (dtable::dup3): Set O_NOCTTY flag if newfd is not stdin/stdout/stderr. * fhandler.cc (fhandler_base::reset): Rename from operator =() and reduce functionality and sense of copy direction. (fhandler_base::open_with_arch): Use published interface to query io_handle(). Use new copyto() fhandler method to copy from/to found archetype. * fhandler.h: Throughout, delete size(), add copyout, clone, and fhandler_* (void *) methods. (fhandler_base::reset): Rename from operator =(). (fhandler_termios::is_dev_tty): Delete. (fhandler_termios): change "protected" region to "private". (fhandler_termios::is_dev_tty): Delete. (fhandler_termios): Rearrange protected/public. (fhandler_termios::fhandler_termios): Remember last fhandler_termios "opened". (fhandler_termios::~fhandler_termios): Forget last fhandler_termios opened. (ioctl): Rename from ioctl_termios. Take a void * argument. Reflect argument change in pinfo::set_ctty. (fhandler_console::dup): Declare new function. Set ctty here if appropriate. (fhandler_pty_master::from_master): Privatize. (fhandler_pty_master::to_master): Ditto. (fhandler_pty_master::dwProcessId): Ditto. (fhandler_pty_master::fhandler_pty_master): Add an `int' argument. (fhandler_pty_master::open_setup): Declare new function. (fhandler_pty_master::~fhandler_pty_master): Declare new method. (fhandler_nodevice): Remove commented out function declaration. * fhandler_console.cc: Use get_ttyp() instead of tc() throughout. (fhandler_console::dup): Define new function to set controlling ctty on dup, as appropriate. (fhandler_console::ioctl): Reflect ioctl_termios name change. (fhandler_console::setup): Rename from get_tty_stuff. (fhandler_console::open_setup): Reflect argument change in pinfo::set_ctty. (fhandler_console::fhandler_console): Set _tc here. * fhandler_termios.cc (handler_termios::ioctl): Rename. Take a void * arg like other ioctl functions. * fhandler_tty.cc (fhandler_pty_slave::dup): Call myself->set_ctty to potentially reset the controlling terminal. (fhandler_pty_slave::ioctl): Reflect name/arg change for ioctl_termios. (fhandler_pty_slave::fhandler_pty_slave): Take a "unit" argument. Call setup() here so that we will know the unit number of this fhandler as soon as possible. Set the unit as appropriate. (handler_pty_master::open): Move most stuff to constructor and open_setup. (handler_pty_slave::open_setup): Reflect argument change in pinfo::set_ctty. (handler_pty_master::open_setup): Define new function. (fhandler_pty_master::cleanup): Clear handles as a flag that the destructor does not have to do "close" operations. (fhandler_pty_master::close): Ditto. (fhandler_pty_master::~fhandler_pty_master): Define new method. (fhandler_pty_master::ioctl): Reflect name/arg change for ioctl_termios. (fhandler_pty_master::setup): Allocate tty here. Rely on handles being returned from allocated test rather than opening them here. Avoid setting _need_nl here since it is already zeroed in the constructor. Set up device information with DEV_TTYM_MAJOR. * path.h (path_conv &operator =): Take a const argument. (path_conv::dup): Ditto. (pathconv_arg::PC_OPEN): New enum. (pathconv_arg::PC_CTTY): Ditto. (path_types::PATH_CTTY): Ditto. (path_types::PATH_OPEN): Ditto. (path_conv::isopen): New method. (path_conv::isctty_capable): Ditto. * path.cc (path_conv::check): Set PATH_OPEN and PATH_CTTY as appropriate. * pipe.cc (fhandler_pipe::open): Use copyto to copy pipe handle. * syscall.cc (open): Reinstate fd > 2 check to disallow resetting ctty on non-std* handles. * tty.cc (tty_list::allocate): Pass out handles for allocated tty. use `not_allocated' to find unallocated ttys. Avoid keeping the lock since the allocation of the tty should be sufficient to prevent multiple access. (tty::not_allocated): Clarify comment. Rename. Return handles when an unused tty is found. Simply test for existing tty. (tty::exists): Rewrite to use `not_allocated'. * tty.h (NTTYS): Reset down to actual number supported by devices.in. (tty::not_allocated): Declare new function. (tty_list::allocate): Pass out read/write tty handles. Zero them when not found. * fhandler_proc.cc: Reflect name change from FH_PTYM -> FH_PTMX. * pinfo.h (pinfo::set_ctty): Reduce/reorder arguments passed in. * pinfo.cc (pinfo::set_ctty): Ditto. Just use tc() built into the passed-in fhandler_termios pointer. Return true if ctty is assigned. * syscalls.cc (open): Call build_fh_pc with PC_OPEN flag. Set PC_CTTY if appropriate. (stat_worker): Remove is_dev_tty () stuff.
334 lines
7.6 KiB
C++
334 lines
7.6 KiB
C++
/* fhandler_fifo.cc - See fhandler.h for a description of the fhandler classes.
|
|
|
|
Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
|
|
Red Hat, Inc.
|
|
|
|
This file is part of Cygwin.
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
details. */
|
|
|
|
#include "winsup.h"
|
|
#include "miscfuncs.h"
|
|
|
|
#include "cygerrno.h"
|
|
#include "security.h"
|
|
#include "path.h"
|
|
#include "fhandler.h"
|
|
#include "dtable.h"
|
|
#include "cygheap.h"
|
|
#include "sigproc.h"
|
|
#include "cygtls.h"
|
|
#include "shared_info.h"
|
|
#include "ntdll.h"
|
|
|
|
fhandler_fifo::fhandler_fifo ():
|
|
fhandler_base_overlapped (), wait_state (fifo_unknown), dummy_client (NULL)
|
|
{
|
|
max_atomic_write = DEFAULT_PIPEBUFSIZE;
|
|
need_fork_fixup (true);
|
|
}
|
|
|
|
HANDLE
|
|
fhandler_fifo::open_nonserver (const char *npname, unsigned low_flags,
|
|
LPSECURITY_ATTRIBUTES sa_buf)
|
|
{
|
|
DWORD mode = 0;
|
|
if (low_flags == O_RDONLY)
|
|
mode = GENERIC_READ;
|
|
else if (low_flags == O_WRONLY)
|
|
mode = GENERIC_WRITE;
|
|
else
|
|
mode = GENERIC_READ | GENERIC_WRITE;
|
|
while (1)
|
|
{
|
|
HANDLE h = CreateFile (npname, mode, 0, sa_buf, OPEN_EXISTING,
|
|
FILE_FLAG_OVERLAPPED, NULL);
|
|
if (h != INVALID_HANDLE_VALUE || GetLastError () != ERROR_PIPE_NOT_CONNECTED)
|
|
return h;
|
|
if (IsEventSignalled (signal_arrived))
|
|
{
|
|
set_errno (EINTR);
|
|
return NULL;
|
|
}
|
|
}
|
|
}
|
|
|
|
char *
|
|
fhandler_fifo::fifo_name (char *buf)
|
|
{
|
|
/* Generate a semi-unique name to associate with this fifo. */
|
|
__small_sprintf (buf, "\\\\.\\pipe\\__cygfifo__%S_%08x_%016X",
|
|
&installation_key, get_dev (), get_ino ());
|
|
return buf;
|
|
}
|
|
|
|
#define FIFO_PIPE_MODE (PIPE_TYPE_BYTE | PIPE_READMODE_BYTE)
|
|
#define FIFO_BUF_SIZE 4096
|
|
#define cnp(m, s) CreateNamedPipe(npname, (m), FIFO_PIPE_MODE, \
|
|
PIPE_UNLIMITED_INSTANCES, (s), (s), \
|
|
NMPWAIT_WAIT_FOREVER, sa_buf)
|
|
|
|
inline PSECURITY_ATTRIBUTES
|
|
sec_user_cloexec (bool cloexec, PSECURITY_ATTRIBUTES sa, PSID sid)
|
|
{
|
|
return cloexec ? sec_user_nih (sa, sid) : sec_user (sa, sid);
|
|
}
|
|
|
|
int
|
|
fhandler_fifo::open (int flags, mode_t)
|
|
{
|
|
int res = 1;
|
|
char npname[MAX_PATH];
|
|
|
|
fifo_name (npname);
|
|
unsigned low_flags = flags & O_ACCMODE;
|
|
DWORD mode = 0;
|
|
if (low_flags == O_WRONLY)
|
|
mode = PIPE_ACCESS_OUTBOUND;
|
|
else if (low_flags == O_RDONLY || low_flags == O_RDWR)
|
|
mode = PIPE_ACCESS_DUPLEX;
|
|
else
|
|
{
|
|
set_errno (EINVAL);
|
|
res = 0;
|
|
}
|
|
|
|
if (res)
|
|
{
|
|
char char_sa_buf[1024];
|
|
LPSECURITY_ATTRIBUTES sa_buf =
|
|
sec_user_cloexec (flags & O_CLOEXEC, (PSECURITY_ATTRIBUTES) char_sa_buf,
|
|
cygheap->user.sid());
|
|
bool do_seterrno = true;
|
|
|
|
HANDLE h;
|
|
bool nonblocking_write = !!((flags & (O_WRONLY | O_NONBLOCK)) == (O_WRONLY | O_NONBLOCK));
|
|
wait_state = fifo_unknown;
|
|
if (mode != PIPE_ACCESS_OUTBOUND)
|
|
{
|
|
h = cnp (mode | FILE_FLAG_OVERLAPPED, FIFO_BUF_SIZE);
|
|
wait_state = fifo_wait_for_client;
|
|
}
|
|
else
|
|
{
|
|
h = open_nonserver (npname, low_flags, sa_buf);
|
|
if (h != INVALID_HANDLE_VALUE)
|
|
wait_state = fifo_ok;
|
|
else if (nonblocking_write)
|
|
{
|
|
set_errno (ENXIO);
|
|
do_seterrno = false;
|
|
}
|
|
else if ((h = cnp (PIPE_ACCESS_DUPLEX, 1)) != INVALID_HANDLE_VALUE)
|
|
{
|
|
if ((dummy_client = open_nonserver (npname, low_flags, sa_buf))
|
|
!= INVALID_HANDLE_VALUE)
|
|
{
|
|
wait_state = fifo_wait_for_server;
|
|
ProtectHandle (dummy_client);
|
|
}
|
|
else
|
|
{
|
|
DWORD saveerr = GetLastError ();
|
|
CloseHandle (h);
|
|
h = INVALID_HANDLE_VALUE;
|
|
SetLastError (saveerr);
|
|
}
|
|
}
|
|
}
|
|
if (h == INVALID_HANDLE_VALUE)
|
|
{
|
|
if (do_seterrno)
|
|
__seterrno ();
|
|
res = 0;
|
|
}
|
|
else if (setup_overlapped ())
|
|
{
|
|
CloseHandle (h);
|
|
__seterrno ();
|
|
res = 0;
|
|
}
|
|
else
|
|
{
|
|
set_io_handle (h);
|
|
set_flags (flags);
|
|
res = 1;
|
|
}
|
|
}
|
|
|
|
debug_printf ("returning %d, errno %d", res, get_errno ());
|
|
return res;
|
|
}
|
|
|
|
bool
|
|
fhandler_fifo::wait (bool iswrite)
|
|
{
|
|
DWORD ninstances;
|
|
switch (wait_state)
|
|
{
|
|
case fifo_wait_for_next_client:
|
|
DisconnectNamedPipe (get_handle ());
|
|
if (!GetNamedPipeHandleState (get_handle (), NULL, &ninstances, NULL, NULL, NULL, 0))
|
|
{
|
|
__seterrno ();
|
|
wait_state = fifo_error;
|
|
return false;
|
|
}
|
|
if (ninstances <= 1)
|
|
{
|
|
wait_state = fifo_eof;
|
|
return false;
|
|
}
|
|
case fifo_wait_for_client:
|
|
{
|
|
DWORD dummy_bytes;
|
|
while (1)
|
|
{
|
|
int res = ConnectNamedPipe (get_handle (), get_overlapped ());
|
|
if (GetLastError () != ERROR_NO_DATA && GetLastError () != ERROR_PIPE_CONNECTED)
|
|
{
|
|
res = wait_overlapped (res, iswrite, &dummy_bytes, false);
|
|
if (!res)
|
|
{
|
|
if (get_errno () != EINTR)
|
|
wait_state = fifo_error;
|
|
else if (!_my_tls.call_signal_handler ())
|
|
wait_state = fifo_eintr;
|
|
else
|
|
continue;
|
|
return false;
|
|
}
|
|
}
|
|
wait_state = fifo_ok;
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
case fifo_wait_for_server:
|
|
char npname[MAX_PATH];
|
|
fifo_name (npname);
|
|
char char_sa_buf[1024];
|
|
LPSECURITY_ATTRIBUTES sa_buf;
|
|
sa_buf = sec_user_cloexec (close_on_exec (),
|
|
(PSECURITY_ATTRIBUTES) char_sa_buf,
|
|
cygheap->user.sid());
|
|
while (1)
|
|
{
|
|
if (WaitNamedPipe (npname, 10))
|
|
/* connected, maybe */;
|
|
else if (GetLastError () != ERROR_SEM_TIMEOUT)
|
|
{
|
|
__seterrno ();
|
|
return false;
|
|
}
|
|
else if (!IsEventSignalled (signal_arrived))
|
|
continue;
|
|
else if (_my_tls.call_signal_handler ())
|
|
continue;
|
|
else
|
|
{
|
|
set_errno (EINTR);
|
|
return false;
|
|
}
|
|
HANDLE h = open_nonserver (npname, get_flags () & O_ACCMODE, sa_buf);
|
|
if (h != INVALID_HANDLE_VALUE)
|
|
{
|
|
ForceCloseHandle (get_handle ());
|
|
ForceCloseHandle (dummy_client);
|
|
dummy_client = NULL;
|
|
wait_state = fifo_ok;
|
|
set_io_handle (h);
|
|
break;
|
|
}
|
|
if (GetLastError () == ERROR_PIPE_LISTENING)
|
|
continue;
|
|
else
|
|
{
|
|
__seterrno ();
|
|
return false;
|
|
}
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void __stdcall
|
|
fhandler_fifo::raw_read (void *in_ptr, size_t& len)
|
|
{
|
|
while (wait_state != fifo_eof && wait_state != fifo_error && wait_state != fifo_eintr)
|
|
if (!wait (false))
|
|
len = (wait_state == fifo_error || wait_state == fifo_eintr) ? (size_t) -1 : 0;
|
|
else
|
|
{
|
|
size_t prev_len = len;
|
|
fhandler_base_overlapped::raw_read (in_ptr, len);
|
|
if (len)
|
|
break;
|
|
wait_state = fifo_wait_for_next_client;
|
|
len = prev_len;
|
|
}
|
|
if (wait_state == fifo_eintr)
|
|
wait_state = fifo_wait_for_client;
|
|
debug_printf ("returning %d, mode %d, %E\n", len, get_errno ());
|
|
}
|
|
|
|
ssize_t __stdcall
|
|
fhandler_fifo::raw_write (const void *ptr, size_t len)
|
|
{
|
|
return wait (true) ? fhandler_base_overlapped::raw_write (ptr, len) : -1;
|
|
}
|
|
|
|
int __stdcall
|
|
fhandler_fifo::fstatvfs (struct statvfs *sfs)
|
|
{
|
|
fhandler_disk_file fh (pc);
|
|
fh.get_device () = FH_FS;
|
|
return fh.fstatvfs (sfs);
|
|
}
|
|
|
|
int
|
|
fhandler_fifo::close ()
|
|
{
|
|
wait_state = fifo_eof;
|
|
if (dummy_client)
|
|
{
|
|
ForceCloseHandle (dummy_client);
|
|
dummy_client = NULL;
|
|
}
|
|
return fhandler_base::close ();
|
|
}
|
|
|
|
int
|
|
fhandler_fifo::dup (fhandler_base *child, int flags)
|
|
{
|
|
int res = fhandler_base_overlapped::dup (child, flags);
|
|
fhandler_fifo *fifo_child = (fhandler_fifo *) child;
|
|
if (res == 0 && dummy_client)
|
|
{
|
|
bool dres = DuplicateHandle (GetCurrentProcess (), dummy_client,
|
|
GetCurrentProcess (),
|
|
&fifo_child->dummy_client, 0,
|
|
TRUE, DUPLICATE_SAME_ACCESS);
|
|
if (!dres)
|
|
{
|
|
fifo_child->dummy_client = NULL;
|
|
child->close ();
|
|
__seterrno ();
|
|
res = -1;
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
|
|
void
|
|
fhandler_fifo::set_close_on_exec (bool val)
|
|
{
|
|
fhandler_base::set_close_on_exec (val);
|
|
if (dummy_client)
|
|
set_no_inheritance (dummy_client, val);
|
|
}
|