• R/O
  • SSH

posixpp: 提交

The main posixpp library and associated tests.


Commit MetaInfo

修订版adc44f7e5a6f03e87a7b6a306f4ffec4455afe4e (tree)
时间2022-01-11 04:22:51
作者Eric Hopper <hopper@omni...>
CommiterEric Hopper

Log Message

Add support for fcntl + wrapper for F_DUPFD and F_DUPFD_CLOEXEC ...
fcntl commands.

更改概述

差异

diff -r 857a394ad592 -r adc44f7e5a6f pubincludes/posixpp/fd.h
--- a/pubincludes/posixpp/fd.h Mon Jan 10 11:19:25 2022 -0800
+++ b/pubincludes/posixpp/fd.h Mon Jan 10 11:22:51 2022 -0800
@@ -122,6 +122,40 @@
122122 );
123123 }
124124 }
125+
126+ /**
127+ * \brief See man page fcntl(2), the section of FD_DUPFD and FD_DUPFD_CLOEXEC.
128+ *
129+ * Sort of like a copy constructor. Like dup, the new file descriptor created
130+ * will refer to the same underlying kernel file handle.
131+ *
132+ * @param minval The minimum allowed integer value for the new file
133+ * descriptor. This, or some greater value will be picked, but whichever
134+ * value is picked, it will not have previously referred to any open file.
135+ *
136+ * @param cloexec Whether or not the close on exec flag should be set on the
137+ * new file descriptor.
138+ *
139+ * @return The new file descriptor, or an error.
140+ */
141+ [[nodiscard]] expected<fd>
142+ dup_to_unused(unsigned int minval, bool cloexec=false) const noexcept
143+ {
144+ // TODO: Fix hardcoded F_DUPFD and F_DUPFD_CLOEXEC values.
145+ static constexpr int F_DUPFD = 0;
146+ static constexpr int F_DUPFD_CLOEXEC = 1030;
147+ using ::syscalls::linux::fcntl;
148+ void * const arg = reinterpret_cast<void *>(minval);
149+ if (!cloexec) {
150+ return error_cascade(
151+ fcntl(fd_, F_DUPFD, arg), int_to_fd
152+ );
153+ } else {
154+ return error_cascade(
155+ fcntl(fd_, F_DUPFD_CLOEXEC, arg), int_to_fd
156+ );
157+ }
158+ }
125159 //! @}
126160
127161 protected:
diff -r 857a394ad592 -r adc44f7e5a6f pubincludes/syscalls/linux/simple_io.h
--- a/pubincludes/syscalls/linux/simple_io.h Mon Jan 10 11:19:25 2022 -0800
+++ b/pubincludes/syscalls/linux/simple_io.h Mon Jan 10 11:22:51 2022 -0800
@@ -45,4 +45,9 @@
4545 return syscall_expected(call_id::dup3, oldfd, newfd, flags);
4646 }
4747
48+inline expected_t fcntl(int fd, int cmd, void *val) noexcept
49+{
50+ return syscall_expected(call_id::fcntl, fd, val);
51+}
52+
4853 } // namespace syscalls::linux
Show on old repository browser