2 // Automated Testing Framework (atf)
4 // Copyright (c) 2010 The NetBSD Foundation, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
10 // 1. Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include "exceptions.hpp"
40 #include "signals.hpp"
43 namespace impl
= tools::timers
;
44 #define IMPL_NAME "tools::timers"
46 // ------------------------------------------------------------------------
47 // Auxiliary functions.
48 // ------------------------------------------------------------------------
52 handler(const int signo
__attribute__((__unused__
)), siginfo_t
* si
,
53 void* uc
__attribute__((__unused__
)))
55 impl::timer
* timer
= static_cast< impl::timer
* >(si
->si_value
.sival_ptr
);
57 timer
->timeout_callback();
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
64 struct impl::timer::impl
{
66 ::itimerspec m_old_it
;
68 struct ::sigaction m_old_sa
;
69 volatile bool m_fired
;
71 impl(void) : m_fired(false)
76 impl::timer::timer(const unsigned int seconds
) :
79 struct ::sigaction sa
;
80 sigemptyset(&sa
.sa_mask
);
82 sa
.sa_flags
= SA_SIGINFO
;
83 #endif /* !defined(__minix) */
84 sa
.sa_sigaction
= ::handler
;
85 if (::sigaction(SIGALRM
, &sa
, &m_pimpl
->m_old_sa
) == -1)
86 throw tools::system_error(IMPL_NAME
"::timer::timer",
87 "Failed to set signal handler", errno
);
90 se
.sigev_notify
= SIGEV_SIGNAL
;
91 se
.sigev_signo
= SIGALRM
;
92 se
.sigev_value
.sival_ptr
= static_cast< void* >(this);
93 se
.sigev_notify_function
= NULL
;
94 se
.sigev_notify_attributes
= NULL
;
95 if (::timer_create(CLOCK_MONOTONIC
, &se
, &m_pimpl
->m_timer
) == -1) {
96 ::sigaction(SIGALRM
, &m_pimpl
->m_old_sa
, NULL
);
97 throw tools::system_error(IMPL_NAME
"::timer::timer",
98 "Failed to create timer", errno
);
101 struct ::itimerspec it
;
102 it
.it_interval
.tv_sec
= 0;
103 it
.it_interval
.tv_nsec
= 0;
104 it
.it_value
.tv_sec
= seconds
;
105 it
.it_value
.tv_nsec
= 0;
106 if (::timer_settime(m_pimpl
->m_timer
, 0, &it
, &m_pimpl
->m_old_it
) == -1) {
107 ::sigaction(SIGALRM
, &m_pimpl
->m_old_sa
, NULL
);
108 ::timer_delete(m_pimpl
->m_timer
);
109 throw tools::system_error(IMPL_NAME
"::timer::timer",
110 "Failed to program timer", errno
);
114 impl::timer::~timer(void)
116 #if !defined(NDEBUG) && defined(__minix)
120 #endif /* !defined(NDEBUG) && defined(__minix) */
121 ::timer_delete(m_pimpl
->m_timer
);
124 #if !defined(NDEBUG) && defined(__minix)
126 #endif /* !defined(NDEBUG) && defined(__minix) */
127 ::sigaction(SIGALRM
, &m_pimpl
->m_old_sa
, NULL
);
132 impl::timer::fired(void)
135 return m_pimpl
->m_fired
;
139 impl::timer::set_fired(void)
141 m_pimpl
->m_fired
= true;
144 // ------------------------------------------------------------------------
145 // The "child_timer" class.
146 // ------------------------------------------------------------------------
148 impl::child_timer::child_timer(const unsigned int seconds
, const pid_t pid
,
149 volatile bool& terminate
) :
152 m_terminate(terminate
)
156 impl::child_timer::~child_timer(void)
161 impl::child_timer::timeout_callback(void)
163 static const timespec ts
= { 1, 0 };
165 ::kill(-m_pid
, SIGTERM
);
166 ::nanosleep(&ts
, NULL
);
167 if (::kill(-m_pid
, 0) != -1)
168 ::kill(-m_pid
, SIGKILL
);