xref: /llvm-project/llvm/unittests/Support/SignalsTest.cpp (revision a48633b5bbbbe9e20a7199eaed651ff6024d7967)
1 //========- unittests/Support/SignalsTest.cpp - Signal handling test =========//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #if !defined(_WIN32)
10 #include <unistd.h>
11 #include <sysexits.h>
12 #include <signal.h>
13 #endif // !defined(_WIN32)
14 
15 #include "llvm/Support/Signals.h"
16 
17 #include "gtest/gtest.h"
18 
19 using namespace llvm;
20 
21 #if !defined(_WIN32)
22 TEST(SignalTest, IgnoreMultipleSIGPIPEs) {
23   // Ignore SIGPIPE.
24   signal(SIGPIPE, SIG_IGN);
25 
26   // Disable exit-on-SIGPIPE.
27   sys::SetPipeSignalFunction(nullptr);
28 
29   // Create unidirectional read/write pipes.
30   int fds[2];
31   int err = pipe(fds);
32   if (err != 0)
33     return; // If we can't make pipes, this isn't testing anything.
34 
35   // Close the read pipe.
36   close(fds[0]);
37 
38   // Attempt to write to the write pipe. Currently we're asserting that the
39   // write fails, which isn't great.
40   //
41   // What we really want is a death test that checks that this block exits
42   // with a special exit "success" code, as opposed to unexpectedly exiting due
43   // to a kill-by-SIGNAL or due to the default SIGPIPE handler.
44   //
45   // Unfortunately llvm's unit tests aren't set up to support death tests well.
46   // For one, death tests are flaky in a multithreaded context. And sigactions
47   // inherited from llvm-lit interfere with what's being tested.
48   const void *buf = (const void *)&fds;
49   err = write(fds[1], buf, 1);
50   ASSERT_EQ(err, -1);
51   err = write(fds[1], buf, 1);
52   ASSERT_EQ(err, -1);
53 }
54 #endif // !defined(_WIN32)
55