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
);
157 Error
<< strsignal(sig
);
168 if (context
->is_shutdown() == false)
170 context
->set_shutdown(SHUTDOWN_FORCED
);
175 Error
<< "Ignoring SIGPIPE";
180 Error
<< "Inside of gdb";
184 Error
<< "Signal handling thread got unexpected signal " << strsignal(sig
);
194 SignalThread::SignalThread() :
195 magic_memory(MAGIC_MEMORY
),
196 thread(pthread_self())
198 pthread_mutex_init(&shutdown_mutex
, NULL
);
200 if (bool(getenv("LIBTEST_IN_GDB")) == false)
202 sigaddset(&set
, SIGALRM
);
203 sigaddset(&set
, SIGABRT
);
204 sigaddset(&set
, SIGQUIT
);
205 sigaddset(&set
, SIGINT
);
206 sigaddset(&set
, SIGVTALRM
);
208 sigaddset(&set
, SIGPIPE
);
210 sigaddset(&set
, SIGUSR2
);
212 sem_init(&lock
, 0, 0);
214 sigemptyset(&original_set
);
215 pthread_sigmask(SIG_BLOCK
, NULL
, &original_set
);
219 bool SignalThread::setup()
221 set_shutdown(SHUTDOWN_RUNNING
);
223 if (sigismember(&original_set
, SIGQUIT
))
225 Error
<< strsignal(SIGQUIT
) << " has been previously set.";
228 if (sigismember(&original_set
, SIGINT
))
230 Error
<< strsignal(SIGINT
) << " has been previously set.";
233 if (sigismember(&original_set
, SIGVTALRM
))
235 Error
<< strsignal(SIGVTALRM
) << " has been previously set.";
238 if (sigismember(&original_set
, SIGUSR2
))
240 Error
<< strsignal(SIGUSR2
) << " has been previously set.";
244 if ((error
= pthread_sigmask(SIG_BLOCK
, &set
, NULL
)) != 0)
246 Error
<< "pthread_sigmask() died during pthread_sigmask(" << strerror(error
) << ")";
250 if ((error
= pthread_create(&thread
, NULL
, &sig_thread
, this)) != 0)
252 Error
<< "pthread_create() died during pthread_create(" << strerror(error
) << ")";