Skip to content

Commit d598b2f

Browse files
committed
daemon: Share D-Bus connection between server and client
1 parent cb3e8c0 commit d598b2f

3 files changed

Lines changed: 33 additions & 39 deletions

File tree

credentialsd/src/dbus/flow_control.rs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ use credentialsd_common::model::{
1111
};
1212
use credentialsd_common::server::{BackgroundEvent, WindowHandle};
1313
use futures_lite::{Stream, StreamExt};
14+
use tokio::sync::mpsc::Receiver;
1415
use tokio::sync::oneshot;
15-
use tokio::sync::{
16-
mpsc::{self, Sender},
17-
Mutex as AsyncMutex,
18-
};
19-
use zbus::connection::{Builder, Connection};
16+
use tokio::sync::{mpsc::Sender, Mutex as AsyncMutex};
17+
use tokio::task::AbortHandle;
18+
use zbus::connection::Connection;
2019

2120
use crate::credential_service::ManageDevice;
2221
use crate::dbus::ui_control::Ceremony;
@@ -27,33 +26,23 @@ use crate::{
2726
model::{CredentialRequest, CredentialResponse},
2827
};
2928

30-
pub const SERVICE_NAME: &str = "xyz.iinuwa.credentialsd.FlowControl";
31-
3229
pub async fn start_flow_control_service<M: ManageDevice + Debug + Send + Sync + 'static>(
33-
device_manager: M,
34-
) -> zbus::Result<(
35-
Connection,
36-
Sender<(
30+
conn: Connection,
31+
mut listener: Receiver<(
3732
CredentialRequest,
3833
RequestingApplication,
3934
Option<WindowHandle>, // Client window handle
4035
oneshot::Sender<Result<CredentialResponse, CredentialServiceError>>,
4136
)>,
42-
)> {
37+
device_manager: M,
38+
) -> zbus::Result<AbortHandle> {
4339
let svc = Arc::new(AsyncMutex::new(device_manager));
4440
let svc2 = svc.clone();
45-
let conn = Builder::session()?.name(SERVICE_NAME)?.build().await?;
46-
let (initiator_tx, mut initiator_rx) = mpsc::channel::<(
47-
CredentialRequest,
48-
RequestingApplication,
49-
Option<WindowHandle>,
50-
oneshot::Sender<Result<CredentialResponse, CredentialServiceError>>,
51-
)>(2);
52-
let conn2 = conn.clone();
53-
tokio::spawn(async move {
54-
while let Some((msg, requesting_app, window_handle, tx)) = initiator_rx.recv().await {
41+
42+
let task = tokio::spawn(async move {
43+
while let Some((msg, requesting_app, window_handle, tx)) = listener.recv().await {
5544
let svc = svc2.clone();
56-
let ui_control_client = UiControlServiceClient::new(conn2.clone());
45+
let ui_control_client = UiControlServiceClient::new(conn.clone());
5746
if let Err(_) =
5847
tx.send(handle(svc, ui_control_client, msg, requesting_app, window_handle).await)
5948
{
@@ -63,7 +52,7 @@ pub async fn start_flow_control_service<M: ManageDevice + Debug + Send + Sync +
6352
}
6453
}
6554
});
66-
Ok((conn, initiator_tx))
55+
Ok(task.abort_handle())
6756
}
6857

6958
async fn handle<M: ManageDevice + Debug + Send + Sync + 'static, UC: UiController + Debug>(

credentialsd/src/gateway/dbus.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ use credentialsd_common::{
1818
},
1919
};
2020

21-
use crate::webauthn::AppId;
21+
use crate::{webauthn::AppId, DBUS_SERVICE_NAME};
2222

2323
use super::{check_origin_from_app, GatewayService, RequestContext};
2424

25-
pub const SERVICE_NAME: &str = "xyz.iinuwa.credentialsd.Credentials";
2625
pub const PORTAL_SERVICE_PATH: &str = "/org/freedesktop/portal/desktop";
2726

2827
pub(super) async fn start_dbus_gateway(
@@ -32,7 +31,7 @@ pub(super) async fn start_dbus_gateway(
3231
.inspect_err(|err| {
3332
tracing::error!("Failed to connect to D-Bus session: {err}");
3433
})?
35-
.name(SERVICE_NAME)?
34+
.name(DBUS_SERVICE_NAME)?
3635
.serve_at(
3736
PORTAL_SERVICE_PATH,
3837
CredentialPortalGateway {

credentialsd/src/main.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ mod webauthn;
77
use std::error::Error;
88

99
use credential_service::nfc::InProcessNfcHandler;
10+
use tokio::sync::mpsc;
1011

1112
use crate::{
1213
credential_service::{
1314
hybrid::InternalHybridHandler, usb::InProcessUsbHandler, CredentialService,
1415
},
15-
dbus::{CredentialRequestControllerClient, UiControlServiceClient},
16+
dbus::CredentialRequestControllerClient,
1617
};
1718

19+
pub const DBUS_SERVICE_NAME: &str = "xyz.iinuwa.credentialsd.Credentials";
20+
1821
#[tokio::main]
1922
async fn main() {
2023
// Initialize logger
@@ -25,29 +28,32 @@ async fn main() {
2528
}
2629

2730
async fn run() -> Result<(), Box<dyn Error>> {
28-
print!("Connecting to D-Bus as client...\t");
29-
let dbus_client_conn = zbus::connection::Builder::session()?.build().await?;
31+
print!("Starting D-Bus public client service...");
32+
let (incoming_request_tx, incoming_request_rx) = mpsc::channel(2);
33+
let request_controller = CredentialRequestControllerClient {
34+
initiator: incoming_request_tx,
35+
};
36+
let dbus_conn = gateway::start_gateway(request_controller).await?;
3037
println!(" ✅");
3138

32-
print!("Starting D-Bus UI -> Credential control service...");
39+
// initialize client to interact with UI
3340
let credential_service = CredentialService::new(
3441
InternalHybridHandler::new(),
3542
InProcessNfcHandler {},
3643
InProcessUsbHandler {},
3744
);
38-
let (_flow_control_conn, initiator) =
39-
dbus::start_flow_control_service(credential_service).await?;
40-
println!(" ✅");
41-
42-
print!("Starting D-Bus public client service...");
43-
let initiator = CredentialRequestControllerClient { initiator };
44-
let _gateway_conn = gateway::start_gateway(initiator).await?;
45-
println!(" ✅");
45+
let flow_control_svc = dbus::start_flow_control_service(
46+
dbus_conn.clone(),
47+
incoming_request_rx,
48+
credential_service,
49+
)
50+
.await?;
4651

4752
println!("Waiting for messages...");
4853
tokio::signal::ctrl_c()
4954
.await
5055
.map_err(|err| format!("Failed to wait for shutdown signals: {err}. Shutting down"))?;
5156

57+
flow_control_svc.abort();
5258
Ok(())
5359
}

0 commit comments

Comments
 (0)