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