1b0d29bc4SBrooks Davis// Copyright 2010 The Kyua Authors. 2b0d29bc4SBrooks Davis// All rights reserved. 3b0d29bc4SBrooks Davis// 4b0d29bc4SBrooks Davis// Redistribution and use in source and binary forms, with or without 5b0d29bc4SBrooks Davis// modification, are permitted provided that the following conditions are 6b0d29bc4SBrooks Davis// met: 7b0d29bc4SBrooks Davis// 8b0d29bc4SBrooks Davis// * Redistributions of source code must retain the above copyright 9b0d29bc4SBrooks Davis// notice, this list of conditions and the following disclaimer. 10b0d29bc4SBrooks Davis// * Redistributions in binary form must reproduce the above copyright 11b0d29bc4SBrooks Davis// notice, this list of conditions and the following disclaimer in the 12b0d29bc4SBrooks Davis// documentation and/or other materials provided with the distribution. 13b0d29bc4SBrooks Davis// * Neither the name of Google Inc. nor the names of its contributors 14b0d29bc4SBrooks Davis// may be used to endorse or promote products derived from this software 15b0d29bc4SBrooks Davis// without specific prior written permission. 16b0d29bc4SBrooks Davis// 17b0d29bc4SBrooks Davis// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18b0d29bc4SBrooks Davis// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19b0d29bc4SBrooks Davis// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20b0d29bc4SBrooks Davis// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21b0d29bc4SBrooks Davis// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22b0d29bc4SBrooks Davis// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23b0d29bc4SBrooks Davis// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24b0d29bc4SBrooks Davis// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25b0d29bc4SBrooks Davis// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26b0d29bc4SBrooks Davis// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27b0d29bc4SBrooks Davis// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28b0d29bc4SBrooks Davis 29b0d29bc4SBrooks Davis#if !defined(UTILS_PROCESS_CHILD_IPP) 30b0d29bc4SBrooks Davis#define UTILS_PROCESS_CHILD_IPP 31b0d29bc4SBrooks Davis 32b0d29bc4SBrooks Davis#include <cstdlib> 33b0d29bc4SBrooks Davis 34b0d29bc4SBrooks Davis#include "utils/process/child.hpp" 35b0d29bc4SBrooks Davis 36b0d29bc4SBrooks Davisnamespace utils { 37b0d29bc4SBrooks Davisnamespace process { 38b0d29bc4SBrooks Davis 39b0d29bc4SBrooks Davis 40b0d29bc4SBrooks Davis/// Spawns a new subprocess and redirects its stdout and stderr to files. 41b0d29bc4SBrooks Davis/// 42b0d29bc4SBrooks Davis/// If the subprocess cannot be completely set up for any reason, it attempts to 43b0d29bc4SBrooks Davis/// dump an error message to its stderr channel and it then calls std::abort(). 44b0d29bc4SBrooks Davis/// 45b0d29bc4SBrooks Davis/// \param hook The function to execute in the subprocess. Must not return. 46b0d29bc4SBrooks Davis/// \param stdout_file The name of the file in which to store the stdout. 47b0d29bc4SBrooks Davis/// \param stderr_file The name of the file in which to store the stderr. 48b0d29bc4SBrooks Davis/// 49b0d29bc4SBrooks Davis/// \return A new child object, returned as a dynamically-allocated object 50b0d29bc4SBrooks Davis/// because children classes are unique and thus noncopyable. 51b0d29bc4SBrooks Davis/// 52b0d29bc4SBrooks Davis/// \throw process::system_error If the process cannot be spawned due to a 53b0d29bc4SBrooks Davis/// system call error. 54b0d29bc4SBrooks Davistemplate< typename Hook > 55*b392a90bSJohn Baldwinstd::unique_ptr< child > 56b0d29bc4SBrooks Davischild::fork_files(Hook hook, const fs::path& stdout_file, 57b0d29bc4SBrooks Davis const fs::path& stderr_file) 58b0d29bc4SBrooks Davis{ 59*b392a90bSJohn Baldwin std::unique_ptr< child > child = fork_files_aux(stdout_file, stderr_file); 60b0d29bc4SBrooks Davis if (child.get() == NULL) { 61b0d29bc4SBrooks Davis try { 62b0d29bc4SBrooks Davis hook(); 63b0d29bc4SBrooks Davis std::abort(); 64b0d29bc4SBrooks Davis } catch (const std::runtime_error& e) { 65b0d29bc4SBrooks Davis detail::report_error_and_abort(e); 66b0d29bc4SBrooks Davis } catch (...) { 67b0d29bc4SBrooks Davis detail::report_error_and_abort(); 68b0d29bc4SBrooks Davis } 69b0d29bc4SBrooks Davis } 70b0d29bc4SBrooks Davis 71b0d29bc4SBrooks Davis return child; 72b0d29bc4SBrooks Davis} 73b0d29bc4SBrooks Davis 74b0d29bc4SBrooks Davis 75b0d29bc4SBrooks Davis/// Spawns a new subprocess and multiplexes and captures its stdout and stderr. 76b0d29bc4SBrooks Davis/// 77b0d29bc4SBrooks Davis/// If the subprocess cannot be completely set up for any reason, it attempts to 78b0d29bc4SBrooks Davis/// dump an error message to its stderr channel and it then calls std::abort(). 79b0d29bc4SBrooks Davis/// 80b0d29bc4SBrooks Davis/// \param hook The function to execute in the subprocess. Must not return. 81b0d29bc4SBrooks Davis/// 82b0d29bc4SBrooks Davis/// \return A new child object, returned as a dynamically-allocated object 83b0d29bc4SBrooks Davis/// because children classes are unique and thus noncopyable. 84b0d29bc4SBrooks Davis/// 85b0d29bc4SBrooks Davis/// \throw process::system_error If the process cannot be spawned due to a 86b0d29bc4SBrooks Davis/// system call error. 87b0d29bc4SBrooks Davistemplate< typename Hook > 88*b392a90bSJohn Baldwinstd::unique_ptr< child > 89b0d29bc4SBrooks Davischild::fork_capture(Hook hook) 90b0d29bc4SBrooks Davis{ 91*b392a90bSJohn Baldwin std::unique_ptr< child > child = fork_capture_aux(); 92b0d29bc4SBrooks Davis if (child.get() == NULL) { 93b0d29bc4SBrooks Davis try { 94b0d29bc4SBrooks Davis hook(); 95b0d29bc4SBrooks Davis std::abort(); 96b0d29bc4SBrooks Davis } catch (const std::runtime_error& e) { 97b0d29bc4SBrooks Davis detail::report_error_and_abort(e); 98b0d29bc4SBrooks Davis } catch (...) { 99b0d29bc4SBrooks Davis detail::report_error_and_abort(); 100b0d29bc4SBrooks Davis } 101b0d29bc4SBrooks Davis } 102b0d29bc4SBrooks Davis 103b0d29bc4SBrooks Davis return child; 104b0d29bc4SBrooks Davis} 105b0d29bc4SBrooks Davis 106b0d29bc4SBrooks Davis 107b0d29bc4SBrooks Davis} // namespace process 108b0d29bc4SBrooks Davis} // namespace utils 109b0d29bc4SBrooks Davis 110b0d29bc4SBrooks Davis#endif // !defined(UTILS_PROCESS_CHILD_IPP) 111