1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 * Data Differential YATL (i.e. libtest) library
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
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.
37 #include "libtest/yatlcon.h"
38 #include <libtest/common.h>
42 #include <libtest/signal.h>
44 using namespace libtest
;
46 #define MAGIC_MEMORY 123569
48 bool SignalThread::is_shutdown()
51 pthread_mutex_lock(&shutdown_mutex
);
52 ret
= bool(__shutdown
!= SHUTDOWN_RUNNING
);
53 pthread_mutex_unlock(&shutdown_mutex
);
58 void SignalThread::set_shutdown(shutdown_t arg
)
60 pthread_mutex_lock(&shutdown_mutex
);
62 pthread_mutex_unlock(&shutdown_mutex
);
64 if (arg
== SHUTDOWN_GRACEFUL
)
66 if (pthread_kill(thread
, SIGUSR2
) == 0)
69 pthread_join(thread
, &retval
);
74 shutdown_t
SignalThread::get_shutdown()
77 pthread_mutex_lock(&shutdown_mutex
);
79 pthread_mutex_unlock(&shutdown_mutex
);
84 void SignalThread::post()
89 void SignalThread::test()
91 assert(magic_memory
== MAGIC_MEMORY
);
92 if (bool(getenv("LIBTEST_IN_GDB")) == false)
94 assert(sigismember(&set
, SIGALRM
));
95 assert(sigismember(&set
, SIGABRT
));
96 assert(sigismember(&set
, SIGQUIT
));
97 assert(sigismember(&set
, SIGINT
));
98 assert(sigismember(&set
, SIGVTALRM
));
100 assert(sigismember(&set
, SIGUSR2
));
103 bool SignalThread::unblock()
106 if ((error
= pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
)) != 0)
108 Error
<< "While trying to reset signal mask to original set, pthread_sigmask() died during pthread_sigmask(" << strerror(error
) << ")";
115 SignalThread::~SignalThread()
117 if (is_shutdown() == false)
119 set_shutdown(SHUTDOWN_GRACEFUL
);
123 if (pthread_equal(thread
, pthread_self()) != 0 and (pthread_kill(thread
, 0) == ESRCH
) == true)
126 pthread_join(thread
, &retval
);
136 static void *sig_thread(void *arg
)
138 SignalThread
*context
= (SignalThread
*)arg
;
143 while (context
->get_shutdown() == SHUTDOWN_RUNNING
)
147 if (context
->wait(sig
) == -1)
149 Error
<< "sigwait() returned errno:" << strerror(errno
);
164 Error
<< "SIGVTALRM was called";
172 if (context
->is_shutdown() == false)
174 context
->set_shutdown(SHUTDOWN_FORCED
);
179 Error
<< "Ignoring SIGPIPE";
184 Error
<< "Inside of gdb";
188 Error
<< "Signal handling thread got unexpected signal " << strsignal(sig
);
198 SignalThread::SignalThread() :
199 magic_memory(MAGIC_MEMORY
),
200 thread(pthread_self())
202 pthread_mutex_init(&shutdown_mutex
, NULL
);
204 if (bool(getenv("LIBTEST_IN_GDB")) == false)
206 sigaddset(&set
, SIGALRM
);
207 sigaddset(&set
, SIGABRT
);
208 sigaddset(&set
, SIGQUIT
);
209 sigaddset(&set
, SIGINT
);
210 sigaddset(&set
, SIGVTALRM
);
214 Out
<< "Inside of GDB, disabling signal handlers";
217 sigaddset(&set
, SIGPIPE
);
219 sigaddset(&set
, SIGUSR2
);
221 sem_init(&lock
, 0, 0);
223 sigemptyset(&original_set
);
224 pthread_sigmask(SIG_BLOCK
, NULL
, &original_set
);
228 bool SignalThread::setup()
230 set_shutdown(SHUTDOWN_RUNNING
);
232 if (sigismember(&original_set
, SIGQUIT
))
234 Error
<< strsignal(SIGQUIT
) << " has been previously set.";
237 if (sigismember(&original_set
, SIGINT
))
239 Error
<< strsignal(SIGINT
) << " has been previously set.";
242 if (sigismember(&original_set
, SIGVTALRM
))
244 Error
<< strsignal(SIGVTALRM
) << " has been previously set.";
247 if (sigismember(&original_set
, SIGUSR2
))
249 Error
<< strsignal(SIGUSR2
) << " has been previously set.";
253 if ((error
= pthread_sigmask(SIG_BLOCK
, &set
, NULL
)) != 0)
255 Error
<< "pthread_sigmask() died during pthread_sigmask(" << strerror(error
) << ")";
259 if ((error
= pthread_create(&thread
, NULL
, &sig_thread
, this)) != 0)
261 Error
<< "pthread_create() died during pthread_create(" << strerror(error
) << ")";