From 8ed5c9b6297dce0115a462e6ae1a644a9a278bdb Mon Sep 17 00:00:00 2001 From: Christopher Faylor Date: Tue, 30 Dec 2003 01:57:16 +0000 Subject: [PATCH] Move open_fhs from fhandler.h to cygheap.h for easier tracking. Accommodate this change throughout. * dtable.cc (dtable::add_archetype): Use correct count when iterating through *active* archetypes. * dtable.cc (dtable::fixup_after_exec): Close handle if close_on_exec and archetype exists since archetype currently does not set windows close-on-exec flag (FIXME). * fhandler_tty.cc (fhandler_tty_slave::open): Change debugging output slightly. (fhandler_tty_slave::close): Ditto. (fhandler_tty_slave::fixup_after_fork): Just report on inherited use counts since since archetype currently does not set windows close-on-exec flag (FIXME). * pinfo.cc (_pinfo::set_ctty): Change debugging output slightly. * cygheap.cc (cygheap_init): Remove open_fhs accommodation and use count reporting. * perthread.h (vfork_save::fhctty): New element. * perthread.h (vfork_save::open_fhs): New element. * fork.cc (vfork): Muck around with ctty and open_fhs stuff, both of which are now saved in the vfork_save structure. --- winsup/cygwin/ChangeLog | 23 +++++++++++++++++++++++ winsup/cygwin/cygheap.cc | 5 ----- winsup/cygwin/cygheap.h | 1 + winsup/cygwin/dtable.cc | 20 ++++++++------------ winsup/cygwin/fhandler.h | 4 +--- winsup/cygwin/fhandler_console.cc | 18 ++++++++---------- winsup/cygwin/fhandler_tty.cc | 26 ++++++++++++-------------- winsup/cygwin/fork.cc | 18 +++++++++++++++++- winsup/cygwin/perthread.h | 2 ++ winsup/cygwin/pinfo.cc | 4 ++-- winsup/cygwin/syscalls.cc | 5 +++-- 11 files changed, 77 insertions(+), 49 deletions(-) diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog index b838327eb..68d36756e 100644 --- a/winsup/cygwin/ChangeLog +++ b/winsup/cygwin/ChangeLog @@ -1,3 +1,26 @@ +2003-12-29 Christopher Faylor + + Move open_fhs from fhandler.h to cygheap.h for easier tracking. + Accommodate this change throughout. + * dtable.cc (dtable::add_archetype): Use correct count when iterating + through *active* archetypes. + * dtable.cc (dtable::fixup_after_exec): Close handle if close_on_exec + and archetype exists since archetype currently does not set windows + close-on-exec flag (FIXME). + * fhandler_tty.cc (fhandler_tty_slave::open): Change debugging output + slightly. + (fhandler_tty_slave::close): Ditto. + (fhandler_tty_slave::fixup_after_fork): Just report on inherited use + counts since since archetype currently does not set windows + close-on-exec flag (FIXME). + * pinfo.cc (_pinfo::set_ctty): Change debugging output slightly. + * cygheap.cc (cygheap_init): Remove open_fhs accommodation and use + count reporting. + * perthread.h (vfork_save::fhctty): New element. + * perthread.h (vfork_save::open_fhs): New element. + * fork.cc (vfork): Muck around with ctty and open_fhs stuff, both of + which are now saved in the vfork_save structure. + 2003-12-27 Christopher Faylor * fhandler.h (fhandler_tty_slave::archetype): Make public. diff --git a/winsup/cygwin/cygheap.cc b/winsup/cygwin/cygheap.cc index 54b645fcc..6859e4335 100644 --- a/winsup/cygwin/cygheap.cc +++ b/winsup/cygwin/cygheap.cc @@ -212,11 +212,6 @@ cygheap_init () && (set_process_privilege (SE_CREATE_GLOBAL_NAME, true) >= 0 || GetLastError () == ERROR_NO_SUCH_PRIVILEGE) ? "Global\\" : ""); - if (cygheap->ctty) - { - fhandler_console::open_fhs++; - report_tty_counts (cygheap->ctty, "inherited", "incremented ", "unchanged "); - } } /* Copyright (C) 1997, 2000 DJ Delorie */ diff --git a/winsup/cygwin/cygheap.h b/winsup/cygwin/cygheap.h index afe7d01ec..af21814df 100644 --- a/winsup/cygwin/cygheap.h +++ b/winsup/cygwin/cygheap.h @@ -262,6 +262,7 @@ struct init_cygheap struct sigaction *sigs; fhandler_tty_slave *ctty; /* Current tty */ + int open_fhs; }; #define CYGHEAPSIZE (sizeof (init_cygheap) + (20000 * sizeof (fhandler_union)) + (5 * 65536)) diff --git a/winsup/cygwin/dtable.cc b/winsup/cygwin/dtable.cc index a7095c597..e5bb80218 100644 --- a/winsup/cygwin/dtable.cc +++ b/winsup/cygwin/dtable.cc @@ -186,10 +186,10 @@ dtable::add_archetype () void dtable::delete_archetype (fhandler_base *fh) { - for (unsigned i = 0; i < narchetypes; i++) + for (unsigned i = 0; i < farchetype; i++) if (fh == archetypes[i]) { - debug_printf ("deleting archive element %d for %s", i, fh->get_name ()); + debug_printf ("deleting element %d for %s", i, fh->get_name ()); if (i < --farchetype) archetypes[i] = archetypes[farchetype]; break; @@ -660,7 +660,11 @@ dtable::fixup_after_exec (HANDLE parent) { fh->clear_readahead (); if (fh->get_close_on_exec ()) - release (i); + { + if (fh->archetype) + fh->close (); + release (i); + } else { fh->fixup_after_exec (parent); @@ -704,7 +708,7 @@ dtable::vfork_child_dup () if (cygheap->ctty) { cygheap->ctty->usecount++; - fhandler_console::open_fhs++; + cygheap->open_fhs++; report_tty_counts (cygheap->ctty, "vfork dup", "incremented ", ""); } @@ -742,9 +746,6 @@ 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; } @@ -758,7 +759,6 @@ 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) @@ -773,10 +773,6 @@ 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 a064e24b0..9ce75fbc4 100644 --- a/winsup/cygwin/fhandler.h +++ b/winsup/cygwin/fhandler.h @@ -829,8 +829,6 @@ class fhandler_console: public fhandler_termios void set_cursor_maybe (); public: - static int open_fhs; - fhandler_console (); fhandler_console* is_console () { return this; } @@ -1197,7 +1195,7 @@ struct fhandler_nodevice: public fhandler_base #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,\ + fhs_op, cygheap->open_fhs,\ use_op, ((fhandler_tty_slave *) fh)->archetype->usecount); typedef union diff --git a/winsup/cygwin/fhandler_console.cc b/winsup/cygwin/fhandler_console.cc index 46f1cb1a5..86c02009a 100644 --- a/winsup/cygwin/fhandler_console.cc +++ b/winsup/cygwin/fhandler_console.cc @@ -73,8 +73,6 @@ static console_state NO_COPY *shared_console_info; dev_console NO_COPY *fhandler_console::dev_state; -int NO_COPY fhandler_console::open_fhs; - /* Allocate and initialize the shared record for the current console. Returns a pointer to shared_console_info. */ tty_min * @@ -664,10 +662,10 @@ fhandler_console::open (int flags, mode_t) TTYCLEARF (RSTCONS); set_open_status (); - open_fhs++; - debug_printf ("incremented open_fhs, now %d", open_fhs); - debug_printf ("opened conin$ %p, conout$ %p", - get_io_handle (), get_output_handle ()); + cygheap->open_fhs++; + debug_printf ("incremented open_fhs, now %d", cygheap->open_fhs); + debug_printf ("opened conin$ %p, conout$ %p", get_io_handle (), + get_output_handle ()); return 1; } @@ -679,14 +677,14 @@ fhandler_console::close (void) CloseHandle (get_output_handle ()); set_io_handle (NULL); set_output_handle (NULL); - if (!cygheap->fdtab.in_vfork_cleanup () && --open_fhs <= 0 + if (!cygheap->fdtab.in_vfork_cleanup () && --(cygheap->open_fhs) <= 0 && myself->ctty != TTY_CONSOLE) { - syscall_printf ("open_fhs %d, freeing console %p", - fhandler_console::open_fhs, myself->ctty); + syscall_printf ("open_fhs %d, freeing console %p", cygheap->open_fhs, + myself->ctty); FreeConsole (); } - debug_printf ("decremented open_fhs, now %d", open_fhs); + debug_printf ("decremented open_fhs, now %d", cygheap->open_fhs); return 0; } diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc index 69d03a9e3..ef308b09a 100644 --- a/winsup/cygwin/fhandler_tty.cc +++ b/winsup/cygwin/fhandler_tty.cc @@ -455,8 +455,8 @@ fhandler_tty_slave::open (int flags, mode_t) if (arch) { *this = *(fhandler_tty_slave *) arch; - termios_printf ("copied tty fhandler from cygheap"); - fhandler_console::open_fhs++; + termios_printf ("copied tty fhandler archetype"); + cygheap->open_fhs++; goto out; } @@ -573,9 +573,8 @@ fhandler_tty_slave::open (int flags, mode_t) set_output_handle (to_master_local); set_open_status (); - if (fhandler_console::open_fhs++ == 0 && !GetConsoleCP () - && !output_done_event && wincap.pty_needs_alloc_console () - && !GetProcessWindowStation ()) + if (cygheap->open_fhs++ == 0 && !GetConsoleCP () && !output_done_event + && wincap.pty_needs_alloc_console () && !GetProcessWindowStation ()) { BOOL b; HWINSTA h = CreateWindowStation (NULL, 0, GENERIC_READ | GENERIC_WRITE, &sec_none_nih); @@ -610,7 +609,7 @@ out: int fhandler_tty_slave::close () { - if (!--fhandler_console::open_fhs && myself->ctty == -1) + if (!--cygheap->open_fhs && myself->ctty == -1) FreeConsole (); archetype->usecount--; @@ -620,13 +619,13 @@ fhandler_tty_slave::close () { #ifdef DEBUGGING if (archetype->usecount < 0) - system_printf ("usecount %d", archetype->usecount); + system_printf ("error: usecount %d", archetype->usecount); #endif - termios_printf ("just returning because archetype usecount is > 0"); + termios_printf ("just returning because archetype usecount is != 0"); return 0; } - termios_printf ("closing last open %s handle", pc.dev.name); + termios_printf ("closing last open %s handle", ttyname ()); return fhandler_tty_common::close (); } @@ -914,7 +913,7 @@ fhandler_tty_slave::dup (fhandler_base *child) *(fhandler_tty_slave *) child = *arch; child->usecount = 0; arch->usecount++; - fhandler_console::open_fhs++; + cygheap->open_fhs++; report_tty_counts (child, "duped", "incremented ", ""); myself->set_ctty (get_ttyp (), openflags, arch); return 0; @@ -1364,9 +1363,8 @@ fhandler_tty_common::set_close_on_exec (int val) void fhandler_tty_slave::fixup_after_fork (HANDLE parent) { - fhandler_console::open_fhs++; - termios_printf ("incremented open_fhs %d", fhandler_console::open_fhs); - fhandler_tty_common::fixup_after_fork (parent); + // fhandler_tty_common::fixup_after_fork (parent); + report_tty_counts (this, "inherited", "", ""); } void @@ -1411,7 +1409,7 @@ fhandler_tty_master::init_console () return -1; console->init (INVALID_HANDLE_VALUE, GENERIC_READ | GENERIC_WRITE, O_BINARY); - fhandler_console::open_fhs--; /* handled when individual fds are opened */ + cygheap->open_fhs--; /* handled when individual fds are opened */ console->set_r_no_interrupt (1); return 0; } diff --git a/winsup/cygwin/fork.cc b/winsup/cygwin/fork.cc index 7461c0bb4..477d85c0f 100644 --- a/winsup/cygwin/fork.cc +++ b/winsup/cygwin/fork.cc @@ -719,9 +719,11 @@ vfork () vf->ctty = myself->ctty; vf->sid = myself->sid; vf->pgid = myself->pgid; + vf->fhctty = cygheap->ctty; + vf->open_fhs = cygheap->open_fhs; int res = cygheap->fdtab.vfork_child_dup () ? 0 : -1; debug_printf ("%d = vfork()", res); - call_signal_handler_now (); // FIXME: racy + call_signal_handler_now (); // FIXME: racy vf->tls = _my_tls; return res; } @@ -737,6 +739,20 @@ vfork () myself->ctty = vf->ctty; myself->sid = vf->sid; myself->pgid = vf->pgid; + termios_printf ("cygheap->ctty %p, vf->fhctty %p", cygheap->ctty, vf->fhctty); + if (cygheap->ctty != vf->fhctty) + { + vf->fhctty->close (); + if (vf->pid <= 0) + { + if (vf->ctty) + vf->fhctty->close (); + cygheap->ctty = vf->fhctty; + } + } + + if (vf->pid <= 0) + cygheap->open_fhs = vf->open_fhs; if (vf->pid < 0) { diff --git a/winsup/cygwin/perthread.h b/winsup/cygwin/perthread.h index 217fc7a74..e0efffc1c 100644 --- a/winsup/cygwin/perthread.h +++ b/winsup/cygwin/perthread.h @@ -59,6 +59,8 @@ class vfork_save int ctty; pid_t sid; pid_t pgid; + class fhandler_tty_slave *fhctty; + int open_fhs; int is_active () { return pid < 0; } void restore_pid (int val) { diff --git a/winsup/cygwin/pinfo.cc b/winsup/cygwin/pinfo.cc index 0472145f8..4ce702e6a 100644 --- a/winsup/cygwin/pinfo.cc +++ b/winsup/cygwin/pinfo.cc @@ -257,7 +257,7 @@ pinfo::set_acl() void _pinfo::set_ctty (tty_min *tc, int flags, fhandler_tty_slave *arch) { - debug_printf ("ctty %d", ctty); + debug_printf ("checking if /dev/tty%d differs from input", ctty); if ((ctty < 0 || ctty == tc->ntty) && !(flags & O_NOCTTY)) { ctty = tc->ntty; @@ -292,7 +292,7 @@ _pinfo::set_ctty (tty_min *tc, int flags, fhandler_tty_slave *arch) if (arch) { arch->usecount++; - fhandler_console::open_fhs++; + cygheap->open_fhs++; report_tty_counts (cygheap->ctty, "ctty", "incremented ", ""); } } diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc index 8bf74d5ae..47d6b10de 100644 --- a/winsup/cygwin/syscalls.cc +++ b/winsup/cygwin/syscalls.cc @@ -104,6 +104,7 @@ close_all_files (void) { debug_printf ("closing ctty"); cygheap->ctty->close (); + cygheap->ctty = NULL; } ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "close_all_files"); @@ -320,7 +321,7 @@ setsid (void) syscall_printf ("hmm. pgid %d pid %d", myself->pgid, myself->pid); else { - if (myself->ctty >= 0 && fhandler_console::open_fhs <= 0) + if (myself->ctty >= 0 && cygheap->open_fhs <= 0) { syscall_printf ("freeing console"); FreeConsole (); @@ -329,7 +330,7 @@ setsid (void) myself->sid = getpid (); myself->pgid = getpid (); syscall_printf ("sid %d, pgid %d, ctty %d, open_fhs %d", myself->sid, - myself->pgid, myself->ctty, fhandler_console::open_fhs); + myself->pgid, myself->ctty, cygheap->open_fhs); if (cygheap->ctty) { cygheap->ctty->close ();