Skip to content

Commit 28f7ec2

Browse files
committed
refactor: clean up "event_observer" logic
The `event_observer` field in the Vmm struct is of type Option<Stdin>. There are two problems 1. With the way the code is written, it will never be `None` 2. `Stdin` is a singleton, there is no need to store it _anywhere_. With that in mind, we can just remove this field, and update its two uses to just directly operate on std::io::stdin(). Since it never `None', we can also remove the logic that matches and handles the `None` case. Furthermore, the `Drop` impl used to print the same error message twice in case resetting stdin to canonical mode failed, so fix that to only print it once. Signed-off-by: Patrick Roy <[email protected]>
1 parent ad6bf18 commit 28f7ec2

File tree

2 files changed

+11
-24
lines changed

2 files changed

+11
-24
lines changed

src/vmm/src/builder.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ fn create_vmm_and_vcpus(
178178
};
179179

180180
let vmm = Vmm {
181-
events_observer: Some(std::io::stdin()),
182181
instance_info: instance_info.clone(),
183182
shutdown_exit_code: None,
184183
kvm,
@@ -900,7 +899,6 @@ pub(crate) mod tests {
900899
let (_, vcpus_exit_evt) = vm.create_vcpus(1).unwrap();
901900

902901
Vmm {
903-
events_observer: Some(std::io::stdin()),
904902
instance_info: InstanceInfo::default(),
905903
shutdown_exit_code: None,
906904
kvm,

src/vmm/src/lib.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,6 @@ pub enum VmmError {
253253
Vm(#[from] vstate::vm::VmError),
254254
/// Kvm error: {0}
255255
Kvm(#[from] vstate::kvm::KvmError),
256-
/// Error thrown by observer object on Vmm initialization: {0}
257-
VmmObserverInit(vmm_sys_util::errno::Error),
258-
/// Error thrown by observer object on Vmm teardown: {0}
259-
VmmObserverTeardown(vmm_sys_util::errno::Error),
260256
/// VMGenID error: {0}
261257
VMGenID(#[from] VmGenIdError),
262258
}
@@ -299,7 +295,6 @@ pub enum DumpCpuConfigError {
299295
/// Contains the state and associated methods required for the Firecracker VMM.
300296
#[derive(Debug)]
301297
pub struct Vmm {
302-
events_observer: Option<std::io::Stdin>,
303298
/// The [`InstanceInfo`] state of this [`Vmm`].
304299
pub instance_info: InstanceInfo,
305300
shutdown_exit_code: Option<FcExitCode>,
@@ -363,17 +358,16 @@ impl Vmm {
363358
let vcpu_count = vcpus.len();
364359
let barrier = Arc::new(Barrier::new(vcpu_count + 1));
365360

366-
if let Some(stdin) = self.events_observer.as_mut() {
367-
// Set raw mode for stdin.
368-
stdin.lock().set_raw_mode().inspect_err(|&err| {
369-
warn!("Cannot set raw mode for the terminal. {:?}", err);
370-
})?;
361+
let stdin = std::io::stdin().lock();
362+
// Set raw mode for stdin.
363+
stdin.set_raw_mode().inspect_err(|&err| {
364+
warn!("Cannot set raw mode for the terminal. {:?}", err);
365+
})?;
371366

372-
// Set non blocking stdin.
373-
stdin.lock().set_non_block(true).inspect_err(|&err| {
374-
warn!("Cannot set non block for the terminal. {:?}", err);
375-
})?;
376-
}
367+
// Set non blocking stdin.
368+
stdin.set_non_block(true).inspect_err(|&err| {
369+
warn!("Cannot set non block for the terminal. {:?}", err);
370+
})?;
377371

378372
self.vcpus_handles.reserve(vcpu_count);
379373

@@ -850,13 +844,8 @@ impl Drop for Vmm {
850844
// has already been stopped by the event manager at this point.
851845
self.stop(self.shutdown_exit_code.unwrap_or(FcExitCode::Ok));
852846

853-
if let Some(observer) = self.events_observer.as_mut() {
854-
let res = observer.lock().set_canon_mode().inspect_err(|&err| {
855-
warn!("Cannot set canonical mode for the terminal. {:?}", err);
856-
});
857-
if let Err(err) = res {
858-
warn!("{}", VmmError::VmmObserverTeardown(err));
859-
}
847+
if let Err(err) = std::io::stdin().lock().set_canon_mode() {
848+
warn!("Cannot set canonical mode for the terminal. {:?}", err);
860849
}
861850

862851
// Write the metrics before exiting.

0 commit comments

Comments
 (0)