pipe_in.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_PROCESS_POSIX_PIPE_IN_HPP
  10. #define BOOST_PROCESS_POSIX_PIPE_IN_HPP
  11. #include <boost/process/pipe.hpp>
  12. #include <boost/process/detail/posix/handler.hpp>
  13. #include <unistd.h>
  14. namespace boost { namespace process { namespace detail { namespace posix {
  15. struct pipe_in : handler_base_ext
  16. {
  17. int source;
  18. int sink; //opposite end
  19. pipe_in(int sink, int source) : source(source), sink(sink) {}
  20. template<typename T>
  21. pipe_in(T & p) : source(p.native_source()), sink(p.native_sink())
  22. {
  23. p.assign_source(-1);
  24. }
  25. template<typename Executor>
  26. void on_error(Executor &, const std::error_code &) const
  27. {
  28. ::close(source);
  29. }
  30. template<typename Executor>
  31. void on_success(Executor &) const
  32. {
  33. ::close(source);
  34. }
  35. template <class Executor>
  36. void on_exec_setup(Executor &e) const
  37. {
  38. if (::dup2(source, STDIN_FILENO) == -1)
  39. e.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
  40. ::close(source);
  41. ::close(sink);
  42. }
  43. };
  44. class async_pipe;
  45. struct async_pipe_in : public pipe_in
  46. {
  47. async_pipe &pipe;
  48. template<typename AsyncPipe>
  49. async_pipe_in(AsyncPipe & p) : pipe_in(p.native_sink(), p.native_source()), pipe(p)
  50. {
  51. }
  52. template<typename Pipe, typename Executor>
  53. static void close(Pipe & pipe, Executor &)
  54. {
  55. boost::system::error_code ec;
  56. std::move(pipe).source().close(ec);
  57. }
  58. template<typename Executor>
  59. void on_error(Executor & exec, const std::error_code &)
  60. {
  61. close(pipe, exec);
  62. }
  63. template<typename Executor>
  64. void on_success(Executor &exec)
  65. {
  66. close(pipe, exec);
  67. }
  68. };
  69. }}}}
  70. #endif