summary refs log tree commit diff
path: root/host/start-vmm/unix.rs
diff options
context:
space:
mode:
Diffstat (limited to 'host/start-vmm/unix.rs')
-rw-r--r--host/start-vmm/unix.rs17
1 files changed, 15 insertions, 2 deletions
diff --git a/host/start-vmm/unix.rs b/host/start-vmm/unix.rs
index 8213497..8475eb4 100644
--- a/host/start-vmm/unix.rs
+++ b/host/start-vmm/unix.rs
@@ -2,8 +2,21 @@
 // SPDX-FileCopyrightText: 2023 Alyssa Ross <hi@alyssa.is>
 
 use std::ffi::c_int;
-use std::os::fd::BorrowedFd;
+use std::io;
+use std::os::fd::{AsFd, BorrowedFd};
 
 extern "C" {
-    pub fn clear_cloexec(fd: BorrowedFd) -> c_int;
+    fn clear_cloexec(fd: BorrowedFd) -> c_int;
 }
+
+pub trait AsFdExt: AsFd {
+    fn clear_cloexec(&self) -> io::Result<()> {
+        // SAFETY: trivial.
+        match unsafe { clear_cloexec(self.as_fd()) } {
+            -1 => Err(io::Error::last_os_error()),
+            _ => Ok(()),
+        }
+    }
+}
+
+impl<T: AsFd> AsFdExt for T {}