Re: [PATCH] Fix getlogin() to check only stdin to get a valid tty

Torbjorn SVENSSON <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>

On 2023-07-13 10:06, Jordi Sanfeliu via Newlib wrote:
> Hello Torbjörn,
> 
> Thanks for you reply.
> 
> It seems to me that this code returns 0 as soon as one of the 3 fds is 
> 0, regardless if the rest are valid tty names.
> 
> I think that what you meant is this:
> 
>          if ((tty = ttyname (0)) == 0)
>              if ((tty = ttyname (1)) == 0)
>                  if ((tty = ttyname (2)) == 0)
>                      return 0;
> 
> which IMO would be even a better patch.
> 
> What do you think?

Actually, you're right. Sorry for the confusion.
I took a sneak peak at how glibc does this and there is this comment:

   /* Get name of tty connected to fd 0.  Return NULL if not a tty or
      if fd 0 isn't open.  Note that a lot of documentation says that
      getlogin() is based on the controlling terminal---what they
      really mean is "the terminal connected to standard input".  The
      getlogin() implementation of DEC Unix, SunOS, Solaris, HP-UX all
      return NULL if fd 0 has been closed, so this is the compatible
      thing to do.  Note that ttyname(open("/dev/tty")) on those
      systems returns /dev/tty, so that is not a possible solution for
      getlogin().  */

Based on this comment, I guess it would be sane to drop the check on 
stdout and stderr, but it would have the consequence that you are not 
able to pipe some data on stdin to the application that calls getlogin 
as it would fail in that scenario.

I'm not a maintainer of newlib so I don't really have anything to say 
about what path you decide to go.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.