signal.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * uTest, libtest
  4. *
  5. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * * The names of its contributors may not be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. #include <libtest/common.h>
  37. #include <csignal>
  38. #include <libtest/signal.h>
  39. using namespace libtest;
  40. #define MAGIC_MEMORY 123569
  41. bool SignalThread::is_shutdown()
  42. {
  43. bool ret;
  44. pthread_mutex_lock(&shutdown_mutex);
  45. ret= bool(__shutdown != SHUTDOWN_RUNNING);
  46. pthread_mutex_unlock(&shutdown_mutex);
  47. return ret;
  48. }
  49. void SignalThread::set_shutdown(shutdown_t arg)
  50. {
  51. pthread_mutex_lock(&shutdown_mutex);
  52. __shutdown= arg;
  53. pthread_mutex_unlock(&shutdown_mutex);
  54. if (arg == SHUTDOWN_GRACEFUL)
  55. {
  56. pthread_kill(thread, SIGUSR2);
  57. void *retval;
  58. pthread_join(thread, &retval);
  59. }
  60. }
  61. shutdown_t SignalThread::get_shutdown()
  62. {
  63. shutdown_t local;
  64. pthread_mutex_lock(&shutdown_mutex);
  65. local= __shutdown;
  66. pthread_mutex_unlock(&shutdown_mutex);
  67. return local;
  68. }
  69. void SignalThread::post()
  70. {
  71. sem_post(&lock);
  72. }
  73. void SignalThread::test()
  74. {
  75. assert(magic_memory == MAGIC_MEMORY);
  76. if (not getenv("LIBTEST_IN_GDB"))
  77. {
  78. assert(sigismember(&set, SIGABRT));
  79. assert(sigismember(&set, SIGQUIT));
  80. assert(sigismember(&set, SIGINT));
  81. }
  82. assert(sigismember(&set, SIGUSR2));
  83. }
  84. extern "C" {
  85. static void *sig_thread(void *arg)
  86. {
  87. SignalThread *context= (SignalThread*)arg;
  88. context->test();
  89. context->post();
  90. while (context->get_shutdown() == SHUTDOWN_RUNNING)
  91. {
  92. int sig;
  93. if (context->wait(sig) == -1)
  94. {
  95. Error << "sigwait() returned errno:" << strerror(errno);
  96. continue;
  97. }
  98. switch (sig)
  99. {
  100. case SIGABRT:
  101. case SIGUSR2:
  102. case SIGINT:
  103. case SIGQUIT:
  104. if (context->is_shutdown() == false)
  105. {
  106. Error << "Signal handling thread got signal " << strsignal(sig);
  107. context->set_shutdown(SHUTDOWN_FORCED);
  108. }
  109. break;
  110. default:
  111. Error << "Signal handling thread got unexpected signal " << strsignal(sig);
  112. break;
  113. }
  114. }
  115. return NULL;
  116. }
  117. }
  118. SignalThread::SignalThread() :
  119. magic_memory(MAGIC_MEMORY)
  120. {
  121. pthread_mutex_init(&shutdown_mutex, NULL);
  122. sigemptyset(&set);
  123. if (not getenv("LIBTEST_IN_GDB"))
  124. {
  125. sigaddset(&set, SIGABRT);
  126. sigaddset(&set, SIGQUIT);
  127. sigaddset(&set, SIGINT);
  128. }
  129. sigaddset(&set, SIGUSR2);
  130. sem_init(&lock, 0, 0);
  131. }
  132. bool SignalThread::setup()
  133. {
  134. set_shutdown(SHUTDOWN_RUNNING);
  135. sigset_t old_set;
  136. sigemptyset(&old_set);
  137. pthread_sigmask(SIG_BLOCK, NULL, &old_set);
  138. if (sigismember(&old_set, SIGQUIT))
  139. {
  140. Error << strsignal(SIGQUIT) << " has been previously set.";
  141. }
  142. if (sigismember(&old_set, SIGINT))
  143. {
  144. Error << strsignal(SIGINT) << " has been previously set.";
  145. }
  146. if (sigismember(&old_set, SIGUSR2))
  147. {
  148. Error << strsignal(SIGUSR2) << " has been previously set.";
  149. }
  150. int error;
  151. if ((error= pthread_sigmask(SIG_BLOCK, &set, NULL)) != 0)
  152. {
  153. Error << "pthread_sigmask() died during pthread_sigmask(" << strerror(error) << ")";
  154. return false;
  155. }
  156. if ((error= pthread_create(&thread, NULL, &sig_thread, this)) != 0)
  157. {
  158. Error << "pthread_create() died during pthread_create(" << strerror(error) << ")";
  159. return false;
  160. }
  161. sem_wait(&lock);
  162. return true;
  163. }