Add a shutdown_scd() function, to explore managing scdaemon.
This commit is contained in:
parent
833cdbc238
commit
897847cb46
1 changed files with 42 additions and 6 deletions
|
@ -60,7 +60,7 @@ impl ScdClient {
|
||||||
///
|
///
|
||||||
/// If `agent` is None, a Context with the default GnuPG home directory
|
/// If `agent` is None, a Context with the default GnuPG home directory
|
||||||
/// is used.
|
/// is used.
|
||||||
fn new(agent: Option<Agent>) -> Result<Self> {
|
fn new(agent: Option<Agent>, init: bool) -> Result<Self> {
|
||||||
let agent = if let Some(agent) = agent {
|
let agent = if let Some(agent) = agent {
|
||||||
agent
|
agent
|
||||||
} else {
|
} else {
|
||||||
|
@ -74,9 +74,11 @@ impl ScdClient {
|
||||||
card_caps: None,
|
card_caps: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if init {
|
||||||
// call "SCD SERIALNO", which causes scdaemon to be started by gpg
|
// call "SCD SERIALNO", which causes scdaemon to be started by gpg
|
||||||
// agent (if it's not running yet)
|
// agent (if it's not running yet)
|
||||||
scdc.init()?;
|
scdc.init()?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(scdc)
|
Ok(scdc)
|
||||||
}
|
}
|
||||||
|
@ -109,7 +111,7 @@ impl ScdClient {
|
||||||
pub fn open(
|
pub fn open(
|
||||||
agent: Option<Agent>,
|
agent: Option<Agent>,
|
||||||
) -> Result<CardClientBox, OpenpgpCardError> {
|
) -> Result<CardClientBox, OpenpgpCardError> {
|
||||||
let card = ScdClient::new(agent)?;
|
let card = ScdClient::new(agent, true)?;
|
||||||
Ok(Box::new(card) as CardClientBox)
|
Ok(Box::new(card) as CardClientBox)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,7 +121,7 @@ impl ScdClient {
|
||||||
agent: Option<Agent>,
|
agent: Option<Agent>,
|
||||||
serial: &str,
|
serial: &str,
|
||||||
) -> Result<CardClientBox, OpenpgpCardError> {
|
) -> Result<CardClientBox, OpenpgpCardError> {
|
||||||
let mut card = ScdClient::new(agent)?;
|
let mut card = ScdClient::new(agent, true)?;
|
||||||
card.select_card(serial)?;
|
card.select_card(serial)?;
|
||||||
|
|
||||||
Ok(Box::new(card) as CardClientBox)
|
Ok(Box::new(card) as CardClientBox)
|
||||||
|
@ -152,6 +154,40 @@ impl ScdClient {
|
||||||
|
|
||||||
Err(anyhow!("Card not found"))
|
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<Agent>) -> Result<()> {
|
||||||
|
let mut scdc = Self::new(agent, false)?;
|
||||||
|
|
||||||
|
scdc.send("SCD RESTART")?;
|
||||||
|
scdc.send("SCD BYE")?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CardClient for ScdClient {
|
impl CardClient for ScdClient {
|
||||||
|
|
Loading…
Reference in a new issue