1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 * Data Differential Utility 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.
46 #include <util/signal.hpp>
48 namespace datadifferential
{
51 #define MAGIC_MEMORY 123569
53 bool SignalThread::is_shutdown()
56 pthread_mutex_lock(&shutdown_mutex
);
57 ret
= bool(__shutdown
!= SHUTDOWN_RUNNING
);
58 pthread_mutex_unlock(&shutdown_mutex
);
63 void SignalThread::set_shutdown(shutdown_t arg
)
65 pthread_mutex_lock(&shutdown_mutex
);
67 pthread_mutex_unlock(&shutdown_mutex
);
69 if (arg
== SHUTDOWN_GRACEFUL
)
71 if (pthread_kill(thread
, SIGUSR2
) == 0)
74 pthread_join(thread
, &retval
);
79 shutdown_t
SignalThread::get_shutdown()
82 pthread_mutex_lock(&shutdown_mutex
);
84 pthread_mutex_unlock(&shutdown_mutex
);
89 void SignalThread::post()
94 void SignalThread::test()
96 assert(magic_memory
== MAGIC_MEMORY
);
97 assert(sigismember(&set
, SIGABRT
));
98 assert(sigismember(&set
, SIGINT
));
99 assert(sigismember(&set
, SIGQUIT
));
100 assert(sigismember(&set
, SIGTERM
));
101 assert(sigismember(&set
, SIGUSR2
));
104 SignalThread::~SignalThread()
106 if (not is_shutdown())
108 set_shutdown(SHUTDOWN_GRACEFUL
);
112 if (pthread_equal(thread
, pthread_self()) != 0 and (pthread_kill(thread
, 0) == ESRCH
) == true)
115 pthread_join(thread
, &retval
);
123 static void *sig_thread(void *arg
)
125 SignalThread
*context
= (SignalThread
*)arg
;
130 while (context
->get_shutdown() == SHUTDOWN_RUNNING
)
134 if (context
->wait(sig
) == -1)
136 std::cerr
<< "sigwait() returned errno:" << strerror(errno
) << std::endl
;
149 if (context
->is_shutdown() == false)
151 context
->set_shutdown(SHUTDOWN_FORCED
);
154 if (context
->exit_on_signal())
162 std::cerr
<< "Signal handling thread got unexpected signal " << strsignal(sig
) << std::endl
;
172 SignalThread::SignalThread(bool exit_on_signal_arg
) :
173 _exit_on_signal(exit_on_signal_arg
),
174 magic_memory(MAGIC_MEMORY
),
175 thread(pthread_self())
177 pthread_mutex_init(&shutdown_mutex
, NULL
);
180 sigaddset(&set
, SIGABRT
);
181 sigaddset(&set
, SIGINT
);
182 sigaddset(&set
, SIGQUIT
);
183 sigaddset(&set
, SIGTERM
);
184 sigaddset(&set
, SIGUSR2
);
186 sem_init(&lock
, 0, 0);
190 bool SignalThread::setup()
192 set_shutdown(SHUTDOWN_RUNNING
);
195 if ((error
= pthread_sigmask(SIG_BLOCK
, &set
, NULL
)) != 0)
197 std::cerr
<< "pthread_sigmask() died during pthread_sigmask(" << strerror(error
) << ")" << std::endl
;
201 if ((error
= pthread_create(&thread
, NULL
, &sig_thread
, this)) != 0)
203 std::cerr
<< "pthread_create() died during pthread_create(" << strerror(error
) << ")" << std::endl
;
212 } /* namespace util */
213 } /* namespace datadifferential */