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