* 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.
This commit is contained in:
Christopher Faylor 2011-11-08 06:26:15 +00:00
parent 5d46c490dd
commit 926014453f
5 changed files with 24 additions and 5 deletions

View File

@ -1,3 +1,12 @@
2011-11-08 Christopher Faylor <me.cygwin2011@cgf.cx>
* 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 <me.cygwin2011@cgf.cx> 2011-11-08 Christopher Faylor <me.cygwin2011@cgf.cx>
* include/cygwin/stdlib.h: Update copyright. * include/cygwin/stdlib.h: Update copyright.

View File

@ -887,7 +887,7 @@ static void
decode_tty (char *buf, WCHAR *w32) decode_tty (char *buf, WCHAR *w32)
{ {
int ttyn = wcstol (w32, NULL, 10); 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 /* Try to derive posix filename from given handle. Return true if

View File

@ -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 class fhandler_pty_master: public fhandler_pty_common
{ {
int pktmode; // non-zero if pty in a packet mode. int pktmode; // non-zero if pty in a packet mode.

View File

@ -1424,7 +1424,7 @@ fhandler_pty_master::ptsname_r (char *buf, size_t buflen)
{ {
char tmpbuf[TTY_NAME_MAX]; char tmpbuf[TTY_NAME_MAX];
__small_sprintf (tmpbuf, "/dev/pty%d", get_unit ()); __ptsname (tmpbuf, get_unit ());
if (buflen <= strlen (tmpbuf)) if (buflen <= strlen (tmpbuf))
{ {
set_errno (ERANGE); set_errno (ERANGE);

View File

@ -35,6 +35,10 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <fcntl.h> #include <fcntl.h>
#include <err.h> #include <err.h>
#include "path.h"
#include "fhandler.h"
#include "dtable.h"
#include "cygheap.h"
#include "cygtls.h" #include "cygtls.h"
extern "C" int extern "C" int
@ -108,20 +112,25 @@ openpty (int *amaster, int *aslave, char *name, const struct termios *termp,
{ {
grantpt (master); grantpt (master);
unlockpt (master); unlockpt (master);
strcpy (pts, ptsname (master)); __ptsname (pts, cygheap->fdtab[master]->get_unit ());
revoke (pts); revoke (pts);
if ((slave = open (pts, O_RDWR | O_NOCTTY)) >= 0) if ((slave = open (pts, O_RDWR | O_NOCTTY)) >= 0)
{ {
if (amaster) if (amaster)
*amaster = master; *amaster = master;
if (aslave)
*aslave = slave;
if (name) if (name)
strcpy (name, pts); strcpy (name, pts);
if (termp) if (termp)
tcsetattr (slave, TCSAFLUSH, termp); tcsetattr (slave, TCSAFLUSH, termp);
if (winp) if (winp)
ioctl (slave, TIOCSWINSZ, (char *) 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; return 0;
} }
close (master); close (master);