]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
csplit,ls,sort: handle a more complete set of signals
authorPádraig Brady <P@draigBrady.com>
Fri, 21 Nov 2025 14:22:22 +0000 (14:22 +0000)
committerPádraig Brady <P@draigBrady.com>
Sat, 22 Nov 2025 12:43:28 +0000 (12:43 +0000)
* src/term-sig.h: A new file defining a TERM_SIG array signals.
* src/local.mk: Reference the new file.
* src/csplit.c: Likewise.
* src/sort.c: Likewise.
* src/timeout.c: Likewise.
* src/ls.c: Likewise.  Also handle SIGTSTP separately.
* NEWS: Mention the improvement.

NEWS
src/csplit.c
src/local.mk
src/ls.c
src/sort.c
src/term-sig.h [new file with mode: 0644]
src/timeout.c

diff --git a/NEWS b/NEWS
index 86dd62c5dd712f73208fa834bf33cc1aa8f7470d..acb3a4e18704a03c957967e96731164a6d6e796c 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -32,6 +32,10 @@ GNU coreutils NEWS                                    -*- outline -*-
   timeout(1) in a shell backgrounded job, will not terminate upon receiving
   SIGINT or SIGQUIT, as these are ignored by default in shell background jobs.
 
+** Improvements
+
+  csplit, ls, and sort, now handle a more complete set of terminating signals.
+
 
 * Noteworthy changes in release 9.9 (2025-11-10) [stable]
 
index 3ee89aff3407a7927fe09df1e5954dd96bc5877b..57254ef4e54906a568bed68ce5f409c3e542371c 100644 (file)
@@ -33,6 +33,7 @@
 #include "quote.h"
 #include "safe-read.h"
 #include "stdio--.h"
+#include "term-sig.h"
 #include "xdectoint.h"
 #include "xstrtol.h"
 
@@ -1365,36 +1366,16 @@ main (int argc, char **argv)
   parse_patterns (argc, optind, argv);
 
   {
-    static int const sig[] =
-      {
-        /* The usual suspects.  */
-        SIGALRM, SIGHUP, SIGINT, SIGPIPE, SIGQUIT, SIGTERM,
-#ifdef SIGPOLL
-        SIGPOLL,
-#endif
-#ifdef SIGPROF
-        SIGPROF,
-#endif
-#ifdef SIGVTALRM
-        SIGVTALRM,
-#endif
-#ifdef SIGXCPU
-        SIGXCPU,
-#endif
-#ifdef SIGXFSZ
-        SIGXFSZ,
-#endif
-      };
-    enum { nsigs = countof (sig) };
+    enum { nsigs = countof (term_sig) };
 
     struct sigaction act;
 
     sigemptyset (&caught_signals);
     for (int i = 0; i < nsigs; i++)
       {
-        sigaction (sig[i], nullptr, &act);
+        sigaction (term_sig[i], nullptr, &act);
         if (act.sa_handler != SIG_IGN)
-          sigaddset (&caught_signals, sig[i]);
+          sigaddset (&caught_signals, term_sig[i]);
       }
 
     act.sa_handler = interrupt_handler;
@@ -1402,8 +1383,8 @@ main (int argc, char **argv)
     act.sa_flags = 0;
 
     for (int i = 0; i < nsigs; i++)
-      if (sigismember (&caught_signals, sig[i]))
-        sigaction (sig[i], &act, nullptr);
+      if (sigismember (&caught_signals, term_sig[i]))
+        sigaction (term_sig[i], &act, nullptr);
   }
 
   split_file ();
index 5d2c379c56edcf8d63130b66c6e8b415a1890b41..49bb9820715282ad1980f11d4aa6cf5214f11e44 100644 (file)
@@ -62,6 +62,7 @@ noinst_HEADERS =              \
   src/statx.h                  \
   src/system.h                 \
   src/temp-stream.h            \
+  src/term-sig.h               \
   src/uname.h                  \
   src/wc.h
 
index 22d9fe9db71ed0fe7be10367103def9943d18ac0..5f3f597554093b2f2e62df4706f89cded6aa9235 100644 (file)
--- a/src/ls.c
+++ b/src/ls.c
 #include "stat-size.h"
 #include "stat-time.h"
 #include "strftime.h"
+#include "term-sig.h"
 #include "xdectoint.h"
 #include "xstrtol.h"
 #include "xstrtol-error.h"
@@ -1598,33 +1599,16 @@ static void
 signal_setup (bool init)
 {
   /* The signals that are trapped, and the number of such signals.  */
-  static int const sig[] =
+  static int const stop_sig[] =
     {
       /* This one is handled specially.  */
       SIGTSTP,
-
-      /* The usual suspects.  */
-      SIGALRM, SIGHUP, SIGINT, SIGPIPE, SIGQUIT, SIGTERM,
-#ifdef SIGPOLL
-      SIGPOLL,
-#endif
-#ifdef SIGPROF
-      SIGPROF,
-#endif
-#ifdef SIGVTALRM
-      SIGVTALRM,
-#endif
-#ifdef SIGXCPU
-      SIGXCPU,
-#endif
-#ifdef SIGXFSZ
-      SIGXFSZ,
-#endif
     };
-  enum { nsigs = countof (sig) };
+
+  enum { nsigs = countof (term_sig), nstop = countof (stop_sig) };
 
 #if ! SA_NOCLDSTOP
-  static bool caught_sig[nsigs];
+  static bool caught_sig[nsigs + nstop];
 #endif
 
   if (init)
@@ -1633,29 +1617,34 @@ signal_setup (bool init)
       struct sigaction act;
 
       sigemptyset (&caught_signals);
-      for (int j = 0; j < nsigs; j++)
+      for (int j = 0; j < nsigs + nstop; j++)
         {
-          sigaction (sig[j], nullptr, &act);
+          int sig = j < nsigs ? term_sig[j]: stop_sig[j - nsigs];
+          sigaction (sig, nullptr, &act);
           if (act.sa_handler != SIG_IGN)
-            sigaddset (&caught_signals, sig[j]);
+            sigaddset (&caught_signals, sig);
         }
 
       act.sa_mask = caught_signals;
       act.sa_flags = SA_RESTART;
 
-      for (int j = 0; j < nsigs; j++)
-        if (sigismember (&caught_signals, sig[j]))
-          {
-            act.sa_handler = sig[j] == SIGTSTP ? stophandler : sighandler;
-            sigaction (sig[j], &act, nullptr);
-          }
+      for (int j = 0; j < nsigs + nstop; j++)
+        {
+          int sig = j < nsigs ? term_sig[j]: stop_sig[j - nsigs];
+          if (sigismember (&caught_signals, sig))
+            {
+              act.sa_handler = j < nsigs ? sighandler : stophandler;
+              sigaction (sig, &act, nullptr);
+            }
+        }
 #else
-      for (int j = 0; j < nsigs; j++)
+      for (int j = 0; j < nsigs + nstop; j++)
         {
-          caught_sig[j] = (signal (sig[j], SIG_IGN) != SIG_IGN);
+          int sig = j < nsigs ? term_sig[j]: stop_sig[j - nsigs];
+          caught_sig[j] = (signal (sig, SIG_IGN) != SIG_IGN);
           if (caught_sig[j])
             {
-              signal (sig[j], sig[j] == SIGTSTP ? stophandler : sighandler);
+              signal (sig, j < nsigs ? sighandler : stophandler);
               siginterrupt (sig[j], 0);
             }
         }
@@ -1663,15 +1652,16 @@ signal_setup (bool init)
     }
   else /* restore.  */
     {
+      for (int j = 0; j < nsigs + nstop; j++)
+        {
+          int sig = j < nsigs ? term_sig[j]: stop_sig[j - nsigs];
 #if SA_NOCLDSTOP
-      for (int j = 0; j < nsigs; j++)
-        if (sigismember (&caught_signals, sig[j]))
-          signal (sig[j], SIG_DFL);
+          if (sigismember (&caught_signals, sig))
 #else
-      for (int j = 0; j < nsigs; j++)
-        if (caught_sig[j])
-          signal (sig[j], SIG_DFL);
+          if (caught_sig[j])
 #endif
+            signal (sig, SIG_DFL);
+        }
     }
 }
 
index a74470bf8ec6b15e1f023d42e617c374e592bf45..dee50d98c0fd5688c542368d91f1b29d288c0afe 100644 (file)
@@ -51,6 +51,7 @@
 #include "readtokens0.h"
 #include "stdlib--.h"
 #include "strnumcmp.h"
+#include "term-sig.h"
 #include "xmemcoll.h"
 #include "xnanosleep.h"
 #include "xstrtol.h"
@@ -4447,27 +4448,7 @@ main (int argc, char **argv)
   inittables ();
 
   {
-    static int const sig[] =
-      {
-        /* The usual suspects.  */
-        SIGALRM, SIGHUP, SIGINT, SIGQUIT, SIGTERM,
-#ifdef SIGPOLL
-        SIGPOLL,
-#endif
-#ifdef SIGPROF
-        SIGPROF,
-#endif
-#ifdef SIGVTALRM
-        SIGVTALRM,
-#endif
-#ifdef SIGXCPU
-        SIGXCPU,
-#endif
-#ifdef SIGXFSZ
-        SIGXFSZ,
-#endif
-      };
-    enum { nsigs = countof (sig) };
+    enum { nsigs = countof (term_sig) };
 
 #if SA_NOCLDSTOP
     struct sigaction act;
@@ -4475,9 +4456,9 @@ main (int argc, char **argv)
     sigemptyset (&caught_signals);
     for (size_t i = 0; i < nsigs; i++)
       {
-        sigaction (sig[i], nullptr, &act);
+        sigaction (term_sig[i], nullptr, &act);
         if (act.sa_handler != SIG_IGN)
-          sigaddset (&caught_signals, sig[i]);
+          sigaddset (&caught_signals, term_sig[i]);
       }
 
     act.sa_handler = sighandler;
@@ -4485,14 +4466,14 @@ main (int argc, char **argv)
     act.sa_flags = 0;
 
     for (size_t i = 0; i < nsigs; i++)
-      if (sigismember (&caught_signals, sig[i]))
-        sigaction (sig[i], &act, nullptr);
+      if (sigismember (&caught_signals, term_sig[i]))
+        sigaction (term_sig[i], &act, nullptr);
 #else
     for (size_t i = 0; i < nsigs; i++)
-      if (signal (sig[i], SIG_IGN) != SIG_IGN)
+      if (signal (term_sig[i], SIG_IGN) != SIG_IGN)
         {
-          signal (sig[i], sighandler);
-          siginterrupt (sig[i], 1);
+          signal (term_sig[i], sighandler);
+          siginterrupt (term_sig[i], 1);
         }
 #endif
   }
diff --git a/src/term-sig.h b/src/term-sig.h
new file mode 100644 (file)
index 0000000..06ed0a7
--- /dev/null
@@ -0,0 +1,50 @@
+#ifndef TERM_SIG_H
+# define TERM_SIG_H
+
+# include <signal.h>
+
+static int const term_sig[] =
+  {
+    SIGALRM, /* our timeout.  */
+    SIGINT,  /* Ctrl-C at terminal for example.  */
+    SIGQUIT, /* Ctrl-\ at terminal for example.  */
+    SIGHUP,  /* terminal closed for example.  */
+    SIGTERM, /* if terminated, stop monitored proc.  */
+
+    SIGPIPE, SIGUSR1, SIGUSR2,
+
+    SIGILL, SIGTRAP, SIGABRT, SIGBUS, SIGFPE, SIGSEGV,
+
+# ifdef SIGXCPU
+    SIGXCPU,
+# endif
+# ifdef SIGXFSZ
+    SIGXFSZ,
+# endif
+# ifdef SIGSYS
+    SIGSYS,
+# endif
+# ifdef SIGVTALRM
+    SIGVTALRM,
+# endif
+# ifdef SIGPROF
+    SIGPROF,
+# endif
+# ifdef SIGPOLL
+    SIGPOLL,
+# endif
+# ifdef SIGPWR
+    SIGPWR,
+# endif
+# ifdef SIGSTKFLT
+    SIGSTKFLT,
+# endif
+# ifdef SIGEMT
+    SIGEMT,
+# endif
+# ifdef SIGBREAK
+    SIGBREAK,
+# endif
+  };
+
+#endif
index 2e770e9ff59a17a6226eb69f92acd27ca4f29a3c..68ddfd5d603ee33107a7a7bb0970c306798bbcdb 100644 (file)
@@ -59,6 +59,7 @@
 #include "dtimespec-bound.h"
 #include "sig2str.h"
 #include "operand2sig.h"
+#include "term-sig.h"
 #include "quote.h"
 
 #if HAVE_SETRLIMIT
@@ -199,42 +200,6 @@ chld (MAYBE_UNUSED int sig)
 {
 }
 
-static int const term_sig[] =
-  {
-    SIGALRM, /* our timeout.  */
-    SIGINT,  /* Ctrl-C at terminal for example.  */
-    SIGQUIT, /* Ctrl-\ at terminal for example.  */
-    SIGHUP,  /* terminal closed for example.  */
-    SIGTERM, /* if terminated, stop monitored proc.  */
-
-    SIGPIPE, SIGUSR1, SIGUSR2,
-    SIGILL, SIGTRAP, SIGABRT, SIGBUS, SIGFPE, SIGSEGV,
-#ifdef SIGXCPU
-    SIGXCPU,
-#endif
-#ifdef SIGXFSZ
-    SIGXFSZ,
-#endif
-#ifdef SIGSYS
-    SIGSYS,
-#endif
-#ifdef SIGVTALRM
-    SIGVTALRM,
-#endif
-#ifdef SIGPROF
-    SIGPROF,
-#endif
-#ifdef SIGPOLL
-    SIGPOLL,
-#endif
-#ifdef SIGPWR
-    SIGPWR,
-#endif
-#ifdef SIGSTKFLT
-    SIGSTKFLT,
-#endif
-  };
-
 static void
 cleanup (int sig)
 {