From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/13255 Path: news.gmane.org!.POSTED!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: [PATCH] return EBADF from ttyname_r Date: Thu, 13 Sep 2018 11:29:16 -0400 Message-ID: <20180913152916.GI1878@brightrain.aerifal.cx> References: <20180913003424.12234-1-benjamin@python.org> <20180913085314.GR4418@port70.net> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: blaine.gmane.org 1536852446 13255 195.159.176.226 (13 Sep 2018 15:27:26 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 13 Sep 2018 15:27:26 +0000 (UTC) User-Agent: Mutt/1.5.21 (2010-09-15) Cc: Benjamin Peterson To: musl@lists.openwall.com Original-X-From: musl-return-13271-gllmg-musl=m.gmane.org@lists.openwall.com Thu Sep 13 17:27:21 2018 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1g0TWb-0003K4-Gs for gllmg-musl@m.gmane.org; Thu, 13 Sep 2018 17:27:21 +0200 Original-Received: (qmail 27700 invoked by uid 550); 13 Sep 2018 15:29:30 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 27677 invoked from network); 13 Sep 2018 15:29:29 -0000 Content-Disposition: inline In-Reply-To: <20180913085314.GR4418@port70.net> Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:13255 Archived-At: On Thu, Sep 13, 2018 at 10:53:14AM +0200, Szabolcs Nagy wrote: > * Benjamin Peterson [2018-09-12 17:34:24 -0700]: > > POSIX allows ttyname(_r) to return EBADF if passed file descriptor is invalid. > > i think EBADF is always a 'may fail' in posix, so not strictly required. > > > - if (!isatty(fd)) return ENOTTY; > > + if (!isatty(fd)) { > > + if (errno == EBADF) return EBADF; > > + return ENOTTY; > > + } > > musl isatty uses __syscall which does not set errno so this is wrong. > > note that on glibc isatty sets errno according what the kernel returns > however linux has different code paths in ioctl for different type of > fds and in some cases it can fail in interesting ways (iirc on a socket > fd it will fail with EINVAL or EFAULT at least on some linux versions > and it can even spuriously succeed on non-tty fds because the TCGETS > ioctl command was reused on some audio device to do different things) That's why we no longer use TCGETS but rather TIOCGWINSZ. > this means users cannot rely on errno value being sane, > so there is not much point trying to do something fancy here. I'm not sure this is actually an issue anymore, but if it is, we should simply translate anything other than EBADF to ENOTTY. There is no other meaningful error. Either the fd is valid or it's not, and if it is valid, either it is a tty or it's not. Rich