2000-02-24 10:49:44 +08:00
|
|
|
/* pipe.cc: pipe for Cygwin.
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-09-12 04:01:02 +08:00
|
|
|
Copyright 1996, 1998, 1999, 2000, 2001 Red Hat, Inc.
|
2000-02-18 03:38:33 +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. */
|
|
|
|
|
2000-08-03 00:28:18 +08:00
|
|
|
#include "winsup.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <errno.h>
|
2000-08-22 11:58:47 +08:00
|
|
|
#include "cygerrno.h"
|
2001-07-27 03:22:24 +08:00
|
|
|
#include "security.h"
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "fhandler.h"
|
|
|
|
#include "dtable.h"
|
2001-04-19 05:10:15 +08:00
|
|
|
#include "cygheap.h"
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "thread.h"
|
2001-09-25 05:50:44 +08:00
|
|
|
#include "sigproc.h"
|
|
|
|
#include "pinfo.h"
|
|
|
|
|
|
|
|
static unsigned pipecount;
|
|
|
|
static const NO_COPY char pipeid_fmt[] = "stupid_pipe.%u.%u";
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-09-23 00:55:02 +08:00
|
|
|
fhandler_pipe::fhandler_pipe (const char *name, DWORD devtype) :
|
2001-09-25 05:50:44 +08:00
|
|
|
fhandler_base (devtype, name),
|
|
|
|
guard (0), writepipe_exists(0), orig_pid (0), id (0)
|
2001-09-23 00:55:02 +08:00
|
|
|
{
|
|
|
|
set_cb (sizeof *this);
|
|
|
|
}
|
|
|
|
|
|
|
|
off_t
|
|
|
|
fhandler_pipe::lseek (off_t offset, int whence)
|
|
|
|
{
|
|
|
|
debug_printf ("(%d, %d)", offset, whence);
|
|
|
|
set_errno (ESPIPE);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_pipe::set_close_on_exec (int val)
|
|
|
|
{
|
|
|
|
this->fhandler_base::set_close_on_exec (val);
|
2001-09-25 05:50:44 +08:00
|
|
|
if (guard)
|
|
|
|
set_inheritance (guard, val);
|
|
|
|
if (writepipe_exists)
|
|
|
|
set_inheritance (writepipe_exists, val);
|
2001-09-23 00:55:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_pipe::read (void *in_ptr, size_t in_len)
|
|
|
|
{
|
|
|
|
int res = this->fhandler_base::read (in_ptr, in_len);
|
|
|
|
ReleaseMutex (guard);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fhandler_pipe::close ()
|
|
|
|
{
|
|
|
|
int res = this->fhandler_base::close ();
|
|
|
|
if (guard)
|
|
|
|
CloseHandle (guard);
|
2001-09-25 05:50:44 +08:00
|
|
|
if (writepipe_exists)
|
|
|
|
{debug_printf ("writepipe_exists closed");
|
|
|
|
CloseHandle (writepipe_exists);
|
|
|
|
}
|
2001-09-23 00:55:02 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-09-25 05:50:44 +08:00
|
|
|
bool
|
|
|
|
fhandler_pipe::hit_eof ()
|
|
|
|
{
|
|
|
|
char buf[80];
|
|
|
|
HANDLE ev;
|
|
|
|
if (!orig_pid)
|
|
|
|
return bg_ok;
|
|
|
|
__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;
|
|
|
|
}
|
|
|
|
|
2001-09-23 00:55:02 +08:00
|
|
|
int
|
|
|
|
fhandler_pipe::dup (fhandler_base *child)
|
|
|
|
{
|
|
|
|
int res = this->fhandler_base::dup (child);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
fhandler_pipe *ftp = (fhandler_pipe *) child;
|
|
|
|
|
|
|
|
if (guard == NULL)
|
|
|
|
ftp->guard = NULL;
|
|
|
|
else if (!DuplicateHandle (hMainProc, guard, hMainProc, &ftp->guard, 0, 1,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
|
|
|
return -1;
|
2001-09-25 05:50:44 +08:00
|
|
|
|
|
|
|
if (writepipe_exists == NULL)
|
|
|
|
ftp->writepipe_exists = NULL;
|
|
|
|
else if (!DuplicateHandle (hMainProc, writepipe_exists, hMainProc,
|
|
|
|
&ftp->writepipe_exists, 0, 1,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ftp->id = id;
|
|
|
|
ftp->orig_pid = orig_pid;
|
2001-09-23 00:55:02 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-09-25 05:50:44 +08:00
|
|
|
|
|
|
|
int
|
2000-02-18 03:38:33 +08:00
|
|
|
make_pipe (int fildes[2], unsigned int psize, int mode)
|
|
|
|
{
|
2001-08-07 08:01:42 +08:00
|
|
|
SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "make_pipe");
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
HANDLE r, w;
|
2000-10-08 02:12:11 +08:00
|
|
|
int fdr = -1, fdw = -1;
|
2000-02-18 03:38:33 +08:00
|
|
|
SECURITY_ATTRIBUTES *sa = (mode & O_NOINHERIT) ? &sec_none_nih : &sec_none;
|
2000-10-08 02:12:11 +08:00
|
|
|
int res = -1;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-04-19 05:10:15 +08:00
|
|
|
if ((fdr = cygheap->fdtab.find_unused_handle ()) < 0)
|
2000-02-18 03:38:33 +08:00
|
|
|
set_errno (ENMFILE);
|
2001-04-19 05:10:15 +08:00
|
|
|
else if ((fdw = cygheap->fdtab.find_unused_handle (fdr + 1)) < 0)
|
2000-09-05 01:52:42 +08:00
|
|
|
set_errno (ENMFILE);
|
2000-02-18 03:38:33 +08:00
|
|
|
else if (!CreatePipe (&r, &w, sa, psize))
|
|
|
|
__seterrno ();
|
|
|
|
else
|
|
|
|
{
|
2001-08-07 08:01:42 +08: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");
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
int binmode = mode & O_TEXT ? 0 : 1;
|
|
|
|
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;
|
|
|
|
|
2000-10-08 02:12:11 +08:00
|
|
|
res = 0;
|
2001-09-23 00:55:02 +08:00
|
|
|
fhr->create_guard (sa);
|
2001-09-25 05:50:44 +08:00
|
|
|
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;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2000-10-08 02:12:11 +08:00
|
|
|
syscall_printf ("%d = make_pipe ([%d, %d], %d, %p)", res, fdr, fdw, psize, mode);
|
2001-08-07 08:01:42 +08:00
|
|
|
ReleaseResourceLock(LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "make_pipe");
|
2000-10-08 02:12:11 +08:00
|
|
|
return res;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
pipe (int filedes[2])
|
|
|
|
{
|
2000-08-25 01:54:43 +08:00
|
|
|
extern DWORD binmode;
|
|
|
|
return make_pipe (filedes, 16384, (!binmode || binmode == O_BINARY) ? O_BINARY : O_TEXT);
|
2000-02-18 03:38:33 +08: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-19 05:10:15 +08:00
|
|
|
cygheap->fdtab[filedes[0]]->set_r_no_interrupt (1);
|
2000-02-18 03:38:33 +08:00
|
|
|
return res;
|
|
|
|
}
|