From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.fibranet.cat (www.fibranet.cat [88.99.13.26]) by sourceware.org (Postfix) with ESMTPS id 4552D3858C1F for ; Wed, 12 Jul 2023 17:34:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4552D3858C1F Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=fibranet.cat Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=fibranet.cat Received: from [192.168.1.2] (233.red-83-41-32.dynamicip.rima-tde.net [83.41.32.233]) (authenticated bits=0) by mail.fibranet.cat (8.15.2/8.15.2) with ESMTPSA id 36CHXwQ91489152 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Wed, 12 Jul 2023 19:34:01 +0200 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.fibranet.cat 36CHXwQ91489152 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fibranet.cat; s=default; t=1689183241; bh=WxXo2nxQDpOZcjaKOr3cUVpBfmvd9YPVLLojST7axK8=; h=Date:From:Subject:To:From; b=YsfZA9OeKmv/CMMbPMEMcyt7Wsbhmc7o0OPPXHAw8w1S+9r/q5zPFk9OPVeto8KDJ N0TpUa0Aj49rMpeMO9d/+E9zhrUHJdlGiyAXJ2VDJXSiFgajnvOt7y/x6IUBnzggJ5 +K/jCJkq2ZVb2WvbELQ6OPOW5r+u3GTXEuP1aKAE= Message-ID: <1de3b3ee-7dd8-db16-6e17-365dbd9fde84@fibranet.cat> Date: Wed, 12 Jul 2023 19:33:58 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 From: Jordi Sanfeliu Subject: [PATCH] Fix getlogin() to check only stdin to get a valid tty To: newlib@sourceware.org Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.6.2 (mail.fibranet.cat [88.99.13.26]); Wed, 12 Jul 2023 19:34:01 +0200 (CEST) X-FibraNet-MailScanner-Information: FibraNet E-Mail Virus Protection Service X-FibraNet-MailScanner-ID: 36CHXwQ91489152 X-FibraNet-MailScanner: Found to be clean X-FibraNet-MailScanner-SpamCheck: X-FibraNet-MailScanner-From: jordi@fibranet.cat X-FibraNet-MailScanner-Watermark: 1689788042.38707@EuRPx5+kEVQbUVmEd4o0sQ X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 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