From 3c4f2024a1c4cddd0fc4239827de4f83528c17f7 Mon Sep 17 00:00:00 2001 From: Christopher Faylor Date: Fri, 3 Jul 2009 18:05:51 +0000 Subject: [PATCH] * dcrt0.cc (jit_debug): New global. (initial_env): Set jit_debug when we are automatically starting a gdb process. * dtable.cc (dtable::get_debugger_info): Don't tty tricks when we are being debugged by our own captive gdb, as determined by jit_debug == true. (dtable::init_std_file_from_handle): Detect errors when initializing a tty early rather than at random points later. * fhandler.h (fhandler_*::init): Return int to indicate success/failure. * fhandler.cc (fhandler_base::init): Reflect change in return value. * pipe.cc (fhandler_pipe::init): Ditto. (fhandler_pipe::create_selectable): Don't say we're retrying when we aren't. * fhandler_console.cc (fhandler_console::init): Ditto. Return success/failure. * fhandler_serial.cc (fhandler_serial::init): Ditto. * fhandler_tty.cc (fhandler_tty_slave::init): Ditto. (fhandler_tty_slave::open): Make debugging output more detailed. * tty.cc (tty_list::terminate): Don't close I/O handles before all slaves have checked in. (tty::slave_alive): Make a non-inlined function. Check if tty pipe handles can be created as an additional exists check. * tty.h (tty::slave_alive): Just define here. --- winsup/cygwin/ChangeLog | 27 ++++++++++++++++++++++++ winsup/cygwin/dcrt0.cc | 3 +++ winsup/cygwin/dtable.cc | 11 ++++++---- winsup/cygwin/fhandler.cc | 3 ++- winsup/cygwin/fhandler.h | 10 ++++----- winsup/cygwin/fhandler_console.cc | 4 ++-- winsup/cygwin/fhandler_serial.cc | 4 ++-- winsup/cygwin/fhandler_tty.cc | 7 ++++--- winsup/cygwin/pipe.cc | 7 ++++--- winsup/cygwin/tty.cc | 34 +++++++++++++++++++++++++------ winsup/cygwin/tty.h | 13 +----------- 11 files changed, 85 insertions(+), 38 deletions(-) diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog index 25169c5a4..fc4bf70e4 100644 --- a/winsup/cygwin/ChangeLog +++ b/winsup/cygwin/ChangeLog @@ -1,3 +1,30 @@ +2009-07-03 Christopher Faylor + + * dcrt0.cc (jit_debug): New global. + (initial_env): Set jit_debug when we are automatically starting a gdb + process. + * dtable.cc (dtable::get_debugger_info): Don't tty tricks when we are + being debugged by our own captive gdb, as determined by jit_debug == + true. + (dtable::init_std_file_from_handle): Detect errors when initializing a + tty early rather than at random points later. + * fhandler.h (fhandler_*::init): Return int to indicate + success/failure. + * fhandler.cc (fhandler_base::init): Reflect change in return value. + * pipe.cc (fhandler_pipe::init): Ditto. + (fhandler_pipe::create_selectable): Don't say we're retrying when we + aren't. + * fhandler_console.cc (fhandler_console::init): Ditto. Return + success/failure. + * fhandler_serial.cc (fhandler_serial::init): Ditto. + * fhandler_tty.cc (fhandler_tty_slave::init): Ditto. + (fhandler_tty_slave::open): Make debugging output more detailed. + * tty.cc (tty_list::terminate): Don't close I/O handles before all + slaves have checked in. + (tty::slave_alive): Make a non-inlined function. Check if tty pipe + handles can be created as an additional exists check. + * tty.h (tty::slave_alive): Just define here. + 2009-07-03 Corinna Vinschen * posix.sgml: Add fpurge and mkstemps to BSD list. diff --git a/winsup/cygwin/dcrt0.cc b/winsup/cygwin/dcrt0.cc index f31bf937f..042c1a73b 100644 --- a/winsup/cygwin/dcrt0.cc +++ b/winsup/cygwin/dcrt0.cc @@ -48,6 +48,8 @@ static char NO_COPY **envp; static char title_buf[TITLESIZE + 1]; +bool NO_COPY jit_debug; + static void do_global_dtors () { @@ -499,6 +501,7 @@ initial_env () if (strstr (buf1, buf)) { error_start_init (p); + jit_debug = true; try_to_debug (); console_printf ("*** Sending Break. gdb may issue spurious SIGTRAP message.\n"); break_here (); diff --git a/winsup/cygwin/dtable.cc b/winsup/cygwin/dtable.cc index e63a9b5e1..08b04f091 100644 --- a/winsup/cygwin/dtable.cc +++ b/winsup/cygwin/dtable.cc @@ -109,7 +109,8 @@ dtable::extend (int howmuch) void dtable::get_debugger_info () { - if (being_debugged ()) + extern bool jit_debug; + if (!jit_debug && being_debugged ()) { char std[3][sizeof ("/dev/ttyNNNN")]; std[0][0] = std[1][0] = std [2][0] = '\0'; @@ -383,9 +384,11 @@ dtable::init_std_file_from_handle (int fd, HANDLE handle) This needs further investigation but the workaround not to close the handles will have a marginal hit of three extra handles per process at most. */ - fh->init (dev == FH_CONSOLE && wincap.has_console_handle_problem () - ? INVALID_HANDLE_VALUE : handle, access, bin); - set_std_handle (fd); + if (fh->init (dev == FH_CONSOLE && wincap.has_console_handle_problem () + ? INVALID_HANDLE_VALUE : handle, access, bin)) + set_std_handle (fd); + else + api_fatal ("couldn't initialize fd %d for %s", fd, fh->get_name ()); paranoid_printf ("fd %d, handle %p", fd, handle); } } diff --git a/winsup/cygwin/fhandler.cc b/winsup/cygwin/fhandler.cc index 0488374c6..ffbd02ffa 100644 --- a/winsup/cygwin/fhandler.cc +++ b/winsup/cygwin/fhandler.cc @@ -1123,7 +1123,7 @@ fhandler_base::fstatvfs (struct statvfs *sfs) return fh.fstatvfs (sfs); } -void +int fhandler_base::init (HANDLE f, DWORD a, mode_t bin) { set_io_handle (f); @@ -1139,6 +1139,7 @@ fhandler_base::init (HANDLE f, DWORD a, mode_t bin) set_flags (flags | bin); set_open_status (); debug_printf ("created new fhandler_base for handle %p, bin %d", f, rbinary ()); + return 1; } int diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h index 51a68a090..db55e2c05 100644 --- a/winsup/cygwin/fhandler.h +++ b/winsup/cygwin/fhandler.h @@ -325,7 +325,7 @@ class fhandler_base void *operator new (size_t, void *p) __attribute__ ((nothrow)) {return p;} - virtual void init (HANDLE, DWORD, mode_t); + virtual int init (HANDLE, DWORD, mode_t); virtual int tcflush (int); virtual int tcsendbreak (int); @@ -566,7 +566,7 @@ public: int __stdcall fadvise (_off64_t, _off64_t, int) __attribute__ ((regparm (3))); int __stdcall ftruncate (_off64_t, bool) __attribute__ ((regparm (3))); int ready_for_read (int fd, DWORD howlong); - void init (HANDLE, DWORD, mode_t); + int init (HANDLE, DWORD, mode_t); static int create (fhandler_pipe *[2], unsigned, int); static int create_selectable (LPSECURITY_ATTRIBUTES, HANDLE&, HANDLE&, DWORD, const char * = NULL); friend class fhandler_fifo; @@ -771,7 +771,7 @@ class fhandler_serial: public fhandler_base int open (int flags, mode_t mode); int close (); - void init (HANDLE h, DWORD a, mode_t flags); + int init (HANDLE h, DWORD a, mode_t flags); void overlapped_setup (); int dup (fhandler_base *child); void raw_read (void *ptr, size_t& ulen); @@ -971,7 +971,7 @@ class fhandler_console: public fhandler_termios int dup (fhandler_base *child); int ioctl (unsigned int cmd, void *); - void init (HANDLE, DWORD, mode_t); + int init (HANDLE, DWORD, mode_t); bool mouse_aware () {return dev_state->use_mouse;} select_record *select_read (select_stuff *); @@ -1032,7 +1032,7 @@ class fhandler_tty_slave: public fhandler_tty_common int open (int flags, mode_t mode = 0); int write (const void *ptr, size_t len); void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3))); - void init (HANDLE, DWORD, mode_t); + int init (HANDLE, DWORD, mode_t); int tcsetattr (int a, const struct termios *t); int tcgetattr (struct termios *t); diff --git a/winsup/cygwin/fhandler_console.cc b/winsup/cygwin/fhandler_console.cc index 6ec1e29da..f8a930cae 100644 --- a/winsup/cygwin/fhandler_console.cc +++ b/winsup/cygwin/fhandler_console.cc @@ -1855,7 +1855,7 @@ get_nonascii_key (INPUT_RECORD& input_rec, char *tmp) return NULL; } -void +int fhandler_console::init (HANDLE f, DWORD a, mode_t bin) { // this->fhandler_termios::init (f, mode, bin); @@ -1873,7 +1873,7 @@ fhandler_console::init (HANDLE f, DWORD a, mode_t bin) if (f != INVALID_HANDLE_VALUE) CloseHandle (f); /* Reopened by open */ - tcsetattr (0, &tc->ti); + return !tcsetattr (0, &tc->ti); } int diff --git a/winsup/cygwin/fhandler_serial.cc b/winsup/cygwin/fhandler_serial.cc index 484350390..8a08beecc 100644 --- a/winsup/cygwin/fhandler_serial.cc +++ b/winsup/cygwin/fhandler_serial.cc @@ -190,10 +190,10 @@ err: return -1; } -void +int fhandler_serial::init (HANDLE f, DWORD flags, mode_t bin) { - open (flags, bin & (O_BINARY | O_TEXT)); + return open (flags, bin & (O_BINARY | O_TEXT)); } int diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc index 979ffc3d8..ae857e9ef 100644 --- a/winsup/cygwin/fhandler_tty.cc +++ b/winsup/cygwin/fhandler_tty.cc @@ -559,7 +559,8 @@ fhandler_tty_slave::open (int flags, mode_t) hMainProc, &from_master_local, 0, TRUE, DUPLICATE_SAME_ACCESS)) { - termios_printf ("can't duplicate input, %E"); + termios_printf ("can't duplicate input from %u/%p, %E", + get_ttyp ()->master_pid, get_ttyp ()->from_master); __seterrno (); return 0; } @@ -654,7 +655,7 @@ fhandler_tty_slave::cygserver_attach_tty (LPHANDLE from_master_ptr, return 1; } -void +int fhandler_tty_slave::init (HANDLE, DWORD a, mode_t) { int flags = 0; @@ -667,7 +668,7 @@ fhandler_tty_slave::init (HANDLE, DWORD a, mode_t) if (a == (GENERIC_READ | GENERIC_WRITE)) flags = O_RDWR; - open (flags); + return open (flags); } int diff --git a/winsup/cygwin/pipe.cc b/winsup/cygwin/pipe.cc index 787cc398d..e8c7070b1 100644 --- a/winsup/cygwin/pipe.cc +++ b/winsup/cygwin/pipe.cc @@ -29,7 +29,7 @@ fhandler_pipe::fhandler_pipe () uninterruptible_io (true); } -void +int fhandler_pipe::init (HANDLE f, DWORD a, mode_t mode) { // FIXME: Have to clean this up someday @@ -53,6 +53,7 @@ fhandler_pipe::init (HANDLE f, DWORD a, mode_t mode) if (mode & O_NOINHERIT) close_on_exec (true); setup_overlapped (opened_properly); + return 1; } extern "C" int sscanf (const char *, const char *, ...); @@ -259,12 +260,12 @@ fhandler_pipe::create_selectable (LPSECURITY_ATTRIBUTES sa_ptr, HANDLE& r, case ERROR_PIPE_BUSY: /* The pipe is already open with compatible parameters. Pick a new name and retry. */ - debug_printf ("pipe busy, retrying"); + debug_printf ("pipe busy", name ? ", retrying" : ""); break; case ERROR_ACCESS_DENIED: /* The pipe is already open with incompatible parameters. Pick a new name and retry. */ - debug_printf ("pipe access denied, retrying"); + debug_printf ("pipe access denied%s", name ? ", retrying" : ""); break; default: { diff --git a/winsup/cygwin/tty.cc b/winsup/cygwin/tty.cc index f831a536d..624805f20 100644 --- a/winsup/cygwin/tty.cc +++ b/winsup/cygwin/tty.cc @@ -147,8 +147,6 @@ tty_list::terminate () if (ttynum != -1 && tty_master && ttys[ttynum].master_pid == myself->pid) { tty *t = ttys + ttynum; - CloseHandle (tty_master->from_master); - CloseHandle (tty_master->to_master); /* Wait for children which rely on tty handling in this process to go away */ for (int i = 0; ; i++) @@ -166,6 +164,8 @@ tty_list::terminate () } lock_ttys here (); + CloseHandle (tty_master->from_master); + CloseHandle (tty_master->to_master); termios_printf ("tty %d master about to finish", ttynum); CloseHandle (tty_master->get_io_handle ()); @@ -209,7 +209,7 @@ tty_list::init () /* Search for tty class for our console. Allocate new tty if our process is the only cygwin process in the current console. Return tty number or -1 if error. - If flag == 0, just find a free tty. + If with_console == 0, just find a free tty. */ int tty_list::allocate (bool with_console) @@ -218,8 +218,6 @@ tty_list::allocate (bool with_console) int freetty = -1; HANDLE hmaster = NULL; - /* FIXME: This whole function needs a protective mutex. */ - lock_ttys here; if (!with_console) @@ -261,7 +259,7 @@ tty_list::allocate (bool with_console) } } - /* There is no tty allocated to console, allocate the first free found */ + /* There is no tty allocated to console; allocate the first free found */ if (freetty == -1) goto out; @@ -294,6 +292,30 @@ tty::slave_alive () return alive (TTY_SLAVE_ALIVE); } +bool +tty::exists () +{ + /* Attempt to open the from-master side of the tty. If it is accessible + then it exists although it may have been privileges to actually use it. */ + char pipename[sizeof("ttyNNNN-from-master")]; + __small_sprintf (pipename, "tty%d-from-master", ntty); + HANDLE r, w; + int res = fhandler_pipe::create_selectable (&sec_none_nih, r, w, 0, pipename); + if (res) + return true; + + CloseHandle (r); + CloseHandle (w); + + HANDLE h = open_output_mutex (); + if (h) + { + CloseHandle (h); + return true; + } + return slave_alive (); +} + bool tty::alive (const char *fmt) { diff --git a/winsup/cygwin/tty.h b/winsup/cygwin/tty.h index 612ed3038..b4c02a411 100644 --- a/winsup/cygwin/tty.h +++ b/winsup/cygwin/tty.h @@ -102,18 +102,7 @@ public: HANDLE open_mutex (const char *mutex); HANDLE open_output_mutex (); HANDLE open_input_mutex (); - bool exists () - { - if (!master_pid) - return false; - HANDLE h = open_output_mutex (); - if (h) - { - CloseHandle (h); - return 1; - } - return slave_alive (); - } + bool exists (); void set_master_closed () {master_pid = -1;} static void __stdcall create_master (int); static void __stdcall init_session ();