qemu_chr_fe_write() reports failure as -1 with the cause in errno, not as a negated errno, so errno::into_io_result() turned every failure into EPERM and a frontend could never see EAGAIN. Build the error from errno instead, in both CharFrontend::write() and the BQL-free variant.
Also add CharFrontend::add_write_watch() and remove_watch(), so that a device whose backend would block can retry once it is writable again. remove_watch() tolerates a source that the backend has already torn down. Signed-off-by: Brian Cain <[email protected]> --- rust/chardev/src/chardev.rs | 82 +++++++++++++++++++++++++++++-------- 1 file changed, 66 insertions(+), 16 deletions(-) diff --git a/rust/chardev/src/chardev.rs b/rust/chardev/src/chardev.rs index fb9674b3fb4..8a3836d78e4 100644 --- a/rust/chardev/src/chardev.rs +++ b/rust/chardev/src/chardev.rs @@ -54,7 +54,11 @@ fn write(&mut self, buf: &[u8]) -> io::Result<usize> { let len = buf.len().try_into().unwrap(); let r = unsafe { bindings::qemu_chr_fe_write(addr_of_mut!(*chr), buf.as_ptr(), len) }; - errno::into_io_result(r).map(|cnt| cnt as usize) + if r < 0 { + Err(io::Error::last_os_error()) + } else { + Ok(r as usize) + } } fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { @@ -62,13 +66,13 @@ fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { let len = buf.len().try_into().unwrap(); let r = unsafe { bindings::qemu_chr_fe_write_all(addr_of_mut!(*chr), buf.as_ptr(), len) }; - errno::into_io_result(r).and_then(|cnt| { - if cnt as usize == buf.len() { - Ok(()) - } else { - Err(ErrorKind::WriteZero.into()) - } - }) + if r < 0 { + Err(io::Error::last_os_error()) + } else if r as usize == buf.len() { + Ok(()) + } else { + Err(ErrorKind::WriteZero.into()) + } } } @@ -193,6 +197,48 @@ pub fn accept_input(&self) { unsafe { bindings::qemu_chr_fe_accept_input(addr_of_mut!(*chr)) } } + /// Watch for output becoming writable or disconnected. + /// + /// # Safety + /// The caller must keep `opaque` valid until the source is removed, and + /// must remove the source before the frontend is destroyed. + pub unsafe fn add_write_watch( + &self, + callback: bindings::FEWatchFunc, + opaque: *mut c_void, + ) -> u32 { + let mut chr = self.inner.borrow_mut(); + unsafe { + bindings::qemu_chr_fe_add_watch( + addr_of_mut!(*chr), + glib_sys::G_IO_OUT | glib_sys::G_IO_HUP, + callback, + opaque, + ) + } + } + + /// Remove a watch previously returned by `add_write_watch`. + pub fn remove_watch(&self, tag: u32) { + if tag == 0 { + return; + } + let chr = self.inner.borrow(); + if chr.chr.is_null() { + return; + } + // SAFETY: the backend and its context remain valid while borrowed. + // Looking up the source handles watches already removed by the backend + // and also works when NULL selects the default context. + unsafe { + let context = (*chr.chr).gcontext; + let source = glib_sys::g_main_context_find_source_by_id(context, tag); + if !source.is_null() { + glib_sys::g_source_destroy(source); + } + } + } + /// Temporarily borrow the character device, allowing it to be used /// as an implementor of `Write`. Note that it is not valid to drop /// the big QEMU lock while the character device is borrowed, as @@ -229,7 +275,11 @@ pub fn write(&self, buf: &[u8]) -> io::Result<usize> { let len = buf.len().try_into().unwrap(); // SAFETY: qemu_chr_fe_write is thread-safe let r = unsafe { bindings::qemu_chr_fe_write(self.inner.as_ptr(), buf.as_ptr(), len) }; - errno::into_io_result(r).map(|cnt| cnt as usize) + if r < 0 { + Err(io::Error::last_os_error()) + } else { + Ok(r as usize) + } } /// Write data to a character backend from the front end. This function @@ -243,13 +293,13 @@ pub fn write_all(&self, buf: &[u8]) -> io::Result<()> { let len = buf.len().try_into().unwrap(); // SAFETY: qemu_chr_fe_write_all is thread-safe let r = unsafe { bindings::qemu_chr_fe_write_all(self.inner.as_ptr(), buf.as_ptr(), len) }; - errno::into_io_result(r).and_then(|cnt| { - if cnt as usize == buf.len() { - Ok(()) - } else { - Err(ErrorKind::WriteZero.into()) - } - }) + if r < 0 { + Err(io::Error::last_os_error()) + } else if r as usize == buf.len() { + Ok(()) + } else { + Err(ErrorKind::WriteZero.into()) + } } } -- 2.34.1
