blob: ce2cff123b8d1cb761da7f66082bedebb512f158 [file] [log] [blame]
William A. Kennington IIIeac9d472020-08-03 13:57:14 -07001#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
7namespace stdplus
8{
9namespace fd
10{
11namespace detail
12{
13
14int 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 III73a20c42021-06-29 18:13:12 -070021DupableFd::DupableFd() noexcept : handle(std::nullopt)
22{
23}
24
William A. Kennington IIIeac9d472020-08-03 13:57:14 -070025DupableFd::DupableFd(const int& fd) : handle(fd)
26{
27}
28
29DupableFd::DupableFd(int&& fd) : handle(std::move(fd))
30{
31 fd::setFdFlags(*this, fd::getFdFlags(*this).set(fd::FdFlag::CloseOnExec));
32}
33
34int DupableFd::release()
35{
36 return handle.release();
37}
38
39int DupableFd::get() const
40{
41 return handle.value();
42}
43
44} // namespace fd
45} // namespace stdplus