async_pipe.hpp 17 KB

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