xref: /minix3/external/bsd/atf/dist/tools/timers.cpp (revision e1cdaee10649323af446eb1a74571984b2ab3181)
10a6a1f1dSLionel Sambuc //
20a6a1f1dSLionel Sambuc // Automated Testing Framework (atf)
30a6a1f1dSLionel Sambuc //
40a6a1f1dSLionel Sambuc // Copyright (c) 2010 The NetBSD Foundation, Inc.
50a6a1f1dSLionel Sambuc // All rights reserved.
60a6a1f1dSLionel Sambuc //
70a6a1f1dSLionel Sambuc // Redistribution and use in source and binary forms, with or without
80a6a1f1dSLionel Sambuc // modification, are permitted provided that the following conditions
90a6a1f1dSLionel Sambuc // are met:
100a6a1f1dSLionel Sambuc // 1. Redistributions of source code must retain the above copyright
110a6a1f1dSLionel Sambuc //    notice, this list of conditions and the following disclaimer.
120a6a1f1dSLionel Sambuc // 2. Redistributions in binary form must reproduce the above copyright
130a6a1f1dSLionel Sambuc //    notice, this list of conditions and the following disclaimer in the
140a6a1f1dSLionel Sambuc //    documentation and/or other materials provided with the distribution.
150a6a1f1dSLionel Sambuc //
160a6a1f1dSLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
170a6a1f1dSLionel Sambuc // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
180a6a1f1dSLionel Sambuc // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
190a6a1f1dSLionel Sambuc // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
200a6a1f1dSLionel Sambuc // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
210a6a1f1dSLionel Sambuc // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
220a6a1f1dSLionel Sambuc // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
230a6a1f1dSLionel Sambuc // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
240a6a1f1dSLionel Sambuc // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
250a6a1f1dSLionel Sambuc // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
260a6a1f1dSLionel Sambuc // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
270a6a1f1dSLionel Sambuc // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
280a6a1f1dSLionel Sambuc //
290a6a1f1dSLionel Sambuc 
300a6a1f1dSLionel Sambuc extern "C" {
310a6a1f1dSLionel Sambuc #include <sys/time.h>
320a6a1f1dSLionel Sambuc }
330a6a1f1dSLionel Sambuc 
340a6a1f1dSLionel Sambuc #include <cassert>
350a6a1f1dSLionel Sambuc #include <cerrno>
360a6a1f1dSLionel Sambuc #include <csignal>
370a6a1f1dSLionel Sambuc #include <ctime>
380a6a1f1dSLionel Sambuc 
390a6a1f1dSLionel Sambuc #include "exceptions.hpp"
400a6a1f1dSLionel Sambuc #include "signals.hpp"
410a6a1f1dSLionel Sambuc #include "timers.hpp"
420a6a1f1dSLionel Sambuc 
430a6a1f1dSLionel Sambuc namespace impl = tools::timers;
440a6a1f1dSLionel Sambuc #define IMPL_NAME "tools::timers"
450a6a1f1dSLionel Sambuc 
460a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
470a6a1f1dSLionel Sambuc // Auxiliary functions.
480a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
490a6a1f1dSLionel Sambuc 
500a6a1f1dSLionel Sambuc static
510a6a1f1dSLionel Sambuc void
handler(const int signo,siginfo_t * si,void * uc)520a6a1f1dSLionel Sambuc handler(const int signo __attribute__((__unused__)), siginfo_t* si,
530a6a1f1dSLionel Sambuc         void* uc __attribute__((__unused__)))
540a6a1f1dSLionel Sambuc {
550a6a1f1dSLionel Sambuc     impl::timer* timer = static_cast< impl::timer* >(si->si_value.sival_ptr);
560a6a1f1dSLionel Sambuc     timer->set_fired();
570a6a1f1dSLionel Sambuc     timer->timeout_callback();
580a6a1f1dSLionel Sambuc }
590a6a1f1dSLionel Sambuc 
600a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
610a6a1f1dSLionel Sambuc // The "timer" class.
620a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
630a6a1f1dSLionel Sambuc 
640a6a1f1dSLionel Sambuc struct impl::timer::impl {
650a6a1f1dSLionel Sambuc     ::timer_t m_timer;
660a6a1f1dSLionel Sambuc     ::itimerspec m_old_it;
670a6a1f1dSLionel Sambuc 
680a6a1f1dSLionel Sambuc     struct ::sigaction m_old_sa;
690a6a1f1dSLionel Sambuc     volatile bool m_fired;
700a6a1f1dSLionel Sambuc 
implimpl::timer::impl710a6a1f1dSLionel Sambuc     impl(void) : m_fired(false)
720a6a1f1dSLionel Sambuc     {
730a6a1f1dSLionel Sambuc     }
740a6a1f1dSLionel Sambuc };
750a6a1f1dSLionel Sambuc 
timer(const unsigned int seconds)760a6a1f1dSLionel Sambuc impl::timer::timer(const unsigned int seconds) :
770a6a1f1dSLionel Sambuc     m_pimpl(new impl())
780a6a1f1dSLionel Sambuc {
790a6a1f1dSLionel Sambuc     struct ::sigaction sa;
800a6a1f1dSLionel Sambuc     sigemptyset(&sa.sa_mask);
810a6a1f1dSLionel Sambuc #if !defined(__minix)
820a6a1f1dSLionel Sambuc     sa.sa_flags = SA_SIGINFO;
830a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
840a6a1f1dSLionel Sambuc     sa.sa_sigaction = ::handler;
850a6a1f1dSLionel Sambuc     if (::sigaction(SIGALRM, &sa, &m_pimpl->m_old_sa) == -1)
860a6a1f1dSLionel Sambuc         throw tools::system_error(IMPL_NAME "::timer::timer",
870a6a1f1dSLionel Sambuc                                 "Failed to set signal handler", errno);
880a6a1f1dSLionel Sambuc 
890a6a1f1dSLionel Sambuc     struct ::sigevent se;
900a6a1f1dSLionel Sambuc     se.sigev_notify = SIGEV_SIGNAL;
910a6a1f1dSLionel Sambuc     se.sigev_signo = SIGALRM;
920a6a1f1dSLionel Sambuc     se.sigev_value.sival_ptr = static_cast< void* >(this);
930a6a1f1dSLionel Sambuc     se.sigev_notify_function = NULL;
940a6a1f1dSLionel Sambuc     se.sigev_notify_attributes = NULL;
950a6a1f1dSLionel Sambuc     if (::timer_create(CLOCK_MONOTONIC, &se, &m_pimpl->m_timer) == -1) {
960a6a1f1dSLionel Sambuc         ::sigaction(SIGALRM, &m_pimpl->m_old_sa, NULL);
970a6a1f1dSLionel Sambuc         throw tools::system_error(IMPL_NAME "::timer::timer",
980a6a1f1dSLionel Sambuc                                 "Failed to create timer", errno);
990a6a1f1dSLionel Sambuc     }
1000a6a1f1dSLionel Sambuc 
1010a6a1f1dSLionel Sambuc     struct ::itimerspec it;
1020a6a1f1dSLionel Sambuc     it.it_interval.tv_sec = 0;
1030a6a1f1dSLionel Sambuc     it.it_interval.tv_nsec = 0;
1040a6a1f1dSLionel Sambuc     it.it_value.tv_sec = seconds;
1050a6a1f1dSLionel Sambuc     it.it_value.tv_nsec = 0;
1060a6a1f1dSLionel Sambuc     if (::timer_settime(m_pimpl->m_timer, 0, &it, &m_pimpl->m_old_it) == -1) {
1070a6a1f1dSLionel Sambuc        ::sigaction(SIGALRM, &m_pimpl->m_old_sa, NULL);
1080a6a1f1dSLionel Sambuc        ::timer_delete(m_pimpl->m_timer);
1090a6a1f1dSLionel Sambuc         throw tools::system_error(IMPL_NAME "::timer::timer",
1100a6a1f1dSLionel Sambuc                                 "Failed to program timer", errno);
1110a6a1f1dSLionel Sambuc     }
1120a6a1f1dSLionel Sambuc }
1130a6a1f1dSLionel Sambuc 
~timer(void)1140a6a1f1dSLionel Sambuc impl::timer::~timer(void)
1150a6a1f1dSLionel Sambuc {
116*e1cdaee1SLionel Sambuc #if !defined(NDEBUG) && defined(__minix)
1170a6a1f1dSLionel Sambuc     int ret;
1180a6a1f1dSLionel Sambuc 
119*e1cdaee1SLionel Sambuc     ret =
120*e1cdaee1SLionel Sambuc #endif /* !defined(NDEBUG) && defined(__minix) */
121*e1cdaee1SLionel Sambuc     	::timer_delete(m_pimpl->m_timer);
1220a6a1f1dSLionel Sambuc     assert(ret != -1);
1230a6a1f1dSLionel Sambuc 
124*e1cdaee1SLionel Sambuc #if !defined(NDEBUG) && defined(__minix)
125*e1cdaee1SLionel Sambuc     ret =
126*e1cdaee1SLionel Sambuc #endif /* !defined(NDEBUG) && defined(__minix) */
127*e1cdaee1SLionel Sambuc     	::sigaction(SIGALRM, &m_pimpl->m_old_sa, NULL);
1280a6a1f1dSLionel Sambuc     assert(ret != -1);
1290a6a1f1dSLionel Sambuc }
1300a6a1f1dSLionel Sambuc 
1310a6a1f1dSLionel Sambuc bool
fired(void) const1320a6a1f1dSLionel Sambuc impl::timer::fired(void)
1330a6a1f1dSLionel Sambuc     const
1340a6a1f1dSLionel Sambuc {
1350a6a1f1dSLionel Sambuc     return m_pimpl->m_fired;
1360a6a1f1dSLionel Sambuc }
1370a6a1f1dSLionel Sambuc 
1380a6a1f1dSLionel Sambuc void
set_fired(void)1390a6a1f1dSLionel Sambuc impl::timer::set_fired(void)
1400a6a1f1dSLionel Sambuc {
1410a6a1f1dSLionel Sambuc     m_pimpl->m_fired = true;
1420a6a1f1dSLionel Sambuc }
1430a6a1f1dSLionel Sambuc 
1440a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
1450a6a1f1dSLionel Sambuc // The "child_timer" class.
1460a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
1470a6a1f1dSLionel Sambuc 
child_timer(const unsigned int seconds,const pid_t pid,volatile bool & terminate)1480a6a1f1dSLionel Sambuc impl::child_timer::child_timer(const unsigned int seconds, const pid_t pid,
1490a6a1f1dSLionel Sambuc                                volatile bool& terminate) :
1500a6a1f1dSLionel Sambuc     timer(seconds),
1510a6a1f1dSLionel Sambuc     m_pid(pid),
1520a6a1f1dSLionel Sambuc     m_terminate(terminate)
1530a6a1f1dSLionel Sambuc {
1540a6a1f1dSLionel Sambuc }
1550a6a1f1dSLionel Sambuc 
~child_timer(void)1560a6a1f1dSLionel Sambuc impl::child_timer::~child_timer(void)
1570a6a1f1dSLionel Sambuc {
1580a6a1f1dSLionel Sambuc }
1590a6a1f1dSLionel Sambuc 
1600a6a1f1dSLionel Sambuc void
timeout_callback(void)1610a6a1f1dSLionel Sambuc impl::child_timer::timeout_callback(void)
1620a6a1f1dSLionel Sambuc {
1630a6a1f1dSLionel Sambuc     static const timespec ts = { 1, 0 };
1640a6a1f1dSLionel Sambuc     m_terminate = true;
1650a6a1f1dSLionel Sambuc     ::kill(-m_pid, SIGTERM);
1660a6a1f1dSLionel Sambuc     ::nanosleep(&ts, NULL);
1670a6a1f1dSLionel Sambuc     if (::kill(-m_pid, 0) != -1)
1680a6a1f1dSLionel Sambuc        ::kill(-m_pid, SIGKILL);
1690a6a1f1dSLionel Sambuc }
170