pipe_out.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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(), "dup3() failed");
  46. ::close(sink);
  47. ::close(source);
  48. }
  49. template<>
  50. template<typename Executor>
  51. void pipe_out<2,-1>::on_exec_setup(Executor &e) const
  52. {
  53. if (::dup2(sink, STDERR_FILENO) == -1)
  54. e.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
  55. ::close(sink);
  56. ::close(source);
  57. }
  58. template<>
  59. template<typename Executor>
  60. void pipe_out<1,2>::on_exec_setup(Executor &e) const
  61. {
  62. if (::dup2(sink, STDOUT_FILENO) == -1)
  63. e.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
  64. if (::dup2(sink, STDERR_FILENO) == -1)
  65. e.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
  66. ::close(sink);
  67. ::close(source);
  68. }
  69. class async_pipe;
  70. template<int p1, int p2>
  71. struct async_pipe_out : public pipe_out<p1, p2>
  72. {
  73. async_pipe &pipe;
  74. template<typename AsyncPipe>
  75. async_pipe_out(AsyncPipe & p) : pipe_out<p1, p2>(p.native_sink(), p.native_source()), pipe(p)
  76. {
  77. }
  78. template<typename Pipe, typename Executor>
  79. static void close(Pipe & pipe, Executor &)
  80. {
  81. boost::system::error_code ec;
  82. std::move(pipe).sink().close(ec);
  83. }
  84. template<typename Executor>
  85. void on_error(Executor & exec, const std::error_code &)
  86. {
  87. close(pipe, exec);
  88. }
  89. template<typename Executor>
  90. void on_success(Executor &exec)
  91. {
  92. close(pipe, exec);
  93. }
  94. };
  95. }}}}
  96. #endif