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
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)
10 #ifndef BOOST_PROCESS_DETAIL_POSIX_FD_HPP
11 #define BOOST_PROCESS_DETAIL_POSIX_FD_HPP
13 #include <boost/process/detail/posix/handler.hpp>
15 #include <boost/process/detail/used_handles.hpp>
18 namespace boost { namespace process { namespace detail { namespace posix {
21 struct close_fd_ : handler_base_ext, ::boost::process::detail::uses_handles
23 close_fd_(int fd) : fd_(fd) {}
25 template <class PosixExecutor>
26 void on_exec_setup(PosixExecutor& e) const
28 if (::close(fd_) == -1)
29 e.set_error(::boost::process::detail::get_last_error(), "close() failed");
32 int get_used_handles() {return fd_;}
39 template <class Range>
40 struct close_fds_ : handler_base_ext, ::boost::process::detail::uses_handles
43 close_fds_(const Range &fds) : fds_(fds) {}
45 template <class PosixExecutor>
46 void on_exec_setup(PosixExecutor& e) const
48 for (auto & fd_ : fds_)
49 if (::close(fd_) == -1)
51 e.set_error(::boost::process::detail::get_last_error(), "close() failed");
56 Range& get_used_handles() {return fds_;}
64 template <class FileDescriptor>
65 struct bind_fd_ : handler_base_ext, ::boost::process::detail::uses_handles
68 bind_fd_(int id, const FileDescriptor &fd) : id_(id), fd_(fd) {}
70 template <class PosixExecutor>
71 void on_exec_setup(PosixExecutor& e) const
73 if (::dup2(fd_, id_) == -1)
74 e.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
77 std::array<int, 2> get_used_handles() {return {id_, fd_};}
89 close_fd_ close(int _fd) const {return close_fd_(_fd);}
90 close_fds_<std::vector<int>> close(const std::initializer_list<int> & vec) const {return std::vector<int>(vec);}
91 template<typename Range>
92 close_fds_<Range> close(const Range & r) const {return r;}
94 template <class FileDescriptor>
95 bind_fd_<FileDescriptor> bind(int id, const FileDescriptor & fd) const {return {id, fd};}