File indexing completed on 2025-01-18 09:50:06
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_PROCESS_DETAIL_POSIX_PIPE_OUT_HPP
0012 #define BOOST_PROCESS_DETAIL_POSIX_PIPE_OUT_HPP
0013
0014 #include <boost/process/pipe.hpp>
0015 #include <boost/process/detail/posix/handler.hpp>
0016 #include <unistd.h>
0017
0018 namespace boost { namespace process { namespace detail { namespace posix {
0019
0020 template<int p1, int p2>
0021 struct pipe_out : handler_base_ext
0022 {
0023 int sink;
0024 int source;
0025
0026 pipe_out(int sink, int source) : sink(sink), source(source) {}
0027
0028 template<typename T>
0029 pipe_out(T & p) : sink(p.native_sink()), source(p.native_source())
0030 {
0031 p.assign_sink(-1);
0032 }
0033
0034 template<typename Executor>
0035 void on_error(Executor &, const std::error_code &) const
0036 {
0037 ::close(sink);
0038 }
0039
0040 template<typename Executor>
0041 void on_success(Executor &) const
0042 {
0043 ::close(sink);
0044 }
0045
0046 template <typename Executor>
0047 void on_exec_setup(Executor &e) const;
0048 };
0049
0050 template<>
0051 template<typename Executor>
0052 void pipe_out<1,-1>::on_exec_setup(Executor &e) const
0053 {
0054 if (::dup2(sink, STDOUT_FILENO) == -1)
0055 e.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
0056
0057 if (sink != STDOUT_FILENO)
0058 ::close(sink);
0059 ::close(source);
0060 }
0061
0062 template<>
0063 template<typename Executor>
0064 void pipe_out<2,-1>::on_exec_setup(Executor &e) const
0065 {
0066 if (::dup2(sink, STDERR_FILENO) == -1)
0067 e.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
0068
0069 if (sink != STDOUT_FILENO)
0070 ::close(sink);
0071 ::close(source);
0072 }
0073
0074 template<>
0075 template<typename Executor>
0076 void pipe_out<1,2>::on_exec_setup(Executor &e) const
0077 {
0078 if (::dup2(sink, STDOUT_FILENO) == -1)
0079 e.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
0080 if (::dup2(sink, STDERR_FILENO) == -1)
0081 e.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
0082 if ((sink != STDOUT_FILENO) && (sink != STDERR_FILENO))
0083 ::close(sink);
0084 }
0085
0086 class async_pipe;
0087
0088 template<int p1, int p2>
0089 struct async_pipe_out : public pipe_out<p1, p2>
0090 {
0091 async_pipe &pipe;
0092 template<typename AsyncPipe>
0093 async_pipe_out(AsyncPipe & p) : pipe_out<p1, p2>(p.native_sink(), p.native_source()), pipe(p)
0094 {
0095 }
0096
0097 template<typename Pipe, typename Executor>
0098 static void close(Pipe & pipe, Executor &)
0099 {
0100 boost::system::error_code ec;
0101 std::move(pipe).sink().close(ec);
0102 }
0103
0104 template<typename Executor>
0105 void on_error(Executor & exec, const std::error_code &)
0106 {
0107 close(pipe, exec);
0108 }
0109
0110 template<typename Executor>
0111 void on_success(Executor &exec)
0112 {
0113 close(pipe, exec);
0114 }
0115 };
0116
0117
0118 }}}}
0119
0120 #endif