740027
From e56f09afdbd4bd920e4a1f3b03e29eaccd954dac Mon Sep 17 00:00:00 2001
740027
From: Kamil Dudka <kdudka@redhat.com>
740027
Date: Tue, 6 Sep 2016 17:38:26 +0200
740027
Subject: [PATCH] ls: allow interruption when reading slow directories
740027
740027
Postpone installation of signal handlers until they're needed.
740027
That is right before the first escape sequence is printed.
740027
740027
* src/ls.c (signal_setup): A new function refactored from main()
740027
to set and restore signal handlers.
740027
(main): Move signal handler setup to put_indicator()
740027
so that the default signal handling is untouched as long as possible.
740027
Adjusted condition for restoring signal handlers to reflect the change.
740027
(put_indicator): Install signal handlers if called for the very first
740027
time.  It uses the same code that was in main() prior to this commit.
740027
* NEWS: Mention the improvement.
740027
740027
See https://bugzilla.redhat.com/1365933
740027
Fixes http://bugs.gnu.org/24232
740027
740027
Upstream-commit: 5445f7811ff945ea13aa2a0fd797eb4c0a0e4db0
740027
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
740027
---
740027
 src/ls.c | 161 ++++++++++++++++++++++++++++++++++++---------------------------
740027
 1 file changed, 93 insertions(+), 68 deletions(-)
740027
740027
diff --git a/src/ls.c b/src/ls.c
740027
index a89c87a..1300938 100644
740027
--- a/src/ls.c
740027
+++ b/src/ls.c
740027
@@ -1246,13 +1246,12 @@ process_signals (void)
740027
     }
740027
 }
740027
 
740027
-int
740027
-main (int argc, char **argv)
740027
-{
740027
-  int i;
740027
-  struct pending *thispend;
740027
-  int n_files;
740027
+/* Setup signal handlers if INIT is true,
740027
+   otherwise restore to the default.  */
740027
 
740027
+static void
740027
+signal_setup (bool init)
740027
+{
740027
   /* The signals that are trapped, and the number of such signals.  */
740027
   static int const sig[] =
740027
     {
740027
@@ -1280,8 +1279,77 @@ main (int argc, char **argv)
740027
   enum { nsigs = ARRAY_CARDINALITY (sig) };
740027
 
740027
 #if ! SA_NOCLDSTOP
740027
-  bool caught_sig[nsigs];
740027
+  static bool caught_sig[nsigs];
740027
+#endif
740027
+
740027
+  int j;
740027
+
740027
+  if (init)
740027
+    {
740027
+#if SA_NOCLDSTOP
740027
+      struct sigaction act;
740027
+
740027
+      sigemptyset (&caught_signals);
740027
+      for (j = 0; j < nsigs; j++)
740027
+        {
740027
+          sigaction (sig[j], NULL, &act;;
740027
+          if (act.sa_handler != SIG_IGN)
740027
+            sigaddset (&caught_signals, sig[j]);
740027
+        }
740027
+
740027
+      act.sa_mask = caught_signals;
740027
+      act.sa_flags = SA_RESTART;
740027
+
740027
+      for (j = 0; j < nsigs; j++)
740027
+        if (sigismember (&caught_signals, sig[j]))
740027
+          {
740027
+            act.sa_handler = sig[j] == SIGTSTP ? stophandler : sighandler;
740027
+            sigaction (sig[j], &act, NULL);
740027
+          }
740027
+#else
740027
+      for (j = 0; j < nsigs; j++)
740027
+        {
740027
+          caught_sig[j] = (signal (sig[j], SIG_IGN) != SIG_IGN);
740027
+          if (caught_sig[j])
740027
+            {
740027
+              signal (sig[j], sig[j] == SIGTSTP ? stophandler : sighandler);
740027
+              siginterrupt (sig[j], 0);
740027
+            }
740027
+        }
740027
 #endif
740027
+    }
740027
+  else /* restore.  */
740027
+    {
740027
+#if SA_NOCLDSTOP
740027
+      for (j = 0; j < nsigs; j++)
740027
+        if (sigismember (&caught_signals, sig[j]))
740027
+          signal (sig[j], SIG_DFL);
740027
+#else
740027
+      for (j = 0; j < nsigs; j++)
740027
+        if (caught_sig[j])
740027
+          signal (sig[j], SIG_DFL);
740027
+#endif
740027
+    }
740027
+}
740027
+
740027
+static inline void
740027
+signal_init (void)
740027
+{
740027
+  signal_setup (true);
740027
+}
740027
+
740027
+static inline void
740027
+signal_restore (void)
740027
+{
740027
+  signal_setup (false);
740027
+}
740027
+
740027
+int
740027
+main (int argc, char **argv)
740027
+{
740027
+  int i;
740027
+  struct pending *thispend;
740027
+  int n_files;
740027
 
740027
   initialize_main (&argc, &argv);
740027
   set_program_name (argv[0]);
740027
@@ -1317,46 +1385,6 @@ main (int argc, char **argv)
740027
           || (is_colored (C_MISSING) && (format == long_format
740027
               || format == security_format)))
740027
         check_symlink_color = true;
740027
-
740027
-      /* If the standard output is a controlling terminal, watch out
740027
-         for signals, so that the colors can be restored to the
740027
-         default state if "ls" is suspended or interrupted.  */
740027
-
740027
-      if (0 <= tcgetpgrp (STDOUT_FILENO))
740027
-        {
740027
-          int j;
740027
-#if SA_NOCLDSTOP
740027
-          struct sigaction act;
740027
-
740027
-          sigemptyset (&caught_signals);
740027
-          for (j = 0; j < nsigs; j++)
740027
-            {
740027
-              sigaction (sig[j], NULL, &act;;
740027
-              if (act.sa_handler != SIG_IGN)
740027
-                sigaddset (&caught_signals, sig[j]);
740027
-            }
740027
-
740027
-          act.sa_mask = caught_signals;
740027
-          act.sa_flags = SA_RESTART;
740027
-
740027
-          for (j = 0; j < nsigs; j++)
740027
-            if (sigismember (&caught_signals, sig[j]))
740027
-              {
740027
-                act.sa_handler = sig[j] == SIGTSTP ? stophandler : sighandler;
740027
-                sigaction (sig[j], &act, NULL);
740027
-              }
740027
-#else
740027
-          for (j = 0; j < nsigs; j++)
740027
-            {
740027
-              caught_sig[j] = (signal (sig[j], SIG_IGN) != SIG_IGN);
740027
-              if (caught_sig[j])
740027
-                {
740027
-                  signal (sig[j], sig[j] == SIGTSTP ? stophandler : sighandler);
740027
-                  siginterrupt (sig[j], 0);
740027
-                }
740027
-            }
740027
-#endif
740027
-        }
740027
     }
740027
 
740027
   if (dereference == DEREF_UNDEFINED)
740027
@@ -1467,32 +1495,21 @@ main (int argc, char **argv)
740027
       print_dir_name = true;
740027
     }
740027
 
740027
-  if (print_with_color)
740027
+  if (print_with_color && used_color)
740027
     {
740027
       int j;
740027
 
740027
-      if (used_color)
740027
-        {
740027
-          /* Skip the restore when it would be a no-op, i.e.,
740027
-             when left is "\033[" and right is "m".  */
740027
-          if (!(color_indicator[C_LEFT].len == 2
740027
-                && memcmp (color_indicator[C_LEFT].string, "\033[", 2) == 0
740027
-                && color_indicator[C_RIGHT].len == 1
740027
-                && color_indicator[C_RIGHT].string[0] == 'm'))
740027
-            restore_default_color ();
740027
-        }
740027
+      /* Skip the restore when it would be a no-op, i.e.,
740027
+         when left is "\033[" and right is "m".  */
740027
+      if (!(color_indicator[C_LEFT].len == 2
740027
+            && memcmp (color_indicator[C_LEFT].string, "\033[", 2) == 0
740027
+            && color_indicator[C_RIGHT].len == 1
740027
+            && color_indicator[C_RIGHT].string[0] == 'm'))
740027
+        restore_default_color ();
740027
+
740027
       fflush (stdout);
740027
 
740027
-      /* Restore the default signal handling.  */
740027
-#if SA_NOCLDSTOP
740027
-      for (j = 0; j < nsigs; j++)
740027
-        if (sigismember (&caught_signals, sig[j]))
740027
-          signal (sig[j], SIG_DFL);
740027
-#else
740027
-      for (j = 0; j < nsigs; j++)
740027
-        if (caught_sig[j])
740027
-          signal (sig[j], SIG_DFL);
740027
-#endif
740027
+      signal_restore ();
740027
 
740027
       /* Act on any signals that arrived before the default was restored.
740027
          This can process signals out of order, but there doesn't seem to
740027
@@ -4512,6 +4529,14 @@ put_indicator (const struct bin_str *ind)
740027
   if (! used_color)
740027
     {
740027
       used_color = true;
740027
+
740027
+      /* If the standard output is a controlling terminal, watch out
740027
+         for signals, so that the colors can be restored to the
740027
+         default state if "ls" is suspended or interrupted.  */
740027
+
740027
+      if (0 <= tcgetpgrp (STDOUT_FILENO))
740027
+        signal_init ();
740027
+
740027
       prep_non_filename_text ();
740027
     }
740027
 
740027
-- 
740027
2.13.5
740027