blob: d096d04cf8b12964938ea6225dedda5c55a17990 [file] [log] [blame]
William A. Kennington IIIeac9d472020-08-03 13:57:14 -07001#include <fcntl.h>
Patrick Williamsd1984dd2023-05-10 16:12:44 -05002
William A. Kennington IIIeac9d472020-08-03 13:57:14 -07003#include <stdplus/fd/dupable.hpp>
4#include <stdplus/fd/ops.hpp>
5#include <stdplus/util/cexec.hpp>
Patrick Williamsd1984dd2023-05-10 16:12:44 -05006
William A. Kennington IIIeac9d472020-08-03 13:57:14 -07007#include <utility>
8
9namespace stdplus
10{
11namespace fd
12{
13namespace detail
14{
15
16int 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 Williamsd1984dd2023-05-10 16:12:44 -050023DupableFd::DupableFd() noexcept : handle(std::nullopt) {}
William A. Kennington III73a20c42021-06-29 18:13:12 -070024
Patrick Williamsd1984dd2023-05-10 16:12:44 -050025DupableFd::DupableFd(const int& fd) : handle(fd) {}
William A. Kennington IIIeac9d472020-08-03 13:57:14 -070026
27DupableFd::DupableFd(int&& fd) : handle(std::move(fd))
28{
29 fd::setFdFlags(*this, fd::getFdFlags(*this).set(fd::FdFlag::CloseOnExec));
30}
31
32int DupableFd::release()
33{
34 return handle.release();
35}
36
37int DupableFd::get() const
38{
39 return handle.value();
40}
41
42} // namespace fd
43} // namespace stdplus