]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/process/detail/posix/fd.hpp
bd5f153d3731f90658be80ef5d4f12fea05347bc
[ceph.git] / ceph / src / boost / boost / process / detail / posix / fd.hpp
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
10 #ifndef BOOST_PROCESS_DETAIL_POSIX_FD_HPP
11 #define BOOST_PROCESS_DETAIL_POSIX_FD_HPP
12
13 #include <boost/process/detail/posix/handler.hpp>
14 #include <unistd.h>
15 #include <boost/process/detail/used_handles.hpp>
16 #include <array>
17
18 namespace boost { namespace process { namespace detail { namespace posix {
19
20
21 struct close_fd_ : handler_base_ext, ::boost::process::detail::uses_handles
22 {
23 close_fd_(int fd) : fd_(fd) {}
24
25 template <class PosixExecutor>
26 void on_exec_setup(PosixExecutor& e) const
27 {
28 if (::close(fd_) == -1)
29 e.set_error(::boost::process::detail::get_last_error(), "close() failed");
30 }
31
32 int get_used_handles() {return fd_;}
33
34
35 private:
36 int fd_;
37 };
38
39 template <class Range>
40 struct close_fds_ : handler_base_ext, ::boost::process::detail::uses_handles
41 {
42 public:
43 close_fds_(const Range &fds) : fds_(fds) {}
44
45 template <class PosixExecutor>
46 void on_exec_setup(PosixExecutor& e) const
47 {
48 for (auto & fd_ : fds_)
49 if (::close(fd_) == -1)
50 {
51 e.set_error(::boost::process::detail::get_last_error(), "close() failed");
52 break;
53 }
54 }
55
56 Range& get_used_handles() {return fds_;}
57
58 private:
59 Range fds_;
60 };
61
62
63
64 template <class FileDescriptor>
65 struct bind_fd_ : handler_base_ext, ::boost::process::detail::uses_handles
66 {
67 public:
68 bind_fd_(int id, const FileDescriptor &fd) : id_(id), fd_(fd) {}
69
70 template <class PosixExecutor>
71 void on_exec_setup(PosixExecutor& e) const
72 {
73 if (::dup2(fd_, id_) == -1)
74 e.set_error(::boost::process::detail::get_last_error(), "dup2() failed");
75 }
76
77 std::array<int, 2> get_used_handles() {return {id_, fd_};}
78
79
80 private:
81 int id_;
82 FileDescriptor fd_;
83 };
84
85
86 struct fd_
87 {
88 constexpr 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;}
93
94 template <class FileDescriptor>
95 bind_fd_<FileDescriptor> bind(int id, const FileDescriptor & fd) const {return {id, fd};}
96
97 };
98
99
100 }}}}
101
102 #endif