Subject: Re: bug#78244: Exit code 4 whilst installing util-linux-selinux on arch
From: Paul Eggert <[email-masked]>
To: Jdj asd <[email-masked]>
Cc: [email-masked]
Date: Fri, 20 Jun 2025 12:16:03 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

On 2025-05-04 02:49, Jdj asd wrote:
> I was trying to install util-linux-selinux which would've replaced a core
> package when I got exit code 4. I tried running it as root and with git
> clone but I was always getting this error and this email to report the
> issue to.

I installed the attached patch which should fix the test failure. Thanks 
for reporting it.

From aec89a3e7dfaed1545b2dcaa8525df21cf61a37e Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Fri, 20 Jun 2025 11:53:21 -0700
Subject: [PATCH] tty: better fix for Bug#26371

* src/tty.c (TTY_USAGE): Rename from TTY_FAILURE, since this
is used only for usage failures.  All uses changed.
(TTY_TTYNAME_FAILURE): New constant.
(main): Remove no-longer-needed assignment of ENOENT to errno.
Make status-setting clearer too.
Report an error if ttyname fails even though stdin is a terminal,
instead of silently pretending that stdin is not a terminal.
* tests/tty/tty.sh: Test for this issue.  This should fix Bug#78244.
---
 NEWS               |  5 +++++
 THANKS.in          |  1 +
 doc/coreutils.texi |  1 +
 src/tty.c          | 20 ++++++++++++--------
 tests/tty/tty.sh   | 10 ++++++++--
 5 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/NEWS b/NEWS
index 8f9ac26d0..50f27cd87 100644
--- a/NEWS
+++ b/NEWS
@@ -17,6 +17,11 @@ GNU coreutils NEWS                                    -*- outline -*-
   'sort +0.18446744073709551615R input' on 64 bit systems.
   [bug introduced in coreutils-7.2]
 
+  tty now exits with status 4 with a special diagnostic if ttyname
+  fails even though standard input is a tty.  Formerly it quietly
+  pretended that standard input was not a tty.
+  [This bug was present in "the beginning".]
+
 ** Improvements
 
   stty supports setting arbitrary baud rates on supported systems,
diff --git a/THANKS.in b/THANKS.in
index 57ace387e..8c97a8138 100644
--- a/THANKS.in
+++ b/THANKS.in
@@ -120,6 +120,7 @@ Chris Lesniewski                    [email-masked]
 Chris Sylvain                       [email-masked]
 Chris Yeo                           [email-masked]
 Christi Alice Scarborough           [email-masked]
+Christian Brauner                   [email-masked]
 Christian Harkort                   [email-masked]
 Christian Jullien                   [email-masked]
 Christian Krackowizer               [email-masked]
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 7556571d7..fc62c6d8d 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -16020,6 +16020,7 @@ Exit status:
 1 if standard input is a non-terminal file
 2 if given incorrect arguments
 3 if a write error occurs
+4 if the terminal's name cannot be determined
 @end display

diff --git a/src/tty.c b/src/tty.c
index b01f1a207..d5dea5200 100644
--- a/src/tty.c
+++ b/src/tty.c
@@ -33,8 +33,9 @@
 enum
   {
     TTY_STDIN_NOTTY = 1,
-    TTY_FAILURE = 2,
-    TTY_WRITE_ERROR = 3
+    TTY_USAGE = 2,
+    TTY_WRITE_ERROR = 3,
+    TTY_TTYNAME_FAILURE = 4
   };
 
 /* The official name of this program (e.g., no 'g' prefix).  */
@@ -103,26 +104,29 @@ main (int argc, char **argv)
         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
 
         default:
-          usage (TTY_FAILURE);
+          usage (TTY_USAGE);
         }
     }
 
   if (optind < argc)
     {
       error (0, 0, _("extra operand %s"), quote (argv[optind]));
-      usage (TTY_FAILURE);
+      usage (TTY_USAGE);
     }
 
-  errno = ENOENT;
-
   if (silent)
     return isatty (STDIN_FILENO) ? EXIT_SUCCESS : TTY_STDIN_NOTTY;
 
-  int status = EXIT_SUCCESS;
+  int status;
   char const *tty = ttyname (STDIN_FILENO);
 
-  if (! tty)
+  if (tty)
+    status = EXIT_SUCCESS;
+  else
     {
+      int ttyname_err = errno;
+      if (isatty (STDIN_FILENO))
+        error (TTY_TTYNAME_FAILURE, ttyname_err, "ttyname");
       tty = _("not a tty");
       status = TTY_STDIN_NOTTY;
     }
diff --git a/tests/tty/tty.sh b/tests/tty/tty.sh
index 8201b42eb..b2b8aa25a 100755
--- a/tests/tty/tty.sh
+++ b/tests/tty/tty.sh
@@ -20,8 +20,14 @@
 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
 print_ver_ tty
 
+tty_works_on_stdin=false
+
 if test -t 0; then
-  tty || fail=1
+  if tty; then
+    tty_works_on_stdin=true
+  else
+    test $? -eq 4 || fail=1
+  fi
   tty -s || fail=1
 fi
 
@@ -34,7 +40,7 @@ returns_ 2 tty a || fail=1
 returns_ 2 tty -s a || fail=1
 
 if test -w /dev/full && test -c /dev/full; then
-  if test -t 0; then
+  if $tty_works_on_stdin; then
     returns_ 3 tty >/dev/full || fail=1
   fi
   returns_ 3 tty </dev/null >/dev/full || fail=1
-- 
2.48.1