* dtable.cc (dtable::fixup_close): Define new function.

(dtable::fixup_after_exec): Use fixup_close() and detect when it was not
possible to open an inherited file handle.
(dtable::fixup_after_fork): Defensively close any file handles which were not,
for some reason, inheritable.
* dtable.h: Make #pragma once.
(dtable::fixup_close): Declare new function.
* fhandler_console.cc (fhandler_console::set_unit): Set I/O handles to NULL
when this function fails.
This commit is contained in:
Christopher Faylor
2012-04-01 22:28:39 +00:00
parent c4ee9311c2
commit 881beea81d
4 changed files with 47 additions and 14 deletions

View File

@ -834,6 +834,17 @@ dtable::set_file_pointers_for_exec ()
unlock ();
}
void
dtable::fixup_close (size_t i, fhandler_base *fh)
{
if (fh->archetype)
{
debug_printf ("closing fd %d since it is an archetype", i);
fh->close_with_arch ();
}
release (i);
}
void
dtable::fixup_after_exec ()
{
@ -844,15 +855,11 @@ dtable::fixup_after_exec ()
{
fh->clear_readahead ();
fh->fixup_after_exec ();
if (fh->close_on_exec ())
{
if (fh->archetype)
{
debug_printf ("closing fd %d since it is an archetype", i);
fh->close_with_arch ();
}
release (i);
}
/* Close the handle if it's close-on-exec or if an error was detected
(typically with opening a console in a gui app) by fixup_after_exec.
*/
if (fh->close_on_exec () || !fh->get_io_handle ())
fixup_close (i, fh);
else if (fh->get_popen_pid ())
close (i);
else if (i == 0)
@ -873,6 +880,13 @@ dtable::fixup_after_fork (HANDLE parent)
{
debug_printf ("fd %d (%s)", i, fh->get_name ());
fh->fixup_after_fork (parent);
if (!fh->get_io_handle ())
{
/* This should actually never happen but it's here to make sure
we don't crash due to access of an unopened file handle. */
fixup_close (i, fh);
continue;
}
}
if (i == 0)
SetStdHandle (std_consts[i], fh->get_io_handle ());