summary refs log tree commit diff
path: root/sys_util/src/lib.rs
diff options
context:
space:
mode:
authorZach Reizner <zachr@google.com>2019-08-13 11:20:14 -0700
committerCommit Bot <commit-bot@chromium.org>2019-10-10 02:09:13 +0000
commita8adff0ff14f66570a3aa86f6106b55081526be1 (patch)
tree515ed01d01828a8622bac5203d508da708db5d41 /sys_util/src/lib.rs
parent0b6f02fea7716ec5752555ea44aafed214b58faa (diff)
downloadcrosvm-a8adff0ff14f66570a3aa86f6106b55081526be1.tar
crosvm-a8adff0ff14f66570a3aa86f6106b55081526be1.tar.gz
crosvm-a8adff0ff14f66570a3aa86f6106b55081526be1.tar.bz2
crosvm-a8adff0ff14f66570a3aa86f6106b55081526be1.tar.lz
crosvm-a8adff0ff14f66570a3aa86f6106b55081526be1.tar.xz
crosvm-a8adff0ff14f66570a3aa86f6106b55081526be1.tar.zst
crosvm-a8adff0ff14f66570a3aa86f6106b55081526be1.zip
devices: jail serial device
This change plumbs the jail throughout the arch specific device creation
process. It also adds a custom callback support for the ProxyDevice so
that the main process can interrupt the child serial process when it has
incoming bytes.

TEST=crosvm run
BUG=None

Change-Id: I6af7d2cb0acbba9bf42eaeeb294cee2bce4a1f36
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1752589
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Tested-by: Zach Reizner <zachr@chromium.org>
Commit-Queue: Zach Reizner <zachr@chromium.org>
Diffstat (limited to 'sys_util/src/lib.rs')
-rw-r--r--sys_util/src/lib.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/sys_util/src/lib.rs b/sys_util/src/lib.rs
index 7cba7eb..0dab89d 100644
--- a/sys_util/src/lib.rs
+++ b/sys_util/src/lib.rs
@@ -321,3 +321,24 @@ pub fn validate_raw_fd(raw_fd: RawFd) -> Result<RawFd> {
     }
     Ok(dup_fd as RawFd)
 }
+
+/// Utility function that returns true if the given FD is readable without blocking.
+///
+/// On an error, such as an invalid or incompatible FD, this will return false, which can not be
+/// distinguished from a non-ready to read FD.
+pub fn poll_in(fd: &AsRawFd) -> bool {
+    let mut fds = libc::pollfd {
+        fd: fd.as_raw_fd(),
+        events: libc::POLLIN,
+        revents: 0,
+    };
+    // Safe because we give a valid pointer to a list (of 1) FD and check the return value.
+    let ret = unsafe { libc::poll(&mut fds, 1, 0) };
+    // An error probably indicates an invalid FD, or an FD that can't be polled. Returning false in
+    // that case is probably correct as such an FD is unlikely to be readable, although there are
+    // probably corner cases in which that is wrong.
+    if ret == -1 {
+        return false;
+    }
+    fds.revents & libc::POLLIN != 0
+}