• R/O
  • SSH

posixpp: Commit

The main posixpp library and associated tests.


Commit MetaInfo

Revisiond30c74a036b553087ea39b939f8ef9fe5a336aec (tree)
Time2021-04-30 01:31:19
AuthorEric Hopper <hopper@omni...>
CommiterEric Hopper

Log Message

Implement dup3 as well, and add TODO for test case.

Change Summary

Incremental Difference

diff -r 2737e2304026 -r d30c74a036b5 pubincludes/posixpp/fd.h
--- a/pubincludes/posixpp/fd.h Thu Apr 29 09:31:13 2021 -0700
+++ b/pubincludes/posixpp/fd.h Thu Apr 29 09:31:19 2021 -0700
@@ -4,6 +4,7 @@
44 #include <posixpp/fdflags.h>
55 #include <posixpp/modeflags.h>
66 #include <syscalls/linux/simple_io.h>
7+#include <optional>
78
89 namespace posixpp {
910
@@ -56,10 +57,25 @@
5657 return error_cascade(dup(fd_), int_to_fd);
5758 }
5859
59- //! See man page dup2(2)
60- [[nodiscard]] expected<void> dup2(fd &newfd) const noexcept {
60+ //! See man page dup2(2) or dup3(2) (if the cloexec flag has a value).
61+ [[nodiscard]] expected<void> dup2(
62+ fd &newfd,
63+ ::std::optional<bool> cloexec=::std::optional<bool>{}
64+ ) const noexcept
65+ {
6166 using ::syscalls::linux::dup2;
62- return error_cascade_void(dup2(fd_, newfd.as_fd()));
67+ using ::syscalls::linux::dup3;
68+ if (!cloexec.has_value()) {
69+ return error_cascade_void(dup2(fd_, newfd.fd_));
70+ } else {
71+ auto const flagval = cloexec.value() ? fdflags::cloexec : fdflags{};
72+ // TODO: Write a test case for this, which is hard because it
73+ // requires the 'exec' system call, and that the exec'ed program
74+ // run part of the test.
75+ return error_cascade_void(
76+ dup3(fd_, newfd.fd_, flagval.getbits())
77+ );
78+ }
6379 }
6480
6581 expected<::std::size_t>
diff -r 2737e2304026 -r d30c74a036b5 pubincludes/syscalls/linux/simple_io.h
--- a/pubincludes/syscalls/linux/simple_io.h Thu Apr 29 09:31:13 2021 -0700
+++ b/pubincludes/syscalls/linux/simple_io.h Thu Apr 29 09:31:19 2021 -0700
@@ -41,5 +41,10 @@
4141 return syscall_expected(call_id::dup2, oldfd, newfd);
4242 }
4343
44+inline expected_t dup3(int oldfd, int newfd, int flags) noexcept
45+{
46+ return syscall_expected(call_id::dup3, oldfd, newfd, flags);
47+}
48+
4449 }
4550 }
diff -r 2737e2304026 -r d30c74a036b5 pubincludes/syscalls/linux/x86_64/fdflags.h
--- a/pubincludes/syscalls/linux/x86_64/fdflags.h Thu Apr 29 09:31:13 2021 -0700
+++ b/pubincludes/syscalls/linux/x86_64/fdflags.h Thu Apr 29 09:31:19 2021 -0700
@@ -19,6 +19,8 @@
1919
2020 public:
2121 friend class openflags;
22+ //! Default empty set
23+ constexpr fdflags() : base_t{0} {}
2224
2325 static const fdflags accmode;
2426 static const fdflags tmpfile;
@@ -63,6 +65,9 @@
6365 friend base_t;
6466
6567 public:
68+ //! Default empty set.
69+ constexpr openflags() : base_t{0} {}
70+ //! All fdflags are also openflags, but not all openflags are fdflags.
6671 explicit constexpr openflags(fdflags const &val) : base_t(val.getbits()) {}
6772
6873 static const openflags creat; //!< O_CREAT
diff -r 2737e2304026 -r d30c74a036b5 pubincludes/syscalls/linux/x86_64/syscall.h
--- a/pubincludes/syscalls/linux/x86_64/syscall.h Thu Apr 29 09:31:13 2021 -0700
+++ b/pubincludes/syscalls/linux/x86_64/syscall.h Thu Apr 29 09:31:19 2021 -0700
@@ -342,6 +342,7 @@
342342 unshare,
343343 epoll_pwait = 281,
344344 epoll_create1 = 291,
345+ dup3 = 292,
345346 syncfs = 306,
346347 setns = 308
347348 };
Show on old repository browser