aboutsummaryrefslogtreecommitdiff
path: root/login
diff options
context:
space:
mode:
Diffstat (limited to 'login')
-rw-r--r--login/openpty.c30
1 files changed, 24 insertions, 6 deletions
diff --git a/login/openpty.c b/login/openpty.c
index 9e556c27a5..6703128ea8 100644
--- a/login/openpty.c
+++ b/login/openpty.c
@@ -94,6 +94,8 @@ openpty (int *amaster, int *aslave, char *name,
char *buf = _buf;
int master, ret = -1, slave = -1;
+ *buf = '\0';
+
master = getpt ();
if (master == -1)
return -1;
@@ -104,12 +106,22 @@ openpty (int *amaster, int *aslave, char *name,
if (unlockpt (master))
goto on_error;
- if (pts_name (master, &buf, sizeof (_buf)))
- goto on_error;
-
- slave = open (buf, O_RDWR | O_NOCTTY);
+#ifdef TIOCGPTPEER
+ /* Try to allocate slave fd solely based on master fd first. */
+ slave = ioctl (master, TIOCGPTPEER, O_RDWR | O_NOCTTY);
+#endif
if (slave == -1)
- goto on_error;
+ {
+ /* Fallback to path-based slave fd allocation in case kernel doesn't
+ * support TIOCGPTPEER.
+ */
+ if (pts_name (master, &buf, sizeof (_buf)))
+ goto on_error;
+
+ slave = open (buf, O_RDWR | O_NOCTTY);
+ if (slave == -1)
+ goto on_error;
+ }
/* XXX Should we ignore errors here? */
if (termp)
@@ -122,7 +134,13 @@ openpty (int *amaster, int *aslave, char *name,
*amaster = master;
*aslave = slave;
if (name != NULL)
- strcpy (name, buf);
+ {
+ if (*buf == '\0')
+ if (pts_name (master, &buf, sizeof (_buf)))
+ goto on_error;
+
+ strcpy (name, buf);
+ }
ret = 0;