]> git.ipfire.org Git - thirdparty/tar.git/commitdiff
Actually prefer /dev/full over /dev/null as a replacement for stdin
authorSergey Poznyakoff <gray@gnu.org>
Fri, 8 Jan 2021 15:34:19 +0000 (17:34 +0200)
committerSergey Poznyakoff <gray@gnu.org>
Fri, 8 Jan 2021 15:46:32 +0000 (17:46 +0200)
* lib/stdopen.c (stdopen): Fix improper condition.
Avoid leaking extra file descriptor.
* src/tar.c (main): Set name of the stdout for diagnostics.
Bail out if stdopen fails.

lib/stdopen.c
src/tar.c

index d977572727625281bf4c8ff29acbbb25527ebf06..8c89116872146dfc47d15d9ce779c053c70c7152 100644 (file)
@@ -52,15 +52,22 @@ stdopen (void)
               static const int contrary_mode[]
                 = { O_WRONLY, O_RDONLY, O_RDONLY };
               int mode = contrary_mode[fd];
-              int new_fd;
+              int new_fd = -1;
               /* Open /dev/null with the contrary mode so that the typical
                  read (stdin) or write (stdout, stderr) operation will fail.
                  With descriptor 0, we can do even better on systems that
                  have /dev/full, by opening that write-only instead of
                  /dev/null.  The only drawback is that a write-provoked
                  failure comes with a misleading errno value, ENOSPC.  */
-              if (mode == O_RDONLY
-                  || (new_fd = open ("/dev/full", mode) != fd))
+              if (mode == O_WRONLY)
+               {
+                 if ((new_fd = open ("/dev/full", mode)) != fd)
+                   {
+                     close (new_fd);
+                     new_fd = -1;
+                   }
+               }
+             if (new_fd == -1)
                 new_fd = open ("/dev/null", mode);
               if (new_fd != fd)
                 {
index 728e38bc8e3119fdc354828118c79177c43ff489..61d77c3dbc4c4593d8938ee5b9f4b4140b29fb2e 100644 (file)
--- a/src/tar.c
+++ b/src/tar.c
@@ -2751,8 +2751,11 @@ main (int argc, char **argv)
 
   set_quoting_style (0, DEFAULT_QUOTING_STYLE);
 
+  close_stdout_set_file_name (_("stdout"));
   /* Make sure we have first three descriptors available */
-  stdopen ();
+  if (!stdopen ())
+    FATAL_ERROR ((0, errno, "%s",
+                 _("failed to ensure first three descriptors are available")));
 
   /* Pre-allocate a few structures.  */