async_pipe.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // Copyright (c) 2016 Klemens D. Morgenstern
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_PROCESS_DETAIL_POSIX_ASYNC_PIPE_HPP_
  6. #define BOOST_PROCESS_DETAIL_POSIX_ASYNC_PIPE_HPP_
  7. #include <boost/process/v1/detail/posix/basic_pipe.hpp>
  8. #include <boost/asio/posix/stream_descriptor.hpp>
  9. #include <boost/asio/post.hpp>
  10. #include <system_error>
  11. #include <string>
  12. #include <utility>
  13. namespace boost { namespace process { BOOST_PROCESS_V1_INLINE namespace v1 { namespace detail { namespace posix {
  14. class async_pipe
  15. {
  16. ::boost::asio::posix::stream_descriptor _source;
  17. ::boost::asio::posix::stream_descriptor _sink ;
  18. public:
  19. typedef int native_handle_type;
  20. typedef ::boost::asio::posix::stream_descriptor handle_type;
  21. typedef typename handle_type::executor_type executor_type;
  22. executor_type get_executor()
  23. {
  24. return _source.get_executor();
  25. }
  26. inline async_pipe(boost::asio::io_context & ios) : async_pipe(ios, ios) {}
  27. inline async_pipe(boost::asio::io_context & ios_source,
  28. boost::asio::io_context & ios_sink) : _source(ios_source), _sink(ios_sink)
  29. {
  30. int fds[2];
  31. if (::pipe(fds) == -1)
  32. boost::process::v1::detail::throw_last_error("pipe(2) failed");
  33. _source.assign(fds[0]);
  34. _sink .assign(fds[1]);
  35. };
  36. inline async_pipe(boost::asio::io_context & ios, const std::string & name)
  37. : async_pipe(ios, ios, name) {}
  38. inline async_pipe(boost::asio::io_context & ios_source,
  39. boost::asio::io_context & io_sink, const std::string & name);
  40. inline async_pipe(const async_pipe& lhs);
  41. async_pipe(async_pipe&& lhs) : _source(std::move(lhs._source)), _sink(std::move(lhs._sink))
  42. {
  43. lhs._source = ::boost::asio::posix::stream_descriptor{lhs._source.get_executor()};
  44. lhs._sink = ::boost::asio::posix::stream_descriptor{lhs._sink. get_executor()};
  45. }
  46. template<class CharT, class Traits = std::char_traits<CharT>>
  47. explicit async_pipe(::boost::asio::io_context & ios_source,
  48. ::boost::asio::io_context & ios_sink,
  49. const basic_pipe<CharT, Traits> & p)
  50. : _source(ios_source, p.native_source()), _sink(ios_sink, p.native_sink())
  51. {
  52. }
  53. template<class CharT, class Traits = std::char_traits<CharT>>
  54. explicit async_pipe(boost::asio::io_context & ios, const basic_pipe<CharT, Traits> & p)
  55. : async_pipe(ios, ios, p)
  56. {
  57. }
  58. template<class CharT, class Traits = std::char_traits<CharT>>
  59. inline async_pipe& operator=(const basic_pipe<CharT, Traits>& p);
  60. inline async_pipe& operator=(const async_pipe& rhs);
  61. inline async_pipe& operator=(async_pipe&& lhs);
  62. ~async_pipe()
  63. {
  64. boost::system::error_code ec;
  65. close(ec);
  66. }
  67. template<class CharT, class Traits = std::char_traits<CharT>>
  68. inline explicit operator basic_pipe<CharT, Traits>() const;
  69. void cancel()
  70. {
  71. if (_sink.is_open())
  72. _sink.cancel();
  73. if (_source.is_open())
  74. _source.cancel();
  75. }
  76. void close()
  77. {
  78. if (_sink.is_open())
  79. _sink.close();
  80. if (_source.is_open())
  81. _source.close();
  82. }
  83. void close(boost::system::error_code & ec)
  84. {
  85. if (_sink.is_open())
  86. _sink.close(ec);
  87. if (_source.is_open())
  88. _source.close(ec);
  89. }
  90. bool is_open() const
  91. {
  92. return _sink.is_open() || _source.is_open();
  93. }
  94. void async_close()
  95. {
  96. if (_sink.is_open())
  97. boost::asio::post(_sink.get_executor(), [this]{_sink.close();});
  98. if (_source.is_open())
  99. boost::asio::post(_source.get_executor(), [this]{_source.close();});
  100. }
  101. template<typename MutableBufferSequence>
  102. std::size_t read_some(const MutableBufferSequence & buffers)
  103. {
  104. return _source.read_some(buffers);
  105. }
  106. template<typename MutableBufferSequence>
  107. std::size_t write_some(const MutableBufferSequence & buffers)
  108. {
  109. return _sink.write_some(buffers);
  110. }
  111. template<typename MutableBufferSequence>
  112. std::size_t read_some(const MutableBufferSequence & buffers, boost::system::error_code & ec) noexcept
  113. {
  114. return _source.read_some(buffers, ec);
  115. }
  116. template<typename MutableBufferSequence>
  117. std::size_t write_some(const MutableBufferSequence & buffers, boost::system::error_code & ec) noexcept
  118. {
  119. return _sink.write_some(buffers, ec);
  120. }
  121. native_handle_type native_source() const {return const_cast<boost::asio::posix::stream_descriptor&>(_source).native_handle();}
  122. native_handle_type native_sink () const {return const_cast<boost::asio::posix::stream_descriptor&>(_sink ).native_handle();}
  123. template<typename MutableBufferSequence,
  124. typename ReadHandler>
  125. BOOST_ASIO_INITFN_RESULT_TYPE(
  126. ReadHandler, void(boost::system::error_code, std::size_t))
  127. async_read_some(
  128. const MutableBufferSequence & buffers,
  129. ReadHandler &&handler)
  130. {
  131. return _source.async_read_some(buffers, std::forward<ReadHandler>(handler));
  132. }
  133. template<typename ConstBufferSequence,
  134. typename WriteHandler>
  135. BOOST_ASIO_INITFN_RESULT_TYPE(
  136. WriteHandler, void(boost::system::error_code, std::size_t))
  137. async_write_some(
  138. const ConstBufferSequence & buffers,
  139. WriteHandler&& handler)
  140. {
  141. return _sink.async_write_some(buffers, std::forward<WriteHandler>(handler));
  142. }
  143. const handle_type & sink () const & {return _sink;}
  144. const handle_type & source() const & {return _source;}
  145. handle_type && sink() && { return std::move(_sink); }
  146. handle_type && source()&& { return std::move(_source); }
  147. handle_type source(::boost::asio::io_context& ios) &&
  148. {
  149. ::boost::asio::posix::stream_descriptor stolen(ios, _source.release());
  150. return stolen;
  151. }
  152. handle_type sink (::boost::asio::io_context& ios) &&
  153. {
  154. ::boost::asio::posix::stream_descriptor stolen(ios, _sink.release());
  155. return stolen;
  156. }
  157. handle_type source(::boost::asio::io_context& ios) const &
  158. {
  159. auto source_in = const_cast<::boost::asio::posix::stream_descriptor &>(_source).native_handle();
  160. return ::boost::asio::posix::stream_descriptor(ios, ::dup(source_in));
  161. }
  162. handle_type sink (::boost::asio::io_context& ios) const &
  163. {
  164. auto sink_in = const_cast<::boost::asio::posix::stream_descriptor &>(_sink).native_handle();
  165. return ::boost::asio::posix::stream_descriptor(ios, ::dup(sink_in));
  166. }
  167. };
  168. async_pipe::async_pipe(boost::asio::io_context & ios_source,
  169. boost::asio::io_context & ios_sink,
  170. const std::string & name) : _source(ios_source), _sink(ios_sink)
  171. {
  172. auto fifo = mkfifo(name.c_str(), 0666 );
  173. if (fifo != 0)
  174. boost::process::v1::detail::throw_last_error("mkfifo() failed");
  175. int read_fd = open(name.c_str(), O_RDWR);
  176. if (read_fd == -1)
  177. boost::process::v1::detail::throw_last_error();
  178. int write_fd = dup(read_fd);
  179. if (write_fd == -1)
  180. boost::process::v1::detail::throw_last_error();
  181. _source.assign(read_fd);
  182. _sink .assign(write_fd);
  183. }
  184. async_pipe::async_pipe(const async_pipe & p) :
  185. _source(const_cast<async_pipe&>(p)._source.get_executor()),
  186. _sink( const_cast<async_pipe&>(p)._sink.get_executor())
  187. {
  188. //cannot get the handle from a const object.
  189. auto source_in = const_cast<::boost::asio::posix::stream_descriptor &>(_source).native_handle();
  190. auto sink_in = const_cast<::boost::asio::posix::stream_descriptor &>(_sink).native_handle();
  191. if (source_in == -1)
  192. _source.assign(-1);
  193. else
  194. {
  195. _source.assign(::dup(source_in));
  196. if (_source.native_handle()== -1)
  197. ::boost::process::v1::detail::throw_last_error("dup()");
  198. }
  199. if (sink_in == -1)
  200. _sink.assign(-1);
  201. else
  202. {
  203. _sink.assign(::dup(sink_in));
  204. if (_sink.native_handle() == -1)
  205. ::boost::process::v1::detail::throw_last_error("dup()");
  206. }
  207. }
  208. async_pipe& async_pipe::operator=(const async_pipe & p)
  209. {
  210. int source;
  211. int sink;
  212. //cannot get the handle from a const object.
  213. auto source_in = const_cast<::boost::asio::posix::stream_descriptor &>(p._source).native_handle();
  214. auto sink_in = const_cast<::boost::asio::posix::stream_descriptor &>(p._sink).native_handle();
  215. if (source_in == -1)
  216. source = -1;
  217. else
  218. {
  219. source = ::dup(source_in);
  220. if (source == -1)
  221. ::boost::process::v1::detail::throw_last_error("dup()");
  222. }
  223. if (sink_in == -1)
  224. sink = -1;
  225. else
  226. {
  227. sink = ::dup(sink_in);
  228. if (sink == -1)
  229. ::boost::process::v1::detail::throw_last_error("dup()");
  230. }
  231. _source.assign(source);
  232. _sink. assign(sink);
  233. return *this;
  234. }
  235. async_pipe& async_pipe::operator=(async_pipe && lhs)
  236. {
  237. std::swap(_source, lhs._source);
  238. std::swap(_sink, lhs._sink);
  239. return *this;
  240. }
  241. template<class CharT, class Traits>
  242. async_pipe::operator basic_pipe<CharT, Traits>() const
  243. {
  244. int source;
  245. int sink;
  246. //cannot get the handle from a const object.
  247. auto source_in = const_cast<::boost::asio::posix::stream_descriptor &>(_source).native_handle();
  248. auto sink_in = const_cast<::boost::asio::posix::stream_descriptor &>(_sink).native_handle();
  249. if (source_in == -1)
  250. source = -1;
  251. else
  252. {
  253. source = ::dup(source_in);
  254. if (source == -1)
  255. ::boost::process::v1::detail::throw_last_error("dup()");
  256. }
  257. if (sink_in == -1)
  258. sink = -1;
  259. else
  260. {
  261. sink = ::dup(sink_in);
  262. if (sink == -1)
  263. ::boost::process::v1::detail::throw_last_error("dup()");
  264. }
  265. return basic_pipe<CharT, Traits>{source, sink};
  266. }
  267. inline bool operator==(const async_pipe & lhs, const async_pipe & rhs)
  268. {
  269. return compare_handles(lhs.native_source(), rhs.native_source()) &&
  270. compare_handles(lhs.native_sink(), rhs.native_sink());
  271. }
  272. inline bool operator!=(const async_pipe & lhs, const async_pipe & rhs)
  273. {
  274. return !compare_handles(lhs.native_source(), rhs.native_source()) ||
  275. !compare_handles(lhs.native_sink(), rhs.native_sink());
  276. }
  277. template<class Char, class Traits>
  278. inline bool operator==(const async_pipe & lhs, const basic_pipe<Char, Traits> & rhs)
  279. {
  280. return compare_handles(lhs.native_source(), rhs.native_source()) &&
  281. compare_handles(lhs.native_sink(), rhs.native_sink());
  282. }
  283. template<class Char, class Traits>
  284. inline bool operator!=(const async_pipe & lhs, const basic_pipe<Char, Traits> & rhs)
  285. {
  286. return !compare_handles(lhs.native_source(), rhs.native_source()) ||
  287. !compare_handles(lhs.native_sink(), rhs.native_sink());
  288. }
  289. template<class Char, class Traits>
  290. inline bool operator==(const basic_pipe<Char, Traits> & lhs, const async_pipe & rhs)
  291. {
  292. return compare_handles(lhs.native_source(), rhs.native_source()) &&
  293. compare_handles(lhs.native_sink(), rhs.native_sink());
  294. }
  295. template<class Char, class Traits>
  296. inline bool operator!=(const basic_pipe<Char, Traits> & lhs, const async_pipe & rhs)
  297. {
  298. return !compare_handles(lhs.native_source(), rhs.native_source()) ||
  299. !compare_handles(lhs.native_sink(), rhs.native_sink());
  300. }
  301. }}}}}
  302. #endif /* INCLUDE_BOOST_PIPE_DETAIL_WINDOWS_ASYNC_PIPE_HPP_ */