From e9d2908765c3970f5d2d9e70eeb6459e47b372da Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Sun, 16 Oct 2022 08:23:29 +0200 Subject: [PATCH] Ignore Ctrl-C and enable Ctrl-D We ignore Ctrl-C/SIGINT via a dummy signal handler and enable the use of Ctrl-D. For convenience, we introduce "exit" and "quit" cli commands. --- Cargo.lock | 1 + Cargo.toml | 1 + src/cli.rs | 11 ++++++++++- src/main.rs | 23 +++++++++++++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index f85bf81..be79831 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -216,6 +216,7 @@ dependencies = [ "chrono", "futures", "hex", + "libc", "lightning", "lightning-background-processor", "lightning-block-sync", diff --git a/Cargo.toml b/Cargo.toml index 0f20050..ea92890 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ bitcoin = "0.29.0" bitcoin-bech32 = "0.12" bech32 = "0.8" hex = "0.3" +libc = "0.2" futures = "0.3" chrono = "0.4" diff --git a/src/cli.rs b/src/cli.rs index 7d86515..8dbba3d 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -147,7 +147,9 @@ pub(crate) async fn poll_for_user_input( inbound_payments: PaymentInfoStorage, outbound_payments: PaymentInfoStorage, ldk_data_dir: String, network: Network, ) { - println!("LDK startup successful. To view available commands: \"help\"."); + println!( + "LDK startup successful. Enter \"help\" to view available commands. Press Ctrl-D to quit." + ); println!("LDK logs are available at /.ldk/logs"); println!("Local Node ID is {}.", channel_manager.get_our_node_id()); loop { @@ -158,6 +160,11 @@ pub(crate) async fn poll_for_user_input( break println!("ERROR: {e:#}"); } + if line.len() == 0 { + // We hit EOF / Ctrl-D + break; + } + let mut words = line.split_whitespace(); if let Some(word) = words.next() { match word { @@ -459,6 +466,7 @@ pub(crate) async fn poll_for_user_input( Err(e) => println!("ERROR: failed to send onion message: {:?}", e), } } + "quit" | "exit" => break, _ => println!("Unknown command. See `\"help\" for available commands."), } } @@ -479,6 +487,7 @@ fn help() { println!("listpeers"); println!("signmessage "); println!("sendonionmessage "); + println!("quit") } fn node_info(channel_manager: &Arc, peer_manager: &Arc) { diff --git a/src/main.rs b/src/main.rs index 6cff9f1..f3d888b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -775,5 +775,28 @@ async fn start_ldk() { #[tokio::main] pub async fn main() { + #[cfg(not(target_os = "windows"))] + { + // Catch Ctrl-C with a dummy signal handler. + unsafe { + let mut new_action: libc::sigaction = core::mem::zeroed(); + let mut old_action: libc::sigaction = core::mem::zeroed(); + + extern "C" fn dummy_handler( + _: libc::c_int, _: *const libc::siginfo_t, _: *const libc::c_void, + ) { + } + + new_action.sa_sigaction = dummy_handler as libc::sighandler_t; + new_action.sa_flags = libc::SA_SIGINFO; + + libc::sigaction( + libc::SIGINT, + &new_action as *const libc::sigaction, + &mut old_action as *mut libc::sigaction, + ); + } + } + start_ldk().await; } -- 2.30.2