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,
|
monotonic_clock::monotonic_now,
|
||||||
switch_root::Sysroot,
|
switch_root::Sysroot,
|
||||||
systemd_bus::{
|
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 {
|
struct BootTracker {
|
||||||
scope: Scope,
|
scope: Scope,
|
||||||
target: RootRequest,
|
target: RootRequest,
|
||||||
@@ -196,14 +204,30 @@ impl BusFeed {
|
|||||||
|
|
||||||
impl BootTracker {
|
impl BootTracker {
|
||||||
async fn receive(&mut self, connection: &mut BusConnection) -> Result<Vec<BootChange>, Error> {
|
async fn receive(&mut self, connection: &mut BusConnection) -> Result<Vec<BootChange>, Error> {
|
||||||
let signals = connection
|
match connection.receive_signals(BACKGROUND_CHECK_INTERVAL).await {
|
||||||
.receive_signals(BACKGROUND_CHECK_INTERVAL)
|
SignalBatch::Open(signals) => {
|
||||||
.await?;
|
self.arm_root_switch(queued_units(&signals));
|
||||||
Ok(self
|
Ok(self
|
||||||
.collect_changes(connection, signals, async |tracker, connection, signal| {
|
.collect_changes(connection, signals, async |tracker, connection, signal| {
|
||||||
tracker.changes_for(connection, signal).await
|
tracker.changes_for(connection, signal).await
|
||||||
})
|
})
|
||||||
.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>(
|
async fn collect_changes<Item>(
|
||||||
@@ -288,6 +312,7 @@ impl BootTracker {
|
|||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|job| shutting_down || job.kind == JobKind::BringsUp)
|
.filter(|job| shutting_down || job.kind == JobKind::BringsUp)
|
||||||
.collect();
|
.collect();
|
||||||
|
self.arm_root_switch(jobs.iter().map(|job| job.unit.as_str()));
|
||||||
let requested_root = self.requested_root(connection, &jobs).await?;
|
let requested_root = self.requested_root(connection, &jobs).await?;
|
||||||
let boot_progress = self.boot_progress_changes(connection, attempt).await?;
|
let boot_progress = self.boot_progress_changes(connection, attempt).await?;
|
||||||
let tracked_root = self.track(connection, &requested_root).await;
|
let tracked_root = self.track(connection, &requested_root).await;
|
||||||
@@ -407,13 +432,6 @@ impl BootTracker {
|
|||||||
let unit = tracked
|
let unit = tracked
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map_or(unit, |tracked| tracked.name().to_owned());
|
.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
|
tracked
|
||||||
.and_then(TrackedUnit::into_change)
|
.and_then(TrackedUnit::into_change)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
|||||||
@@ -62,6 +62,11 @@ impl StatusOutput {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum SignalBatch {
|
||||||
|
Open(Vec<Signal>),
|
||||||
|
Closed(Vec<Signal>),
|
||||||
|
}
|
||||||
|
|
||||||
pub enum Signal {
|
pub enum Signal {
|
||||||
JobNew {
|
JobNew {
|
||||||
id: u32,
|
id: u32,
|
||||||
@@ -207,17 +212,14 @@ impl Session {
|
|||||||
poll(&mut watched, Some(&Timespec::default())).is_ok_and(|ready| ready > 0)
|
poll(&mut watched, Some(&Timespec::default())).is_ok_and(|ready| ready > 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_one_chunk(&self) -> Result<(), Error> {
|
fn read_one_chunk(&self) -> bool {
|
||||||
let still_connected =
|
self.channel.read_write(Some(Duration::ZERO)).is_ok() && self.channel.is_connected()
|
||||||
self.channel.read_write(Some(Duration::ZERO)).is_ok() && self.channel.is_connected();
|
|
||||||
still_connected.then_some(()).ok_or(Error::Disconnected)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn arrived_messages(&self) -> Result<Vec<Message>, Error> {
|
fn arrived_messages(&self) -> Vec<Message> {
|
||||||
iter::repeat_with(|| self.read_one_chunk())
|
iter::repeat_with(|| self.read_one_chunk())
|
||||||
.find(|outcome| outcome.is_err() || !self.socket_has_unread_bytes())
|
.find(|still_connected| !still_connected || !self.socket_has_unread_bytes());
|
||||||
.unwrap_or(Ok(()))?;
|
iter::from_fn(|| self.channel.pop_message()).collect()
|
||||||
Ok(iter::from_fn(|| self.channel.pop_message()).collect())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -240,15 +242,18 @@ impl BusConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn drain_replies(&mut self) -> Result<Vec<Message>, Error> {
|
fn drain_replies(&mut self) -> Result<Vec<Message>, Error> {
|
||||||
let (signals, replies): (Vec<Message>, Vec<Message>) = self
|
let session = self.session.get_ref();
|
||||||
.session
|
let (signals, replies): (Vec<Message>, Vec<Message>) = session
|
||||||
.get_ref()
|
.arrived_messages()
|
||||||
.arrived_messages()?
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.partition(|message| message.msg_type() == MessageType::Signal);
|
.partition(|message| message.msg_type() == MessageType::Signal);
|
||||||
self.early_signals
|
self.early_signals
|
||||||
.extend(signals.iter().filter_map(parse_signal));
|
.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> {
|
async fn next_replies(&mut self) -> Result<Vec<Message>, Error> {
|
||||||
@@ -350,12 +355,17 @@ impl BusConnection {
|
|||||||
.read1()?)
|
.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() {
|
if self.early_signals.is_empty() {
|
||||||
readable_or_timeout(&self.session, patience).await;
|
readable_or_timeout(&self.session, patience).await;
|
||||||
}
|
}
|
||||||
self.drain_replies()?;
|
let still_connected = self.drain_replies().is_ok();
|
||||||
Ok(mem::take(&mut self.early_signals))
|
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