popen.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. // Copyright (c) 2022 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_V2_POPEN_HPP
  6. #define BOOST_PROCESS_V2_POPEN_HPP
  7. #include <boost/process/v2/process.hpp>
  8. #include <boost/process/v2/stdio.hpp>
  9. #if defined(BOOST_PROCESS_V2_STANDALONE)
  10. #include <asio/connect_pipe.hpp>
  11. #include <asio/readable_pipe.hpp>
  12. #include <asio/writable_pipe.hpp>
  13. #else
  14. #include <boost/asio/connect_pipe.hpp>
  15. #include <boost/asio/readable_pipe.hpp>
  16. #include <boost/asio/writable_pipe.hpp>
  17. #endif
  18. BOOST_PROCESS_V2_BEGIN_NAMESPACE
  19. /// A subprocess with automatically assigned pipes.
  20. /** The purpose os the popen is to provide a convenient way
  21. * to use the stdin & stdout of a process.
  22. *
  23. * @code {.cpp}
  24. * popen proc(executor, find_executable("addr2line"), {argv[0]});
  25. * asio::write(proc, asio::buffer("main\n"));
  26. * std::string line;
  27. * asio::read_until(proc, asio::dynamic_buffer(line), '\n');
  28. * @endcode
  29. *
  30. *
  31. * Popen can be used as a stream object in other protocols.
  32. */
  33. template<typename Executor = BOOST_PROCESS_V2_ASIO_NAMESPACE::any_io_executor>
  34. struct basic_popen : basic_process<Executor>
  35. {
  36. /// The executor of the process
  37. using executor_type = Executor;
  38. /// Rebinds the popen type to another executor.
  39. template <typename Executor1>
  40. struct rebind_executor
  41. {
  42. /// The pipe type when rebound to the specified executor.
  43. typedef basic_popen<Executor1> other;
  44. };
  45. /// Move construct a popen
  46. basic_popen(basic_popen &&) = default;
  47. /// Move assign a popen
  48. basic_popen& operator=(basic_popen &&) = default;
  49. /// Move construct a popen and change the executor type.
  50. template<typename Executor1>
  51. basic_popen(basic_popen<Executor1>&& lhs)
  52. : basic_process<Executor>(std::move(lhs)),
  53. stdin_(std::move(lhs.stdin_)), stdout_(std::move(lhs.stdout_))
  54. {
  55. }
  56. /// Create a closed process handle
  57. explicit basic_popen(executor_type exec) : basic_process<Executor>{std::move(exec)} {}
  58. /// Create a closed process handle
  59. template <typename ExecutionContext>
  60. explicit basic_popen(ExecutionContext & context,
  61. typename std::enable_if<
  62. is_convertible<ExecutionContext&,
  63. BOOST_PROCESS_V2_ASIO_NAMESPACE::execution_context&>::value, void *>::type = nullptr)
  64. : basic_process<Executor>{context}
  65. {
  66. }
  67. /// Construct a child from a property list and launch it using the default process launcher.
  68. template<typename ... Inits>
  69. explicit basic_popen(
  70. executor_type executor,
  71. const filesystem::path& exe,
  72. std::initializer_list<string_view> args,
  73. Inits&&... inits)
  74. : basic_process<Executor>(executor)
  75. {
  76. this->basic_process<Executor>::operator=(
  77. default_process_launcher()(
  78. this->get_executor(), exe, args,
  79. std::forward<Inits>(inits)...,
  80. process_stdio{stdin_, stdout_}
  81. ));
  82. }
  83. /// Construct a child from a property list and launch it using the default process launcher.
  84. template<typename Launcher, typename ... Inits>
  85. explicit basic_popen(
  86. Launcher && launcher,
  87. executor_type executor,
  88. const filesystem::path& exe,
  89. std::initializer_list<string_view> args,
  90. Inits&&... inits)
  91. : basic_process<Executor>(executor)
  92. {
  93. this->basic_process<Executor>::operator=(
  94. std::forward<Launcher>(launcher)(
  95. this->get_executor(), exe, args,
  96. std::forward<Inits>(inits)...,
  97. process_stdio{stdin_, stdout_}
  98. ));
  99. }
  100. /// Construct a child from a property list and launch it using the default process launcher.
  101. template<typename ... Inits>
  102. explicit basic_popen(
  103. executor_type executor,
  104. const filesystem::path& exe,
  105. std::initializer_list<wstring_view> args,
  106. Inits&&... inits)
  107. : basic_process<Executor>(executor)
  108. {
  109. this->basic_process<Executor>::operator=(
  110. default_process_launcher()(
  111. this->get_executor(), exe, args,
  112. std::forward<Inits>(inits)...,
  113. process_stdio{stdin_, stdout_}
  114. ));
  115. }
  116. /// Construct a child from a property list and launch it using the default process launcher.
  117. template<typename Launcher, typename ... Inits>
  118. explicit basic_popen(
  119. Launcher && launcher,
  120. executor_type executor,
  121. const filesystem::path& exe,
  122. std::initializer_list<wstring_view> args,
  123. Inits&&... inits)
  124. : basic_process<Executor>(executor)
  125. {
  126. this->basic_process<Executor>::operator=(
  127. std::forward<Launcher>(launcher)(
  128. this->get_executor(), exe, args,
  129. std::forward<Inits>(inits)...,
  130. process_stdio{stdin_, stdout_}
  131. ));
  132. }
  133. /// Construct a child from a property list and launch it using the default process launcher.
  134. template<typename Args, typename ... Inits>
  135. explicit basic_popen(
  136. executor_type executor,
  137. const filesystem::path& exe,
  138. Args&& args, Inits&&... inits)
  139. : basic_process<Executor>(executor)
  140. {
  141. this->basic_process<Executor>::operator=(
  142. default_process_launcher()(
  143. std::move(executor), exe, args,
  144. std::forward<Inits>(inits)...,
  145. process_stdio{stdin_, stdout_}
  146. ));
  147. }
  148. /// Construct a child from a property list and launch it using the default process launcher.
  149. template<typename Launcher, typename Args, typename ... Inits>
  150. explicit basic_popen(
  151. Launcher && launcher,
  152. executor_type executor,
  153. const filesystem::path& exe,
  154. Args&& args, Inits&&... inits)
  155. : basic_process<Executor>(executor)
  156. {
  157. this->basic_process<Executor>::operator=(
  158. std::forward<Launcher>(launcher)(
  159. std::move(executor), exe, args,
  160. std::forward<Inits>(inits)...,
  161. process_stdio{stdin_, stdout_}
  162. ));
  163. }
  164. /// Construct a child from a property list and launch it using the default process launcher.
  165. template<typename ExecutionContext, typename ... Inits>
  166. explicit basic_popen(
  167. ExecutionContext & context,
  168. typename std::enable_if<
  169. std::is_convertible<ExecutionContext&,
  170. BOOST_PROCESS_V2_ASIO_NAMESPACE::execution_context&>::value,
  171. const filesystem::path&>::type exe,
  172. std::initializer_list<string_view> args,
  173. Inits&&... inits)
  174. : basic_process<Executor>(context)
  175. {
  176. this->basic_process<Executor>::operator=(
  177. default_process_launcher()(
  178. this->get_executor(), exe, args,
  179. std::forward<Inits>(inits)...,
  180. process_stdio{stdin_, stdout_}
  181. ));
  182. }
  183. /// Construct a child from a property list and launch it using the default process launcher.
  184. template<typename Launcher, typename ExecutionContext, typename ... Inits>
  185. explicit basic_popen(
  186. Launcher && launcher,
  187. ExecutionContext & context,
  188. typename std::enable_if<
  189. std::is_convertible<ExecutionContext&,
  190. BOOST_PROCESS_V2_ASIO_NAMESPACE::execution_context&>::value,
  191. const filesystem::path&>::type exe,
  192. std::initializer_list<string_view> args,
  193. Inits&&... inits)
  194. : basic_process<Executor>(context)
  195. {
  196. this->basic_process<Executor>::operator=(
  197. std::forward<Launcher>(launcher)(
  198. this->get_executor(), exe, args,
  199. std::forward<Inits>(inits)...,
  200. process_stdio{stdin_, stdout_}
  201. ));
  202. }
  203. /// Construct a child from a property list and launch it using the default process launcher.
  204. template<typename ExecutionContext, typename Args, typename ... Inits>
  205. explicit basic_popen(
  206. ExecutionContext & context,
  207. typename std::enable_if<
  208. std::is_convertible<ExecutionContext&,
  209. BOOST_PROCESS_V2_ASIO_NAMESPACE::execution_context&>::value,
  210. const filesystem::path&>::type exe,
  211. Args&& args, Inits&&... inits)
  212. : basic_process<Executor>(context)
  213. {
  214. this->basic_process<Executor>::operator=(
  215. default_process_launcher()(
  216. this->get_executor(), exe, args,
  217. std::forward<Inits>(inits)...,
  218. process_stdio{stdin_, stdout_}
  219. ));
  220. }
  221. /// Construct a child from a property list and launch it using the default process launcher.
  222. template<typename Launcher, typename ExecutionContext, typename Args, typename ... Inits>
  223. explicit basic_popen(
  224. Launcher && launcher,
  225. ExecutionContext & context,
  226. typename std::enable_if<
  227. std::is_convertible<ExecutionContext&,
  228. BOOST_PROCESS_V2_ASIO_NAMESPACE::execution_context&>::value,
  229. const filesystem::path&>::type exe,
  230. Args&& args, Inits&&... inits)
  231. : basic_process<Executor>(context)
  232. {
  233. this->basic_process<Executor>::operator=(
  234. std::forward<Launcher>(launcher)(
  235. this->get_executor(), exe, args,
  236. std::forward<Inits>(inits)...,
  237. process_stdio{stdin_, stdout_}
  238. ));
  239. }
  240. /// The type used for stdin on the parent process side.
  241. using stdin_type = BOOST_PROCESS_V2_ASIO_NAMESPACE::basic_writable_pipe<Executor>;
  242. /// The type used for stdout on the parent process side.
  243. using stdout_type = BOOST_PROCESS_V2_ASIO_NAMESPACE::basic_readable_pipe<Executor>;
  244. /// Get the stdin pipe.
  245. stdin_type & get_stdin() {return stdin_; }
  246. /// Get the stdout pipe.
  247. stdout_type & get_stdout() {return stdout_; }
  248. /// Get the stdin pipe.
  249. const stdin_type & get_stdin() const {return stdin_; }
  250. /// Get the stdout pipe.
  251. const stdout_type & get_stdout() const {return stdout_; }
  252. /// Write some data to the pipe.
  253. /**
  254. * This function is used to write data to the pipe. The function call will
  255. * block until one or more bytes of the data has been written successfully,
  256. * or until an error occurs.
  257. *
  258. * @param buffers One or more data buffers to be written to the pipe.
  259. *
  260. * @returns The number of bytes written.
  261. *
  262. * @throws boost::system::system_error Thrown on failure. An error code of
  263. * boost::asio::error::eof indicates that the connection was closed by the
  264. * subprocess.
  265. *
  266. * @note The write_some operation may not transmit all of the data to the
  267. * peer. Consider using the @ref write function if you need to ensure that
  268. * all data is written before the blocking operation completes.
  269. *
  270. * @par Example
  271. * To write a single data buffer use the @ref buffer function as follows:
  272. * @code
  273. * pipe.write_some(boost::asio::buffer(data, size));
  274. * @endcode
  275. * See the @ref buffer documentation for information on writing multiple
  276. * buffers in one go, and how to use it with arrays, boost::array or
  277. * std::vector.
  278. */
  279. template <typename ConstBufferSequence>
  280. std::size_t write_some(const ConstBufferSequence& buffers)
  281. {
  282. return stdin_.write_some(buffers);
  283. }
  284. /// Write some data to the pipe.
  285. /**
  286. * This function is used to write data to the pipe. The function call will
  287. * block until one or more bytes of the data has been written successfully,
  288. * or until an error occurs.
  289. *
  290. * @param buffers One or more data buffers to be written to the pipe.
  291. *
  292. * @param ec Set to indicate what error occurred, if any.
  293. *
  294. * @returns The number of bytes written. Returns 0 if an error occurred.
  295. *
  296. * @note The write_some operation may not transmit all of the data to the
  297. * subprocess. Consider using the @ref write function if you need to ensure that
  298. * all data is written before the blocking operation completes.
  299. */
  300. template <typename ConstBufferSequence>
  301. std::size_t write_some(const ConstBufferSequence& buffers,
  302. boost::system::error_code& ec)
  303. {
  304. return stdin_.write_some(buffers, ec);
  305. }
  306. /// Start an asynchronous write.
  307. /**
  308. * This function is used to asynchronously write data to the pipe. It is an
  309. * initiating function for an @ref asynchronous_operation, and always returns
  310. * immediately.
  311. *
  312. * @param buffers One or more data buffers to be written to the pipe.
  313. * Although the buffers object may be copied as necessary, ownership of the
  314. * underlying memory blocks is retained by the caller, which must guarantee
  315. * that they remain valid until the completion handler is called.
  316. *
  317. * @param token The @ref completion_token that will be used to produce a
  318. * completion handler, which will be called when the write completes.
  319. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  320. * @ref yield_context, or a function object with the correct completion
  321. * signature. The function signature of the completion handler must be:
  322. * @code void handler(
  323. * const boost::system::error_code& error, // Result of operation.
  324. * std::size_t bytes_transferred // Number of bytes written.
  325. * ); @endcode
  326. * Regardless of whether the asynchronous operation completes immediately or
  327. * not, the completion handler will not be invoked from within this function.
  328. * On immediate completion, invocation of the handler will be performed in a
  329. * manner equivalent to using boost::asio::post().
  330. *
  331. * @par Completion Signature
  332. * @code void(boost::system::error_code, std::size_t) @endcode
  333. *
  334. * @note The write operation may not transmit all of the data to the peer.
  335. * Consider using the @ref async_write function if you need to ensure that all
  336. * data is written before the asynchronous operation completes.
  337. *
  338. * @par Example
  339. * To write a single data buffer use the @ref buffer function as follows:
  340. * @code
  341. * popen.async_write_some(boost::asio::buffer(data, size), handler);
  342. * @endcode
  343. * See the @ref buffer documentation for information on writing multiple
  344. * buffers in one go, and how to use it with arrays, boost::array or
  345. * std::vector.
  346. */
  347. template <typename ConstBufferSequence,
  348. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  349. std::size_t)) WriteToken
  350. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  351. BOOST_PROCESS_V2_INITFN_AUTO_RESULT_TYPE(WriteToken,
  352. void (boost::system::error_code, std::size_t))
  353. async_write_some(const ConstBufferSequence& buffers,
  354. BOOST_ASIO_MOVE_ARG(WriteToken) token
  355. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  356. {
  357. return stdin_.async_write_some(buffers, std::forward<WriteToken>(token));
  358. }
  359. /// Read some data from the pipe.
  360. /**
  361. * This function is used to read data from the pipe. The function call will
  362. * block until one or more bytes of data has been read successfully, or until
  363. * an error occurs.
  364. *
  365. * @param buffers One or more buffers into which the data will be read.
  366. *
  367. * @returns The number of bytes read.
  368. *
  369. * @throws boost::system::system_error Thrown on failure. An error code of
  370. * boost::asio::error::eof indicates that the connection was closed by the
  371. * peer.
  372. *
  373. * @note The read_some operation may not read all of the requested number of
  374. * bytes. Consider using the @ref read function if you need to ensure that
  375. * the requested amount of data is read before the blocking operation
  376. * completes.
  377. *
  378. * @par Example
  379. * To read into a single data buffer use the @ref buffer function as follows:
  380. * @code
  381. * basic_readable_pipe.read_some(boost::asio::buffer(data, size));
  382. * @endcode
  383. * See the @ref buffer documentation for information on reading into multiple
  384. * buffers in one go, and how to use it with arrays, boost::array or
  385. * std::vector.
  386. */
  387. template <typename MutableBufferSequence>
  388. std::size_t read_some(const MutableBufferSequence& buffers)
  389. {
  390. return stdout_.read_some(buffers);
  391. }
  392. /// Read some data from the pipe.
  393. /**
  394. * This function is used to read data from the pipe. The function call will
  395. * block until one or more bytes of data has been read successfully, or until
  396. * an error occurs.
  397. *
  398. * @param buffers One or more buffers into which the data will be read.
  399. *
  400. * @param ec Set to indicate what error occurred, if any.
  401. *
  402. * @returns The number of bytes read. Returns 0 if an error occurred.
  403. *
  404. * @note The read_some operation may not read all of the requested number of
  405. * bytes. Consider using the @ref read function if you need to ensure that
  406. * the requested amount of data is read before the blocking operation
  407. * completes.
  408. */
  409. template <typename MutableBufferSequence>
  410. std::size_t read_some(const MutableBufferSequence& buffers,
  411. boost::system::error_code& ec)
  412. {
  413. return stdout_.read_some(buffers, ec);
  414. }
  415. /// Start an asynchronous read.
  416. /**
  417. * This function is used to asynchronously read data from the pipe. It is an
  418. * initiating function for an @ref asynchronous_operation, and always returns
  419. * immediately.
  420. *
  421. * @param buffers One or more buffers into which the data will be read.
  422. * Although the buffers object may be copied as necessary, ownership of the
  423. * underlying memory blocks is retained by the caller, which must guarantee
  424. * that they remain valid until the completion handler is called.
  425. *
  426. * @param token The @ref completion_token that will be used to produce a
  427. * completion handler, which will be called when the read completes.
  428. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  429. * @ref yield_context, or a function object with the correct completion
  430. * signature. The function signature of the completion handler must be:
  431. * @code void handler(
  432. * const boost::system::error_code& error, // Result of operation.
  433. * std::size_t bytes_transferred // Number of bytes read.
  434. * ); @endcode
  435. * Regardless of whether the asynchronous operation completes immediately or
  436. * not, the completion handler will not be invoked from within this function.
  437. * On immediate completion, invocation of the handler will be performed in a
  438. * manner equivalent to using boost::asio::post().
  439. *
  440. * @par Completion Signature
  441. * @code void(boost::system::error_code, std::size_t) @endcode
  442. *
  443. * @note The read operation may not read all of the requested number of bytes.
  444. * Consider using the @ref async_read function if you need to ensure that the
  445. * requested amount of data is read before the asynchronous operation
  446. * completes.
  447. *
  448. * @par Example
  449. * To read into a single data buffer use the @ref buffer function as follows:
  450. * @code
  451. * basic_readable_pipe.async_read_some(
  452. * boost::asio::buffer(data, size), handler);
  453. * @endcode
  454. * See the @ref buffer documentation for information on reading into multiple
  455. * buffers in one go, and how to use it with arrays, boost::array or
  456. * std::vector.
  457. */
  458. template <typename MutableBufferSequence,
  459. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  460. std::size_t)) ReadToken
  461. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  462. BOOST_PROCESS_V2_INITFN_AUTO_RESULT_TYPE(ReadToken,
  463. void (boost::system::error_code, std::size_t))
  464. async_read_some(const MutableBufferSequence& buffers,
  465. BOOST_ASIO_MOVE_ARG(ReadToken) token
  466. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  467. {
  468. return stdout_.async_read_some(buffers, std::forward<ReadToken>(token));
  469. }
  470. private:
  471. stdin_type stdin_ {basic_process<Executor>::get_executor()};
  472. stdout_type stdout_{basic_process<Executor>::get_executor()};
  473. };
  474. /// A popen object with the default executor.
  475. using popen = basic_popen<>;
  476. BOOST_PROCESS_V2_END_NAMESPACE
  477. #endif //BOOST_PROCESS_V2_POPEN_HPP