xref: /minix3/external/bsd/kyua-cli/dist/utils/signals/interrupts_test.cpp (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc // Copyright 2012 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc //   documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc //   may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc //   without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc #include "utils/signals/interrupts.hpp"
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc extern "C" {
32*11be35a1SLionel Sambuc #include <signal.h>
33*11be35a1SLionel Sambuc #include <unistd.h>
34*11be35a1SLionel Sambuc }
35*11be35a1SLionel Sambuc 
36*11be35a1SLionel Sambuc #include <cstdlib>
37*11be35a1SLionel Sambuc 
38*11be35a1SLionel Sambuc #include <atf-c++.hpp>
39*11be35a1SLionel Sambuc 
40*11be35a1SLionel Sambuc #include "utils/format/macros.hpp"
41*11be35a1SLionel Sambuc #include "utils/process/child.ipp"
42*11be35a1SLionel Sambuc #include "utils/process/status.hpp"
43*11be35a1SLionel Sambuc #include "utils/signals/exceptions.hpp"
44*11be35a1SLionel Sambuc #include "utils/signals/programmer.hpp"
45*11be35a1SLionel Sambuc 
46*11be35a1SLionel Sambuc namespace process = utils::process;
47*11be35a1SLionel Sambuc namespace signals = utils::signals;
48*11be35a1SLionel Sambuc 
49*11be35a1SLionel Sambuc 
50*11be35a1SLionel Sambuc namespace {
51*11be35a1SLionel Sambuc 
52*11be35a1SLionel Sambuc 
53*11be35a1SLionel Sambuc /// Set to the signal that fired; -1 if none.
54*11be35a1SLionel Sambuc static volatile int fired_signal = -1;
55*11be35a1SLionel Sambuc 
56*11be35a1SLionel Sambuc 
57*11be35a1SLionel Sambuc /// Test handler for signals.
58*11be35a1SLionel Sambuc ///
59*11be35a1SLionel Sambuc /// \post fired_signal is set to the signal that triggered the handler.
60*11be35a1SLionel Sambuc ///
61*11be35a1SLionel Sambuc /// \param signo The signal that triggered the handler.
62*11be35a1SLionel Sambuc static void
signal_handler(const int signo)63*11be35a1SLionel Sambuc signal_handler(const int signo)
64*11be35a1SLionel Sambuc {
65*11be35a1SLionel Sambuc     fired_signal = signo;
66*11be35a1SLionel Sambuc }
67*11be35a1SLionel Sambuc 
68*11be35a1SLionel Sambuc 
69*11be35a1SLionel Sambuc /// Child process that pauses waiting to be killed.
70*11be35a1SLionel Sambuc static void
pause_child(void)71*11be35a1SLionel Sambuc pause_child(void)
72*11be35a1SLionel Sambuc {
73*11be35a1SLionel Sambuc     sigset_t mask;
74*11be35a1SLionel Sambuc     sigemptyset(&mask);
75*11be35a1SLionel Sambuc     if (sigsuspend(&mask) == -1)
76*11be35a1SLionel Sambuc         ::exit(EXIT_FAILURE);
77*11be35a1SLionel Sambuc     else {
78*11be35a1SLionel Sambuc         // If this happens, it is because we received a non-deadly signal and
79*11be35a1SLionel Sambuc         // the execution resumed.  This is not what we expect, so exit with an
80*11be35a1SLionel Sambuc         // arbitrary code.
81*11be35a1SLionel Sambuc         ::exit(45);
82*11be35a1SLionel Sambuc     }
83*11be35a1SLionel Sambuc }
84*11be35a1SLionel Sambuc 
85*11be35a1SLionel Sambuc 
86*11be35a1SLionel Sambuc /// Checks that interrupts_handler() handles a particular signal.
87*11be35a1SLionel Sambuc ///
88*11be35a1SLionel Sambuc /// This indirectly checks the check_interrupt() function, which is not part of
89*11be35a1SLionel Sambuc /// the class but is tightly related.
90*11be35a1SLionel Sambuc ///
91*11be35a1SLionel Sambuc /// \param signo The signal to check.
92*11be35a1SLionel Sambuc static void
check_interrupts_handler(const int signo)93*11be35a1SLionel Sambuc check_interrupts_handler(const int signo)
94*11be35a1SLionel Sambuc {
95*11be35a1SLionel Sambuc     signals::programmer test_handler(signo, signal_handler);
96*11be35a1SLionel Sambuc 
97*11be35a1SLionel Sambuc     {
98*11be35a1SLionel Sambuc         signals::interrupts_handler interrupts;
99*11be35a1SLionel Sambuc 
100*11be35a1SLionel Sambuc         signals::check_interrupt();
101*11be35a1SLionel Sambuc         ::kill(getpid(), signo);
102*11be35a1SLionel Sambuc         ATF_REQUIRE_THROW_RE(signals::interrupted_error,
103*11be35a1SLionel Sambuc                              F("Interrupted by signal %s") % signo,
104*11be35a1SLionel Sambuc                              signals::check_interrupt());
105*11be35a1SLionel Sambuc     }
106*11be35a1SLionel Sambuc 
107*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(-1, fired_signal);
108*11be35a1SLionel Sambuc     ::kill(getpid(), signo);
109*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(signo, fired_signal);
110*11be35a1SLionel Sambuc 
111*11be35a1SLionel Sambuc     test_handler.unprogram();
112*11be35a1SLionel Sambuc }
113*11be35a1SLionel Sambuc 
114*11be35a1SLionel Sambuc 
115*11be35a1SLionel Sambuc /// Checks that interrupts_inhibiter() handles a particular signal.
116*11be35a1SLionel Sambuc ///
117*11be35a1SLionel Sambuc /// \param signo The signal to check.
118*11be35a1SLionel Sambuc static void
check_interrupts_inhibiter(const int signo)119*11be35a1SLionel Sambuc check_interrupts_inhibiter(const int signo)
120*11be35a1SLionel Sambuc {
121*11be35a1SLionel Sambuc     signals::programmer test_handler(signo, signal_handler);
122*11be35a1SLionel Sambuc 
123*11be35a1SLionel Sambuc     {
124*11be35a1SLionel Sambuc         signals::interrupts_inhibiter inhibiter;
125*11be35a1SLionel Sambuc         ::kill(::getpid(), signo);
126*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(-1, fired_signal);
127*11be35a1SLionel Sambuc     }
128*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(signo, fired_signal);
129*11be35a1SLionel Sambuc 
130*11be35a1SLionel Sambuc     test_handler.unprogram();
131*11be35a1SLionel Sambuc }
132*11be35a1SLionel Sambuc 
133*11be35a1SLionel Sambuc 
134*11be35a1SLionel Sambuc }  // anonymous namespace
135*11be35a1SLionel Sambuc 
136*11be35a1SLionel Sambuc 
137*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(interrupts_handler__sighup);
ATF_TEST_CASE_BODY(interrupts_handler__sighup)138*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(interrupts_handler__sighup)
139*11be35a1SLionel Sambuc {
140*11be35a1SLionel Sambuc     check_interrupts_handler(SIGHUP);
141*11be35a1SLionel Sambuc }
142*11be35a1SLionel Sambuc 
143*11be35a1SLionel Sambuc 
144*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(interrupts_handler__sigint);
ATF_TEST_CASE_BODY(interrupts_handler__sigint)145*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(interrupts_handler__sigint)
146*11be35a1SLionel Sambuc {
147*11be35a1SLionel Sambuc     check_interrupts_handler(SIGINT);
148*11be35a1SLionel Sambuc }
149*11be35a1SLionel Sambuc 
150*11be35a1SLionel Sambuc 
151*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(interrupts_handler__sigterm);
ATF_TEST_CASE_BODY(interrupts_handler__sigterm)152*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(interrupts_handler__sigterm)
153*11be35a1SLionel Sambuc {
154*11be35a1SLionel Sambuc     check_interrupts_handler(SIGTERM);
155*11be35a1SLionel Sambuc }
156*11be35a1SLionel Sambuc 
157*11be35a1SLionel Sambuc 
158*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(interrupts_handler__kill_children);
ATF_TEST_CASE_BODY(interrupts_handler__kill_children)159*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(interrupts_handler__kill_children)
160*11be35a1SLionel Sambuc {
161*11be35a1SLionel Sambuc     std::auto_ptr< process::child > child1(process::child::fork_capture(
162*11be35a1SLionel Sambuc          pause_child));
163*11be35a1SLionel Sambuc     std::auto_ptr< process::child > child2(process::child::fork_capture(
164*11be35a1SLionel Sambuc          pause_child));
165*11be35a1SLionel Sambuc 
166*11be35a1SLionel Sambuc     signals::interrupts_handler interrupts;
167*11be35a1SLionel Sambuc 
168*11be35a1SLionel Sambuc     // Our children pause until the reception of a signal.  Interrupting
169*11be35a1SLionel Sambuc     // ourselves will cause the signal to be re-delivered to our children due to
170*11be35a1SLionel Sambuc     // the interrupts_handler semantics.  If this does not happen, the wait
171*11be35a1SLionel Sambuc     // calls below would block indefinitely and cause our test to time out.
172*11be35a1SLionel Sambuc     ::kill(::getpid(), SIGHUP);
173*11be35a1SLionel Sambuc 
174*11be35a1SLionel Sambuc     const process::status status1 = child1->wait();
175*11be35a1SLionel Sambuc     ATF_REQUIRE(status1.signaled());
176*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(SIGHUP, status1.termsig());
177*11be35a1SLionel Sambuc     const process::status status2 = child2->wait();
178*11be35a1SLionel Sambuc     ATF_REQUIRE(status2.signaled());
179*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(SIGHUP, status2.termsig());
180*11be35a1SLionel Sambuc }
181*11be35a1SLionel Sambuc 
182*11be35a1SLionel Sambuc 
183*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(interrupts_inhibiter__sighup);
ATF_TEST_CASE_BODY(interrupts_inhibiter__sighup)184*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(interrupts_inhibiter__sighup)
185*11be35a1SLionel Sambuc {
186*11be35a1SLionel Sambuc     check_interrupts_inhibiter(SIGHUP);
187*11be35a1SLionel Sambuc }
188*11be35a1SLionel Sambuc 
189*11be35a1SLionel Sambuc 
190*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(interrupts_inhibiter__sigint);
ATF_TEST_CASE_BODY(interrupts_inhibiter__sigint)191*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(interrupts_inhibiter__sigint)
192*11be35a1SLionel Sambuc {
193*11be35a1SLionel Sambuc     check_interrupts_inhibiter(SIGINT);
194*11be35a1SLionel Sambuc }
195*11be35a1SLionel Sambuc 
196*11be35a1SLionel Sambuc 
197*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(interrupts_inhibiter__sigterm);
ATF_TEST_CASE_BODY(interrupts_inhibiter__sigterm)198*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(interrupts_inhibiter__sigterm)
199*11be35a1SLionel Sambuc {
200*11be35a1SLionel Sambuc     check_interrupts_inhibiter(SIGTERM);
201*11be35a1SLionel Sambuc }
202*11be35a1SLionel Sambuc 
203*11be35a1SLionel Sambuc 
ATF_INIT_TEST_CASES(tcs)204*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
205*11be35a1SLionel Sambuc {
206*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, interrupts_handler__sighup);
207*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, interrupts_handler__sigint);
208*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, interrupts_handler__sigterm);
209*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, interrupts_handler__kill_children);
210*11be35a1SLionel Sambuc 
211*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, interrupts_inhibiter__sighup);
212*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, interrupts_inhibiter__sigint);
213*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, interrupts_inhibiter__sigterm);
214*11be35a1SLionel Sambuc }
215