Skip to content

Commit 2c07f68

Browse files
committed
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: jimblandy/perf-event#29 [0]: rust-lang/rust#24237
1 parent 189db47 commit 2c07f68

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

perf-event/src/builder.rs

+7
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ impl<'a> Builder<'a> {
153153
let (pid, flags) = self.who.as_args();
154154
let group_fd = group_fd.unwrap_or(-1);
155155

156+
// Enable CLOEXEC by default. This the behaviour that the rust stdlib
157+
// uses for all its file descriptors.
158+
//
159+
// If you need to get a perf_event_open fd which does not have CLOEXEC
160+
// set then you can modify the flags after the fact with fcntl(2).
161+
let flags = flags | sys::bindings::PERF_FLAG_FD_CLOEXEC;
162+
156163
let mut attrs = self.attrs;
157164

158165
let file = unsafe {

0 commit comments

Comments
 (0)