Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 20 additions & 4 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/polly/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ license = "Apache-2.0"
repository = "https://github.com/containers/libkrun"

[dependencies]
libc = ">=0.2.39"
utils = { package = "krun-utils", version = "=0.1.0-1.18.0", path="../utils" }

[target.'cfg(unix)'.dependencies]
libc = ">=0.2.39"
38 changes: 30 additions & 8 deletions src/polly/src/event_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
use std::collections::HashMap;
use std::fmt::Formatter;
use std::io;
use std::os::unix::io::{AsRawFd, RawFd};
use std::sync::{Arc, Mutex};

#[cfg(unix)]
use std::os::unix::io::{AsRawFd, RawFd};
#[cfg(target_os = "windows")]
use utils::windows::{AsRawFd, RawFd};

use utils::epoll::{self, Epoll, EpollEvent};

pub type Result<T> = std::result::Result<T, Error>;
Expand All @@ -33,11 +37,13 @@ impl std::fmt::Debug for Error {
Poll(err) => write!(f, "Error during epoll call: {err}"),
AlreadyExists(pollable) => write!(
f,
"A handler for the specified pollable {pollable} already exists."
"A handler for the specified pollable {:?} already exists.",
pollable
),
NotFound(pollable) => write!(
f,
"A handler for the specified pollable {pollable} was not found."
"A handler for the specified pollable {:?} was not found.",
pollable
),
}
}
Expand Down Expand Up @@ -110,7 +116,7 @@ impl EventManager {
let interest_list = subscriber.lock().unwrap().interest_list();

for event in interest_list {
self.register(event.data() as i32, event, subscriber.clone())?
self.register(event.data() as Pollable, event, subscriber.clone())?
}

Ok(())
Expand Down Expand Up @@ -202,6 +208,7 @@ impl EventManager {
&mut self.ready_events[..],
) {
Ok(event_count) => event_count,
#[cfg(unix)]
Err(e) if e.raw_os_error() == Some(libc::EINTR) => 0,
Err(e) => return Err(Error::Poll(e)),
};
Expand Down Expand Up @@ -254,9 +261,19 @@ mod tests {

impl DummySubscriber {
fn new() -> Self {
let event_fd_1 = EventFd::new(0).unwrap();
let event_fd_2 = EventFd::new(0).unwrap();
// On Windows the IOCP only delivers a packet when the underlying
// Event object is signaled. Linux eventfds are always writable
// (EPOLLOUT fires instantly) so this isn't needed there.
#[cfg(target_os = "windows")]
{
event_fd_1.write(1).unwrap();
event_fd_2.write(1).unwrap();
}
DummySubscriber {
event_fd_1: EventFd::new(0).unwrap(),
event_fd_2: EventFd::new(0).unwrap(),
event_fd_1,
event_fd_2,
processed_ev1_in: false,
processed_ev2_in: false,
register_ev2: false,
Expand Down Expand Up @@ -339,7 +356,7 @@ mod tests {

impl Subscriber for DummySubscriber {
fn process(&mut self, event: &EpollEvent, event_manager: &mut EventManager) {
let source = event.data() as i32;
let source = event.data() as Pollable;
let event_set = EventSet::from_bits(event.events()).unwrap();

// We only know how to treat EPOLLOUT and EPOLLIN.
Expand Down Expand Up @@ -521,6 +538,11 @@ mod tests {

let dummy_fd = dummy_subscriber.lock().unwrap().event_fd_1.as_raw_fd();
assert!(event_manager.subscriber(dummy_fd).is_ok());
assert!(event_manager.subscriber(-1).is_err());

#[cfg(unix)]
let bad_fd: Pollable = -1;
#[cfg(windows)]
let bad_fd: Pollable = std::ptr::null_mut();
assert!(event_manager.subscriber(bad_fd).is_err());
}
}
15 changes: 14 additions & 1 deletion src/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ repository = "https://github.com/containers/libkrun"

[dependencies]
bitflags = "1.2.0"
libc = ">=0.2.85"
log = "0.4.0"

[target.'cfg(unix)'.dependencies]
libc = ">=0.2.85"
nix = "0.30.1"
vmm-sys-util = "0.14"
crossbeam-channel = ">=0.5.15"
Expand All @@ -20,3 +22,14 @@ kvm-bindings = { version = "0.12", features = ["fam-wrappers"] }

[target.'cfg(target_os = "macos")'.dependencies]
nix = { version = "0.30.1", features = ["fs"] }

[target.'cfg(target_os = "windows")'.dependencies]
windows-sys = { version = "0.61.2", features = [
"Win32_Foundation",
"Win32_Security",
"Win32_System_IO",
"Win32_System_Performance",
"Win32_System_SystemInformation",
"Win32_System_Threading",
"Win32_System_Time",
] }
7 changes: 7 additions & 0 deletions src/utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

#[cfg(unix)]
pub use vmm_sys_util::{errno, tempdir, tempfile, terminal};
#[cfg(target_os = "linux")]
pub use vmm_sys_util::{eventfd, ioctl};
Expand All @@ -16,6 +17,12 @@ pub mod macos;
pub use macos::epoll;
#[cfg(target_os = "macos")]
pub use macos::eventfd;
#[cfg(target_os = "windows")]
pub mod windows;
#[cfg(target_os = "windows")]
pub use windows::epoll;
#[cfg(target_os = "windows")]
pub use windows::eventfd;
pub mod pollable_channel;
#[cfg(target_arch = "x86_64")]
pub mod rand;
Expand Down
7 changes: 6 additions & 1 deletion src/utils/src/pollable_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ use crate::eventfd::{EventFd, EFD_NONBLOCK, EFD_SEMAPHORE};
use std::collections::VecDeque;
use std::io;
use std::io::ErrorKind;
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, RawFd};
use std::sync::{Arc, Mutex};

#[cfg(target_os = "windows")]
use crate::windows::{AsRawFd, RawFd};
#[cfg(unix)]
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, RawFd};

/// A multiple producer single consumer channel that can be listened to by a file descriptor
pub fn pollable_channel<T: Send>(
) -> io::Result<(PollableChannelSender<T>, PollableChannelReciever<T>)> {
Expand Down Expand Up @@ -82,6 +86,7 @@ impl<T: Send> AsRawFd for PollableChannelReciever<T> {
}
}

#[cfg(unix)]
impl<T: Send> AsFd for PollableChannelReciever<T> {
fn as_fd(&self) -> BorrowedFd<'_> {
// SAFETY: The lifetime of the fd is the same as the lifetime of self.inner.eventfd which
Expand Down
Loading
Loading