@@ -25,6 +25,7 @@
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
+#include <sys/sysmacros.h>
#include <_itoa.h>
@@ -33,6 +34,19 @@
char *__ttyname;
#endif
+/* Return true if this is a UNIX98 pty device, as defined in
+ linux/Documentation/devices.txt. */
+static int
+is_pty (struct stat64 *sb)
+{
+#ifdef _STATBUF_ST_RDEV
+ int m = major (sb->st_rdev);
+ return (136 <= m && m <= 143);
+#else
+ return false;
+#endif
+}
+
static char *getttyname (const char *dev, dev_t mydev,
ino64_t myino, int save, int *dostat)
internal_function;
@@ -170,12 +184,19 @@ ttyname (int fd)
#ifdef _STATBUF_ST_RDEV
&& S_ISCHR (st1.st_mode)
&& st1.st_rdev == st.st_rdev
+ && st1.st_dev == st.st_dev
#else
&& st1.st_ino == st.st_ino
&& st1.st_dev == st.st_dev
#endif
)
return ttyname_buf;
+
+ /* If the link doesn't exist, then it points to a device in another
+ namespace. If it is a UNIX98 pty, then return the /proc/self
+ fd, as it points to a name unreachable in our namespace. */
+ if (is_pty (&st) && strlen (procname) < buflen - 1)
+ return strcpy (ttyname_buf, procname);
}
if (__xstat64 (_STAT_VER, "/dev/pts", &st1) == 0 && S_ISDIR (st1.st_mode))
@@ -25,6 +25,7 @@
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
+#include <sys/sysmacros.h>
#include <_itoa.h>
@@ -32,6 +33,19 @@ static int getttyname_r (char *buf, size_t buflen,
dev_t mydev, ino64_t myino, int save,
int *dostat) internal_function;
+/* Return true if this is a UNIX98 pty device, as defined in
+ linux/Documentation/devices.txt. */
+static int
+is_pty (struct stat64 *sb)
+{
+#ifdef _STATBUF_ST_RDEV
+ int m = major (sb->st_rdev);
+ return (136 <= m && m <= 143);
+#else
+ return false;
+#endif
+}
+
static int
internal_function attribute_compat_text_section
getttyname_r (char *buf, size_t buflen, dev_t mydev, ino64_t myino,
@@ -158,6 +172,15 @@ __ttyname_r (int fd, char *buf, size_t buflen)
#endif
)
return 0;
+
+ /* If the link doesn't exist, then it points to a device in another
+ namespace. If it is a UNIX98 pty, then return the /proc/self
+ fd, as it points to a name unreachable in our namespace. */
+ if (is_pty (&st) && strlen (procname) < buflen - 1)
+ {
+ strcpy (buf, procname);
+ return 0;
+ }
}
/* Prepare the result buffer. */