File indexing completed on 2025-01-18 09:50:06
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_PROCESS_DETAIL_POSIX_FD_HPP
0011 #define BOOST_PROCESS_DETAIL_POSIX_FD_HPP
0012
0013 #include <boost/process/detail/posix/handler.hpp>
0014 #include <unistd.h>
0015 #include <boost/process/detail/used_handles.hpp>
0016 #include <array>
0017
0018 namespace boost { namespace process { namespace detail { namespace posix {
0019
0020
0021 struct close_fd_ : handler_base_ext, ::boost::process::detail::uses_handles
0022 {
0023 close_fd_(int fd) : fd_(fd) {}
0024
0025 template <class PosixExecutor>
0026 void on_exec_setup(PosixExecutor& e) const
0027 {
0028 if (::close(fd_) == -1)
0029 e.set_error(::boost::process::detail::get_last_error(), "close() failed");
0030 }
0031
0032 int get_used_handles() {return fd_;}
0033
0034
0035 private:
0036 int fd_;
0037 };
0038
0039 template <class Range>
0040 struct close_fds_ : handler_base_ext, ::boost::process::detail::uses_handles
0041 {
0042 public:
0043 close_fds_(const Range &fds) : fds_(fds) {}
0044
0045 template <class PosixExecutor>
0046 void on_exec_setup(PosixExecutor& e) const
0047 {
0048 for (auto & fd_ : fds_)
0049 if (::close(fd_) == -1)
0050 {
0051 e.set_error(::boost::process::detail::get_last_error(), "close() failed");
0052 break;
0053 }
0054 }
0055
0056 Range& get_used_handles() {return fds_;}
0057
0058 private:
0059 Range fds_;
0060 };
0061
0062
0063
0064 template <class FileDescriptor>
0065 struct bind_fd_ : handler_base_ext, ::boost::process::detail::uses_handles
0066 {
0067 public:
0068 bind_fd_(int id, const FileDescriptor &fd) : id_(id), fd_(fd) {}
0069
0070 template <class PosixExecutor>
0071 void on_exec_setup(PosixExecutor& e) const
0072 {
0073 if (::dup2(fd_, id_) == -1)
0074 e.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
0075 }
0076
0077 std::array<int, 2> get_used_handles() {return {id_, fd_};}
0078
0079
0080 private:
0081 int id_;
0082 FileDescriptor fd_;
0083 };
0084
0085
0086 struct fd_
0087 {
0088 constexpr fd_() {};
0089 close_fd_ close(int _fd) const {return close_fd_(_fd);}
0090 close_fds_<std::vector<int>> close(const std::initializer_list<int> & vec) const {return std::vector<int>(vec);}
0091 template<typename Range>
0092 close_fds_<Range> close(const Range & r) const {return r;}
0093
0094 template <class FileDescriptor>
0095 bind_fd_<FileDescriptor> bind(int id, const FileDescriptor & fd) const {return {id, fd};}
0096
0097 };
0098
0099
0100 }}}}
0101
0102 #endif