* cygheap.h (init_cygheap::manage_console_count): Declare new function.

(init_cygheap::console_count): Renamed from open_fhs.  Make private.
* cygheap.cc (init_cygheap::manage_console_count): Define new function.
* dtable.cc (dtable::fixup_after_exec): Always call fixup_after_exec on
elements of fd even when they are about to be closed.
* fhandler.h (report_tty_counts): Remove open_fhs from debugging output.
* fhandler_console.cc (fhandler_console::open): Use manage_console_count rather
than manipulating count directly.
(fhandler_console::close): Ditto.
(fhandler_console::fixup_after_fork): Ditto.
(fhandler_console::fixup_after_exec): Ditto.  Don't close handles if
close_on_exec.
* fhandler_tty.cc (fhandler_tty_slave::open): Use manage_console_count() rather
than manipulating count directly.  Reflect change in arguments to
report_tty_counts().
(fhandler_tty_slave::close): Ditto for both.
(fhandler_tty_slave::dup): Ditto for both.
(fhandler_tty_slave::ioctl): Use myctty() rather than raw ctty #.
(fhandler_tty_slave::fixup_after_fork): Reflect change in arguments to
report_tty_counts().
(fhandler_tty_master::init_console): Use manage_console_count() rather than
manipulating count directly.
* fhandler_clipboard.cc (fhandler_dev_clipboard::fixup_after_exec): Don't
perform any operations if close_on_exec.
* fhandler_dsp.cc (fhandler_dev_dsp::fixup_after_exec): Ditto.
* fhandler_raw.cc (fhandler_dev_raw::fixup_after_exec): Ditto.
* fhandler_serial.cc (fhandler_serial::fixup_after_exec): Ditto.
* pinfo.h (_pinfo::_ctty): Declare new function.
(myctty): Declare new macro.
(__ctty): Declare new macro.
* pinfo.cc (_pinfo::_ctty): Define new function.
(_pinfo::set_ctty): Use manage_console_count() rather than manipulating count
directly.
* signal.cc (kill_pgrp): Use myctty() and __ctty() macros rather than raw ctty
#.
* syscalls.cc (setsid): Ditto.  Use manage_console_count() rather than
manipulating count directly.
This commit is contained in:
Christopher Faylor 2005-11-14 04:28:45 +00:00
parent 70c500b343
commit 59297e0464
18 changed files with 148 additions and 71 deletions

View File

@ -1,3 +1,44 @@
2005-11-13 Christopher Faylor <cgf@timesys.com>
* cygheap.h (init_cygheap::manage_console_count): Declare new function.
(init_cygheap::console_count): Renamed from open_fhs. Make private.
* cygheap.cc (init_cygheap::manage_console_count): Define new function.
* dtable.cc (dtable::fixup_after_exec): Always call fixup_after_exec on
elements of fd even when they are about to be closed.
* fhandler.h (report_tty_counts): Remove open_fhs from debugging
output.
* fhandler_console.cc (fhandler_console::open): Use
manage_console_count rather than manipulating count directly.
(fhandler_console::close): Ditto.
(fhandler_console::fixup_after_fork): Ditto.
(fhandler_console::fixup_after_exec): Ditto. Don't close handles if
close_on_exec.
* fhandler_tty.cc (fhandler_tty_slave::open): Use
manage_console_count() rather than manipulating count directly.
Reflect change in arguments to report_tty_counts().
(fhandler_tty_slave::close): Ditto for both.
(fhandler_tty_slave::dup): Ditto for both.
(fhandler_tty_slave::ioctl): Use myctty() rather than raw ctty #.
(fhandler_tty_slave::fixup_after_fork): Reflect change in arguments to
report_tty_counts().
(fhandler_tty_master::init_console): Use manage_console_count() rather
than manipulating count directly.
* fhandler_clipboard.cc (fhandler_dev_clipboard::fixup_after_exec):
Don't perform any operations if close_on_exec.
* fhandler_dsp.cc (fhandler_dev_dsp::fixup_after_exec): Ditto.
* fhandler_raw.cc (fhandler_dev_raw::fixup_after_exec): Ditto.
* fhandler_serial.cc (fhandler_serial::fixup_after_exec): Ditto.
* pinfo.h (_pinfo::_ctty): Declare new function.
(myctty): Declare new macro.
(__ctty): Declare new macro.
* pinfo.cc (_pinfo::_ctty): Define new function.
(_pinfo::set_ctty): Use manage_console_count() rather than manipulating
count directly.
* signal.cc (kill_pgrp): Use myctty() and __ctty() macros rather than
raw ctty #.
* syscalls.cc (setsid): Ditto. Use manage_console_count() rather than
manipulating count directly.
2005-11-13 Corinna Vinschen <corinna@vinschen.de>
* fhandler_socket.cc (fhandler_socket::recvfrom): Just return 0 (EOF)

View File

@ -90,6 +90,20 @@ cygheap_fixup_in_child (bool execed)
}
}
int
init_cygheap::manage_console_count (const char *something, int amount, bool avoid_freeing_console)
{
console_count += amount;
debug_printf ("%s: console_count %d, amount %d, %s, avoid_freeing_console %d",
something, console_count, amount, myctty (), avoid_freeing_console);
if (!avoid_freeing_console && amount <= 0 && !console_count && myself->ctty == -1)
{
FreeConsole ();
debug_printf ("freed console");
}
return console_count;
}
void
init_cygheap::close_ctty ()
{

View File

@ -296,13 +296,16 @@ struct init_cygheap
#endif
struct _cygtls **threadlist;
size_t sthreads;
int open_fhs;
pid_t pid; /* my pid */
HANDLE pid_handle; /* handle for my pid */
hook_chain hooks;
void close_ctty ();
int manage_console_count (const char *, int, bool = false) __attribute__ ((regparm (3)));
private:
int console_count;
};
#define _CYGHEAPSIZE_SLOP (128 * 1024)
#define CYGHEAPSIZE (sizeof (init_cygheap) + (20000 * sizeof (fhandler_union)) + _CYGHEAPSIZE_SLOP)
#define CYGHEAPSIZE_MIN (sizeof (init_cygheap) + (10000 * sizeof (fhandler_union)))

View File

@ -683,20 +683,17 @@ dtable::fixup_after_exec ()
if ((fh = fds[i]) != NULL)
{
fh->clear_readahead ();
fh->fixup_after_exec ();
if (fh->close_on_exec ())
{
if (fh->archetype)
fh->close ();
release (i);
}
else
{
fh->fixup_after_exec ();
if (i == 0)
SetStdHandle (std_consts[i], fh->get_io_handle ());
else if (i <= 2)
SetStdHandle (std_consts[i], fh->get_output_handle ());
}
else if (i == 0)
SetStdHandle (std_consts[i], fh->get_io_handle ());
else if (i <= 2)
SetStdHandle (std_consts[i], fh->get_output_handle ());
}
}
@ -733,7 +730,7 @@ dtable::vfork_child_dup ()
if (cygheap->ctty)
{
cygheap->ctty->usecount++;
cygheap->open_fhs++;
cygheap->console_count++;
report_tty_counts (cygheap->ctty, "vfork dup", "incremented ", "");
}

View File

@ -1261,10 +1261,9 @@ struct fhandler_nodevice: public fhandler_base
// int __stdcall fstat (struct __stat64 *buf, path_conv *);
};
#define report_tty_counts(fh, call, fhs_op, use_op) \
termios_printf ("%s %s, %sopen_fhs %d, %susecount %d",\
#define report_tty_counts(fh, call, use_op) \
termios_printf ("%s %s, %susecount %d",\
fh->ttyname (), call,\
fhs_op, cygheap->open_fhs,\
use_op, ((fhandler_tty_slave *) fh)->archetype->usecount);
typedef union

View File

@ -276,7 +276,10 @@ fhandler_dev_clipboard::close ()
void
fhandler_dev_clipboard::fixup_after_exec ()
{
eof = false;
pos = msize = 0;
membuffer = NULL;
if (!close_on_exec ())
{
eof = false;
pos = msize = 0;
membuffer = NULL;
}
}

View File

@ -678,8 +678,7 @@ fhandler_console::open (int flags, mode_t)
tc->rstcons (false);
set_open_status ();
cygheap->open_fhs++;
debug_printf ("incremented open_fhs, now %d", cygheap->open_fhs);
cygheap->manage_console_count ("fhandler_console::open", 1);
debug_printf ("opened conin$ %p, conout$ %p", get_io_handle (),
get_output_handle ());
@ -691,12 +690,8 @@ fhandler_console::close ()
{
CloseHandle (get_io_handle ());
CloseHandle (get_output_handle ());
if (!hExeced && --(cygheap->open_fhs) <= 0 && myself->ctty != TTY_CONSOLE)
{
syscall_printf ("open_fhs %d", cygheap->open_fhs);
FreeConsole ();
}
debug_printf ("decremented open_fhs, now %d", cygheap->open_fhs);
if (!hExeced)
cygheap->manage_console_count ("fhandler_console::close", -1);
return 0;
}
@ -1771,10 +1766,13 @@ fhandler_console::fixup_after_fork (HANDLE)
/* Windows does not allow duplication of console handles between processes
so open the console explicitly. */
cygheap->open_fhs--; /* The downside of storing this in cygheap. */
if (!open (O_NOCTTY | get_flags (), 0))
system_printf ("error opening console after fork, %E");
/* Need to decrement console_count since this open is basically a no-op to reopen
the console and we've already recorded that fact. */
cygheap->manage_console_count ("fhandler_console::fixup_after_fork", -1);
if (!close_on_exec ())
{
CloseHandle (h);
@ -1802,8 +1800,9 @@ fhandler_console::fixup_after_exec ()
HANDLE h = get_handle ();
HANDLE oh = get_output_handle ();
cygheap->open_fhs--; /* The downside of storing this in cygheap. */
if (!open (O_NOCTTY | get_flags (), 0))
if (close_on_exec () || open (O_NOCTTY | get_flags (), 0))
cygheap->manage_console_count ("fhandler_console::fixup_after_exec", -1);
else
{
bool sawerr = false;
if (!get_io_handle ())
@ -1821,6 +1820,9 @@ fhandler_console::fixup_after_exec ()
system_printf ("error opening console after exec, errno %d, %E", get_errno ());
}
CloseHandle (h);
CloseHandle (oh);
if (!close_on_exec ())
{
CloseHandle (h);
CloseHandle (oh);
}
}

View File

@ -1361,11 +1361,14 @@ fhandler_dev_dsp::fixup_after_fork (HANDLE parent)
void
fhandler_dev_dsp::fixup_after_exec ()
{
debug_printf ("audio_in=%08x audio_out=%08x",
(int)audio_in_, (int)audio_out_);
if (archetype != this)
return ((fhandler_dev_dsp *)archetype)->fixup_after_exec ();
debug_printf ("audio_in=%08x audio_out=%08x, close_on_exec %d",
(int) audio_in_, (int) audio_out_, close_on_exec ());
if (!close_on_exec ())
{
if (archetype != this)
return ((fhandler_dev_dsp *) archetype)->fixup_after_exec ();
audio_in_ = NULL;
audio_out_ = NULL;
audio_in_ = NULL;
audio_out_ = NULL;
}
}

View File

@ -124,11 +124,14 @@ fhandler_dev_raw::fixup_after_fork (HANDLE)
void
fhandler_dev_raw::fixup_after_exec ()
{
if (devbufsiz > 1L)
devbuf = new char [devbufsiz];
devbufstart = 0;
devbufend = 0;
lastblk_to_read (false);
if (!close_on_exec ())
{
if (devbufsiz > 1L)
devbuf = new char [devbufsiz];
devbufstart = 0;
devbufend = 0;
lastblk_to_read (false);
}
}
int

View File

@ -1064,8 +1064,9 @@ fhandler_serial::fixup_after_fork (HANDLE parent)
void
fhandler_serial::fixup_after_exec ()
{
overlapped_setup ();
debug_printf ("io_status.hEvent %p", io_status.hEvent);
if (!close_on_exec ())
overlapped_setup ();
debug_printf ("io_status.hEvent %p, close_on_exec %d", io_status.hEvent, close_on_exec ());
}
int

View File

@ -432,7 +432,6 @@ fhandler_socket::fixup_after_fork (HANDLE parent)
void
fhandler_socket::fixup_after_exec ()
{
debug_printf ("here");
if (!close_on_exec ())
fixup_after_fork (NULL);
}

View File

@ -139,8 +139,8 @@ fhandler_termios::bg_check (int sig)
if (sig < 0)
sig = -sig;
termios_printf ("bg I/O pgid %d, tpgid %d, ctty %d",
myself->pgid, tc->getpgid (), myself->ctty);
termios_printf ("bg I/O pgid %d, tpgid %d, %s", myself->pgid, tc->getpgid (),
myctty ());
if (tc->getsid () == 0)
{

View File

@ -457,7 +457,7 @@ fhandler_tty_slave::open (int flags, mode_t)
*this = *(fhandler_tty_slave *) arch;
termios_printf ("copied tty fhandler archetype");
set_flags ((flags & ~O_TEXT) | O_BINARY);
cygheap->open_fhs++;
cygheap->manage_console_count ("fhandler_tty_slave::open<arch>", 1);
goto out;
}
@ -576,7 +576,8 @@ fhandler_tty_slave::open (int flags, mode_t)
set_output_handle (to_master_local);
set_open_status ();
if (cygheap->open_fhs++ == 0 && !GetConsoleCP () && !output_done_event
if (cygheap->manage_console_count ("fhandler_tty_slave::open", 1) == 1
&& !GetConsoleCP () && !output_done_event
&& wincap.pty_needs_alloc_console () && !GetProcessWindowStation ())
{
BOOL b;
@ -603,7 +604,7 @@ fhandler_tty_slave::open (int flags, mode_t)
out:
usecount = 0;
archetype->usecount++;
report_tty_counts (this, "opened", "incremented ", "");
report_tty_counts (this, "opened", "");
myself->set_ctty (get_ttyp (), flags, arch);
return 1;
@ -614,11 +615,10 @@ fhandler_tty_slave::close ()
{
if (!hExeced)
{
if (!--cygheap->open_fhs && myself->ctty == -1)
FreeConsole ();
cygheap->manage_console_count ("fhandler_tty_slave::close", -1);
archetype->usecount--;
report_tty_counts (this, "closed", "decremented ", "");
report_tty_counts (this, "closed", "");
if (archetype->usecount)
{
@ -921,8 +921,8 @@ fhandler_tty_slave::dup (fhandler_base *child)
*(fhandler_tty_slave *) child = *arch;
child->usecount = 0;
arch->usecount++;
cygheap->open_fhs++;
report_tty_counts (child, "duped", "incremented ", "");
cygheap->manage_console_count ("fhandler_tty_slave::dup", 1);
report_tty_counts (child, "duped", "");
myself->set_ctty (get_ttyp (), openflags, arch);
return 0;
}
@ -1070,8 +1070,8 @@ fhandler_tty_slave::ioctl (unsigned int cmd, void *arg)
&& myself->ctty == get_unit () && (get_ttyp ()->ti.c_lflag & TOSTOP))
{
/* background process */
termios_printf ("bg ioctl pgid %d, tpgid %d, ctty %d",
myself->pgid, get_ttyp ()->getpgid (), myself->ctty);
termios_printf ("bg ioctl pgid %d, tpgid %d, %s", myself->pgid,
get_ttyp ()->getpgid (), myctty ());
raise (SIGTTOU);
}
@ -1389,7 +1389,7 @@ void
fhandler_tty_slave::fixup_after_fork (HANDLE parent)
{
// fhandler_tty_common::fixup_after_fork (parent);
report_tty_counts (this, "inherited", "", "");
report_tty_counts (this, "inherited", "");
}
void
@ -1434,7 +1434,7 @@ fhandler_tty_master::init_console ()
return -1;
console->init (INVALID_HANDLE_VALUE, GENERIC_READ | GENERIC_WRITE, O_BINARY);
cygheap->open_fhs--; /* handled when individual fds are opened */
cygheap->manage_console_count ("fhandler_tty_master::init_console", -1, true);
console->uninterruptible_io (true);
return 0;
}

View File

@ -647,8 +647,8 @@ vfork ()
vf->sid = myself->sid;
vf->pgid = myself->pgid;
cygheap->ctty_on_hold = cygheap->ctty;
vf->open_fhs = cygheap->open_fhs;
debug_printf ("cygheap->ctty_on_hold %p, cygheap->open_fhs %d", cygheap->ctty_on_hold, cygheap->open_fhs);
vf->console_count = cygheap->console_count;
debug_printf ("cygheap->ctty_on_hold %p, cygheap->console_count %d", cygheap->ctty_on_hold, cygheap->console_count);
int res = cygheap->fdtab.vfork_child_dup () ? 0 : -1;
debug_printf ("%d = vfork()", res);
_my_tls.call_signal_handler (); // FIXME: racy
@ -668,7 +668,7 @@ vfork ()
myself->sid = vf->sid;
myself->pgid = vf->pgid;
termios_printf ("cygheap->ctty %p, cygheap->ctty_on_hold %p", cygheap->ctty, cygheap->ctty_on_hold);
cygheap->open_fhs = vf->open_fhs;
cygheap->console_count = vf->console_count;
if (vf->pid < 0)
{

View File

@ -315,6 +315,18 @@ pinfo::set_acl()
debug_printf ("SetKernelObjectSecurity %E");
}
const char *
_pinfo::_ctty (char *buf)
{
if (ctty == TTY_CONSOLE)
strcpy (buf, "ctty /dev/console");
else if (ctty < 0)
strcpy (buf, "no ctty");
else
__small_sprintf (buf, "ctty /dev/tty%d", ctty);
return buf;
}
void
_pinfo::set_ctty (tty_min *tc, int flags, fhandler_tty_slave *arch)
{
@ -353,8 +365,8 @@ _pinfo::set_ctty (tty_min *tc, int flags, fhandler_tty_slave *arch)
if (arch)
{
arch->usecount++;
cygheap->open_fhs++;
report_tty_counts (cygheap->ctty, "ctty", "incremented ", "");
cygheap->manage_console_count ("pinfo::set_ctty", 1);
report_tty_counts (cygheap->ctty, "ctty", "");
}
}
}

View File

@ -119,6 +119,7 @@ public:
bool alert_parent (char);
int __stdcall kill (siginfo_t&) __attribute__ ((regparm (2)));
bool __stdcall exists () __attribute__ ((regparm (1)));
const char *_ctty (char *);
friend void __stdcall set_myself (HANDLE);
@ -240,6 +241,9 @@ extern pinfo myself;
#define _P_VFORK 0
#define _P_SYSTEM 512
#define __ctty() _ctty ((char *) alloca (sizeof ("ctty /dev/tty") + 20))
#define myctty() myself->__ctty ()
/* For mmaps across fork(). */
int __stdcall fixup_mmaps_after_fork (HANDLE parent);
/* for shm areas across fork (). */

View File

@ -289,8 +289,8 @@ kill_pgrp (pid_t pid, siginfo_t& si)
(pid > 1 && p->pgid != pid) ||
(si.si_signo < 0 && NOTSTATE (p, PID_STOPPED)))
continue;
sigproc_printf ("killing pid %d, pgrp %d, p->ctty %d, myself->ctty %d",
p->pid, p->pgid, p->ctty, myself->ctty);
sigproc_printf ("killing pid %d, pgrp %d, p->%s, %s", p->pid, p->pgid,
p->__ctty (), myctty ());
if (p == myself)
killself++;
else if (p->kill (si))

View File

@ -341,18 +341,14 @@ setsid (void)
syscall_printf ("hmm. pgid %d pid %d", myself->pgid, myself->pid);
else
{
if (myself->ctty >= 0 && cygheap->open_fhs <= 0)
{
syscall_printf ("freeing console");
FreeConsole ();
}
myself->ctty = -1;
cygheap->manage_console_count ("setsid", 0);
myself->sid = getpid ();
myself->pgid = getpid ();
if (cygheap->ctty)
cygheap->close_ctty ();
syscall_printf ("sid %d, pgid %d, ctty %d, open_fhs %d", myself->sid,
myself->pgid, myself->ctty, cygheap->open_fhs);
syscall_printf ("sid %d, pgid %d, ctty %d", myself->sid, myself->pgid,
myself->ctty);
return myself->sid;
}