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#if !defined(UTILS_PROCESS_CHILD_IPP) 30#define UTILS_PROCESS_CHILD_IPP 31 32#include <cstdlib> 33 34#include "utils/process/child.hpp" 35 36namespace utils { 37namespace 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. 54template< typename Hook > 55std::unique_ptr< child > 56child::fork_files(Hook hook, const fs::path& stdout_file, 57 const fs::path& stderr_file) 58{ 59 std::unique_ptr< child > child = fork_files_aux(stdout_file, stderr_file); 60 if (child.get() == NULL) { 61 try { 62 hook(); 63 std::abort(); 64 } catch (...) { 65 std::abort(); 66 } 67 } 68 69 return child; 70} 71 72 73/// Spawns a new subprocess and multiplexes and captures its stdout and stderr. 74/// 75/// If the subprocess cannot be completely set up for any reason, it attempts to 76/// dump an error message to its stderr channel and it then calls std::abort(). 77/// 78/// \param hook The function to execute in the subprocess. Must not return. 79/// 80/// \return A new child object, returned as a dynamically-allocated object 81/// because children classes are unique and thus noncopyable. 82/// 83/// \throw process::system_error If the process cannot be spawned due to a 84/// system call error. 85template< typename Hook > 86std::unique_ptr< child > 87child::fork_capture(Hook hook) 88{ 89 std::unique_ptr< child > child = fork_capture_aux(); 90 if (child.get() == NULL) { 91 try { 92 hook(); 93 std::abort(); 94 } catch (...) { 95 std::abort(); 96 } 97 } 98 99 return child; 100} 101 102 103} // namespace process 104} // namespace utils 105 106#endif // !defined(UTILS_PROCESS_CHILD_IPP) 107