2000-02-24 03:49:44 +01:00
|
|
|
/* pipe.cc: pipe for Cygwin.
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-02-24 03:49:44 +01:00
|
|
|
Copyright 1996, 1998, 1999, 2000 Cygnus Solutions.
|
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. */
|
|
|
|
|
2000-08-02 18:28:18 +02:00
|
|
|
#include "winsup.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <errno.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"
|
|
|
|
#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"
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
static int
|
|
|
|
make_pipe (int fildes[2], unsigned int psize, int mode)
|
|
|
|
{
|
|
|
|
SetResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK," make_pipe");
|
|
|
|
|
|
|
|
HANDLE r, w;
|
2000-10-07 20:12:11 +02:00
|
|
|
int fdr = -1, fdw = -1;
|
2000-02-17 20:38:33 +01:00
|
|
|
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-04-18 23:10:15 +02:00
|
|
|
if ((fdr = cygheap->fdtab.find_unused_handle ()) < 0)
|
2000-02-17 20:38:33 +01:00
|
|
|
set_errno (ENMFILE);
|
2001-04-18 23:10:15 +02:00
|
|
|
else if ((fdw = cygheap->fdtab.find_unused_handle (fdr + 1)) < 0)
|
2000-09-04 19:52:42 +02:00
|
|
|
set_errno (ENMFILE);
|
2000-02-17 20:38:33 +01:00
|
|
|
else if (!CreatePipe (&r, &w, sa, psize))
|
|
|
|
__seterrno ();
|
|
|
|
else
|
|
|
|
{
|
2001-04-18 23:10:15 +02:00
|
|
|
fhandler_base *fhr = cygheap->fdtab.build_fhandler (fdr, FH_PIPER, "/dev/piper");
|
|
|
|
fhandler_base *fhw = cygheap->fdtab.build_fhandler (fdw, FH_PIPEW, "/dev/pipew");
|
2000-02-17 20:38:33 +01: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-07 20:12:11 +02:00
|
|
|
res = 0;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2000-10-07 20:12:11 +02:00
|
|
|
syscall_printf ("%d = make_pipe ([%d, %d], %d, %p)", res, fdr, fdw, psize, mode);
|
2000-02-17 20:38:33 +01:00
|
|
|
ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK," make_pipe");
|
2000-10-07 20:12:11 +02:00
|
|
|
return res;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
dup (int fd)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
SetResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK," dup");
|
|
|
|
|
2001-04-18 23:10:15 +02:00
|
|
|
res = dup2 (fd, cygheap->fdtab.find_unused_handle ());
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK," dup");
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
dup2 (int oldfd, int newfd)
|
|
|
|
{
|
2001-04-18 23:10:15 +02:00
|
|
|
return cygheap->fdtab.dup2 (oldfd, newfd);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|