Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ capnp-rpc = "0.23"
tokio = { version = "1", features = ["net", "rt"] }
tokio-util = { version = "0.7", features = ["compat"] }
futures = "0.3.0"
libc = "0.2"

[build-dependencies]
capnpc = "0.23"
Expand Down
5 changes: 5 additions & 0 deletions config_spec.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ name = "connect"
type = "String"
optional = true
doc = "Connect only to this node (format: ip:port or hostname:port)"

[[param]]
name = "daemon"
type = "bool"
default = "false"
1 change: 1 addition & 0 deletions src/bin/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ fn main() {
Commands::Stop => {
let shutdown_req = client.shutdown_request();
shutdown_req.send().promise.await.unwrap();
println!("Kernel node stopping...");
}
}
}))
Expand Down
11 changes: 10 additions & 1 deletion src/bin/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ use bitcoinkernel::{
core::BlockHashExt, prelude::BlockValidationStateExt, ChainType, ChainstateManagerBuilder,
Context, ContextBuilder, Log, Logger, SynchronizationState, ValidationMode,
};
use kernel_node::peer::{BitcoinPeer, NodeState, TipState};
use kernel_node::{
daemonize::Daemonize,
peer::{BitcoinPeer, NodeState, TipState},
};
use kernel_node::{
ipc::IpcInterface,
kernel_util::{ChainExt, DirnameExt},
Expand Down Expand Up @@ -347,6 +350,12 @@ fn main() {
START.call_once(|| {
setup_logging();
});
if config.daemon {
let daemonize = Daemonize::new(config.datadir.data_dir());
info!("Kernel node starting...");
daemonize.fork().unwrap();
}

let (shutdown_tx, shutdown_rx) = mpsc::channel();
let ipc_shutdown = shutdown_tx.clone();

Expand Down
40 changes: 40 additions & 0 deletions src/daemonize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::{
fs::{self, File},
os::fd::IntoRawFd,
};

const MASK_OCTAL: u32 = 0o027;
const DEV_NULL: &str = "/dev/null";

pub struct Daemonize {
working_directory: String,
}

impl Daemonize {
pub fn new(working_directory: String) -> Self {
Self { working_directory }
}

pub fn fork(self) -> std::io::Result<()> {
match unsafe { libc::fork() } {
-1 => return Err(std::io::Error::last_os_error()),
0 => (),
_pid => std::process::exit(0),
}
if unsafe { libc::setsid() } == -1 {
return Err(std::io::Error::last_os_error());
}
std::env::set_current_dir(&self.working_directory)?;
unsafe { libc::umask(MASK_OCTAL) };
let null_std_in = File::open(DEV_NULL)?;
unsafe { libc::dup2(null_std_in.into_raw_fd(), libc::STDIN_FILENO) };
let pid_file = format!("{}/node.pid", self.working_directory);
let process_id = std::process::id();
fs::write(pid_file, process_id.to_string())?;
let log_file = format!("{}/node.log", self.working_directory);
let log_file = File::create(log_file)?;
unsafe { libc::dup2(log_file.into_raw_fd(), libc::STDOUT_FILENO) };
unsafe { libc::dup2(libc::STDOUT_FILENO, libc::STDERR_FILENO) };
Ok(())
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod daemonize;
pub mod ipc;
pub mod kernel_util;
pub mod peer;
Expand Down
Loading