From 9bae85429257202d32b001bb33841cd4b3296147 Mon Sep 17 00:00:00 2001 From: Phantomical Date: Thu, 13 Apr 2023 21:10:49 -0700 Subject: [PATCH] Set CLOEXEC flag on perf_event_open fds This appears to be what the rust stdlib does (see [0]) so following along with their conventions is probably best. This commit was inspired by this pr to perf-event: https://github.com/jimblandy/perf-event/pull/29 [0]: https://github.com/rust-lang/rust/issues/24237 --- perf-event/src/builder.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/perf-event/src/builder.rs b/perf-event/src/builder.rs index d2678d6..c91794b 100644 --- a/perf-event/src/builder.rs +++ b/perf-event/src/builder.rs @@ -153,6 +153,13 @@ impl<'a> Builder<'a> { let (pid, flags) = self.who.as_args(); let group_fd = group_fd.unwrap_or(-1); + // Enable CLOEXEC by default. This the behaviour that the rust stdlib + // uses for all its file descriptors. + // + // If you need to get a perf_event_open fd which does not have CLOEXEC + // set then you can modify the flags after the fact with fcntl(2). + let flags = flags | sys::bindings::PERF_FLAG_FD_CLOEXEC; + let mut attrs = self.attrs; let file = unsafe {