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.
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
, SIGABRT
));
95 assert(sigismember(&set
, SIGQUIT
));
96 assert(sigismember(&set
, SIGINT
));
98 assert(sigismember(&set
, SIGUSR2
));
101 SignalThread::~SignalThread()
103 if (is_shutdown() == false)
105 set_shutdown(SHUTDOWN_GRACEFUL
);
109 if (pthread_equal(thread
, pthread_self()) != 0 and (pthread_kill(thread
, 0) == ESRCH
) == true)
112 pthread_join(thread
, &retval
);
118 if ((error
= pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
)) != 0)
120 Error
<< "While trying to reset signal mask to original set, pthread_sigmask() died during pthread_sigmask(" << strerror(error
) << ")";
126 static void *sig_thread(void *arg
)
128 SignalThread
*context
= (SignalThread
*)arg
;
133 while (context
->get_shutdown() == SHUTDOWN_RUNNING
)
137 if (context
->wait(sig
) == -1)
139 Error
<< "sigwait() returned errno:" << strerror(errno
);
149 if (context
->is_shutdown() == false)
151 context
->set_shutdown(SHUTDOWN_FORCED
);
156 Error
<< "Ignoring SIGPIPE";
161 Error
<< "Inside of gdb";
165 Error
<< "Signal handling thread got unexpected signal " << strsignal(sig
);
175 SignalThread::SignalThread() :
176 magic_memory(MAGIC_MEMORY
),
177 thread(pthread_self())
179 pthread_mutex_init(&shutdown_mutex
, NULL
);
181 if (bool(getenv("LIBTEST_IN_GDB")) == false)
183 sigaddset(&set
, SIGABRT
);
184 sigaddset(&set
, SIGQUIT
);
185 sigaddset(&set
, SIGINT
);
187 sigaddset(&set
, SIGPIPE
);
189 sigaddset(&set
, SIGUSR2
);
191 sem_init(&lock
, 0, 0);
193 sigemptyset(&original_set
);
194 pthread_sigmask(SIG_BLOCK
, NULL
, &original_set
);
198 bool SignalThread::setup()
200 set_shutdown(SHUTDOWN_RUNNING
);
202 if (sigismember(&original_set
, SIGQUIT
))
204 Error
<< strsignal(SIGQUIT
) << " has been previously set.";
206 if (sigismember(&original_set
, SIGINT
))
208 Error
<< strsignal(SIGINT
) << " has been previously set.";
210 if (sigismember(&original_set
, SIGUSR2
))
212 Error
<< strsignal(SIGUSR2
) << " has been previously set.";
216 if ((error
= pthread_sigmask(SIG_BLOCK
, &set
, NULL
)) != 0)
218 Error
<< "pthread_sigmask() died during pthread_sigmask(" << strerror(error
) << ")";
222 if ((error
= pthread_create(&thread
, NULL
, &sig_thread
, this)) != 0)
224 Error
<< "pthread_create() died during pthread_create(" << strerror(error
) << ")";