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
|
|
|
|
2012-01-22 22:43:25 +01:00
|
|
|
Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
|
2011-03-09 17:47:44 +01:00
|
|
|
Red Hat, Inc.
|
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"
|
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
|
|
|
|
2007-07-07 19:00:33 +02:00
|
|
|
fhandler_fifo::fhandler_fifo ():
|
2011-10-30 05:50:36 +01:00
|
|
|
fhandler_base_overlapped (),
|
|
|
|
read_ready (NULL), write_ready (NULL)
|
2003-09-20 02:31:13 +02:00
|
|
|
{
|
2011-03-09 17:47:44 +01:00
|
|
|
max_atomic_write = DEFAULT_PIPEBUFSIZE;
|
2007-07-07 19:00:33 +02:00
|
|
|
need_fork_fixup (true);
|
2003-09-20 02:31:13 +02:00
|
|
|
}
|
|
|
|
|
2011-10-30 05:50:36 +01:00
|
|
|
#define fnevent(w) fifo_name (npbuf, w "-event")
|
|
|
|
#define fnpipe() fifo_name (npbuf, "fifo")
|
|
|
|
#define create_pipe(r, w) \
|
2011-11-23 19:56:57 +01:00
|
|
|
fhandler_pipe::create (sa_buf, (r), (w), 0, fnpipe (), open_mode)
|
2003-09-20 02:31:13 +02:00
|
|
|
|
2009-07-24 22:54:33 +02:00
|
|
|
char *
|
2011-10-30 05:50:36 +01:00
|
|
|
fhandler_fifo::fifo_name (char *buf, const char *what)
|
2009-07-24 22:54:33 +02:00
|
|
|
{
|
|
|
|
/* Generate a semi-unique name to associate with this fifo. */
|
2011-10-30 05:50:36 +01:00
|
|
|
__small_sprintf (buf, "%s.%08x.%016X", what, get_dev (),
|
|
|
|
get_ino ());
|
2009-07-24 22:54:33 +02:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
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 if (h == write_ready)
|
|
|
|
what = "writer";
|
|
|
|
else
|
|
|
|
what = "overlapped event";
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
success,
|
|
|
|
error_errno_set,
|
|
|
|
error_set_errno
|
|
|
|
} res;
|
2012-01-22 22:43:25 +01:00
|
|
|
bool reader, writer, duplexer;
|
2011-10-30 05:50:36 +01:00
|
|
|
DWORD open_mode = FILE_FLAG_OVERLAPPED;
|
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;
|
|
|
|
writer = false;
|
2012-01-22 22:43:25 +01:00
|
|
|
duplexer = false;
|
2011-10-30 05:50:36 +01:00
|
|
|
break;
|
|
|
|
case O_WRONLY:
|
|
|
|
writer = true;
|
|
|
|
reader = false;
|
2012-01-22 22:43:25 +01:00
|
|
|
duplexer = false;
|
2011-10-30 05:50:36 +01:00
|
|
|
break;
|
|
|
|
case O_RDWR:
|
|
|
|
open_mode |= PIPE_ACCESS_DUPLEX;
|
2012-01-22 22:43:25 +01:00
|
|
|
reader = true;
|
|
|
|
writer = false;
|
|
|
|
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);
|
|
|
|
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());
|
|
|
|
char npbuf[MAX_PATH];
|
|
|
|
|
|
|
|
/* Create control events for this named pipe */
|
2012-01-22 22:43:25 +01:00
|
|
|
if (!(read_ready = CreateEvent (sa_buf, duplexer, false, fnevent ("r"))))
|
2011-10-30 05:50:36 +01:00
|
|
|
{
|
|
|
|
debug_printf ("CreatEvent for %s failed, %E", npbuf);
|
|
|
|
res = error_set_errno;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-01-22 22:43:25 +01:00
|
|
|
if (!(write_ready = CreateEvent (sa_buf, false, false, fnevent ("w"))))
|
2007-07-07 19:00:33 +02:00
|
|
|
{
|
2011-10-30 05:50:36 +01:00
|
|
|
debug_printf ("CreatEvent for %s failed, %E", npbuf);
|
|
|
|
res = error_set_errno;
|
|
|
|
goto out;
|
|
|
|
}
|
2008-11-26 18:21:04 +01:00
|
|
|
|
2011-10-30 05:50:36 +01:00
|
|
|
/* If we're reading, create the pipe, signal that we're ready and wait for
|
|
|
|
a writer.
|
|
|
|
FIXME: Probably need to special case O_RDWR case. */
|
|
|
|
if (!reader)
|
|
|
|
/* We are not a reader */;
|
|
|
|
else if (create_pipe (&get_io_handle (), NULL))
|
|
|
|
{
|
|
|
|
debug_printf ("create of reader failed");
|
|
|
|
res = error_set_errno;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-01-22 22:43:25 +01:00
|
|
|
else if (!arm (read_ready))
|
2011-10-30 05:50:36 +01:00
|
|
|
{
|
|
|
|
res = error_set_errno;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-01-22 22:43:25 +01:00
|
|
|
else if (!duplexer && !wait (write_ready))
|
2011-10-30 05:50:36 +01:00
|
|
|
{
|
|
|
|
res = error_errno_set;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we're writing, it's a little tricky since it is possible that
|
|
|
|
we're attempting to open the other end of a pipe which is already
|
|
|
|
connected. In that case, we detect ERROR_PIPE_BUSY, reset the
|
|
|
|
read_ready event and wait for the reader to allow us to connect
|
|
|
|
by signalling read_ready.
|
|
|
|
|
|
|
|
Once the pipe has been set up, we signal write_ready. */
|
|
|
|
if (writer)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
while (1)
|
|
|
|
if (!wait (read_ready))
|
|
|
|
{
|
|
|
|
res = error_errno_set;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
else if ((err = create_pipe (NULL, &get_io_handle ())) == 0)
|
|
|
|
break;
|
|
|
|
else if (err == ERROR_PIPE_BUSY)
|
|
|
|
{
|
|
|
|
debug_only_printf ("pipe busy");
|
|
|
|
ResetEvent (read_ready);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
debug_printf ("create of writer failed");
|
|
|
|
res = error_set_errno;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-01-22 22:43:25 +01:00
|
|
|
if (!arm (write_ready))
|
2009-07-24 22:54:33 +02:00
|
|
|
{
|
2011-10-30 05:50:36 +01:00
|
|
|
res = error_set_errno;
|
|
|
|
goto out;
|
2009-07-24 22:54:33 +02:00
|
|
|
}
|
2011-10-30 05:50:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If setup_overlapped() succeeds (and why wouldn't it?) we are all set. */
|
|
|
|
if (setup_overlapped () == 0)
|
|
|
|
res = success;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
debug_printf ("setup_overlapped failed, %E");
|
|
|
|
res = error_set_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2011-10-30 05:50:36 +01:00
|
|
|
if (get_io_handle ())
|
|
|
|
CloseHandle (get_io_handle ());
|
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
|
|
|
}
|
|
|
|
|
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 if (h == write_ready)
|
|
|
|
what = "writer";
|
|
|
|
else
|
|
|
|
what = "overlapped event";
|
|
|
|
#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
|
|
|
|
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;
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
fhandler_base_overlapped::raw_read (in_ptr, len);
|
2012-01-22 22:43:25 +01:00
|
|
|
if (len || i || WaitForSingleObject (read_ready, 0) != WAIT_OBJECT_0)
|
2011-10-30 05:50:36 +01:00
|
|
|
break;
|
|
|
|
/* If we got here, then fhandler_base_overlapped::raw_read returned 0,
|
|
|
|
indicating "EOF" and something has set read_ready to zero. That means
|
|
|
|
we should have a client waiting to connect.
|
|
|
|
FIXME: If the client CTRL-C's the open during this time then this
|
|
|
|
could hang indefinitely. Maybe implement a timeout? */
|
|
|
|
if (!DisconnectNamedPipe (get_io_handle ()))
|
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("DisconnectNamedPipe failed, %E");
|
2011-10-30 05:50:36 +01:00
|
|
|
goto errno_out;
|
|
|
|
}
|
|
|
|
else if (!ConnectNamedPipe (get_io_handle (), get_overlapped ())
|
|
|
|
&& GetLastError () != ERROR_IO_PENDING)
|
|
|
|
{
|
|
|
|
debug_printf ("ConnectNamedPipe failed, %E");
|
|
|
|
goto errno_out;
|
|
|
|
}
|
2012-01-22 22:43:25 +01:00
|
|
|
else if (!arm (read_ready))
|
|
|
|
goto errno_out;
|
2011-10-30 05:50:36 +01:00
|
|
|
else if (!wait (get_overlapped_buffer ()->hEvent))
|
|
|
|
goto errout; /* If wait() fails, errno is set so no need to set it */
|
|
|
|
len = orig_len; /* Reset since raw_read above set it to zero. */
|
|
|
|
}
|
|
|
|
return;
|
2003-09-20 02:31:13 +02:00
|
|
|
|
2011-10-30 05:50:36 +01:00
|
|
|
errno_out:
|
|
|
|
__seterrno ();
|
|
|
|
errout:
|
|
|
|
len = -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
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_fifo::close ()
|
|
|
|
{
|
2011-10-30 05:50:36 +01:00
|
|
|
CloseHandle (read_ready);
|
|
|
|
CloseHandle (write_ready);
|
2009-07-24 22:54:33 +02:00
|
|
|
return fhandler_base::close ();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2011-10-30 05:50:36 +01:00
|
|
|
if (fhandler_base_overlapped::dup (child, flags))
|
2009-07-24 22:54:33 +02:00
|
|
|
{
|
2011-10-30 05:50:36 +01:00
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fhandler_fifo *fhf = (fhandler_fifo *) child;
|
|
|
|
if (!DuplicateHandle (GetCurrentProcess (), read_ready,
|
|
|
|
GetCurrentProcess (), &fhf->read_ready,
|
|
|
|
0, true, DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
fhf->close ();
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!DuplicateHandle (GetCurrentProcess (), write_ready,
|
|
|
|
GetCurrentProcess (), &fhf->write_ready,
|
|
|
|
0, true, DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
CloseHandle (fhf->read_ready);
|
|
|
|
fhf->close ();
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
2009-07-24 22:54:33 +02:00
|
|
|
}
|
2011-10-30 05:50:36 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_fifo::fixup_after_fork (HANDLE parent)
|
|
|
|
{
|
|
|
|
fhandler_base_overlapped::fixup_after_fork (parent);
|
|
|
|
fork_fixup (parent, read_ready, "read_ready");
|
|
|
|
fork_fixup (parent, write_ready, "write_ready");
|
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);
|
2009-07-24 22:54:33 +02:00
|
|
|
}
|