SignalsTest.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. #if !defined(_WIN32)
  9. #include <unistd.h>
  10. #include <sysexits.h>
  11. #include <signal.h>
  12. #endif // !defined(_WIN32)
  13. #include "llvm/Support/Signals.h"
  14. #include "gtest/gtest.h"
  15. using namespace llvm;
  16. #if !defined(_WIN32)
  17. TEST(SignalTest, IgnoreMultipleSIGPIPEs) {
  18. // Ignore SIGPIPE.
  19. signal(SIGPIPE, SIG_IGN);
  20. // Disable exit-on-SIGPIPE.
  21. sys::SetPipeSignalFunction(nullptr);
  22. // Create unidirectional read/write pipes.
  23. int fds[2];
  24. int err = pipe(fds);
  25. if (err != 0)
  26. return; // If we can't make pipes, this isn't testing anything.
  27. // Close the read pipe.
  28. close(fds[0]);
  29. // Attempt to write to the write pipe. Currently we're asserting that the
  30. // write fails, which isn't great.
  31. //
  32. // What we really want is a death test that checks that this block exits
  33. // with a special exit "success" code, as opposed to unexpectedly exiting due
  34. // to a kill-by-SIGNAL or due to the default SIGPIPE handler.
  35. //
  36. // Unfortunately llvm's unit tests aren't set up to support death tests well.
  37. // For one, death tests are flaky in a multithreaded context. And sigactions
  38. // inherited from llvm-lit interfere with what's being tested.
  39. const void *buf = (const void *)&fds;
  40. err = write(fds[1], buf, 1);
  41. ASSERT_EQ(err, -1);
  42. err = write(fds[1], buf, 1);
  43. ASSERT_EQ(err, -1);
  44. }
  45. #endif // !defined(_WIN32)