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