263d183a2b4d7022f333529d6dbbbdbb0b6cba00
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 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/common.h>
41 #include <libtest/signal.h>
43 using namespace libtest
;
45 #define MAGIC_MEMORY 123569
47 bool SignalThread::is_shutdown()
50 pthread_mutex_lock(&shutdown_mutex
);
51 ret
= bool(__shutdown
!= SHUTDOWN_RUNNING
);
52 pthread_mutex_unlock(&shutdown_mutex
);
57 void SignalThread::set_shutdown(shutdown_t arg
)
59 pthread_mutex_lock(&shutdown_mutex
);
61 pthread_mutex_unlock(&shutdown_mutex
);
63 if (arg
== SHUTDOWN_GRACEFUL
)
65 if (pthread_kill(thread
, SIGUSR2
) == 0)
68 pthread_join(thread
, &retval
);
73 shutdown_t
SignalThread::get_shutdown()
76 pthread_mutex_lock(&shutdown_mutex
);
78 pthread_mutex_unlock(&shutdown_mutex
);
83 void SignalThread::post()
88 void SignalThread::test()
90 assert(magic_memory
== MAGIC_MEMORY
);
91 if (not getenv("LIBTEST_IN_GDB"))
93 assert(sigismember(&set
, SIGABRT
));
94 assert(sigismember(&set
, SIGQUIT
));
95 assert(sigismember(&set
, SIGINT
));
97 assert(sigismember(&set
, SIGUSR2
));
100 SignalThread::~SignalThread()
102 if (pthread_equal(thread
, pthread_self()) != 0 and (pthread_kill(thread
, 0) == ESRCH
) == true)
105 pthread_join(thread
, &retval
);
112 static void *sig_thread(void *arg
)
114 SignalThread
*context
= (SignalThread
*)arg
;
119 while (context
->get_shutdown() == SHUTDOWN_RUNNING
)
123 if (context
->wait(sig
) == -1)
125 Error
<< "sigwait() returned errno:" << strerror(errno
);
135 if (context
->is_shutdown() == false)
137 context
->set_shutdown(SHUTDOWN_FORCED
);
142 Error
<< "Signal handling thread got unexpected signal " << strsignal(sig
);
152 SignalThread::SignalThread() :
153 magic_memory(MAGIC_MEMORY
),
154 thread(pthread_self())
156 pthread_mutex_init(&shutdown_mutex
, NULL
);
158 if (not getenv("LIBTEST_IN_GDB"))
160 sigaddset(&set
, SIGABRT
);
161 sigaddset(&set
, SIGQUIT
);
162 sigaddset(&set
, SIGINT
);
165 sigaddset(&set
, SIGUSR2
);
167 sem_init(&lock
, 0, 0);
171 bool SignalThread::setup()
173 set_shutdown(SHUTDOWN_RUNNING
);
176 sigemptyset(&old_set
);
177 pthread_sigmask(SIG_BLOCK
, NULL
, &old_set
);
179 if (sigismember(&old_set
, SIGQUIT
))
181 Error
<< strsignal(SIGQUIT
) << " has been previously set.";
183 if (sigismember(&old_set
, SIGINT
))
185 Error
<< strsignal(SIGINT
) << " has been previously set.";
187 if (sigismember(&old_set
, SIGUSR2
))
189 Error
<< strsignal(SIGUSR2
) << " has been previously set.";
193 if ((error
= pthread_sigmask(SIG_BLOCK
, &set
, NULL
)) != 0)
195 Error
<< "pthread_sigmask() died during pthread_sigmask(" << strerror(error
) << ")";
199 if ((error
= pthread_create(&thread
, NULL
, &sig_thread
, this)) != 0)
201 Error
<< "pthread_create() died during pthread_create(" << strerror(error
) << ")";