From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 111391 invoked by alias); 9 Aug 2016 20:41:40 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Received: (qmail 111292 invoked by uid 89); 9 Aug 2016 20:41:39 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.5 required=5.0 tests=AWL,BAYES_00,KAM_LAZY_DOMAIN_SECURITY,RP_MATCHES_RCVD autolearn=no version=3.3.2 spammy=our X-HELO: h2.hallyn.com Date: Tue, 09 Aug 2016 20:41:00 -0000 From: "Serge E. Hallyn" To: "Serge E. Hallyn" Cc: libc-alpha@sourceware.org, vapier@gentoo.org Subject: Re: [PATCH] linux ttyname and ttyname_r: return link if appropriate Message-ID: <20160809204126.GA2873@mail.hallyn.com> References: <20160418195318.GB30476@ubuntumail> <20160418200222.GY5369@vapier.lan> <20160418202301.GA30919@ubuntumail> <20160420021012.GA19449@ubuntumail> <20160420022823.GT5369@vapier.lan> <20160420185141.GA31095@ubuntumail> <20160727165711.GA27815@altlinux.org> <20160806020855.GA19897@mail.hallyn.com> <20160806084559.GS6702@vapier.lan> <20160806150002.GA24315@mail.hallyn.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20160806150002.GA24315@mail.hallyn.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-SW-Source: 2016-08/txt/msg00307.txt.bz2 Quoting Serge E. Hallyn (serge@hallyn.com): > Quoting Mike Frysinger (vapier@gentoo.org): > > On 05 Aug 2016 21:08, Serge E. Hallyn wrote: > > > #ifdef _STATBUF_ST_RDEV > > > && S_ISCHR (st1.st_mode) > > > && st1.st_rdev == st.st_rdev > > > + && st1.st_dev == st.st_dev > > > + && st1.st_ino == st.st_ino > > > #else > > > && st1.st_ino == st.st_ino > > > && st1.st_dev == st.st_dev > > > #endif > > > > wouldn't it be better to shuffle the ifdef then ? > > #ifdef _STATBUF_ST_RDEV > > && S_ISCHR (st1.st_mode) > > && st1.st_rdev == st.st_rdev > > #endif > > && st1.st_ino == st.st_ino > > && st1.st_dev == st.st_dev) > > That occurred to me when I looked at it again after sending, > seems reasonable. Is this now ready to be applied? More peopel appear to be running into it, https://github.com/lxc/lxc/issues/554 > Subject: [PATCH] linux ttyname and ttyname_r: return link if appropriate > > 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. > --- > sysdeps/unix/sysv/linux/ttyname.c | 25 ++++++++++++++++++++++--- > sysdeps/unix/sysv/linux/ttyname_r.c | 26 ++++++++++++++++++++++++-- > 2 files changed, 46 insertions(+), 5 deletions(-) > > diff --git a/sysdeps/unix/sysv/linux/ttyname.c b/sysdeps/unix/sysv/linux/ttyname.c > index 7a001b4..3065ed4 100644 > --- a/sysdeps/unix/sysv/linux/ttyname.c > +++ b/sysdeps/unix/sysv/linux/ttyname.c > @@ -25,6 +25,7 @@ > #include > #include > #include > +#include > > #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,17 @@ ttyname (int fd) > #ifdef _STATBUF_ST_RDEV > && S_ISCHR (st1.st_mode) > && st1.st_rdev == st.st_rdev > -#else > - && st1.st_ino == st.st_ino > - && st1.st_dev == st.st_dev > #endif > + && st1.st_dev == st.st_dev > + && st1.st_ino == st.st_ino > ) > 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)) > diff --git a/sysdeps/unix/sysv/linux/ttyname_r.c b/sysdeps/unix/sysv/linux/ttyname_r.c > index d15bc74..97527c0 100644 > --- a/sysdeps/unix/sysv/linux/ttyname_r.c > +++ b/sysdeps/unix/sysv/linux/ttyname_r.c > @@ -25,6 +25,7 @@ > #include > #include > #include > +#include > > #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, > @@ -152,12 +166,20 @@ __ttyname_r (int fd, char *buf, size_t buflen) > #ifdef _STATBUF_ST_RDEV > && S_ISCHR (st1.st_mode) > && st1.st_rdev == st.st_rdev > -#else > +#endif > && st1.st_ino == st.st_ino > && st1.st_dev == st.st_dev > -#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. */ > -- > 2.7.4