pipe_out.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/pipe.hpp>
  13. #include <boost/process/detail/posix/handler.hpp>
  14. #include <unistd.h>
  15. namespace boost { namespace process { namespace detail { namespace posix {
  16. template<int p1, int p2>
  17. struct pipe_out : handler_base_ext
  18. {
  19. int sink;
  20. int source; //opposite end
  21. pipe_out(int sink, int source) : sink(sink), source(source) {}
  22. template<typename T>
  23. pipe_out(T & p) : sink(p.native_sink()), source(p.native_source())
  24. {
  25. p.assign_sink(-1);
  26. }
  27. template<typename Executor>
  28. void on_error(Executor &, const std::error_code &) const
  29. {
  30. ::close(sink);
  31. }
  32. template<typename Executor>
  33. void on_success(Executor &) const
  34. {
  35. ::close(sink);
  36. }
  37. template <typename Executor>
  38. void on_exec_setup(Executor &e) const;
  39. };
  40. template<>
  41. template<typename Executor>
  42. void pipe_out<1,-1>::on_exec_setup(Executor &e) const
  43. {
  44. if (::dup2(sink, STDOUT_FILENO) == -1)
  45. e.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
  46. if (sink != STDOUT_FILENO)
  47. ::close(sink);
  48. ::close(source);
  49. }
  50. template<>
  51. template<typename Executor>
  52. void pipe_out<2,-1>::on_exec_setup(Executor &e) const
  53. {
  54. if (::dup2(sink, STDERR_FILENO) == -1)
  55. e.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
  56. if (sink != STDOUT_FILENO)
  57. ::close(sink);
  58. ::close(source);
  59. }
  60. template<>
  61. template<typename Executor>
  62. void pipe_out<1,2>::on_exec_setup(Executor &e) const
  63. {
  64. if (::dup2(sink, STDOUT_FILENO) == -1)
  65. e.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
  66. if (::dup2(sink, STDERR_FILENO) == -1)
  67. e.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
  68. if ((sink != STDOUT_FILENO) && (sink != STDERR_FILENO))
  69. ::close(sink);
  70. }
  71. class async_pipe;
  72. template<int p1, int p2>
  73. struct async_pipe_out : public pipe_out<p1, p2>
  74. {
  75. async_pipe &pipe;
  76. template<typename AsyncPipe>
  77. async_pipe_out(AsyncPipe & p) : pipe_out<p1, p2>(p.native_sink(), p.native_source()), pipe(p)
  78. {
  79. }
  80. template<typename Pipe, typename Executor>
  81. static void close(Pipe & pipe, Executor &)
  82. {
  83. boost::system::error_code ec;
  84. std::move(pipe).sink().close(ec);
  85. }
  86. template<typename Executor>
  87. void on_error(Executor & exec, const std::error_code &)
  88. {
  89. close(pipe, exec);
  90. }
  91. template<typename Executor>
  92. void on_success(Executor &exec)
  93. {
  94. close(pipe, exec);
  95. }
  96. };
  97. }}}}
  98. #endif