2005-04-16 07:20:01 +02:00
|
|
|
/* fhandler_fifo.cc - See fhandler.h for a description of the fhandler classes.
|
2003-09-20 02:31:13 +02:00
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include "winsup.h"
|
2019-03-22 20:30:36 +01:00
|
|
|
#include <w32api/winioctl.h>
|
2008-04-07 18:15:45 +02:00
|
|
|
#include "miscfuncs.h"
|
2003-09-20 02:31:13 +02:00
|
|
|
|
|
|
|
#include "cygerrno.h"
|
|
|
|
#include "security.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "fhandler.h"
|
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
2007-07-07 19:00:33 +02:00
|
|
|
#include "sigproc.h"
|
|
|
|
#include "cygtls.h"
|
2009-10-31 14:24:06 +01:00
|
|
|
#include "shared_info.h"
|
2011-04-29 10:27:11 +02:00
|
|
|
#include "ntdll.h"
|
2012-06-17 22:50:24 +02:00
|
|
|
#include "cygwait.h"
|
2003-09-20 02:31:13 +02:00
|
|
|
|
2019-03-22 20:30:36 +01:00
|
|
|
/* This is only to be used for writers. When reading,
|
|
|
|
STATUS_PIPE_EMPTY simply means there's no data to be read. */
|
|
|
|
#define STATUS_PIPE_IS_CLOSED(status) \
|
|
|
|
({ NTSTATUS _s = (status); \
|
|
|
|
_s == STATUS_PIPE_CLOSING \
|
|
|
|
|| _s == STATUS_PIPE_BROKEN \
|
|
|
|
|| _s == STATUS_PIPE_EMPTY; })
|
|
|
|
|
2019-04-14 21:16:02 +02:00
|
|
|
#define STATUS_PIPE_NO_INSTANCE_AVAILABLE(status) \
|
|
|
|
({ NTSTATUS _s = (status); \
|
|
|
|
_s == STATUS_INSTANCE_NOT_AVAILABLE \
|
|
|
|
|| _s == STATUS_PIPE_NOT_AVAILABLE \
|
|
|
|
|| _s == STATUS_PIPE_BUSY; })
|
|
|
|
|
2007-07-07 19:00:33 +02:00
|
|
|
fhandler_fifo::fhandler_fifo ():
|
2019-03-22 20:30:37 +01:00
|
|
|
fhandler_base (), read_ready (NULL), write_ready (NULL),
|
2019-04-14 21:15:56 +02:00
|
|
|
listen_client_thr (NULL), lct_termination_evt (NULL), nhandlers (0),
|
2019-04-15 17:43:57 +02:00
|
|
|
nconnected (0), reader (false), writer (false), duplexer (false),
|
|
|
|
max_atomic_write (DEFAULT_PIPEBUFSIZE)
|
2003-09-20 02:31:13 +02:00
|
|
|
{
|
2019-03-22 20:30:36 +01:00
|
|
|
pipe_name_buf[0] = L'\0';
|
2007-07-07 19:00:33 +02:00
|
|
|
need_fork_fixup (true);
|
2003-09-20 02:31:13 +02:00
|
|
|
}
|
|
|
|
|
2019-03-22 20:30:36 +01:00
|
|
|
PUNICODE_STRING
|
|
|
|
fhandler_fifo::get_pipe_name ()
|
2009-07-24 22:54:33 +02:00
|
|
|
{
|
2019-03-22 20:30:36 +01:00
|
|
|
if (!pipe_name_buf[0])
|
|
|
|
{
|
|
|
|
__small_swprintf (pipe_name_buf, L"%S-fifo.%08x.%016X",
|
|
|
|
&cygheap->installation_key, get_dev (), get_ino ());
|
|
|
|
RtlInitUnicodeString (&pipe_name, pipe_name_buf);
|
|
|
|
}
|
|
|
|
return &pipe_name;
|
2009-07-24 22:54:33 +02:00
|
|
|
}
|
|
|
|
|
2010-01-14 19:46:02 +01:00
|
|
|
inline PSECURITY_ATTRIBUTES
|
|
|
|
sec_user_cloexec (bool cloexec, PSECURITY_ATTRIBUTES sa, PSID sid)
|
|
|
|
{
|
|
|
|
return cloexec ? sec_user_nih (sa, sid) : sec_user (sa, sid);
|
|
|
|
}
|
2005-04-22 15:58:09 +02:00
|
|
|
|
2012-01-22 22:43:25 +01:00
|
|
|
bool inline
|
|
|
|
fhandler_fifo::arm (HANDLE h)
|
|
|
|
{
|
|
|
|
#ifdef DEBUGGING
|
|
|
|
const char *what;
|
|
|
|
if (h == read_ready)
|
|
|
|
what = "reader";
|
|
|
|
else
|
2019-03-22 20:30:36 +01:00
|
|
|
what = "writer";
|
2012-01-22 22:43:25 +01:00
|
|
|
debug_only_printf ("arming %s", what);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool res = SetEvent (h);
|
|
|
|
if (!res)
|
2012-12-07 21:59:44 +01:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
debug_printf ("SetEvent for %s failed, %E", what);
|
|
|
|
#else
|
|
|
|
debug_printf ("SetEvent failed, %E");
|
|
|
|
#endif
|
2012-01-22 22:43:25 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-03-22 20:30:37 +01:00
|
|
|
static HANDLE
|
|
|
|
create_event ()
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
HANDLE evt = NULL;
|
|
|
|
|
|
|
|
InitializeObjectAttributes (&attr, NULL, 0, NULL, NULL);
|
|
|
|
status = NtCreateEvent (&evt, EVENT_ALL_ACCESS, &attr,
|
|
|
|
NotificationEvent, FALSE);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
return evt;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_pipe_non_blocking (HANDLE ph, bool nonblocking)
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
FILE_PIPE_INFORMATION fpi;
|
|
|
|
|
|
|
|
fpi.ReadMode = FILE_PIPE_MESSAGE_MODE;
|
|
|
|
fpi.CompletionMode = nonblocking ? FILE_PIPE_COMPLETE_OPERATION
|
|
|
|
: FILE_PIPE_QUEUE_OPERATION;
|
|
|
|
status = NtSetInformationFile (ph, &io, &fpi, sizeof fpi,
|
|
|
|
FilePipeInformation);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
debug_printf ("NtSetInformationFile(FilePipeInformation): %y", status);
|
|
|
|
}
|
|
|
|
|
2019-03-22 20:30:36 +01:00
|
|
|
NTSTATUS
|
|
|
|
fhandler_fifo::npfs_handle (HANDLE &nph)
|
|
|
|
{
|
|
|
|
static NO_COPY SRWLOCK npfs_lock;
|
|
|
|
static NO_COPY HANDLE npfs_dirh;
|
|
|
|
|
|
|
|
NTSTATUS status = STATUS_SUCCESS;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
|
|
|
|
/* Lockless after first call. */
|
|
|
|
if (npfs_dirh)
|
|
|
|
{
|
|
|
|
nph = npfs_dirh;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
AcquireSRWLockExclusive (&npfs_lock);
|
|
|
|
if (!npfs_dirh)
|
|
|
|
{
|
|
|
|
InitializeObjectAttributes (&attr, &ro_u_npfs, 0, NULL, NULL);
|
|
|
|
status = NtOpenFile (&npfs_dirh, FILE_READ_ATTRIBUTES | SYNCHRONIZE,
|
|
|
|
&attr, &io, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
ReleaseSRWLockExclusive (&npfs_lock);
|
|
|
|
if (NT_SUCCESS (status))
|
|
|
|
nph = npfs_dirh;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2019-03-22 20:30:37 +01:00
|
|
|
/* Called when a FIFO is first opened for reading and again each time
|
2019-04-14 21:15:56 +02:00
|
|
|
a new client handler is needed. Each pipe instance is created in
|
|
|
|
blocking mode so that we can easily wait for a connection. After
|
|
|
|
it is connected, it is put in nonblocking mode. */
|
2019-03-22 20:30:36 +01:00
|
|
|
HANDLE
|
2019-03-22 20:30:37 +01:00
|
|
|
fhandler_fifo::create_pipe_instance (bool first)
|
2019-03-22 20:30:36 +01:00
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
HANDLE npfsh;
|
|
|
|
HANDLE ph = NULL;
|
|
|
|
ACCESS_MASK access;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
ULONG hattr;
|
|
|
|
ULONG sharing;
|
|
|
|
ULONG nonblocking = FILE_PIPE_QUEUE_OPERATION;
|
2019-03-22 20:30:37 +01:00
|
|
|
ULONG max_instances = -1;
|
2019-03-22 20:30:36 +01:00
|
|
|
LARGE_INTEGER timeout;
|
|
|
|
|
|
|
|
status = npfs_handle (npfsh);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
access = GENERIC_READ | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES
|
|
|
|
| SYNCHRONIZE;
|
|
|
|
sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
|
2019-05-28 21:50:05 +02:00
|
|
|
hattr = openflags & O_CLOEXEC ? 0 : OBJ_INHERIT;
|
2019-03-22 20:30:37 +01:00
|
|
|
if (first)
|
|
|
|
hattr |= OBJ_CASE_INSENSITIVE;
|
2019-03-22 20:30:36 +01:00
|
|
|
InitializeObjectAttributes (&attr, get_pipe_name (),
|
|
|
|
hattr, npfsh, NULL);
|
|
|
|
timeout.QuadPart = -500000;
|
|
|
|
status = NtCreateNamedPipeFile (&ph, access, &attr, &io, sharing,
|
2019-03-22 20:30:37 +01:00
|
|
|
first ? FILE_CREATE : FILE_OPEN, 0,
|
2019-03-22 20:30:36 +01:00
|
|
|
FILE_PIPE_MESSAGE_TYPE,
|
|
|
|
FILE_PIPE_MESSAGE_MODE,
|
|
|
|
nonblocking, max_instances,
|
|
|
|
DEFAULT_PIPEBUFSIZE, DEFAULT_PIPEBUFSIZE,
|
|
|
|
&timeout);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
return ph;
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:36:26 +02:00
|
|
|
/* Connect to a pipe instance. */
|
2019-03-22 20:30:36 +01:00
|
|
|
NTSTATUS
|
2019-05-09 17:36:26 +02:00
|
|
|
fhandler_fifo::open_pipe (HANDLE& ph)
|
2019-03-22 20:30:36 +01:00
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
HANDLE npfsh;
|
|
|
|
ACCESS_MASK access;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
ULONG sharing;
|
|
|
|
|
|
|
|
status = npfs_handle (npfsh);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
return status;
|
|
|
|
access = GENERIC_WRITE | SYNCHRONIZE;
|
2019-05-28 21:50:05 +02:00
|
|
|
InitializeObjectAttributes (&attr, get_pipe_name (),
|
|
|
|
openflags & O_CLOEXEC ? 0 : OBJ_INHERIT,
|
2019-03-22 20:30:36 +01:00
|
|
|
npfsh, NULL);
|
|
|
|
sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
|
|
|
|
status = NtOpenFile (&ph, access, &attr, &io, sharing, 0);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2019-03-22 20:30:37 +01:00
|
|
|
int
|
2019-04-14 21:15:56 +02:00
|
|
|
fhandler_fifo::add_client_handler ()
|
2019-03-22 20:30:37 +01:00
|
|
|
{
|
2019-04-14 21:16:04 +02:00
|
|
|
int ret = -1;
|
2019-03-22 20:30:37 +01:00
|
|
|
fifo_client_handler fc;
|
|
|
|
fhandler_base *fh;
|
2019-04-14 21:16:04 +02:00
|
|
|
HANDLE ph = NULL;
|
2019-04-14 21:15:56 +02:00
|
|
|
bool first = (nhandlers == 0);
|
2019-03-22 20:30:37 +01:00
|
|
|
|
2019-04-14 21:15:56 +02:00
|
|
|
if (nhandlers == MAX_CLIENTS)
|
2019-03-22 20:30:37 +01:00
|
|
|
{
|
|
|
|
set_errno (EMFILE);
|
2019-04-14 21:16:04 +02:00
|
|
|
goto out;
|
2019-03-22 20:30:37 +01:00
|
|
|
}
|
2019-04-14 21:16:04 +02:00
|
|
|
if (!(fc.connect_evt = create_event ()))
|
|
|
|
goto out;
|
2019-03-22 20:30:37 +01:00
|
|
|
if (!(fh = build_fh_dev (dev ())))
|
|
|
|
{
|
|
|
|
set_errno (EMFILE);
|
2019-04-14 21:16:04 +02:00
|
|
|
goto out;
|
2019-03-22 20:30:37 +01:00
|
|
|
}
|
2019-04-14 21:16:04 +02:00
|
|
|
ph = create_pipe_instance (first);
|
2019-03-22 20:30:37 +01:00
|
|
|
if (!ph)
|
|
|
|
{
|
2019-04-14 21:16:04 +02:00
|
|
|
delete fh;
|
|
|
|
goto out;
|
2019-03-22 20:30:37 +01:00
|
|
|
}
|
2019-04-14 21:16:04 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fh->set_handle (ph);
|
2019-05-09 18:04:14 +02:00
|
|
|
fh->set_flags ((openflags & ~O_ACCMODE) | O_RDONLY);
|
|
|
|
fh->set_nonblocking (false);
|
2019-04-14 21:16:04 +02:00
|
|
|
ret = 0;
|
|
|
|
fc.fh = fh;
|
2019-06-22 16:07:48 +02:00
|
|
|
fifo_client_lock ();
|
2019-04-14 21:16:04 +02:00
|
|
|
fc_handler[nhandlers++] = fc;
|
2019-06-22 16:07:48 +02:00
|
|
|
fifo_client_unlock ();
|
2019-04-14 21:16:04 +02:00
|
|
|
}
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
2019-03-22 20:30:37 +01:00
|
|
|
|
2019-06-22 17:46:49 +02:00
|
|
|
int
|
2019-04-14 21:16:04 +02:00
|
|
|
fhandler_fifo::delete_client_handler (int i)
|
|
|
|
{
|
2019-06-22 17:46:49 +02:00
|
|
|
int ret = fc_handler[i].close ();
|
2019-04-14 21:16:04 +02:00
|
|
|
if (i < --nhandlers)
|
|
|
|
memmove (fc_handler + i, fc_handler + i + 1,
|
|
|
|
(nhandlers - i) * sizeof (fc_handler[i]));
|
2019-06-22 17:46:49 +02:00
|
|
|
return ret;
|
2019-03-22 20:30:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Just hop to the listen_client_thread method. */
|
|
|
|
DWORD WINAPI
|
|
|
|
listen_client_func (LPVOID param)
|
|
|
|
{
|
|
|
|
fhandler_fifo *fh = (fhandler_fifo *) param;
|
|
|
|
return fh->listen_client_thread ();
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:55:30 +02:00
|
|
|
/* Start a thread that listens for client connections. */
|
2019-03-22 20:30:37 +01:00
|
|
|
bool
|
|
|
|
fhandler_fifo::listen_client ()
|
|
|
|
{
|
|
|
|
if (!(lct_termination_evt = create_event ()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
listen_client_thr = CreateThread (NULL, PREFERRED_IO_BLKSIZE,
|
|
|
|
listen_client_func, (PVOID) this, 0, NULL);
|
|
|
|
if (!listen_client_thr)
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
HANDLE evt = InterlockedExchangePointer (&lct_termination_evt, NULL);
|
|
|
|
if (evt)
|
|
|
|
CloseHandle (evt);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-09 16:58:29 +02:00
|
|
|
void
|
|
|
|
fhandler_fifo::record_connection (fifo_client_handler& fc)
|
|
|
|
{
|
|
|
|
fc.state = fc_connected;
|
|
|
|
nconnected++;
|
2019-05-09 18:04:14 +02:00
|
|
|
fc.fh->set_nonblocking (true);
|
2019-05-09 16:58:29 +02:00
|
|
|
set_pipe_non_blocking (fc.fh->get_handle (), true);
|
|
|
|
HANDLE evt = InterlockedExchangePointer (&fc.connect_evt, NULL);
|
|
|
|
if (evt)
|
|
|
|
CloseHandle (evt);
|
|
|
|
}
|
|
|
|
|
2019-03-22 20:30:37 +01:00
|
|
|
DWORD
|
|
|
|
fhandler_fifo::listen_client_thread ()
|
|
|
|
{
|
2019-04-14 21:16:01 +02:00
|
|
|
DWORD ret = -1;
|
|
|
|
|
2019-03-22 20:30:37 +01:00
|
|
|
while (1)
|
|
|
|
{
|
2019-04-14 21:16:04 +02:00
|
|
|
/* At the beginning of the loop, all client handlers are
|
|
|
|
in the fc_connected or fc_invalid state. */
|
2019-03-22 20:30:37 +01:00
|
|
|
|
2019-04-14 21:16:04 +02:00
|
|
|
/* Delete any invalid clients. */
|
2019-03-22 20:30:38 +01:00
|
|
|
fifo_client_lock ();
|
2019-04-14 21:16:04 +02:00
|
|
|
int i = 0;
|
|
|
|
while (i < nhandlers)
|
|
|
|
{
|
|
|
|
if (fc_handler[i].state == fc_invalid)
|
2019-06-22 17:46:49 +02:00
|
|
|
{
|
|
|
|
if (delete_client_handler (i) < 0)
|
|
|
|
{
|
|
|
|
fifo_client_unlock ();
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2019-04-14 21:16:04 +02:00
|
|
|
else
|
|
|
|
i++;
|
|
|
|
}
|
2019-06-22 16:07:48 +02:00
|
|
|
fifo_client_unlock ();
|
2019-04-14 21:16:04 +02:00
|
|
|
|
|
|
|
/* Create a new client handler. */
|
|
|
|
if (add_client_handler () < 0)
|
2019-06-22 16:07:48 +02:00
|
|
|
goto out;
|
2019-03-22 20:30:38 +01:00
|
|
|
|
2019-04-14 21:16:01 +02:00
|
|
|
/* Allow a writer to open. */
|
2019-03-22 20:30:37 +01:00
|
|
|
if (!arm (read_ready))
|
|
|
|
{
|
|
|
|
__seterrno ();
|
2019-04-14 21:16:01 +02:00
|
|
|
goto out;
|
2019-03-22 20:30:37 +01:00
|
|
|
}
|
|
|
|
|
2019-04-14 21:16:04 +02:00
|
|
|
/* Listen for a writer to connect to the new client handler. */
|
|
|
|
fifo_client_handler& fc = fc_handler[nhandlers - 1];
|
2019-05-09 17:55:30 +02:00
|
|
|
NTSTATUS status;
|
|
|
|
IO_STATUS_BLOCK io;
|
2019-04-14 21:16:04 +02:00
|
|
|
|
2019-05-09 17:55:30 +02:00
|
|
|
status = NtFsControlFile (fc.fh->get_handle (), fc.connect_evt,
|
|
|
|
NULL, NULL, &io, FSCTL_PIPE_LISTEN,
|
|
|
|
NULL, 0, NULL, 0);
|
|
|
|
if (status == STATUS_PENDING)
|
|
|
|
{
|
|
|
|
HANDLE w[2] = { fc.connect_evt, lct_termination_evt };
|
|
|
|
DWORD waitret = WaitForMultipleObjects (2, w, false, INFINITE);
|
|
|
|
switch (waitret)
|
2019-04-14 21:16:04 +02:00
|
|
|
{
|
2019-05-09 17:55:30 +02:00
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
status = io.Status;
|
2019-04-14 21:16:04 +02:00
|
|
|
break;
|
2019-05-09 17:55:30 +02:00
|
|
|
case WAIT_OBJECT_0 + 1:
|
|
|
|
ret = 0;
|
|
|
|
status = STATUS_THREAD_IS_TERMINATING;
|
2019-04-14 21:16:04 +02:00
|
|
|
break;
|
|
|
|
default:
|
2019-05-09 17:55:30 +02:00
|
|
|
__seterrno ();
|
|
|
|
debug_printf ("WaitForMultipleObjects failed, %E");
|
|
|
|
status = STATUS_THREAD_IS_TERMINATING;
|
|
|
|
break;
|
2019-04-14 21:16:04 +02:00
|
|
|
}
|
2019-05-09 17:55:30 +02:00
|
|
|
}
|
|
|
|
HANDLE ph = NULL;
|
2019-06-22 16:07:48 +02:00
|
|
|
fifo_client_lock ();
|
2019-05-09 17:55:30 +02:00
|
|
|
switch (status)
|
|
|
|
{
|
|
|
|
case STATUS_SUCCESS:
|
|
|
|
case STATUS_PIPE_CONNECTED:
|
|
|
|
record_connection (fc);
|
|
|
|
break;
|
|
|
|
case STATUS_THREAD_IS_TERMINATING:
|
|
|
|
/* Try to cancel the pending listen. Otherwise the first
|
|
|
|
writer to connect after the thread is restarted will be
|
|
|
|
invisible.
|
|
|
|
|
|
|
|
FIXME: Is there a direct way to do this? We do it by
|
|
|
|
opening and closing a write handle to the client side. */
|
|
|
|
open_pipe (ph);
|
|
|
|
/* We don't care about the return value of open_pipe. Even
|
|
|
|
if the latter failed, a writer might have connected. */
|
|
|
|
if (WaitForSingleObject (fc.connect_evt, 0) == WAIT_OBJECT_0
|
|
|
|
&& (NT_SUCCESS (io.Status) || io.Status == STATUS_PIPE_CONNECTED))
|
|
|
|
record_connection (fc);
|
|
|
|
else
|
|
|
|
fc.state = fc_invalid;
|
|
|
|
/* By closing ph we ensure that if fc connected to ph, fc
|
|
|
|
will be declared invalid on the next read attempt. */
|
|
|
|
if (ph)
|
|
|
|
CloseHandle (ph);
|
2019-06-22 16:07:48 +02:00
|
|
|
fifo_client_unlock ();
|
2019-05-09 17:55:30 +02:00
|
|
|
goto out;
|
|
|
|
default:
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
fc.state = fc_invalid;
|
2019-06-22 16:07:48 +02:00
|
|
|
fifo_client_unlock ();
|
2019-05-09 17:55:30 +02:00
|
|
|
goto out;
|
|
|
|
}
|
2019-06-22 16:07:48 +02:00
|
|
|
fifo_client_unlock ();
|
2019-04-14 21:16:04 +02:00
|
|
|
/* Check for thread termination in case WaitForMultipleObjects
|
|
|
|
didn't get called above. */
|
|
|
|
if (IsEventSignalled (lct_termination_evt))
|
2019-04-14 21:16:01 +02:00
|
|
|
{
|
|
|
|
ret = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-03-22 20:30:37 +01:00
|
|
|
}
|
2019-04-14 21:16:01 +02:00
|
|
|
out:
|
2019-04-20 17:31:08 +02:00
|
|
|
if (ret < 0)
|
|
|
|
debug_printf ("exiting lct with error, %E");
|
2019-03-22 20:30:37 +01:00
|
|
|
ResetEvent (read_ready);
|
2019-04-14 21:16:01 +02:00
|
|
|
return ret;
|
2019-03-22 20:30:37 +01:00
|
|
|
}
|
|
|
|
|
2003-09-20 02:31:13 +02:00
|
|
|
int
|
2007-07-07 19:00:33 +02:00
|
|
|
fhandler_fifo::open (int flags, mode_t)
|
2003-09-20 02:31:13 +02:00
|
|
|
{
|
2011-10-30 05:50:36 +01:00
|
|
|
enum
|
|
|
|
{
|
2019-03-22 20:30:36 +01:00
|
|
|
success,
|
|
|
|
error_errno_set,
|
|
|
|
error_set_errno
|
2011-10-30 05:50:36 +01:00
|
|
|
} res;
|
2007-07-07 19:00:33 +02:00
|
|
|
|
2011-10-30 05:50:36 +01:00
|
|
|
/* Determine what we're doing with this fhandler: reading, writing, both */
|
|
|
|
switch (flags & O_ACCMODE)
|
2003-09-20 02:31:13 +02:00
|
|
|
{
|
2011-10-30 05:50:36 +01:00
|
|
|
case O_RDONLY:
|
|
|
|
reader = true;
|
|
|
|
break;
|
|
|
|
case O_WRONLY:
|
|
|
|
writer = true;
|
|
|
|
break;
|
|
|
|
case O_RDWR:
|
2012-01-22 22:43:25 +01:00
|
|
|
reader = true;
|
2019-04-14 21:15:57 +02:00
|
|
|
duplexer = true;
|
2011-10-30 05:50:36 +01:00
|
|
|
break;
|
|
|
|
default:
|
2007-07-07 19:00:33 +02:00
|
|
|
set_errno (EINVAL);
|
2011-10-30 05:50:36 +01:00
|
|
|
res = error_errno_set;
|
|
|
|
goto out;
|
2007-07-07 19:00:33 +02:00
|
|
|
}
|
2009-07-24 22:54:33 +02:00
|
|
|
|
2012-01-22 22:43:25 +01:00
|
|
|
debug_only_printf ("reader %d, writer %d, duplexer %d", reader, writer, duplexer);
|
2011-10-30 05:50:36 +01:00
|
|
|
set_flags (flags);
|
2019-03-26 00:06:10 +01:00
|
|
|
if (reader && !duplexer)
|
2019-03-22 20:30:37 +01:00
|
|
|
nohandle (true);
|
|
|
|
|
2019-03-22 20:30:36 +01:00
|
|
|
/* Create control events for this named pipe */
|
2011-10-30 05:50:36 +01:00
|
|
|
char char_sa_buf[1024];
|
|
|
|
LPSECURITY_ATTRIBUTES sa_buf;
|
|
|
|
sa_buf = sec_user_cloexec (flags & O_CLOEXEC, (PSECURITY_ATTRIBUTES) char_sa_buf,
|
|
|
|
cygheap->user.sid());
|
|
|
|
|
2019-03-22 20:30:36 +01:00
|
|
|
char npbuf[MAX_PATH];
|
|
|
|
__small_sprintf (npbuf, "r-event.%08x.%016X", get_dev (), get_ino ());
|
2019-05-09 18:32:17 +02:00
|
|
|
if (!(read_ready = CreateEvent (sa_buf, false, false, npbuf)))
|
2011-10-30 05:50:36 +01:00
|
|
|
{
|
2019-03-22 20:30:36 +01:00
|
|
|
debug_printf ("CreateEvent for %s failed, %E", npbuf);
|
2011-10-30 05:50:36 +01:00
|
|
|
res = error_set_errno;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-03-22 20:30:36 +01:00
|
|
|
npbuf[0] = 'w';
|
|
|
|
if (!(write_ready = CreateEvent (sa_buf, false, false, npbuf)))
|
2007-07-07 19:00:33 +02:00
|
|
|
{
|
2019-03-22 20:30:36 +01:00
|
|
|
debug_printf ("CreateEvent for %s failed, %E", npbuf);
|
2011-10-30 05:50:36 +01:00
|
|
|
res = error_set_errno;
|
|
|
|
goto out;
|
|
|
|
}
|
2008-11-26 18:21:04 +01:00
|
|
|
|
2019-04-14 21:15:56 +02:00
|
|
|
/* If we're a duplexer, create the pipe and the first client handler. */
|
2019-03-26 00:06:10 +01:00
|
|
|
if (duplexer)
|
|
|
|
{
|
2019-05-09 17:36:26 +02:00
|
|
|
HANDLE ph = NULL;
|
|
|
|
|
2019-05-09 17:23:44 +02:00
|
|
|
if (add_client_handler () < 0)
|
2019-03-26 00:06:10 +01:00
|
|
|
{
|
|
|
|
res = error_errno_set;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-05-09 17:36:26 +02:00
|
|
|
NTSTATUS status = open_pipe (ph);
|
2019-05-09 17:23:44 +02:00
|
|
|
if (NT_SUCCESS (status))
|
2019-04-20 17:49:35 +02:00
|
|
|
{
|
2019-05-09 17:23:44 +02:00
|
|
|
record_connection (fc_handler[0]);
|
2019-05-09 17:36:26 +02:00
|
|
|
set_handle (ph);
|
|
|
|
set_pipe_non_blocking (ph, flags & O_NONBLOCK);
|
2019-04-20 17:49:35 +02:00
|
|
|
}
|
2019-05-09 17:23:44 +02:00
|
|
|
else
|
2019-03-26 00:06:10 +01:00
|
|
|
{
|
2019-05-09 17:23:44 +02:00
|
|
|
__seterrno_from_nt_status (status);
|
2019-03-26 00:06:10 +01:00
|
|
|
res = error_errno_set;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-22 20:30:37 +01:00
|
|
|
/* If we're reading, start the listen_client thread (which should
|
|
|
|
signal read_ready), and wait for a writer. */
|
2019-03-22 20:30:36 +01:00
|
|
|
if (reader)
|
2011-10-30 05:50:36 +01:00
|
|
|
{
|
2019-03-22 20:30:37 +01:00
|
|
|
if (!listen_client ())
|
2019-03-22 20:30:36 +01:00
|
|
|
{
|
2019-03-22 20:30:37 +01:00
|
|
|
debug_printf ("create of listen_client thread failed");
|
2019-03-22 20:30:36 +01:00
|
|
|
res = error_errno_set;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-04-14 21:16:01 +02:00
|
|
|
else if (!duplexer && !wait (write_ready))
|
2019-03-22 20:30:36 +01:00
|
|
|
{
|
|
|
|
res = error_errno_set;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
else
|
2019-04-20 17:41:12 +02:00
|
|
|
{
|
|
|
|
init_fixup_before ();
|
|
|
|
res = success;
|
|
|
|
}
|
2011-10-30 05:50:36 +01:00
|
|
|
}
|
|
|
|
|
2019-03-22 20:30:36 +01:00
|
|
|
/* If we're writing, wait for read_ready and then connect to the
|
2019-03-22 20:30:37 +01:00
|
|
|
pipe. This should always succeed quickly if the reader's
|
|
|
|
listen_client thread is running. Then signal write_ready. */
|
2011-10-30 05:50:36 +01:00
|
|
|
if (writer)
|
|
|
|
{
|
2019-04-14 21:16:02 +02:00
|
|
|
while (1)
|
2019-03-22 20:30:37 +01:00
|
|
|
{
|
2019-04-14 21:16:02 +02:00
|
|
|
if (!wait (read_ready))
|
|
|
|
{
|
|
|
|
res = error_errno_set;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-05-09 17:36:26 +02:00
|
|
|
NTSTATUS status = open_pipe (get_handle ());
|
2019-04-14 21:16:02 +02:00
|
|
|
if (NT_SUCCESS (status))
|
|
|
|
{
|
2019-04-15 17:43:57 +02:00
|
|
|
set_pipe_non_blocking (get_handle (), flags & O_NONBLOCK);
|
2019-04-14 21:16:02 +02:00
|
|
|
if (!arm (write_ready))
|
|
|
|
res = error_set_errno;
|
|
|
|
else
|
|
|
|
res = success;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
else if (STATUS_PIPE_NO_INSTANCE_AVAILABLE (status))
|
|
|
|
Sleep (1);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
debug_printf ("create of writer failed");
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
res = error_errno_set;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-03-22 20:30:37 +01:00
|
|
|
}
|
2011-10-30 05:50:36 +01:00
|
|
|
}
|
|
|
|
out:
|
|
|
|
if (res == error_set_errno)
|
|
|
|
__seterrno ();
|
|
|
|
if (res != success)
|
|
|
|
{
|
|
|
|
if (read_ready)
|
2007-07-07 19:00:33 +02:00
|
|
|
{
|
2011-10-30 05:50:36 +01:00
|
|
|
CloseHandle (read_ready);
|
|
|
|
read_ready = NULL;
|
2007-07-07 19:00:33 +02:00
|
|
|
}
|
2011-10-30 05:50:36 +01:00
|
|
|
if (write_ready)
|
2007-07-07 19:00:33 +02:00
|
|
|
{
|
2011-10-30 05:50:36 +01:00
|
|
|
CloseHandle (write_ready);
|
|
|
|
write_ready = NULL;
|
2003-09-20 02:31:13 +02:00
|
|
|
}
|
2019-03-30 08:12:02 +01:00
|
|
|
if (get_handle ())
|
|
|
|
CloseHandle (get_handle ());
|
2019-03-22 20:30:37 +01:00
|
|
|
if (listen_client_thr)
|
2019-04-20 17:44:01 +02:00
|
|
|
stop_listen_client ();
|
2003-09-20 02:31:13 +02:00
|
|
|
}
|
2011-10-30 05:50:36 +01:00
|
|
|
debug_printf ("res %d", res);
|
|
|
|
return res == success;
|
2003-09-20 02:31:13 +02:00
|
|
|
}
|
|
|
|
|
2018-10-10 13:20:04 +02:00
|
|
|
off_t
|
|
|
|
fhandler_fifo::lseek (off_t offset, int whence)
|
|
|
|
{
|
|
|
|
debug_printf ("(%D, %d)", offset, whence);
|
|
|
|
set_errno (ESPIPE);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-07-07 19:00:33 +02:00
|
|
|
bool
|
2011-10-30 05:50:36 +01:00
|
|
|
fhandler_fifo::wait (HANDLE h)
|
2003-09-20 02:31:13 +02:00
|
|
|
{
|
2011-10-30 05:50:36 +01:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
const char *what;
|
|
|
|
if (h == read_ready)
|
|
|
|
what = "reader";
|
|
|
|
else
|
2019-03-22 20:30:36 +01:00
|
|
|
what = "writer";
|
2011-10-30 05:50:36 +01:00
|
|
|
#endif
|
|
|
|
/* Set the wait to zero for non-blocking I/O-related events. */
|
|
|
|
DWORD wait = ((h == read_ready || h == write_ready)
|
|
|
|
&& get_flags () & O_NONBLOCK) ? 0 : INFINITE;
|
|
|
|
|
|
|
|
debug_only_printf ("waiting for %s", what);
|
|
|
|
/* Wait for the event. Set errno, as appropriate if something goes wrong. */
|
2011-12-09 17:02:56 +01:00
|
|
|
switch (cygwait (h, wait))
|
2006-06-23 02:19:39 +02:00
|
|
|
{
|
2011-10-30 05:50:36 +01:00
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
debug_only_printf ("successfully waited for %s", what);
|
|
|
|
return true;
|
2012-06-17 22:50:24 +02:00
|
|
|
case WAIT_SIGNALED:
|
|
|
|
debug_only_printf ("interrupted by signal while waiting for %s", what);
|
|
|
|
set_errno (EINTR);
|
|
|
|
return false;
|
|
|
|
case WAIT_CANCELED:
|
|
|
|
debug_only_printf ("cancellable interruption while waiting for %s", what);
|
|
|
|
pthread::static_cancel_self (); /* never returns */
|
|
|
|
break;
|
2011-10-30 05:50:36 +01:00
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
if (h == write_ready)
|
2009-07-24 22:54:33 +02:00
|
|
|
{
|
2011-10-30 05:50:36 +01:00
|
|
|
debug_only_printf ("wait timed out waiting for write but will still open reader since non-blocking mode");
|
|
|
|
return true;
|
2009-07-24 22:54:33 +02:00
|
|
|
}
|
2011-10-30 05:50:36 +01:00
|
|
|
else
|
2009-07-24 22:54:33 +02:00
|
|
|
{
|
2011-10-30 05:50:36 +01:00
|
|
|
set_errno (ENXIO);
|
2009-07-24 22:54:33 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2011-10-30 05:50:36 +01:00
|
|
|
default:
|
|
|
|
debug_only_printf ("unknown error while waiting for %s", what);
|
|
|
|
__seterrno ();
|
|
|
|
return false;
|
|
|
|
}
|
2007-07-07 19:00:33 +02:00
|
|
|
}
|
2006-06-23 02:19:39 +02:00
|
|
|
|
2019-03-22 20:30:36 +01:00
|
|
|
ssize_t __reg3
|
|
|
|
fhandler_fifo::raw_write (const void *ptr, size_t len)
|
|
|
|
{
|
|
|
|
ssize_t ret = -1;
|
2019-06-08 17:05:39 +02:00
|
|
|
size_t nbytes = 0;
|
|
|
|
ULONG chunk;
|
2019-04-15 17:43:57 +02:00
|
|
|
NTSTATUS status = STATUS_SUCCESS;
|
2019-03-22 20:30:36 +01:00
|
|
|
IO_STATUS_BLOCK io;
|
2019-04-15 17:43:57 +02:00
|
|
|
HANDLE evt = NULL;
|
2019-03-22 20:30:36 +01:00
|
|
|
|
2019-06-08 17:05:39 +02:00
|
|
|
if (!len)
|
|
|
|
return 0;
|
|
|
|
|
2019-04-15 17:43:57 +02:00
|
|
|
if (len <= max_atomic_write)
|
|
|
|
chunk = len;
|
|
|
|
else if (is_nonblocking ())
|
|
|
|
chunk = len = max_atomic_write;
|
|
|
|
else
|
|
|
|
chunk = max_atomic_write;
|
|
|
|
|
|
|
|
/* Create a wait event if the FIFO is in blocking mode. */
|
|
|
|
if (!is_nonblocking () && !(evt = CreateEvent (NULL, false, false, NULL)))
|
2019-06-08 17:05:39 +02:00
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
|
|
|
}
|
2019-04-15 17:43:57 +02:00
|
|
|
|
|
|
|
/* Write in chunks, accumulating a total. If there's an error, just
|
|
|
|
return the accumulated total unless the first write fails, in
|
|
|
|
which case return -1. */
|
|
|
|
while (nbytes < len)
|
2019-03-22 20:30:36 +01:00
|
|
|
{
|
2019-04-15 17:43:57 +02:00
|
|
|
ULONG_PTR nbytes_now = 0;
|
|
|
|
size_t left = len - nbytes;
|
2019-06-08 17:05:39 +02:00
|
|
|
ULONG len1;
|
|
|
|
DWORD waitret = WAIT_OBJECT_0;
|
|
|
|
|
2019-04-15 17:43:57 +02:00
|
|
|
if (left > chunk)
|
|
|
|
len1 = chunk;
|
2019-03-22 20:30:36 +01:00
|
|
|
else
|
2019-06-08 17:05:39 +02:00
|
|
|
len1 = (ULONG) left;
|
2019-04-15 17:43:57 +02:00
|
|
|
nbytes_now = 0;
|
|
|
|
status = NtWriteFile (get_handle (), evt, NULL, NULL, &io,
|
|
|
|
(PVOID) ptr, len1, NULL, NULL);
|
|
|
|
if (evt && status == STATUS_PENDING)
|
|
|
|
{
|
2019-06-08 17:05:39 +02:00
|
|
|
waitret = cygwait (evt);
|
|
|
|
if (waitret == WAIT_OBJECT_0)
|
|
|
|
status = io.Status;
|
2019-04-15 17:43:57 +02:00
|
|
|
}
|
2019-06-08 17:05:39 +02:00
|
|
|
if (waitret == WAIT_CANCELED)
|
|
|
|
status = STATUS_THREAD_CANCELED;
|
|
|
|
else if (waitret == WAIT_SIGNALED)
|
|
|
|
status = STATUS_THREAD_SIGNALED;
|
|
|
|
else if (isclosed ()) /* A signal handler might have closed the fd. */
|
|
|
|
{
|
|
|
|
if (waitret == WAIT_OBJECT_0)
|
|
|
|
set_errno (EBADF);
|
|
|
|
else
|
|
|
|
__seterrno ();
|
|
|
|
len = (size_t) -1;
|
|
|
|
}
|
|
|
|
else if (NT_SUCCESS (status))
|
2019-04-15 17:43:57 +02:00
|
|
|
{
|
|
|
|
nbytes_now = io.Information;
|
|
|
|
/* NtWriteFile returns success with # of bytes written == 0
|
|
|
|
if writing on a non-blocking pipe fails because the pipe
|
|
|
|
buffer doesn't have sufficient space. */
|
|
|
|
if (nbytes_now == 0)
|
|
|
|
set_errno (EAGAIN);
|
|
|
|
ptr = ((char *) ptr) + chunk;
|
|
|
|
nbytes += nbytes_now;
|
|
|
|
}
|
|
|
|
else if (STATUS_PIPE_IS_CLOSED (status))
|
|
|
|
{
|
|
|
|
set_errno (EPIPE);
|
|
|
|
raise (SIGPIPE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
if (nbytes_now == 0)
|
|
|
|
len = 0; /* Terminate loop. */
|
|
|
|
if (nbytes > 0)
|
|
|
|
ret = nbytes;
|
2019-03-22 20:30:36 +01:00
|
|
|
}
|
2019-04-15 17:43:57 +02:00
|
|
|
if (evt)
|
|
|
|
CloseHandle (evt);
|
2019-06-08 17:05:39 +02:00
|
|
|
if (status == STATUS_THREAD_SIGNALED && ret < 0)
|
2019-04-15 17:43:57 +02:00
|
|
|
set_errno (EINTR);
|
|
|
|
else if (status == STATUS_THREAD_CANCELED)
|
|
|
|
pthread::static_cancel_self ();
|
2019-03-22 20:30:36 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-03-22 20:30:38 +01:00
|
|
|
/* A FIFO open for reading is at EOF if no process has it open for
|
|
|
|
writing. We test this by checking nconnected. But we must take
|
|
|
|
account of the possible delay from the time of connection to the
|
|
|
|
time the connection is recorded by the listen_client thread. */
|
|
|
|
bool
|
|
|
|
fhandler_fifo::hit_eof ()
|
|
|
|
{
|
2019-04-14 21:15:56 +02:00
|
|
|
bool eof;
|
|
|
|
bool retry = true;
|
|
|
|
|
|
|
|
retry:
|
|
|
|
fifo_client_lock ();
|
2019-03-22 20:30:38 +01:00
|
|
|
eof = (nconnected == 0);
|
2019-04-14 21:15:56 +02:00
|
|
|
fifo_client_unlock ();
|
|
|
|
if (eof && retry)
|
|
|
|
{
|
|
|
|
retry = false;
|
|
|
|
/* Give the listen_client thread time to catch up. */
|
|
|
|
Sleep (1);
|
|
|
|
goto retry;
|
|
|
|
}
|
2019-03-22 20:30:38 +01:00
|
|
|
return eof;
|
|
|
|
}
|
|
|
|
|
2019-05-09 18:28:21 +02:00
|
|
|
/* Is the lct running? */
|
|
|
|
int
|
|
|
|
fhandler_fifo::check_listen_client_thread ()
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (listen_client_thr)
|
|
|
|
{
|
|
|
|
DWORD waitret = WaitForSingleObject (listen_client_thr, 0);
|
|
|
|
switch (waitret)
|
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
CloseHandle (listen_client_thr);
|
|
|
|
break;
|
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
debug_printf ("WaitForSingleObject failed, %E");
|
|
|
|
ret = -1;
|
|
|
|
__seterrno ();
|
|
|
|
CloseHandle (listen_client_thr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-05-01 03:20:37 +02:00
|
|
|
void __reg3
|
2009-03-27 16:04:42 +01:00
|
|
|
fhandler_fifo::raw_read (void *in_ptr, size_t& len)
|
2007-07-07 19:00:33 +02:00
|
|
|
{
|
2011-10-30 05:50:36 +01:00
|
|
|
size_t orig_len = len;
|
2019-03-22 20:30:37 +01:00
|
|
|
|
2019-05-09 18:28:21 +02:00
|
|
|
/* Make sure the lct is running. */
|
|
|
|
int res = check_listen_client_thread ();
|
|
|
|
debug_printf ("lct status %d", res);
|
|
|
|
if (res < 0 || (res == 0 && !listen_client ()))
|
2019-03-22 20:30:37 +01:00
|
|
|
goto errout;
|
|
|
|
|
2019-06-08 17:05:39 +02:00
|
|
|
if (!len)
|
|
|
|
return;
|
|
|
|
|
2019-03-22 20:30:36 +01:00
|
|
|
while (1)
|
2011-10-30 05:50:36 +01:00
|
|
|
{
|
2019-03-22 20:30:38 +01:00
|
|
|
if (hit_eof ())
|
2011-10-30 05:50:36 +01:00
|
|
|
{
|
2019-03-22 20:30:37 +01:00
|
|
|
len = 0;
|
|
|
|
return;
|
2011-10-30 05:50:36 +01:00
|
|
|
}
|
2019-03-22 20:30:37 +01:00
|
|
|
|
|
|
|
/* Poll the connected clients for input. */
|
2019-03-22 20:30:38 +01:00
|
|
|
fifo_client_lock ();
|
2019-04-14 21:15:56 +02:00
|
|
|
for (int i = 0; i < nhandlers; i++)
|
|
|
|
if (fc_handler[i].state == fc_connected)
|
2019-03-22 20:30:37 +01:00
|
|
|
{
|
|
|
|
len = orig_len;
|
2019-04-14 21:15:56 +02:00
|
|
|
fc_handler[i].fh->fhandler_base::raw_read (in_ptr, len);
|
2019-03-22 20:30:37 +01:00
|
|
|
ssize_t nread = (ssize_t) len;
|
|
|
|
if (nread > 0)
|
2019-03-22 20:30:38 +01:00
|
|
|
{
|
|
|
|
fifo_client_unlock ();
|
|
|
|
return;
|
|
|
|
}
|
2019-05-09 16:47:47 +02:00
|
|
|
/* If the pipe is empty, we get nread == -1 with
|
|
|
|
ERROR_NO_DATA. */
|
|
|
|
else if (nread < 0 && GetLastError () != ERROR_NO_DATA)
|
|
|
|
{
|
|
|
|
fifo_client_unlock ();
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
else if (nread == 0)
|
2019-03-26 00:06:10 +01:00
|
|
|
/* Client has disconnected. */
|
2019-03-22 20:30:37 +01:00
|
|
|
{
|
2019-04-14 21:15:56 +02:00
|
|
|
fc_handler[i].state = fc_invalid;
|
2019-03-22 20:30:37 +01:00
|
|
|
nconnected--;
|
|
|
|
}
|
|
|
|
}
|
2019-03-22 20:30:38 +01:00
|
|
|
fifo_client_unlock ();
|
2019-03-22 20:30:36 +01:00
|
|
|
if (is_nonblocking ())
|
2011-10-30 05:50:36 +01:00
|
|
|
{
|
2019-03-22 20:30:36 +01:00
|
|
|
set_errno (EAGAIN);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-06-08 17:05:39 +02:00
|
|
|
/* Allow interruption. */
|
|
|
|
DWORD waitret = cygwait (NULL, cw_nowait, cw_cancel | cw_sig_eintr);
|
|
|
|
if (waitret == WAIT_CANCELED)
|
|
|
|
pthread::static_cancel_self ();
|
|
|
|
else if (waitret == WAIT_SIGNALED)
|
2019-03-22 20:30:36 +01:00
|
|
|
{
|
2019-06-08 17:05:39 +02:00
|
|
|
if (_my_tls.call_signal_handler ())
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
set_errno (EINTR);
|
|
|
|
goto errout;
|
|
|
|
}
|
2019-03-22 20:30:36 +01:00
|
|
|
}
|
2011-10-30 05:50:36 +01:00
|
|
|
}
|
2019-06-08 17:05:39 +02:00
|
|
|
/* We might have been closed by a signal handler or another thread. */
|
|
|
|
if (isclosed ())
|
|
|
|
{
|
|
|
|
set_errno (EBADF);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
/* Don't hog the CPU. */
|
|
|
|
Sleep (1);
|
2011-10-30 05:50:36 +01:00
|
|
|
}
|
|
|
|
errout:
|
2019-06-08 17:05:39 +02:00
|
|
|
len = (size_t) -1;
|
2003-09-20 02:31:13 +02:00
|
|
|
}
|
2007-02-27 13:58:56 +01:00
|
|
|
|
2013-01-21 05:34:52 +01:00
|
|
|
int __reg2
|
2007-02-27 13:58:56 +01:00
|
|
|
fhandler_fifo::fstatvfs (struct statvfs *sfs)
|
|
|
|
{
|
2007-12-04 14:29:44 +01:00
|
|
|
fhandler_disk_file fh (pc);
|
|
|
|
fh.get_device () = FH_FS;
|
|
|
|
return fh.fstatvfs (sfs);
|
2007-02-27 13:58:56 +01:00
|
|
|
}
|
2009-07-24 22:54:33 +02:00
|
|
|
|
2019-03-22 20:30:37 +01:00
|
|
|
int
|
|
|
|
fifo_client_handler::close ()
|
|
|
|
{
|
|
|
|
int res = 0;
|
2019-04-20 17:31:08 +02:00
|
|
|
HANDLE evt = InterlockedExchangePointer (&connect_evt, NULL);
|
2019-03-22 20:30:37 +01:00
|
|
|
|
2019-04-20 17:31:08 +02:00
|
|
|
if (evt)
|
|
|
|
CloseHandle (evt);
|
2019-03-22 20:30:37 +01:00
|
|
|
if (fh)
|
2019-04-14 21:16:00 +02:00
|
|
|
{
|
|
|
|
res = fh->fhandler_base::close ();
|
|
|
|
delete fh;
|
|
|
|
}
|
2019-03-22 20:30:37 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2009-07-24 22:54:33 +02:00
|
|
|
int
|
2019-04-14 21:16:02 +02:00
|
|
|
fhandler_fifo::stop_listen_client ()
|
2009-07-24 22:54:33 +02:00
|
|
|
{
|
2019-04-14 21:16:02 +02:00
|
|
|
int ret = 0;
|
2019-04-20 17:22:29 +02:00
|
|
|
HANDLE thr, evt;
|
|
|
|
|
|
|
|
thr = InterlockedExchangePointer (&listen_client_thr, NULL);
|
2019-03-22 20:30:37 +01:00
|
|
|
if (thr)
|
|
|
|
{
|
2019-04-20 17:22:29 +02:00
|
|
|
if (lct_termination_evt)
|
|
|
|
SetEvent (lct_termination_evt);
|
2019-03-22 20:30:37 +01:00
|
|
|
WaitForSingleObject (thr, INFINITE);
|
|
|
|
DWORD err;
|
|
|
|
GetExitCodeThread (thr, &err);
|
|
|
|
if (err)
|
2019-04-14 21:16:02 +02:00
|
|
|
{
|
|
|
|
ret = -1;
|
2019-04-20 17:22:29 +02:00
|
|
|
debug_printf ("listen_client_thread exited with error");
|
2019-04-14 21:16:02 +02:00
|
|
|
}
|
2019-03-22 20:30:37 +01:00
|
|
|
CloseHandle (thr);
|
|
|
|
}
|
2019-04-20 17:22:29 +02:00
|
|
|
evt = InterlockedExchangePointer (&lct_termination_evt, NULL);
|
2019-03-22 20:30:37 +01:00
|
|
|
if (evt)
|
|
|
|
CloseHandle (evt);
|
2019-04-14 21:16:02 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_fifo::close ()
|
|
|
|
{
|
|
|
|
int ret = stop_listen_client ();
|
2019-03-22 20:30:37 +01:00
|
|
|
if (read_ready)
|
|
|
|
CloseHandle (read_ready);
|
|
|
|
if (write_ready)
|
|
|
|
CloseHandle (write_ready);
|
2019-06-22 16:07:48 +02:00
|
|
|
fifo_client_lock ();
|
2019-04-14 21:15:56 +02:00
|
|
|
for (int i = 0; i < nhandlers; i++)
|
|
|
|
if (fc_handler[i].close () < 0)
|
2019-04-14 21:16:02 +02:00
|
|
|
ret = -1;
|
2019-06-22 16:07:48 +02:00
|
|
|
fifo_client_unlock ();
|
2019-04-14 21:16:02 +02:00
|
|
|
return fhandler_base::close () || ret;
|
2009-07-24 22:54:33 +02:00
|
|
|
}
|
|
|
|
|
2019-05-09 17:23:44 +02:00
|
|
|
/* If we have a write handle (i.e., we're a duplexer or a writer),
|
|
|
|
keep the nonblocking state of the windows pipe in sync with our
|
|
|
|
nonblocking state. */
|
2019-04-26 00:21:11 +02:00
|
|
|
int
|
|
|
|
fhandler_fifo::fcntl (int cmd, intptr_t arg)
|
|
|
|
{
|
2019-05-09 17:23:44 +02:00
|
|
|
if (cmd != F_SETFL || nohandle ())
|
2019-04-26 00:21:11 +02:00
|
|
|
return fhandler_base::fcntl (cmd, arg);
|
|
|
|
|
|
|
|
const bool was_nonblocking = is_nonblocking ();
|
|
|
|
int res = fhandler_base::fcntl (cmd, arg);
|
|
|
|
const bool now_nonblocking = is_nonblocking ();
|
|
|
|
if (now_nonblocking != was_nonblocking)
|
|
|
|
set_pipe_non_blocking (get_handle (), now_nonblocking);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2009-07-24 22:54:33 +02:00
|
|
|
int
|
2011-10-16 00:37:30 +02:00
|
|
|
fhandler_fifo::dup (fhandler_base *child, int flags)
|
2009-07-24 22:54:33 +02:00
|
|
|
{
|
2019-04-14 21:16:03 +02:00
|
|
|
int ret = -1;
|
|
|
|
fhandler_fifo *fhf = NULL;
|
|
|
|
|
2019-03-22 20:30:36 +01:00
|
|
|
if (fhandler_base::dup (child, flags))
|
2019-04-14 21:16:03 +02:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
fhf = (fhandler_fifo *) child;
|
2011-10-30 05:50:36 +01:00
|
|
|
if (!DuplicateHandle (GetCurrentProcess (), read_ready,
|
|
|
|
GetCurrentProcess (), &fhf->read_ready,
|
|
|
|
0, true, DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
fhf->close ();
|
|
|
|
__seterrno ();
|
2019-04-14 21:16:03 +02:00
|
|
|
goto out;
|
2011-10-30 05:50:36 +01:00
|
|
|
}
|
|
|
|
if (!DuplicateHandle (GetCurrentProcess (), write_ready,
|
|
|
|
GetCurrentProcess (), &fhf->write_ready,
|
|
|
|
0, true, DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
CloseHandle (fhf->read_ready);
|
|
|
|
fhf->close ();
|
|
|
|
__seterrno ();
|
2019-04-14 21:16:03 +02:00
|
|
|
goto out;
|
2009-07-24 22:54:33 +02:00
|
|
|
}
|
2019-06-22 16:07:48 +02:00
|
|
|
fifo_client_lock ();
|
2019-04-14 21:15:56 +02:00
|
|
|
for (int i = 0; i < nhandlers; i++)
|
2019-03-22 20:30:39 +01:00
|
|
|
{
|
2019-04-14 21:15:56 +02:00
|
|
|
if (!DuplicateHandle (GetCurrentProcess (), fc_handler[i].fh->get_handle (),
|
2019-03-22 20:30:39 +01:00
|
|
|
GetCurrentProcess (),
|
2019-04-14 21:15:56 +02:00
|
|
|
&fhf->fc_handler[i].fh->get_handle (),
|
2019-04-20 17:31:08 +02:00
|
|
|
0, true, DUPLICATE_SAME_ACCESS))
|
2019-03-22 20:30:39 +01:00
|
|
|
{
|
2019-06-22 16:07:48 +02:00
|
|
|
fifo_client_unlock ();
|
2019-03-22 20:30:39 +01:00
|
|
|
CloseHandle (fhf->read_ready);
|
|
|
|
CloseHandle (fhf->write_ready);
|
|
|
|
fhf->close ();
|
|
|
|
__seterrno ();
|
2019-04-14 21:16:03 +02:00
|
|
|
goto out;
|
2019-03-22 20:30:39 +01:00
|
|
|
}
|
|
|
|
}
|
2019-06-22 16:07:48 +02:00
|
|
|
fifo_client_unlock ();
|
2019-04-14 21:16:03 +02:00
|
|
|
if (!reader || fhf->listen_client ())
|
|
|
|
ret = 0;
|
2019-04-20 17:41:12 +02:00
|
|
|
if (reader)
|
|
|
|
fhf->init_fixup_before ();
|
2019-04-14 21:16:03 +02:00
|
|
|
out:
|
|
|
|
return ret;
|
2011-10-30 05:50:36 +01:00
|
|
|
}
|
|
|
|
|
2019-04-20 17:41:12 +02:00
|
|
|
void
|
|
|
|
fhandler_fifo::init_fixup_before ()
|
|
|
|
{
|
|
|
|
cygheap->fdtab.inc_need_fixup_before ();
|
|
|
|
}
|
|
|
|
|
2011-10-30 05:50:36 +01:00
|
|
|
void
|
|
|
|
fhandler_fifo::fixup_after_fork (HANDLE parent)
|
|
|
|
{
|
2019-03-22 20:30:36 +01:00
|
|
|
fhandler_base::fixup_after_fork (parent);
|
2011-10-30 05:50:36 +01:00
|
|
|
fork_fixup (parent, read_ready, "read_ready");
|
|
|
|
fork_fixup (parent, write_ready, "write_ready");
|
2019-06-22 16:07:48 +02:00
|
|
|
fifo_client_lock ();
|
2019-04-14 21:15:56 +02:00
|
|
|
for (int i = 0; i < nhandlers; i++)
|
2019-05-09 18:35:58 +02:00
|
|
|
fc_handler[i].fh->fhandler_base::fixup_after_fork (parent);
|
2019-06-22 16:07:48 +02:00
|
|
|
fifo_client_unlock ();
|
2019-04-20 17:46:09 +02:00
|
|
|
if (reader && !listen_client ())
|
|
|
|
debug_printf ("failed to start lct, %E");
|
2009-07-24 22:54:33 +02:00
|
|
|
}
|
|
|
|
|
2019-04-18 17:39:52 +02:00
|
|
|
void
|
|
|
|
fhandler_fifo::fixup_after_exec ()
|
|
|
|
{
|
|
|
|
fhandler_base::fixup_after_exec ();
|
2019-04-20 17:46:09 +02:00
|
|
|
if (reader && !listen_client ())
|
|
|
|
debug_printf ("failed to start lct, %E");
|
2019-04-18 17:39:52 +02:00
|
|
|
}
|
|
|
|
|
2009-07-24 22:54:33 +02:00
|
|
|
void
|
|
|
|
fhandler_fifo::set_close_on_exec (bool val)
|
|
|
|
{
|
|
|
|
fhandler_base::set_close_on_exec (val);
|
2011-10-30 05:50:36 +01:00
|
|
|
set_no_inheritance (read_ready, val);
|
|
|
|
set_no_inheritance (write_ready, val);
|
2019-06-22 16:07:48 +02:00
|
|
|
fifo_client_lock ();
|
2019-04-14 21:15:56 +02:00
|
|
|
for (int i = 0; i < nhandlers; i++)
|
2019-05-09 18:35:58 +02:00
|
|
|
fc_handler[i].fh->fhandler_base::set_close_on_exec (val);
|
2019-06-22 16:07:48 +02:00
|
|
|
fifo_client_unlock ();
|
2009-07-24 22:54:33 +02:00
|
|
|
}
|