X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libtest%2Fsignal.cc;h=04d06b3642752a5db31bd38f52a5519c60bc2140;hb=1e8bc3cb173d8b4e206363de7fdabeddb32d0f53;hp=c5ec42ba2cb037bebaee65d9ef36ce4e1c6c24e3;hpb=28602fd2f5f1c758b50cd82d4545da8e6c55f5c7;p=m6w6%2Flibmemcached diff --git a/libtest/signal.cc b/libtest/signal.cc index c5ec42ba..04d06b36 100644 --- a/libtest/signal.cc +++ b/libtest/signal.cc @@ -1,86 +1,37 @@ /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: * - * uTest, libtest + * libtest * * Copyright (C) 2011 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 - * met: + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ + +#include #include -#include -#include -#include +#include #include using namespace libtest; -struct context_st { - sigset_t set; - sem_t lock; - - context_st() - { - sigemptyset(&set); - sigaddset(&set, SIGABRT); - sigaddset(&set, SIGINT); - sigaddset(&set, SIGUSR2); +#define MAGIC_MEMORY 123569 - sem_init(&lock, 0, 0); - } - - void test() - { - assert(sigismember(&set, SIGABRT)); - assert(sigismember(&set, SIGINT)); - assert(sigismember(&set, SIGUSR2)); - } - - 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; - -bool is_shutdown() +bool SignalThread::is_shutdown() { bool ret; pthread_mutex_lock(&shutdown_mutex); @@ -90,7 +41,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; @@ -98,14 +49,15 @@ void set_shutdown(shutdown_t arg) if (arg == SHUTDOWN_GRACEFUL) { - pthread_kill(thread, SIGUSR2); - - 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); @@ -115,17 +67,56 @@ 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, SIGABRT)); + assert(sigismember(&set, SIGQUIT)); + assert(sigismember(&set, SIGINT)); + } + assert(sigismember(&set, SIGUSR2)); +} + +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); + + 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) << ")"; + } +} + 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; @@ -138,13 +129,22 @@ static void *sig_thread(void *arg) switch (sig) { case SIGABRT: + case SIGUSR2: case SIGINT: - Error << "Signal handling thread got signal " << strsignal(sig); - set_shutdown(SHUTDOWN_FORCED); + case SIGQUIT: + if (context->is_shutdown() == false) + { + context->set_shutdown(SHUTDOWN_FORCED); + } + break; + case SIGPIPE: + { + Error << "Ignoring SIGPIPE"; + } break; - // Signal thread is being told that a graceful shutdown is occuring - case SIGUSR2: + case 0: + Error << "Inside of gdb"; break; default: @@ -153,34 +153,65 @@ 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, SIGABRT); + sigaddset(&set, SIGQUIT); + sigaddset(&set, SIGINT); + } + sigaddset(&set, SIGPIPE); + + sigaddset(&set, SIGUSR2); + + sem_init(&lock, 0, 0); + + sigemptyset(&original_set); + pthread_sigmask(SIG_BLOCK, NULL, &original_set); +} + - context_st *context= new context_st; +bool SignalThread::setup() +{ + set_shutdown(SHUTDOWN_RUNNING); - assert(context); + if (sigismember(&original_set, SIGQUIT)) + { + Error << strsignal(SIGQUIT) << " has been previously set."; + } + if (sigismember(&original_set, SIGINT)) + { + Error << strsignal(SIGINT) << " 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; }