fd.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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_DETAIL_POSIX_FD_HPP
  10. #define BOOST_PROCESS_DETAIL_POSIX_FD_HPP
  11. #include <boost/process/detail/posix/handler.hpp>
  12. #include <unistd.h>
  13. #include <boost/process/detail/used_handles.hpp>
  14. #include <array>
  15. namespace boost { namespace process { namespace detail { namespace posix {
  16. struct close_fd_ : handler_base_ext, ::boost::process::detail::uses_handles
  17. {
  18. close_fd_(int fd) : fd_(fd) {}
  19. template <class PosixExecutor>
  20. void on_exec_setup(PosixExecutor& e) const
  21. {
  22. if (::close(fd_) == -1)
  23. e.set_error(::boost::process::detail::get_last_error(), "close() failed");
  24. }
  25. int get_used_handles() {return fd_;}
  26. private:
  27. int fd_;
  28. };
  29. template <class Range>
  30. struct close_fds_ : handler_base_ext, ::boost::process::detail::uses_handles
  31. {
  32. public:
  33. close_fds_(const Range &fds) : fds_(fds) {}
  34. template <class PosixExecutor>
  35. void on_exec_setup(PosixExecutor& e) const
  36. {
  37. for (auto & fd_ : fds_)
  38. if (::close(fd_) == -1)
  39. {
  40. e.set_error(::boost::process::detail::get_last_error(), "close() failed");
  41. break;
  42. }
  43. }
  44. Range& get_used_handles() {return fds_;}
  45. private:
  46. Range fds_;
  47. };
  48. template <class FileDescriptor>
  49. struct bind_fd_ : handler_base_ext, ::boost::process::detail::uses_handles
  50. {
  51. public:
  52. bind_fd_(int id, const FileDescriptor &fd) : id_(id), fd_(fd) {}
  53. template <class PosixExecutor>
  54. void on_exec_setup(PosixExecutor& e) const
  55. {
  56. if (::dup2(fd_, id_) == -1)
  57. e.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
  58. }
  59. std::array<int, 2> get_used_handles() {return {id_, fd_};}
  60. private:
  61. int id_;
  62. FileDescriptor fd_;
  63. };
  64. struct fd_
  65. {
  66. constexpr fd_() {};
  67. close_fd_ close(int _fd) const {return close_fd_(_fd);}
  68. close_fds_<std::vector<int>> close(const std::initializer_list<int> & vec) const {return std::vector<int>(vec);}
  69. template<typename Range>
  70. close_fds_<Range> close(const Range & r) const {return r;}
  71. template <class FileDescriptor>
  72. bind_fd_<FileDescriptor> bind(int id, const FileDescriptor & fd) const {return {id, fd};}
  73. };
  74. }}}}
  75. #endif