Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Thu, 13 Sep 2018 11:29:16 -0400
From: Rich Felker <>
Cc: Benjamin Peterson <>
Subject: Re: [PATCH] return EBADF from ttyname_r

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.


Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.