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 sa.sa_flags = SA_SIGINFO; 82 sa.sa_sigaction = ::handler; 83 if (::sigaction(SIGALRM, &sa, &m_pimpl->m_old_sa) == -1) 84 throw tools::system_error(IMPL_NAME "::timer::timer", 85 "Failed to set signal handler", errno); 86 87 struct ::sigevent se; 88 se.sigev_notify = SIGEV_SIGNAL; 89 se.sigev_signo = SIGALRM; 90 se.sigev_value.sival_ptr = static_cast< void* >(this); 91 se.sigev_notify_function = NULL; 92 se.sigev_notify_attributes = NULL; 93 if (::timer_create(CLOCK_MONOTONIC, &se, &m_pimpl->m_timer) == -1) { 94 ::sigaction(SIGALRM, &m_pimpl->m_old_sa, NULL); 95 throw tools::system_error(IMPL_NAME "::timer::timer", 96 "Failed to create timer", errno); 97 } 98 99 struct ::itimerspec it; 100 it.it_interval.tv_sec = 0; 101 it.it_interval.tv_nsec = 0; 102 it.it_value.tv_sec = seconds; 103 it.it_value.tv_nsec = 0; 104 if (::timer_settime(m_pimpl->m_timer, 0, &it, &m_pimpl->m_old_it) == -1) { 105 ::sigaction(SIGALRM, &m_pimpl->m_old_sa, NULL); 106 ::timer_delete(m_pimpl->m_timer); 107 throw tools::system_error(IMPL_NAME "::timer::timer", 108 "Failed to program timer", errno); 109 } 110 } 111 112 impl::timer::~timer(void) 113 { 114 int ret; 115 116 ret = ::timer_delete(m_pimpl->m_timer); 117 assert(ret != -1); 118 119 ret = ::sigaction(SIGALRM, &m_pimpl->m_old_sa, NULL); 120 assert(ret != -1); 121 } 122 123 bool 124 impl::timer::fired(void) 125 const 126 { 127 return m_pimpl->m_fired; 128 } 129 130 void 131 impl::timer::set_fired(void) 132 { 133 m_pimpl->m_fired = true; 134 } 135 136 // ------------------------------------------------------------------------ 137 // The "child_timer" class. 138 // ------------------------------------------------------------------------ 139 140 impl::child_timer::child_timer(const unsigned int seconds, const pid_t pid, 141 volatile bool& terminate) : 142 timer(seconds), 143 m_pid(pid), 144 m_terminate(terminate) 145 { 146 } 147 148 impl::child_timer::~child_timer(void) 149 { 150 } 151 152 void 153 impl::child_timer::timeout_callback(void) 154 { 155 static const timespec ts = { 1, 0 }; 156 m_terminate = true; 157 ::kill(-m_pid, SIGTERM); 158 ::nanosleep(&ts, NULL); 159 if (::kill(-m_pid, 0) != -1) 160 ::kill(-m_pid, SIGKILL); 161 } 162