signal.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Data Differential YATL (i.e. libtest) library
  4. *
  5. * Copyright (C) 2012 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/yatlcon.h"
  37. #include <libtest/common.h>
  38. #include <fcntl.h>
  39. #include <semaphore.h>
  40. #include <sys/stat.h>
  41. #include <unistd.h>
  42. #include <csignal>
  43. #include <libtest/signal.h>
  44. using namespace libtest;
  45. #define MAGIC_MEMORY 123569
  46. bool SignalThread::is_shutdown()
  47. {
  48. bool ret;
  49. pthread_mutex_lock(&shutdown_mutex);
  50. ret= bool(__shutdown != SHUTDOWN_RUNNING);
  51. pthread_mutex_unlock(&shutdown_mutex);
  52. return ret;
  53. }
  54. void SignalThread::set_shutdown(shutdown_t arg)
  55. {
  56. pthread_mutex_lock(&shutdown_mutex);
  57. __shutdown= arg;
  58. pthread_mutex_unlock(&shutdown_mutex);
  59. if (arg == SHUTDOWN_GRACEFUL)
  60. {
  61. if (pthread_kill(thread, SIGUSR2) == 0)
  62. {
  63. void *retval;
  64. pthread_join(thread, &retval);
  65. }
  66. }
  67. }
  68. shutdown_t SignalThread::get_shutdown()
  69. {
  70. shutdown_t local;
  71. pthread_mutex_lock(&shutdown_mutex);
  72. local= __shutdown;
  73. pthread_mutex_unlock(&shutdown_mutex);
  74. return local;
  75. }
  76. void SignalThread::post()
  77. {
  78. sem_post(lock);
  79. }
  80. void SignalThread::test()
  81. {
  82. fatal_assert(magic_memory == MAGIC_MEMORY);
  83. if (!getenv("LIBTEST_IN_GDB"))
  84. {
  85. assert(sigismember(&set, SIGALRM));
  86. assert(sigismember(&set, SIGABRT));
  87. assert(sigismember(&set, SIGQUIT));
  88. assert(sigismember(&set, SIGINT));
  89. assert(sigismember(&set, SIGVTALRM));
  90. }
  91. assert(sigismember(&set, SIGUSR2));
  92. }
  93. bool SignalThread::unblock()
  94. {
  95. int error;
  96. if ((error= pthread_sigmask(SIG_UNBLOCK, &set, NULL)) != 0)
  97. {
  98. Error << "While trying to reset signal mask to original set, pthread_sigmask() died during pthread_sigmask(" << strerror(error) << ")";
  99. return false;
  100. }
  101. return true;
  102. }
  103. std::string SignalThread::random_lock_name(std::string::size_type len = 10)
  104. {
  105. static auto& chrs = "0123456789"
  106. "abcdefghijklmnopqrstuvwxyz"
  107. "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  108. thread_local static std::mt19937 rg{std::random_device{}()};
  109. thread_local static std::uniform_int_distribution<std::string::size_type> pick(0, sizeof(chrs) - 2);
  110. // convenient to named semaphore definition
  111. // http://man7.org/linux/man-pages/man7/sem_overview.7.html
  112. std::string s{"/"};
  113. s.reserve(len--);
  114. while (len--)
  115. {
  116. s += chrs[pick(rg)];
  117. }
  118. return s;
  119. }
  120. SignalThread::~SignalThread()
  121. {
  122. if (is_shutdown() == false)
  123. {
  124. set_shutdown(SHUTDOWN_GRACEFUL);
  125. }
  126. #if 0
  127. if (pthread_equal(thread, pthread_self()) != 0 and (pthread_kill(thread, 0) == ESRCH) == true)
  128. {
  129. void *retval;
  130. pthread_join(thread, &retval);
  131. }
  132. #endif
  133. sem_close(lock);
  134. unblock();
  135. }
  136. extern "C" {
  137. static void *sig_thread(void *arg)
  138. {
  139. SignalThread *context= (SignalThread*)arg;
  140. context->test();
  141. context->post();
  142. while (context->get_shutdown() == SHUTDOWN_RUNNING)
  143. {
  144. int sig;
  145. if (context->wait(sig) == -1)
  146. {
  147. Error << "sigwait() returned errno:" << strerror(errno);
  148. continue;
  149. }
  150. switch (sig)
  151. {
  152. case SIGALRM:
  153. case SIGVTALRM:
  154. Error << strsignal(sig);
  155. if (gdb_is_caller())
  156. {
  157. abort();
  158. }
  159. exit(EXIT_FAILURE);
  160. case SIGABRT:
  161. case SIGUSR2:
  162. case SIGINT:
  163. case SIGQUIT:
  164. if (context->is_shutdown() == false)
  165. {
  166. context->set_shutdown(SHUTDOWN_FORCED);
  167. }
  168. break;
  169. case SIGPIPE:
  170. {
  171. Error << "Ignoring SIGPIPE";
  172. }
  173. break;
  174. case 0:
  175. Error << "Inside of gdb";
  176. break;
  177. default:
  178. Error << "Signal handling thread got unexpected signal " << strsignal(sig);
  179. break;
  180. }
  181. }
  182. return NULL;
  183. }
  184. }
  185. SignalThread::SignalThread() :
  186. magic_memory(MAGIC_MEMORY),
  187. __shutdown{},
  188. thread(pthread_self())
  189. {
  190. pthread_mutex_init(&shutdown_mutex, NULL);
  191. sigemptyset(&set);
  192. if (!getenv("LIBTEST_IN_GDB"))
  193. {
  194. sigaddset(&set, SIGALRM);
  195. sigaddset(&set, SIGABRT);
  196. sigaddset(&set, SIGQUIT);
  197. sigaddset(&set, SIGINT);
  198. sigaddset(&set, SIGVTALRM);
  199. }
  200. sigaddset(&set, SIGPIPE);
  201. sigaddset(&set, SIGUSR2);
  202. sigemptyset(&original_set);
  203. pthread_sigmask(SIG_BLOCK, NULL, &original_set);
  204. }
  205. bool SignalThread::setup()
  206. {
  207. set_shutdown(SHUTDOWN_RUNNING);
  208. const char * lock_name = random_lock_name().c_str();
  209. lock = sem_open(lock_name, O_CREAT|O_EXCL, S_IRUSR|S_IWUSR, 0);
  210. if (lock == SEM_FAILED)
  211. {
  212. Error << errno << ": " << strerror(errno)
  213. << " when opening lock '" << lock_name << "'.";
  214. }
  215. if (sigismember(&original_set, SIGQUIT))
  216. {
  217. Error << strsignal(SIGQUIT) << " has been previously set.";
  218. }
  219. if (sigismember(&original_set, SIGINT))
  220. {
  221. Error << strsignal(SIGINT) << " has been previously set.";
  222. }
  223. if (sigismember(&original_set, SIGVTALRM))
  224. {
  225. Error << strsignal(SIGVTALRM) << " has been previously set.";
  226. }
  227. if (sigismember(&original_set, SIGUSR2))
  228. {
  229. Error << strsignal(SIGUSR2) << " has been previously set.";
  230. }
  231. int error;
  232. if ((error= pthread_sigmask(SIG_BLOCK, &set, NULL)) != 0)
  233. {
  234. Error << "pthread_sigmask() died during pthread_sigmask(" << strerror(error) << ")";
  235. return false;
  236. }
  237. if ((error= pthread_create(&thread, NULL, &sig_thread, this)) != 0)
  238. {
  239. Error << "pthread_create() died during pthread_create(" << strerror(error) << ")";
  240. return false;
  241. }
  242. sem_wait(lock);
  243. return true;
  244. }