From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/13258 Path: news.gmane.org!.POSTED!not-for-mail From: Benjamin Peterson Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH] improve error handling of ttyname_r and isatty Date: Thu, 13 Sep 2018 09:27:55 -0700 Message-ID: <20180913162755.18911-1-benjamin@python.org> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org X-Trace: blaine.gmane.org 1536855965 25092 195.159.176.226 (13 Sep 2018 16:26:05 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 13 Sep 2018 16:26:05 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-13274-gllmg-musl=m.gmane.org@lists.openwall.com Thu Sep 13 18:26:01 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 1g0URN-0006P4-2s for gllmg-musl@m.gmane.org; Thu, 13 Sep 2018 18:26:01 +0200 Original-Received: (qmail 27808 invoked by uid 550); 13 Sep 2018 16:28:10 -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 27770 invoked from network); 13 Sep 2018 16:28:09 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=python.org; s=200901; t=1536856078; bh=UuTidNF3/NQyQMskLU11TwxLo6wZjLQ3KGu6DY3f8Dg=; h=From:To:Subject:Date:From; b=hR8TuwjxhFupqKkwT9CLGTJhay6zwyg5fGTtVxpXEzp0fA4tprJWL92laeoDEjs6X FSJEXBFaWucv7olrZ7h4DjoNKZF3U34hkDG79nnHi9c6bP/L/bzIF7JWsNYUIWM+zi NJHK993aTRwP1SIXBX6G1UQkAbVgf1RW+pEw0tjc= X-ME-Proxy: X-ME-Sender: X-Mailer: git-send-email 2.17.1 Xref: news.gmane.org gmane.linux.lib.musl.general:13258 Archived-At: POSIX allows ttyname(_r) and isatty to return EBADF if passed file descriptor is invalid. --- src/unistd/isatty.c | 6 +++++- src/unistd/ttyname_r.c | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/unistd/isatty.c b/src/unistd/isatty.c index c8badaf5..3d5213c6 100644 --- a/src/unistd/isatty.c +++ b/src/unistd/isatty.c @@ -1,9 +1,13 @@ #include +#include #include #include "syscall.h" int isatty(int fd) { struct winsize wsz; - return !__syscall(SYS_ioctl, fd, TIOCGWINSZ, &wsz); + unsigned long r = __syscall_ret(__syscall(SYS_ioctl, fd, TIOCGWINSZ, &wsz)); + if (r == 0) return 1; + if (errno != EBADF) errno = ENOTTY; + return 0; } diff --git a/src/unistd/ttyname_r.c b/src/unistd/ttyname_r.c index 33aa4ae1..3f836a6a 100644 --- a/src/unistd/ttyname_r.c +++ b/src/unistd/ttyname_r.c @@ -10,7 +10,10 @@ int ttyname_r(int fd, char *name, size_t size) char procname[sizeof "/proc/self/fd/" + 3*sizeof(int) + 2]; ssize_t l; - if (!isatty(fd)) return ENOTTY; + if (!isatty(fd)) { + if (errno != EBADF) errno = ENOTTY; + return errno; + } __procfdname(procname, fd); l = readlink(procname, name, size); -- 2.17.1