xref: /minix/external/bsd/atf/dist/tools/timers.cpp (revision 0a6a1f1d)
1 //
2 // Automated Testing Framework (atf)
3 //
4 // Copyright (c) 2010 The NetBSD Foundation, Inc.
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
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.
15 //
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.
28 //
29 
30 extern "C" {
31 #include <sys/time.h>
32 }
33 
34 #include <cassert>
35 #include <cerrno>
36 #include <csignal>
37 #include <ctime>
38 
39 #include "exceptions.hpp"
40 #include "signals.hpp"
41 #include "timers.hpp"
42 
43 namespace impl = tools::timers;
44 #define IMPL_NAME "tools::timers"
45 
46 // ------------------------------------------------------------------------
47 // Auxiliary functions.
48 // ------------------------------------------------------------------------
49 
50 static
51 void
52 handler(const int signo __attribute__((__unused__)), siginfo_t* si,
53         void* uc __attribute__((__unused__)))
54 {
55     impl::timer* timer = static_cast< impl::timer* >(si->si_value.sival_ptr);
56     timer->set_fired();
57     timer->timeout_callback();
58 }
59 
60 // ------------------------------------------------------------------------
61 // The "timer" class.
62 // ------------------------------------------------------------------------
63 
64 struct impl::timer::impl {
65     ::timer_t m_timer;
66     ::itimerspec m_old_it;
67 
68     struct ::sigaction m_old_sa;
69     volatile bool m_fired;
70 
71     impl(void) : m_fired(false)
72     {
73     }
74 };
75 
76 impl::timer::timer(const unsigned int seconds) :
77     m_pimpl(new impl())
78 {
79     struct ::sigaction sa;
80     sigemptyset(&sa.sa_mask);
81 #if !defined(__minix)
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);
88 
89     struct ::sigevent se;
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);
99     }
100 
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);
111     }
112 }
113 
114 impl::timer::~timer(void)
115 {
116     int ret;
117 
118     ret = ::timer_delete(m_pimpl->m_timer);
119     assert(ret != -1);
120 
121     ret = ::sigaction(SIGALRM, &m_pimpl->m_old_sa, NULL);
122     assert(ret != -1);
123 }
124 
125 bool
126 impl::timer::fired(void)
127     const
128 {
129     return m_pimpl->m_fired;
130 }
131 
132 void
133 impl::timer::set_fired(void)
134 {
135     m_pimpl->m_fired = true;
136 }
137 
138 // ------------------------------------------------------------------------
139 // The "child_timer" class.
140 // ------------------------------------------------------------------------
141 
142 impl::child_timer::child_timer(const unsigned int seconds, const pid_t pid,
143                                volatile bool& terminate) :
144     timer(seconds),
145     m_pid(pid),
146     m_terminate(terminate)
147 {
148 }
149 
150 impl::child_timer::~child_timer(void)
151 {
152 }
153 
154 void
155 impl::child_timer::timeout_callback(void)
156 {
157     static const timespec ts = { 1, 0 };
158     m_terminate = true;
159     ::kill(-m_pid, SIGTERM);
160     ::nanosleep(&ts, NULL);
161     if (::kill(-m_pid, 0) != -1)
162        ::kill(-m_pid, SIGKILL);
163 }
164