Skip to content

Commit

Permalink
Merge pull request #3271 from cloudflare/kenton/KJ_SYSCALL_FD
Browse files Browse the repository at this point in the history
Use KJ_SYSCALL_FD and kj::OwnFd
  • Loading branch information
kentonv authored Dec 27, 2024
2 parents 7bef4f8 + b3454c2 commit f549cdc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 28 deletions.
8 changes: 4 additions & 4 deletions build/deps/gen/dep_capnp_cpp.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
30 changes: 11 additions & 19 deletions src/workerd/server/workerd.c++
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,14 @@ class FileWatcher {
}

private:
kj::AutoCloseFd inotifyFd;
kj::OwnFd inotifyFd;
kj::UnixEventPort::FdObserver observer;

kj::HashMap<kj::String, int> watches;
kj::HashMap<int, kj::HashSet<kj::String>> filesWatched;

static kj::AutoCloseFd makeInotify() {
int fd;
KJ_SYSCALL(fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC));
return kj::AutoCloseFd(fd);
static kj::OwnFd makeInotify() {
return KJ_SYSCALL_FD(inotify_init1(IN_NONBLOCK | IN_CLOEXEC));
}
};

Expand Down Expand Up @@ -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<void> onChange() {
Expand All @@ -270,19 +264,17 @@ class FileWatcher {
}

private:
kj::AutoCloseFd kqueueFd;
kj::OwnFd kqueueFd;
kj::UnixEventPort::FdObserver observer;
kj::Vector<kj::AutoCloseFd> filesWatched;
kj::Vector<kj::OwnFd> filesWatched;

static kj::AutoCloseFd makeKqueue() {
int fd_;
KJ_SYSCALL(fd_ = kqueue());
auto fd = kj::AutoCloseFd(fd_);
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;
Expand Down Expand Up @@ -1447,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

Expand Down
10 changes: 5 additions & 5 deletions src/workerd/util/perfetto-tracing.c++
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -79,10 +79,10 @@ kj::Array<kj::ArrayPtr<const char>> PerfettoSession::parseCategories(kj::StringP
}

struct PerfettoSession::Impl {
kj::AutoCloseFd fd;
kj::OwnFd fd;
std::unique_ptr<perfetto::TracingSession> 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();
Expand All @@ -93,7 +93,7 @@ PerfettoSession::PerfettoSession(kj::StringPtr path, kj::StringPtr categories)
: impl(kj::heap<Impl>(openTraceFile(path), categories)) {}

PerfettoSession::PerfettoSession(int fd, kj::StringPtr categories)
: impl(kj::heap<Impl>(kj::AutoCloseFd(fd), categories)) {}
: impl(kj::heap<Impl>(kj::OwnFd(fd), categories)) {}

PerfettoSession::~PerfettoSession() noexcept(false) {
if (impl) {
Expand Down

0 comments on commit f549cdc

Please sign in to comment.