diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog index 345224889..b838327eb 100644 --- a/winsup/cygwin/ChangeLog +++ b/winsup/cygwin/ChangeLog @@ -1,3 +1,18 @@ +2003-12-27 Christopher Faylor + + * fhandler.h (fhandler_tty_slave::archetype): Make public. + (report_tty_counts): New macro. Use throughout for reporting tty use + counts. + * dtable.cc (dtable::vfork_child_dup): Add debugging output for + usecount increment. Increment open_fhs if appropriate. + (dtable::vfork_parent_restore): "Close" artificially bumped ctty. + (dtable::vfork_child_fixup): Close ctty since it was bumped prior to + vfork. Save open_fhs around close since the closing of these handles + has no effect on the console. + * fhandler_tty.cc (fhandler_tty_slave::open): Reorganize calls to allow + for easier tracking of usecount modification. + (fhandler_tty_slave::open): Ditto. + 2003-12-26 Christopher Faylor * syscalls.cc (close_all_files): Simplify logic around closing ctty. diff --git a/winsup/cygwin/cygheap.cc b/winsup/cygwin/cygheap.cc index 16a95f441..54b645fcc 100644 --- a/winsup/cygwin/cygheap.cc +++ b/winsup/cygwin/cygheap.cc @@ -215,9 +215,7 @@ cygheap_init () if (cygheap->ctty) { fhandler_console::open_fhs++; - debug_printf ("tty%d, open_fhs %d, arch usecount %d", - cygheap->ctty->get_ttyp ()->ntty, - fhandler_console::open_fhs, cygheap->ctty->usecount); + report_tty_counts (cygheap->ctty, "inherited", "incremented ", "unchanged "); } } diff --git a/winsup/cygwin/dtable.cc b/winsup/cygwin/dtable.cc index b4c5db0d7..a7095c597 100644 --- a/winsup/cygwin/dtable.cc +++ b/winsup/cygwin/dtable.cc @@ -31,6 +31,7 @@ details. */ #include "dtable.h" #include "cygheap.h" #include "ntdll.h" +#include "tty.h" static const NO_COPY DWORD std_consts[] = {STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, STD_ERROR_HANDLE}; @@ -701,7 +702,11 @@ dtable::vfork_child_dup () /* Remove impersonation */ cygheap->user.deimpersonate (); if (cygheap->ctty) - cygheap->ctty->usecount++; + { + cygheap->ctty->usecount++; + fhandler_console::open_fhs++; + report_tty_counts (cygheap->ctty, "vfork dup", "incremented ", ""); + } for (size_t i = 0; i < size; i++) if (not_open (i)) @@ -737,6 +742,9 @@ dtable::vfork_parent_restore () fds_on_hold = NULL; cfree (deleteme); + if (cygheap->ctty) + cygheap->ctty->close (); + ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "restore"); return; } @@ -750,6 +758,7 @@ dtable::vfork_child_fixup () fhandler_base **saveme = fds; fds = fds_on_hold; + int old_open_fhs = fhandler_console::open_fhs; fhandler_base *fh; for (int i = 0; i < (int) size; i++) if ((fh = fds[i]) != NULL) @@ -764,6 +773,10 @@ dtable::vfork_child_fixup () } } + fhandler_console::open_fhs = old_open_fhs; + if (cygheap->ctty) + cygheap->ctty->close (); + fds = saveme; cfree (fds_on_hold); fds_on_hold = NULL; diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h index 5cbf387da..a064e24b0 100644 --- a/winsup/cygwin/fhandler.h +++ b/winsup/cygwin/fhandler.h @@ -122,9 +122,9 @@ class fhandler_base DWORD fs_flags; HANDLE read_state; path_conv pc; - class fhandler_base *archetype; public: + class fhandler_base *archetype; int usecount; void set_name (path_conv &pc); @@ -1194,6 +1194,12 @@ 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",\ + fh->ttyname (), call,\ + fhs_op, fhandler_console::open_fhs,\ + use_op, ((fhandler_tty_slave *) fh)->archetype->usecount); + typedef union { char __base[sizeof (fhandler_base)]; diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc index ff1d48f24..69d03a9e3 100644 --- a/winsup/cygwin/fhandler_tty.cc +++ b/winsup/cygwin/fhandler_tty.cc @@ -601,9 +601,8 @@ fhandler_tty_slave::open (int flags, mode_t) out: usecount = 0; archetype->usecount++; + report_tty_counts (this, "opened", "incremented ", ""); myself->set_ctty (get_ttyp (), flags, arch); - termios_printf ("%s opened, incremented open_fhs %d, archetype usecount %d", - pc.dev.name, fhandler_console::open_fhs, archetype->usecount); return 1; } @@ -613,9 +612,11 @@ fhandler_tty_slave::close () { if (!--fhandler_console::open_fhs && myself->ctty == -1) FreeConsole (); - termios_printf ("decremented open_fhs %d, archetype usecount %d", - fhandler_console::open_fhs, archetype->usecount); - if (--archetype->usecount) + + archetype->usecount--; + report_tty_counts (this, "closed", "decremented ", ""); + + if (archetype->usecount) { #ifdef DEBUGGING if (archetype->usecount < 0) @@ -909,14 +910,13 @@ fhandler_tty_slave::read (void *ptr, size_t& len) int fhandler_tty_slave::dup (fhandler_base *child) { - fhandler_console::open_fhs++; fhandler_tty_slave *arch = (fhandler_tty_slave *) archetype; *(fhandler_tty_slave *) child = *arch; - arch->usecount++; child->usecount = 0; + arch->usecount++; + fhandler_console::open_fhs++; + report_tty_counts (child, "duped", "incremented ", ""); myself->set_ctty (get_ttyp (), openflags, arch); - termios_printf ("incremented open_fhs %d, archetype usecount %d", - fhandler_console::open_fhs, archetype->usecount); return 0; } diff --git a/winsup/cygwin/pinfo.cc b/winsup/cygwin/pinfo.cc index 92a9ea9aa..0472145f8 100644 --- a/winsup/cygwin/pinfo.cc +++ b/winsup/cygwin/pinfo.cc @@ -293,8 +293,7 @@ _pinfo::set_ctty (tty_min *tc, int flags, fhandler_tty_slave *arch) { arch->usecount++; fhandler_console::open_fhs++; - debug_printf ("tty%d, open_fhs %d, arch usecount %d", tc->ntty, - fhandler_console::open_fhs, arch->usecount); + report_tty_counts (cygheap->ctty, "ctty", "incremented ", ""); } } }