]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
timeout: handle signals more transparently
authorPádraig Brady <P@draigBrady.com>
Fri, 8 Jul 2011 13:49:05 +0000 (14:49 +0100)
committerPádraig Brady <P@draigBrady.com>
Fri, 8 Jul 2011 14:11:27 +0000 (15:11 +0100)
* m4/jm-macros.m4: Define HAVE_SETRLIMIT.
* src/timeout.c: If the child exited with a signal,
raise that signal to the timeout process itself,
so that callers may also see the signal status.
Use setrlimit to disable core dumps for the timeout
process, which would be generated by some signals.

m4/jm-macros.m4
src/timeout.c

index ec553105fb29e65af4912e6eaa4accbe3350d8b1..9bb6fa425671e1683613a7a9df544060e24dafd8 100644 (file)
@@ -65,6 +65,8 @@ AC_DEFUN([coreutils_MACROS],
 
   # Used by sort.c.
   AC_CHECK_FUNCS_ONCE([nl_langinfo])
+  # Used by timeout.c
+  AC_CHECK_FUNCS_ONCE([setrlimit])
 
   # Used by tail.c.
   AC_CHECK_FUNCS([inotify_init],
index ab54ed675d48a72d1fd84af84697ecef7cc9a45d..ef660a717ada25a057671403ddc39cb7885ccfaa 100644 (file)
 #include "error.h"
 #include "quote.h"
 
+#if HAVE_SETRLIMIT
+/* FreeBSD 5.0 at least needs <sys/types.h> and <sys/time.h> included
+   before <sys/resource.h>.  Currently "system.h" includes <sys/time.h>.  */
+# include <sys/resource.h>
+#endif
+
 #define PROGRAM_NAME "timeout"
 
 #define AUTHORS proper_name_utf8 ("Padraig Brady", "P\303\241draig Brady")
@@ -370,7 +376,23 @@ main (int argc, char **argv)
           if (WIFEXITED (status))
             status = WEXITSTATUS (status);
           else if (WIFSIGNALED (status))
-            status = WTERMSIG (status) + 128; /* what sh does at least.  */
+            {
+              int sig = WTERMSIG (status);
+#if HAVE_SETRLIMIT && defined RLIMIT_CORE
+              if (!timed_out)
+                {
+                  /* exit with the signal flag set, but avoid core files.  */
+                  if (setrlimit (RLIMIT_CORE, &(struct rlimit) {0,0}) == 0)
+                    {
+                      signal (sig, SIG_DFL);
+                      raise (sig);
+                    }
+                  else
+                    error (0, errno, _("warning: disabling core dumps failed"));
+                }
+#endif
+              status = sig + 128; /* what sh returns for signaled processes.  */
+            }
           else
             {
               /* shouldn't happen.  */