]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/kyua/utils/process/child.ipp
Import the kyua test framework.
[FreeBSD/FreeBSD.git] / contrib / kyua / utils / process / child.ipp
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 #if !defined(UTILS_PROCESS_CHILD_IPP)
30 #define UTILS_PROCESS_CHILD_IPP
31
32 #include <cstdlib>
33
34 #include "utils/process/child.hpp"
35
36 namespace utils {
37 namespace process {
38
39
40 /// Spawns a new subprocess and redirects its stdout and stderr to files.
41 ///
42 /// If the subprocess cannot be completely set up for any reason, it attempts to
43 /// dump an error message to its stderr channel and it then calls std::abort().
44 ///
45 /// \param hook The function to execute in the subprocess.  Must not return.
46 /// \param stdout_file The name of the file in which to store the stdout.
47 /// \param stderr_file The name of the file in which to store the stderr.
48 ///
49 /// \return A new child object, returned as a dynamically-allocated object
50 /// because children classes are unique and thus noncopyable.
51 ///
52 /// \throw process::system_error If the process cannot be spawned due to a
53 ///     system call error.
54 template< typename Hook >
55 std::auto_ptr< child >
56 child::fork_files(Hook hook, const fs::path& stdout_file,
57                   const fs::path& stderr_file)
58 {
59     std::auto_ptr< child > child = fork_files_aux(stdout_file, stderr_file);
60     if (child.get() == NULL) {
61         try {
62             hook();
63             std::abort();
64         } catch (const std::runtime_error& e) {
65             detail::report_error_and_abort(e);
66         } catch (...) {
67             detail::report_error_and_abort();
68         }
69     }
70
71     return child;
72 }
73
74
75 /// Spawns a new subprocess and multiplexes and captures its stdout and stderr.
76 ///
77 /// If the subprocess cannot be completely set up for any reason, it attempts to
78 /// dump an error message to its stderr channel and it then calls std::abort().
79 ///
80 /// \param hook The function to execute in the subprocess.  Must not return.
81 ///
82 /// \return A new child object, returned as a dynamically-allocated object
83 /// because children classes are unique and thus noncopyable.
84 ///
85 /// \throw process::system_error If the process cannot be spawned due to a
86 ///     system call error.
87 template< typename Hook >
88 std::auto_ptr< child >
89 child::fork_capture(Hook hook)
90 {
91     std::auto_ptr< child > child = fork_capture_aux();
92     if (child.get() == NULL) {
93         try {
94             hook();
95             std::abort();
96         } catch (const std::runtime_error& e) {
97             detail::report_error_and_abort(e);
98         } catch (...) {
99             detail::report_error_and_abort();
100         }
101     }
102
103     return child;
104 }
105
106
107 }  // namespace process
108 }  // namespace utils
109
110 #endif  // !defined(UTILS_PROCESS_CHILD_IPP)