X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libtest%2Fsignal.cc;h=70012f1d94ad672e8a01d036e7e491ccd4662b66;hb=ae86283e79c226f2a24d5c74e3ffc8f38f0a66a8;hp=d62221eb51b78f2c4bd016e850f139223e22044e;hpb=e347c3ba4e8368833dfb4a6a4515c750adf1e38b;p=awesomized%2Flibmemcached diff --git a/libtest/signal.cc b/libtest/signal.cc index d62221eb..70012f1d 100644 --- a/libtest/signal.cc +++ b/libtest/signal.cc @@ -1,8 +1,8 @@ /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * uTest, libtest * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * Data Differential YATL (i.e. libtest) library + * + * Copyright (C) 2012 Data Differential, http://datadifferential.com/ * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are @@ -34,51 +34,18 @@ * */ +#include "libtest/yatlcon.h" #include -#include -#include #include #include using namespace libtest; -struct context_st { - sigset_t set; - sem_t lock; - - context_st() - { - sigemptyset(&set); - sigaddset(&set, SIGQUIT); - sigaddset(&set, SIGINT); - - sem_init(&lock, 0, 0); - } - - void test() - { - assert(sigismember(&set, SIGQUIT)); - assert(sigismember(&set, SIGINT)); - } - - int wait(int& sig) - { - return sigwait(&set, &sig); - } - - ~context_st() - { - sem_destroy(&lock); - } -}; - -static volatile shutdown_t __shutdown; -static pthread_mutex_t shutdown_mutex; -static pthread_t thread; +#define MAGIC_MEMORY 123569 -bool is_shutdown() +bool SignalThread::is_shutdown() { bool ret; pthread_mutex_lock(&shutdown_mutex); @@ -88,7 +55,7 @@ bool is_shutdown() return ret; } -void set_shutdown(shutdown_t arg) +void SignalThread::set_shutdown(shutdown_t arg) { pthread_mutex_lock(&shutdown_mutex); __shutdown= arg; @@ -96,14 +63,15 @@ void set_shutdown(shutdown_t arg) if (arg == SHUTDOWN_GRACEFUL) { - pthread_kill(thread, SIGQUIT); - - void *retval; - pthread_join(thread, &retval); + if (pthread_kill(thread, SIGUSR2) == 0) + { + void *retval; + pthread_join(thread, &retval); + } } } -shutdown_t get_shutdown() +shutdown_t SignalThread::get_shutdown() { shutdown_t local; pthread_mutex_lock(&shutdown_mutex); @@ -113,17 +81,66 @@ shutdown_t get_shutdown() return local; } +void SignalThread::post() +{ + sem_post(&lock); +} + +void SignalThread::test() +{ + assert(magic_memory == MAGIC_MEMORY); + if (bool(getenv("LIBTEST_IN_GDB")) == false) + { + assert(sigismember(&set, SIGALRM)); + assert(sigismember(&set, SIGABRT)); + assert(sigismember(&set, SIGQUIT)); + assert(sigismember(&set, SIGINT)); + assert(sigismember(&set, SIGVTALRM)); + } + assert(sigismember(&set, SIGUSR2)); +} + +bool SignalThread::unblock() +{ + int error; + if ((error= pthread_sigmask(SIG_UNBLOCK, &set, NULL)) != 0) + { + Error << "While trying to reset signal mask to original set, pthread_sigmask() died during pthread_sigmask(" << strerror(error) << ")"; + return false; + } + + return true; +} + +SignalThread::~SignalThread() +{ + if (is_shutdown() == false) + { + set_shutdown(SHUTDOWN_GRACEFUL); + } + +#if 0 + if (pthread_equal(thread, pthread_self()) != 0 and (pthread_kill(thread, 0) == ESRCH) == true) + { + void *retval; + pthread_join(thread, &retval); + } +#endif + sem_destroy(&lock); + + unblock(); +} + extern "C" { static void *sig_thread(void *arg) { - context_st *context= (context_st*)arg; - assert(context); + SignalThread *context= (SignalThread*)arg; context->test(); - sem_post(&context->lock); + context->post(); - while (get_shutdown() == SHUTDOWN_RUNNING) + while (context->get_shutdown() == SHUTDOWN_RUNNING) { int sig; @@ -135,14 +152,33 @@ static void *sig_thread(void *arg) switch (sig) { + case SIGALRM: + case SIGVTALRM: + Error << strsignal(sig); + if (gdb_is_caller()) + { + abort(); + } + exit(EXIT_FAILURE); + + case SIGABRT: + case SIGUSR2: case SIGINT: case SIGQUIT: - if (is_shutdown() == false) + if (context->is_shutdown() == false) { - Error << "Signal handling thread got signal " << strsignal(sig); - set_shutdown(SHUTDOWN_FORCED); + context->set_shutdown(SHUTDOWN_FORCED); } break; + case SIGPIPE: + { + Error << "Ignoring SIGPIPE"; + } + break; + + case 0: + Error << "Inside of gdb"; + break; default: Error << "Signal handling thread got unexpected signal " << strsignal(sig); @@ -150,47 +186,74 @@ static void *sig_thread(void *arg) } } - delete context; - return NULL; } } -void setup_signals() +SignalThread::SignalThread() : + magic_memory(MAGIC_MEMORY), + thread(pthread_self()) { pthread_mutex_init(&shutdown_mutex, NULL); - set_shutdown(SHUTDOWN_RUNNING); + sigemptyset(&set); + if (bool(getenv("LIBTEST_IN_GDB")) == false) + { + sigaddset(&set, SIGALRM); + sigaddset(&set, SIGABRT); + sigaddset(&set, SIGQUIT); + sigaddset(&set, SIGINT); + sigaddset(&set, SIGVTALRM); + } + sigaddset(&set, SIGPIPE); + + sigaddset(&set, SIGUSR2); - context_st *context= new context_st; + sem_init(&lock, 0, 0); - assert(context); + sigemptyset(&original_set); + pthread_sigmask(SIG_BLOCK, NULL, &original_set); +} - sigset_t old_set; - sigemptyset(&old_set); - pthread_sigmask(SIG_BLOCK, NULL, &old_set); - if (sigismember(&old_set, SIGQUIT)) +bool SignalThread::setup() +{ + set_shutdown(SHUTDOWN_RUNNING); + + if (sigismember(&original_set, SIGQUIT)) { Error << strsignal(SIGQUIT) << " has been previously set."; } - if (sigismember(&old_set, SIGINT)) + + if (sigismember(&original_set, SIGINT)) { Error << strsignal(SIGINT) << " has been previously set."; } + if (sigismember(&original_set, SIGVTALRM)) + { + Error << strsignal(SIGVTALRM) << " has been previously set."; + } + + if (sigismember(&original_set, SIGUSR2)) + { + Error << strsignal(SIGUSR2) << " has been previously set."; + } + int error; - if ((error= pthread_sigmask(SIG_BLOCK, &context->set, NULL)) != 0) + if ((error= pthread_sigmask(SIG_BLOCK, &set, NULL)) != 0) { Error << "pthread_sigmask() died during pthread_sigmask(" << strerror(error) << ")"; - exit(EXIT_FAILURE); + return false; } - if ((error= pthread_create(&thread, NULL, &sig_thread, (void *) &context->set)) != 0) + if ((error= pthread_create(&thread, NULL, &sig_thread, this)) != 0) { Error << "pthread_create() died during pthread_create(" << strerror(error) << ")"; - exit(EXIT_FAILURE); + return false; } - sem_wait(&context->lock); + sem_wait(&lock); + + return true; }