]> git.bitcoin.ninja Git - ldk-sample/commitdiff
Ignore Ctrl-C and enable Ctrl-D
authorElias Rohrer <ero@tnull.de>
Sun, 16 Oct 2022 06:23:29 +0000 (08:23 +0200)
committerElias Rohrer <ero@tnull.de>
Wed, 19 Oct 2022 09:17:01 +0000 (11:17 +0200)
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
Cargo.toml
src/cli.rs
src/main.rs

index f85bf81772e89b9c70c8016648e1fa9801a51c0f..be7983146fab886db1570a69675ab373d3aad34c 100644 (file)
@@ -216,6 +216,7 @@ dependencies = [
  "chrono",
  "futures",
  "hex",
+ "libc",
  "lightning",
  "lightning-background-processor",
  "lightning-block-sync",
index 0f20050e8a63d17023978cf510272fbeee544553..ea92890a12848ab7210eb540286ac821ac84fb6e 100644 (file)
@@ -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"
index 7d865158e6e7e786a93b52ff99a513d766f50b08..8dbba3d31b0f5af1eabf095da2fc73b13d361d75 100644 (file)
@@ -147,7 +147,9 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
        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 <your-supplied-ldk-data-dir-path>/.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<E: EventHandler>(
                        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<E: EventHandler>(
                                                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 <message>");
        println!("sendonionmessage <node_id_1,node_id_2,..,destination_node_id>");
+       println!("quit")
 }
 
 fn node_info(channel_manager: &Arc<ChannelManager>, peer_manager: &Arc<PeerManager>) {
index 6cff9f152492058c156e7fb1cf48ccb4e3910f38..f3d888bca37e6818e0e4db727a64792c014fbdf6 100644 (file)
@@ -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;
 }