From 69b537545cbefe41884d6aab33f92931a9b32496 Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Wed, 28 Jun 2023 22:02:00 -0400 Subject: [PATCH] Make server example signal handling work on Linux --- examples/webserver/platform/src/lib.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/examples/webserver/platform/src/lib.rs b/examples/webserver/platform/src/lib.rs index ad54ace3592..e803a77baa3 100644 --- a/examples/webserver/platform/src/lib.rs +++ b/examples/webserver/platform/src/lib.rs @@ -39,6 +39,7 @@ unsafe extern "C" fn signal_handler(sig: c_int, _: *mut siginfo_t, _: *mut libc: }); } +#[cfg(target_os = "macos")] fn setup_signal(sig: c_int) { let sa = libc::sigaction { sa_sigaction: signal_handler as sighandler_t, @@ -58,6 +59,28 @@ fn setup_signal(sig: c_int) { } } +#[cfg(target_os = "linux")] +fn setup_signal(sig: c_int) { + let sa = libc::sigaction { + sa_sigaction: signal_handler as sighandler_t, + sa_mask: unsafe { std::mem::zeroed() }, + sa_flags: SA_SIGINFO, + sa_restorer: None, + }; + + let mut old_sa = libc::sigaction { + sa_sigaction: SIG_DFL, + sa_mask: unsafe { std::mem::zeroed() }, + sa_flags: 0, + sa_restorer: None, + }; + + unsafe { + sigemptyset(&mut old_sa.sa_mask as *mut sigset_t); + sigaction(sig, &sa, &mut old_sa); + } +} + fn call_roc(req_bytes: &[u8]) -> Response { let mut setjmp_result = 0;