Cygwin: FIFO: simplify the fifo_client_handler structure
Replace the 'fhandler_base *' member by a HANDLE to the server side of the Windows named pipe instance. Make the corresponding simplifications throughout.
This commit is contained in:
parent
d05124dc6b
commit
ce23e97640
|
@ -1284,10 +1284,10 @@ enum
|
||||||
|
|
||||||
struct fifo_client_handler
|
struct fifo_client_handler
|
||||||
{
|
{
|
||||||
fhandler_base *fh;
|
HANDLE h;
|
||||||
fifo_client_connect_state state;
|
fifo_client_connect_state state;
|
||||||
fifo_client_handler () : fh (NULL), state (fc_unknown) {}
|
fifo_client_handler () : h (NULL), state (fc_unknown) {}
|
||||||
int close ();
|
void close () { NtClose (h); }
|
||||||
/* Returns FILE_PIPE_DISCONNECTED_STATE, FILE_PIPE_LISTENING_STATE,
|
/* Returns FILE_PIPE_DISCONNECTED_STATE, FILE_PIPE_LISTENING_STATE,
|
||||||
FILE_PIPE_CONNECTED_STATE, FILE_PIPE_CLOSING_STATE,
|
FILE_PIPE_CONNECTED_STATE, FILE_PIPE_CLOSING_STATE,
|
||||||
FILE_PIPE_INPUT_AVAILABLE_STATE, or -1 on error. */
|
FILE_PIPE_INPUT_AVAILABLE_STATE, or -1 on error. */
|
||||||
|
@ -1312,7 +1312,7 @@ class fhandler_fifo: public fhandler_base
|
||||||
HANDLE create_pipe_instance (bool);
|
HANDLE create_pipe_instance (bool);
|
||||||
NTSTATUS open_pipe (HANDLE&);
|
NTSTATUS open_pipe (HANDLE&);
|
||||||
int add_client_handler ();
|
int add_client_handler ();
|
||||||
int delete_client_handler (int);
|
void delete_client_handler (int);
|
||||||
bool listen_client ();
|
bool listen_client ();
|
||||||
int stop_listen_client ();
|
int stop_listen_client ();
|
||||||
int check_listen_client_thread ();
|
int check_listen_client_thread ();
|
||||||
|
@ -1321,8 +1321,7 @@ public:
|
||||||
fhandler_fifo ();
|
fhandler_fifo ();
|
||||||
bool hit_eof ();
|
bool hit_eof ();
|
||||||
int get_nhandlers () const { return nhandlers; }
|
int get_nhandlers () const { return nhandlers; }
|
||||||
HANDLE get_fc_handle (int i) const
|
HANDLE get_fc_handle (int i) const { return fc_handler[i].h; }
|
||||||
{ return fc_handler[i].fh->get_handle (); }
|
|
||||||
bool is_connected (int i) const
|
bool is_connected (int i) const
|
||||||
{ return fc_handler[i].state == fc_connected; }
|
{ return fc_handler[i].state == fc_connected; }
|
||||||
PUNICODE_STRING get_pipe_name ();
|
PUNICODE_STRING get_pipe_name ();
|
||||||
|
@ -1345,12 +1344,6 @@ public:
|
||||||
void fixup_after_fork (HANDLE);
|
void fixup_after_fork (HANDLE);
|
||||||
void fixup_after_exec ();
|
void fixup_after_exec ();
|
||||||
int __reg2 fstatvfs (struct statvfs *buf);
|
int __reg2 fstatvfs (struct statvfs *buf);
|
||||||
void clear_readahead ()
|
|
||||||
{
|
|
||||||
fhandler_base::clear_readahead ();
|
|
||||||
for (int i = 0; i < nhandlers; i++)
|
|
||||||
fc_handler[i].fh->clear_readahead ();
|
|
||||||
}
|
|
||||||
select_record *select_read (select_stuff *);
|
select_record *select_read (select_stuff *);
|
||||||
select_record *select_write (select_stuff *);
|
select_record *select_write (select_stuff *);
|
||||||
select_record *select_except (select_stuff *);
|
select_record *select_except (select_stuff *);
|
||||||
|
@ -1374,8 +1367,6 @@ public:
|
||||||
/* fhf->pipe_name_buf is a *copy* of this->pipe_name_buf, but
|
/* fhf->pipe_name_buf is a *copy* of this->pipe_name_buf, but
|
||||||
fhf->pipe_name.Buffer == this->pipe_name_buf. */
|
fhf->pipe_name.Buffer == this->pipe_name_buf. */
|
||||||
fhf->pipe_name.Buffer = fhf->pipe_name_buf;
|
fhf->pipe_name.Buffer = fhf->pipe_name_buf;
|
||||||
for (int i = 0; i < nhandlers; i++)
|
|
||||||
fhf->fc_handler[i].fh = fc_handler[i].fh->clone ();
|
|
||||||
return fhf;
|
return fhf;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -252,7 +252,6 @@ fhandler_fifo::add_client_handler ()
|
||||||
{
|
{
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
fifo_client_handler fc;
|
fifo_client_handler fc;
|
||||||
fhandler_base *fh;
|
|
||||||
HANDLE ph = NULL;
|
HANDLE ph = NULL;
|
||||||
bool first = (nhandlers == 0);
|
bool first = (nhandlers == 0);
|
||||||
|
|
||||||
|
@ -261,40 +260,26 @@ fhandler_fifo::add_client_handler ()
|
||||||
set_errno (EMFILE);
|
set_errno (EMFILE);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
if (!(fh = build_fh_dev (dev ())))
|
|
||||||
{
|
|
||||||
set_errno (EMFILE);
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
ph = create_pipe_instance (first);
|
ph = create_pipe_instance (first);
|
||||||
if (!ph)
|
if (!ph)
|
||||||
{
|
goto out;
|
||||||
delete fh;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fh->set_handle (ph);
|
|
||||||
fh->set_flags ((openflags & ~O_ACCMODE) | O_RDONLY);
|
|
||||||
fh->set_nonblocking (false);
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
fc.fh = fh;
|
fc.h = ph;
|
||||||
fifo_client_lock ();
|
|
||||||
fc_handler[nhandlers++] = fc;
|
fc_handler[nhandlers++] = fc;
|
||||||
fifo_client_unlock ();
|
|
||||||
}
|
}
|
||||||
out:
|
out:
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
void
|
||||||
fhandler_fifo::delete_client_handler (int i)
|
fhandler_fifo::delete_client_handler (int i)
|
||||||
{
|
{
|
||||||
int ret = fc_handler[i].close ();
|
fc_handler[i].close ();
|
||||||
if (i < --nhandlers)
|
if (i < --nhandlers)
|
||||||
memmove (fc_handler + i, fc_handler + i + 1,
|
memmove (fc_handler + i, fc_handler + i + 1,
|
||||||
(nhandlers - i) * sizeof (fc_handler[i]));
|
(nhandlers - i) * sizeof (fc_handler[i]));
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Just hop to the listen_client_thread method. */
|
/* Just hop to the listen_client_thread method. */
|
||||||
|
@ -331,8 +316,7 @@ fhandler_fifo::record_connection (fifo_client_handler& fc)
|
||||||
SetEvent (write_ready);
|
SetEvent (write_ready);
|
||||||
fc.state = fc_connected;
|
fc.state = fc_connected;
|
||||||
nconnected++;
|
nconnected++;
|
||||||
fc.fh->set_nonblocking (true);
|
set_pipe_non_blocking (fc.h, true);
|
||||||
set_pipe_non_blocking (fc.fh->get_handle (), true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DWORD
|
DWORD
|
||||||
|
@ -355,13 +339,7 @@ fhandler_fifo::listen_client_thread ()
|
||||||
while (i < nhandlers)
|
while (i < nhandlers)
|
||||||
{
|
{
|
||||||
if (fc_handler[i].state == fc_invalid)
|
if (fc_handler[i].state == fc_invalid)
|
||||||
{
|
delete_client_handler (i);
|
||||||
if (delete_client_handler (i) < 0)
|
|
||||||
{
|
|
||||||
fifo_client_unlock ();
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
@ -383,7 +361,7 @@ fhandler_fifo::listen_client_thread ()
|
||||||
NTSTATUS status;
|
NTSTATUS status;
|
||||||
IO_STATUS_BLOCK io;
|
IO_STATUS_BLOCK io;
|
||||||
|
|
||||||
status = NtFsControlFile (fc.fh->get_handle (), evt, NULL, NULL, &io,
|
status = NtFsControlFile (fc.h, evt, NULL, NULL, &io,
|
||||||
FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
|
FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
|
||||||
if (status == STATUS_PENDING)
|
if (status == STATUS_PENDING)
|
||||||
{
|
{
|
||||||
|
@ -424,8 +402,7 @@ fhandler_fifo::listen_client_thread ()
|
||||||
&& (NT_SUCCESS (io.Status) || io.Status == STATUS_PIPE_CONNECTED))
|
&& (NT_SUCCESS (io.Status) || io.Status == STATUS_PIPE_CONNECTED))
|
||||||
{
|
{
|
||||||
debug_printf ("successfully connected bogus client");
|
debug_printf ("successfully connected bogus client");
|
||||||
if (delete_client_handler (nhandlers - 1) < 0)
|
delete_client_handler (nhandlers - 1);
|
||||||
ret = -1;
|
|
||||||
}
|
}
|
||||||
else if ((ps = fc.pipe_state ()) == FILE_PIPE_CONNECTED_STATE
|
else if ((ps = fc.pipe_state ()) == FILE_PIPE_CONNECTED_STATE
|
||||||
|| ps == FILE_PIPE_INPUT_AVAILABLE_STATE)
|
|| ps == FILE_PIPE_INPUT_AVAILABLE_STATE)
|
||||||
|
@ -948,19 +925,6 @@ fhandler_fifo::fstatvfs (struct statvfs *sfs)
|
||||||
return fh.fstatvfs (sfs);
|
return fh.fstatvfs (sfs);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
|
||||||
fifo_client_handler::close ()
|
|
||||||
{
|
|
||||||
int res = 0;
|
|
||||||
|
|
||||||
if (fh)
|
|
||||||
{
|
|
||||||
res = fh->fhandler_base::close ();
|
|
||||||
delete fh;
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
int
|
||||||
fifo_client_handler::pipe_state ()
|
fifo_client_handler::pipe_state ()
|
||||||
{
|
{
|
||||||
|
@ -968,7 +932,7 @@ fifo_client_handler::pipe_state ()
|
||||||
FILE_PIPE_LOCAL_INFORMATION fpli;
|
FILE_PIPE_LOCAL_INFORMATION fpli;
|
||||||
NTSTATUS status;
|
NTSTATUS status;
|
||||||
|
|
||||||
status = NtQueryInformationFile (fh->get_handle (), &io, &fpli,
|
status = NtQueryInformationFile (h, &io, &fpli,
|
||||||
sizeof (fpli), FilePipeLocalInformation);
|
sizeof (fpli), FilePipeLocalInformation);
|
||||||
if (!NT_SUCCESS (status))
|
if (!NT_SUCCESS (status))
|
||||||
{
|
{
|
||||||
|
@ -1022,8 +986,7 @@ fhandler_fifo::close ()
|
||||||
NtClose (write_ready);
|
NtClose (write_ready);
|
||||||
fifo_client_lock ();
|
fifo_client_lock ();
|
||||||
for (int i = 0; i < nhandlers; i++)
|
for (int i = 0; i < nhandlers; i++)
|
||||||
if (fc_handler[i].close () < 0)
|
fc_handler[i].close ();
|
||||||
ret = -1;
|
|
||||||
fifo_client_unlock ();
|
fifo_client_unlock ();
|
||||||
return fhandler_base::close () || ret;
|
return fhandler_base::close () || ret;
|
||||||
}
|
}
|
||||||
|
@ -1078,9 +1041,9 @@ fhandler_fifo::dup (fhandler_base *child, int flags)
|
||||||
fifo_client_lock ();
|
fifo_client_lock ();
|
||||||
for (int i = 0; i < nhandlers; i++)
|
for (int i = 0; i < nhandlers; i++)
|
||||||
{
|
{
|
||||||
if (!DuplicateHandle (GetCurrentProcess (), fc_handler[i].fh->get_handle (),
|
if (!DuplicateHandle (GetCurrentProcess (), fc_handler[i].h,
|
||||||
GetCurrentProcess (),
|
GetCurrentProcess (),
|
||||||
&fhf->fc_handler[i].fh->get_handle (),
|
&fhf->fc_handler[i].h,
|
||||||
0, true, DUPLICATE_SAME_ACCESS))
|
0, true, DUPLICATE_SAME_ACCESS))
|
||||||
{
|
{
|
||||||
fifo_client_unlock ();
|
fifo_client_unlock ();
|
||||||
|
@ -1114,7 +1077,7 @@ fhandler_fifo::fixup_after_fork (HANDLE parent)
|
||||||
fork_fixup (parent, write_ready, "write_ready");
|
fork_fixup (parent, write_ready, "write_ready");
|
||||||
fifo_client_lock ();
|
fifo_client_lock ();
|
||||||
for (int i = 0; i < nhandlers; i++)
|
for (int i = 0; i < nhandlers; i++)
|
||||||
fc_handler[i].fh->fhandler_base::fixup_after_fork (parent);
|
fork_fixup (parent, fc_handler[i].h, "fc_handler[].h");
|
||||||
fifo_client_unlock ();
|
fifo_client_unlock ();
|
||||||
if (reader && !listen_client ())
|
if (reader && !listen_client ())
|
||||||
debug_printf ("failed to start lct, %E");
|
debug_printf ("failed to start lct, %E");
|
||||||
|
@ -1136,6 +1099,6 @@ fhandler_fifo::set_close_on_exec (bool val)
|
||||||
set_no_inheritance (write_ready, val);
|
set_no_inheritance (write_ready, val);
|
||||||
fifo_client_lock ();
|
fifo_client_lock ();
|
||||||
for (int i = 0; i < nhandlers; i++)
|
for (int i = 0; i < nhandlers; i++)
|
||||||
fc_handler[i].fh->fhandler_base::set_close_on_exec (val);
|
set_no_inheritance (fc_handler[i].h, val);
|
||||||
fifo_client_unlock ();
|
fifo_client_unlock ();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue