Skip to content

Commit 8f7f545

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 982d7f7 commit 8f7f545

File tree

2 files changed

+11
-25
lines changed

2 files changed

+11
-25
lines changed

src/vmm/src/builder.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,6 @@ pub fn build_microvm_for_boot(
272272
)?;
273273

274274
let vmm = Vmm {
275-
events_observer: Some(std::io::stdin()),
276275
instance_info: instance_info.clone(),
277276
shutdown_exit_code: None,
278277
kvm,
@@ -473,7 +472,6 @@ pub fn build_microvm_from_snapshot(
473472
DeviceManager::restore(device_ctor_args, &microvm_state.device_states)?;
474473

475474
let mut vmm = Vmm {
476-
events_observer: Some(std::io::stdin()),
477475
instance_info: instance_info.clone(),
478476
shutdown_exit_code: None,
479477
kvm,
@@ -722,7 +720,6 @@ pub(crate) mod tests {
722720
let (_, vcpus_exit_evt) = vm.create_vcpus(1).unwrap();
723721

724722
Vmm {
725-
events_observer: Some(std::io::stdin()),
726723
instance_info: InstanceInfo::default(),
727724
shutdown_exit_code: None,
728725
kvm,

src/vmm/src/lib.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,6 @@ pub enum VmmError {
245245
Vm(#[from] vstate::vm::VmError),
246246
/// Kvm error: {0}
247247
Kvm(#[from] vstate::kvm::KvmError),
248-
/// Error thrown by observer object on Vmm initialization: {0}
249-
VmmObserverInit(vmm_sys_util::errno::Error),
250-
/// Error thrown by observer object on Vmm teardown: {0}
251-
VmmObserverTeardown(vmm_sys_util::errno::Error),
252248
/// VMGenID error: {0}
253249
VMGenID(#[from] VmGenIdError),
254250
/// Failed perform action on device: {0}
@@ -293,7 +289,6 @@ pub enum DumpCpuConfigError {
293289
/// Contains the state and associated methods required for the Firecracker VMM.
294290
#[derive(Debug)]
295291
pub struct Vmm {
296-
events_observer: Option<std::io::Stdin>,
297292
/// The [`InstanceInfo`] state of this [`Vmm`].
298293
pub instance_info: InstanceInfo,
299294
shutdown_exit_code: Option<FcExitCode>,
@@ -343,17 +338,16 @@ impl Vmm {
343338
let vcpu_count = vcpus.len();
344339
let barrier = Arc::new(Barrier::new(vcpu_count + 1));
345340

346-
if let Some(stdin) = self.events_observer.as_mut() {
347-
// Set raw mode for stdin.
348-
stdin.lock().set_raw_mode().inspect_err(|&err| {
349-
warn!("Cannot set raw mode for the terminal. {:?}", err);
350-
})?;
341+
let stdin = std::io::stdin().lock();
342+
// Set raw mode for stdin.
343+
stdin.set_raw_mode().inspect_err(|&err| {
344+
warn!("Cannot set raw mode for the terminal. {:?}", err);
345+
})?;
351346

352-
// Set non blocking stdin.
353-
stdin.lock().set_non_block(true).inspect_err(|&err| {
354-
warn!("Cannot set non block for the terminal. {:?}", err);
355-
})?;
356-
}
347+
// Set non blocking stdin.
348+
stdin.set_non_block(true).inspect_err(|&err| {
349+
warn!("Cannot set non block for the terminal. {:?}", err);
350+
})?;
357351

358352
self.vcpus_handles.reserve(vcpu_count);
359353

@@ -760,13 +754,8 @@ impl Drop for Vmm {
760754
// has already been stopped by the event manager at this point.
761755
self.stop(self.shutdown_exit_code.unwrap_or(FcExitCode::Ok));
762756

763-
if let Some(observer) = self.events_observer.as_mut() {
764-
let res = observer.lock().set_canon_mode().inspect_err(|&err| {
765-
warn!("Cannot set canonical mode for the terminal. {:?}", err);
766-
});
767-
if let Err(err) = res {
768-
warn!("{}", VmmError::VmmObserverTeardown(err));
769-
}
757+
if let Err(err) = std::io::stdin().lock().set_canon_mode() {
758+
warn!("Cannot set canonical mode for the terminal. {:?}", err);
770759
}
771760

772761
// Write the metrics before exiting.

0 commit comments

Comments
 (0)