This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH 1/1] linux ttyname: return link if appropriate
- From: Serge Hallyn <serge dot hallyn at ubuntu dot com>
- To: libc-alpha at sourceware dot org
- Cc: Serge Hallyn <serge dot hallyn at ubuntu dot com>
- Date: Fri, 15 Apr 2016 15:29:29 +0000
- Subject: [PATCH 1/1] linux ttyname: return link if appropriate
- Authentication-results: sourceware.org; auth=none
The current ttyname does the wrong thing in two cases:
1. If the passed-in link (say /proc/self/fd/0) points to a
device, say /dev/pts/2, in a parent mount namespace, and a
/dev/pts/2 exists (in a different devpts) in the current
namespace, then it returns /dev/pts/2. But /dev/pts/2 is
NOT the current tty, it is a different file and device.
2. If the passed-in link (say /proc/self/fd/0) points to
a device, say /dev/pts/2, in a parent mount namespace, and
/dev/pts/2 does not exist in the current namespace, it
returns success but an empty name. As far as I can tell,
there is no reason for it to not return /proc/self/fd/0.
http://pubs.opengroup.org/onlinepubs/009695399/functions/ttyname.html
does not say anything about not returning a link.
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
---
sysdeps/unix/sysv/linux/ttyname.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/sysdeps/unix/sysv/linux/ttyname.c b/sysdeps/unix/sysv/linux/ttyname.c
index 7a001b4..b0500a9 100644
--- a/sysdeps/unix/sysv/linux/ttyname.c
+++ b/sysdeps/unix/sysv/linux/ttyname.c
@@ -170,12 +170,21 @@ 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 dvice in another
+ * namespace. We've already verified it's a tty. Just return the
+ * link itself. */
+ if (strlen(procname) < buflen - 1) {
+ memcpy(ttyname_buf, procname, strlen(procname));
+ ttyname_buf[strlen(procname)] = '\0';
+ return ttyname_buf;
+ }
}
if (__xstat64 (_STAT_VER, "/dev/pts", &st1) == 0 && S_ISDIR (st1.st_mode))
--
2.7.4