1 // Copyright 2010 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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.
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.
29 #if !defined(UTILS_PROCESS_CHILD_IPP)
30 #define UTILS_PROCESS_CHILD_IPP
34 #include "utils/process/child.hpp"
40 /// Spawns a new subprocess and redirects its stdout and stderr to files.
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().
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.
49 /// \return A new child object, returned as a dynamically-allocated object
50 /// because children classes are unique and thus noncopyable.
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)
59 std::auto_ptr< child > child = fork_files_aux(stdout_file, stderr_file);
60 if (child.get() == NULL) {
64 } catch (const std::runtime_error& e) {
65 detail::report_error_and_abort(e);
67 detail::report_error_and_abort();
75 /// Spawns a new subprocess and multiplexes and captures its stdout and stderr.
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().
80 /// \param hook The function to execute in the subprocess. Must not return.
82 /// \return A new child object, returned as a dynamically-allocated object
83 /// because children classes are unique and thus noncopyable.
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)
91 std::auto_ptr< child > child = fork_capture_aux();
92 if (child.get() == NULL) {
96 } catch (const std::runtime_error& e) {
97 detail::report_error_and_abort(e);
99 detail::report_error_and_abort();
107 } // namespace process
110 #endif // !defined(UTILS_PROCESS_CHILD_IPP)