pipe_out.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (c) 2006, 2007 Julio M. Merino Vidal
  2. // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
  3. // Copyright (c) 2009 Boris Schaeling
  4. // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
  5. // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
  6. // Copyright (c) 2016 Klemens D. Morgenstern
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_PROCESS_DETAIL_POSIX_PIPE_OUT_HPP
  11. #define BOOST_PROCESS_DETAIL_POSIX_PIPE_OUT_HPP
  12. #include <boost/process/v1/pipe.hpp>
  13. #include <boost/process/v1/detail/posix/handler.hpp>
  14. #include <unistd.h>
  15. #include <array>
  16. #include <boost/process/v1/detail/used_handles.hpp>
  17. namespace boost { namespace process { BOOST_PROCESS_V1_INLINE namespace v1 { namespace detail { namespace posix {
  18. template<int p1, int p2>
  19. struct pipe_out : handler_base_ext, ::boost::process::v1::detail::uses_handles
  20. {
  21. int sink;
  22. int source; //opposite end
  23. std::array<int, 4> get_used_handles()
  24. {
  25. const auto pp1 = p1 != -1 ? p1 : p2;
  26. const auto pp2 = p2 != -1 ? p2 : p1;
  27. return {source, sink, pp1, pp2};
  28. }
  29. pipe_out(int sink, int source) : sink(sink), source(source) {}
  30. template<typename T>
  31. pipe_out(T & p) : sink(p.native_sink()), source(p.native_source())
  32. {
  33. p.assign_sink(-1);
  34. }
  35. template<typename Executor>
  36. void on_error(Executor &, const std::error_code &) const
  37. {
  38. ::close(sink);
  39. }
  40. template<typename Executor>
  41. void on_success(Executor &) const
  42. {
  43. ::close(sink);
  44. }
  45. template <typename Executor>
  46. void on_exec_setup(Executor &e) const;
  47. };
  48. template<>
  49. template<typename Executor>
  50. void pipe_out<1,-1>::on_exec_setup(Executor &e) const
  51. {
  52. if (::dup2(sink, STDOUT_FILENO) == -1)
  53. e.set_error(::boost::process::v1::detail::get_last_error(), "dup2() failed");
  54. if (sink != STDOUT_FILENO)
  55. ::close(sink);
  56. ::close(source);
  57. }
  58. template<>
  59. template<typename Executor>
  60. void pipe_out<2,-1>::on_exec_setup(Executor &e) const
  61. {
  62. if (::dup2(sink, STDERR_FILENO) == -1)
  63. e.set_error(::boost::process::v1::detail::get_last_error(), "dup2() failed");
  64. if (sink != STDOUT_FILENO)
  65. ::close(sink);
  66. ::close(source);
  67. }
  68. template<>
  69. template<typename Executor>
  70. void pipe_out<1,2>::on_exec_setup(Executor &e) const
  71. {
  72. if (::dup2(sink, STDOUT_FILENO) == -1)
  73. e.set_error(::boost::process::v1::detail::get_last_error(), "dup2() failed");
  74. if (::dup2(sink, STDERR_FILENO) == -1)
  75. e.set_error(::boost::process::v1::detail::get_last_error(), "dup2() failed");
  76. if ((sink != STDOUT_FILENO) && (sink != STDERR_FILENO))
  77. ::close(sink);
  78. ::close(source);
  79. }
  80. class async_pipe;
  81. template<int p1, int p2>
  82. struct async_pipe_out : public pipe_out<p1, p2>
  83. {
  84. async_pipe &pipe;
  85. template<typename AsyncPipe>
  86. async_pipe_out(AsyncPipe & p) : pipe_out<p1, p2>(p.native_sink(), p.native_source()), pipe(p)
  87. {
  88. }
  89. template<typename Pipe, typename Executor>
  90. static void close(Pipe & pipe, Executor &)
  91. {
  92. boost::system::error_code ec;
  93. std::move(pipe).sink().close(ec);
  94. }
  95. template<typename Executor>
  96. void on_error(Executor & exec, const std::error_code &)
  97. {
  98. close(pipe, exec);
  99. }
  100. template<typename Executor>
  101. void on_success(Executor &exec)
  102. {
  103. close(pipe, exec);
  104. }
  105. };
  106. }}}}}
  107. #endif