Skip to content

Commit

Permalink
Set CLOEXEC flag on perf_event_open fds
Browse files Browse the repository at this point in the history
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:
jimblandy/perf-event#29

[0]: rust-lang/rust#24237
  • Loading branch information
Phantomical committed Apr 20, 2023
1 parent 6ba0df7 commit e1a19d7
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions perf-event/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e1a19d7

Please sign in to comment.