From 897847cb469d1b14cea4ad0c01c6772e913ec012 Mon Sep 17 00:00:00 2001 From: Heiko Schaefer Date: Tue, 3 Aug 2021 23:17:06 +0200 Subject: [PATCH] Add a shutdown_scd() function, to explore managing scdaemon. --- scdc/src/lib.rs | 48 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/scdc/src/lib.rs b/scdc/src/lib.rs index b209c94..34a29ac 100644 --- a/scdc/src/lib.rs +++ b/scdc/src/lib.rs @@ -60,7 +60,7 @@ impl ScdClient { /// /// If `agent` is None, a Context with the default GnuPG home directory /// is used. - fn new(agent: Option) -> Result { + fn new(agent: Option, init: bool) -> Result { let agent = if let Some(agent) = agent { agent } else { @@ -74,9 +74,11 @@ impl ScdClient { card_caps: None, }; - // call "SCD SERIALNO", which causes scdaemon to be started by gpg - // agent (if it's not running yet) - scdc.init()?; + if init { + // call "SCD SERIALNO", which causes scdaemon to be started by gpg + // agent (if it's not running yet) + scdc.init()?; + } Ok(scdc) } @@ -109,7 +111,7 @@ impl ScdClient { pub fn open( agent: Option, ) -> Result { - let card = ScdClient::new(agent)?; + let card = ScdClient::new(agent, true)?; Ok(Box::new(card) as CardClientBox) } @@ -119,7 +121,7 @@ impl ScdClient { agent: Option, serial: &str, ) -> Result { - let mut card = ScdClient::new(agent)?; + let mut card = ScdClient::new(agent, true)?; card.select_card(serial)?; Ok(Box::new(card) as CardClientBox) @@ -152,6 +154,40 @@ impl ScdClient { Err(anyhow!("Card not found")) } + + fn send(&mut self, cmd: &str) -> Result<()> { + self.agent.send(cmd)?; + + let mut rt = RT.lock().unwrap(); + + while let Some(response) = rt.block_on(self.agent.next()) { + log::debug!("select res: {:x?}", response); + + if let Err(e) = response { + return Err(anyhow!("Err {:?}", e)); + } + + if let Ok(..) = response { + // drop remaining lines + while let Some(_drop) = rt.block_on(self.agent.next()) { + log::debug!(" drop: {:x?}", _drop); + } + + return Ok(()); + } + } + + Err(anyhow!("Error sending command {}", cmd)) + } + + pub fn shutdown_scd(agent: Option) -> Result<()> { + let mut scdc = Self::new(agent, false)?; + + scdc.send("SCD RESTART")?; + scdc.send("SCD BYE")?; + + Ok(()) + } } impl CardClient for ScdClient {