From 3ae250966d0985a5b3829702884d5790bf9fa8c3 Mon Sep 17 00:00:00 2001 From: Kenton Varda Date: Tue, 24 Dec 2024 11:31:14 -0600 Subject: [PATCH 1/3] Update KJ to get KJ_SYSCALL_FD. KJ PR: https://github.com/capnproto/capnproto/pull/2205 --- build/deps/gen/dep_capnp_cpp.bzl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/build/deps/gen/dep_capnp_cpp.bzl b/build/deps/gen/dep_capnp_cpp.bzl index b22f51a44f2..4eb88898c5f 100644 --- a/build/deps/gen/dep_capnp_cpp.bzl +++ b/build/deps/gen/dep_capnp_cpp.bzl @@ -2,11 +2,11 @@ load("@//:build/http.bzl", "http_archive") -URL = "https://github.com/capnproto/capnproto/tarball/327a56b51f50aaf762d85af501807dce96d9bbcb" -STRIP_PREFIX = "capnproto-capnproto-327a56b/c++" -SHA256 = "d22adc3479740024036497bbf79adeffe0d15c98271869d1a7808fc42882fa1a" +URL = "https://github.com/capnproto/capnproto/tarball/1c676b2df7f97220607591a38c28ce7e4a968ad4" +STRIP_PREFIX = "capnproto-capnproto-1c676b2/c++" +SHA256 = "9281b860a778c9427c55be647d1247d9f8373ab9023e671505881bf2172eba04" TYPE = "tgz" -COMMIT = "327a56b51f50aaf762d85af501807dce96d9bbcb" +COMMIT = "1c676b2df7f97220607591a38c28ce7e4a968ad4" def dep_capnp_cpp(): http_archive( From baa362d5f2f54cd4e491d0a1cfd38c2ccda6cdd7 Mon Sep 17 00:00:00 2001 From: Kenton Varda Date: Tue, 24 Dec 2024 11:31:39 -0600 Subject: [PATCH 2/3] Use KJ_SYSCALL_FD. Turns out this is the only file in this repo that uses KJ_SYSCALL at all! --- src/workerd/server/workerd.c++ | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/workerd/server/workerd.c++ b/src/workerd/server/workerd.c++ index 2388a8742dc..4b11d413d2a 100644 --- a/src/workerd/server/workerd.c++ +++ b/src/workerd/server/workerd.c++ @@ -200,9 +200,7 @@ class FileWatcher { kj::HashMap> filesWatched; static kj::AutoCloseFd makeInotify() { - int fd; - KJ_SYSCALL(fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC)); - return kj::AutoCloseFd(fd); + return KJ_SYSCALL_FD(inotify_init1(IN_NONBLOCK | IN_CLOEXEC)); } }; @@ -233,17 +231,13 @@ class FileWatcher { KJ_IF_SOME(fd, f.getFd()) { // We need to duplicate the FD because the original will probably be closed later and // closing the FD unregisters it from kqueue. - int duped; - KJ_SYSCALL(duped = dup(fd)); - watchFd(kj::AutoCloseFd(duped)); + watchFd(KJ_SYSCALL_FD(dup(fd))); return; } } // No existing file, open from disk. - int fd; - KJ_SYSCALL(fd = open(path.toNativeString(true).cStr(), O_RDONLY)); - watchFd(kj::AutoCloseFd(fd)); + watchFd(KJ_SYSCALL_FD(open(path.toNativeString(true).cStr(), O_RDONLY))); } kj::Promise onChange() { @@ -275,9 +269,7 @@ class FileWatcher { kj::Vector filesWatched; static kj::AutoCloseFd makeKqueue() { - int fd_; - KJ_SYSCALL(fd_ = kqueue()); - auto fd = kj::AutoCloseFd(fd_); + auto fd = KJ_SYSCALL_FD(kqueue()); KJ_SYSCALL(fcntl(fd, F_SETFD, FD_CLOEXEC)); return kj::mv(fd); } From b3454c23c0f16dc383dbf485e7e6e05f59e9a669 Mon Sep 17 00:00:00 2001 From: Kenton Varda Date: Tue, 24 Dec 2024 11:34:46 -0600 Subject: [PATCH 3/3] Rename: kj::AutoCloseFd -> kj::OwnFd --- src/workerd/server/workerd.c++ | 14 +++++++------- src/workerd/util/perfetto-tracing.c++ | 10 +++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/workerd/server/workerd.c++ b/src/workerd/server/workerd.c++ index 4b11d413d2a..d7f1403cc0e 100644 --- a/src/workerd/server/workerd.c++ +++ b/src/workerd/server/workerd.c++ @@ -193,13 +193,13 @@ class FileWatcher { } private: - kj::AutoCloseFd inotifyFd; + kj::OwnFd inotifyFd; kj::UnixEventPort::FdObserver observer; kj::HashMap watches; kj::HashMap> filesWatched; - static kj::AutoCloseFd makeInotify() { + static kj::OwnFd makeInotify() { return KJ_SYSCALL_FD(inotify_init1(IN_NONBLOCK | IN_CLOEXEC)); } }; @@ -264,17 +264,17 @@ class FileWatcher { } private: - kj::AutoCloseFd kqueueFd; + kj::OwnFd kqueueFd; kj::UnixEventPort::FdObserver observer; - kj::Vector filesWatched; + kj::Vector filesWatched; - static kj::AutoCloseFd makeKqueue() { + static kj::OwnFd makeKqueue() { auto fd = KJ_SYSCALL_FD(kqueue()); KJ_SYSCALL(fcntl(fd, F_SETFD, FD_CLOEXEC)); return kj::mv(fd); } - void watchFd(kj::AutoCloseFd fd) { + void watchFd(kj::OwnFd fd) { KJ_SYSCALL(fcntl(fd, F_SETFD, FD_CLOEXEC)); struct kevent change; @@ -1439,7 +1439,7 @@ class CliMain final: public SchemaFileImpl::ErrorReporter { if (fd < 0) { return kj::none; } - return ExeInfo{kj::str(path), kj::newDiskFile(kj::AutoCloseFd(fd))}; + return ExeInfo{kj::str(path), kj::newDiskFile(kj::OwnFd(fd))}; } #endif diff --git a/src/workerd/util/perfetto-tracing.c++ b/src/workerd/util/perfetto-tracing.c++ index e9051d3963a..c3a7edf03e3 100644 --- a/src/workerd/util/perfetto-tracing.c++ +++ b/src/workerd/util/perfetto-tracing.c++ @@ -18,10 +18,10 @@ PERFETTO_TRACK_EVENT_STATIC_STORAGE_IN_NAMESPACE(workerd::traces); namespace workerd { namespace { -kj::AutoCloseFd openTraceFile(kj::StringPtr path) { +kj::OwnFd openTraceFile(kj::StringPtr path) { int fd = open(path.cStr(), O_RDWR | O_CREAT | O_TRUNC, 0600); KJ_REQUIRE(fd >= 0, "Unable to open tracing file"); - return kj::AutoCloseFd(fd); + return kj::OwnFd(fd); } void initializePerfettoOnce() { @@ -79,10 +79,10 @@ kj::Array> PerfettoSession::parseCategories(kj::StringP } struct PerfettoSession::Impl { - kj::AutoCloseFd fd; + kj::OwnFd fd; std::unique_ptr session; - Impl(kj::AutoCloseFd dest, kj::StringPtr categories) + Impl(kj::OwnFd dest, kj::StringPtr categories) : fd(kj::mv(dest)), session(createTracingSession(fd.get(), categories)) { session->StartBlocking(); @@ -93,7 +93,7 @@ PerfettoSession::PerfettoSession(kj::StringPtr path, kj::StringPtr categories) : impl(kj::heap(openTraceFile(path), categories)) {} PerfettoSession::PerfettoSession(int fd, kj::StringPtr categories) - : impl(kj::heap(kj::AutoCloseFd(fd), categories)) {} + : impl(kj::heap(kj::OwnFd(fd), categories)) {} PerfettoSession::~PerfettoSession() noexcept(false) { if (impl) {