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