@@ -932,7 +932,7 @@ fhandler_console::cleanup_for_non_cygwin_app (handle_set_t *p)
932932 /* Cleaning-up console mode for non-cygwin app. */
933933 /* conmode can be tty::restore when non-cygwin app is
934934 exec'ed from login shell. */
935- tty::cons_mode conmode = cons_mode_on_close ();
935+ tty::cons_mode conmode = cons_mode_on_close (p );
936936 set_output_mode (conmode, ti, p);
937937 set_input_mode (conmode, ti, p);
938938 set_disable_master_thread (con.owner == GetCurrentProcessId ());
@@ -1991,8 +1991,9 @@ fhandler_console::close (int flag)
19911991
19921992 acquire_output_mutex (mutex_timeout);
19931993
1994- if (shared_console_info[unit] && myself->ppid == 1
1995- && (dev_t ) myself->ctty == get_device ())
1994+ if (shared_console_info[unit] && con.curr_input_mode != tty::restore
1995+ && (dev_t ) myself->ctty == get_device ()
1996+ && cons_mode_on_close (&handle_set) == tty::restore)
19961997 {
19971998 set_output_mode (tty::restore, &get_ttyp ()->ti , &handle_set);
19981999 set_input_mode (tty::restore, &get_ttyp ()->ti , &handle_set);
@@ -4704,10 +4705,25 @@ fhandler_console::fstat (struct stat *st)
47044705}
47054706
47064707tty::cons_mode
4707- fhandler_console::cons_mode_on_close ()
4708+ fhandler_console::cons_mode_on_close (handle_set_t *p )
47084709{
4710+ int unit = p->unit ;
47094711 if (myself->ppid != 1 ) /* Execed from normal cygwin process. */
47104712 return tty::cygwin;
47114713
4714+ if (!process_alive (con.owner )) /* The Master process already died. */
4715+ return tty::restore;
4716+ if (con.owner == GetCurrentProcessId ()) /* Master process */
4717+ return tty::restore;
4718+
4719+ PROCESS_BASIC_INFORMATION pbi;
4720+ NTSTATUS status =
4721+ NtQueryInformationProcess (GetCurrentProcess (), ProcessBasicInformation,
4722+ &pbi, sizeof (pbi), NULL );
4723+ if (NT_SUCCESS (status)
4724+ && !process_alive ((DWORD) pbi.InheritedFromUniqueProcessId ))
4725+ /* Execed from normal cygwin process and the parent has been exited. */
4726+ return tty::cygwin;
4727+
47124728 return tty::restore; /* otherwise, restore */
47134729}
0 commit comments