xref: /minix3/external/bsd/atf/dist/tools/timers.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc //
2*0a6a1f1dSLionel Sambuc // Automated Testing Framework (atf)
3*0a6a1f1dSLionel Sambuc //
4*0a6a1f1dSLionel Sambuc // Copyright (c) 2010 The NetBSD Foundation, Inc.
5*0a6a1f1dSLionel Sambuc // All rights reserved.
6*0a6a1f1dSLionel Sambuc //
7*0a6a1f1dSLionel Sambuc // Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc // modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc // are met:
10*0a6a1f1dSLionel Sambuc // 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc //    notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc // 2. Redistributions in binary form must reproduce the above copyright
13*0a6a1f1dSLionel Sambuc //    notice, this list of conditions and the following disclaimer in the
14*0a6a1f1dSLionel Sambuc //    documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc //
16*0a6a1f1dSLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*0a6a1f1dSLionel Sambuc // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*0a6a1f1dSLionel Sambuc // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*0a6a1f1dSLionel Sambuc // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*0a6a1f1dSLionel Sambuc // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*0a6a1f1dSLionel Sambuc // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*0a6a1f1dSLionel Sambuc // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*0a6a1f1dSLionel Sambuc // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*0a6a1f1dSLionel Sambuc // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*0a6a1f1dSLionel Sambuc // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*0a6a1f1dSLionel Sambuc // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*0a6a1f1dSLionel Sambuc // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*0a6a1f1dSLionel Sambuc //
29*0a6a1f1dSLionel Sambuc 
30*0a6a1f1dSLionel Sambuc extern "C" {
31*0a6a1f1dSLionel Sambuc #include <sys/time.h>
32*0a6a1f1dSLionel Sambuc }
33*0a6a1f1dSLionel Sambuc 
34*0a6a1f1dSLionel Sambuc #include <cassert>
35*0a6a1f1dSLionel Sambuc #include <cerrno>
36*0a6a1f1dSLionel Sambuc #include <csignal>
37*0a6a1f1dSLionel Sambuc #include <ctime>
38*0a6a1f1dSLionel Sambuc 
39*0a6a1f1dSLionel Sambuc #include "exceptions.hpp"
40*0a6a1f1dSLionel Sambuc #include "signals.hpp"
41*0a6a1f1dSLionel Sambuc #include "timers.hpp"
42*0a6a1f1dSLionel Sambuc 
43*0a6a1f1dSLionel Sambuc namespace impl = tools::timers;
44*0a6a1f1dSLionel Sambuc #define IMPL_NAME "tools::timers"
45*0a6a1f1dSLionel Sambuc 
46*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
47*0a6a1f1dSLionel Sambuc // Auxiliary functions.
48*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
49*0a6a1f1dSLionel Sambuc 
50*0a6a1f1dSLionel Sambuc static
51*0a6a1f1dSLionel Sambuc void
52*0a6a1f1dSLionel Sambuc handler(const int signo __attribute__((__unused__)), siginfo_t* si,
53*0a6a1f1dSLionel Sambuc         void* uc __attribute__((__unused__)))
54*0a6a1f1dSLionel Sambuc {
55*0a6a1f1dSLionel Sambuc     impl::timer* timer = static_cast< impl::timer* >(si->si_value.sival_ptr);
56*0a6a1f1dSLionel Sambuc     timer->set_fired();
57*0a6a1f1dSLionel Sambuc     timer->timeout_callback();
58*0a6a1f1dSLionel Sambuc }
59*0a6a1f1dSLionel Sambuc 
60*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
61*0a6a1f1dSLionel Sambuc // The "timer" class.
62*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
63*0a6a1f1dSLionel Sambuc 
64*0a6a1f1dSLionel Sambuc struct impl::timer::impl {
65*0a6a1f1dSLionel Sambuc     ::timer_t m_timer;
66*0a6a1f1dSLionel Sambuc     ::itimerspec m_old_it;
67*0a6a1f1dSLionel Sambuc 
68*0a6a1f1dSLionel Sambuc     struct ::sigaction m_old_sa;
69*0a6a1f1dSLionel Sambuc     volatile bool m_fired;
70*0a6a1f1dSLionel Sambuc 
71*0a6a1f1dSLionel Sambuc     impl(void) : m_fired(false)
72*0a6a1f1dSLionel Sambuc     {
73*0a6a1f1dSLionel Sambuc     }
74*0a6a1f1dSLionel Sambuc };
75*0a6a1f1dSLionel Sambuc 
76*0a6a1f1dSLionel Sambuc impl::timer::timer(const unsigned int seconds) :
77*0a6a1f1dSLionel Sambuc     m_pimpl(new impl())
78*0a6a1f1dSLionel Sambuc {
79*0a6a1f1dSLionel Sambuc     struct ::sigaction sa;
80*0a6a1f1dSLionel Sambuc     sigemptyset(&sa.sa_mask);
81*0a6a1f1dSLionel Sambuc #if !defined(__minix)
82*0a6a1f1dSLionel Sambuc     sa.sa_flags = SA_SIGINFO;
83*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
84*0a6a1f1dSLionel Sambuc     sa.sa_sigaction = ::handler;
85*0a6a1f1dSLionel Sambuc     if (::sigaction(SIGALRM, &sa, &m_pimpl->m_old_sa) == -1)
86*0a6a1f1dSLionel Sambuc         throw tools::system_error(IMPL_NAME "::timer::timer",
87*0a6a1f1dSLionel Sambuc                                 "Failed to set signal handler", errno);
88*0a6a1f1dSLionel Sambuc 
89*0a6a1f1dSLionel Sambuc     struct ::sigevent se;
90*0a6a1f1dSLionel Sambuc     se.sigev_notify = SIGEV_SIGNAL;
91*0a6a1f1dSLionel Sambuc     se.sigev_signo = SIGALRM;
92*0a6a1f1dSLionel Sambuc     se.sigev_value.sival_ptr = static_cast< void* >(this);
93*0a6a1f1dSLionel Sambuc     se.sigev_notify_function = NULL;
94*0a6a1f1dSLionel Sambuc     se.sigev_notify_attributes = NULL;
95*0a6a1f1dSLionel Sambuc     if (::timer_create(CLOCK_MONOTONIC, &se, &m_pimpl->m_timer) == -1) {
96*0a6a1f1dSLionel Sambuc         ::sigaction(SIGALRM, &m_pimpl->m_old_sa, NULL);
97*0a6a1f1dSLionel Sambuc         throw tools::system_error(IMPL_NAME "::timer::timer",
98*0a6a1f1dSLionel Sambuc                                 "Failed to create timer", errno);
99*0a6a1f1dSLionel Sambuc     }
100*0a6a1f1dSLionel Sambuc 
101*0a6a1f1dSLionel Sambuc     struct ::itimerspec it;
102*0a6a1f1dSLionel Sambuc     it.it_interval.tv_sec = 0;
103*0a6a1f1dSLionel Sambuc     it.it_interval.tv_nsec = 0;
104*0a6a1f1dSLionel Sambuc     it.it_value.tv_sec = seconds;
105*0a6a1f1dSLionel Sambuc     it.it_value.tv_nsec = 0;
106*0a6a1f1dSLionel Sambuc     if (::timer_settime(m_pimpl->m_timer, 0, &it, &m_pimpl->m_old_it) == -1) {
107*0a6a1f1dSLionel Sambuc        ::sigaction(SIGALRM, &m_pimpl->m_old_sa, NULL);
108*0a6a1f1dSLionel Sambuc        ::timer_delete(m_pimpl->m_timer);
109*0a6a1f1dSLionel Sambuc         throw tools::system_error(IMPL_NAME "::timer::timer",
110*0a6a1f1dSLionel Sambuc                                 "Failed to program timer", errno);
111*0a6a1f1dSLionel Sambuc     }
112*0a6a1f1dSLionel Sambuc }
113*0a6a1f1dSLionel Sambuc 
114*0a6a1f1dSLionel Sambuc impl::timer::~timer(void)
115*0a6a1f1dSLionel Sambuc {
116*0a6a1f1dSLionel Sambuc     int ret;
117*0a6a1f1dSLionel Sambuc 
118*0a6a1f1dSLionel Sambuc     ret = ::timer_delete(m_pimpl->m_timer);
119*0a6a1f1dSLionel Sambuc     assert(ret != -1);
120*0a6a1f1dSLionel Sambuc 
121*0a6a1f1dSLionel Sambuc     ret = ::sigaction(SIGALRM, &m_pimpl->m_old_sa, NULL);
122*0a6a1f1dSLionel Sambuc     assert(ret != -1);
123*0a6a1f1dSLionel Sambuc }
124*0a6a1f1dSLionel Sambuc 
125*0a6a1f1dSLionel Sambuc bool
126*0a6a1f1dSLionel Sambuc impl::timer::fired(void)
127*0a6a1f1dSLionel Sambuc     const
128*0a6a1f1dSLionel Sambuc {
129*0a6a1f1dSLionel Sambuc     return m_pimpl->m_fired;
130*0a6a1f1dSLionel Sambuc }
131*0a6a1f1dSLionel Sambuc 
132*0a6a1f1dSLionel Sambuc void
133*0a6a1f1dSLionel Sambuc impl::timer::set_fired(void)
134*0a6a1f1dSLionel Sambuc {
135*0a6a1f1dSLionel Sambuc     m_pimpl->m_fired = true;
136*0a6a1f1dSLionel Sambuc }
137*0a6a1f1dSLionel Sambuc 
138*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
139*0a6a1f1dSLionel Sambuc // The "child_timer" class.
140*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
141*0a6a1f1dSLionel Sambuc 
142*0a6a1f1dSLionel Sambuc impl::child_timer::child_timer(const unsigned int seconds, const pid_t pid,
143*0a6a1f1dSLionel Sambuc                                volatile bool& terminate) :
144*0a6a1f1dSLionel Sambuc     timer(seconds),
145*0a6a1f1dSLionel Sambuc     m_pid(pid),
146*0a6a1f1dSLionel Sambuc     m_terminate(terminate)
147*0a6a1f1dSLionel Sambuc {
148*0a6a1f1dSLionel Sambuc }
149*0a6a1f1dSLionel Sambuc 
150*0a6a1f1dSLionel Sambuc impl::child_timer::~child_timer(void)
151*0a6a1f1dSLionel Sambuc {
152*0a6a1f1dSLionel Sambuc }
153*0a6a1f1dSLionel Sambuc 
154*0a6a1f1dSLionel Sambuc void
155*0a6a1f1dSLionel Sambuc impl::child_timer::timeout_callback(void)
156*0a6a1f1dSLionel Sambuc {
157*0a6a1f1dSLionel Sambuc     static const timespec ts = { 1, 0 };
158*0a6a1f1dSLionel Sambuc     m_terminate = true;
159*0a6a1f1dSLionel Sambuc     ::kill(-m_pid, SIGTERM);
160*0a6a1f1dSLionel Sambuc     ::nanosleep(&ts, NULL);
161*0a6a1f1dSLionel Sambuc     if (::kill(-m_pid, 0) != -1)
162*0a6a1f1dSLionel Sambuc        ::kill(-m_pid, SIGKILL);
163*0a6a1f1dSLionel Sambuc }
164