1 // Copyright 2010 Google Inc.
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/signals/exceptions.hpp"
44
45 namespace fs = utils::fs;
46 namespace process = utils::process;
47 namespace signals = utils::signals;
48
49
50 namespace {
51
52
53 static void program_reset_raise(void) UTILS_NORETURN;
54
55
56 /// Body of a subprocess that tests the signal::reset function.
57 ///
58 /// This function programs a signal to be ignored, then uses signal::reset to
59 /// bring it back to its default handler and then delivers the signal to self.
60 /// The default behavior of the signal is for the process to die, so this
61 /// function should never return correctly (and thus the child process should
62 /// always die due to a signal if all goes well).
63 static void
program_reset_raise(void)64 program_reset_raise(void)
65 {
66 struct ::sigaction sa;
67 sa.sa_handler = SIG_IGN;
68 sigemptyset(&sa.sa_mask);
69 sa.sa_flags = 0;
70 if (::sigaction(SIGUSR1, &sa, NULL) == -1)
71 std::exit(EXIT_FAILURE);
72
73 signals::reset(SIGUSR1);
74 ::kill(::getpid(), SIGUSR1);
75
76 // Should not be reached, but we do not assert this condition because we
77 // want to exit cleanly if the signal does not abort our execution to let
78 // the parent easily know what happened.
79 std::exit(EXIT_SUCCESS);
80 }
81
82
83 } // anonymous namespace
84
85
86 ATF_TEST_CASE_WITHOUT_HEAD(reset__ok);
ATF_TEST_CASE_BODY(reset__ok)87 ATF_TEST_CASE_BODY(reset__ok)
88 {
89 // TODO(jmmv): We should have a child type that inherits both stdout and
90 // stderr so that we do not have to specify files.
91 std::auto_ptr< process::child > child = process::child::fork_files(
92 program_reset_raise, fs::path("stdout.txt"), fs::path("stderr.txt"));
93 process::status status = child->wait();
94 ATF_REQUIRE(status.signaled());
95 ATF_REQUIRE_EQ(SIGUSR1, status.termsig());
96 }
97
98
99 ATF_TEST_CASE_WITHOUT_HEAD(reset__invalid);
ATF_TEST_CASE_BODY(reset__invalid)100 ATF_TEST_CASE_BODY(reset__invalid)
101 {
102 ATF_REQUIRE_THROW(signals::system_error, signals::reset(-1));
103 }
104
105
ATF_INIT_TEST_CASES(tcs)106 ATF_INIT_TEST_CASES(tcs)
107 {
108 ATF_ADD_TEST_CASE(tcs, reset__ok);
109 ATF_ADD_TEST_CASE(tcs, reset__invalid);
110 }
111