00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00018 #ifndef BOOST_PROCESS_CHILD_HPP
00019 #define BOOST_PROCESS_CHILD_HPP
00020
00021 #include <boost/process/config.hpp>
00022
00023 #if defined(BOOST_POSIX_API)
00024 # include <sys/types.h>
00025 # include <sys/wait.h>
00026 # include <cerrno>
00027 #elif defined(BOOST_WINDOWS_API)
00028 # include <windows.h>
00029 #else
00030 # error "Unsupported platform."
00031 #endif
00032
00033 #include <boost/process/process.hpp>
00034 #include <boost/process/pistream.hpp>
00035 #include <boost/process/postream.hpp>
00036 #include <boost/process/status.hpp>
00037 #include <boost/process/detail/file_handle.hpp>
00038 #include <boost/system/system_error.hpp>
00039 #include <boost/throw_exception.hpp>
00040 #include <boost/shared_ptr.hpp>
00041 #include <boost/assert.hpp>
00042 #include <vector>
00043
00044 namespace boost {
00045 namespace process {
00046
00053 class child : public process
00054 {
00055 public:
00062 postream &get_stdin() const
00063 {
00064 BOOST_ASSERT(stdin_);
00065
00066 return *stdin_;
00067 }
00068
00075 pistream &get_stdout() const
00076 {
00077 BOOST_ASSERT(stdout_);
00078
00079 return *stdout_;
00080 }
00081
00088 pistream &get_stderr() const
00089 {
00090 BOOST_ASSERT(stderr_);
00091
00092 return *stderr_;
00093 }
00094
00106 status wait()
00107 {
00108 #if defined(BOOST_POSIX_API)
00109 int s;
00110 if (::waitpid(get_id(), &s, 0) == -1)
00111 boost::throw_exception(boost::system::system_error(boost::system::error_code(errno, boost::system::get_system_category()), "boost::process::child::wait: waitpid(2) failed"));
00112 return status(s);
00113 #elif defined(BOOST_WINDOWS_API)
00114 ::WaitForSingleObject(process_handle_.get(), INFINITE);
00115 DWORD code;
00116 if (!::GetExitCodeProcess(process_handle_.get(), &code))
00117 boost::throw_exception(boost::system::system_error(boost::system::error_code(::GetLastError(), boost::system::get_system_category()), "boost::process::child::wait: GetExitCodeProcess failed"));
00118 return status(code);
00119 #endif
00120 }
00121
00136 child(id_type id, detail::file_handle fhstdin, detail::file_handle fhstdout, detail::file_handle fhstderr, detail::file_handle fhprocess = detail::file_handle())
00137 : process(id)
00138 #if defined(BOOST_WINDOWS_API)
00139 , process_handle_(fhprocess)
00140 #endif
00141 {
00142 if (fhstdin.valid())
00143 stdin_.reset(new postream(fhstdin));
00144 if (fhstdout.valid())
00145 stdout_.reset(new pistream(fhstdout));
00146 if (fhstderr.valid())
00147 stderr_.reset(new pistream(fhstderr));
00148 }
00149
00150 private:
00159 boost::shared_ptr<postream> stdin_;
00160
00169 boost::shared_ptr<pistream> stdout_;
00170
00179 boost::shared_ptr<pistream> stderr_;
00180
00181 #if defined(BOOST_WINDOWS_API)
00182
00185 detail::file_handle process_handle_;
00186 #endif
00187 };
00188
00195 typedef std::vector<child> children;
00196
00197 }
00198 }
00199
00200 #endif