1 // Copyright 2010 The Kyua Authors.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 //   notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 //   notice, this list of conditions and the following disclaimer in the
12 //   documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 //   may be used to endorse or promote products derived from this software
15 //   without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #include "utils/signals/misc.hpp"
30 
31 extern "C" {
32 #include <signal.h>
33 #include <unistd.h>
34 }
35 
36 #include <cstdlib>
37 
38 #include <atf-c++.hpp>
39 
40 #include "utils/defs.hpp"
41 #include "utils/fs/path.hpp"
42 #include "utils/process/child.ipp"
43 #include "utils/process/status.hpp"
44 #include "utils/signals/exceptions.hpp"
45 
46 namespace fs = utils::fs;
47 namespace process = utils::process;
48 namespace signals = utils::signals;
49 
50 
51 namespace {
52 
53 
54 static void program_reset_raise(void) UTILS_NORETURN;
55 
56 
57 /// Body of a subprocess that tests the signals::reset function.
58 ///
59 /// This function programs a signal to be ignored, then uses signal::reset to
60 /// bring it back to its default handler and then delivers the signal to self.
61 /// The default behavior of the signal is for the process to die, so this
62 /// function should never return correctly (and thus the child process should
63 /// always die due to a signal if all goes well).
64 static void
65 program_reset_raise(void)
66 {
67     struct ::sigaction sa;
68     sa.sa_handler = SIG_IGN;
69     sigemptyset(&sa.sa_mask);
70     sa.sa_flags = 0;
71     if (::sigaction(SIGUSR1, &sa, NULL) == -1)
72         std::exit(EXIT_FAILURE);
73 
74     signals::reset(SIGUSR1);
75     ::kill(::getpid(), SIGUSR1);
76 
77     // Should not be reached, but we do not assert this condition because we
78     // want to exit cleanly if the signal does not abort our execution to let
79     // the parent easily know what happened.
80     std::exit(EXIT_SUCCESS);
81 }
82 
83 
84 /// Body of a subprocess that executes the signals::reset_all function.
85 ///
86 /// The process exits with success if the function worked, or with a failure if
87 /// an error is reported.  No signals are tested.
88 static void
89 run_reset_all(void)
90 {
91     const bool ok = signals::reset_all();
92     std::exit(ok ? EXIT_SUCCESS : EXIT_FAILURE);
93 }
94 
95 
96 }  // anonymous namespace
97 
98 
99 ATF_TEST_CASE_WITHOUT_HEAD(reset__ok);
100 ATF_TEST_CASE_BODY(reset__ok)
101 {
102     std::auto_ptr< process::child > child = process::child::fork_files(
103         program_reset_raise, fs::path("/dev/stdout"), fs::path("/dev/stderr"));
104     process::status status = child->wait();
105     ATF_REQUIRE(status.signaled());
106     ATF_REQUIRE_EQ(SIGUSR1, status.termsig());
107 }
108 
109 
110 ATF_TEST_CASE_WITHOUT_HEAD(reset__invalid);
111 ATF_TEST_CASE_BODY(reset__invalid)
112 {
113     ATF_REQUIRE_THROW(signals::system_error, signals::reset(-1));
114 }
115 
116 
117 ATF_TEST_CASE_WITHOUT_HEAD(reset_all);
118 ATF_TEST_CASE_BODY(reset_all)
119 {
120     std::auto_ptr< process::child > child = process::child::fork_files(
121         run_reset_all, fs::path("/dev/stdout"), fs::path("/dev/stderr"));
122     process::status status = child->wait();
123     ATF_REQUIRE(status.exited());
124     ATF_REQUIRE_EQ(EXIT_SUCCESS, status.exitstatus());
125 }
126 
127 
128 ATF_INIT_TEST_CASES(tcs)
129 {
130     ATF_ADD_TEST_CASE(tcs, reset__ok);
131     ATF_ADD_TEST_CASE(tcs, reset__invalid);
132     ATF_ADD_TEST_CASE(tcs, reset_all);
133 }
134