keyfork: add `--daemon`
This commit is contained in:
parent
35e0eb57a0
commit
4e342ac7a9
|
@ -47,3 +47,4 @@ clap_complete = { version = "4.4.6", optional = true }
|
||||||
sequoia-openpgp = { workspace = true }
|
sequoia-openpgp = { workspace = true }
|
||||||
keyforkd-models.workspace = true
|
keyforkd-models.workspace = true
|
||||||
base64.workspace = true
|
base64.workspace = true
|
||||||
|
nix = { version = "0.29.0", default-features = false, features = ["process"] }
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
use super::Keyfork;
|
use super::Keyfork;
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use nix::{
|
||||||
|
sys::wait::waitpid,
|
||||||
|
unistd::{fork, ForkResult},
|
||||||
|
};
|
||||||
|
|
||||||
use keyfork_mnemonic::{English, Mnemonic};
|
use keyfork_mnemonic::{English, Mnemonic};
|
||||||
use keyfork_prompt::{
|
use keyfork_prompt::{
|
||||||
|
@ -80,12 +84,32 @@ impl RecoverSubcommands {
|
||||||
pub struct Recover {
|
pub struct Recover {
|
||||||
#[command(subcommand)]
|
#[command(subcommand)]
|
||||||
command: RecoverSubcommands,
|
command: RecoverSubcommands,
|
||||||
|
|
||||||
|
/// Daemonize the server once started, restoring control back to the shell.
|
||||||
|
#[arg(long, global=true)]
|
||||||
|
daemon: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Recover {
|
impl Recover {
|
||||||
pub fn handle(&self, _k: &Keyfork) -> Result<()> {
|
pub fn handle(&self, _k: &Keyfork) -> Result<()> {
|
||||||
let seed = self.command.handle()?;
|
let seed = self.command.handle()?;
|
||||||
let mnemonic = Mnemonic::try_from_slice(&seed)?;
|
let mnemonic = Mnemonic::try_from_slice(&seed)?;
|
||||||
|
if self.daemon {
|
||||||
|
// SAFETY: Forking threaded programs is unsafe. We know we don't have multiple
|
||||||
|
// threads at this point.
|
||||||
|
match unsafe { fork() }? {
|
||||||
|
ForkResult::Parent { child } => {
|
||||||
|
// wait for the child to die, so we don't exit prematurely
|
||||||
|
waitpid(Some(child), None)?;
|
||||||
|
return Ok(());
|
||||||
|
},
|
||||||
|
ForkResult::Child => {
|
||||||
|
if let ForkResult::Parent { .. } = unsafe { fork() }? {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
tokio::runtime::Builder::new_multi_thread()
|
tokio::runtime::Builder::new_multi_thread()
|
||||||
.enable_all()
|
.enable_all()
|
||||||
.build()
|
.build()
|
||||||
|
|
Loading…
Reference in New Issue