/* pipe.cc: pipe for Cygwin.
- Copyright 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
- 2005 Red Hat, Inc.
+ Copyright 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
+ 2008 Hat, Inc.
This file is part of Cygwin.
#include "cygthread.h"
#include "ntdll.h"
-static unsigned pipecount;
-static const NO_COPY char pipeid_fmt[] = "stupid_pipe.%u.%u";
-
fhandler_pipe::fhandler_pipe ()
- : fhandler_base (), guard (NULL), broken_pipe (false), writepipe_exists(0),
- orig_pid (0), id (0)
+ : fhandler_base (), popen_pid (0)
+{
+ get_overlapped ()->hEvent = NULL;
+ need_fork_fixup (true);
+}
+
+struct pipesync
+{
+ bool reader;
+ HANDLE ev, non_cygwin_h, ret_handle;
+ pipesync(HANDLE, DWORD);
+ int operator == (int x) const {return !!ev;}
+ static DWORD WINAPI handler (LPVOID *);
+};
+
+inline bool
+getov_result (HANDLE h, DWORD& nbytes, LPOVERLAPPED ov)
+{
+ if (ov && (GetLastError () != ERROR_IO_PENDING
+ || !GetOverlappedResult (h, ov, &nbytes, true)))
+ {
+ __seterrno ();
+ return false;
+ }
+ return true;
+}
+
+static DWORD WINAPI
+pipe_handler (LPVOID in_ps)
+{
+ pipesync ps = *(pipesync *) in_ps;
+ HANDLE in, out;
+ DWORD err = fhandler_pipe::create_selectable (&sec_none_nih, in, out, 0);
+ if (err)
+ {
+ SetLastError (err);
+ system_printf ("couldn't create a shadow pipe for non-cygwin pipe I/O, %E");
+ return 0;
+ }
+ ((pipesync *) in_ps)->ret_handle = ps.reader ? in : out;
+ SetEvent (ps.ev);
+
+ char buf[4096];
+ DWORD read_bytes, write_bytes;
+ HANDLE hread, hwrite, hclose;
+ OVERLAPPED ov, *rov, *wov;
+ memset (&ov, 0, sizeof (ov));
+ ov.hEvent = CreateEvent (&sec_none_nih, true, false, NULL);
+ if (ps.reader)
+ {
+ hread = ps.non_cygwin_h;
+ hclose = hwrite = out;
+ wov = &ov;
+ rov = NULL;
+ }
+ else
+ {
+ hclose = hread = in;
+ hwrite = ps.non_cygwin_h;
+ rov = &ov;
+ wov = NULL;
+ }
+
+ while (1)
+ {
+ ResetEvent (ov.hEvent);
+ BOOL res = ReadFile (hread, buf, 4096, &read_bytes, rov);
+ if (!res && !getov_result (hread, read_bytes, rov))
+ break;
+ if (!read_bytes)
+ break;
+
+ res = WriteFile (hwrite, buf, read_bytes, &write_bytes, wov);
+ if (!res && !getov_result (hwrite, write_bytes, wov))
+ break;
+ if (write_bytes != read_bytes)
+ break;
+ }
+
+ err = GetLastError ();
+ CloseHandle (ov.hEvent);
+ CloseHandle (hclose);
+ CloseHandle (ps.non_cygwin_h);
+ SetLastError (err);
+ return 0;
+}
+
+pipesync::pipesync (HANDLE f, DWORD is_reader):
+ reader (false), ret_handle (NULL)
{
+ ev = CreateEvent (&sec_none_nih, true, false, NULL);
+ if (!ev)
+ {
+ system_printf ("couldn't create synchronization event for non-cygwin pipe, %E");
+ goto out;
+ }
+ non_cygwin_h = f;
+ reader = !!is_reader;
+ ret_handle = NULL;
+
+ DWORD tid;
+ HANDLE ht = CreateThread (&sec_none_nih, 0, pipe_handler, this, 0, &tid);
+
+ if (!ht)
+ goto out;
+ CloseHandle (ht);
+
+ switch (WaitForSingleObject (ev, INFINITE))
+ {
+ case WAIT_OBJECT_0:
+ break;
+ default:
+ system_printf ("WFSO failed waiting for synchronization event for non-cygwin pipe, %E");
+ break;
+ }
+
+out:
+ if (ev)
+ {
+ CloseHandle (ev);
+ ev = NULL;
+ }
+ return;
+}
+
+#define WINPIPE "\\\\.\\pipe\\"
+void
+fhandler_pipe::init (HANDLE f, DWORD a, mode_t mode)
+{
+ // FIXME: Have to clean this up someday
+ if (!*get_win32_name () && get_name ())
+ {
+ char *hold_normalized_name = (char *) alloca (strlen (get_name ()) + 1);
+ strcpy (hold_normalized_name, get_name ());
+ char *s, *d;
+ for (s = hold_normalized_name, d = (char *) get_win32_name (); *s; s++, d++)
+ if (*s == '/')
+ *d = '\\';
+ else
+ *d = *s;
+ *d = '\0';
+ set_name (hold_normalized_name);
+ }
+
+ bool opened_properly = a & FILE_CREATE_PIPE_INSTANCE;
+ a &= ~FILE_CREATE_PIPE_INSTANCE;
+ if (!opened_properly)
+ {
+ pipesync ps (f, a & GENERIC_READ);
+ f = ps.ret_handle;
+ }
+
+ fhandler_base::init (f, a, mode);
+ if (mode & O_NOINHERIT)
+ close_on_exec (true);
+ setup_overlapped ();
}
extern "C" int sscanf (const char *, const char *, ...);
int
fhandler_pipe::open (int flags, mode_t mode)
{
- HANDLE proc, pipe_hdl, nio_hdl = NULL, nwrp_hdl = NULL;
+ HANDLE proc, pipe_hdl, nio_hdl = NULL;
fhandler_pipe *fh = NULL;
size_t size;
int pid, rwflags = (flags & O_ACCMODE);
+ bool inh;
sscanf (get_name (), "/proc/%d/fd/pipe:[%d]", &pid, (int *) &pipe_hdl);
if (pid == myself->pid)
{
- cygheap_fdenum cfd;
+ cygheap_fdenum cfd (true);
while (cfd.next () >= 0)
{
if (cfd->get_handle () != pipe_hdl)
set_errno (EACCES);
goto out;
}
+ inh = !(flags & O_NOINHERIT);
if (!DuplicateHandle (proc, pipe_hdl, hMainProc, &nio_hdl,
- 0, false, DUPLICATE_SAME_ACCESS))
+ 0, inh, DUPLICATE_SAME_ACCESS))
{
__seterrno ();
goto out;
}
- if (fh->writepipe_exists
- && !DuplicateHandle (proc, fh->writepipe_exists,
- hMainProc, &nwrp_hdl,
- 0, false, DUPLICATE_SAME_ACCESS))
- {
- __seterrno ();
- goto out;
- }
- if (fh->read_state)
- {
- create_read_state (2);
- need_fork_fixup (true);
- ProtectHandle1 (read_state, read_state);
- }
- if (fh->get_guard ())
- create_guard ((flags & O_NOINHERIT) ? &sec_none_nih : &sec_none);
init (nio_hdl, fh->get_access (), mode & O_TEXT ?: O_BINARY);
- writepipe_exists = nwrp_hdl;
if (flags & O_NOINHERIT)
close_on_exec (true);
uninterruptible_io (fh->uninterruptible_io ());
CloseHandle (proc);
return 1;
out:
- if (nwrp_hdl)
- CloseHandle (nwrp_hdl);
if (nio_hdl)
CloseHandle (nio_hdl);
if (fh)
return -1;
}
-void
-fhandler_pipe::set_close_on_exec (bool val)
+int
+fhandler_pipe::fadvise (_off64_t offset, _off64_t length, int advice)
{
- fhandler_base::set_close_on_exec (val);
- if (guard)
- set_no_inheritance (guard, val);
- if (writepipe_exists)
- set_no_inheritance (writepipe_exists, val);
+ set_errno (ESPIPE);
+ return -1;
}
-char *fhandler_pipe::get_proc_fd_name (char *buf)
+int
+fhandler_pipe::ftruncate (_off64_t length, bool allow_truncate)
{
- __small_sprintf (buf, "pipe:[%d]", get_handle ());
- return buf;
+ set_errno (allow_truncate ? EINVAL : ESPIPE);
+ return -1;
}
-struct pipeargs
-{
- fhandler_base *fh;
- void *ptr;
- size_t *len;
-};
-
-static DWORD WINAPI
-read_pipe (void *arg)
+char *
+fhandler_pipe::get_proc_fd_name (char *buf)
{
- pipeargs *pi = (pipeargs *) arg;
- pi->fh->fhandler_base::read (pi->ptr, *pi->len);
- return 0;
+ __small_sprintf (buf, "pipe:[%d]", get_handle ());
+ return buf;
}
void __stdcall
fhandler_pipe::read (void *in_ptr, size_t& in_len)
{
- if (broken_pipe)
- in_len = 0;
- else
- {
- pipeargs pi = {dynamic_cast<fhandler_base *>(this), in_ptr, &in_len};
- cygthread *th = new cygthread (read_pipe, &pi, "read_pipe");
- if (th->detach (read_state) && !in_len)
- in_len = (size_t) -1; /* received a signal */
- }
- ReleaseMutex (guard);
+ return read_overlapped (in_ptr, in_len);
}
int
-fhandler_pipe::close ()
-{
- if (guard)
- CloseHandle (guard);
- if (writepipe_exists)
- CloseHandle (writepipe_exists);
-#ifndef NEWVFORK
- if (read_state)
-#else
- // FIXME is this vfork_cleanup test right? Is it responsible for some of
- // the strange pipe behavior that has been reported in the cygwin mailing
- // list?
- if (read_state && !cygheap->fdtab.in_vfork_cleanup ())
-#endif
- ForceCloseHandle (read_state);
- return fhandler_base::close ();
-}
-
-bool
-fhandler_pipe::hit_eof ()
+fhandler_pipe::write (const void *ptr, size_t len)
{
- char buf[80];
- HANDLE ev;
- if (broken_pipe)
- return 1;
- if (!orig_pid)
- return false;
- __small_sprintf (buf, pipeid_fmt, orig_pid, id);
- if ((ev = OpenEvent (EVENT_ALL_ACCESS, FALSE, buf)))
- CloseHandle (ev);
- debug_printf ("%s %p", buf, ev);
- return ev == NULL;
-}
-
-void
-fhandler_pipe::fixup_after_exec ()
-{
- if (read_state)
- {
- create_read_state (2);
- ProtectHandle (read_state);
- }
-}
-
-void
-fhandler_pipe::fixup_after_fork (HANDLE parent)
-{
-debug_printf ("here");
- fhandler_base::fixup_after_fork (parent);
- if (guard)
- fork_fixup (parent, guard, "guard");
- if (writepipe_exists)
- fork_fixup (parent, writepipe_exists, "guard");
- fixup_after_exec ();
+ return write_overlapped (ptr, len);
}
int
fhandler_pipe::dup (fhandler_base *child)
{
- int res = -1;
fhandler_pipe *ftp = (fhandler_pipe *) child;
- ftp->guard = ftp->writepipe_exists = ftp->read_state = NULL;
-
- if (get_handle ())
- {
- res = fhandler_base::dup (child);
- if (res)
- goto err;
- }
-
- if (guard == NULL)
- ftp->guard = NULL;
- else if (!DuplicateHandle (hMainProc, guard, hMainProc, &ftp->guard, 0, 1,
- DUPLICATE_SAME_ACCESS))
- {
- debug_printf ("couldn't duplicate guard %p, %E", guard);
- goto err;
- }
-
- if (writepipe_exists == NULL)
- ftp->writepipe_exists = NULL;
- else if (!DuplicateHandle (hMainProc, writepipe_exists, hMainProc,
- &ftp->writepipe_exists, 0, 1,
- DUPLICATE_SAME_ACCESS))
- {
- debug_printf ("couldn't duplicate writepipe_exists %p, %E", writepipe_exists);
- goto err;
- }
-
- if (read_state == NULL)
- ftp->read_state = NULL;
- else if (!DuplicateHandle (hMainProc, read_state, hMainProc,
- &ftp->read_state, 0, 0,
- DUPLICATE_SAME_ACCESS))
- {
- debug_printf ("couldn't duplicate read_state %p, %E", read_state);
- goto err;
- }
-
- res = 0;
- goto out;
-
-err:
- if (ftp->guard)
- CloseHandle (ftp->guard);
- if (ftp->writepipe_exists)
- CloseHandle (ftp->writepipe_exists);
- if (ftp->read_state)
- CloseHandle (ftp->read_state);
- goto leave;
+ ftp->set_popen_pid (0);
-out:
- ftp->id = id;
- ftp->orig_pid = orig_pid;
- VerifyHandle (ftp->guard);
- VerifyHandle (ftp->writepipe_exists);
- VerifyHandle (ftp->read_state);
+ int res;
+ if (get_handle () && fhandler_base::dup (child))
+ res = -1;
+ else
+ res = 0;
-leave:
debug_printf ("res %d", res);
return res;
}
unlike CreatePipe, which returns a bool for success or failure. */
int
fhandler_pipe::create_selectable (LPSECURITY_ATTRIBUTES sa_ptr, HANDLE& r,
- HANDLE& w, DWORD psize, bool fifo)
+ HANDLE& w, DWORD psize)
{
/* Default to error. */
r = w = INVALID_HANDLE_VALUE;
/* Ensure that there is enough pipe buffer space for atomic writes. */
- if (!fifo && psize < PIPE_BUF)
+ if (psize < PIPE_BUF)
psize = PIPE_BUF;
char pipename[CYG_MAX_PATH];
the pipe was not created earlier by some other process, even if
the pid has been reused. We avoid FILE_FLAG_FIRST_PIPE_INSTANCE
because that is only available for Win2k SP2 and WinXP. */
- r = CreateNamedPipe (pipename, PIPE_ACCESS_INBOUND,
+ r = CreateNamedPipe (pipename, PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1, psize,
psize, NMPWAIT_USE_DEFAULT_WAIT, sa_ptr);
debug_printf ("pipe access denied, retrying");
break;
default:
- /* CreateNamePipe failed. Maybe we are on an older Win9x platform without
- named pipes. Return an anonymous pipe as the best approximation. */
- debug_printf ("CreateNamedPipe failed, resorting to CreatePipe size %lu",
- psize);
- if (CreatePipe (&r, &w, sa_ptr, psize))
- {
- debug_printf ("pipe read handle %p", r);
- debug_printf ("pipe write handle %p", w);
- return 0;
- }
- err = GetLastError ();
- debug_printf ("CreatePipe failed, %E");
- return err;
+ {
+ err = GetLastError ();
+ debug_printf ("CreatePipe failed, %E");
+ return err;
+ }
}
}
/* Open the named pipe for writing.
Be sure to permit FILE_READ_ATTRIBUTES access. */
w = CreateFile (pipename, GENERIC_WRITE | FILE_READ_ATTRIBUTES, 0, sa_ptr,
- OPEN_EXISTING, 0, 0);
+ OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
if (!w || w == INVALID_HANDLE_VALUE)
{
}
int
-fhandler_pipe::create (fhandler_pipe *fhs[2], unsigned psize, int mode, bool fifo)
+fhandler_pipe::create (fhandler_pipe *fhs[2], unsigned psize, int mode)
{
HANDLE r, w;
SECURITY_ATTRIBUTES *sa = (mode & O_NOINHERIT) ? &sec_none_nih : &sec_none;
- int res = -1;
+ int res;
- int ret = create_selectable (sa, r, w, psize, fifo);
+ int ret = create_selectable (sa, r, w, psize);
if (ret)
- __seterrno_from_win_error (ret);
+ {
+ __seterrno_from_win_error (ret);
+ res = -1;
+ }
else
{
fhs[0] = (fhandler_pipe *) build_fh_dev (*piper_dev);
fhs[1] = (fhandler_pipe *) build_fh_dev (*pipew_dev);
- int binmode = mode & O_TEXT ?: O_BINARY;
- fhs[0]->init (r, GENERIC_READ, binmode);
- fhs[1]->init (w, GENERIC_WRITE, binmode);
- if (mode & O_NOINHERIT)
- {
- fhs[0]->close_on_exec (true);
- fhs[1]->close_on_exec (true);
- }
-
- fhs[0]->create_read_state (2);
- fhs[0]->need_fork_fixup (true);
- ProtectHandle1 (fhs[0]->read_state, read_state);
-
+ mode |= mode & O_TEXT ?: O_BINARY;
+ fhs[0]->init (r, FILE_CREATE_PIPE_INSTANCE | GENERIC_READ, mode);
+ fhs[1]->init (w, FILE_CREATE_PIPE_INSTANCE | GENERIC_WRITE, mode);
res = 0;
- fhs[0]->create_guard (sa);
- if (wincap.has_unreliable_pipes ())
- {
- char buf[80];
- int count = pipecount++; /* FIXME: Should this be InterlockedIncrement? */
- __small_sprintf (buf, pipeid_fmt, myself->pid, count);
- fhs[1]->writepipe_exists = CreateEvent (sa, TRUE, FALSE, buf);
- fhs[0]->orig_pid = myself->pid;
- fhs[0]->id = count;
- }
}
syscall_printf ("%d = pipe ([%p, %p], %d, %p)", res, fhs[0], fhs[1], psize, mode);
return 0;
}
-#define DEFAULT_PIPEBUFSIZE (4 * PIPE_BUF)
+int __stdcall
+fhandler_pipe::fstatvfs (struct statvfs *sfs)
+{
+ set_errno (EBADF);
+ return -1;
+}
+
+#define DEFAULT_PIPEBUFSIZE (16 * PIPE_BUF)
extern "C" int
pipe (int filedes[2])