UnixSocketRaw: fix recent regression where we weren't setting CLOEXEC
r.android.com/2120672 incorrectly changed the ctor logic from "set
CLOEXEC" to "unset CLOEXEC". Undo that change.
This is not an exact undo because the ctor used to directly set the
flags (F_SETFD) without querying the existing set (F_GETFD). Technically
FD_CLOEXEC is the only flag, but I've made the ctor do the additional
F_GETFD fcntl syscall anyway to future-proof the code, even though I
don't expect any new flags.
Bug: 240130535
Change-Id: Icac1d6a4a46b10d481479a08b05065f68a4116cc
diff --git a/src/base/unix_socket_unittest.cc b/src/base/unix_socket_unittest.cc
index 45c09d5..c3f2ac7 100644
--- a/src/base/unix_socket_unittest.cc
+++ b/src/base/unix_socket_unittest.cc
@@ -939,6 +939,30 @@
read_slowly_task.Reset();
tx_thread.join();
}
+
+#if !PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA)
+TEST_F(UnixSocketTest, SetsCloexec) {
+ // CLOEXEC set when constructing sockets through helper:
+ {
+ auto raw = UnixSocketRaw::CreateMayFail(base::SockFamily::kUnix,
+ SockType::kStream);
+ int flags = fcntl(raw.fd(), F_GETFD, 0);
+ EXPECT_TRUE(flags & FD_CLOEXEC);
+ }
+ // CLOEXEC set when creating a UnixSocketRaw out of an existing fd:
+ {
+ int fd = socket(AF_UNIX, SOCK_STREAM, 0);
+ int flags = fcntl(fd, F_GETFD, 0);
+ EXPECT_FALSE(flags & FD_CLOEXEC);
+
+ auto raw = UnixSocketRaw(ScopedSocketHandle(fd), base::SockFamily::kUnix,
+ SockType::kStream);
+ flags = fcntl(raw.fd(), F_GETFD, 0);
+ EXPECT_TRUE(flags & FD_CLOEXEC);
+ }
+}
+#endif // !OS_FUCHSIA
+
#endif // !OS_WIN
} // namespace