William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 1 | #include <fcntl.h> |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 2 | |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 3 | #include <stdplus/fd/dupable.hpp> |
| 4 | #include <stdplus/fd/ops.hpp> |
| 5 | #include <stdplus/util/cexec.hpp> |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 6 | |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
| 9 | namespace stdplus |
| 10 | { |
| 11 | namespace fd |
| 12 | { |
| 13 | namespace detail |
| 14 | { |
| 15 | |
| 16 | int ref(const int& fd) |
| 17 | { |
| 18 | return CHECK_ERRNO(fcntl(fd, F_DUPFD_CLOEXEC, fd), "fcntl dupfd_cloexec"); |
| 19 | } |
| 20 | |
| 21 | } // namespace detail |
| 22 | |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 23 | DupableFd::DupableFd() noexcept : handle(std::nullopt) {} |
William A. Kennington III | 73a20c4 | 2021-06-29 18:13:12 -0700 | [diff] [blame] | 24 | |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 25 | DupableFd::DupableFd(const int& fd) : handle(fd) {} |
William A. Kennington III | eac9d47 | 2020-08-03 13:57:14 -0700 | [diff] [blame] | 26 | |
| 27 | DupableFd::DupableFd(int&& fd) : handle(std::move(fd)) |
| 28 | { |
| 29 | fd::setFdFlags(*this, fd::getFdFlags(*this).set(fd::FdFlag::CloseOnExec)); |
| 30 | } |
| 31 | |
| 32 | int DupableFd::release() |
| 33 | { |
| 34 | return handle.release(); |
| 35 | } |
| 36 | |
| 37 | int DupableFd::get() const |
| 38 | { |
| 39 | return handle.value(); |
| 40 | } |
| 41 | |
| 42 | } // namespace fd |
| 43 | } // namespace stdplus |