From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 7F31C3858431 for ; Mon, 17 Jul 2023 19:06:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7F31C3858431 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1689620798; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=5XvkdeB6npHRAm4a/0ohOecJEdIV9PlICocaMvAmdEA=; b=G9MCtQd4coFFtYkdz8G9OKmdC1//mGyA3Yrp0v4MeEpB6+tI4dbxvqoqC/9snmPz3Zm2jk yu4J71TX6CUVXuX1vjA9VvaRhPMcfPbHu2EsPienvGz7a1HyclO/a0/ALAbdjeqzKa7yPF pjeSEhozR6X72tcip1tcDTpnds3Aj4w= Received: from mail-oa1-f70.google.com (mail-oa1-f70.google.com [209.85.160.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-21-UPgSWgXrOxK5X3Fwh_FHjg-1; Mon, 17 Jul 2023 15:06:36 -0400 X-MC-Unique: UPgSWgXrOxK5X3Fwh_FHjg-1 Received: by mail-oa1-f70.google.com with SMTP id 586e51a60fabf-1b3cb1c2848so1178393fac.0 for ; Mon, 17 Jul 2023 12:06:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689620795; x=1692212795; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=5XvkdeB6npHRAm4a/0ohOecJEdIV9PlICocaMvAmdEA=; b=UBvJh+ILKmXBTQb8MvmASHBv7PwI4rLLln6VIr+wv4KGhIkxgj3sln3uUemh60teZC OzkGxkY4zBkEYc2tdh5WuoD8D2GId90AVHnJ+MIlniyVh9LKjQJj+sLXxTbKXv30Al90 wuaems44k5drkE54MMQMxJZqkFo2rAESKlzlgQIFqQAIolBJm2IY4P4jKG2Ads2JPP1U YzWIco1Np3gXaJ6pPO7srtKMfTFAkIdKSQlH30sZ+NmkNBBDhqwpBAxrjv0CbNQBKQet 93gqypRIjA6CVLR+8bV+kTDTUjWNB/tzJxrLTrIZNo+MfZRilYmd7yAc0L3sUVZN1uKZ 4N0Q== X-Gm-Message-State: ABy/qLYsZvVodTa7rb9obGp06Thq5iyFRC9kcRbjOX0Pd6+jIccepTKL H21n7AiFKOa9rksbY7SjpUqqMed7kB7DRa/F59S609ce3/m0N1DjjF3I9zq+a4Uk2a7ljoS9hX7 lSRdGB7rvlh8I6ewvebnLLoDPAE1D0NXn04r40mw= X-Received: by 2002:a05:6359:5aa:b0:134:c4ff:94a3 with SMTP id ee42-20020a05635905aa00b00134c4ff94a3mr1787989rwb.0.1689620795500; Mon, 17 Jul 2023 12:06:35 -0700 (PDT) X-Google-Smtp-Source: APBJJlHZrkxB2587SeofWToDlTH+dDCYEdzX7v/48Ha/rUUNnmlWKHuw+pCmV/cPdBCjMeZ/q9Rr134W0c5sLLfOk+c= X-Received: by 2002:a05:6359:5aa:b0:134:c4ff:94a3 with SMTP id ee42-20020a05635905aa00b00134c4ff94a3mr1787969rwb.0.1689620795017; Mon, 17 Jul 2023 12:06:35 -0700 (PDT) MIME-Version: 1.0 References: <1de3b3ee-7dd8-db16-6e17-365dbd9fde84@fibranet.cat> In-Reply-To: <1de3b3ee-7dd8-db16-6e17-365dbd9fde84@fibranet.cat> From: Jeff Johnston Date: Mon, 17 Jul 2023 15:06:23 -0400 Message-ID: Subject: Re: [PATCH] Fix getlogin() to check only stdin to get a valid tty To: Jordi Sanfeliu Cc: newlib@sourceware.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/alternative; boundary="0000000000004aeab70600b37fcc" X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,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: --0000000000004aeab70600b37fcc Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Original patch pushed, thanks. -- Jeff J. On Wed, Jul 12, 2023 at 1:34=E2=80=AFPM Jordi Sanfeliu via Newlib < newlib@sourceware.org> wrote: > 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 =3D 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 =3D ttyname (0)) =3D=3D 0) > - || ((tty =3D ttyname (1)) =3D=3D 0) > - || ((tty =3D ttyname (2)) =3D=3D 0)) > + if ((tty =3D ttyname (0)) =3D=3D 0) > return 0; > > if ((utmp_fd =3D open (UTMP_FILE, O_RDONLY)) =3D=3D -1) > > > 1. https://www.fiwix.org > 2. > > https://sourceware.org/git/?p=3Dnewlib-cygwin.git;a=3Dblob_plain;f=3Dnewl= ib/libc/unix/getlogin.c;hb=3DHEAD > > -- > Jordi Sanfeliu > FIBRANET Network Services Provider > https://www.fibranet.cat > > --0000000000004aeab70600b37fcc--