2000-02-24 03:49:44 +01:00
|
|
|
/* pipe.cc: pipe for Cygwin.
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2003-01-10 13:32:49 +01:00
|
|
|
Copyright 1996, 1998, 1999, 2000, 2001, 2002, 2003 Red Hat, Inc.
|
2000-02-17 20:38:33 +01: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. */
|
|
|
|
|
2002-07-03 05:20:50 +02:00
|
|
|
/* FIXME: Should this really be fhandler_pipe.cc? */
|
|
|
|
|
2000-08-02 18:28:18 +02:00
|
|
|
#include "winsup.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <unistd.h>
|
2002-11-09 04:17:40 +01:00
|
|
|
#include <sys/socket.h>
|
2000-08-22 05:58:47 +02:00
|
|
|
#include "cygerrno.h"
|
2001-07-26 21:22:24 +02:00
|
|
|
#include "security.h"
|
2000-08-22 07:10:20 +02:00
|
|
|
#include "fhandler.h"
|
2001-10-01 06:10:07 +02:00
|
|
|
#include "path.h"
|
2000-08-22 07:10:20 +02:00
|
|
|
#include "dtable.h"
|
2001-04-18 23:10:15 +02:00
|
|
|
#include "cygheap.h"
|
2000-08-22 07:10:20 +02:00
|
|
|
#include "thread.h"
|
2001-09-24 23:50:44 +02:00
|
|
|
#include "pinfo.h"
|
2002-12-11 05:00:04 +01:00
|
|
|
#include "cygthread.h"
|
2001-09-24 23:50:44 +02:00
|
|
|
|
|
|
|
static unsigned pipecount;
|
|
|
|
static const NO_COPY char pipeid_fmt[] = "stupid_pipe.%u.%u";
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-10-13 19:23:35 +02:00
|
|
|
fhandler_pipe::fhandler_pipe (DWORD devtype)
|
2001-11-05 02:52:20 +01:00
|
|
|
: fhandler_base (devtype), guard (NULL), broken_pipe (false), writepipe_exists(0),
|
2001-11-01 22:15:53 +01:00
|
|
|
orig_pid (0), id (0)
|
2001-09-22 18:55:02 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-04-01 18:11:41 +02:00
|
|
|
_off64_t
|
|
|
|
fhandler_pipe::lseek (_off64_t offset, int whence)
|
2001-09-22 18:55:02 +02:00
|
|
|
{
|
|
|
|
debug_printf ("(%d, %d)", offset, whence);
|
|
|
|
set_errno (ESPIPE);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_pipe::set_close_on_exec (int val)
|
|
|
|
{
|
2003-02-04 04:01:17 +01:00
|
|
|
fhandler_base::set_close_on_exec (val);
|
2001-09-24 23:50:44 +02:00
|
|
|
if (guard)
|
|
|
|
set_inheritance (guard, val);
|
|
|
|
if (writepipe_exists)
|
|
|
|
set_inheritance (writepipe_exists, val);
|
2001-09-22 18:55:02 +02:00
|
|
|
}
|
|
|
|
|
2002-12-11 05:00:04 +01:00
|
|
|
struct pipeargs
|
|
|
|
{
|
|
|
|
fhandler_base *fh;
|
|
|
|
void *ptr;
|
2002-12-14 05:01:32 +01:00
|
|
|
size_t *len;
|
2002-12-11 05:00:04 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static DWORD WINAPI
|
|
|
|
read_pipe (void *arg)
|
|
|
|
{
|
|
|
|
pipeargs *pi = (pipeargs *) arg;
|
2002-12-14 05:01:32 +01:00
|
|
|
pi->fh->fhandler_base::read (pi->ptr, *pi->len);
|
2002-12-11 05:00:04 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-12-14 05:01:32 +01:00
|
|
|
void __stdcall
|
|
|
|
fhandler_pipe::read (void *in_ptr, size_t& in_len)
|
2001-09-22 18:55:02 +02:00
|
|
|
{
|
2001-11-05 02:52:20 +01:00
|
|
|
if (broken_pipe)
|
2002-12-14 05:01:32 +01:00
|
|
|
in_len = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pipeargs pi = {this, in_ptr, &in_len};
|
|
|
|
ResetEvent (read_state);
|
|
|
|
cygthread *th = new cygthread (read_pipe, &pi, "read_pipe");
|
|
|
|
if (th->detach (read_state) && !in_len)
|
2003-09-07 04:22:58 +02:00
|
|
|
in_len = (size_t) -1; /* received a signal */
|
2002-12-14 05:01:32 +01:00
|
|
|
}
|
2001-11-03 06:42:21 +01:00
|
|
|
(void) ReleaseMutex (guard);
|
2002-12-14 05:01:32 +01:00
|
|
|
return;
|
2001-09-22 18:55:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int fhandler_pipe::close ()
|
|
|
|
{
|
2003-02-07 06:48:58 +01:00
|
|
|
int res = fhandler_base::close ();
|
2003-02-07 07:15:10 +01:00
|
|
|
#undef guard
|
2001-09-22 18:55:02 +02:00
|
|
|
if (guard)
|
|
|
|
CloseHandle (guard);
|
2001-09-24 23:50:44 +02:00
|
|
|
if (writepipe_exists)
|
|
|
|
CloseHandle (writepipe_exists);
|
2003-01-03 07:20:23 +01:00
|
|
|
if (read_state && !cygheap->fdtab.in_vfork_cleanup ())
|
2002-12-14 05:01:32 +01:00
|
|
|
CloseHandle (read_state);
|
2001-09-22 18:55:02 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-09-24 23:50:44 +02:00
|
|
|
bool
|
|
|
|
fhandler_pipe::hit_eof ()
|
|
|
|
{
|
|
|
|
char buf[80];
|
|
|
|
HANDLE ev;
|
2001-11-05 02:52:20 +01:00
|
|
|
if (broken_pipe)
|
2001-11-03 06:42:21 +01:00
|
|
|
return 1;
|
2001-09-24 23:50:44 +02:00
|
|
|
if (!orig_pid)
|
2001-09-28 08:06:45 +02:00
|
|
|
return false;
|
2001-09-24 23:50:44 +02:00
|
|
|
__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;
|
|
|
|
}
|
|
|
|
|
2002-12-14 05:01:32 +01:00
|
|
|
void
|
|
|
|
fhandler_pipe::fixup_after_exec (HANDLE parent)
|
|
|
|
{
|
|
|
|
if (read_state)
|
|
|
|
read_state = CreateEvent (&sec_none_nih, FALSE, FALSE, NULL);
|
|
|
|
}
|
|
|
|
|
2001-09-29 03:23:06 +02:00
|
|
|
void
|
|
|
|
fhandler_pipe::fixup_after_fork (HANDLE parent)
|
|
|
|
{
|
2003-02-04 04:01:17 +01:00
|
|
|
fhandler_base::fixup_after_fork (parent);
|
2001-09-29 03:23:06 +02:00
|
|
|
if (guard)
|
|
|
|
fork_fixup (parent, guard, "guard");
|
|
|
|
if (writepipe_exists)
|
|
|
|
fork_fixup (parent, writepipe_exists, "guard");
|
2002-12-14 05:01:32 +01:00
|
|
|
fixup_after_exec (parent);
|
2001-09-29 03:23:06 +02:00
|
|
|
}
|
|
|
|
|
2001-09-22 18:55:02 +02:00
|
|
|
int
|
|
|
|
fhandler_pipe::dup (fhandler_base *child)
|
|
|
|
{
|
2003-03-22 19:30:58 +01:00
|
|
|
if (get_handle ())
|
|
|
|
{
|
|
|
|
int res = fhandler_base::dup (child);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
}
|
2001-09-22 18:55:02 +02:00
|
|
|
|
|
|
|
fhandler_pipe *ftp = (fhandler_pipe *) child;
|
|
|
|
|
2002-12-14 06:10:18 +01:00
|
|
|
/* FIXME: This leaks handles in the failing condition */
|
2001-09-22 18:55:02 +02:00
|
|
|
if (guard == NULL)
|
|
|
|
ftp->guard = NULL;
|
|
|
|
else if (!DuplicateHandle (hMainProc, guard, hMainProc, &ftp->guard, 0, 1,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
2001-09-29 03:23:06 +02:00
|
|
|
{
|
|
|
|
debug_printf ("couldn't duplicate guard %p, %E", guard);
|
|
|
|
return -1;
|
|
|
|
}
|
2001-09-24 23:50:44 +02:00
|
|
|
|
|
|
|
if (writepipe_exists == NULL)
|
|
|
|
ftp->writepipe_exists = NULL;
|
|
|
|
else if (!DuplicateHandle (hMainProc, writepipe_exists, hMainProc,
|
|
|
|
&ftp->writepipe_exists, 0, 1,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
2001-09-29 03:23:06 +02:00
|
|
|
{
|
|
|
|
debug_printf ("couldn't duplicate writepipe_exists %p, %E", writepipe_exists);
|
|
|
|
return -1;
|
|
|
|
}
|
2001-09-24 23:50:44 +02:00
|
|
|
|
2002-12-14 05:01:32 +01:00
|
|
|
if (read_state == NULL)
|
|
|
|
ftp->read_state = NULL;
|
|
|
|
else if (!DuplicateHandle (hMainProc, read_state, hMainProc,
|
|
|
|
&ftp->read_state, 0, 1,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
debug_printf ("couldn't duplicate read_state %p, %E", writepipe_exists);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-09-24 23:50:44 +02:00
|
|
|
ftp->id = id;
|
|
|
|
ftp->orig_pid = orig_pid;
|
2001-09-22 18:55:02 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-09-24 23:50:44 +02:00
|
|
|
int
|
2000-02-17 20:38:33 +01:00
|
|
|
make_pipe (int fildes[2], unsigned int psize, int mode)
|
|
|
|
{
|
|
|
|
HANDLE r, w;
|
|
|
|
SECURITY_ATTRIBUTES *sa = (mode & O_NOINHERIT) ? &sec_none_nih : &sec_none;
|
2000-10-07 20:12:11 +02:00
|
|
|
int res = -1;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-10-16 01:39:33 +02:00
|
|
|
cygheap_fdnew fdr;
|
2001-11-03 04:32:27 +01:00
|
|
|
if (fdr >= 0)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2001-10-16 01:39:33 +02:00
|
|
|
cygheap_fdnew fdw (fdr, false);
|
|
|
|
if (fdw < 0)
|
2001-11-03 04:32:27 +01:00
|
|
|
/* out of fds? */;
|
2001-10-16 01:39:33 +02:00
|
|
|
else if (!CreatePipe (&r, &w, sa, psize))
|
|
|
|
__seterrno ();
|
|
|
|
else
|
2001-09-24 23:50:44 +02:00
|
|
|
{
|
2001-10-16 01:39:33 +02:00
|
|
|
fhandler_pipe *fhr = (fhandler_pipe *) cygheap->fdtab.build_fhandler (fdr, FH_PIPER, "/dev/piper");
|
|
|
|
fhandler_pipe *fhw = (fhandler_pipe *) cygheap->fdtab.build_fhandler (fdw, FH_PIPEW, "/dev/pipew");
|
|
|
|
|
2002-06-05 03:42:28 +02:00
|
|
|
int binmode = mode & O_TEXT ?: O_BINARY;
|
2001-10-16 01:39:33 +02:00
|
|
|
fhr->init (r, GENERIC_READ, binmode);
|
|
|
|
fhw->init (w, GENERIC_WRITE, binmode);
|
|
|
|
if (mode & O_NOINHERIT)
|
|
|
|
{
|
|
|
|
fhr->set_close_on_exec_flag (1);
|
|
|
|
fhw->set_close_on_exec_flag (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fildes[0] = fdr;
|
|
|
|
fildes[1] = fdw;
|
2002-12-14 05:01:32 +01:00
|
|
|
fhr->read_state = CreateEvent (&sec_none_nih, FALSE, FALSE, NULL);
|
2002-12-14 06:44:06 +01:00
|
|
|
fhr->set_need_fork_fixup ();
|
2001-10-16 01:39:33 +02:00
|
|
|
|
|
|
|
res = 0;
|
|
|
|
fhr->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);
|
|
|
|
fhw->writepipe_exists = CreateEvent (sa, TRUE, FALSE, buf);
|
|
|
|
fhr->orig_pid = myself->pid;
|
|
|
|
fhr->id = count;
|
|
|
|
}
|
2001-09-24 23:50:44 +02:00
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2001-10-16 01:39:33 +02:00
|
|
|
syscall_printf ("%d = make_pipe ([%d, %d], %d, %p)", res, fildes[0],
|
2001-11-05 07:09:15 +01:00
|
|
|
fildes[1], psize, mode);
|
2000-10-07 20:12:11 +02:00
|
|
|
return res;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2002-11-09 04:17:40 +01:00
|
|
|
int
|
|
|
|
fhandler_pipe::ioctl (unsigned int cmd, void *p)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case FIONREAD:
|
|
|
|
if (get_device () == FH_PIPEW)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!PeekNamedPipe (get_handle (), NULL, 0, NULL, (DWORD *) &n, NULL))
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return fhandler_base::ioctl (cmd, p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*(int *) p = n;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
extern "C" int
|
|
|
|
pipe (int filedes[2])
|
|
|
|
{
|
2000-08-24 19:54:43 +02:00
|
|
|
extern DWORD binmode;
|
|
|
|
return make_pipe (filedes, 16384, (!binmode || binmode == O_BINARY) ? O_BINARY : O_TEXT);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
_pipe (int filedes[2], unsigned int psize, int mode)
|
|
|
|
{
|
|
|
|
int res = make_pipe (filedes, psize, mode);
|
|
|
|
/* This type of pipe is not interruptible so set the appropriate flag. */
|
|
|
|
if (!res)
|
2001-04-18 23:10:15 +02:00
|
|
|
cygheap->fdtab[filedes[0]]->set_r_no_interrupt (1);
|
2000-02-17 20:38:33 +01:00
|
|
|
return res;
|
|
|
|
}
|