[Support] exit with custom return code for SIGPIPE
authorNick Desaulniers <ndesaulniers@google.com>
Fri, 12 Oct 2018 17:22:07 +0000 (17:22 +0000)
committerNick Desaulniers <ndesaulniers@google.com>
Fri, 12 Oct 2018 17:22:07 +0000 (17:22 +0000)
Summary:
We tell the user to file a bug report on LLVM right now, and
SIGPIPE isn't LLVM's fault so our error message is wrong.

Allows frontends to detect SIGPIPE from writing to closed readers.
This can be seen commonly from piping into head, tee, or split.

Fixes PR25349, rdar://problem/14285346, b/77310947

Reviewers: jfb

Reviewed By: jfb

Subscribers: majnemer, kristina, llvm-commits, thakis, srhines

Differential Revision: https://reviews.llvm.org/D53000

llvm-svn: 344372

llvm/lib/Support/Unix/Signals.inc

index de26695..ad88d5e 100644 (file)
@@ -47,6 +47,7 @@
 #include "llvm/Support/raw_ostream.h"
 #include <algorithm>
 #include <string>
+#include <sysexits.h>
 #ifdef HAVE_BACKTRACE
 # include BACKTRACE_HEADER         // For backtrace().
 #endif
@@ -334,6 +335,10 @@ static RETSIGTYPE SignalHandler(int Sig) {
       if (auto OldInterruptFunction = InterruptFunction.exchange(nullptr))
         return OldInterruptFunction();
 
+      // Send a special return code that drivers can check for, from sysexits.h.
+      if (Sig == SIGPIPE)
+        exit(EX_IOERR);
+
       raise(Sig);   // Execute the default handler.
       return;
    }