public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Fix getlogin() to check only stdin to get a valid tty
@ 2023-07-12 17:33 Jordi Sanfeliu
  2023-07-12 18:50 ` Torbjorn SVENSSON
  2023-07-17 19:06 ` Jeff Johnston
  0 siblings, 2 replies; 15+ messages in thread
From: Jordi Sanfeliu @ 2023-07-12 17:33 UTC (permalink / raw)
  To: newlib

Hello,

In my hobby OS [1] which uses Newlib C as its libc, I noticed that the 
GNU command 'logname' does output nothing when it is redirected or pipe'd.

The current getlogin() implementation [2] forces the three primary file 
descriptors (stdin, stdout and stderr) to be a valid tty before checking 
the utmp file, otherwise it returns NULL. This makes impossible to 
redirect (to a file or to a pipe), the output of a program that is using 
this function because one of its file descriptors won't be a tty.

See:

# cat test.c
#include <stdio.h>

int main(void)
{
	char *user;

	if(user = getlogin()) {
		printf("%s\n", user);
	}

	return 0;
}

# ./test
root
# ./test > xx
# cat xx
# ./test | cat
#

To fix this, I thought that only checking if stdin is a valid tty would 
be enough.

Does this sound reasonable to you?
Thanks.



diff --git a/newlib/libc/unix/getlogin.c b/newlib/libc/unix/getlogin.c
index da4f47a95..e646bcb08 100644
--- a/newlib/libc/unix/getlogin.c
+++ b/newlib/libc/unix/getlogin.c
@@ -16,9 +16,7 @@ getlogin ()
    extern char *ttyname ();
    char *tty;

-  if (((tty = ttyname (0)) == 0)
-      || ((tty = ttyname (1)) == 0)
-      || ((tty = ttyname (2)) == 0))
+  if ((tty = ttyname (0)) == 0)
      return 0;

    if ((utmp_fd = open (UTMP_FILE, O_RDONLY)) == -1)


1. https://www.fiwix.org
2. 
https://sourceware.org/git/?p=newlib-cygwin.git;a=blob_plain;f=newlib/libc/unix/getlogin.c;hb=HEAD

-- 
Jordi Sanfeliu
FIBRANET Network Services Provider
https://www.fibranet.cat

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2023-07-18 18:59 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-12 17:33 [PATCH] Fix getlogin() to check only stdin to get a valid tty Jordi Sanfeliu
2023-07-12 18:50 ` Torbjorn SVENSSON
2023-07-12 20:06   ` Brian Inglis
2023-07-13 16:26     ` Torbjorn SVENSSON
2023-07-13  8:06   ` Jordi Sanfeliu
2023-07-13 16:25     ` Torbjorn SVENSSON
2023-07-13 16:57       ` Jordi Sanfeliu
2023-07-13 19:01         ` Jeff Johnston
2023-07-17  7:54           ` Corinna Vinschen
2023-07-13 21:12         ` Stefan Tauner
2023-07-17 19:06 ` Jeff Johnston
2023-07-18  5:57   ` Jordi Sanfeliu
2023-07-18  7:08   ` Jordi Sanfeliu
2023-07-18 17:44     ` Jeff Johnston
2023-07-18 18:59       ` Jordi Sanfeliu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).