2005-04-16 13:20:01 +08:00
|
|
|
/* fhandler_fifo.cc - See fhandler.h for a description of the fhandler classes.
|
2003-09-20 08:31:13 +08:00
|
|
|
|
2012-01-23 05:43:25 +08:00
|
|
|
Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
|
2011-03-10 00:47:44 +08:00
|
|
|
Red Hat, Inc.
|
2003-09-20 08:31:13 +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. */
|
|
|
|
|
|
|
|
#include "winsup.h"
|
2008-04-08 00:15:45 +08:00
|
|
|
#include "miscfuncs.h"
|
2003-09-20 08:31:13 +08:00
|
|
|
|
|
|
|
#include "cygerrno.h"
|
|
|
|
#include "security.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "fhandler.h"
|
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
2007-07-08 01:00:33 +08:00
|
|
|
#include "sigproc.h"
|
|
|
|
#include "cygtls.h"
|
2009-10-31 21:24:06 +08:00
|
|
|
#include "shared_info.h"
|
2011-04-29 16:27:11 +08:00
|
|
|
#include "ntdll.h"
|
2003-09-20 08:31:13 +08:00
|
|
|
|
2007-07-08 01:00:33 +08:00
|
|
|
fhandler_fifo::fhandler_fifo ():
|
2011-10-30 12:50:36 +08:00
|
|
|
fhandler_base_overlapped (),
|
|
|
|
read_ready (NULL), write_ready (NULL)
|
2003-09-20 08:31:13 +08:00
|
|
|
{
|
2011-03-10 00:47:44 +08:00
|
|
|
max_atomic_write = DEFAULT_PIPEBUFSIZE;
|
2007-07-08 01:00:33 +08:00
|
|
|
need_fork_fixup (true);
|
2003-09-20 08:31:13 +08:00
|
|
|
}
|
|
|
|
|
2011-10-30 12:50:36 +08:00
|
|
|
#define fnevent(w) fifo_name (npbuf, w "-event")
|
|
|
|
#define fnpipe() fifo_name (npbuf, "fifo")
|
|
|
|
#define create_pipe(r, w) \
|
2011-11-24 02:56:57 +08:00
|
|
|
fhandler_pipe::create (sa_buf, (r), (w), 0, fnpipe (), open_mode)
|
2003-09-20 08:31:13 +08:00
|
|
|
|
2009-07-25 04:54:33 +08:00
|
|
|
char *
|
2011-10-30 12:50:36 +08:00
|
|
|
fhandler_fifo::fifo_name (char *buf, const char *what)
|
2009-07-25 04:54:33 +08:00
|
|
|
{
|
|
|
|
/* Generate a semi-unique name to associate with this fifo. */
|
2011-10-30 12:50:36 +08:00
|
|
|
__small_sprintf (buf, "%s.%08x.%016X", what, get_dev (),
|
|
|
|
get_ino ());
|
2009-07-25 04:54:33 +08:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2010-01-15 02:46:02 +08: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 21:58:09 +08:00
|
|
|
|
2012-01-23 05:43:25 +08: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)
|
|
|
|
debug_printf ("SetEvent for %s failed, %E", res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2003-09-20 08:31:13 +08:00
|
|
|
int
|
2007-07-08 01:00:33 +08:00
|
|
|
fhandler_fifo::open (int flags, mode_t)
|
2003-09-20 08:31:13 +08:00
|
|
|
{
|
2011-10-30 12:50:36 +08:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
success,
|
|
|
|
error_errno_set,
|
|
|
|
error_set_errno
|
|
|
|
} res;
|
2012-01-23 05:43:25 +08:00
|
|
|
bool reader, writer, duplexer;
|
2011-10-30 12:50:36 +08:00
|
|
|
DWORD open_mode = FILE_FLAG_OVERLAPPED;
|
2007-07-08 01:00:33 +08:00
|
|
|
|
2011-10-30 12:50:36 +08:00
|
|
|
/* Determine what we're doing with this fhandler: reading, writing, both */
|
|
|
|
switch (flags & O_ACCMODE)
|
2003-09-20 08:31:13 +08:00
|
|
|
{
|
2011-10-30 12:50:36 +08:00
|
|
|
case O_RDONLY:
|
|
|
|
reader = true;
|
|
|
|
writer = false;
|
2012-01-23 05:43:25 +08:00
|
|
|
duplexer = false;
|
2011-10-30 12:50:36 +08:00
|
|
|
break;
|
|
|
|
case O_WRONLY:
|
|
|
|
writer = true;
|
|
|
|
reader = false;
|
2012-01-23 05:43:25 +08:00
|
|
|
duplexer = false;
|
2011-10-30 12:50:36 +08:00
|
|
|
break;
|
|
|
|
case O_RDWR:
|
|
|
|
open_mode |= PIPE_ACCESS_DUPLEX;
|
2012-01-23 05:43:25 +08:00
|
|
|
reader = true;
|
|
|
|
writer = false;
|
|
|
|
duplexer = true;
|
2011-10-30 12:50:36 +08:00
|
|
|
break;
|
|
|
|
default:
|
2007-07-08 01:00:33 +08:00
|
|
|
set_errno (EINVAL);
|
2011-10-30 12:50:36 +08:00
|
|
|
res = error_errno_set;
|
|
|
|
goto out;
|
2007-07-08 01:00:33 +08:00
|
|
|
}
|
2009-07-25 04:54:33 +08:00
|
|
|
|
2012-01-23 05:43:25 +08:00
|
|
|
debug_only_printf ("reader %d, writer %d, duplexer %d", reader, writer, duplexer);
|
2011-10-30 12:50:36 +08: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-23 05:43:25 +08:00
|
|
|
if (!(read_ready = CreateEvent (sa_buf, duplexer, false, fnevent ("r"))))
|
2011-10-30 12:50:36 +08:00
|
|
|
{
|
|
|
|
debug_printf ("CreatEvent for %s failed, %E", npbuf);
|
|
|
|
res = error_set_errno;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-01-23 05:43:25 +08:00
|
|
|
if (!(write_ready = CreateEvent (sa_buf, false, false, fnevent ("w"))))
|
2007-07-08 01:00:33 +08:00
|
|
|
{
|
2011-10-30 12:50:36 +08:00
|
|
|
debug_printf ("CreatEvent for %s failed, %E", npbuf);
|
|
|
|
res = error_set_errno;
|
|
|
|
goto out;
|
|
|
|
}
|
2008-11-27 01:21:04 +08:00
|
|
|
|
2011-10-30 12:50:36 +08: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-23 05:43:25 +08:00
|
|
|
else if (!arm (read_ready))
|
2011-10-30 12:50:36 +08:00
|
|
|
{
|
|
|
|
res = error_set_errno;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-01-23 05:43:25 +08:00
|
|
|
else if (!duplexer && !wait (write_ready))
|
2011-10-30 12:50:36 +08: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-23 05:43:25 +08:00
|
|
|
if (!arm (write_ready))
|
2009-07-25 04:54:33 +08:00
|
|
|
{
|
2011-10-30 12:50:36 +08:00
|
|
|
res = error_set_errno;
|
|
|
|
goto out;
|
2009-07-25 04:54:33 +08:00
|
|
|
}
|
2011-10-30 12:50:36 +08: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-08 01:00:33 +08:00
|
|
|
{
|
2011-10-30 12:50:36 +08:00
|
|
|
CloseHandle (read_ready);
|
|
|
|
read_ready = NULL;
|
2007-07-08 01:00:33 +08:00
|
|
|
}
|
2011-10-30 12:50:36 +08:00
|
|
|
if (write_ready)
|
2007-07-08 01:00:33 +08:00
|
|
|
{
|
2011-10-30 12:50:36 +08:00
|
|
|
CloseHandle (write_ready);
|
|
|
|
write_ready = NULL;
|
2003-09-20 08:31:13 +08:00
|
|
|
}
|
2011-10-30 12:50:36 +08:00
|
|
|
if (get_io_handle ())
|
|
|
|
CloseHandle (get_io_handle ());
|
2003-09-20 08:31:13 +08:00
|
|
|
}
|
2011-10-30 12:50:36 +08:00
|
|
|
debug_printf ("res %d", res);
|
|
|
|
return res == success;
|
2003-09-20 08:31:13 +08:00
|
|
|
}
|
|
|
|
|
2007-07-08 01:00:33 +08:00
|
|
|
bool
|
2011-10-30 12:50:36 +08:00
|
|
|
fhandler_fifo::wait (HANDLE h)
|
2003-09-20 08:31:13 +08:00
|
|
|
{
|
2011-10-30 12:50:36 +08: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-10 00:02:56 +08:00
|
|
|
switch (cygwait (h, wait))
|
2006-06-23 08:19:39 +08:00
|
|
|
{
|
2011-10-30 12:50:36 +08:00
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
debug_only_printf ("successfully waited for %s", what);
|
|
|
|
return true;
|
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
if (h == write_ready)
|
2009-07-25 04:54:33 +08:00
|
|
|
{
|
2011-10-30 12:50:36 +08:00
|
|
|
debug_only_printf ("wait timed out waiting for write but will still open reader since non-blocking mode");
|
|
|
|
return true;
|
2009-07-25 04:54:33 +08:00
|
|
|
}
|
2011-10-30 12:50:36 +08:00
|
|
|
else
|
2009-07-25 04:54:33 +08:00
|
|
|
{
|
2011-10-30 12:50:36 +08:00
|
|
|
set_errno (ENXIO);
|
2009-07-25 04:54:33 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2011-10-30 12:50:36 +08:00
|
|
|
case WAIT_OBJECT_0 + 1:
|
|
|
|
debug_only_printf ("interrupted by signal while waiting for %s", what);
|
|
|
|
set_errno (EINTR);
|
|
|
|
return false;
|
|
|
|
case WAIT_OBJECT_0 + 2:
|
|
|
|
debug_only_printf ("cancellable interruption while waiting for %s", what);
|
|
|
|
pthread::static_cancel_self (); /* never returns */
|
2007-07-08 01:00:33 +08:00
|
|
|
break;
|
2011-10-30 12:50:36 +08:00
|
|
|
default:
|
|
|
|
debug_only_printf ("unknown error while waiting for %s", what);
|
|
|
|
__seterrno ();
|
|
|
|
return false;
|
|
|
|
}
|
2007-07-08 01:00:33 +08:00
|
|
|
}
|
2006-06-23 08:19:39 +08:00
|
|
|
|
2009-07-25 04:54:33 +08:00
|
|
|
void __stdcall
|
2009-03-27 23:04:42 +08:00
|
|
|
fhandler_fifo::raw_read (void *in_ptr, size_t& len)
|
2007-07-08 01:00:33 +08:00
|
|
|
{
|
2011-10-30 12:50:36 +08:00
|
|
|
size_t orig_len = len;
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
fhandler_base_overlapped::raw_read (in_ptr, len);
|
2012-01-23 05:43:25 +08:00
|
|
|
if (len || i || WaitForSingleObject (read_ready, 0) != WAIT_OBJECT_0)
|
2011-10-30 12:50:36 +08: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 ()))
|
|
|
|
{
|
|
|
|
debug_printf ("DisconnecttNamedPipe failed, %E");
|
|
|
|
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-23 05:43:25 +08:00
|
|
|
else if (!arm (read_ready))
|
|
|
|
goto errno_out;
|
2011-10-30 12:50:36 +08: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 08:31:13 +08:00
|
|
|
|
2011-10-30 12:50:36 +08:00
|
|
|
errno_out:
|
|
|
|
__seterrno ();
|
|
|
|
errout:
|
|
|
|
len = -1;
|
2003-09-20 08:31:13 +08:00
|
|
|
}
|
2007-02-27 20:58:56 +08:00
|
|
|
|
|
|
|
int __stdcall
|
|
|
|
fhandler_fifo::fstatvfs (struct statvfs *sfs)
|
|
|
|
{
|
2007-12-04 21:29:44 +08:00
|
|
|
fhandler_disk_file fh (pc);
|
|
|
|
fh.get_device () = FH_FS;
|
|
|
|
return fh.fstatvfs (sfs);
|
2007-02-27 20:58:56 +08:00
|
|
|
}
|
2009-07-25 04:54:33 +08:00
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_fifo::close ()
|
|
|
|
{
|
2011-10-30 12:50:36 +08:00
|
|
|
CloseHandle (read_ready);
|
|
|
|
CloseHandle (write_ready);
|
2009-07-25 04:54:33 +08:00
|
|
|
return fhandler_base::close ();
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-10-16 06:37:30 +08:00
|
|
|
fhandler_fifo::dup (fhandler_base *child, int flags)
|
2009-07-25 04:54:33 +08:00
|
|
|
{
|
2011-10-30 12:50:36 +08:00
|
|
|
if (fhandler_base_overlapped::dup (child, flags))
|
2009-07-25 04:54:33 +08:00
|
|
|
{
|
2011-10-30 12:50:36 +08: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-25 04:54:33 +08:00
|
|
|
}
|
2011-10-30 12:50:36 +08: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-25 04:54:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_fifo::set_close_on_exec (bool val)
|
|
|
|
{
|
|
|
|
fhandler_base::set_close_on_exec (val);
|
2011-10-30 12:50:36 +08:00
|
|
|
set_no_inheritance (read_ready, val);
|
|
|
|
set_no_inheritance (write_ready, val);
|
2009-07-25 04:54:33 +08:00
|
|
|
}
|