From 926014453f474ba583f485751600e851c4d5666a Mon Sep 17 00:00:00 2001 From: Christopher Faylor Date: Tue, 8 Nov 2011 06:26:15 +0000 Subject: [PATCH] * fhandler.h (__ptsname): New macro. * dtable.cc (decode_tty): Use __ptsname to generate the slave pty name. * fhandler_tty.cc (fhandler_pty_master::ptsname_r): Ditto. * bsdlib.cc: Add needed includes for openpty() changes. (openpty): Use __ptsname to generate the slave pty name. Close slave fd when aslave == NULL. --- winsup/cygwin/ChangeLog | 9 +++++++++ winsup/cygwin/dtable.cc | 2 +- winsup/cygwin/fhandler.h | 1 + winsup/cygwin/fhandler_tty.cc | 2 +- winsup/cygwin/libc/bsdlib.cc | 15 ++++++++++++--- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog index 4381fc406..c979d76ca 100644 --- a/winsup/cygwin/ChangeLog +++ b/winsup/cygwin/ChangeLog @@ -1,3 +1,12 @@ +2011-11-08 Christopher Faylor + + * fhandler.h (__ptsname): New macro. + * dtable.cc (decode_tty): Use __ptsname to generate the slave pty name. + * fhandler_tty.cc (fhandler_pty_master::ptsname_r): Ditto. + * bsdlib.cc: Add needed includes for openpty() changes. + (openpty): Use __ptsname to generate the slave pty name. Close slave + fd when aslave == NULL. + 2011-11-08 Christopher Faylor * include/cygwin/stdlib.h: Update copyright. diff --git a/winsup/cygwin/dtable.cc b/winsup/cygwin/dtable.cc index 982a671c2..f8fbbd25b 100644 --- a/winsup/cygwin/dtable.cc +++ b/winsup/cygwin/dtable.cc @@ -887,7 +887,7 @@ static void decode_tty (char *buf, WCHAR *w32) { int ttyn = wcstol (w32, NULL, 10); - __small_sprintf (buf, "/dev/pty%d", ttyn); + __ptsname (buf, ttyn); } /* Try to derive posix filename from given handle. Return true if diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h index 189c0f3b5..4ceba4e5d 100644 --- a/winsup/cygwin/fhandler.h +++ b/winsup/cygwin/fhandler.h @@ -1456,6 +1456,7 @@ class fhandler_pty_slave: public fhandler_pty_common } }; +#define __ptsname(buf, unit) __small_sprintf ((buf), "/dev/pty%d", (unit)) class fhandler_pty_master: public fhandler_pty_common { int pktmode; // non-zero if pty in a packet mode. diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc index c68211b66..9431cb067 100644 --- a/winsup/cygwin/fhandler_tty.cc +++ b/winsup/cygwin/fhandler_tty.cc @@ -1424,7 +1424,7 @@ fhandler_pty_master::ptsname_r (char *buf, size_t buflen) { char tmpbuf[TTY_NAME_MAX]; - __small_sprintf (tmpbuf, "/dev/pty%d", get_unit ()); + __ptsname (tmpbuf, get_unit ()); if (buflen <= strlen (tmpbuf)) { set_errno (ERANGE); diff --git a/winsup/cygwin/libc/bsdlib.cc b/winsup/cygwin/libc/bsdlib.cc index 3b6e7e428..599df7107 100644 --- a/winsup/cygwin/libc/bsdlib.cc +++ b/winsup/cygwin/libc/bsdlib.cc @@ -35,6 +35,10 @@ #include #include #include +#include "path.h" +#include "fhandler.h" +#include "dtable.h" +#include "cygheap.h" #include "cygtls.h" extern "C" int @@ -108,20 +112,25 @@ openpty (int *amaster, int *aslave, char *name, const struct termios *termp, { grantpt (master); unlockpt (master); - strcpy (pts, ptsname (master)); + __ptsname (pts, cygheap->fdtab[master]->get_unit ()); revoke (pts); if ((slave = open (pts, O_RDWR | O_NOCTTY)) >= 0) { if (amaster) *amaster = master; - if (aslave) - *aslave = slave; if (name) strcpy (name, pts); if (termp) tcsetattr (slave, TCSAFLUSH, termp); if (winp) ioctl (slave, TIOCSWINSZ, (char *) winp); + /* The man page doesn't say that aslave can be NULL but we have + allowed it for years. As of 2011-11-08 we now avoid a handle + leak in this case. */ + if (aslave) + *aslave = slave; + else + close (slave); return 0; } close (master);