wallet: resolve the wallet-rpc binary against the executable, then PATH
This commit is contained in:
parent
1e3eceabcb
commit
96ba463d5f
2 changed files with 202 additions and 7 deletions
18
src/node.rs
18
src/node.rs
|
|
@ -654,7 +654,7 @@ impl Node {
|
|||
) -> Result<(), String> {
|
||||
let mut cfg = self.wallet_child_config();
|
||||
if cfg.wallet_rpc_bin.as_os_str().is_empty() {
|
||||
cfg.wallet_rpc_bin = PathBuf::from("monero-wallet-rpc");
|
||||
cfg.wallet_rpc_bin = crate::wallet_child::default_wallet_rpc_bin();
|
||||
}
|
||||
let name = match (network, prune) {
|
||||
(ChainNetwork::Mainnet, true) => "mainnet-pruned",
|
||||
|
|
@ -677,7 +677,7 @@ impl Node {
|
|||
pub fn set_remote_daemon(&self, host: &str, port: u16) -> Result<(), String> {
|
||||
let mut cfg = self.wallet_child_config();
|
||||
if cfg.wallet_rpc_bin.as_os_str().is_empty() {
|
||||
cfg.wallet_rpc_bin = PathBuf::from("monero-wallet-rpc");
|
||||
cfg.wallet_rpc_bin = crate::wallet_child::default_wallet_rpc_bin();
|
||||
}
|
||||
cfg.source = ChainSource::Remote {
|
||||
host: host.trim().to_string(),
|
||||
|
|
@ -720,7 +720,7 @@ impl Node {
|
|||
}
|
||||
let mut cfg = self.wallet_child_config();
|
||||
if cfg.wallet_rpc_bin.as_os_str().is_empty() {
|
||||
cfg.wallet_rpc_bin = PathBuf::from("monero-wallet-rpc");
|
||||
cfg.wallet_rpc_bin = crate::wallet_child::default_wallet_rpc_bin();
|
||||
}
|
||||
cfg.source = ChainSource::Local {
|
||||
monerod_bin: monerod_bin.to_path_buf(),
|
||||
|
|
@ -3678,10 +3678,18 @@ pub(crate) mod tests {
|
|||
}
|
||||
);
|
||||
assert!(!remote.source.is_onion());
|
||||
// An empty binary is filled in by the resolver, not by a bare name it
|
||||
// assumes is on PATH: whatever it answers here is the exact path the
|
||||
// spawn would use, so a bundled copy beside the app is picked up with
|
||||
// no configuration at all.
|
||||
assert_eq!(
|
||||
remote.wallet_rpc_bin,
|
||||
PathBuf::from("monero-wallet-rpc"),
|
||||
"an empty binary is filled in, like the in-process source"
|
||||
crate::wallet_child::default_wallet_rpc_bin(),
|
||||
"an empty binary is filled in by the resolver, like the in-process source"
|
||||
);
|
||||
assert!(
|
||||
!remote.wallet_rpc_bin.as_os_str().is_empty(),
|
||||
"the resolver never leaves the field empty"
|
||||
);
|
||||
|
||||
node.set_remote_daemon("peer.onion", 18089)
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@
|
|||
//! and dropping it shuts the node down. It still binds the same loopback RPC
|
||||
//! port, so everything downstream of `--daemon-address` is unchanged.
|
||||
|
||||
use std::ffi::OsStr;
|
||||
use std::fs;
|
||||
use std::net::{Ipv4Addr, SocketAddr, TcpStream};
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
|
|
@ -49,8 +50,89 @@ use crate::wallet::{self, ChainNetwork, ChainSource, Wallet};
|
|||
pub const DEFAULT_MONEROD_RPC_PORT: u16 = 18081;
|
||||
|
||||
/// Name of the `monero-wallet-rpc` binary when the caller does not point at one.
|
||||
///
|
||||
/// A bare name is the **last** step of [`resolve_wallet_rpc_bin`], never the
|
||||
/// first: the release ships the official Monero binary beside the app, so a
|
||||
/// user who unpacks the tarball must not also need it on `PATH`.
|
||||
pub const DEFAULT_WALLET_RPC_BIN: &str = "monero-wallet-rpc";
|
||||
|
||||
/// Shown when no `monero-wallet-rpc` exists anywhere the app looks. The desktop
|
||||
/// twin of Android's `NO_WALLET_RPC_MESSAGE`: one string, so the F7 row and the
|
||||
/// start path can never disagree about why a start cannot work.
|
||||
pub const NO_WALLET_RPC_MESSAGE: &str =
|
||||
"monero-wallet-rpc not found — put it beside this binary, in share/onionwire/, or on PATH";
|
||||
|
||||
/// The path the wallet child will spawn, in the order the app looks:
|
||||
///
|
||||
/// 1. `explicit`, when the caller set one. A user's choice is never
|
||||
/// second-guessed and is not existence-checked: a wrapper script or a path
|
||||
/// this process cannot stat is still the answer, and a bad one is named by
|
||||
/// the spawn error.
|
||||
/// 2. `<exe_dir>/monero-wallet-rpc` — where the release tarball puts it.
|
||||
/// 3. `<exe_dir>/share/onionwire/monero-wallet-rpc` — a packaged install.
|
||||
/// 4. the first executable `monero-wallet-rpc` in `PATH`.
|
||||
///
|
||||
/// `None` means nothing was found anywhere, which is what lets the F7 row say
|
||||
/// so instead of offering a start that can only fail at the spawn.
|
||||
pub fn resolve_wallet_rpc_bin(explicit: &Path) -> Option<PathBuf> {
|
||||
let exe_dir = std::env::current_exe()
|
||||
.ok()
|
||||
.and_then(|exe| exe.parent().map(Path::to_path_buf));
|
||||
resolve_wallet_rpc_bin_in(
|
||||
explicit,
|
||||
exe_dir.as_deref(),
|
||||
std::env::var_os("PATH").as_deref(),
|
||||
)
|
||||
}
|
||||
|
||||
/// What the app stores and spawns when nobody named a binary: the resolved path
|
||||
/// when there is one, else the bare name — so a configuration stays savable and
|
||||
/// the missing binary is reported by the spawn rather than by the validator.
|
||||
pub fn default_wallet_rpc_bin() -> PathBuf {
|
||||
resolve_wallet_rpc_bin(Path::new("")).unwrap_or_else(|| PathBuf::from(DEFAULT_WALLET_RPC_BIN))
|
||||
}
|
||||
|
||||
/// [`resolve_wallet_rpc_bin`] with both ambient inputs (the executable's own
|
||||
/// directory and `PATH`) spelled out, so the order is testable without mutating
|
||||
/// the process environment.
|
||||
fn resolve_wallet_rpc_bin_in(
|
||||
explicit: &Path,
|
||||
exe_dir: Option<&Path>,
|
||||
path_var: Option<&OsStr>,
|
||||
) -> Option<PathBuf> {
|
||||
if !explicit.as_os_str().is_empty() {
|
||||
return Some(explicit.to_path_buf());
|
||||
}
|
||||
if let Some(dir) = exe_dir {
|
||||
for candidate in [
|
||||
dir.join(DEFAULT_WALLET_RPC_BIN),
|
||||
dir.join("share")
|
||||
.join("onionwire")
|
||||
.join(DEFAULT_WALLET_RPC_BIN),
|
||||
] {
|
||||
if is_executable_file(&candidate) {
|
||||
return Some(candidate);
|
||||
}
|
||||
}
|
||||
}
|
||||
for dir in std::env::split_paths(path_var?) {
|
||||
let candidate = dir.join(DEFAULT_WALLET_RPC_BIN);
|
||||
if is_executable_file(&candidate) {
|
||||
return Some(candidate);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// A file this process may execute. A directory named `monero-wallet-rpc`, or a
|
||||
/// file with no execute bit, is not something the app can spawn.
|
||||
fn is_executable_file(path: &Path) -> bool {
|
||||
match fs::metadata(path) {
|
||||
Ok(meta) => meta.is_file() && meta.permissions().mode() & 0o111 != 0,
|
||||
Err(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Name of the `arti` binary the SOCKS child runs when the caller does not
|
||||
/// point at one. `arti proxy -p <port>`; there is no in-process SOCKS listener.
|
||||
pub const DEFAULT_ARTI_BIN: &str = "arti";
|
||||
|
|
@ -140,7 +222,7 @@ impl WalletChild {
|
|||
create_dir_700(&wallet_dir).map_err(|e| ChildError(format!("wallet dir: {e}")))?;
|
||||
Ok(Self {
|
||||
source,
|
||||
wallet_rpc_bin: PathBuf::from(DEFAULT_WALLET_RPC_BIN),
|
||||
wallet_rpc_bin: default_wallet_rpc_bin(),
|
||||
arti_bin: PathBuf::from(DEFAULT_ARTI_BIN),
|
||||
wallet_dir,
|
||||
wallet_filename,
|
||||
|
|
@ -783,7 +865,7 @@ impl Default for ChildConfig {
|
|||
fn default() -> Self {
|
||||
Self {
|
||||
source: ChainSource::None,
|
||||
wallet_rpc_bin: PathBuf::from(DEFAULT_WALLET_RPC_BIN),
|
||||
wallet_rpc_bin: default_wallet_rpc_bin(),
|
||||
arti_bin: PathBuf::from(DEFAULT_ARTI_BIN),
|
||||
wallet_filename: wallet::DEFAULT_WALLET_FILENAME.to_string(),
|
||||
}
|
||||
|
|
@ -2347,6 +2429,111 @@ mod tests {
|
|||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// The search order, step by step. An explicit path is the user's choice and
|
||||
/// wins over everything; below it the app looks beside its own executable,
|
||||
/// then in `share/onionwire` beside it, then on `PATH`. Nothing found is
|
||||
/// `None` — not a bare name — so the F7 row can say so instead of handing a
|
||||
/// spawn a path that only fails three seconds later.
|
||||
#[test]
|
||||
fn the_wallet_rpc_search_order_is_explicit_then_beside_the_exe_then_path() {
|
||||
let dir = tmp_dir("wallet-rpc-resolve");
|
||||
let exe_dir = dir.join("bin");
|
||||
let share = exe_dir.join("share").join("onionwire");
|
||||
let on_path = dir.join("path-bin");
|
||||
fs::create_dir_all(&share).unwrap();
|
||||
fs::create_dir_all(&on_path).unwrap();
|
||||
let log = dir.join("argv.log");
|
||||
let explicit = PathBuf::from("/opt/mine/monero-wallet-rpc");
|
||||
let bundled = fake_bin(&exe_dir, DEFAULT_WALLET_RPC_BIN, &log);
|
||||
let in_share = fake_bin(&share, DEFAULT_WALLET_RPC_BIN, &log);
|
||||
let on_path_bin = fake_bin(&on_path, DEFAULT_WALLET_RPC_BIN, &log);
|
||||
let path_var = on_path.as_os_str();
|
||||
|
||||
assert_eq!(
|
||||
resolve_wallet_rpc_bin_in(&explicit, Some(&exe_dir), Some(path_var)),
|
||||
Some(explicit.clone()),
|
||||
"an explicit path must keep winning"
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_wallet_rpc_bin_in(Path::new(""), Some(&exe_dir), Some(path_var)),
|
||||
Some(bundled.clone()),
|
||||
"the copy beside the executable is the first fallback"
|
||||
);
|
||||
fs::remove_file(&bundled).unwrap();
|
||||
assert_eq!(
|
||||
resolve_wallet_rpc_bin_in(Path::new(""), Some(&exe_dir), Some(path_var)),
|
||||
Some(in_share.clone()),
|
||||
"share/onionwire beside the executable is the second fallback"
|
||||
);
|
||||
fs::remove_file(&in_share).unwrap();
|
||||
assert_eq!(
|
||||
resolve_wallet_rpc_bin_in(Path::new(""), Some(&exe_dir), Some(path_var)),
|
||||
Some(on_path_bin.clone()),
|
||||
"PATH is the last fallback"
|
||||
);
|
||||
fs::remove_file(&on_path_bin).unwrap();
|
||||
assert_eq!(
|
||||
resolve_wallet_rpc_bin_in(Path::new(""), Some(&exe_dir), Some(path_var)),
|
||||
None,
|
||||
"nothing found is no answer, so the row can say so"
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_wallet_rpc_bin_in(Path::new(""), None, None),
|
||||
None,
|
||||
"no executable dir and no PATH is also no answer"
|
||||
);
|
||||
}
|
||||
|
||||
/// A file that cannot be executed is not an answer: a stray
|
||||
/// `monero-wallet-rpc` text file beside the app must not be spawned.
|
||||
#[test]
|
||||
fn a_non_executable_file_is_not_a_wallet_rpc_answer() {
|
||||
let dir = tmp_dir("wallet-rpc-not-exec");
|
||||
let exe_dir = dir.join("bin");
|
||||
fs::create_dir_all(&exe_dir).unwrap();
|
||||
let plain = exe_dir.join(DEFAULT_WALLET_RPC_BIN);
|
||||
fs::write(&plain, b"# not a binary\n").unwrap();
|
||||
let mut perms = fs::metadata(&plain).unwrap().permissions();
|
||||
perms.set_mode(0o600);
|
||||
fs::set_permissions(&plain, perms).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
resolve_wallet_rpc_bin_in(Path::new(""), Some(&exe_dir), Some(exe_dir.as_os_str())),
|
||||
None,
|
||||
"a non-executable file is not a binary this app can spawn"
|
||||
);
|
||||
}
|
||||
|
||||
/// The same rule on `PATH`: only an executable in a `PATH` entry counts, and
|
||||
/// an entry that is not a directory is skipped rather than fatal.
|
||||
#[test]
|
||||
fn path_entries_are_searched_for_an_executable_only() {
|
||||
let dir = tmp_dir("wallet-rpc-path");
|
||||
let first = dir.join("first");
|
||||
let second = dir.join("second");
|
||||
fs::create_dir_all(&first).unwrap();
|
||||
fs::create_dir_all(&second).unwrap();
|
||||
let log = dir.join("argv.log");
|
||||
fs::write(first.join(DEFAULT_WALLET_RPC_BIN), b"# not executable\n").unwrap();
|
||||
let real = fake_bin(&second, DEFAULT_WALLET_RPC_BIN, &log);
|
||||
let path_var = format!(
|
||||
"{}:{}:{}",
|
||||
first.display(),
|
||||
dir.join("no-such-dir").display(),
|
||||
second.display()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_wallet_rpc_bin_in(
|
||||
Path::new(""),
|
||||
Some(&dir.join("nowhere")),
|
||||
Some(OsStr::new(&path_var))
|
||||
),
|
||||
Some(real),
|
||||
"the executable copy wins even when a non-executable one is earlier"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Test-only stand-in for the ports the fake children cannot bind.
|
||||
|
|
|
|||
Loading…
Reference in a new issue