Browse Source

code fixes for windows-msvc target

darkfi 3 weeks ago
parent
commit
46d7677701
7 changed files with 69 additions and 18 deletions
  1. 7 4
      bin/darkirc/Cargo.toml
  2. 2 0
      bin/darkirc/src/main.rs
  3. 5 3
      bin/drk/Cargo.toml
  4. 1 0
      bin/drk/src/lib.rs
  5. 10 1
      bin/drk/src/main.rs
  6. 10 10
      src/net/transport/mod.rs
  7. 34 0
      src/util/cli.rs

+ 7 - 4
bin/darkirc/Cargo.toml

@@ -27,8 +27,6 @@ darkfi-sdk = {path = "../../src/sdk", features = ["async"]}
 darkfi-serial = {path = "../../src/serial", features = ["async"]}
 libc = "0.2.186"
 
-tikv-jemallocator = "0.5.4"
-
 # Event Graph DB
 kvdb-overlay = {git = "https://git.dark.fi/darkrenaissance/kvdb-overlay", version = "0.1.0"}
 
@@ -55,8 +53,6 @@ semver = "1.0.28"
 
 # Daemon
 easy-parallel = "3.3.1"
-signal-hook-async-std = "0.4.0"
-signal-hook = "0.4.4"
 tracing-subscriber = { version = "0.3.23", default-features = false, features = ["fmt"] }
 tracing-appender = "0.2.5"
 smol = "2.0.2"
@@ -66,5 +62,12 @@ serde = {version = "1.0.228", features = ["derive"]}
 structopt = "0.3.26"
 structopt-toml = "0.5.1"
 
+[target.'cfg(not(target_env = "msvc"))'.dependencies]
+tikv-jemallocator = "0.5.4"
+
+[target.'cfg(not(target_os = "windows"))'.dependencies]
+signal-hook-async-std = "0.4.0"
+signal-hook = "0.4.4"
+
 [lints]
 workspace = true

+ 2 - 0
bin/darkirc/src/main.rs

@@ -234,9 +234,11 @@ struct Args {
     rpc: RpcSettingsOpt,
 }
 
+#[cfg(not(target_env = "msvc"))]
 #[global_allocator]
 static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
 
+#[cfg(not(target_env = "msvc"))]
 #[allow(non_upper_case_globals)]
 #[export_name = "malloc_conf"]
 pub static malloc_conf: &[u8] = b"dirty_decay_ms:1000,muzzy_decay_ms:1000\0";

+ 5 - 3
bin/drk/Cargo.toml

@@ -28,7 +28,6 @@ futures = "0.3.32"
 kvdb-overlay = {git = "https://git.dark.fi/darkrenaissance/kvdb-overlay", version = "0.1.0"}
 lazy_static = "1.5.0"
 libc = "0.2"
-linenoise-rs = "0.1.1"
 num-bigint = "0.4.6"
 prettytable-rs = "0.10.0"
 rand = "0.8.6"
@@ -43,8 +42,6 @@ log = { version = "0.4", features = ["release_max_level_off"] }
 
 # Daemon
 easy-parallel = "3.3.1"
-signal-hook-async-std = "0.4.0"
-signal-hook = "0.4.4"
 tracing-subscriber = { version = "0.3.23", default-features = false, features = ["fmt"] }
 tracing-appender = "0.2.5"
 smol = "2.0.2"
@@ -54,5 +51,10 @@ serde = {version = "1.0.228", features = ["derive"]}
 structopt = "0.3.26"
 structopt-toml = "0.5.1"
 
+[target.'cfg(not(target_os = "windows"))'.dependencies]
+signal-hook-async-std = "0.4.0"
+signal-hook = "0.4.4"
+linenoise-rs = "0.1.1"
+
 [lints]
 workspace = true

+ 1 - 0
bin/drk/src/lib.rs

@@ -48,6 +48,7 @@ pub mod token;
 pub mod cli_util;
 
 /// Drk interactive shell
+#[cfg(not(target_os = "windows"))]
 pub mod interactive;
 
 /// Wallet functionality related to Money

+ 10 - 1
bin/drk/src/main.rs

@@ -63,12 +63,14 @@ use drk::{
     },
     common::*,
     dao::{DaoParams, ProposalRecord},
-    interactive::interactive,
     money::BALANCE_BASE10_DECIMALS,
     swap::PartialSwapData,
     Drk,
 };
 
+#[cfg(not(target_os = "windows"))]
+use drk::interactive::interactive;
+
 const CONFIG_FILE: &str = "drk_config.toml";
 const CONFIG_FILE_CONTENTS: &str = include_str!("../drk_config.toml");
 
@@ -624,6 +626,7 @@ async fn realmain(args: Args, ex: ExecutorPtr) -> Result<()> {
     };
 
     match args.command {
+        #[cfg(not(target_os = "windows"))]
         Subcmd::Interactive => {
             // Create an unbounded smol channel, so we can have a
             // printing queue the background logger and tasks can
@@ -661,6 +664,12 @@ async fn realmain(args: Args, ex: ExecutorPtr) -> Result<()> {
             Ok(())
         }
 
+        #[cfg(target_os = "windows")]
+        Subcmd::Interactive => {
+            eprintln!("Interactive shell is not supported on this platform");
+            exit(2);
+        }
+
         Subcmd::Kaching => {
             if !args.fun {
                 println!("Apparently you don't like fun...");

+ 10 - 10
src/net/transport/mod.rs

@@ -24,7 +24,7 @@ use url::Url;
 
 use crate::util::logger::verbose;
 
-#[cfg(feature = "p2p-unix")]
+#[cfg(all(unix, feature = "p2p-unix"))]
 use std::io::ErrorKind;
 
 /// TLS upgrade mechanism
@@ -46,7 +46,7 @@ pub(crate) mod tor;
 pub(crate) mod nym;
 
 /// Unix socket transport
-#[cfg(feature = "p2p-unix")]
+#[cfg(all(unix, feature = "p2p-unix"))]
 pub(crate) mod unix;
 
 /// QUIC transport
@@ -79,7 +79,7 @@ pub enum DialerVariant {
     NymTls(nym::NymDialer),
 
     /// Unix socket
-    #[cfg(feature = "p2p-unix")]
+    #[cfg(all(unix, feature = "p2p-unix"))]
     Unix(unix::UnixDialer),
 
     /// SOCKS5 proxy
@@ -107,7 +107,7 @@ pub enum ListenerVariant {
     /// Tor
     Tor(tor::TorListener),
 
-    #[cfg(feature = "p2p-unix")]
+    #[cfg(all(unix, feature = "p2p-unix"))]
     /// Unix socket
     Unix(unix::UnixListener),
 
@@ -134,7 +134,7 @@ macro_rules! enforce_hostport {
     };
 }
 
-#[cfg(feature = "p2p-unix")]
+#[cfg(all(unix, feature = "p2p-unix"))]
 macro_rules! enforce_abspath {
     ($endpoint:ident) => {
         if $endpoint.host_str().is_some() || $endpoint.port().is_some() {
@@ -208,7 +208,7 @@ impl Dialer {
                 Ok(Self { endpoint, variant, provide_tls_client_cert })
             }
 
-            #[cfg(feature = "p2p-unix")]
+            #[cfg(all(unix, feature = "p2p-unix"))]
             "unix" => {
                 // Build a Unix socket dialer
                 enforce_abspath!(endpoint);
@@ -324,7 +324,7 @@ impl Dialer {
                 todo!();
             }
 
-            #[cfg(feature = "p2p-unix")]
+            #[cfg(all(unix, feature = "p2p-unix"))]
             DialerVariant::Unix(dialer) => {
                 let path = match self.endpoint.to_file_path() {
                     Ok(v) => v,
@@ -421,7 +421,7 @@ impl Listener {
                 Ok(Self { endpoint, variant, require_tls_client_cert })
             }
 
-            #[cfg(feature = "p2p-unix")]
+            #[cfg(all(unix, feature = "p2p-unix"))]
             "unix" => {
                 enforce_abspath!(endpoint);
                 let variant = unix::UnixListener::new().await?;
@@ -469,7 +469,7 @@ impl Listener {
                 Ok(Box::new(l))
             }
 
-            #[cfg(feature = "p2p-unix")]
+            #[cfg(all(unix, feature = "p2p-unix"))]
             ListenerVariant::Unix(listener) => {
                 let path = match self.endpoint.to_file_path() {
                     Ok(v) => v,
@@ -546,7 +546,7 @@ impl PtStream for arti_client::DataStream {}
 #[cfg(feature = "p2p-tor")]
 impl PtStream for futures_rustls::TlsStream<arti_client::DataStream> {}
 
-#[cfg(feature = "p2p-unix")]
+#[cfg(all(unix, feature = "p2p-unix"))]
 impl PtStream for smol::net::unix::UnixStream {}
 
 #[cfg(feature = "p2p-quic")]

+ 34 - 0
src/util/cli.rs

@@ -218,6 +218,7 @@ macro_rules! async_daemonize {
         }
 
         /// Auxiliary structure used to keep track of signals
+        #[cfg(not(windows))]
         struct SignalHandler {
             /// Termination signal channel receiver
             term_rx: smol::channel::Receiver<()>,
@@ -227,6 +228,7 @@ macro_rules! async_daemonize {
             sighup_pub: darkfi::system::PublisherPtr<Args>,
         }
 
+        #[cfg(not(windows))]
         impl SignalHandler {
             fn new(
                 ex: std::sync::Arc<smol::Executor<'static>>,
@@ -257,7 +259,38 @@ macro_rules! async_daemonize {
             }
         }
 
+        /// Windows lacks POSIX signal handling, so the process is simply
+        /// terminated externally and this handler blocks forever.
+        #[cfg(windows)]
+        struct SignalHandler {
+            /// Termination signal channel receiver
+            term_rx: smol::channel::Receiver<()>,
+        }
+
+        #[cfg(windows)]
+        impl SignalHandler {
+            fn new(
+                ex: std::sync::Arc<smol::Executor<'static>>,
+            ) -> Result<(Self, smol::Task<Result<()>>)> {
+                let (term_tx, term_rx) = smol::channel::bounded::<()>(1);
+                // Keep the sender alive so the channel never disconnects.
+                std::mem::forget(term_tx);
+                let signals_task = ex.spawn(async { Ok::<(), darkfi::Error>(()) });
+
+                Ok((Self { term_rx }, signals_task))
+            }
+
+            /// Handler waits for termination signal
+            async fn wait_termination(&self, signals_task: smol::Task<Result<()>>) -> Result<()> {
+                let _ = signals_task;
+                let _ = self.term_rx.recv().await;
+
+                Ok(())
+            }
+        }
+
         /// Auxiliary task to handle SIGINT for forceful process abort
+        #[cfg(not(windows))]
         async fn handle_abort(mut signals: signal_hook_async_std::Signals) {
             let mut n_sigint = 0;
             while let Some(signal) = signals.next().await {
@@ -271,6 +304,7 @@ macro_rules! async_daemonize {
         }
 
         /// Auxiliary task to handle SIGHUP, SIGTERM, SIGINT and SIGQUIT signals
+        #[cfg(not(windows))]
         async fn handle_signals(
             mut signals: signal_hook_async_std::Signals,
             term_tx: smol::channel::Sender<()>,