2005-04-16 07:20:01 +02:00
|
|
|
/* fhandler_fifo.cc - See fhandler.h for a description of the fhandler classes.
|
2003-09-20 02:31:13 +02:00
|
|
|
|
2007-02-27 13:58:56 +01:00
|
|
|
Copyright 2002, 2003, 2004, 2005, 2006, 2007 Red Hat, Inc.
|
2003-09-20 02:31:13 +02: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. */
|
|
|
|
|
|
|
|
#include "winsup.h"
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2007-02-27 13:58:56 +01:00
|
|
|
#include <sys/statvfs.h>
|
2003-09-20 02:31:13 +02:00
|
|
|
|
|
|
|
#include "cygerrno.h"
|
|
|
|
#include "perprocess.h"
|
|
|
|
#include "security.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "fhandler.h"
|
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
|
|
|
#include "pinfo.h"
|
|
|
|
|
|
|
|
fhandler_fifo::fhandler_fifo ()
|
2005-04-16 07:20:01 +02:00
|
|
|
: fhandler_pipe (), output_handle (NULL),
|
2003-09-20 02:31:13 +02:00
|
|
|
read_use (0), write_use (0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_fifo::set_use (int incr)
|
|
|
|
{
|
|
|
|
long oread_use = read_use;
|
|
|
|
|
|
|
|
if (get_flags () & (O_WRONLY | O_APPEND))
|
|
|
|
write_use += incr;
|
|
|
|
else if (get_flags () & O_RDWR)
|
|
|
|
{
|
|
|
|
write_use += incr;
|
|
|
|
read_use += incr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
read_use += incr;
|
|
|
|
|
|
|
|
if (incr >= 0)
|
|
|
|
return;
|
|
|
|
if (read_use <= 0 && oread_use != read_use)
|
|
|
|
{
|
|
|
|
HANDLE h = get_handle ();
|
|
|
|
if (h)
|
|
|
|
{
|
|
|
|
set_io_handle (NULL);
|
|
|
|
CloseHandle (h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_fifo::close ()
|
|
|
|
{
|
|
|
|
fhandler_pipe::close ();
|
2004-06-07 06:26:32 +02:00
|
|
|
if (get_output_handle ())
|
|
|
|
CloseHandle (get_output_handle ());
|
2005-07-05 05:16:46 +02:00
|
|
|
if (!hExeced)
|
|
|
|
set_use (-1);
|
2003-09-20 02:31:13 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define DUMMY_O_RDONLY 4
|
2005-04-22 15:58:09 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_fifo::close_one_end ()
|
|
|
|
{
|
|
|
|
int testflags = (get_flags () & (O_RDWR | O_WRONLY | O_APPEND)) ?: DUMMY_O_RDONLY;
|
|
|
|
static int flagtypes[] = {DUMMY_O_RDONLY | O_RDWR, O_WRONLY | O_APPEND | O_RDWR};
|
|
|
|
HANDLE *handles[2] = {&(get_handle ()), &(get_output_handle ())};
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
if (!(testflags & flagtypes[i]))
|
|
|
|
{
|
|
|
|
CloseHandle (*handles[i]);
|
|
|
|
*handles[i] = NULL;
|
|
|
|
}
|
|
|
|
else if (i == 0 && !read_state)
|
|
|
|
{
|
|
|
|
create_read_state (2);
|
|
|
|
need_fork_fixup (true);
|
|
|
|
}
|
|
|
|
}
|
2003-09-20 02:31:13 +02:00
|
|
|
int
|
|
|
|
fhandler_fifo::open_not_mine (int flags)
|
|
|
|
{
|
2005-05-19 03:25:20 +02:00
|
|
|
winpids pids ((DWORD) 0);
|
2004-06-07 06:26:32 +02:00
|
|
|
int res = 0;
|
2003-09-20 02:31:13 +02:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < pids.npids; i++)
|
|
|
|
{
|
|
|
|
_pinfo *p = pids[i];
|
2004-06-07 06:26:32 +02:00
|
|
|
commune_result r;
|
|
|
|
if (p->pid != myself->pid)
|
2003-09-20 02:31:13 +02:00
|
|
|
{
|
2005-09-29 00:56:47 +02:00
|
|
|
r = p->commune_request (PICOM_FIFO, get_win32_name ());
|
2004-06-07 06:26:32 +02:00
|
|
|
if (r.handles[0] == NULL)
|
|
|
|
continue; // process doesn't own fifo
|
2005-04-16 07:20:01 +02:00
|
|
|
debug_printf ("pid %d, handles[0] %p, handles[1] %p", p->pid,
|
|
|
|
r.handles[0], r.handles[1]);
|
2003-09-20 02:31:13 +02:00
|
|
|
}
|
2004-06-07 06:26:32 +02:00
|
|
|
else
|
2003-09-20 02:31:13 +02:00
|
|
|
{
|
2004-06-07 06:26:32 +02:00
|
|
|
/* FIXME: racy? */
|
|
|
|
fhandler_fifo *fh = cygheap->fdtab.find_fifo (get_win32_name ());
|
|
|
|
if (!fh)
|
2003-09-20 02:31:13 +02:00
|
|
|
continue;
|
2004-06-07 06:26:32 +02:00
|
|
|
if (!DuplicateHandle (hMainProc, fh->get_handle (), hMainProc,
|
|
|
|
&r.handles[0], 0, false, DUPLICATE_SAME_ACCESS))
|
2003-09-20 02:31:13 +02:00
|
|
|
{
|
|
|
|
__seterrno ();
|
2004-06-07 06:26:32 +02:00
|
|
|
goto out;
|
2003-09-20 02:31:13 +02:00
|
|
|
}
|
2005-04-17 02:12:14 +02:00
|
|
|
if (!DuplicateHandle (hMainProc, fh->get_output_handle (), hMainProc,
|
2004-06-07 06:26:32 +02:00
|
|
|
&r.handles[1], 0, false, DUPLICATE_SAME_ACCESS))
|
2003-09-20 02:31:13 +02:00
|
|
|
{
|
2004-06-07 06:26:32 +02:00
|
|
|
CloseHandle (r.handles[0]);
|
|
|
|
__seterrno ();
|
|
|
|
goto out;
|
2003-09-20 02:31:13 +02:00
|
|
|
}
|
|
|
|
}
|
2004-06-07 06:26:32 +02:00
|
|
|
|
2005-04-22 15:58:09 +02:00
|
|
|
set_io_handle (r.handles[0]);
|
|
|
|
set_output_handle (r.handles[1]);
|
2003-09-20 02:31:13 +02:00
|
|
|
set_flags (flags);
|
2005-04-22 15:58:09 +02:00
|
|
|
close_one_end ();
|
|
|
|
res = 1;
|
2003-09-20 02:31:13 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_errno (EAGAIN);
|
|
|
|
|
|
|
|
out:
|
|
|
|
debug_printf ("res %d", res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2006-06-23 02:19:39 +02:00
|
|
|
#define FIFO_PREFIX "_cygfifo_"
|
|
|
|
|
2003-09-20 02:31:13 +02:00
|
|
|
int
|
|
|
|
fhandler_fifo::open (int flags, mode_t)
|
|
|
|
{
|
|
|
|
int res = 1;
|
2006-06-23 02:19:39 +02:00
|
|
|
char mutex[CYG_MAX_PATH];
|
|
|
|
char *emutex = mutex + CYG_MAX_PATH;
|
|
|
|
char *p, *p1;
|
2006-07-17 21:30:30 +02:00
|
|
|
DWORD resw;
|
2006-06-23 02:19:39 +02:00
|
|
|
|
|
|
|
/* Generate a semi-unique name to associate with this fifo but try to ensure
|
|
|
|
that it is no larger than CYG_MAX_PATH */
|
2007-03-29 18:37:36 +02:00
|
|
|
strcpy (mutex, cygheap->shared_prefix);
|
|
|
|
for (p = mutex + strlen (mutex), p1 = strchr (get_name (), '\0');
|
2006-06-23 02:19:39 +02:00
|
|
|
--p1 >= get_name () && p < emutex ; p++)
|
|
|
|
*p = (*p1 == '/') ? '_' : *p1;
|
|
|
|
strncpy (p, FIFO_PREFIX, emutex - p);
|
|
|
|
mutex[CYG_MAX_PATH - 1] = '\0';
|
|
|
|
|
|
|
|
/* Create a mutex lock access to this fifo to prevent a race by two processes
|
|
|
|
trying to figure out if they own the fifo or if they should create it. */
|
|
|
|
HANDLE h = CreateMutex (&sec_none_nih, false, mutex);
|
|
|
|
if (!h)
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
system_printf ("couldn't open fifo mutex '%s', %E", mutex);
|
|
|
|
res = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
lock_process::locker.release (); /* Since we may be a while, release the
|
|
|
|
process lock that is held when we
|
|
|
|
open an fd. */
|
|
|
|
/* FIXME? Need to wait for signal here?
|
|
|
|
This shouldn't block for long, but... */
|
2006-07-17 21:30:30 +02:00
|
|
|
resw = WaitForSingleObject (h, INFINITE);
|
2006-06-23 02:19:39 +02:00
|
|
|
lock_process::locker.acquire (); /* Restore the lock */
|
|
|
|
if (resw != WAIT_OBJECT_0 && resw != WAIT_ABANDONED_0)
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
system_printf ("Wait for fifo mutex '%s' failed, %E", mutex);
|
|
|
|
goto out;
|
|
|
|
}
|
2003-09-20 02:31:13 +02:00
|
|
|
|
2004-06-07 06:26:32 +02:00
|
|
|
set_io_handle (NULL);
|
|
|
|
set_output_handle (NULL);
|
|
|
|
if (open_not_mine (flags))
|
|
|
|
goto out;
|
|
|
|
|
2003-09-20 02:31:13 +02:00
|
|
|
fhandler_pipe *fhs[2];
|
2005-04-17 02:12:14 +02:00
|
|
|
if (create (fhs, 1, flags, true))
|
2004-06-07 06:26:32 +02:00
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
res = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-04-17 02:12:14 +02:00
|
|
|
set_flags (flags);
|
2004-06-07 06:26:32 +02:00
|
|
|
set_io_handle (fhs[0]->get_handle ());
|
|
|
|
set_output_handle (fhs[1]->get_handle ());
|
|
|
|
guard = fhs[0]->guard;
|
|
|
|
read_state = fhs[0]->read_state;
|
|
|
|
delete (fhs[0]);
|
|
|
|
delete (fhs[1]);
|
|
|
|
set_use (1);
|
2005-04-16 07:20:01 +02:00
|
|
|
need_fork_fixup (true);
|
2004-06-07 06:26:32 +02:00
|
|
|
}
|
2003-09-20 02:31:13 +02:00
|
|
|
|
|
|
|
out:
|
2006-06-23 02:19:39 +02:00
|
|
|
if (h)
|
|
|
|
{
|
|
|
|
ReleaseMutex (h);
|
|
|
|
CloseHandle (h);
|
|
|
|
}
|
2003-09-20 02:31:13 +02:00
|
|
|
debug_printf ("returning %d, errno %d", res, get_errno ());
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-09-28 21:22:25 +02:00
|
|
|
fhandler_fifo::dup (fhandler_base *child)
|
2003-09-20 02:31:13 +02:00
|
|
|
{
|
2005-09-28 21:22:25 +02:00
|
|
|
int res = fhandler_pipe::dup (child);
|
2003-09-20 02:31:13 +02:00
|
|
|
if (!res)
|
|
|
|
{
|
|
|
|
fhandler_fifo *ff = (fhandler_fifo *) child;
|
2005-06-07 20:41:31 +02:00
|
|
|
if (get_output_handle ()
|
2005-09-28 21:22:25 +02:00
|
|
|
&& !DuplicateHandle (hMainProc, get_output_handle (), hMainProc,
|
2005-06-07 20:41:31 +02:00
|
|
|
&ff->get_output_handle (), false, true,
|
|
|
|
DUPLICATE_SAME_ACCESS))
|
2003-09-20 02:31:13 +02:00
|
|
|
{
|
2005-04-16 07:20:01 +02:00
|
|
|
__seterrno ();
|
2003-09-20 02:31:13 +02:00
|
|
|
child->close ();
|
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
}
|
2005-06-07 20:41:31 +02:00
|
|
|
return res;
|
2003-09-20 02:31:13 +02:00
|
|
|
}
|
2007-02-27 13:58:56 +01:00
|
|
|
|
|
|
|
int __stdcall
|
|
|
|
fhandler_fifo::fstatvfs (struct statvfs *sfs)
|
|
|
|
{
|
|
|
|
/* Call statvfs on parent dir. */
|
|
|
|
char *c, dir[CYG_MAX_PATH];
|
|
|
|
strcpy (dir, get_name ());
|
|
|
|
if ((c = strrchr (dir, '/')))
|
|
|
|
{
|
|
|
|
*c = '\0';
|
|
|
|
return statvfs (dir, sfs);
|
|
|
|
}
|
|
|
|
set_errno (EBADF);
|
|
|
|
return -1;
|
|
|
|
}
|