fix: arm the root switch from signals that arrive with a disconnect
This commit is contained in:
1 parent
fe4b5d64d2
commit
dbe39fb2b4
2 files changed
+60
-32
No files matched your search
@@ -20,7 +20,8 @@ use crate::{
|
||||
monotonic_clock::monotonic_now,
|
||||
switch_root::Sysroot,
|
||||
systemd_bus::{
|
||||
BusConnection, FetchedUnit, JobListing, Scope, Session, Signal, StatusOutput,
|
||||
BusConnection, FetchedUnit, JobListing, Scope, Session, Signal, SignalBatch,
|
||||
StatusOutput,
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -95,6 +96,13 @@ impl TrackedUnit {
|
||||
}
|
||||
}
|
||||
|
||||
fn queued_units(signals: &[Signal]) -> impl Iterator<Item = &str> {
|
||||
signals.iter().filter_map(|signal| match signal {
|
||||
Signal::JobNew { unit, .. } => Some(unit.as_str()),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
struct BootTracker {
|
||||
scope: Scope,
|
||||
target: RootRequest,
|
||||
@@ -196,14 +204,30 @@ impl BusFeed {
|
||||
|
||||
impl BootTracker {
|
||||
async fn receive(&mut self, connection: &mut BusConnection) -> Result<Vec<BootChange>, Error> {
|
||||
let signals = connection
|
||||
.receive_signals(BACKGROUND_CHECK_INTERVAL)
|
||||
.await?;
|
||||
Ok(self
|
||||
.collect_changes(connection, signals, async |tracker, connection, signal| {
|
||||
tracker.changes_for(connection, signal).await
|
||||
})
|
||||
.await)
|
||||
match connection.receive_signals(BACKGROUND_CHECK_INTERVAL).await {
|
||||
SignalBatch::Open(signals) => {
|
||||
self.arm_root_switch(queued_units(&signals));
|
||||
Ok(self
|
||||
.collect_changes(connection, signals, async |tracker, connection, signal| {
|
||||
tracker.changes_for(connection, signal).await
|
||||
})
|
||||
.await)
|
||||
}
|
||||
SignalBatch::Closed(signals) => {
|
||||
self.arm_root_switch(queued_units(&signals));
|
||||
Err(Error::Disconnected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn arm_root_switch<'unit>(&mut self, mut queued_units: impl Iterator<Item = &'unit str>) {
|
||||
if self.phase == Phase::Initrd
|
||||
&& matches!(self.root_switch, RootSwitch::NotQueued)
|
||||
&& queued_units.any(|unit| unit == SWITCH_ROOT_UNIT)
|
||||
&& let Ok(sysroot) = Sysroot::hold()
|
||||
{
|
||||
self.root_switch = RootSwitch::Queued(sysroot);
|
||||
}
|
||||
}
|
||||
|
||||
async fn collect_changes<Item>(
|
||||
@@ -288,6 +312,7 @@ impl BootTracker {
|
||||
.into_iter()
|
||||
.filter(|job| shutting_down || job.kind == JobKind::BringsUp)
|
||||
.collect();
|
||||
self.arm_root_switch(jobs.iter().map(|job| job.unit.as_str()));
|
||||
let requested_root = self.requested_root(connection, &jobs).await?;
|
||||
let boot_progress = self.boot_progress_changes(connection, attempt).await?;
|
||||
let tracked_root = self.track(connection, &requested_root).await;
|
||||
@@ -407,13 +432,6 @@ impl BootTracker {
|
||||
let unit = tracked
|
||||
.as_ref()
|
||||
.map_or(unit, |tracked| tracked.name().to_owned());
|
||||
if self.phase == Phase::Initrd
|
||||
&& matches!(self.root_switch, RootSwitch::NotQueued)
|
||||
&& unit == SWITCH_ROOT_UNIT
|
||||
&& let Ok(sysroot) = Sysroot::hold()
|
||||
{
|
||||
self.root_switch = RootSwitch::Queued(sysroot);
|
||||
}
|
||||
tracked
|
||||
.and_then(TrackedUnit::into_change)
|
||||
.into_iter()
|
||||
|
||||
@@ -62,6 +62,11 @@ impl StatusOutput {
|
||||
}
|
||||
}
|
||||
|
||||
pub enum SignalBatch {
|
||||
Open(Vec<Signal>),
|
||||
Closed(Vec<Signal>),
|
||||
}
|
||||
|
||||
pub enum Signal {
|
||||
JobNew {
|
||||
id: u32,
|
||||
@@ -207,17 +212,14 @@ impl Session {
|
||||
poll(&mut watched, Some(&Timespec::default())).is_ok_and(|ready| ready > 0)
|
||||
}
|
||||
|
||||
fn read_one_chunk(&self) -> Result<(), Error> {
|
||||
let still_connected =
|
||||
self.channel.read_write(Some(Duration::ZERO)).is_ok() && self.channel.is_connected();
|
||||
still_connected.then_some(()).ok_or(Error::Disconnected)
|
||||
fn read_one_chunk(&self) -> bool {
|
||||
self.channel.read_write(Some(Duration::ZERO)).is_ok() && self.channel.is_connected()
|
||||
}
|
||||
|
||||
fn arrived_messages(&self) -> Result<Vec<Message>, Error> {
|
||||
fn arrived_messages(&self) -> Vec<Message> {
|
||||
iter::repeat_with(|| self.read_one_chunk())
|
||||
.find(|outcome| outcome.is_err() || !self.socket_has_unread_bytes())
|
||||
.unwrap_or(Ok(()))?;
|
||||
Ok(iter::from_fn(|| self.channel.pop_message()).collect())
|
||||
.find(|still_connected| !still_connected || !self.socket_has_unread_bytes());
|
||||
iter::from_fn(|| self.channel.pop_message()).collect()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -240,15 +242,18 @@ impl BusConnection {
|
||||
}
|
||||
|
||||
fn drain_replies(&mut self) -> Result<Vec<Message>, Error> {
|
||||
let (signals, replies): (Vec<Message>, Vec<Message>) = self
|
||||
.session
|
||||
.get_ref()
|
||||
.arrived_messages()?
|
||||
let session = self.session.get_ref();
|
||||
let (signals, replies): (Vec<Message>, Vec<Message>) = session
|
||||
.arrived_messages()
|
||||
.into_iter()
|
||||
.partition(|message| message.msg_type() == MessageType::Signal);
|
||||
self.early_signals
|
||||
.extend(signals.iter().filter_map(parse_signal));
|
||||
Ok(replies)
|
||||
if session.channel.is_connected() {
|
||||
Ok(replies)
|
||||
} else {
|
||||
Err(Error::Disconnected)
|
||||
}
|
||||
}
|
||||
|
||||
async fn next_replies(&mut self) -> Result<Vec<Message>, Error> {
|
||||
@@ -350,12 +355,17 @@ impl BusConnection {
|
||||
.read1()?)
|
||||
}
|
||||
|
||||
pub async fn receive_signals(&mut self, patience: Duration) -> Result<Vec<Signal>, Error> {
|
||||
pub async fn receive_signals(&mut self, patience: Duration) -> SignalBatch {
|
||||
if self.early_signals.is_empty() {
|
||||
readable_or_timeout(&self.session, patience).await;
|
||||
}
|
||||
self.drain_replies()?;
|
||||
Ok(mem::take(&mut self.early_signals))
|
||||
let still_connected = self.drain_replies().is_ok();
|
||||
let signals = mem::take(&mut self.early_signals);
|
||||
if still_connected {
|
||||
SignalBatch::Open(signals)
|
||||
} else {
|
||||
SignalBatch::Closed(signals)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user