etc/services - sync with NetBSD-8
[minix.git] / external / bsd / atf / dist / tools / timers.cpp
blobfb4b9a0b110db553845be1b2d4abe4f245e9b41e
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.
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.
30 extern "C" {
31 #include <sys/time.h>
34 #include <cassert>
35 #include <cerrno>
36 #include <csignal>
37 #include <ctime>
39 #include "exceptions.hpp"
40 #include "signals.hpp"
41 #include "timers.hpp"
43 namespace impl = tools::timers;
44 #define IMPL_NAME "tools::timers"
46 // ------------------------------------------------------------------------
47 // Auxiliary functions.
48 // ------------------------------------------------------------------------
50 static
51 void
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);
56 timer->set_fired();
57 timer->timeout_callback();
60 // ------------------------------------------------------------------------
61 // The "timer" class.
62 // ------------------------------------------------------------------------
64 struct impl::timer::impl {
65 ::timer_t m_timer;
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) :
77 m_pimpl(new impl())
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);
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);
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)
117 int ret;
119 ret =
120 #endif /* !defined(NDEBUG) && defined(__minix) */
121 ::timer_delete(m_pimpl->m_timer);
122 assert(ret != -1);
124 #if !defined(NDEBUG) && defined(__minix)
125 ret =
126 #endif /* !defined(NDEBUG) && defined(__minix) */
127 ::sigaction(SIGALRM, &m_pimpl->m_old_sa, NULL);
128 assert(ret != -1);
131 bool
132 impl::timer::fired(void)
133 const
135 return m_pimpl->m_fired;
138 void
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) :
150 timer(seconds),
151 m_pid(pid),
152 m_terminate(terminate)
156 impl::child_timer::~child_timer(void)
160 void
161 impl::child_timer::timeout_callback(void)
163 static const timespec ts = { 1, 0 };
164 m_terminate = true;
165 ::kill(-m_pid, SIGTERM);
166 ::nanosleep(&ts, NULL);
167 if (::kill(-m_pid, 0) != -1)
168 ::kill(-m_pid, SIGKILL);