diff --git a/Cargo.lock b/Cargo.lock index e59e95aa8e..d2bce90204 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -969,12 +969,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "cfg_aliases" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" - [[package]] name = "chacha20" version = "0.9.1" @@ -4678,18 +4672,6 @@ dependencies = [ "libc", ] -[[package]] -name = "nix" -version = "0.28.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab2156c4fce2f8df6c499cc1c763e4394b7482525bf2a9701c9d79d215f519e4" -dependencies = [ - "bitflags 2.5.0", - "cfg-if", - "cfg_aliases", - "libc", -] - [[package]] name = "node-launchpad" version = "0.3.7" @@ -4712,7 +4694,6 @@ dependencies = [ "lazy_static", "libc", "log", - "nix 0.28.0", "pretty_assertions", "prometheus-parse", "rand 0.8.5", diff --git a/node-launchpad/Cargo.toml b/node-launchpad/Cargo.toml index 99f506b5b5..a02e46e61d 100644 --- a/node-launchpad/Cargo.toml +++ b/node-launchpad/Cargo.toml @@ -40,7 +40,6 @@ json5 = "0.4.1" lazy_static = "1.4.0" libc = "0.2.148" log = "0.4.20" -nix = { version = "0.28.0", features = ["user"] } pretty_assertions = "1.4.0" prometheus-parse = "0.2.5" rand = "0.8.5" diff --git a/node-launchpad/src/bin/tui/main.rs b/node-launchpad/src/bin/tui/main.rs index 754848e644..b95b154297 100644 --- a/node-launchpad/src/bin/tui/main.rs +++ b/node-launchpad/src/bin/tui/main.rs @@ -8,6 +8,9 @@ mod terminal; +#[macro_use] +extern crate tracing; + use clap::Parser; use color_eyre::eyre::Result; use node_launchpad::{ @@ -15,6 +18,8 @@ use node_launchpad::{ config::configure_winsw, utils::{initialize_logging, initialize_panic_handler, version}, }; +#[cfg(target_os = "windows")] +use sn_node_manager::config::is_running_as_root; use sn_peers_acquisition::PeersArgs; use std::{env, path::PathBuf}; use tokio::task::LocalSet; @@ -51,12 +56,10 @@ pub struct Cli { } async fn tokio_main() -> Result<()> { - initialize_logging()?; - initialize_panic_handler()?; - let args = Cli::parse(); + info!("Starting app with args: {args:?}"); let mut app = App::new( args.tick_rate, args.frame_rate, @@ -74,14 +77,28 @@ fn is_running_in_terminal() -> bool { #[tokio::main] async fn main() -> Result<()> { + initialize_logging()?; configure_winsw().await?; if !is_running_in_terminal() { + info!("Running in non-terminal mode. Launching terminal."); // If we weren't already running in a terminal, this process returns early, having spawned // a new process that launches a terminal. let terminal_type = terminal::detect_and_setup_terminal()?; - terminal::launch_terminal(&terminal_type)?; + terminal::launch_terminal(&terminal_type) + .inspect_err(|err| error!("Error while launching terminal: {err:?}"))?; return Ok(()); + } else { + // Windows spawns the terminal directly, so the check for root has to happen here as well. + debug!("Running inside a terminal!"); + #[cfg(target_os = "windows")] + if !is_running_as_root() { + { + // todo: There is no terminal to show this error message when double clicking on the exe. + error!("Admin privileges required to run on Windows. Exiting."); + color_eyre::eyre::bail!("Admin privileges required to run on Windows. Exiting."); + } + } } // Construct a local task set that can run `!Send` futures. diff --git a/node-launchpad/src/bin/tui/terminal.rs b/node-launchpad/src/bin/tui/terminal.rs index 5d1c6b4ca0..e02040460d 100644 --- a/node-launchpad/src/bin/tui/terminal.rs +++ b/node-launchpad/src/bin/tui/terminal.rs @@ -8,6 +8,8 @@ use clap::Parser; use color_eyre::eyre::{eyre, Result}; +#[cfg(target_os = "windows")] +use sn_node_manager::config::is_running_as_root; use std::{path::PathBuf, process::Command}; use which::which; @@ -24,24 +26,12 @@ pub(crate) enum TerminalType { WindowsTerminal(PathBuf), Xterm(PathBuf), } - -#[cfg(not(windows))] -pub(crate) fn is_running_root() -> bool { - use nix::unistd::geteuid; - geteuid().is_root() -} - -#[cfg(windows)] -pub(crate) fn is_running_root() -> bool { - // Example: Attempt to read from a typically restricted system directory - std::fs::read_dir("C:\\Windows\\System32\\config").is_ok() -} - pub(crate) fn detect_and_setup_terminal() -> Result { - if !is_running_root() { - #[cfg(target_os = "windows")] + #[cfg(target_os = "windows")] + if !is_running_as_root() { { // todo: There is no terminal to show this error message when double clicking on the exe. + error!("Admin privileges required to run on Windows. Exiting."); color_eyre::eyre::bail!("Admin privileges required to run"); } } @@ -86,6 +76,7 @@ fn get_available_linux_terminal() -> Result { } pub(crate) fn launch_terminal(terminal_type: &TerminalType) -> Result<()> { + info!("Launching terminal: {terminal_type:?}"); let launchpad_path = std::env::current_exe()?; match terminal_type { diff --git a/sn_node_manager/src/cmd/auditor.rs b/sn_node_manager/src/cmd/auditor.rs index 359d8cd6d9..b788d0edb6 100644 --- a/sn_node_manager/src/cmd/auditor.rs +++ b/sn_node_manager/src/cmd/auditor.rs @@ -6,10 +6,10 @@ // KIND, either express or implied. Please review the Licences for the specific language governing // permissions and limitations relating to use of the SAFE Network Software. -use super::{download_and_get_upgrade_bin_path, is_running_as_root, print_upgrade_summary}; +use super::{download_and_get_upgrade_bin_path, print_upgrade_summary}; use crate::{ add_services::{add_auditor, config::AddAuditorServiceOptions}, - config, + config::{self, is_running_as_root}, helpers::{download_and_extract_release, get_bin_version}, print_banner, ServiceManager, VerbosityLevel, }; diff --git a/sn_node_manager/src/cmd/daemon.rs b/sn_node_manager/src/cmd/daemon.rs index ddfc75847e..44d9cec81f 100644 --- a/sn_node_manager/src/cmd/daemon.rs +++ b/sn_node_manager/src/cmd/daemon.rs @@ -6,10 +6,9 @@ // KIND, either express or implied. Please review the Licences for the specific language governing // permissions and limitations relating to use of the SAFE Network Software. -use super::is_running_as_root; use crate::{ add_services::{add_daemon, config::AddDaemonServiceOptions}, - config, + config::{self, is_running_as_root}, helpers::{download_and_extract_release, get_bin_version}, print_banner, ServiceManager, VerbosityLevel, }; diff --git a/sn_node_manager/src/cmd/faucet.rs b/sn_node_manager/src/cmd/faucet.rs index add34c5d2f..a5acd8e0c7 100644 --- a/sn_node_manager/src/cmd/faucet.rs +++ b/sn_node_manager/src/cmd/faucet.rs @@ -6,10 +6,10 @@ // KIND, either express or implied. Please review the Licences for the specific language governing // permissions and limitations relating to use of the SAFE Network Software. -use super::{download_and_get_upgrade_bin_path, is_running_as_root, print_upgrade_summary}; +use super::{download_and_get_upgrade_bin_path, print_upgrade_summary}; use crate::{ add_services::{add_faucet, config::AddFaucetServiceOptions}, - config, + config::{self, is_running_as_root}, helpers::{download_and_extract_release, get_bin_version}, print_banner, ServiceManager, VerbosityLevel, }; diff --git a/sn_node_manager/src/cmd/mod.rs b/sn_node_manager/src/cmd/mod.rs index b612536e7e..ec4055a7a3 100644 --- a/sn_node_manager/src/cmd/mod.rs +++ b/sn_node_manager/src/cmd/mod.rs @@ -27,16 +27,6 @@ use std::{ process::{Command, Stdio}, }; -#[cfg(unix)] -pub fn is_running_as_root() -> bool { - users::get_effective_uid() == 0 -} - -#[cfg(windows)] -pub fn is_running_as_root() -> bool { - true -} - pub async fn download_and_get_upgrade_bin_path( custom_bin_path: Option, release_type: ReleaseType, diff --git a/sn_node_manager/src/cmd/node.rs b/sn_node_manager/src/cmd/node.rs index c90c7edaed..717a3f9125 100644 --- a/sn_node_manager/src/cmd/node.rs +++ b/sn_node_manager/src/cmd/node.rs @@ -8,13 +8,13 @@ #![allow(clippy::too_many_arguments)] -use super::{download_and_get_upgrade_bin_path, is_running_as_root, print_upgrade_summary}; +use super::{download_and_get_upgrade_bin_path, print_upgrade_summary}; use crate::{ add_services::{ add_node, config::{AddNodeServiceOptions, PortRange}, }, - config, + config::{self, is_running_as_root}, helpers::{download_and_extract_release, get_bin_version}, print_banner, refresh_node_registry, status_report, ServiceManager, VerbosityLevel, }; diff --git a/sn_node_manager/src/config.rs b/sn_node_manager/src/config.rs index 5648625d12..64cb732e0a 100644 --- a/sn_node_manager/src/config.rs +++ b/sn_node_manager/src/config.rs @@ -244,13 +244,14 @@ pub fn create_owned_dir(path: PathBuf, _owner: &str) -> Result<()> { #[cfg(unix)] pub fn is_running_as_root() -> bool { - users::get_effective_uid() == 0 + use nix::unistd::geteuid; + geteuid().is_root() } #[cfg(windows)] pub fn is_running_as_root() -> bool { - // The Windows implementation for this will be much more complex. - true + // Example: Attempt to read from a typically restricted system directory + std::fs::read_dir("C:\\Windows\\System32\\config").is_ok() } pub fn get_user_safenode_data_dir() -> Result {