null_out.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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_POSIX_PIPE_OUT_HPP
  11. #define BOOST_PROCESS_POSIX_PIPE_OUT_HPP
  12. #include <boost/process/detail/posix/handler.hpp>
  13. #include <boost/process/detail/posix/file_descriptor.hpp>
  14. #include <boost/process/detail/used_handles.hpp>
  15. #include <unistd.h>
  16. #include <array>
  17. namespace boost { namespace process { namespace detail { namespace posix {
  18. template<int p1, int p2>
  19. struct null_out : handler_base_ext, ::boost::process::detail::uses_handles
  20. {
  21. file_descriptor sink{"/dev/null", file_descriptor::write};
  22. template <typename Executor>
  23. void on_exec_setup(Executor &e) const;
  24. std::array<int, 3> get_used_handles()
  25. {
  26. const auto pp1 = p1 != -1 ? p1 : p2;
  27. const auto pp2 = p2 != -1 ? p2 : p1;
  28. return {sink.handle(), pp1, pp2};
  29. }
  30. };
  31. template<>
  32. template<typename Executor>
  33. void null_out<1,-1>::on_exec_setup(Executor &e) const
  34. {
  35. if (::dup2(sink.handle(), STDOUT_FILENO) == -1)
  36. e.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
  37. }
  38. template<>
  39. template<typename Executor>
  40. void null_out<2,-1>::on_exec_setup(Executor &e) const
  41. {
  42. if (::dup2(sink.handle(), STDERR_FILENO) == -1)
  43. e.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
  44. }
  45. template<>
  46. template<typename Executor>
  47. void null_out<1,2>::on_exec_setup(Executor &e) const
  48. {
  49. if (::dup2(sink.handle(), STDOUT_FILENO) == -1)
  50. e.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
  51. if (::dup2(sink.handle(), STDERR_FILENO) == -1)
  52. e.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
  53. }
  54. }}}}
  55. #endif