refactor: split the bus feed's link from its boot tracker and poll fd streams directly
This commit is contained in:
1 parent
cba3e404f9
commit
734988247d
4 files changed
+214
-204
No files matched your search
@@ -11,10 +11,7 @@ use crate::{
|
|||||||
console_palette::ConsolePalette,
|
console_palette::ConsolePalette,
|
||||||
console_takeover::Console,
|
console_takeover::Console,
|
||||||
passphrase_input::asks_to_quit,
|
passphrase_input::asks_to_quit,
|
||||||
stream_sources::{
|
stream_sources::{BACKGROUND_CHECK_INTERVAL, items_when_readable, periodic_inputs},
|
||||||
BACKGROUND_CHECK_INTERVAL, CarriedItem, periodic_inputs, readable_source,
|
|
||||||
unfold_carrying,
|
|
||||||
},
|
|
||||||
watch_screen::{ConsoleDesk, ScreenInput, WatchEnding},
|
watch_screen::{ConsoleDesk, ScreenInput, WatchEnding},
|
||||||
},
|
},
|
||||||
screen_settings::font_glyphs::Glyphs,
|
screen_settings::font_glyphs::Glyphs,
|
||||||
@@ -139,14 +136,7 @@ pub fn full_repaint_inputs() -> impl Stream<Item = ScreenInput> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn password_request_inputs(watch: RequestWatch) -> io::Result<impl Stream<Item = ScreenInput>> {
|
pub fn password_request_inputs(watch: RequestWatch) -> io::Result<impl Stream<Item = ScreenInput>> {
|
||||||
let readiness = readable_source(watch)?;
|
Ok(items_when_readable(watch, RequestWatch::changed)?
|
||||||
Ok(unfold_carrying(readiness, |readiness| async move {
|
.filter(|changed| ready(*changed))
|
||||||
readiness.readable().await.ok()?.clear_ready();
|
.map(|_| ScreenInput::PasswordRequests))
|
||||||
Some(CarriedItem {
|
|
||||||
item: readiness.get_ref().changed(),
|
|
||||||
state: readiness,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.filter(|changed| ready(*changed))
|
|
||||||
.map(|_| ScreenInput::PasswordRequests))
|
|
||||||
}
|
}
|
||||||
@@ -35,6 +35,21 @@ pub fn readable_source<Source: AsRawFd>(source: Source) -> io::Result<AsyncFd<So
|
|||||||
Ok(unsafe { AsyncFd::register_with_interest(source, Interest::READABLE) }?)
|
Ok(unsafe { AsyncFd::register_with_interest(source, Interest::READABLE) }?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn items_when_readable<Source: AsRawFd, Item>(
|
||||||
|
source: Source,
|
||||||
|
read: impl Fn(&Source) -> Item,
|
||||||
|
) -> io::Result<impl Stream<Item = Item>> {
|
||||||
|
let readiness = readable_source(source)?;
|
||||||
|
Ok(stream::poll_fn(move |context| {
|
||||||
|
readiness.poll_read_ready(context).map(|ready| {
|
||||||
|
ready.ok().map(|mut guard| {
|
||||||
|
guard.clear_ready();
|
||||||
|
read(readiness.get_ref())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn readable_or_timeout<Source: AsRawFd>(readiness: &AsyncFd<Source>, patience: Duration) {
|
pub async fn readable_or_timeout<Source: AsRawFd>(readiness: &AsyncFd<Source>, patience: Duration) {
|
||||||
if let Ok(Ok(mut guard)) = timeout(patience, readiness.readable()).await {
|
if let Ok(Ok(mut guard)) = timeout(patience, readiness.readable()).await {
|
||||||
guard.clear_ready();
|
guard.clear_ready();
|
||||||
@@ -51,13 +66,8 @@ pub fn periodic_inputs(
|
|||||||
period: Duration,
|
period: Duration,
|
||||||
input: fn() -> ScreenInput,
|
input: fn() -> ScreenInput,
|
||||||
) -> impl Stream<Item = ScreenInput> {
|
) -> impl Stream<Item = ScreenInput> {
|
||||||
unfold_carrying(steady_interval(period), move |mut ticks| async move {
|
let mut ticks = steady_interval(period);
|
||||||
ticks.tick().await;
|
stream::poll_fn(move |context| ticks.poll_tick(context).map(|_| Some(input())))
|
||||||
Some(CarriedItem {
|
|
||||||
item: input(),
|
|
||||||
state: ticks,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn frame_inputs() -> impl Stream<Item = ScreenInput> {
|
pub fn frame_inputs() -> impl Stream<Item = ScreenInput> {
|
||||||
@@ -81,16 +91,11 @@ fn terminal_inputs(drained: io::Result<Vec<Event>>) -> Vec<ScreenInput> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn key_inputs() -> io::Result<impl Stream<Item = ScreenInput>> {
|
pub fn key_inputs() -> io::Result<impl Stream<Item = ScreenInput>> {
|
||||||
let readiness = readable_source(io::stdin())?;
|
Ok(
|
||||||
Ok(unfold_carrying(readiness, |readiness| async move {
|
items_when_readable(io::stdin(), |_| drain_terminal_events())?
|
||||||
readiness.readable().await.ok()?.clear_ready();
|
.map(terminal_inputs)
|
||||||
Some(CarriedItem {
|
.flat_map(stream::iter),
|
||||||
item: drain_terminal_events(),
|
)
|
||||||
state: readiness,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.map(terminal_inputs)
|
|
||||||
.flat_map(stream::iter))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn termination_inputs() -> io::Result<impl Stream<Item = ScreenInput>> {
|
pub fn termination_inputs() -> io::Result<impl Stream<Item = ScreenInput>> {
|
||||||
|
|||||||
+161
-129
@@ -1,7 +1,7 @@
|
|||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
iter::once,
|
iter::once,
|
||||||
mem, process,
|
process,
|
||||||
time::{Duration, Instant},
|
time::{Duration, Instant},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -10,7 +10,7 @@ use futures::{Stream, StreamExt, stream};
|
|||||||
use tokio::time::sleep;
|
use tokio::time::sleep;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
boot_model::{BootChange, BootState, HANDOFF_UNIT, JobKind, Phase},
|
boot_model::{Activity, BootChange, BootState, HANDOFF_UNIT, JobKind, Phase, UnitFacts},
|
||||||
console_session::{
|
console_session::{
|
||||||
stream_sources::{BACKGROUND_CHECK_INTERVAL, CarriedItem, unfold_carrying},
|
stream_sources::{BACKGROUND_CHECK_INTERVAL, CarriedItem, unfold_carrying},
|
||||||
watch_screen::ScreenInput,
|
watch_screen::ScreenInput,
|
||||||
@@ -42,16 +42,26 @@ pub enum RootRequest {
|
|||||||
ManagerDefault,
|
ManagerDefault,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||||
|
enum ConnectAttempt {
|
||||||
|
First,
|
||||||
|
Again,
|
||||||
|
}
|
||||||
|
|
||||||
enum Link {
|
enum Link {
|
||||||
Connected(BusConnection),
|
Connected(BusConnection),
|
||||||
Connecting { deadline: Instant },
|
Connecting {
|
||||||
|
deadline: Instant,
|
||||||
|
attempt: ConnectAttempt,
|
||||||
|
},
|
||||||
Finished,
|
Finished,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Link {
|
impl Link {
|
||||||
fn connecting() -> Self {
|
fn connecting(attempt: ConnectAttempt) -> Self {
|
||||||
Self::Connecting {
|
Self::Connecting {
|
||||||
deadline: Instant::now() + RECONNECT_PATIENCE,
|
deadline: Instant::now() + RECONNECT_PATIENCE,
|
||||||
|
attempt,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -61,124 +71,163 @@ enum RootSwitch {
|
|||||||
Queued(Sysroot),
|
Queued(Sysroot),
|
||||||
}
|
}
|
||||||
|
|
||||||
struct TrackedUnit {
|
enum TrackedUnit {
|
||||||
name: String,
|
Known(String),
|
||||||
change: Option<BootChange>,
|
New {
|
||||||
|
facts: UnitFacts,
|
||||||
|
activity: Activity,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
struct BusFeed {
|
impl TrackedUnit {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
match self {
|
||||||
|
Self::Known(name) => name,
|
||||||
|
Self::New { facts, .. } => &facts.name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_change(self) -> Option<BootChange> {
|
||||||
|
match self {
|
||||||
|
Self::Known(_) => None,
|
||||||
|
Self::New { facts, activity } => Some(BootChange::TrackUnit { facts, activity }),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct BootTracker {
|
||||||
scope: Scope,
|
scope: Scope,
|
||||||
target: RootRequest,
|
target: RootRequest,
|
||||||
phase: Phase,
|
phase: Phase,
|
||||||
link: Link,
|
|
||||||
unit_paths: HashMap<Path<'static>, String>,
|
unit_paths: HashMap<Path<'static>, String>,
|
||||||
root_switch: RootSwitch,
|
root_switch: RootSwitch,
|
||||||
gave_origin: bool,
|
}
|
||||||
|
|
||||||
|
struct BusFeed {
|
||||||
|
link: Link,
|
||||||
|
tracker: BootTracker,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct FeedProgress<'feed> {
|
struct FeedProgress<'feed> {
|
||||||
feed: &'feed mut BusFeed,
|
tracker: &'feed mut BootTracker,
|
||||||
connection: &'feed mut BusConnection,
|
connection: &'feed mut BusConnection,
|
||||||
changes: Vec<BootChange>,
|
changes: Vec<BootChange>,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct WatchedConnection {
|
|
||||||
connection: BusConnection,
|
|
||||||
changes: Vec<BootChange>,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn boot_inputs(changes: Vec<BootChange>) -> Vec<ScreenInput> {
|
|
||||||
if changes.is_empty() {
|
|
||||||
return Vec::new();
|
|
||||||
}
|
|
||||||
vec![ScreenInput::Boot(changes)]
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BusFeed {
|
impl BusFeed {
|
||||||
fn new(phase: Phase, scope: Scope, target: RootRequest) -> Self {
|
fn new(phase: Phase, scope: Scope, target: RootRequest) -> Self {
|
||||||
Self {
|
Self {
|
||||||
scope,
|
link: Link::connecting(ConnectAttempt::First),
|
||||||
target,
|
tracker: BootTracker {
|
||||||
root_switch: RootSwitch::NotQueued,
|
scope,
|
||||||
phase,
|
target,
|
||||||
link: Link::connecting(),
|
phase,
|
||||||
unit_paths: HashMap::new(),
|
unit_paths: HashMap::new(),
|
||||||
gave_origin: false,
|
root_switch: RootSwitch::NotQueued,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn next_inputs(&mut self) -> Option<Vec<ScreenInput>> {
|
async fn next_inputs(&mut self) -> Option<Vec<ScreenInput>> {
|
||||||
match mem::replace(&mut self.link, Link::Finished) {
|
match &mut self.link {
|
||||||
Link::Finished => None,
|
Link::Finished => None,
|
||||||
Link::Connecting { deadline } => Some(self.connect(deadline).await),
|
Link::Connecting { deadline, attempt } => {
|
||||||
Link::Connected(connection) => Some(self.receive(connection).await),
|
let deadline = *deadline;
|
||||||
|
let attempt = *attempt;
|
||||||
|
Some(self.connect(deadline, attempt).await)
|
||||||
|
}
|
||||||
|
Link::Connected(connection) => {
|
||||||
|
let received = self.tracker.receive(connection).await;
|
||||||
|
Some(match received {
|
||||||
|
Ok(changes) if changes.is_empty() => Vec::new(),
|
||||||
|
Ok(changes) => vec![ScreenInput::Boot(changes)],
|
||||||
|
Err(_) => self.lose_connection(),
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn open_watched(&mut self) -> Result<WatchedConnection, Error> {
|
async fn open_watched(&mut self, attempt: ConnectAttempt) -> Result<Vec<BootChange>, Error> {
|
||||||
let mut connection = BusConnection::register(Session::open(self.scope)?)?;
|
let mut connection = BusConnection::register(Session::open(self.tracker.scope)?)?;
|
||||||
let changes = self.start_watching(&mut connection).await?;
|
let changes = self
|
||||||
Ok(WatchedConnection {
|
.tracker
|
||||||
connection,
|
.start_watching(&mut connection, attempt)
|
||||||
changes,
|
.await?;
|
||||||
})
|
self.link = Link::Connected(connection);
|
||||||
|
Ok(changes)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn connect(&mut self, deadline: Instant) -> Vec<ScreenInput> {
|
async fn connect(&mut self, deadline: Instant, attempt: ConnectAttempt) -> Vec<ScreenInput> {
|
||||||
match self.open_watched().await {
|
match self.open_watched(attempt).await {
|
||||||
Ok(watched) => {
|
Ok(changes) => vec![ScreenInput::Boot(changes), ScreenInput::FullRepaint],
|
||||||
self.link = Link::Connected(watched.connection);
|
|
||||||
vec![ScreenInput::Boot(watched.changes), ScreenInput::FullRepaint]
|
|
||||||
}
|
|
||||||
Err(_) if Instant::now() < deadline => {
|
Err(_) if Instant::now() < deadline => {
|
||||||
sleep(BACKGROUND_CHECK_INTERVAL).await;
|
sleep(BACKGROUND_CHECK_INTERVAL).await;
|
||||||
self.link = Link::Connecting { deadline };
|
|
||||||
Vec::new()
|
Vec::new()
|
||||||
}
|
}
|
||||||
Err(_) => vec![ScreenInput::Failed(Error::ManagerUnreachable)],
|
Err(_) => {
|
||||||
|
self.link = Link::Finished;
|
||||||
|
vec![ScreenInput::Failed(Error::ManagerUnreachable)]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn receive(&mut self, mut connection: BusConnection) -> Vec<ScreenInput> {
|
|
||||||
let Ok(signals) = connection.receive_signals(BACKGROUND_CHECK_INTERVAL).await else {
|
|
||||||
return self.lose_connection();
|
|
||||||
};
|
|
||||||
let progress = stream::iter(signals)
|
|
||||||
.fold(
|
|
||||||
FeedProgress {
|
|
||||||
feed: &mut *self,
|
|
||||||
connection: &mut connection,
|
|
||||||
changes: Vec::new(),
|
|
||||||
},
|
|
||||||
|mut progress, signal| async move {
|
|
||||||
let changes = progress.feed.changes_for(progress.connection, signal).await;
|
|
||||||
progress.changes.extend(changes);
|
|
||||||
progress
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
let changes = progress.changes;
|
|
||||||
self.link = Link::Connected(connection);
|
|
||||||
boot_inputs(changes)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lose_connection(&mut self) -> Vec<ScreenInput> {
|
fn lose_connection(&mut self) -> Vec<ScreenInput> {
|
||||||
let at = monotonic_now();
|
let at = monotonic_now();
|
||||||
let RootSwitch::Queued(sysroot) = &self.root_switch else {
|
let tracker = &mut self.tracker;
|
||||||
if self.phase == Phase::Shutdown {
|
let RootSwitch::Queued(sysroot) = &tracker.root_switch else {
|
||||||
return boot_inputs(vec![BootChange::ManagerExited { at }]);
|
if tracker.phase == Phase::Shutdown {
|
||||||
|
self.link = Link::Finished;
|
||||||
|
return vec![ScreenInput::Boot(vec![BootChange::ManagerExited { at }])];
|
||||||
}
|
}
|
||||||
self.link = Link::connecting();
|
self.link = Link::connecting(ConnectAttempt::Again);
|
||||||
return boot_inputs(vec![BootChange::ForgetJobs]);
|
return vec![ScreenInput::Boot(vec![BootChange::ForgetJobs])];
|
||||||
};
|
};
|
||||||
if let Err(error) = sysroot.enter() {
|
if let Err(error) = sysroot.enter() {
|
||||||
|
self.link = Link::Finished;
|
||||||
return vec![ScreenInput::Failed(error.into())];
|
return vec![ScreenInput::Failed(error.into())];
|
||||||
}
|
}
|
||||||
self.root_switch = RootSwitch::NotQueued;
|
tracker.root_switch = RootSwitch::NotQueued;
|
||||||
self.unit_paths.clear();
|
tracker.unit_paths.clear();
|
||||||
self.phase = Phase::Host;
|
tracker.phase = Phase::Host;
|
||||||
self.link = Link::connecting();
|
self.link = Link::connecting(ConnectAttempt::Again);
|
||||||
boot_inputs(vec![BootChange::SwitchRoot { at }])
|
vec![ScreenInput::Boot(vec![BootChange::SwitchRoot { at }])]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn collect_changes<Item>(
|
||||||
|
&mut self,
|
||||||
|
connection: &mut BusConnection,
|
||||||
|
items: Vec<Item>,
|
||||||
|
step: impl AsyncFn(&mut Self, &mut BusConnection, Item) -> Vec<BootChange>,
|
||||||
|
) -> Vec<BootChange> {
|
||||||
|
let step = &step;
|
||||||
|
stream::iter(items)
|
||||||
|
.fold(
|
||||||
|
FeedProgress {
|
||||||
|
tracker: self,
|
||||||
|
connection,
|
||||||
|
changes: Vec::new(),
|
||||||
|
},
|
||||||
|
|mut progress, item| async move {
|
||||||
|
let changes = step(progress.tracker, progress.connection, item).await;
|
||||||
|
progress.changes.extend(changes);
|
||||||
|
progress
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.changes
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn requested_root(
|
async fn requested_root(
|
||||||
@@ -203,18 +252,18 @@ impl BusFeed {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn boot_progress_changes(
|
async fn boot_progress_changes(
|
||||||
&mut self,
|
&self,
|
||||||
connection: &mut BusConnection,
|
connection: &mut BusConnection,
|
||||||
|
attempt: ConnectAttempt,
|
||||||
) -> Result<Vec<BootChange>, Error> {
|
) -> Result<Vec<BootChange>, Error> {
|
||||||
let boot_in_progress = connection.boot_in_progress().await?;
|
let boot_in_progress = connection.boot_in_progress().await?;
|
||||||
let origin = (!self.gave_origin).then(|| {
|
let origin = (attempt == ConnectAttempt::First).then(|| {
|
||||||
BootChange::SetOrigin(if boot_in_progress {
|
BootChange::SetOrigin(if boot_in_progress {
|
||||||
Duration::ZERO
|
Duration::ZERO
|
||||||
} else {
|
} else {
|
||||||
monotonic_now()
|
monotonic_now()
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
self.gave_origin = true;
|
|
||||||
let finished = if boot_in_progress {
|
let finished = if boot_in_progress {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
} else {
|
} else {
|
||||||
@@ -223,33 +272,10 @@ impl BusFeed {
|
|||||||
Ok(origin.into_iter().chain(finished).collect())
|
Ok(origin.into_iter().chain(finished).collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn track_all(
|
|
||||||
&mut self,
|
|
||||||
connection: &mut BusConnection,
|
|
||||||
units: Vec<String>,
|
|
||||||
) -> Vec<BootChange> {
|
|
||||||
stream::iter(units)
|
|
||||||
.fold(
|
|
||||||
FeedProgress {
|
|
||||||
feed: &mut *self,
|
|
||||||
connection: &mut *connection,
|
|
||||||
changes: Vec::new(),
|
|
||||||
},
|
|
||||||
|mut progress, unit| async move {
|
|
||||||
let tracked = progress.feed.track(progress.connection, &unit).await;
|
|
||||||
progress
|
|
||||||
.changes
|
|
||||||
.extend(tracked.and_then(|tracked| tracked.change));
|
|
||||||
progress
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.changes
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn start_watching(
|
async fn start_watching(
|
||||||
&mut self,
|
&mut self,
|
||||||
connection: &mut BusConnection,
|
connection: &mut BusConnection,
|
||||||
|
attempt: ConnectAttempt,
|
||||||
) -> Result<Vec<BootChange>, Error> {
|
) -> Result<Vec<BootChange>, Error> {
|
||||||
connection.subscribe().await?;
|
connection.subscribe().await?;
|
||||||
if self.scope == Scope::System {
|
if self.scope == Scope::System {
|
||||||
@@ -263,21 +289,34 @@ impl BusFeed {
|
|||||||
.filter(|job| shutting_down || job.kind == JobKind::BringsUp)
|
.filter(|job| shutting_down || job.kind == JobKind::BringsUp)
|
||||||
.collect();
|
.collect();
|
||||||
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).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;
|
||||||
let root = tracked_root
|
let root = tracked_root
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map_or(requested_root, |tracked| tracked.name.clone());
|
.map_or(requested_root, |tracked| tracked.name().to_owned());
|
||||||
let tracked_handoff = self.track(connection, HANDOFF_UNIT).await;
|
let tracked_handoff = self.track(connection, HANDOFF_UNIT).await;
|
||||||
let handoff = tracked_handoff
|
let handoff = tracked_handoff
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|tracked| BootChange::SetHandoff(tracked.name.clone()));
|
.map(|tracked| BootChange::SetHandoff(tracked.name().to_owned()));
|
||||||
let watched_units = jobs
|
let watched_units = jobs
|
||||||
.iter()
|
.iter()
|
||||||
.map(|job| job.unit.clone())
|
.map(|job| job.unit.clone())
|
||||||
.chain(BootState::new(self.phase).milestone_targets)
|
.chain(BootState::new(self.phase).milestone_targets)
|
||||||
.collect();
|
.collect();
|
||||||
let tracked_changes = self.track_all(connection, watched_units).await;
|
let tracked_changes = self
|
||||||
|
.collect_changes(
|
||||||
|
connection,
|
||||||
|
watched_units,
|
||||||
|
async |tracker, connection, unit| {
|
||||||
|
tracker
|
||||||
|
.track(connection, &unit)
|
||||||
|
.await
|
||||||
|
.and_then(TrackedUnit::into_change)
|
||||||
|
.into_iter()
|
||||||
|
.collect()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await;
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"bootvine: watching {root} in {} as pid {} over {}",
|
"bootvine: watching {root} in {} as pid {} over {}",
|
||||||
self.phase.label(),
|
self.phase.label(),
|
||||||
@@ -291,8 +330,8 @@ impl BusFeed {
|
|||||||
let queued_at = monotonic_now();
|
let queued_at = monotonic_now();
|
||||||
Ok(boot_progress
|
Ok(boot_progress
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.chain(tracked_root.and_then(|tracked| tracked.change))
|
.chain(tracked_root.and_then(TrackedUnit::into_change))
|
||||||
.chain(tracked_handoff.and_then(|tracked| tracked.change))
|
.chain(tracked_handoff.and_then(TrackedUnit::into_change))
|
||||||
.chain(handoff)
|
.chain(handoff)
|
||||||
.chain(tracked_changes)
|
.chain(tracked_changes)
|
||||||
.chain(once(BootChange::SetRoot(root)))
|
.chain(once(BootChange::SetRoot(root)))
|
||||||
@@ -306,26 +345,19 @@ impl BusFeed {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn track(&mut self, connection: &mut BusConnection, unit: &str) -> Option<TrackedUnit> {
|
async fn track(&mut self, connection: &mut BusConnection, unit: &str) -> Option<TrackedUnit> {
|
||||||
if let Some(known) = self.unit_paths.values().find(|known| *known == unit) {
|
if self.unit_paths.values().any(|known| known == unit) {
|
||||||
return Some(TrackedUnit {
|
return Some(TrackedUnit::Known(unit.to_owned()));
|
||||||
name: known.clone(),
|
|
||||||
change: None,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
let FetchedUnit {
|
let FetchedUnit {
|
||||||
path,
|
path,
|
||||||
facts,
|
facts,
|
||||||
activity,
|
activity,
|
||||||
} = connection.fetch_unit(unit).await.ok()?;
|
} = connection.fetch_unit(unit).await.ok()?;
|
||||||
let name = facts.name.clone();
|
if self.unit_paths.values().any(|known| *known == facts.name) {
|
||||||
if self.unit_paths.values().any(|known| *known == name) {
|
return Some(TrackedUnit::Known(facts.name));
|
||||||
return Some(TrackedUnit { name, change: None });
|
|
||||||
}
|
}
|
||||||
self.unit_paths.insert(path, name.clone());
|
self.unit_paths.insert(path, facts.name.clone());
|
||||||
Some(TrackedUnit {
|
Some(TrackedUnit::New { facts, activity })
|
||||||
name,
|
|
||||||
change: Some(BootChange::TrackUnit { facts, activity }),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn changes_for(
|
async fn changes_for(
|
||||||
@@ -374,7 +406,7 @@ impl BusFeed {
|
|||||||
let tracked = self.track(connection, &unit).await;
|
let tracked = self.track(connection, &unit).await;
|
||||||
let unit = tracked
|
let unit = tracked
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map_or(unit, |tracked| tracked.name.clone());
|
.map_or(unit, |tracked| tracked.name().to_owned());
|
||||||
if self.phase == Phase::Initrd
|
if self.phase == Phase::Initrd
|
||||||
&& matches!(self.root_switch, RootSwitch::NotQueued)
|
&& matches!(self.root_switch, RootSwitch::NotQueued)
|
||||||
&& unit == SWITCH_ROOT_UNIT
|
&& unit == SWITCH_ROOT_UNIT
|
||||||
@@ -383,7 +415,7 @@ impl BusFeed {
|
|||||||
self.root_switch = RootSwitch::Queued(sysroot);
|
self.root_switch = RootSwitch::Queued(sysroot);
|
||||||
}
|
}
|
||||||
tracked
|
tracked
|
||||||
.and_then(|tracked| tracked.change)
|
.and_then(TrackedUnit::into_change)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.chain(once(BootChange::QueueJob { id, unit, kind, at }))
|
.chain(once(BootChange::QueueJob { id, unit, kind, at }))
|
||||||
.collect()
|
.collect()
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ use std::{
|
|||||||
future::ready,
|
future::ready,
|
||||||
io, iter, mem,
|
io, iter, mem,
|
||||||
os::fd::{AsFd, AsRawFd, BorrowedFd, RawFd},
|
os::fd::{AsFd, AsRawFd, BorrowedFd, RawFd},
|
||||||
|
pin::pin,
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -10,13 +11,15 @@ use dbus::{
|
|||||||
arg::{AppendAll, PropMap, RefArg, Variant},
|
arg::{AppendAll, PropMap, RefArg, Variant},
|
||||||
channel::{BusType, Channel},
|
channel::{BusType, Channel},
|
||||||
};
|
};
|
||||||
use futures::{StreamExt, stream};
|
use futures::{TryStreamExt, stream};
|
||||||
use rustix::event::{PollFd, PollFlags, Timespec, poll};
|
use rustix::event::{PollFd, PollFlags, Timespec, poll};
|
||||||
use tokio::{io::unix::AsyncFd, time::timeout};
|
use tokio::{io::unix::AsyncFd, time::timeout};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
boot_model::{Activity, JobKind, JobResult, UnitFacts, UnitKind, UnitTimeouts},
|
boot_model::{Activity, JobKind, JobResult, UnitFacts, UnitKind, UnitTimeouts},
|
||||||
console_session::stream_sources::{CarriedItem, readable_source, unfold_carrying},
|
console_session::stream_sources::{
|
||||||
|
CarriedItem, readable_or_timeout, readable_source, unfold_carrying,
|
||||||
|
},
|
||||||
systemd_link::link_error::Error,
|
systemd_link::link_error::Error,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -102,7 +105,7 @@ impl From<ListedJob> for JobListing {
|
|||||||
|
|
||||||
pub struct Session {
|
pub struct Session {
|
||||||
channel: Channel,
|
channel: Channel,
|
||||||
pub peer_to_peer: bool,
|
peer_to_peer: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn method_call(
|
fn method_call(
|
||||||
@@ -153,8 +156,9 @@ impl Session {
|
|||||||
if scope == Scope::System
|
if scope == Scope::System
|
||||||
&& let Ok(channel) = Channel::open_private(PRIVATE_SOCKET)
|
&& let Ok(channel) = Channel::open_private(PRIVATE_SOCKET)
|
||||||
{
|
{
|
||||||
send_authentication_carrier(&channel)?;
|
let session = Self::watching(channel, true);
|
||||||
return Ok(Self::watching(channel, true));
|
session.send(authentication_carrier()?)?;
|
||||||
|
return Ok(session);
|
||||||
}
|
}
|
||||||
let bus = match scope {
|
let bus = match scope {
|
||||||
Scope::System => BusType::System,
|
Scope::System => BusType::System,
|
||||||
@@ -222,13 +226,6 @@ pub struct BusConnection {
|
|||||||
early_signals: Vec<Signal>,
|
early_signals: Vec<Signal>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn into_results(batch: Result<Vec<Message>, Error>) -> Vec<Result<Message, Error>> {
|
|
||||||
match batch {
|
|
||||||
Ok(messages) => messages.into_iter().map(Ok).collect(),
|
|
||||||
Err(error) => vec![Err(error)],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BusConnection {
|
impl BusConnection {
|
||||||
pub fn register(session: Session) -> io::Result<Self> {
|
pub fn register(session: Session) -> io::Result<Self> {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
@@ -259,29 +256,24 @@ impl BusConnection {
|
|||||||
if !drained.is_empty() {
|
if !drained.is_empty() {
|
||||||
return Ok(drained);
|
return Ok(drained);
|
||||||
}
|
}
|
||||||
self.session.readable_mut().await?.clear_ready();
|
self.session.readable().await?.clear_ready();
|
||||||
self.drain_replies()
|
self.drain_replies()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn reply_to(&mut self, serial: u32) -> Result<Message, Error> {
|
async fn reply_to(&mut self, serial: u32) -> Result<Message, Error> {
|
||||||
unfold_carrying(self, |connection| async move {
|
let mut replies = pin!(
|
||||||
let replies = connection.next_replies().await;
|
unfold_carrying(self, |connection| async move {
|
||||||
Some(CarriedItem {
|
let replies = connection.next_replies().await;
|
||||||
item: replies,
|
Some(CarriedItem {
|
||||||
state: connection,
|
item: replies,
|
||||||
|
state: connection,
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
.map_ok(|replies| stream::iter(replies.into_iter().map(Ok::<_, Error>)))
|
||||||
.flat_map(|replies| stream::iter(into_results(replies)))
|
.try_flatten()
|
||||||
.filter(|reply| {
|
.try_filter(|reply| ready(reply.get_reply_serial() == Some(serial)))
|
||||||
ready(
|
);
|
||||||
reply
|
replies.try_next().await?.ok_or(Error::Disconnected)
|
||||||
.as_ref()
|
|
||||||
.map_or(true, |message| message.get_reply_serial() == Some(serial)),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.take(1)
|
|
||||||
.fold(Err(Error::Disconnected), |_, reply| ready(reply))
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn call(&mut self, call: Message) -> Result<Message, Error> {
|
async fn call(&mut self, call: Message) -> Result<Message, Error> {
|
||||||
@@ -359,10 +351,8 @@ impl BusConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn receive_signals(&mut self, patience: Duration) -> Result<Vec<Signal>, Error> {
|
pub async fn receive_signals(&mut self, patience: Duration) -> Result<Vec<Signal>, Error> {
|
||||||
if self.early_signals.is_empty()
|
if self.early_signals.is_empty() {
|
||||||
&& let Ok(Ok(mut guard)) = timeout(patience, self.session.readable_mut()).await
|
readable_or_timeout(&self.session, patience).await;
|
||||||
{
|
|
||||||
guard.clear_ready();
|
|
||||||
}
|
}
|
||||||
self.drain_replies()?;
|
self.drain_replies()?;
|
||||||
Ok(mem::take(&mut self.early_signals))
|
Ok(mem::take(&mut self.early_signals))
|
||||||
@@ -447,17 +437,10 @@ impl AsFd for Session {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_authentication_carrier(channel: &Channel) -> Result<(), Error> {
|
fn authentication_carrier() -> Result<Message, Error> {
|
||||||
let Ok(mut ping) = Message::new_method_call(DESTINATION, MANAGER_PATH, PEER_INTERFACE, "Ping")
|
let mut ping = method_call(DESTINATION, MANAGER_PATH, PEER_INTERFACE, "Ping", ())?;
|
||||||
else {
|
|
||||||
return Err(Error::MalformedCall);
|
|
||||||
};
|
|
||||||
ping.set_no_reply(true);
|
ping.set_no_reply(true);
|
||||||
if channel.send(ping).is_err() {
|
Ok(ping)
|
||||||
return Err(Error::Disconnected);
|
|
||||||
}
|
|
||||||
channel.flush();
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_signal(message: &Message) -> Option<Signal> {
|
fn parse_signal(message: &Message) -> Option<Signal> {
|
||||||
|
|||||||
Reference in new issue
Block a user