Skip to content

Commit

Permalink
Merge pull request #77 from tnull/2022-10-catch-ctrlc
Browse files Browse the repository at this point in the history
Catch Ctrl-C and enable Ctrl-D
  • Loading branch information
TheBlueMatt committed Oct 24, 2022
2 parents 523ca2d + e20eeb0 commit 76c26ba
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ jobs:
build:
strategy:
matrix:
platform: [ ubuntu-latest, macos-latest, windows-latest ]
toolchain: [ stable, beta ]
include:
- toolchain: stable
check-fmt: true
runs-on: ubuntu-latest
runs-on: ${{ matrix.platform }}
steps:
- name: Checkout source code
uses: actions/checkout@v2
Expand Down
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 @@ -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"
Expand Down
11 changes: 10 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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."),
}
}
Expand All @@ -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>) {
Expand Down
23 changes: 23 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,5 +772,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;
}

0 comments on commit 76c26ba

Please sign in to comment.