2000-02-18 03:38:33 +08:00
|
|
|
/* fhandler_tty.cc
|
|
|
|
|
2001-03-11 09:58:23 +08:00
|
|
|
Copyright 1997, 1998, 2000, 2001 Red Hat, Inc.
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
2000-08-03 00:28:18 +08:00
|
|
|
#include "winsup.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <limits.h>
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "cygerrno.h"
|
2001-07-27 03:22:24 +08:00
|
|
|
#include "security.h"
|
2000-08-22 13:10:20 +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 "sync.h"
|
|
|
|
#include "sigproc.h"
|
2000-08-12 13:47:12 +08:00
|
|
|
#include "pinfo.h"
|
2000-09-03 12:16:35 +08:00
|
|
|
#include "cygheap.h"
|
2000-09-08 00:23:51 +08:00
|
|
|
#include "shared_info.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* Tty master stuff */
|
|
|
|
|
|
|
|
fhandler_tty_master NO_COPY *tty_master;
|
|
|
|
|
|
|
|
static DWORD WINAPI process_input (void *); // Input queue thread
|
|
|
|
static DWORD WINAPI process_output (void *); // Output queue thread
|
|
|
|
static DWORD WINAPI process_ioctl (void *); // Ioctl requests thread
|
|
|
|
|
|
|
|
fhandler_tty_master::fhandler_tty_master (const char *name, int unit) :
|
|
|
|
fhandler_pty_master (name, FH_TTYM, unit)
|
|
|
|
{
|
|
|
|
set_cb (sizeof *this);
|
|
|
|
console = NULL;
|
|
|
|
hThread = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_tty_master::init (int ntty)
|
|
|
|
{
|
|
|
|
HANDLE h;
|
|
|
|
termios_printf ("Creating master for tty%d", ntty);
|
|
|
|
|
|
|
|
if (init_console ())
|
|
|
|
{
|
|
|
|
termios_printf ("can't create fhandler");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
termios ti;
|
|
|
|
memset (&ti, 0, sizeof (ti));
|
|
|
|
console->tcsetattr (0, &ti);
|
|
|
|
|
|
|
|
ttynum = ntty;
|
|
|
|
|
|
|
|
cygwin_shared->tty[ttynum]->common_init (this);
|
|
|
|
|
2001-09-01 13:38:46 +08:00
|
|
|
inuse = get_ttyp ()->create_inuse (TTY_MASTER_ALIVE);
|
2001-03-20 02:27:37 +08:00
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
h = makethread (process_input, NULL, 0, "ttyin");
|
|
|
|
if (h == NULL)
|
|
|
|
{
|
|
|
|
termios_printf ("can't create input thread");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetThreadPriority (h, THREAD_PRIORITY_HIGHEST);
|
|
|
|
CloseHandle (h);
|
|
|
|
}
|
|
|
|
|
|
|
|
h = makethread (process_ioctl, NULL, 0, "ttyioctl");
|
|
|
|
if (h == NULL)
|
|
|
|
{
|
|
|
|
termios_printf ("can't create ioctl thread");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetThreadPriority (h, THREAD_PRIORITY_HIGHEST);
|
|
|
|
CloseHandle (h);
|
|
|
|
}
|
|
|
|
|
|
|
|
hThread = makethread (process_output, NULL, 0, "ttyout");
|
|
|
|
if (hThread != NULL)
|
|
|
|
SetThreadPriority (hThread, THREAD_PRIORITY_HIGHEST);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
termios_printf ("can't create output thread");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#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*/
|
|
|
|
|
|
|
|
DWORD
|
|
|
|
fhandler_tty_common::__acquire_output_mutex (const char *fn, int ln,
|
|
|
|
DWORD ms)
|
|
|
|
{
|
2000-03-07 13:33:09 +08:00
|
|
|
if (strace.active)
|
2000-08-03 03:26:01 +08:00
|
|
|
strace.prntf (_STRACE_TERMIOS, fn, "(%d): tty output_mutex: waiting %d ms", ln, ms);
|
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
|
|
|
|
if (strace.active)
|
2000-08-03 03:26:01 +08:00
|
|
|
strace.prntf (_STRACE_TERMIOS, fn, "(%d): tty 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;
|
|
|
|
ostack[osi].tname = threadname (0, 0);
|
|
|
|
termios_printf ("acquired for %s:%d, osi %d", fn, ln, osi);
|
|
|
|
osi++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_tty_common::__release_output_mutex (const char *fn, int ln)
|
|
|
|
{
|
|
|
|
if (ReleaseMutex (output_mutex))
|
|
|
|
{
|
2000-03-10 05:04:05 +08:00
|
|
|
#ifndef DEBUGGING
|
|
|
|
if (strace.active)
|
2000-08-03 03:26:01 +08:00
|
|
|
strace.prntf (_STRACE_TERMIOS, fn, "(%d): tty output_mutex released", ln);
|
2000-03-10 05:04:05 +08:00
|
|
|
#else
|
2000-02-18 03:38:33 +08:00
|
|
|
if (osi > 0)
|
|
|
|
osi--;
|
|
|
|
termios_printf ("released at %s:%d, osi %d", 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process tty input. */
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_pty_master::doecho (const void *str, DWORD len)
|
|
|
|
{
|
|
|
|
acquire_output_mutex (INFINITE);
|
2000-03-12 12:44:37 +08:00
|
|
|
if (!WriteFile (get_ttyp ()->to_master, str, len, &len, NULL))
|
|
|
|
termios_printf ("Write to %p failed, %E", get_ttyp ()->to_master);
|
2000-02-18 03:38:33 +08:00
|
|
|
// WaitForSingleObject (output_done_event, INFINITE);
|
|
|
|
release_output_mutex ();
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_pty_master::accept_input ()
|
|
|
|
{
|
2001-04-27 14:27:28 +08:00
|
|
|
DWORD bytes_left, written;
|
2000-02-18 03:38:33 +08:00
|
|
|
DWORD n;
|
2001-03-04 23:34:25 +08:00
|
|
|
DWORD rc;
|
2001-04-27 14:27:28 +08:00
|
|
|
char* p;
|
2001-03-04 23:34:25 +08:00
|
|
|
|
|
|
|
rc = WaitForSingleObject (input_mutex, INFINITE);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-04-27 14:27:28 +08:00
|
|
|
bytes_left = n = eat_readahead (-1);
|
|
|
|
get_ttyp ()->read_retval = 0;
|
|
|
|
p = rabuf;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-03-11 09:58:23 +08:00
|
|
|
if (n != 0)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2001-04-27 14:27:28 +08:00
|
|
|
while (bytes_left > 0)
|
|
|
|
{
|
|
|
|
termios_printf ("about to write %d chars to slave", bytes_left);
|
|
|
|
rc = WriteFile (get_output_handle (), p, bytes_left, &written, NULL);
|
|
|
|
if (!rc)
|
|
|
|
{
|
|
|
|
debug_printf ("error writing to pipe %E");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
get_ttyp ()->read_retval += written;
|
|
|
|
p += written;
|
|
|
|
bytes_left -= written;
|
|
|
|
if (bytes_left > 0)
|
|
|
|
{
|
|
|
|
debug_printf ("to_slave pipe is full");
|
|
|
|
SetEvent (input_available_event);
|
|
|
|
ReleaseMutex (input_mutex);
|
|
|
|
Sleep (10);
|
|
|
|
rc = WaitForSingleObject (input_mutex, INFINITE);
|
|
|
|
}
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2001-03-04 23:34:25 +08:00
|
|
|
else
|
|
|
|
termios_printf ("sending EOF to slave");
|
|
|
|
SetEvent (input_available_event);
|
|
|
|
ReleaseMutex (input_mutex);
|
2000-02-18 03:38:33 +08:00
|
|
|
return get_ttyp ()->read_retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DWORD WINAPI
|
2000-02-21 13:20:38 +08:00
|
|
|
process_input (void *)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
char rawbuf[INP_BUFFER_SIZE];
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int nraw = tty_master->console->read ((void *) rawbuf,
|
|
|
|
(size_t) INP_BUFFER_SIZE);
|
2001-03-05 14:28:25 +08:00
|
|
|
(void) tty_master->line_edit (rawbuf, nraw);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process tty output requests */
|
|
|
|
|
|
|
|
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;
|
2000-03-12 14:29:54 +08:00
|
|
|
char outbuf[OUT_BUFFER_SIZE + 1];
|
2000-02-18 03:38:33 +08:00
|
|
|
DWORD n;
|
|
|
|
int column = 0;
|
2000-03-12 12:44:37 +08:00
|
|
|
int rc = 0;
|
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
|
|
|
if (need_nl)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-03-12 14:29:54 +08:00
|
|
|
/* We need to return a left over \n character, resulting from
|
|
|
|
\r\n conversion. Note that we already checked for FLUSHO and
|
2001-04-29 07:48:28 +08:00
|
|
|
output_stopped at the time that we read the character, so we
|
2000-03-12 14:29:54 +08:00
|
|
|
don't check again here. */
|
|
|
|
buf[0] = '\n';
|
|
|
|
need_nl = 0;
|
2000-08-03 21:02:09 +08:00
|
|
|
rc = 1;
|
2000-03-12 14:29:54 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-03-12 14:29:54 +08:00
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
2000-03-12 12:44:37 +08:00
|
|
|
/* Set RLEN to the number of bytes to read from the pipe. */
|
|
|
|
rlen = len;
|
|
|
|
if (get_ttyp ()->ti.c_oflag & OPOST && get_ttyp ()->ti.c_oflag & ONLCR)
|
|
|
|
{
|
|
|
|
/* We are going to expand \n to \r\n, so don't read more than
|
|
|
|
half of the number of bytes requested. */
|
|
|
|
rlen /= 2;
|
|
|
|
if (rlen == 0)
|
|
|
|
rlen = 1;
|
|
|
|
}
|
|
|
|
if (rlen > sizeof outbuf)
|
|
|
|
rlen = sizeof outbuf;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-03-12 12:44:37 +08:00
|
|
|
HANDLE handle = get_io_handle ();
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-03-12 14:29:54 +08:00
|
|
|
n = 0; // get_readahead_into_buffer (outbuf, len);
|
|
|
|
if (!n)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-03-12 14:29:54 +08:00
|
|
|
/* Doing a busy wait like this is quite inefficient, but nothing
|
|
|
|
else seems to work completely. Windows should provide some sort
|
|
|
|
of overlapped I/O for pipes, or something, but it doesn't. */
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (!PeekNamedPipe (handle, NULL, 0, NULL, &n, NULL))
|
|
|
|
goto err;
|
|
|
|
if (n > 0)
|
|
|
|
break;
|
|
|
|
if (hit_eof ())
|
|
|
|
goto out;
|
2001-08-15 15:49:15 +08:00
|
|
|
if (n == 0 && is_nonblocking ())
|
2000-03-12 14:29:54 +08:00
|
|
|
{
|
|
|
|
set_errno (EAGAIN);
|
|
|
|
rc = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sleep (10);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ReadFile (handle, outbuf, rlen, &n, NULL) == FALSE)
|
2000-03-12 12:44:37 +08:00
|
|
|
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-09-08 00:23:51 +08:00
|
|
|
get_ttyp ()->write_error = 0;
|
|
|
|
if (output_done_event != NULL)
|
|
|
|
SetEvent (output_done_event);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-03-12 12:44:37 +08:00
|
|
|
if (get_ttyp ()->ti.c_lflag & FLUSHO)
|
2000-09-08 00:23:51 +08:00
|
|
|
continue;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-03-12 14:29:54 +08:00
|
|
|
char *optr;
|
|
|
|
optr = buf;
|
|
|
|
if (pktmode_on)
|
|
|
|
*optr++ = TIOCPKT_DATA;
|
|
|
|
|
2000-03-12 12:44:37 +08:00
|
|
|
if (!(get_ttyp ()->ti.c_oflag & OPOST)) // post-process output
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-03-12 14:29:54 +08:00
|
|
|
memcpy (optr, outbuf, n);
|
|
|
|
optr += n;
|
2000-03-12 12:44:37 +08:00
|
|
|
}
|
|
|
|
else // raw output mode
|
|
|
|
{
|
2000-03-12 14:29:54 +08:00
|
|
|
char *iptr = outbuf;
|
2000-03-12 12:44:37 +08:00
|
|
|
|
|
|
|
while (n--)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-03-12 12:44:37 +08:00
|
|
|
switch (*iptr)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-03-12 12:44:37 +08:00
|
|
|
case '\r':
|
|
|
|
if ((get_ttyp ()->ti.c_oflag & ONOCR) && column == 0)
|
|
|
|
{
|
|
|
|
iptr++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (get_ttyp ()->ti.c_oflag & OCRNL)
|
|
|
|
*iptr = '\n';
|
|
|
|
else
|
|
|
|
column = 0;
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
if (get_ttyp ()->ti.c_oflag & ONLCR)
|
|
|
|
{
|
|
|
|
*optr++ = '\r';
|
|
|
|
column = 0;
|
|
|
|
}
|
|
|
|
if (get_ttyp ()->ti.c_oflag & ONLRET)
|
|
|
|
column = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
column++;
|
|
|
|
break;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2000-03-12 12:44:37 +08:00
|
|
|
|
|
|
|
/* Don't store data past the end of the user's buffer. This
|
|
|
|
can happen if the user requests a read of 1 byte when
|
|
|
|
doing \r\n expansion. */
|
|
|
|
if (optr - buf >= (int) len)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-03-12 12:44:37 +08:00
|
|
|
if (*iptr != '\n' || n != 0)
|
|
|
|
system_printf ("internal error: %d unexpected characters", n);
|
2000-03-12 14:29:54 +08:00
|
|
|
need_nl = 1;
|
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
|
|
|
*optr++ = *iptr++;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2000-03-12 12:44:37 +08:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static DWORD WINAPI
|
2000-02-21 13:20:38 +08:00
|
|
|
process_output (void *)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
char buf[OUT_BUFFER_SIZE*2];
|
|
|
|
|
2000-03-12 14:29:54 +08:00
|
|
|
for (;;)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-03-12 14:29:54 +08:00
|
|
|
int n = tty_master->process_slave_output (buf, OUT_BUFFER_SIZE, 0);
|
2000-02-18 03:38:33 +08:00
|
|
|
if (n < 0)
|
|
|
|
{
|
|
|
|
termios_printf ("ReadFile %E");
|
|
|
|
ExitThread (0);
|
|
|
|
}
|
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
/* End of file. */
|
|
|
|
ExitThread (0);
|
|
|
|
}
|
|
|
|
n = tty_master->console->write ((void *) buf, (size_t) n);
|
2000-09-08 00:23:51 +08:00
|
|
|
tty_master->get_ttyp ()->write_error = n == -1 ? get_errno () : 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Process tty ioctl requests */
|
|
|
|
|
|
|
|
static DWORD WINAPI
|
2000-02-21 13:20:38 +08:00
|
|
|
process_ioctl (void *)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
WaitForSingleObject (tty_master->ioctl_request_event, INFINITE);
|
|
|
|
termios_printf ("ioctl() request");
|
|
|
|
tty_master->get_ttyp ()->ioctl_retval =
|
|
|
|
tty_master->console->ioctl (tty_master->get_ttyp ()->cmd,
|
|
|
|
(void *) &tty_master->get_ttyp ()->arg);
|
|
|
|
SetEvent (tty_master->ioctl_done_event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************************/
|
|
|
|
/* Tty slave stuff */
|
|
|
|
|
2001-03-11 09:58:23 +08:00
|
|
|
fhandler_tty_slave::fhandler_tty_slave (int num, const char *name) :
|
2000-02-18 03:38:33 +08:00
|
|
|
fhandler_tty_common (FH_TTYS, name, num)
|
|
|
|
{
|
|
|
|
set_cb (sizeof *this);
|
|
|
|
ttynum = num;
|
|
|
|
/* FIXME: This is wasteful. We should rewrite the set_name path to eliminate the
|
|
|
|
need for double allocates. */
|
2001-04-24 10:07:58 +08:00
|
|
|
unix_path_name = (char *) crealloc (unix_path_name, strlen (win32_path_name) + 1);
|
|
|
|
strcpy (unix_path_name, win32_path_name);
|
|
|
|
unix_path_name[0] = unix_path_name[4] = '/';
|
|
|
|
debug_printf ("unix '%s', win32 '%s'", unix_path_name, win32_path_name);
|
2000-02-18 03:38:33 +08:00
|
|
|
inuse = NULL;
|
|
|
|
}
|
|
|
|
|
2001-03-11 09:58:23 +08:00
|
|
|
fhandler_tty_slave::fhandler_tty_slave (const char *name) :
|
2000-02-18 03:38:33 +08:00
|
|
|
fhandler_tty_common (FH_TTYS, name, 0)
|
|
|
|
{
|
|
|
|
set_cb (sizeof *this);
|
|
|
|
inuse = NULL;
|
|
|
|
}
|
|
|
|
|
2001-04-24 10:07:58 +08:00
|
|
|
/* FIXME: This function needs to close handles when it has
|
|
|
|
a failing condition. */
|
2000-02-18 03:38:33 +08:00
|
|
|
int
|
|
|
|
fhandler_tty_slave::open (const char *, int flags, mode_t)
|
|
|
|
{
|
|
|
|
tcinit (cygwin_shared->tty[ttynum]);
|
|
|
|
|
|
|
|
attach_tty (ttynum);
|
2001-04-29 07:48:28 +08:00
|
|
|
tc->set_ctty (ttynum, flags);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
set_flags (flags);
|
|
|
|
/* Create synchronisation events */
|
|
|
|
char buf[40];
|
|
|
|
|
|
|
|
/* output_done_event may or may not exist. It will exist if the tty
|
|
|
|
was opened by fhandler_tty_master::init, normally called at
|
|
|
|
startup if use_tty is non-zero. It will not exist if this is a
|
|
|
|
pty opened by fhandler_pty_master::open. In the former case, tty
|
|
|
|
output is handled by a separate thread which controls output. */
|
|
|
|
__small_sprintf (buf, OUTPUT_DONE_EVENT, ttynum);
|
|
|
|
output_done_event = OpenEvent (EVENT_ALL_ACCESS, TRUE, buf);
|
|
|
|
|
2001-09-01 13:17:34 +08:00
|
|
|
if (!(output_mutex = get_ttyp ()->open_output_mutex ()))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
termios_printf ("open output mutex failed, %E");
|
|
|
|
__seterrno ();
|
|
|
|
return 0;
|
|
|
|
}
|
2001-09-01 13:17:34 +08:00
|
|
|
if (!(input_mutex = get_ttyp ()->open_input_mutex ()))
|
2001-03-04 23:34:25 +08:00
|
|
|
{
|
|
|
|
termios_printf ("open input mutex failed, %E");
|
|
|
|
__seterrno ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
__small_sprintf (buf, INPUT_AVAILABLE_EVENT, ttynum);
|
|
|
|
if (!(input_available_event = OpenEvent (EVENT_ALL_ACCESS, TRUE, buf)))
|
|
|
|
{
|
|
|
|
termios_printf ("open input event failed, %E");
|
|
|
|
__seterrno ();
|
|
|
|
return 0;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* The ioctl events may or may not exist. See output_done_event,
|
|
|
|
above. */
|
|
|
|
__small_sprintf (buf, IOCTL_REQUEST_EVENT, ttynum);
|
|
|
|
ioctl_request_event = OpenEvent (EVENT_ALL_ACCESS, TRUE, buf);
|
|
|
|
__small_sprintf (buf, IOCTL_DONE_EVENT, ttynum);
|
|
|
|
ioctl_done_event = OpenEvent (EVENT_ALL_ACCESS, TRUE, buf);
|
|
|
|
|
|
|
|
/* FIXME: Needs a method to eliminate tty races */
|
|
|
|
{
|
|
|
|
acquire_output_mutex (500);
|
2001-09-01 13:38:46 +08:00
|
|
|
inuse = get_ttyp ()->create_inuse (TTY_SLAVE_ALIVE);
|
2000-02-18 03:38:33 +08:00
|
|
|
get_ttyp ()->was_opened = TRUE;
|
|
|
|
release_output_mutex ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Duplicate tty handles. */
|
|
|
|
|
|
|
|
if (!get_ttyp ()->from_slave || !get_ttyp ()->to_slave)
|
|
|
|
{
|
|
|
|
termios_printf ("tty handles have been closed");
|
|
|
|
set_errno (EACCES);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE tty_owner = OpenProcess (PROCESS_DUP_HANDLE, FALSE,
|
|
|
|
get_ttyp ()->master_pid);
|
|
|
|
if (tty_owner == NULL)
|
|
|
|
{
|
2001-03-11 09:58:23 +08:00
|
|
|
termios_printf ("can't open tty (%d) handle process %d",
|
2000-02-18 03:38:33 +08:00
|
|
|
ttynum, get_ttyp ()->master_pid);
|
|
|
|
__seterrno ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE nh;
|
|
|
|
if (!DuplicateHandle (tty_owner, get_ttyp ()->from_master, hMainProc, &nh, 0, TRUE,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
termios_printf ("can't duplicate input, %E");
|
|
|
|
__seterrno ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
set_io_handle (nh);
|
2000-10-17 09:42:04 +08:00
|
|
|
ProtectHandle1 (nh, from_pty);
|
2000-02-18 03:38:33 +08:00
|
|
|
termios_printf ("duplicated from_master %p->%p from tty_owner %p",
|
|
|
|
get_ttyp ()->from_master, nh, tty_owner);
|
|
|
|
if (!DuplicateHandle (tty_owner, get_ttyp ()->to_master, hMainProc, &nh, 0, TRUE,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
termios_printf ("can't duplicate output, %E");
|
|
|
|
__seterrno ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
set_output_handle (nh);
|
2000-10-17 09:42:04 +08:00
|
|
|
ProtectHandle1 (nh, to_pty);
|
2000-02-18 03:38:33 +08:00
|
|
|
CloseHandle (tty_owner);
|
|
|
|
|
2001-04-24 10:07:58 +08:00
|
|
|
set_open_status ();
|
2001-03-11 09:58:23 +08:00
|
|
|
termios_printf ("tty%d opened", ttynum);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-02-21 13:20:38 +08:00
|
|
|
fhandler_tty_slave::init (HANDLE, DWORD a, mode_t)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
int mode = 0;
|
|
|
|
|
|
|
|
a &= GENERIC_READ | GENERIC_WRITE;
|
|
|
|
if (a == GENERIC_READ)
|
|
|
|
mode = O_RDONLY;
|
|
|
|
if (a == GENERIC_WRITE)
|
|
|
|
mode = O_WRONLY;
|
|
|
|
if (a == (GENERIC_READ | GENERIC_WRITE))
|
|
|
|
mode = O_RDWR;
|
|
|
|
|
2001-06-19 05:18:59 +08:00
|
|
|
open (0, mode);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_tty_slave::write (const void *ptr, size_t len)
|
|
|
|
{
|
|
|
|
DWORD n, towrite = len;
|
|
|
|
|
2001-03-11 09:58:23 +08:00
|
|
|
termios_printf ("tty%d, write(%x, %d)", ttynum, ptr, len);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
acquire_output_mutex (INFINITE);
|
|
|
|
|
|
|
|
while (len)
|
|
|
|
{
|
|
|
|
n = min (OUT_BUFFER_SIZE, len);
|
|
|
|
char *buf = (char *)ptr;
|
|
|
|
ptr = (char *) ptr + n;
|
|
|
|
len -= n;
|
|
|
|
|
2000-09-08 00:23:51 +08:00
|
|
|
/* Previous write may have set write_error to != 0. Check it here.
|
|
|
|
This is less than optimal, but the alternative slows down tty
|
|
|
|
writes enormously. */
|
|
|
|
if (get_ttyp ()->write_error)
|
|
|
|
{
|
|
|
|
set_errno (get_ttyp ()->write_error);
|
|
|
|
towrite = (DWORD) -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
if (WriteFile (get_output_handle (), buf, n, &n, NULL) == FALSE)
|
|
|
|
{
|
2000-08-29 10:06:49 +08:00
|
|
|
DWORD err = GetLastError ();
|
2000-02-18 03:38:33 +08:00
|
|
|
termios_printf ("WriteFile failed, %E");
|
2000-08-29 10:06:49 +08:00
|
|
|
switch (err)
|
|
|
|
{
|
|
|
|
case ERROR_NO_DATA:
|
|
|
|
err = ERROR_IO_DEVICE;
|
|
|
|
default:
|
2000-09-19 07:46:10 +08:00
|
|
|
__seterrno_from_win_error (err);
|
2000-08-29 10:06:49 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
_raise (SIGHUP); /* FIXME: Should this be SIGTTOU? */
|
2000-08-29 10:06:49 +08:00
|
|
|
towrite = (DWORD) -1;
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (output_done_event != NULL)
|
|
|
|
{
|
2000-03-12 12:44:37 +08:00
|
|
|
DWORD rc;
|
|
|
|
DWORD x = n * 1000;
|
|
|
|
rc = WaitForSingleObject (output_done_event, x);
|
2001-03-11 09:58:23 +08:00
|
|
|
termios_printf ("waited %d ms for output_done_event, WFSO %d", x, rc);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
release_output_mutex ();
|
|
|
|
return towrite;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_tty_slave::read (void *ptr, size_t len)
|
|
|
|
{
|
|
|
|
DWORD n;
|
|
|
|
int totalread = 0;
|
|
|
|
int vmin = INT_MAX;
|
|
|
|
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-19 02:05:01 +08:00
|
|
|
char peek_buf[INP_BUFFER_SIZE];
|
2001-03-04 23:34:25 +08:00
|
|
|
DWORD time_to_wait;
|
|
|
|
DWORD rc;
|
|
|
|
HANDLE w4[2];
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-03-04 23:34:25 +08:00
|
|
|
termios_printf ("read(%x, %d) handle %d", ptr, len, get_handle ());
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
if (!(get_ttyp ()->ti.c_lflag & ICANON))
|
|
|
|
{
|
2001-03-04 23:34:25 +08:00
|
|
|
vmin = min (INP_BUFFER_SIZE, get_ttyp ()->ti.c_cc[VMIN]);
|
2000-02-18 03:38:33 +08:00
|
|
|
vtime = get_ttyp ()->ti.c_cc[VTIME];
|
2001-03-04 23:34:25 +08:00
|
|
|
if (vmin < 0) vmin = 0;
|
|
|
|
if (vtime < 0) vtime = 0;
|
|
|
|
if (vmin == 0)
|
2001-03-11 09:58:23 +08:00
|
|
|
time_to_wait = INFINITE;
|
2001-03-04 23:34:25 +08:00
|
|
|
else
|
2001-04-24 10:07:58 +08:00
|
|
|
time_to_wait = (vtime == 0 ? INFINITE : 100 * vtime);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2001-03-04 23:34:25 +08:00
|
|
|
else
|
|
|
|
time_to_wait = INFINITE;
|
|
|
|
|
|
|
|
w4[0] = signal_arrived;
|
|
|
|
w4[1] = input_available_event;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
while (len)
|
|
|
|
{
|
2001-03-04 23:34:25 +08:00
|
|
|
rc = WaitForMultipleObjects (2, w4, FALSE, time_to_wait);
|
|
|
|
if (rc == WAIT_OBJECT_0)
|
2001-03-11 09:58:23 +08:00
|
|
|
{
|
2001-03-17 09:45:40 +08:00
|
|
|
/* if we've received signal after successfully reading some data,
|
2001-03-11 09:58:23 +08:00
|
|
|
just return all data successfully read */
|
|
|
|
if (totalread > 0)
|
|
|
|
break;
|
|
|
|
set_sig_errno (EINTR);
|
|
|
|
return -1;
|
|
|
|
}
|
2001-03-04 23:34:25 +08:00
|
|
|
else if (rc == WAIT_FAILED)
|
2001-03-11 09:58:23 +08:00
|
|
|
{
|
|
|
|
termios_printf ("wait for input event failed, %E");
|
|
|
|
break;
|
|
|
|
}
|
2001-03-04 23:34:25 +08:00
|
|
|
else if (rc == WAIT_TIMEOUT)
|
2001-03-11 09:58:23 +08:00
|
|
|
break;
|
2001-03-04 23:34:25 +08:00
|
|
|
rc = WaitForSingleObject (input_mutex, 1000);
|
|
|
|
if (rc == WAIT_FAILED)
|
2001-03-11 09:58:23 +08:00
|
|
|
{
|
|
|
|
termios_printf ("wait for input mutex failed, %E");
|
|
|
|
break;
|
|
|
|
}
|
2001-03-04 23:34:25 +08:00
|
|
|
else if (rc == WAIT_TIMEOUT)
|
2001-03-11 09:58:23 +08:00
|
|
|
{
|
|
|
|
termios_printf ("failed to acquire input mutex after input event arrived");
|
|
|
|
break;
|
|
|
|
}
|
2001-03-19 02:05:01 +08:00
|
|
|
if (!PeekNamedPipe (get_handle (), peek_buf, sizeof(peek_buf), &bytes_in_pipe, NULL, NULL))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2001-03-04 23:34:25 +08:00
|
|
|
termios_printf ("PeekNamedPipe failed, %E");
|
2001-03-11 09:58:23 +08:00
|
|
|
_raise (SIGHUP);
|
|
|
|
bytes_in_pipe = 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2001-03-04 23:34:25 +08:00
|
|
|
readlen = min (bytes_in_pipe, min (len, sizeof (buf)));
|
2001-03-11 09:58:23 +08:00
|
|
|
if (readlen)
|
|
|
|
{
|
|
|
|
termios_printf ("reading %d bytes (vtime %d)", readlen, vtime);
|
|
|
|
if (ReadFile (get_handle (), buf, readlen, &n, NULL) == FALSE)
|
|
|
|
{
|
2001-03-04 23:34:25 +08:00
|
|
|
termios_printf ("read failed, %E");
|
|
|
|
_raise (SIGHUP);
|
|
|
|
}
|
2001-09-08 05:32:07 +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 */
|
|
|
|
if (!PeekNamedPipe (get_handle (), peek_buf, 1, &bytes_in_pipe, NULL, NULL))
|
2001-03-19 02:05:01 +08:00
|
|
|
{
|
|
|
|
termios_printf ("PeekNamedPipe failed, %E");
|
|
|
|
_raise (SIGHUP);
|
|
|
|
bytes_in_pipe = 0;
|
|
|
|
}
|
2001-03-11 09:58:23 +08:00
|
|
|
if (n)
|
|
|
|
{
|
|
|
|
len -= n;
|
|
|
|
totalread += n;
|
|
|
|
memcpy (ptr, buf, n);
|
|
|
|
ptr = (char *) ptr + n;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
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;
|
2001-03-04 23:34:25 +08:00
|
|
|
if (totalread >= vmin && (vmin > 0 || totalread > 0))
|
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
|
|
|
}
|
|
|
|
termios_printf ("%d=read(%x, %d)", totalread, ptr, len);
|
|
|
|
return totalread;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_tty_common::dup (fhandler_base *child)
|
|
|
|
{
|
|
|
|
fhandler_tty_slave *fts = (fhandler_tty_slave *) child;
|
|
|
|
int errind;
|
|
|
|
|
|
|
|
fts->ttynum = ttynum;
|
|
|
|
fts->tcinit (get_ttyp ());
|
|
|
|
|
|
|
|
attach_tty (ttynum);
|
2001-05-14 02:05:52 +08:00
|
|
|
tc->set_ctty (ttynum, openflags);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
HANDLE nh;
|
|
|
|
|
|
|
|
if (output_done_event == NULL)
|
|
|
|
fts->output_done_event = NULL;
|
|
|
|
else if (!DuplicateHandle (hMainProc, output_done_event, hMainProc,
|
|
|
|
&fts->output_done_event, 0, 1,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
errind = 1;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (ioctl_request_event == NULL)
|
|
|
|
fts->ioctl_request_event = NULL;
|
|
|
|
else if (!DuplicateHandle (hMainProc, ioctl_request_event, hMainProc,
|
|
|
|
&fts->ioctl_request_event, 0, 1,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
errind = 2;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (ioctl_done_event == NULL)
|
|
|
|
fts->ioctl_done_event = NULL;
|
|
|
|
else if (!DuplicateHandle (hMainProc, ioctl_done_event, hMainProc,
|
|
|
|
&fts->ioctl_done_event, 0, 1,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
errind = 3;
|
|
|
|
goto err;
|
|
|
|
}
|
2001-03-04 23:34:25 +08:00
|
|
|
if (!DuplicateHandle (hMainProc, input_available_event, hMainProc,
|
|
|
|
&fts->input_available_event, 0, 1,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
errind = 4;
|
|
|
|
goto err;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
if (!DuplicateHandle (hMainProc, output_mutex, hMainProc,
|
|
|
|
&fts->output_mutex, 0, 1,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
2001-03-04 23:34:25 +08:00
|
|
|
errind = 5;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!DuplicateHandle (hMainProc, input_mutex, hMainProc,
|
|
|
|
&fts->input_mutex, 0, 1,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
errind = 6;
|
2000-02-18 03:38:33 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!DuplicateHandle (hMainProc, get_handle (), hMainProc,
|
|
|
|
&nh, 0, 1,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
2001-03-04 23:34:25 +08:00
|
|
|
errind = 7;
|
2000-02-18 03:38:33 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
fts->set_io_handle (nh);
|
|
|
|
|
|
|
|
if (!DuplicateHandle (hMainProc, get_output_handle (), hMainProc,
|
|
|
|
&nh, 0, 1,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
2001-03-04 23:34:25 +08:00
|
|
|
errind = 8;
|
2000-02-18 03:38:33 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
fts->set_output_handle (nh);
|
|
|
|
|
|
|
|
if (inuse == NULL)
|
|
|
|
fts->inuse = NULL;
|
|
|
|
else if (!DuplicateHandle (hMainProc, inuse, hMainProc,
|
|
|
|
&fts->inuse, 0, 1,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
2001-03-04 23:34:25 +08:00
|
|
|
errind = 9;
|
2000-02-18 03:38:33 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
__seterrno ();
|
|
|
|
termios_printf ("dup %d failed in DuplicateHandle, %E", errind);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_tty_slave::tcgetattr (struct termios *t)
|
|
|
|
{
|
|
|
|
*t = get_ttyp ()->ti;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-02-21 13:20:38 +08:00
|
|
|
fhandler_tty_slave::tcsetattr (int, const struct termios *t)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
acquire_output_mutex (INFINITE);
|
|
|
|
get_ttyp ()->ti = *t;
|
|
|
|
release_output_mutex ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-02-21 13:20:38 +08:00
|
|
|
fhandler_tty_slave::tcflush (int)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_tty_slave::ioctl (unsigned int cmd, void *arg)
|
|
|
|
{
|
|
|
|
termios_printf ("ioctl (%x)", cmd);
|
|
|
|
|
|
|
|
if (myself->pgid && get_ttyp ()->getpgid () != myself->pgid &&
|
|
|
|
myself->ctty == ttynum && (get_ttyp ()->ti.c_lflag & TOSTOP))
|
|
|
|
{
|
|
|
|
/* background process */
|
2001-03-11 09:58:23 +08:00
|
|
|
termios_printf ("bg ioctl pgid %d, tpgid %d, ctty %d",
|
|
|
|
myself->pgid, get_ttyp ()->getpgid (), myself->ctty);
|
2000-02-18 03:38:33 +08:00
|
|
|
_raise (SIGTTOU);
|
|
|
|
}
|
2000-05-12 13:06:43 +08:00
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case TIOCGWINSZ:
|
|
|
|
case TIOCSWINSZ:
|
|
|
|
break;
|
|
|
|
case FIONBIO:
|
2001-08-15 15:49:15 +08:00
|
|
|
set_nonblocking (*(int *) arg);
|
2000-05-12 13:06:43 +08:00
|
|
|
goto out;
|
2000-02-18 03:38:33 +08:00
|
|
|
default:
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
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;
|
|
|
|
if (ioctl_request_event)
|
|
|
|
SetEvent (ioctl_request_event);
|
|
|
|
* (struct winsize *) arg = get_ttyp ()->arg.winsize;
|
|
|
|
if (ioctl_done_event)
|
|
|
|
WaitForSingleObject (ioctl_done_event, INFINITE);
|
|
|
|
get_ttyp ()->winsize = get_ttyp ()->arg.winsize;
|
|
|
|
break;
|
|
|
|
case TIOCSWINSZ:
|
|
|
|
get_ttyp ()->ioctl_retval = -1;
|
|
|
|
get_ttyp ()->arg.winsize = * (struct winsize *) arg;
|
|
|
|
if (ioctl_request_event)
|
|
|
|
SetEvent (ioctl_request_event);
|
|
|
|
if (ioctl_done_event)
|
|
|
|
WaitForSingleObject (ioctl_done_event, INFINITE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
release_output_mutex ();
|
|
|
|
|
|
|
|
out:
|
2000-02-18 03:38:33 +08:00
|
|
|
termios_printf ("%d = ioctl (%x)", get_ttyp ()->ioctl_retval, cmd);
|
|
|
|
return get_ttyp ()->ioctl_retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************
|
|
|
|
fhandler_pty_master
|
|
|
|
*/
|
|
|
|
fhandler_pty_master::fhandler_pty_master (const char *name, DWORD devtype, int unit) :
|
|
|
|
fhandler_tty_common (devtype, name, unit)
|
|
|
|
{
|
|
|
|
set_cb (sizeof *this);
|
|
|
|
ioctl_request_event = NULL;
|
|
|
|
ioctl_done_event = NULL;
|
2000-03-12 14:29:54 +08:00
|
|
|
pktmode = need_nl = 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
inuse = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_pty_master::open (const char *, int flags, mode_t)
|
|
|
|
{
|
|
|
|
ttynum = cygwin_shared->tty.allocate_tty (0);
|
|
|
|
if (ttynum < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
cygwin_shared->tty[ttynum]->common_init (this);
|
2001-09-01 13:38:46 +08:00
|
|
|
inuse = get_ttyp ()->create_inuse (TTY_MASTER_ALIVE);
|
2000-02-18 03:38:33 +08:00
|
|
|
set_flags (flags);
|
2001-04-24 10:07:58 +08:00
|
|
|
set_open_status ();
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
termios_printf ("opened pty master tty%d<%p>", ttynum, this);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_tty_common::close ()
|
|
|
|
{
|
|
|
|
if (output_done_event && !CloseHandle (output_done_event))
|
|
|
|
termios_printf ("CloseHandle (output_done_event), %E");
|
|
|
|
if (ioctl_done_event && !CloseHandle (ioctl_done_event))
|
|
|
|
termios_printf ("CloseHandle (ioctl_done_event), %E");
|
|
|
|
if (ioctl_request_event && !CloseHandle (ioctl_request_event))
|
|
|
|
termios_printf ("CloseHandle (ioctl_request_event), %E");
|
|
|
|
if (inuse && !CloseHandle (inuse))
|
|
|
|
termios_printf ("CloseHandle (inuse), %E");
|
|
|
|
if (!ForceCloseHandle (output_mutex))
|
|
|
|
termios_printf ("CloseHandle (output_mutex<%p>), %E", output_mutex);
|
2001-03-04 23:34:25 +08:00
|
|
|
if (!ForceCloseHandle (input_mutex))
|
|
|
|
termios_printf ("CloseHandle (input_mutex<%p>), %E", input_mutex);
|
2001-03-20 02:27:37 +08:00
|
|
|
|
|
|
|
/* Send EOF to slaves if master side is closed */
|
|
|
|
if (!get_ttyp ()->master_alive ())
|
|
|
|
{
|
|
|
|
termios_printf ("no more masters left. sending EOF" );
|
|
|
|
SetEvent (input_available_event);
|
|
|
|
}
|
|
|
|
|
2001-03-04 23:34:25 +08:00
|
|
|
if (!ForceCloseHandle (input_available_event))
|
|
|
|
termios_printf ("CloseHandle (input_available_event<%p>), %E", input_available_event);
|
2000-10-17 09:42:04 +08:00
|
|
|
if (!ForceCloseHandle1 (get_handle (), from_pty))
|
2000-02-18 03:38:33 +08:00
|
|
|
termios_printf ("CloseHandle (get_handle ()<%p>), %E", get_handle ());
|
2000-10-17 09:42:04 +08:00
|
|
|
if (!ForceCloseHandle1 (get_output_handle (), to_pty))
|
2000-02-18 03:38:33 +08:00
|
|
|
termios_printf ("CloseHandle (get_output_handle ()<%p>), %E", get_output_handle ());
|
|
|
|
|
|
|
|
inuse = NULL;
|
2001-07-21 11:20:01 +08:00
|
|
|
termios_printf ("tty%d <%p,%p> closed", ttynum, get_handle (), get_output_handle ());
|
2000-02-18 03:38:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_pty_master::close ()
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
while (accept_input () > 0)
|
|
|
|
continue;
|
|
|
|
#endif
|
|
|
|
this->fhandler_tty_common::close ();
|
|
|
|
|
|
|
|
if (!get_ttyp ()->master_alive ())
|
|
|
|
{
|
|
|
|
termios_printf ("freeing tty%d (%d)", ttynum, get_ttyp ()->ntty);
|
2000-10-17 09:42:04 +08:00
|
|
|
#if 0
|
2000-02-18 03:38:33 +08:00
|
|
|
if (get_ttyp ()->to_slave)
|
2000-10-17 09:42:04 +08:00
|
|
|
ForceCloseHandle1 (get_ttyp ()->to_slave, to_slave);
|
2000-02-18 03:38:33 +08:00
|
|
|
if (get_ttyp ()->from_slave)
|
2000-10-17 09:42:04 +08:00
|
|
|
ForceCloseHandle1 (get_ttyp ()->from_slave, from_slave);
|
|
|
|
#endif
|
2000-02-18 03:38:33 +08:00
|
|
|
if (get_ttyp ()->from_master)
|
|
|
|
CloseHandle (get_ttyp ()->from_master);
|
|
|
|
if (get_ttyp ()->to_master)
|
|
|
|
CloseHandle (get_ttyp ()->to_master);
|
|
|
|
get_ttyp ()->init ();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_pty_master::write (const void *ptr, size_t len)
|
|
|
|
{
|
2001-03-05 14:28:25 +08:00
|
|
|
(void) line_edit ((char *) ptr, len);
|
2000-02-18 03:38:33 +08:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_pty_master::read (void *ptr, size_t len)
|
|
|
|
{
|
2000-09-08 00:23:51 +08:00
|
|
|
return process_slave_output ((char *) ptr, len, pktmode);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_pty_master::tcgetattr (struct termios *t)
|
|
|
|
{
|
|
|
|
*t = cygwin_shared->tty[ttynum]->ti;
|
|
|
|
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
|
|
|
{
|
|
|
|
cygwin_shared->tty[ttynum]->ti = *t;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-02-21 13:20:38 +08:00
|
|
|
fhandler_pty_master::tcflush (int)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_pty_master::ioctl (unsigned int cmd, void *arg)
|
|
|
|
{
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case TIOCPKT:
|
|
|
|
pktmode = * (int *) arg;
|
|
|
|
break;
|
|
|
|
case TIOCGWINSZ:
|
|
|
|
* (struct winsize *) arg = get_ttyp ()->winsize;
|
|
|
|
break;
|
|
|
|
case TIOCSWINSZ:
|
|
|
|
get_ttyp ()->winsize = * (struct winsize *) arg;
|
|
|
|
_kill (-get_ttyp ()->getpgid (), SIGWINCH);
|
|
|
|
break;
|
|
|
|
case FIONBIO:
|
2001-08-15 15:49:15 +08:00
|
|
|
set_nonblocking (*(int *) arg);
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
fhandler_pty_master::ptsname (void)
|
|
|
|
{
|
|
|
|
static char buf[32];
|
|
|
|
|
|
|
|
__small_sprintf (buf, "/dev/tty%d", ttynum);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_tty_common::set_close_on_exec (int val)
|
|
|
|
{
|
2001-03-11 09:58:23 +08:00
|
|
|
#ifndef DEBUGGING
|
2000-02-18 03:38:33 +08:00
|
|
|
this->fhandler_base::set_close_on_exec (val);
|
2001-03-11 09:58:23 +08:00
|
|
|
#else
|
|
|
|
/* FIXME: This is a duplication from fhandler_base::set_close_on_exec.
|
|
|
|
It is here because we need to specify the "from_pty" stuff here or
|
|
|
|
we'll get warnings from ForceCloseHandle when debugging. */
|
2001-09-21 04:58:29 +08:00
|
|
|
set_inheritance (get_io_handle (), val);
|
2001-03-11 09:58:23 +08:00
|
|
|
set_close_on_exec_flag (val);
|
|
|
|
#endif
|
2000-02-18 03:38:33 +08:00
|
|
|
if (output_done_event)
|
|
|
|
set_inheritance (output_done_event, val);
|
|
|
|
if (ioctl_request_event)
|
|
|
|
set_inheritance (ioctl_request_event, val);
|
|
|
|
if (ioctl_done_event)
|
|
|
|
set_inheritance (ioctl_done_event, val);
|
|
|
|
if (inuse)
|
|
|
|
set_inheritance (inuse, val);
|
2001-09-21 04:58:29 +08:00
|
|
|
set_inheritance (output_mutex, val);
|
|
|
|
set_inheritance (input_mutex, val);
|
2001-03-04 23:34:25 +08:00
|
|
|
set_inheritance (input_available_event, val);
|
2001-09-21 04:58:29 +08:00
|
|
|
set_inheritance (output_handle, val);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_tty_common::fixup_after_fork (HANDLE parent)
|
|
|
|
{
|
2001-03-13 05:27:42 +08:00
|
|
|
this->fhandler_termios::fixup_after_fork (parent);
|
2000-02-18 03:38:33 +08:00
|
|
|
if (output_done_event)
|
|
|
|
fork_fixup (parent, output_done_event, "output_done_event");
|
|
|
|
if (ioctl_request_event)
|
|
|
|
fork_fixup (parent, ioctl_request_event, "ioctl_request_event");
|
|
|
|
if (ioctl_done_event)
|
|
|
|
fork_fixup (parent, ioctl_done_event, "ioctl_done_event");
|
|
|
|
if (output_mutex)
|
|
|
|
{
|
|
|
|
fork_fixup (parent, output_mutex, "output_mutex");
|
|
|
|
ProtectHandle (output_mutex);
|
|
|
|
}
|
2001-03-04 23:34:25 +08:00
|
|
|
if (input_mutex)
|
|
|
|
{
|
|
|
|
fork_fixup (parent, input_mutex, "input_mutex");
|
|
|
|
ProtectHandle (input_mutex);
|
|
|
|
}
|
|
|
|
if (input_available_event)
|
|
|
|
fork_fixup (parent, input_available_event, "input_available_event");
|
2000-02-18 03:38:33 +08:00
|
|
|
fork_fixup (parent, inuse, "inuse");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_pty_master::set_close_on_exec (int val)
|
|
|
|
{
|
|
|
|
this->fhandler_tty_common::set_close_on_exec (val);
|
|
|
|
|
|
|
|
/* FIXME: There is a console handle leak here. */
|
|
|
|
if (get_ttyp ()->master_pid == GetCurrentProcessId ())
|
|
|
|
{
|
|
|
|
get_ttyp ()->from_slave = get_handle ();
|
|
|
|
get_ttyp ()->to_slave = get_output_handle ();
|
2000-10-17 09:42:04 +08:00
|
|
|
termios_printf ("from_slave %p, to_slave %p", get_handle (),
|
|
|
|
get_output_handle ());
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_tty_master::fixup_after_fork (HANDLE child)
|
|
|
|
{
|
|
|
|
this->fhandler_pty_master::fixup_after_fork (child);
|
|
|
|
console->fixup_after_fork (child);
|
|
|
|
}
|
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
void
|
|
|
|
fhandler_tty_master::fixup_after_exec (HANDLE)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
console->close ();
|
|
|
|
init_console ();
|
2000-09-03 12:16:35 +08:00
|
|
|
return;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_tty_master::init_console ()
|
|
|
|
{
|
2001-04-19 05:10:15 +08:00
|
|
|
console = (fhandler_console *) cygheap->fdtab.build_fhandler (-1, FH_CONSOLE, "/dev/ttym");
|
2000-02-18 03:38:33 +08:00
|
|
|
if (console == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
console->init (INVALID_HANDLE_VALUE, GENERIC_READ | GENERIC_WRITE, O_BINARY);
|
|
|
|
console->set_r_no_interrupt (1);
|
|
|
|
return 0;
|
|
|
|
}
|