* dtable.cc (dtable::find_fifo): Release lock after fifo found (still racy).

* fhandler.h (fhandler_fifo::get_io_handle): New fifo-specific method.
* fhandler_fifo.cc (fhandler_fifo::close): Close output_handle only if it is
open.
(fhandler_fifo::open_not_mine): Reorganize slightly.  Don't call _pinfo methods
when the fifo is owned by me or suffer dtable lock_cs deadlock.
(fhandler_fifo::open): Call open_not_mine first, otherwise open myself
(racy).
* pinfo.cc (_pinfo::commune_recv): Duplicate fifo handles here in requesting
processes arena to avoid one potential race (of many).
(_pinfo::commune_send): Move all PICOM_FIFO code under one case statement.
* thread.cc (pthread::init_mainthread) Use existing hMainProc handle rather
than calling GetCurrentProcess.
This commit is contained in:
Christopher Faylor 2004-06-07 04:26:32 +00:00
parent 6a02213b52
commit beffbc5efd
6 changed files with 119 additions and 71 deletions

View File

@ -1,3 +1,22 @@
2004-06-07 Christopher Faylor <cgf@alum.bu.edu>
* dtable.cc (dtable::find_fifo): Release lock after fifo found (still
racy).
* fhandler.h (fhandler_fifo::get_io_handle): New fifo-specific method.
* fhandler_fifo.cc (fhandler_fifo::close): Close output_handle only if
it is open.
(fhandler_fifo::open_not_mine): Reorganize slightly. Don't call _pinfo
methods when the fifo is owned by me or suffer dtable lock_cs deadlock.
(fhandler_fifo::open): Call open_not_mine first, otherwise open myself
(racy).
* pinfo.cc (_pinfo::commune_recv): Duplicate fifo handles here in
requesting processes arena to avoid one potential race (of many).
(_pinfo::commune_send): Move all PICOM_FIFO code under one case
statement.
* thread.cc (pthread::init_mainthread) Use existing hMainProc handle
rather than calling GetCurrentProcess.
2004-06-04 Christopher Faylor <cgf@alum.bu.edu> 2004-06-04 Christopher Faylor <cgf@alum.bu.edu>
* winbase.h (ilockincr): Add more neverending changes from the * winbase.h (ilockincr): Add more neverending changes from the

View File

@ -557,13 +557,18 @@ fhandler_fifo *
dtable::find_fifo (const char *path) dtable::find_fifo (const char *path)
{ {
lock (); lock ();
fhandler_fifo *fh_res = NULL;
for (unsigned i = 0; i < size; i++) for (unsigned i = 0; i < size; i++)
{ {
fhandler_base *fh = fds[i]; fhandler_base *fh = fds[i];
if (fh && fh->isfifo () && strcmp (path, fh->get_win32_name ()) == 0) if (fh && fh->isfifo () && strcmp (path, fh->get_win32_name ()) == 0)
return (fhandler_fifo *) fh; {
fh_res = (fhandler_fifo *) fh;
break;
}
} }
return NULL; unlock ();
return fh_res;
} }
select_record * select_record *

View File

@ -324,6 +324,7 @@ class fhandler_base
bool is_fs_special () {return pc.is_fs_special ();} bool is_fs_special () {return pc.is_fs_special ();}
bool device_access_denied (int) __attribute__ ((regparm (2))); bool device_access_denied (int) __attribute__ ((regparm (2)));
int fhaccess (int flags) __attribute__ ((regparm (2))); int fhaccess (int flags) __attribute__ ((regparm (2)));
friend class fhandler_fifo;
}; };
class fhandler_socket: public fhandler_base class fhandler_socket: public fhandler_base
@ -454,6 +455,7 @@ class fhandler_fifo: public fhandler_pipe
HANDLE owner; // You can't have too many mutexes, now, can you? HANDLE owner; // You can't have too many mutexes, now, can you?
long read_use; long read_use;
long write_use; long write_use;
virtual HANDLE& get_io_handle () { return io_handle ?: output_handle; }
public: public:
fhandler_fifo (); fhandler_fifo ();
int open (int flags, mode_t mode = 0); int open (int flags, mode_t mode = 0);

View File

@ -60,7 +60,8 @@ int
fhandler_fifo::close () fhandler_fifo::close ()
{ {
fhandler_pipe::close (); fhandler_pipe::close ();
CloseHandle (get_output_handle ()); if (get_output_handle ())
CloseHandle (get_output_handle ());
set_use (-1); set_use (-1);
return 0; return 0;
} }
@ -72,44 +73,53 @@ fhandler_fifo::open_not_mine (int flags)
winpids pids; winpids pids;
static int flagtypes[] = {DUMMY_O_RDONLY | O_RDWR, O_WRONLY | O_APPEND | O_RDWR}; static int flagtypes[] = {DUMMY_O_RDONLY | O_RDWR, O_WRONLY | O_APPEND | O_RDWR};
HANDLE *usehandles[2] = {&(get_handle ()), &(get_output_handle ())}; HANDLE *usehandles[2] = {&(get_handle ()), &(get_output_handle ())};
int res; int res = 0;
int testflags = (flags & (O_RDWR | O_WRONLY | O_APPEND)) ?: DUMMY_O_RDONLY;
for (unsigned i = 0; i < pids.npids; i++) for (unsigned i = 0; i < pids.npids; i++)
{ {
_pinfo *p = pids[i]; _pinfo *p = pids[i];
HANDLE hp = OpenProcess (PROCESS_DUP_HANDLE, false, p->dwProcessId);
if (!hp)
{
__seterrno ();
goto err;
}
HANDLE handles[2];
commune_result r; commune_result r;
r = p->commune_send (PICOM_FIFO, get_win32_name ()); if (p->pid != myself->pid)
if (r.handles[0] == NULL)
continue; // process doesn't own fifo
flags = (flags & (O_RDWR | O_WRONLY | O_APPEND)) ?: DUMMY_O_RDONLY;
for (int i = 0; i < 2; i++)
{ {
if (!(flags & flagtypes[i])) r = p->commune_send (PICOM_FIFO, get_win32_name ());
if (r.handles[0] == NULL)
continue; // process doesn't own fifo
}
else
{
/* FIXME: racy? */
fhandler_fifo *fh = cygheap->fdtab.find_fifo (get_win32_name ());
if (!fh)
continue; continue;
if (!DuplicateHandle (hp, r.handles[i], hMainProc, usehandles[i], 0, if (!DuplicateHandle (hMainProc, fh->get_handle (), hMainProc,
false, DUPLICATE_SAME_ACCESS)) &r.handles[0], 0, false, DUPLICATE_SAME_ACCESS))
{ {
debug_printf ("couldn't duplicate handle %d/%p, %E", i, handles[i]);
__seterrno (); __seterrno ();
goto err; goto out;
} }
if (!DuplicateHandle (hMainProc, fh->get_handle (), hMainProc,
if (i == 0) &r.handles[1], 0, false, DUPLICATE_SAME_ACCESS))
{ {
read_state = CreateEvent (&sec_none_nih, FALSE, FALSE, NULL); CloseHandle (r.handles[0]);
need_fork_fixup (true); __seterrno ();
goto out;
} }
} }
CloseHandle (hp);
for (int i = 0; i < 2; i++)
if (!(testflags & flagtypes[i]))
CloseHandle (r.handles[i]);
else
{
*usehandles[i] = r.handles[i];
if (i == 0)
{
read_state = CreateEvent (&sec_none_nih, FALSE, FALSE, NULL);
need_fork_fixup (true);
}
}
res = 1; res = 1;
set_flags (flags); set_flags (flags);
@ -118,10 +128,6 @@ fhandler_fifo::open_not_mine (int flags)
set_errno (EAGAIN); set_errno (EAGAIN);
err:
res = 0;
debug_printf ("failed");
out: out:
debug_printf ("res %d", res); debug_printf ("res %d", res);
return res; return res;
@ -132,26 +138,31 @@ fhandler_fifo::open (int flags, mode_t)
{ {
int res = 1; int res = 1;
set_io_handle (NULL);
set_output_handle (NULL);
if (open_not_mine (flags))
goto out;
fhandler_pipe *fhs[2]; fhandler_pipe *fhs[2];
if (create (fhs, 0, flags, true)) if (create (fhs, 0, flags, true))
goto errnout; {
__seterrno ();
set_flags (fhs[0]->get_flags ()); res = 0;
set_io_handle (fhs[0]->get_handle ()); }
set_output_handle (fhs[1]->get_handle ()); else
guard = fhs[0]->guard; {
read_state = fhs[0]->read_state; set_flags (fhs[0]->get_flags ());
writepipe_exists = fhs[1]->writepipe_exists; set_io_handle (fhs[0]->get_handle ());
orig_pid = fhs[0]->orig_pid; set_output_handle (fhs[1]->get_handle ());
id = fhs[0]->id; guard = fhs[0]->guard;
delete (fhs[0]); read_state = fhs[0]->read_state;
delete (fhs[1]); writepipe_exists = fhs[1]->writepipe_exists;
set_use (1); orig_pid = fhs[0]->orig_pid;
goto out; id = fhs[0]->id;
delete (fhs[0]);
errnout: delete (fhs[1]);
__seterrno (); set_use (1);
res = 0; }
out: out:
debug_printf ("returning %d, errno %d", res, get_errno ()); debug_printf ("returning %d, errno %d", res, get_errno ());

View File

@ -351,11 +351,11 @@ _pinfo::commune_recv ()
return; return;
} }
CloseHandle (hp);
hello_pid = 0; hello_pid = 0;
if (!ReadFile (__fromthem, &code, sizeof code, &nr, NULL) || nr != sizeof code) if (!ReadFile (__fromthem, &code, sizeof code, &nr, NULL) || nr != sizeof code)
{ {
CloseHandle (hp);
/* __seterrno ();*/ // this is run from the signal thread, so don't set errno /* __seterrno ();*/ // this is run from the signal thread, so don't set errno
goto out; goto out;
} }
@ -368,6 +368,8 @@ _pinfo::commune_recv ()
CloseHandle (__fromthem); __fromthem = NULL; CloseHandle (__fromthem); __fromthem = NULL;
extern int __argc_safe; extern int __argc_safe;
const char *argv[__argc_safe + 1]; const char *argv[__argc_safe + 1];
CloseHandle (hp);
for (int i = 0; i < __argc_safe; i++) for (int i = 0; i < __argc_safe; i++)
{ {
if (IsBadStringPtr (__argv[i], INT32_MAX)) if (IsBadStringPtr (__argv[i], INT32_MAX))
@ -403,6 +405,7 @@ _pinfo::commune_recv ()
if (!ReadFile (__fromthem, &len, sizeof len, &nr, NULL) if (!ReadFile (__fromthem, &len, sizeof len, &nr, NULL)
|| nr != sizeof len) || nr != sizeof len)
{ {
CloseHandle (hp);
/* __seterrno ();*/ // this is run from the signal thread, so don't set errno /* __seterrno ();*/ // this is run from the signal thread, so don't set errno
goto out; goto out;
} }
@ -410,6 +413,7 @@ _pinfo::commune_recv ()
if (!ReadFile (__fromthem, path, len, &nr, NULL) if (!ReadFile (__fromthem, path, len, &nr, NULL)
|| nr != len) || nr != len)
{ {
CloseHandle (hp);
/* __seterrno ();*/ // this is run from the signal thread, so don't set errno /* __seterrno ();*/ // this is run from the signal thread, so don't set errno
goto out; goto out;
} }
@ -422,8 +426,16 @@ _pinfo::commune_recv ()
{ {
it[0] = fh->get_handle (); it[0] = fh->get_handle ();
it[1] = fh->get_output_handle (); it[1] = fh->get_output_handle ();
for (int i = 0; i < 2; i++)
if (!DuplicateHandle (hMainProc, it[i], hp, &it[i], 0, false,
DUPLICATE_SAME_ACCESS))
{
it[0] = it[1] = NULL; /* FIXME: possibly left a handle open in child? */
break;
}
} }
CloseHandle (hp);
if (!WriteFile (__tothem, it, sizeof (it), &nr, NULL)) if (!WriteFile (__tothem, it, sizeof (it), &nr, NULL))
{ {
/*__seterrno ();*/ // this is run from the signal thread, so don't set errno /*__seterrno ();*/ // this is run from the signal thread, so don't set errno
@ -442,7 +454,7 @@ out:
CloseHandle (__tothem); CloseHandle (__tothem);
} }
#define PIPEBUFSIZE (16 * sizeof (DWORD)) #define PIPEBUFSIZE (4096 * sizeof (DWORD))
commune_result commune_result
_pinfo::commune_send (DWORD code, ...) _pinfo::commune_send (DWORD code, ...)
@ -485,21 +497,6 @@ _pinfo::commune_send (DWORD code, ...)
goto err; goto err;
} }
switch (code)
{
case PICOM_FIFO:
{
char *path = va_arg (args, char *);
size_t len = strlen (path) + 1;
if (!WriteFile (tothem, path, len, &nr, NULL) || nr != len)
{
__seterrno ();
goto err;
}
break;
}
}
if (sig_send (this, __SIGCOMMUNE)) if (sig_send (this, __SIGCOMMUNE))
goto err; goto err;
@ -550,10 +547,25 @@ _pinfo::commune_send (DWORD code, ...)
break; break;
case PICOM_FIFO: case PICOM_FIFO:
{ {
char *path = va_arg (args, char *);
size_t len = strlen (path) + 1;
if (!WriteFile (tothem, &len, sizeof (len), &nr, NULL)
|| nr != sizeof (len))
{
__seterrno ();
goto err;
}
if (!WriteFile (tothem, path, len, &nr, NULL) || nr != len)
{
__seterrno ();
goto err;
}
DWORD x = ReadFile (fromthem, res.handles, sizeof (res.handles), &nr, NULL); DWORD x = ReadFile (fromthem, res.handles, sizeof (res.handles), &nr, NULL);
WriteFile (tothem, &x, sizeof (x), &x, NULL); (void) WriteFile (tothem, &x, sizeof (x), &x, NULL);
if (!x) if (!x)
goto err; goto err;
if (nr != sizeof (res.handles)) if (nr != sizeof (res.handles))
{ {
set_errno (EPIPE); set_errno (EPIPE);

View File

@ -178,9 +178,8 @@ pthread::init_mainthread ()
set_tls_self_pointer (thread); set_tls_self_pointer (thread);
thread->thread_id = GetCurrentThreadId (); thread->thread_id = GetCurrentThreadId ();
if (!DuplicateHandle (GetCurrentProcess (), GetCurrentThread (), if (!DuplicateHandle (hMainProc, GetCurrentThread (), hMainProc,
GetCurrentProcess (), &thread->win32_obj_id, &thread->win32_obj_id, 0, FALSE, DUPLICATE_SAME_ACCESS))
0, FALSE, DUPLICATE_SAME_ACCESS))
api_fatal ("failed to create mainthread handle"); api_fatal ("failed to create mainthread handle");
if (!thread->create_cancel_event ()) if (!thread->create_cancel_event ())
api_fatal ("couldn't create cancel event for main thread"); api_fatal ("couldn't create cancel event for main thread");