Change the name "title_mutex" to "tty_mutex" throughout.

* tty.h (tty_list::allocate_tty): Turn argument into a boolean.
* tty.cc (tty_list::init): Protect entire allocation operation with tty_mutex
(formerly title_mutex) to protect against allocation races.
This commit is contained in:
Christopher Faylor 2004-05-17 15:27:56 +00:00
parent fb4489a53f
commit 641654f72a
7 changed files with 51 additions and 43 deletions

View File

@ -1,3 +1,10 @@
2004-05-17 Christopher Faylor <cgf@alum.bu.edu>
Change the name "title_mutex" to "tty_mutex" throughout.
* tty.h (tty_list::allocate_tty): Turn argument into a boolean.
* tty.cc (tty_list::init): Protect entire allocation operation with
tty_mutex (formerly title_mutex) to protect against allocation races.
2004-05-16 Corinna Vinschen <corinna@vinschen.de> 2004-05-16 Corinna Vinschen <corinna@vinschen.de>
* fhandler_socket.cc (fhandler_socket::recvfrom): Use original flags * fhandler_socket.cc (fhandler_socket::recvfrom): Use original flags

View File

@ -330,7 +330,7 @@ try_to_debug (bool waitloop)
/* if any of these mutexes is owned, we will fail to start any cygwin app /* if any of these mutexes is owned, we will fail to start any cygwin app
until trapped app exits */ until trapped app exits */
ReleaseMutex (title_mutex); ReleaseMutex (tty_mutex);
/* prevent recursive exception handling */ /* prevent recursive exception handling */
char* rawenv = GetEnvironmentStrings () ; char* rawenv = GetEnvironmentStrings () ;
@ -1117,22 +1117,22 @@ signal_exit (int rc)
do_exit (rc); do_exit (rc);
} }
HANDLE NO_COPY title_mutex = NULL; HANDLE NO_COPY tty_mutex = NULL;
void void
events_init (void) events_init (void)
{ {
char *name; char *name;
char mutex_name[CYG_MAX_PATH]; char mutex_name[CYG_MAX_PATH];
/* title_mutex protects modification of console title. It's necessary /* tty_mutex is on while searching for a tty slot. It's necessary
while finding console window handle */ while finding console window handle */
if (!(title_mutex = CreateMutex (&sec_all_nih, FALSE, if (!(tty_mutex = CreateMutex (&sec_all_nih, FALSE,
name = shared_name (mutex_name, name = shared_name (mutex_name,
"title_mutex", 0)))) "tty_mutex", 0))))
api_fatal ("can't create title mutex '%s', %E", name); api_fatal ("can't create title mutex '%s', %E", name);
ProtectHandle (title_mutex); ProtectHandle (tty_mutex);
new_muto (mask_sync); new_muto (mask_sync);
windows_system_directory[0] = '\0'; windows_system_directory[0] = '\0';
(void) GetSystemDirectory (windows_system_directory, sizeof (windows_system_directory) - 2); (void) GetSystemDirectory (windows_system_directory, sizeof (windows_system_directory) - 2);

View File

@ -1776,10 +1776,10 @@ set_console_title (char *title)
char buf[257]; char buf[257];
strncpy (buf, title, sizeof (buf) - 1); strncpy (buf, title, sizeof (buf) - 1);
buf[sizeof (buf) - 1] = '\0'; buf[sizeof (buf) - 1] = '\0';
if ((rc = WaitForSingleObject (title_mutex, 15000)) != WAIT_OBJECT_0) if ((rc = WaitForSingleObject (tty_mutex, 15000)) != WAIT_OBJECT_0)
sigproc_printf ("wait for title mutex failed rc %d, %E", rc); sigproc_printf ("wait for title mutex failed rc %d, %E", rc);
SetConsoleTitle (buf); SetConsoleTitle (buf);
ReleaseMutex (title_mutex); ReleaseMutex (tty_mutex);
debug_printf ("title '%s'", buf); debug_printf ("title '%s'", buf);
} }

View File

@ -1157,7 +1157,7 @@ fhandler_pty_master::fhandler_pty_master ()
int int
fhandler_pty_master::open (int flags, mode_t) fhandler_pty_master::open (int flags, mode_t)
{ {
int ntty = cygwin_shared->tty.allocate_tty (0); int ntty = cygwin_shared->tty.allocate_tty (false);
if (ntty < 0) if (ntty < 0)
return 0; return 0;

View File

@ -121,7 +121,7 @@ attach_tty (int num)
} }
if (NOTSTATE (myself, PID_USETTY)) if (NOTSTATE (myself, PID_USETTY))
return -1; return -1;
return cygwin_shared->tty.allocate_tty (1); return cygwin_shared->tty.allocate_tty (true);
} }
void void
@ -196,32 +196,28 @@ tty_list::init (void)
If flag == 0, just find a free tty. If flag == 0, just find a free tty.
*/ */
int int
tty_list::allocate_tty (int with_console) tty_list::allocate_tty (bool with_console)
{ {
HWND console; HWND console;
int freetty = -1;
/* FIXME: This whole function needs a protective mutex. */ /* FIXME: This whole function needs a protective mutex. */
if (WaitForSingleObject (tty_mutex, INFINITE) == WAIT_FAILED)
termios_printf ("WFSO for tty_mutex %p failed, %E", tty_mutex);
if (!with_console) if (!with_console)
console = NULL; console = NULL;
else if (!(console = GetConsoleWindow ())) else if (!(console = GetConsoleWindow ()))
{ {
char *oldtitle = new char [TITLESIZE]; char oldtitle[TITLESIZE];
if (!oldtitle)
{
termios_printf ("Can't *allocate console title buffer");
return -1;
}
if (!GetConsoleTitle (oldtitle, TITLESIZE)) if (!GetConsoleTitle (oldtitle, TITLESIZE))
{ {
termios_printf ("Can't read console title"); termios_printf ("Can't read console title");
return -1; goto out;
} }
if (WaitForSingleObject (title_mutex, INFINITE) == WAIT_FAILED)
termios_printf ("WFSO for title_mutext %p failed, %E", title_mutex);
char buf[40]; char buf[40];
__small_sprintf (buf, "cygwin.find.console.%d", myself->pid); __small_sprintf (buf, "cygwin.find.console.%d", myself->pid);
@ -234,16 +230,15 @@ tty_list::allocate_tty (int with_console)
} }
SetConsoleTitle (oldtitle); SetConsoleTitle (oldtitle);
Sleep (40); Sleep (40);
ReleaseMutex (title_mutex);
if (console == NULL) if (console == NULL)
{ {
termios_printf ("Can't find console window"); termios_printf ("Can't find console window");
return -1; goto out;
} }
} }
/* Is a tty allocated for console? */
int freetty = -1; HANDLE hmaster = NULL;
/* Is a tty allocated for console? */
for (int i = 0; i < NTTYS; i++) for (int i = 0; i < NTTYS; i++)
{ {
if (!ttys[i].exists ()) if (!ttys[i].exists ())
@ -254,17 +249,19 @@ tty_list::allocate_tty (int with_console)
break; /* No. We've got one. */ break; /* No. We've got one. */
} }
/* FIXME: Is this right? We can potentially query a "nonexistent"
tty slot after falling through from the above? */
if (with_console && ttys[i].gethwnd () == console) if (with_console && ttys[i].gethwnd () == console)
{ {
termios_printf ("console %x already associated with tty%d", termios_printf ("console %x already associated with tty%d",
console, i); console, i);
/* Is the master alive? */ /* Is the master alive? */
HANDLE hMaster; hmaster = OpenProcess (PROCESS_DUP_HANDLE, FALSE, ttys[i].master_pid);
hMaster = OpenProcess (PROCESS_DUP_HANDLE, FALSE, ttys[i].master_pid); if (hmaster)
if (hMaster)
{ {
CloseHandle (hMaster); CloseHandle (hmaster);
return i; freetty = i;
goto out;
} }
/* Master is dead */ /* Master is dead */
freetty = i; freetty = i;
@ -274,23 +271,27 @@ tty_list::allocate_tty (int 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) if (freetty == -1)
{ goto out;
system_printf ("No free ttys available");
return -1; tty *t;
} t = ttys + freetty;
tty *t = ttys + freetty;
t->init (); t->init ();
t->setsid (-1); t->setsid (-1);
t->setpgid (myself->pgid); t->setpgid (myself->pgid);
t->sethwnd (console); t->sethwnd (console);
if (with_console) out:
{ if (freetty < 0)
termios_printf ("console %x associated with tty%d", console, freetty); system_printf ("No tty allocated");
create_tty_master (freetty); else if (!with_console)
}
else
termios_printf ("tty%d allocated", freetty); termios_printf ("tty%d allocated", freetty);
else
{
termios_printf ("console %p associated with tty%d", console, freetty);
if (!hmaster)
create_tty_master (freetty);
}
ReleaseMutex (tty_mutex);
return freetty; return freetty;
} }

View File

@ -125,7 +125,7 @@ class tty_list
public: public:
tty * operator [](int n) {return ttys + n;} tty * operator [](int n) {return ttys + n;}
int allocate_tty (int n); /* n non zero if allocate a tty, pty otherwise */ int allocate_tty (bool); /* true if allocate a tty, pty otherwise */
int connect_tty (int); int connect_tty (int);
void terminate (); void terminate ();
void init (); void init ();

View File

@ -134,7 +134,7 @@ extern int cygserver_running;
#include "debug.h" #include "debug.h"
/* Events/mutexes */ /* Events/mutexes */
extern HANDLE title_mutex; extern HANDLE tty_mutex;
/**************************** Convenience ******************************/ /**************************** Convenience ******************************/