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