public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/1] linux ttyname: return link if appropriate
@ 2016-04-15 15:29 Serge Hallyn
  2016-04-15 16:27 ` Florian Weimer
  0 siblings, 1 reply; 36+ messages in thread
From: Serge Hallyn @ 2016-04-15 15:29 UTC (permalink / raw)
  To: libc-alpha; +Cc: Serge Hallyn

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

^ permalink raw reply	[flat|nested] 36+ messages in thread

end of thread, other threads:[~2016-10-04 12:47 UTC | newest]

Thread overview: 36+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-15 15:29 [PATCH 1/1] linux ttyname: return link if appropriate Serge Hallyn
2016-04-15 16:27 ` Florian Weimer
2016-04-15 16:47   ` Serge Hallyn
2016-04-15 17:06     ` Florian Weimer
2016-04-15 17:43       ` Serge Hallyn
2016-04-15 18:48         ` Serge Hallyn
2016-04-15 19:59           ` Mike Frysinger
2016-04-18 19:53             ` Serge Hallyn
2016-04-18 19:53             ` [PATCH 1/2] " Serge Hallyn
2016-04-18 20:02               ` Mike Frysinger
2016-04-18 20:23                 ` Serge Hallyn
2016-04-20  2:10                   ` [PATCH] linux ttyname and ttyname_r: " Serge Hallyn
2016-04-20  2:28                     ` Mike Frysinger
2016-04-20 18:51                       ` Serge Hallyn
2016-07-27 13:43                         ` Serge E. Hallyn
2016-07-27 17:28                         ` Dmitry V. Levin
2016-08-06  2:09                           ` Serge E. Hallyn
2016-08-06  8:46                             ` Mike Frysinger
2016-08-06 15:00                               ` Serge E. Hallyn
2016-08-09 20:41                                 ` Serge E. Hallyn
2016-08-09 21:18                                 ` Dmitry V. Levin
2016-08-09 21:39                                   ` Serge E. Hallyn
2016-08-09 23:26                                     ` Dmitry V. Levin
2016-08-10 23:24                                       ` Serge E. Hallyn
2016-08-10 23:48                                         ` Dmitry V. Levin
2016-08-10  6:38                                     ` Florian Weimer
2016-08-10 23:03                                       ` Serge E. Hallyn
2016-08-10 23:18                                         ` Dmitry V. Levin
2016-08-10 23:26                                           ` Serge E. Hallyn
2016-10-03  6:16                                           ` Serge E. Hallyn
2016-10-03  7:29                                             ` Andreas Schwab
2016-10-03 14:05                                               ` [PATCH 1/1] linux ttyname and ttyname_r: do not return wrong results Serge E. Hallyn
2016-10-04  9:53                                             ` [PATCH] linux ttyname and ttyname_r: return link if appropriate Florian Weimer
2016-10-04 12:47                                               ` Serge E. Hallyn
2016-04-18 19:54             ` [PATCH 2/2] linux " Serge Hallyn
2016-04-18 20:03               ` Mike Frysinger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).