From 21445b1b8378b72f9dfe7a1773499c09763e5367 Mon Sep 17 00:00:00 2001 From: Clarissa Garvey Date: Thu, 16 Jun 2022 20:09:33 +0000 Subject: [PATCH] base: Upstream unix net set_nonblocking fn Bug: b:231641496 Upstream-Crate: base/src/sys/unix Change-Id: I62b07f2fc3f7a02c1b72da735ac41d6dc73416f9 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3708762 Reviewed-by: Noah Gold Tested-by: kokoro Reviewed-by: Vikram Auradkar Commit-Queue: Clarissa Garvey --- base/Cargo.toml | 3 +++ base/src/sys/unix/net.rs | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/base/Cargo.toml b/base/Cargo.toml index a09952b387..68926f475c 100644 --- a/base/Cargo.toml +++ b/base/Cargo.toml @@ -27,6 +27,9 @@ tempfile = "3" thiserror = "1.0.20" uuid = { version = "0.8.2", features = ["v4"] } +[target.'cfg(unix)'.dependencies] +cvt = "*" + [target.'cfg(windows)'.dependencies] lazy_static = "*" rand = "*" diff --git a/base/src/sys/unix/net.rs b/base/src/sys/unix/net.rs index 74c47e01c4..c3148f89dc 100644 --- a/base/src/sys/unix/net.rs +++ b/base/src/sys/unix/net.rs @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +use cvt::cvt; use std::{ cmp::Ordering, convert::TryFrom, @@ -594,6 +595,12 @@ impl UnixSeqpacket { pub fn set_write_timeout(&self, timeout: Option) -> io::Result<()> { self.set_timeout(timeout, libc::SO_SNDTIMEO) } + + /// Sets the blocking mode for this socket. + pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> { + let mut nonblocking = nonblocking as libc::c_int; + cvt(unsafe { libc::ioctl(self.fd, libc::FIONBIO, &mut nonblocking) }).map(drop) + } } impl Drop for UnixSeqpacket { @@ -787,6 +794,12 @@ impl UnixSeqpacketListener { ); Ok(path_os_str.into()) } + + /// Sets the blocking mode for this socket. + pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> { + let mut nonblocking = nonblocking as libc::c_int; + cvt(unsafe { libc::ioctl(self.fd, libc::FIONBIO, &mut nonblocking) }).map(drop) + } } impl Drop for UnixSeqpacketListener {