From e2bf778b5991929c45b4ae5201fa14d4da6110a6 Mon Sep 17 00:00:00 2001 From: Jose Quintana Date: Wed, 23 Jun 2021 18:16:44 +0200 Subject: [PATCH] feat: windows 64-bit and arm64 targets it introduces two new windows targets: - x86_64-pc-windows-msvc - aarch64-pc-windows-msvc --- .github/workflows/devel.yml | 20 ++++++++++---------- .github/workflows/release.yml | 20 ++++++++++---------- Cargo.lock | 36 ++++++++++++++---------------------- Cargo.toml | 7 +------ README.md | 5 +++-- src/server.rs | 23 ++--------------------- src/signals.rs | 64 +++++++++++++++++----------------------------------------------- 7 files changed, 57 insertions(+), 118 deletions(-) diff --git a/.github/workflows/devel.yml b/.github/workflows/devel.yml index 3331bcf..8f19624 100644 --- a/.github/workflows/devel.yml +++ b/.github/workflows/devel.yml @@ -32,8 +32,8 @@ jobs: - linux-arm-gnueabihf - macos - macos-arm64 - # - windows-msvc - # - windows-msvc-arm64 + - windows-msvc + - windows-msvc-arm64 include: - build: pinned os: ubuntu-20.04 @@ -72,14 +72,14 @@ jobs: os: macos-latest rust: nightly target: aarch64-apple-darwin - # - build: windows-msvc - # os: windows-2019 - # rust: nightly - # target: x86_64-pc-windows-msvc - # - build: windows-msvc-arm64 - # os: windows-2019 - # rust: nightly - # target: aarch64-pc-windows-msvc + - build: windows-msvc + os: windows-2019 + rust: nightly + target: x86_64-pc-windows-msvc + - build: windows-msvc-arm64 + os: windows-2019 + rust: nightly + target: aarch64-pc-windows-msvc steps: - name: Checkout repository diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index cf6aaa1..fa9f81d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -56,8 +56,8 @@ jobs: - linux-arm-gnueabihf - macos - macos-arm64 - # - windows-msvc - # - windows-msvc-arm64 + - windows-msvc + - windows-msvc-arm64 include: - build: linux-musl os: ubuntu-20.04 @@ -87,14 +87,14 @@ jobs: os: macos-latest rust: nightly target: aarch64-apple-darwin - # - build: windows-msvc - # os: windows-2019 - # rust: nightly - # target: x86_64-pc-windows-msvc - # - build: windows-msvc-arm64 - # os: windows-2019 - # rust: nightly - # target: aarch64-pc-windows-msvc + - build: windows-msvc + os: windows-2019 + rust: nightly + target: x86_64-pc-windows-msvc + - build: windows-msvc-arm64 + os: windows-2019 + rust: nightly + target: aarch64-pc-windows-msvc steps: - name: Checkout repository diff --git a/Cargo.lock b/Cargo.lock index 2575697..8bdda13 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -172,6 +172,16 @@ dependencies = [ ] [[package]] +name = "ctrlc" +version = "3.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "232295399409a8b7ae41276757b5a1cc21032848d42bff2352261f958b3ca29a" +dependencies = [ + "nix", + "winapi", +] + +[[package]] name = "digest" version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -565,15 +575,14 @@ dependencies = [ [[package]] name = "nix" -version = "0.14.1" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c722bee1037d430d0f8e687bbdbf222f27cc6e4e68d5caf630857bb2b6dbdce" +checksum = "fa9b4819da1bc61c0ea48b63b7bc8604064dd43013e7cc325df098d49cd7c18a" dependencies = [ "bitflags", "cc", - "cfg-if 0.1.10", + "cfg-if 1.0.0", "libc", - "void", ] [[package]] @@ -814,16 +823,6 @@ dependencies = [ ] [[package]] -name = "signal" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f6ce83b159ab6984d2419f495134972b48754d13ff2e3f8c998339942b56ed9" -dependencies = [ - "libc", - "nix", -] - -[[package]] name = "slab" version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -858,6 +857,7 @@ dependencies = [ "anyhow", "async-compression", "bytes", + "ctrlc", "futures", "headers", "http", @@ -866,12 +866,10 @@ dependencies = [ "jemallocator", "listenfd", "mime_guess", - "nix", "num_cpus", "once_cell", "percent-encoding", "pin-project", - "signal", "structopt", "time", "tokio", @@ -1140,12 +1138,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" [[package]] -name = "void" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" - -[[package]] name = "want" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/Cargo.toml b/Cargo.toml index b44f3da..e34d99a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,12 +46,7 @@ tokio-rustls = { version = "0.22" } humansize = "1.1" time = "0.1" listenfd = "0.3" - -[target.'cfg(not(windows))'.dependencies.nix] -version = "0.14" - -[target.'cfg(not(windows))'.dependencies.signal] -version = "0.7" +ctrlc = { version = "3.1", features = ["termination"] } [target.'cfg(all(target_env = "musl", target_pointer_width = "64"))'.dependencies.jemallocator] version = "0.3" diff --git a/README.md b/README.md index 5fac1aa..ec4027b 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ Below the current supported targets. - x86_64-unknown-linux-musl (64-bit) - aarch64-unknown-linux-musl (ARM64) - aarch64-unknown-linux-gnu (ARM64) -- arm-unknown-linux-gnueabihf (ARM64) +- arm-unknown-linux-gnueabihf (ARM) #### macOS @@ -57,7 +57,8 @@ Below the current supported targets. #### Windows -\* **Note:** Windows support is on the TODO list yet. +- x86_64-pc-windows-msvc (64-bit) +- aarch64-pc-windows-msvc (ARM64) ## Usage diff --git a/src/server.rs b/src/server.rs index bd8b555..6308b39 100644 --- a/src/server.rs +++ b/src/server.rs @@ -8,7 +8,7 @@ use crate::handler::{RequestHandler, RequestHandlerOpts}; use crate::tls::{TlsAcceptor, TlsConfigBuilder}; use crate::Result; use crate::{config::Config, service::RouterService}; -use crate::{cors, error_page, helpers, logger}; +use crate::{cors, error_page, helpers, logger, signals}; /// Define a multi-thread HTTP or HTTP/2 web server. pub struct Server { @@ -171,9 +171,7 @@ impl Server { }); } - handle_signals(); - - Ok(()) + signals::wait_for_ctrl_c() } } @@ -182,20 +180,3 @@ impl Default for Server { Self::new() } } - -#[cfg(not(windows))] -/// Handle incoming signals for Unix-like OS's only -fn handle_signals() { - use crate::signals; - - signals::wait(|sig: signals::Signal| { - let code = signals::as_int(sig); - tracing::warn!("Signal {} caught. Server execution exited.", code); - std::process::exit(code) - }); -} - -#[cfg(windows)] -fn handle_signals() { - // TODO: Windows signals... -} diff --git a/src/signals.rs b/src/signals.rs index 6947b11..a0a75f3 100644 --- a/src/signals.rs +++ b/src/signals.rs @@ -1,51 +1,21 @@ -use nix::errno::Errno; -use nix::libc::c_int; -use nix::sys::signal::{SIGCHLD, SIGINT, SIGTERM}; -use nix::sys::wait::WaitStatus::{Exited, Signaled, StillAlive}; -use nix::sys::wait::{waitpid, WaitPidFlag}; -use nix::Error; +use ctrlc; +use std::sync::mpsc::channel; -pub use signal::Signal; +use crate::{Context, Result}; -/// It waits for an incoming Termination Signal like Ctrl+C (SIGINT), SIGTERM, etc -pub fn wait(func: F) -where - F: Fn(signal::Signal), -{ - let sig_trap = signal::trap::Trap::trap(&[SIGTERM, SIGINT, SIGCHLD]); - for sig in sig_trap { - match sig { - SIGCHLD => { - // Current std::process::Command ip does not have a way to find - // process id, so we just wait until we have no children - loop { - match waitpid(None, Some(WaitPidFlag::WNOHANG)) { - Ok(Exited(pid, status)) => { - println!("{} exited with status {}", pid, status); - continue; - } - Ok(Signaled(pid, sig, _)) => { - println!("{} killed by {}", pid, sig as c_int); - continue; - } - Ok(StillAlive) => break, - Ok(status) => { - println!("Temporary status {:?}", status); - continue; - } - Err(Error::Sys(Errno::ECHILD)) => return, - Err(e) => { - panic!("Error {:?}", e); - } - } - } - } - sig => func(sig), - } - } -} +/// It waits for a `Ctrl-C` signal. +pub fn wait_for_ctrl_c() -> Result { + let (tx, rx) = channel(); + + ctrlc::set_handler(move || tx.send(()).expect("Could not send signal on channel.")) + .with_context(|| "Error setting Ctrl-C handler.".to_owned())?; + + tracing::info!("Press Ctrl+C to shutdown server."); + + rx.recv() + .with_context(|| "Could not receive signal from channel.".to_owned())?; + + tracing::warn!("Ctrl+C signal caught, shutting down server execution."); -/// It casts a given `signal::Signal` to `i32`. -pub fn as_int(sig: signal::Signal) -> i32 { - sig as c_int + Ok(()) } -- libgit2 1.7.2