feat(krun): add exit signal handlers, exit handle, and custom console backends#44
Merged
Merged
Conversation
… backends Add cross-platform SIGTERM/SIGUSR1 signal handlers that trigger graceful VMM shutdown through the exit event fd, ensuring exit observers run before process termination. - Add ExitHandle type for triggering VM exit from any thread (cloneable, thread-safe, async-signal-safe) - Add exit_signal module with POSIX sigaction-based handlers that work on both Linux and macOS - Propagate exit code through the observer chain: on_exit callbacks now receive the i32 exit code, and Vm exposes exit_code() as a shared Arc<AtomicI32> - Add ConsolePortBackend trait for custom in-process console port I/O without file descriptors on the data path - Add PortConfig::Custom variant with input/output adapters for ConsolePortBackend - Move exit EventFd and exit code creation to callers (VmBuilder/libkrun) so they can be shared before VM entry - Update build_microvm signature to accept pre-created exit_evt and exit_code parameters
- Fix alphabetical ordering of re-exports in krun/lib.rs and module declarations in vmm/lib.rs to satisfy cargo fmt - Cast signal handler through *const () before usize to fix -D function-casts-as-integer lint error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ExitHandletype for triggering VM exit programmatically from any thread (cloneable, thread-safe, async-signal-safe)on_exitcallbacks receive thei32exit code, and exposeVm::exit_code()as a sharedArc<AtomicI32>ConsolePortBackendtrait andPortConfig::Customvariant for custom in-process console port I/O without file descriptors on the data pathChanges
src/vmm/src/exit_signal.rs: POSIXsigaction-based handlers for SIGTERM and SIGUSR1 that write to the VMM exit event fd, working on both Linux and macOSsrc/krun/src/api/exit_handle.rs:ExitHandlestruct that dups the exit event fd write end and exposes atrigger()method, withClone/Send/Syncimplementationssrc/krun/src/backends/console.rs: Re-exportsConsolePortBackendfrom the devices cratesrc/devices/src/virtio/console/port_io.rs: AddedConsolePortBackendtrait withread/write/read_wake_fdmethods, plusConsolePortBackendInputAdapterandConsolePortBackendOutputAdapterthat bridge the trait toPortInput/PortOutputsrc/devices/src/virtio/device.rs: ChangedVmmExitObserver::on_vmm_exit()and its blanketFnimpl to acceptexit_code: i32src/krun/src/api/builder.rs:on_exitcallback signature changed fromFn()toFn(i32), pre-createsexit_evtandexit_codeand passes them toVm::new()src/krun/src/api/builders.rs: AddedConsoleBuilder::custom()method that wraps aConsolePortBackendintoPortConfig::Customsrc/krun/src/api/vm.rs: AddedVm::exit_handle()andVm::exit_code()public methods; updatedenter()to passexit_evtandexit_codetobuild_microvmsrc/vmm/src/builder.rs:build_microvmnow accepts pre-createdexit_evt: EventFdandexit_code: Arc<AtomicI32>instead of creating them internally; registers exit signal handlers; addedPortConfig::Customarm increate_explicit_ports;attach_console_devicestakes ownedVirtioConsoleConfigModeinstead of a referencesrc/vmm/src/lib.rs:notify_exit_observersandstoppropagateexit_code: i32src/vmm/src/resources.rs: AddedPortConfig::Customvariant with boxedPortInput/PortOutput; removedDebug/Clonederives fromPortConfigsrc/libkrun/src/lib.rs: Updatedkrun_start_enterto create and passexit_evt/exit_codetobuild_microvmexamples/rust_vm/src/main.rs:on_exitcallback now receives and prints the exit codeTest Plan
cargo buildto verify compilation succeeds across all cratescargo testto verify existing unit tests pass (including updatedVm::new()signatures invm.rstests)rust_vmexample compiles:cargo build -p rust_vmExitHandle::trigger()from a background thread to verify programmatic shutdown works