async_pipe.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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_WINDOWS_ASYNC_PIPE_HPP_
  6. #define BOOST_PROCESS_DETAIL_WINDOWS_ASYNC_PIPE_HPP_
  7. #include <boost/winapi/basic_types.hpp>
  8. #include <boost/winapi/pipes.hpp>
  9. #include <boost/winapi/handles.hpp>
  10. #include <boost/winapi/file_management.hpp>
  11. #include <boost/winapi/get_last_error.hpp>
  12. #include <boost/winapi/access_rights.hpp>
  13. #include <boost/winapi/process.hpp>
  14. #include <boost/process/detail/windows/basic_pipe.hpp>
  15. #include <boost/asio/post.hpp>
  16. #include <boost/asio/windows/stream_handle.hpp>
  17. #include <atomic>
  18. #include <system_error>
  19. #include <string>
  20. namespace boost { namespace process { namespace detail { namespace windows {
  21. inline std::string make_pipe_name()
  22. {
  23. std::string name = "\\\\.\\pipe\\boost_process_auto_pipe_";
  24. auto pid = ::boost::winapi::GetCurrentProcessId();
  25. static std::atomic_size_t cnt{0};
  26. name += std::to_string(pid);
  27. name += "_";
  28. name += std::to_string(cnt++);
  29. return name;
  30. }
  31. class async_pipe
  32. {
  33. ::boost::asio::windows::stream_handle _source;
  34. ::boost::asio::windows::stream_handle _sink ;
  35. inline async_pipe(boost::asio::io_context & ios_source,
  36. boost::asio::io_context & ios_sink,
  37. const std::string & name, bool private_);
  38. public:
  39. typedef ::boost::winapi::HANDLE_ native_handle_type;
  40. typedef ::boost::asio::windows::stream_handle handle_type;
  41. typedef typename handle_type::executor_type executor_type;
  42. async_pipe(boost::asio::io_context & ios) : async_pipe(ios, ios, make_pipe_name(), true) {}
  43. async_pipe(boost::asio::io_context & ios_source, boost::asio::io_context & ios_sink)
  44. : async_pipe(ios_source, ios_sink, make_pipe_name(), true) {}
  45. async_pipe(boost::asio::io_context & ios, const std::string & name)
  46. : async_pipe(ios, ios, name, false) {}
  47. async_pipe(boost::asio::io_context & ios_source, boost::asio::io_context & ios_sink, const std::string & name)
  48. : async_pipe(ios_source, ios_sink, name, false) {}
  49. inline async_pipe(const async_pipe& rhs);
  50. async_pipe(async_pipe&& rhs) : _source(std::move(rhs._source)), _sink(std::move(rhs._sink))
  51. {
  52. }
  53. template<class CharT, class Traits = std::char_traits<CharT>>
  54. explicit async_pipe(::boost::asio::io_context & ios_source,
  55. ::boost::asio::io_context & ios_sink,
  56. const basic_pipe<CharT, Traits> & p)
  57. : _source(ios_source, p.native_source()), _sink(ios_sink, p.native_sink())
  58. {
  59. }
  60. template<class CharT, class Traits = std::char_traits<CharT>>
  61. explicit async_pipe(boost::asio::io_context & ios, const basic_pipe<CharT, Traits> & p)
  62. : async_pipe(ios, ios, p)
  63. {
  64. }
  65. template<class CharT, class Traits = std::char_traits<CharT>>
  66. inline async_pipe& operator=(const basic_pipe<CharT, Traits>& p);
  67. inline async_pipe& operator=(const async_pipe& rhs);
  68. inline async_pipe& operator=(async_pipe&& rhs);
  69. ~async_pipe()
  70. {
  71. boost::system::error_code ec;
  72. close(ec);
  73. }
  74. template<class CharT, class Traits = std::char_traits<CharT>>
  75. inline explicit operator basic_pipe<CharT, Traits>() const;
  76. void cancel()
  77. {
  78. if (_sink.is_open())
  79. _sink.cancel();
  80. if (_source.is_open())
  81. _source.cancel();
  82. }
  83. void close()
  84. {
  85. if (_sink.is_open())
  86. {
  87. _sink.close();
  88. _sink = handle_type(_sink.get_executor());
  89. }
  90. if (_source.is_open())
  91. {
  92. _source.close();
  93. _source = handle_type(_source.get_executor());
  94. }
  95. }
  96. void close(boost::system::error_code & ec)
  97. {
  98. if (_sink.is_open())
  99. {
  100. _sink.close(ec);
  101. _sink = handle_type(_sink.get_executor());
  102. }
  103. if (_source.is_open())
  104. {
  105. _source.close(ec);
  106. _source = handle_type(_source.get_executor());
  107. }
  108. }
  109. bool is_open() const
  110. {
  111. return _sink.is_open() || _source.is_open();
  112. }
  113. void async_close()
  114. {
  115. if (_sink.is_open())
  116. boost::asio::post(_sink.get_executor(), [this]{_sink.close();});
  117. if (_source.is_open())
  118. boost::asio::post(_source.get_executor(), [this]{_source.close();});
  119. }
  120. template<typename MutableBufferSequence>
  121. std::size_t read_some(const MutableBufferSequence & buffers)
  122. {
  123. return _source.read_some(buffers);
  124. }
  125. template<typename MutableBufferSequence>
  126. std::size_t write_some(const MutableBufferSequence & buffers)
  127. {
  128. return _sink.write_some(buffers);
  129. }
  130. template<typename MutableBufferSequence>
  131. std::size_t read_some(const MutableBufferSequence & buffers, boost::system::error_code & ec) noexcept
  132. {
  133. return _source.read_some(buffers, ec);
  134. }
  135. template<typename MutableBufferSequence>
  136. std::size_t write_some(const MutableBufferSequence & buffers, boost::system::error_code & ec) noexcept
  137. {
  138. return _sink.write_some(buffers, ec);
  139. }
  140. native_handle_type native_source() const {return const_cast<boost::asio::windows::stream_handle&>(_source).native_handle();}
  141. native_handle_type native_sink () const {return const_cast<boost::asio::windows::stream_handle&>(_sink ).native_handle();}
  142. template<typename MutableBufferSequence,
  143. typename ReadHandler>
  144. BOOST_ASIO_INITFN_RESULT_TYPE(
  145. ReadHandler, void(boost::system::error_code, std::size_t))
  146. async_read_some(
  147. const MutableBufferSequence & buffers,
  148. ReadHandler &&handler)
  149. {
  150. return _source.async_read_some(buffers, std::forward<ReadHandler>(handler));
  151. }
  152. template<typename ConstBufferSequence,
  153. typename WriteHandler>
  154. BOOST_ASIO_INITFN_RESULT_TYPE(
  155. WriteHandler, void(boost::system::error_code, std::size_t))
  156. async_write_some(
  157. const ConstBufferSequence & buffers,
  158. WriteHandler && handler)
  159. {
  160. return _sink.async_write_some(buffers, std::forward<WriteHandler>(handler));
  161. }
  162. const handle_type & sink () const & {return _sink;}
  163. const handle_type & source() const & {return _source;}
  164. handle_type && source() && { return std::move(_source); }
  165. handle_type && sink() && { return std::move(_sink); }
  166. handle_type source(::boost::asio::io_context& ios) &&
  167. {
  168. ::boost::asio::windows::stream_handle stolen(ios.get_executor(), _source.native_handle());
  169. boost::system::error_code ec;
  170. _source.assign(::boost::winapi::INVALID_HANDLE_VALUE_, ec);
  171. return stolen;
  172. }
  173. handle_type sink (::boost::asio::io_context& ios) &&
  174. {
  175. ::boost::asio::windows::stream_handle stolen(ios.get_executor(), _sink.native_handle());
  176. boost::system::error_code ec;
  177. _sink.assign(::boost::winapi::INVALID_HANDLE_VALUE_, ec);
  178. return stolen;
  179. }
  180. handle_type source(::boost::asio::io_context& ios) const &
  181. {
  182. auto proc = ::boost::winapi::GetCurrentProcess();
  183. ::boost::winapi::HANDLE_ source;
  184. auto source_in = const_cast<handle_type&>(_source).native_handle();
  185. if (source_in == ::boost::winapi::INVALID_HANDLE_VALUE_)
  186. source = ::boost::winapi::INVALID_HANDLE_VALUE_;
  187. else if (!::boost::winapi::DuplicateHandle(
  188. proc, source_in, proc, &source, 0,
  189. static_cast<::boost::winapi::BOOL_>(true),
  190. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  191. throw_last_error("Duplicate Pipe Failed");
  192. return ::boost::asio::windows::stream_handle(ios.get_executor(), source);
  193. }
  194. handle_type sink (::boost::asio::io_context& ios) const &
  195. {
  196. auto proc = ::boost::winapi::GetCurrentProcess();
  197. ::boost::winapi::HANDLE_ sink;
  198. auto sink_in = const_cast<handle_type&>(_sink).native_handle();
  199. if (sink_in == ::boost::winapi::INVALID_HANDLE_VALUE_)
  200. sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
  201. else if (!::boost::winapi::DuplicateHandle(
  202. proc, sink_in, proc, &sink, 0,
  203. static_cast<::boost::winapi::BOOL_>(true),
  204. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  205. throw_last_error("Duplicate Pipe Failed");
  206. return ::boost::asio::windows::stream_handle(ios.get_executor(), sink);
  207. }
  208. };
  209. async_pipe::async_pipe(const async_pipe& p) :
  210. _source(const_cast<handle_type&>(p._source).get_executor()),
  211. _sink (const_cast<handle_type&>(p._sink).get_executor())
  212. {
  213. auto proc = ::boost::winapi::GetCurrentProcess();
  214. ::boost::winapi::HANDLE_ source;
  215. ::boost::winapi::HANDLE_ sink;
  216. //cannot get the handle from a const object.
  217. auto source_in = const_cast<handle_type&>(p._source).native_handle();
  218. auto sink_in = const_cast<handle_type&>(p._sink).native_handle();
  219. if (source_in == ::boost::winapi::INVALID_HANDLE_VALUE_)
  220. source = ::boost::winapi::INVALID_HANDLE_VALUE_;
  221. else if (!::boost::winapi::DuplicateHandle(
  222. proc, source_in, proc, &source, 0,
  223. static_cast<::boost::winapi::BOOL_>(true),
  224. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  225. throw_last_error("Duplicate Pipe Failed");
  226. if (sink_in == ::boost::winapi::INVALID_HANDLE_VALUE_)
  227. sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
  228. else if (!::boost::winapi::DuplicateHandle(
  229. proc, sink_in, proc, &sink, 0,
  230. static_cast<::boost::winapi::BOOL_>(true),
  231. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  232. throw_last_error("Duplicate Pipe Failed");
  233. if (source != ::boost::winapi::INVALID_HANDLE_VALUE_)
  234. _source.assign(source);
  235. if (sink != ::boost::winapi::INVALID_HANDLE_VALUE_)
  236. _sink. assign(sink);
  237. }
  238. async_pipe::async_pipe(boost::asio::io_context & ios_source,
  239. boost::asio::io_context & ios_sink,
  240. const std::string & name, bool private_) : _source(ios_source), _sink(ios_sink)
  241. {
  242. static constexpr int FILE_FLAG_OVERLAPPED_ = 0x40000000; //temporary
  243. ::boost::winapi::HANDLE_ source = ::boost::winapi::create_named_pipe(
  244. #if defined(BOOST_NO_ANSI_APIS)
  245. ::boost::process::detail::convert(name).c_str(),
  246. #else
  247. name.c_str(),
  248. #endif
  249. ::boost::winapi::PIPE_ACCESS_INBOUND_
  250. | FILE_FLAG_OVERLAPPED_, //write flag
  251. 0, private_ ? 1 : ::boost::winapi::PIPE_UNLIMITED_INSTANCES_, 8192, 8192, 0, nullptr);
  252. if (source == boost::winapi::INVALID_HANDLE_VALUE_)
  253. ::boost::process::detail::throw_last_error("create_named_pipe(" + name + ") failed");
  254. _source.assign(source);
  255. ::boost::winapi::HANDLE_ sink = boost::winapi::create_file(
  256. #if defined(BOOST_NO_ANSI_APIS)
  257. ::boost::process::detail::convert(name).c_str(),
  258. #else
  259. name.c_str(),
  260. #endif
  261. ::boost::winapi::GENERIC_WRITE_, 0, nullptr,
  262. ::boost::winapi::OPEN_EXISTING_,
  263. FILE_FLAG_OVERLAPPED_, //to allow read
  264. nullptr);
  265. if (sink == ::boost::winapi::INVALID_HANDLE_VALUE_)
  266. ::boost::process::detail::throw_last_error("create_file() failed");
  267. _sink.assign(sink);
  268. }
  269. template<class CharT, class Traits>
  270. async_pipe& async_pipe::operator=(const basic_pipe<CharT, Traits> & p)
  271. {
  272. auto proc = ::boost::winapi::GetCurrentProcess();
  273. ::boost::winapi::HANDLE_ source;
  274. ::boost::winapi::HANDLE_ sink;
  275. //cannot get the handle from a const object.
  276. auto source_in = p.native_source();
  277. auto sink_in = p.native_sink();
  278. if (source_in == ::boost::winapi::INVALID_HANDLE_VALUE_)
  279. source = ::boost::winapi::INVALID_HANDLE_VALUE_;
  280. else if (!::boost::winapi::DuplicateHandle(
  281. proc, source_in.native_handle(), proc, &source, 0,
  282. static_cast<::boost::winapi::BOOL_>(true),
  283. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  284. throw_last_error("Duplicate Pipe Failed");
  285. if (sink_in == ::boost::winapi::INVALID_HANDLE_VALUE_)
  286. sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
  287. else if (!::boost::winapi::DuplicateHandle(
  288. proc, sink_in.native_handle(), proc, &sink, 0,
  289. static_cast<::boost::winapi::BOOL_>(true),
  290. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  291. throw_last_error("Duplicate Pipe Failed");
  292. //so we also assign the io_context
  293. if (source != ::boost::winapi::INVALID_HANDLE_VALUE_)
  294. _source.assign(source);
  295. if (sink != ::boost::winapi::INVALID_HANDLE_VALUE_)
  296. _sink.assign(sink);
  297. return *this;
  298. }
  299. async_pipe& async_pipe::operator=(const async_pipe & p)
  300. {
  301. auto proc = ::boost::winapi::GetCurrentProcess();
  302. ::boost::winapi::HANDLE_ source;
  303. ::boost::winapi::HANDLE_ sink;
  304. //cannot get the handle from a const object.
  305. auto &source_in = const_cast<::boost::asio::windows::stream_handle &>(p._source);
  306. auto &sink_in = const_cast<::boost::asio::windows::stream_handle &>(p._sink);
  307. source_in.get_executor();
  308. if (source_in.native_handle() == ::boost::winapi::INVALID_HANDLE_VALUE_)
  309. source = ::boost::winapi::INVALID_HANDLE_VALUE_;
  310. else if (!::boost::winapi::DuplicateHandle(
  311. proc, source_in.native_handle(), proc, &source, 0,
  312. static_cast<::boost::winapi::BOOL_>(true),
  313. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  314. throw_last_error("Duplicate Pipe Failed");
  315. if (sink_in.native_handle() == ::boost::winapi::INVALID_HANDLE_VALUE_)
  316. sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
  317. else if (!::boost::winapi::DuplicateHandle(
  318. proc, sink_in.native_handle(), proc, &sink, 0,
  319. static_cast<::boost::winapi::BOOL_>(true),
  320. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  321. throw_last_error("Duplicate Pipe Failed");
  322. //so we also assign the io_context
  323. if (source != ::boost::winapi::INVALID_HANDLE_VALUE_)
  324. _source = ::boost::asio::windows::stream_handle(source_in.get_executor(), source);
  325. else
  326. _source = ::boost::asio::windows::stream_handle(source_in.get_executor());
  327. if (sink != ::boost::winapi::INVALID_HANDLE_VALUE_)
  328. _sink = ::boost::asio::windows::stream_handle(source_in.get_executor(), sink);
  329. else
  330. _sink = ::boost::asio::windows::stream_handle(source_in.get_executor());
  331. return *this;
  332. }
  333. async_pipe& async_pipe::operator=(async_pipe && rhs)
  334. {
  335. _source = std::move(rhs._source);
  336. _sink = std::move(rhs._sink);
  337. return *this;
  338. }
  339. template<class CharT, class Traits>
  340. async_pipe::operator basic_pipe<CharT, Traits>() const
  341. {
  342. auto proc = ::boost::winapi::GetCurrentProcess();
  343. ::boost::winapi::HANDLE_ source;
  344. ::boost::winapi::HANDLE_ sink;
  345. //cannot get the handle from a const object.
  346. auto source_in = const_cast<::boost::asio::windows::stream_handle &>(_source).native_handle();
  347. auto sink_in = const_cast<::boost::asio::windows::stream_handle &>(_sink).native_handle();
  348. if (source_in == ::boost::winapi::INVALID_HANDLE_VALUE_)
  349. source = ::boost::winapi::INVALID_HANDLE_VALUE_;
  350. else if (!::boost::winapi::DuplicateHandle(
  351. proc, source_in, proc, &source, 0,
  352. static_cast<::boost::winapi::BOOL_>(true),
  353. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  354. throw_last_error("Duplicate Pipe Failed");
  355. if (sink_in == ::boost::winapi::INVALID_HANDLE_VALUE_)
  356. sink = ::boost::winapi::INVALID_HANDLE_VALUE_;
  357. else if (!::boost::winapi::DuplicateHandle(
  358. proc, sink_in, proc, &sink, 0,
  359. static_cast<::boost::winapi::BOOL_>(true),
  360. ::boost::winapi::DUPLICATE_SAME_ACCESS_))
  361. throw_last_error("Duplicate Pipe Failed");
  362. return basic_pipe<CharT, Traits>{source, sink};
  363. }
  364. inline bool operator==(const async_pipe & lhs, const async_pipe & rhs)
  365. {
  366. return compare_handles(lhs.native_source(), rhs.native_source()) &&
  367. compare_handles(lhs.native_sink(), rhs.native_sink());
  368. }
  369. inline bool operator!=(const async_pipe & lhs, const async_pipe & rhs)
  370. {
  371. return !compare_handles(lhs.native_source(), rhs.native_source()) ||
  372. !compare_handles(lhs.native_sink(), rhs.native_sink());
  373. }
  374. template<class Char, class Traits>
  375. inline bool operator==(const async_pipe & lhs, const basic_pipe<Char, Traits> & rhs)
  376. {
  377. return compare_handles(lhs.native_source(), rhs.native_source()) &&
  378. compare_handles(lhs.native_sink(), rhs.native_sink());
  379. }
  380. template<class Char, class Traits>
  381. inline bool operator!=(const async_pipe & lhs, const basic_pipe<Char, Traits> & rhs)
  382. {
  383. return !compare_handles(lhs.native_source(), rhs.native_source()) ||
  384. !compare_handles(lhs.native_sink(), rhs.native_sink());
  385. }
  386. template<class Char, class Traits>
  387. inline bool operator==(const basic_pipe<Char, Traits> & lhs, const async_pipe & rhs)
  388. {
  389. return compare_handles(lhs.native_source(), rhs.native_source()) &&
  390. compare_handles(lhs.native_sink(), rhs.native_sink());
  391. }
  392. template<class Char, class Traits>
  393. inline bool operator!=(const basic_pipe<Char, Traits> & lhs, const async_pipe & rhs)
  394. {
  395. return !compare_handles(lhs.native_source(), rhs.native_source()) ||
  396. !compare_handles(lhs.native_sink(), rhs.native_sink());
  397. }
  398. }}}}
  399. #endif /* INCLUDE_BOOST_PIPE_DETAIL_WINDOWS_ASYNC_PIPE_HPP_ */