newlib-cygwin/winsup/cygwin/pipe.cc

272 lines
6.1 KiB
C++
Raw Normal View History

/* pipe.cc: pipe for Cygwin.
2000-02-18 03:38:33 +08:00
Copyright 1996, 1998, 1999, 2000, 2001, 2002 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. */
/* FIXME: Should this really be fhandler_pipe.cc? */
#include "winsup.h"
2000-02-18 03:38:33 +08:00
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include "cygerrno.h"
#include "security.h"
#include "fhandler.h"
Add "path.h" include throughout, where needed. Use new path_conv methods and operators to simplify testing for directory and attributes, throughout. * path.h (path_conv::exists): New method. (path_conv::has_attribute): Ditto. (path_conv::isdir): Ditto. (path_conv::DWORD &): New operator. (path_conv::int &): Ditto. * dir.cc (rmdir): Eliminate a goto. * dtable.cc (dtable::build_fhandler): Accept opt and suffix info for path_conv.check. Return fh == NULL on path_conv error. Pass unit to set_name as appropriate. (dtable::reset_unix_path_name): New method. * dtable.h (dtable): Declare new method. Reflect arg changes to build_fhandler. * fhandler.cc (fhandler_disk_dummy_name): Eliminate. (fhandler_base::set_name): Expect paths to be NULL. Build unix_path_name from win32_path_name when it is a device. (fhandler_base::reset_unix_path_name): New method. (fhandler_base::raw_read): Report EISDIR when ERROR_INVALID_FUNCTION or ERROR_INVALID_PARAMETER and reading a directory. (fhandler_disk_file::fstat): Don't call stat_dev since we should now never be calling fhandler_disk_file methods with devices. (fhandler_base::fhandler_base): Clear {unix,win32}_path_name. (fhandler_base::~fhandler_base): Always free {unix,win32}_path_name. (fhandler_disk_file::fhandler_disk_file): Remove set_no_free_names kludge. (fhandler_disk_file::open): Ditto. * fhandler.h (fhandler_base::no_free_names): Eliminate. (fhandler_base::set_no_free_names): Ditto. * fhandler_tty.cc (fhandler_tty_slave::fhandler_tty_slave): Don't set unix_path_name here. * path.cc (fchdir): Lock fd table throughout. Use new dtable::reset_unix_path_name method to reset path. * syscalls.cc (stat_worker): Reorganize to always call fstat method. Pass path_conv method to fhandler_*::open. (chroot): Elminate a goto.
2001-10-01 12:10:07 +08:00
#include "path.h"
#include "dtable.h"
#include "cygheap.h"
#include "thread.h"
#include "pinfo.h"
#include "cygthread.h"
static unsigned pipecount;
static const NO_COPY char pipeid_fmt[] = "stupid_pipe.%u.%u";
2000-02-18 03:38:33 +08:00
fhandler_pipe::fhandler_pipe (DWORD devtype)
: fhandler_base (devtype), guard (NULL), broken_pipe (false), writepipe_exists(0),
* dtable.cc (dtable::build_fhandler): Issue internal error on unknown device. * fhandler.cc (fhandler_base::close): Show both name and handle in debugging output. * fhandler.h (fhandler_base::get_guard): New virtual method. (fhandler_pipe::get_guard): New method. (fhandler_socket::ready_for_read): Delete declaration. (fhandler_pipe::ready_for_read): Ditto. (fhandler_serial::ready_for_read): Ditto. (fhandler_console::ready_for_read): Ditto. (fhandler_tty_common::ready_for_read): Ditto. (fhandler_windows::ready_for_read): Ditto. (struct select_record::peek): Declare new method. * select.cc (MAKEready): Delete. (peek_pipe): Use get_guard method to retrieve potential guard mutex handle. (fhandler_base::ready_for_read): Rewrite as generic ready-for-read handler. Should only be called for "slow" devices. (fhandler_socket::ready_for_read): Delete definition. (fhandler_pipe::ready_for_read): Ditto. (fhandler_serial::ready_for_read): Ditto. (fhandler_console::ready_for_read): Ditto. (fhandler_tty_common::ready_for_read): Ditto. (fhandler_windows::ready_for_read): Ditto. (fhandler_pipe::select_read): Fill in new peek record in select_record structure. (fhandler_console::select_read): Ditto. (fhandler_tty_common::select_read): Ditto. (fhandler_serial::select_read): Ditto. (fhandler_socket::select_read): Ditto. (fhandler_socket::select_read): Ditto. (fhandler_tty_slave::ready_for_read): Check for tty not open. Set errnos appropriately. * syscalls.cc (_read): Allow ready_for_read to set errno. * pinfo.cc (pinfo::init): Return spawn/NO_WAIT process as valid if it is initializing. * sigproc.cc (getsem): Adjust wait for process to initialize downward to avoid huge waits.
2001-11-02 05:15:53 +08:00
orig_pid (0), id (0)
{
}
* cygwin.din (fstat64): New symbol. (ftruncate64): Ditto. (lseek64): Ditto. (lstat64): Ditto. (mmap64): Ditto. (seekdir64): Ditto. (stat64): Ditto. (telldir64): Ditto. (truncate64): Ditto. * dir.cc (telldir64): New function. (telldir): Call telldir64(). (seekdir64): New function. (seekdir): Call seekdir64(). * fhandler.h: Redefine all methods using __off32_t to use __off64_t. * fhandler.cc: Use __off64_t and struct __stat64 throughout. * fhandler_clipboard.cc: Ditto. * fhandler_disk_file.cc: Ditto. * fhandler_dsp.cc: Ditto. * fhandler_floppy.cc: Ditto. * fhandler_mem.cc: Ditto. * fhandler_random.cc: Ditto. * fhandler_socket.cc: Ditto. * fhandler_tape.cc: Ditto. * fhandler_zero.cc: Ditto. * pipe.cc: Ditto. * glob.c: Ditto, call lstat64 and stat64 in Cygwin. * mmap.cc: Use __off64_t throughout. (mmap64): New function. * sec_acl.cc (acl_worker): Use struct __stat64, call stat64 and lstat64. * syscalls.cc (lseek64): New function. (stat64_to_stat32): Ditto. (fstat64): Ditto. (stat64): Ditto. (lstat64): Ditto. (ftruncate64): Ditto. (truncate64): Ditto. (_fstat): Call fstat64. (_stat): Call stat64. (cygwin_lstat): Rename to avoid declaration problem. Call lstat64. (stat_worker): Use struct __stat64. (access): Ditto. (ftruncate): Call ftruncate64. (truncate): Call truncate64. * wincap.cc: Set flag has_64bit_file_access appropriately. * wincap.h: Add flag has_64bit_file_access. * winsup.h (ILLEGAL_SEEK): Define as __off64_t. (stat_dev): Declare using struct __stat64. (stat_worker): Ditto. * include/cygwin/stat.h (struct __stat32): Define if compiling Cygwin. (struct __stat64): Ditto. (struct stat): Revert definition with explicitly sized datatypes. Eliminate sized field names. * include/cygwin/types.h (blksize_t): New type. (__blkcnt32_t): Ditto. (__blkcnt64_t): Ditto. (blkcnt_t): Ditto.
2002-02-26 01:47:51 +08:00
__off64_t
fhandler_pipe::lseek (__off64_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);
if (guard)
set_inheritance (guard, val);
if (writepipe_exists)
set_inheritance (writepipe_exists, val);
}
struct pipeargs
{
fhandler_base *fh;
void *ptr;
size_t *len;
};
static DWORD WINAPI
read_pipe (void *arg)
{
pipeargs *pi = (pipeargs *) arg;
pi->fh->fhandler_base::read (pi->ptr, *pi->len);
return 0;
}
void __stdcall
fhandler_pipe::read (void *in_ptr, size_t& in_len)
{
if (broken_pipe)
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)
(ssize_t) in_len = -1; /* received a signal */
}
(void) ReleaseMutex (guard);
return;
}
int fhandler_pipe::close ()
{
int res = this->fhandler_base::close ();
if (guard)
CloseHandle (guard);
if (writepipe_exists)
CloseHandle (writepipe_exists);
if (read_state)
CloseHandle (read_state);
return res;
}
bool
fhandler_pipe::hit_eof ()
{
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 (HANDLE parent)
{
if (read_state)
read_state = CreateEvent (&sec_none_nih, FALSE, FALSE, NULL);
}
void
fhandler_pipe::fixup_after_fork (HANDLE parent)
{
this->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 (parent);
}
int
fhandler_pipe::dup (fhandler_base *child)
{
int res = this->fhandler_base::dup (child);
if (res)
return res;
fhandler_pipe *ftp = (fhandler_pipe *) child;
2002-12-14 13:10:18 +08:00
/* FIXME: This leaks handles in the failing condition */
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);
return -1;
}
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);
return -1;
}
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;
}
ftp->id = id;
ftp->orig_pid = orig_pid;
return 0;
}
int
2000-02-18 03:38:33 +08: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;
int res = -1;
2000-02-18 03:38:33 +08:00
cygheap_fdnew fdr;
if (fdr >= 0)
2000-02-18 03:38:33 +08:00
{
cygheap_fdnew fdw (fdr, false);
if (fdw < 0)
/* out of fds? */;
else if (!CreatePipe (&r, &w, sa, psize))
__seterrno ();
else
{
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");
Remove fcntl.h includes throughout. * fhandler.h: Move fcntl.h include here. (fhandler_base::set_flags): Accept supplied_bin argument. Make non-inlined. * dtable.cc (dtable::init_std_file_from_handle): Just use binmode from pc. (reset_to_open_binmode): Use set_flags. * cygwin.din (open): Avoid newlib wrapper. (read): Ditto. (unlink): Ditto. (write): Ditto. * fhandler.cc (fhandler_base::set_flags): Accept supplied_bin argument. Make binmode decisions here. (fhandler_base::open): Avoid using pc if it is NULL. Eliminate binmode logic. Just call set_flags with binmode argument. (fhandler_base::init): Call set_flags with binmode argument. * fhandler_clipboard.cc (fhandler_dev_clipboard::open): Ditto. * fhandler_console.cc (fhandler_console::open): Ditto. (fhandler_console::init): Force binary on open. * fhandler_disk_file.cc (fhandler_disk_file::open): Don't set binmode here. Let it happen in base class. * fhandler_dsp.cc (fhandler_dev_dsp::open): Force binmode open. Set return value appropriately if unable to open. * fhandler_proc.cc (fhandler_proc::open): Make sure flags are set before open_status. * fhandler_process.cc (fhandler_process::open): Ditto. * fhandler_registry.cc (fhandler_registry::open): Ditto. * fhandler_random.cc (fhandler_dev_random::fhandler_dev_random): Ditto. * fhandler_raw.cc (fhandler_dev_raw::open): Force O_BINARY by default. * fhandler_serial.cc (fhandler_serial::init): Ditto. * fhandler_tty.cc (fhandler_tty_slave::open): Ditto. (fhandler_pty_master::open): Ditto. * fhandler_virtual.cc (fhandler_virtual::open): Ditto. * fhandler_windows.cc (fhandler_windows::open): Ditto. * fhandler_zero.cc (fhandler_dev_zero::open): Ditto. * net.cc (fdsock): Ditto. * path.cc (path_conv::check): Avoid checking for extension when error or directory. (set_flags): Set PATH_TEXT explicitly, when appropriate. (mount_info::conv_to_win32_path): Use set_flags() to set path flags. * path.h (PATH_TEXT): New enum. (path_conv::binmode): Return appropriate constant based on binmode. * pipe.cc (make_pipe): Set binmode to O_TEXT xor O_BINARY. * syscalls.cc (setmode_helper): Make debugging message a little clearer. (setmode): Set binmode via set_flags.
2002-06-05 09:42:28 +08:00
int binmode = mode & O_TEXT ?: O_BINARY;
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;
fhr->read_state = CreateEvent (&sec_none_nih, FALSE, FALSE, NULL);
fhr->set_need_fork_fixup ();
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;
}
}
2000-02-18 03:38:33 +08:00
}
syscall_printf ("%d = make_pipe ([%d, %d], %d, %p)", res, fildes[0],
2001-11-05 14:09:15 +08:00
fildes[1], psize, mode);
return res;
2000-02-18 03:38:33 +08: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-18 03:38:33 +08:00
extern "C" int
pipe (int filedes[2])
{
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)
cygheap->fdtab[filedes[0]]->set_r_no_interrupt (1);
2000-02-18 03:38:33 +08:00
return res;
}