2019-01-07 11:33:07 +01:00
|
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
|
// This file is part of Parity Ethereum.
|
2016-02-05 13:40:41 +01:00
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2016-02-05 13:40:41 +01:00
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2016-02-05 13:40:41 +01:00
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2019-01-07 11:33:07 +01:00
|
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-02-05 13:40:41 +01:00
|
|
|
|
|
2016-01-27 17:18:38 +01:00
|
|
|
|
//! Ethcore client application.
|
|
|
|
|
|
|
|
|
|
#![warn(missing_docs)]
|
2016-04-21 13:12:43 +02:00
|
|
|
|
|
2017-02-13 16:38:47 +01:00
|
|
|
|
extern crate ctrlc;
|
2017-12-24 09:34:43 +01:00
|
|
|
|
extern crate dir;
|
2016-02-05 13:49:36 +01:00
|
|
|
|
extern crate fdlimit;
|
2017-07-06 11:36:15 +02:00
|
|
|
|
#[macro_use]
|
2018-05-09 08:47:21 +02:00
|
|
|
|
extern crate log;
|
2017-07-03 07:31:29 +02:00
|
|
|
|
extern crate panic_hook;
|
2018-07-11 13:35:10 +02:00
|
|
|
|
extern crate parity_ethereum;
|
2018-05-09 08:47:21 +02:00
|
|
|
|
extern crate parking_lot;
|
2019-02-01 19:31:02 +01:00
|
|
|
|
extern crate parity_daemonize;
|
|
|
|
|
extern crate ansi_term;
|
2016-01-20 04:19:38 +01:00
|
|
|
|
|
2017-03-23 13:25:31 +01:00
|
|
|
|
#[cfg(windows)] extern crate winapi;
|
2019-02-01 19:31:02 +01:00
|
|
|
|
extern crate ethcore_logger;
|
2017-03-23 13:25:31 +01:00
|
|
|
|
|
2018-07-10 12:17:53 +02:00
|
|
|
|
use std::ffi::OsString;
|
2017-03-23 13:25:31 +01:00
|
|
|
|
use std::fs::{remove_file, metadata, File, create_dir_all};
|
2018-07-10 12:17:53 +02:00
|
|
|
|
use std::io::{self as stdio, Read, Write};
|
2016-12-10 23:58:39 +01:00
|
|
|
|
use std::path::PathBuf;
|
2018-05-09 08:47:21 +02:00
|
|
|
|
use std::sync::Arc;
|
2018-07-10 12:17:53 +02:00
|
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
|
use std::{process, env};
|
2019-02-18 13:38:46 +01:00
|
|
|
|
|
2019-02-01 19:31:02 +01:00
|
|
|
|
use ansi_term::Colour;
|
2018-05-09 08:47:21 +02:00
|
|
|
|
use ctrlc::CtrlC;
|
2016-12-15 19:53:13 +01:00
|
|
|
|
use dir::default_hypervisor_path;
|
2018-05-09 08:47:21 +02:00
|
|
|
|
use fdlimit::raise_fd_limit;
|
2019-02-18 13:38:46 +01:00
|
|
|
|
use ethcore_logger::setup_log;
|
2018-07-11 13:35:10 +02:00
|
|
|
|
use parity_ethereum::{start, ExecutionAction};
|
2019-02-18 13:38:46 +01:00
|
|
|
|
use parity_daemonize::AsHandle;
|
2018-05-09 08:47:21 +02:00
|
|
|
|
use parking_lot::{Condvar, Mutex};
|
2016-12-10 23:58:39 +01:00
|
|
|
|
|
2018-07-10 12:17:53 +02:00
|
|
|
|
const PLEASE_RESTART_EXIT_CODE: i32 = 69;
|
|
|
|
|
const PARITY_EXECUTABLE_NAME: &str = "parity";
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
enum Error {
|
|
|
|
|
BinaryNotFound,
|
|
|
|
|
ExitCode(i32),
|
|
|
|
|
Restart,
|
|
|
|
|
Unknown
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn update_path(name: &str) -> PathBuf {
|
|
|
|
|
let mut dest = default_hypervisor_path();
|
2016-12-11 03:33:10 +01:00
|
|
|
|
dest.push(name);
|
2016-12-10 23:58:39 +01:00
|
|
|
|
dest
|
2016-09-06 15:31:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-10 12:17:53 +02:00
|
|
|
|
fn latest_exe_path() -> Result<PathBuf, Error> {
|
2018-07-11 13:35:10 +02:00
|
|
|
|
File::open(update_path("latest")).and_then(|mut f| {
|
|
|
|
|
let mut exe_path = String::new();
|
|
|
|
|
trace!(target: "updater", "latest binary path: {:?}", f);
|
2018-07-10 12:17:53 +02:00
|
|
|
|
f.read_to_string(&mut exe_path).map(|_| update_path(&exe_path))
|
|
|
|
|
})
|
|
|
|
|
.or(Err(Error::BinaryNotFound))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn latest_binary_is_newer(current_binary: &Option<PathBuf>, latest_binary: &Option<PathBuf>) -> bool {
|
|
|
|
|
match (
|
|
|
|
|
current_binary
|
|
|
|
|
.as_ref()
|
|
|
|
|
.and_then(|p| metadata(p.as_path()).ok())
|
|
|
|
|
.and_then(|m| m.modified().ok()),
|
|
|
|
|
latest_binary
|
|
|
|
|
.as_ref()
|
|
|
|
|
.and_then(|p| metadata(p.as_path()).ok())
|
|
|
|
|
.and_then(|m| m.modified().ok())
|
|
|
|
|
) {
|
|
|
|
|
(Some(latest_exe_time), Some(this_exe_time)) if latest_exe_time > this_exe_time => true,
|
|
|
|
|
_ => false,
|
|
|
|
|
}
|
2016-12-22 18:26:39 +01:00
|
|
|
|
}
|
2016-12-11 03:33:10 +01:00
|
|
|
|
|
2018-07-10 12:17:53 +02:00
|
|
|
|
fn set_spec_name_override(spec_name: &str) {
|
2017-03-23 13:25:31 +01:00
|
|
|
|
if let Err(e) = create_dir_all(default_hypervisor_path())
|
2018-07-10 12:17:53 +02:00
|
|
|
|
.and_then(|_| File::create(update_path("spec_name_override"))
|
2017-03-23 13:25:31 +01:00
|
|
|
|
.and_then(|mut f| f.write_all(spec_name.as_bytes())))
|
2017-03-13 12:10:53 +01:00
|
|
|
|
{
|
2018-07-10 12:17:53 +02:00
|
|
|
|
warn!("Couldn't override chain spec: {} at {:?}", e, update_path("spec_name_override"));
|
2017-03-13 12:10:53 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn take_spec_name_override() -> Option<String> {
|
2018-07-10 12:17:53 +02:00
|
|
|
|
let p = update_path("spec_name_override");
|
|
|
|
|
let r = File::open(p.clone())
|
|
|
|
|
.ok()
|
2018-07-11 13:35:10 +02:00
|
|
|
|
.and_then(|mut f| {
|
|
|
|
|
let mut spec_name = String::new();
|
|
|
|
|
f.read_to_string(&mut spec_name).ok().map(|_| spec_name)
|
2018-07-10 12:17:53 +02:00
|
|
|
|
});
|
2017-03-13 12:10:53 +01:00
|
|
|
|
let _ = remove_file(p);
|
|
|
|
|
r
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-17 23:34:46 +01:00
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
fn global_cleanup() {
|
2018-05-15 07:46:37 +02:00
|
|
|
|
// We need to cleanup all sockets before spawning another Parity process. This makes sure everything is cleaned up.
|
2018-04-21 12:54:48 +02:00
|
|
|
|
// The loop is required because of internal reference counter for winsock dll. We don't know how many crates we use do
|
2017-01-17 23:34:46 +01:00
|
|
|
|
// initialize it. There's at least 2 now.
|
|
|
|
|
for _ in 0.. 10 {
|
2018-04-11 15:22:48 +02:00
|
|
|
|
unsafe { ::winapi::um::winsock2::WSACleanup(); }
|
2017-03-23 13:25:31 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
|
fn global_init() {}
|
|
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
fn global_init() {
|
|
|
|
|
// When restarting in the same process this reinits windows sockets.
|
|
|
|
|
unsafe {
|
|
|
|
|
const WS_VERSION: u16 = 0x202;
|
2018-04-11 15:22:48 +02:00
|
|
|
|
let mut wsdata: ::winapi::um::winsock2::WSADATA = ::std::mem::zeroed();
|
|
|
|
|
::winapi::um::winsock2::WSAStartup(WS_VERSION, &mut wsdata);
|
2017-01-17 23:34:46 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
|
fn global_cleanup() {}
|
|
|
|
|
|
2018-07-10 12:17:53 +02:00
|
|
|
|
// Starts parity binary installed via `parity-updater` and returns the code it exits with.
|
|
|
|
|
fn run_parity() -> Result<(), Error> {
|
2017-03-23 13:25:31 +01:00
|
|
|
|
global_init();
|
2018-07-10 12:17:53 +02:00
|
|
|
|
|
2016-12-11 04:05:02 +01:00
|
|
|
|
let prefix = vec![OsString::from("--can-restart"), OsString::from("--force-direct")];
|
2018-07-11 13:35:10 +02:00
|
|
|
|
|
2018-07-10 12:17:53 +02:00
|
|
|
|
let res: Result<(), Error> = latest_exe_path()
|
|
|
|
|
.and_then(|exe| process::Command::new(exe)
|
2017-01-17 23:34:46 +01:00
|
|
|
|
.args(&(env::args_os().skip(1).chain(prefix.into_iter()).collect::<Vec<_>>()))
|
2016-12-11 04:05:02 +01:00
|
|
|
|
.status()
|
|
|
|
|
.ok()
|
2018-07-10 12:17:53 +02:00
|
|
|
|
.map_or(Err(Error::Unknown), |es| {
|
|
|
|
|
match es.code() {
|
|
|
|
|
// Process success
|
|
|
|
|
Some(0) => Ok(()),
|
|
|
|
|
// Please restart
|
|
|
|
|
Some(PLEASE_RESTART_EXIT_CODE) => Err(Error::Restart),
|
|
|
|
|
// Process error code `c`
|
|
|
|
|
Some(c) => Err(Error::ExitCode(c)),
|
|
|
|
|
// Unknown error, couldn't determine error code
|
|
|
|
|
_ => Err(Error::Unknown),
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-07-11 13:35:10 +02:00
|
|
|
|
);
|
2018-07-10 12:17:53 +02:00
|
|
|
|
|
2017-03-23 13:25:31 +01:00
|
|
|
|
global_cleanup();
|
|
|
|
|
res
|
2016-12-10 23:58:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-02 11:53:50 +02:00
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
/// Status used to exit or restart the program.
|
|
|
|
|
struct ExitStatus {
|
|
|
|
|
/// Whether the program panicked.
|
|
|
|
|
panicking: bool,
|
|
|
|
|
/// Whether the program should exit.
|
|
|
|
|
should_exit: bool,
|
|
|
|
|
/// Whether the program should restart.
|
|
|
|
|
should_restart: bool,
|
|
|
|
|
/// If a restart happens, whether a new chain spec should be used.
|
|
|
|
|
spec_name_override: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-10 12:17:53 +02:00
|
|
|
|
// Run `locally installed version` of parity (i.e, not installed via `parity-updater`)
|
2016-12-22 18:26:39 +01:00
|
|
|
|
// Returns the exit error code.
|
2018-04-13 14:21:15 +02:00
|
|
|
|
fn main_direct(force_can_restart: bool) -> i32 {
|
2017-03-23 13:25:31 +01:00
|
|
|
|
global_init();
|
2018-05-09 08:47:21 +02:00
|
|
|
|
|
|
|
|
|
let mut conf = {
|
|
|
|
|
let args = std::env::args().collect::<Vec<_>>();
|
2018-07-11 13:35:10 +02:00
|
|
|
|
parity_ethereum::Configuration::parse_cli(&args).unwrap_or_else(|e| e.exit())
|
2018-05-09 08:47:21 +02:00
|
|
|
|
};
|
|
|
|
|
|
2019-02-01 19:31:02 +01:00
|
|
|
|
let logger = setup_log(&conf.logger_config()).expect("Logger is initialized only once; qed");
|
|
|
|
|
|
2018-05-09 08:47:21 +02:00
|
|
|
|
if let Some(spec_override) = take_spec_name_override() {
|
|
|
|
|
conf.args.flag_testnet = false;
|
|
|
|
|
conf.args.arg_chain = spec_override;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-18 13:38:46 +01:00
|
|
|
|
// FIXME: `pid_file` shouldn't need to cloned here
|
|
|
|
|
// see: `https://github.com/paritytech/parity-daemonize/pull/13` for more info
|
|
|
|
|
let handle = if let Some(pid) = conf.args.arg_daemon_pid_file.clone() {
|
2019-02-01 19:31:02 +01:00
|
|
|
|
info!("{}", Colour::Blue.paint("starting in daemon mode").to_string());
|
|
|
|
|
let _ = std::io::stdout().flush();
|
|
|
|
|
|
|
|
|
|
match parity_daemonize::daemonize(pid) {
|
|
|
|
|
Ok(h) => Some(h),
|
|
|
|
|
Err(e) => {
|
|
|
|
|
error!(
|
|
|
|
|
"{}",
|
|
|
|
|
Colour::Red.paint(format!("{}", e))
|
|
|
|
|
);
|
|
|
|
|
// flush before returning
|
|
|
|
|
let _ = std::io::stderr().flush();
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
|
2018-05-09 08:47:21 +02:00
|
|
|
|
let can_restart = force_can_restart || conf.args.flag_can_restart;
|
|
|
|
|
|
|
|
|
|
// increase max number of open files
|
|
|
|
|
raise_fd_limit();
|
|
|
|
|
|
2018-07-02 11:53:50 +02:00
|
|
|
|
let exit = Arc::new((Mutex::new(ExitStatus {
|
|
|
|
|
panicking: false,
|
|
|
|
|
should_exit: false,
|
|
|
|
|
should_restart: false,
|
|
|
|
|
spec_name_override: None
|
|
|
|
|
}), Condvar::new()));
|
|
|
|
|
|
|
|
|
|
// Double panic can happen. So when we lock `ExitStatus` after the main thread is notified, it cannot be locked
|
|
|
|
|
// again.
|
|
|
|
|
let exiting = Arc::new(AtomicBool::new(false));
|
2018-05-09 08:47:21 +02:00
|
|
|
|
|
|
|
|
|
let exec = if can_restart {
|
2018-07-02 11:53:50 +02:00
|
|
|
|
start(
|
|
|
|
|
conf,
|
2019-02-01 19:31:02 +01:00
|
|
|
|
logger,
|
2018-07-02 11:53:50 +02:00
|
|
|
|
{
|
|
|
|
|
let e = exit.clone();
|
|
|
|
|
let exiting = exiting.clone();
|
|
|
|
|
move |new_chain: String| {
|
|
|
|
|
if !exiting.swap(true, Ordering::SeqCst) {
|
|
|
|
|
*e.0.lock() = ExitStatus {
|
|
|
|
|
panicking: false,
|
|
|
|
|
should_exit: true,
|
|
|
|
|
should_restart: true,
|
|
|
|
|
spec_name_override: Some(new_chain),
|
|
|
|
|
};
|
|
|
|
|
e.1.notify_all();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
let e = exit.clone();
|
|
|
|
|
let exiting = exiting.clone();
|
|
|
|
|
move || {
|
|
|
|
|
if !exiting.swap(true, Ordering::SeqCst) {
|
|
|
|
|
*e.0.lock() = ExitStatus {
|
|
|
|
|
panicking: false,
|
|
|
|
|
should_exit: true,
|
|
|
|
|
should_restart: true,
|
|
|
|
|
spec_name_override: None,
|
|
|
|
|
};
|
|
|
|
|
e.1.notify_all();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
2016-09-06 15:31:13 +02:00
|
|
|
|
} else {
|
2018-05-09 08:47:21 +02:00
|
|
|
|
trace!(target: "mode", "Not hypervised: not setting exit handlers.");
|
2019-02-01 19:31:02 +01:00
|
|
|
|
start(conf, logger, move |_| {}, move || {})
|
2018-05-09 08:47:21 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let res = match exec {
|
|
|
|
|
Ok(result) => match result {
|
|
|
|
|
ExecutionAction::Instant(Some(s)) => { println!("{}", s); 0 },
|
|
|
|
|
ExecutionAction::Instant(None) => 0,
|
|
|
|
|
ExecutionAction::Running(client) => {
|
2018-07-02 11:53:50 +02:00
|
|
|
|
panic_hook::set_with({
|
|
|
|
|
let e = exit.clone();
|
|
|
|
|
let exiting = exiting.clone();
|
2018-08-10 11:06:30 +02:00
|
|
|
|
move |panic_msg| {
|
|
|
|
|
let _ = stdio::stderr().write_all(panic_msg.as_bytes());
|
2018-07-02 11:53:50 +02:00
|
|
|
|
if !exiting.swap(true, Ordering::SeqCst) {
|
|
|
|
|
*e.0.lock() = ExitStatus {
|
|
|
|
|
panicking: true,
|
|
|
|
|
should_exit: true,
|
|
|
|
|
should_restart: false,
|
|
|
|
|
spec_name_override: None,
|
|
|
|
|
};
|
|
|
|
|
e.1.notify_all();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-05-09 08:47:21 +02:00
|
|
|
|
CtrlC::set_handler({
|
|
|
|
|
let e = exit.clone();
|
2018-07-02 11:53:50 +02:00
|
|
|
|
let exiting = exiting.clone();
|
|
|
|
|
move || {
|
|
|
|
|
if !exiting.swap(true, Ordering::SeqCst) {
|
|
|
|
|
*e.0.lock() = ExitStatus {
|
|
|
|
|
panicking: false,
|
|
|
|
|
should_exit: true,
|
|
|
|
|
should_restart: false,
|
|
|
|
|
spec_name_override: None,
|
|
|
|
|
};
|
|
|
|
|
e.1.notify_all();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-09 08:47:21 +02:00
|
|
|
|
});
|
|
|
|
|
|
2019-02-01 19:31:02 +01:00
|
|
|
|
// so the client has started successfully
|
|
|
|
|
// if this is a daemon, detach from the parent process
|
|
|
|
|
if let Some(mut handle) = handle {
|
|
|
|
|
handle.detach()
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-09 08:47:21 +02:00
|
|
|
|
// Wait for signal
|
|
|
|
|
let mut lock = exit.0.lock();
|
2018-07-02 11:53:50 +02:00
|
|
|
|
if !lock.should_exit {
|
|
|
|
|
let _ = exit.1.wait(&mut lock);
|
|
|
|
|
}
|
2018-05-09 08:47:21 +02:00
|
|
|
|
|
|
|
|
|
client.shutdown();
|
|
|
|
|
|
2018-07-02 11:53:50 +02:00
|
|
|
|
if lock.should_restart {
|
|
|
|
|
if let Some(ref spec_name) = lock.spec_name_override {
|
2018-07-10 12:17:53 +02:00
|
|
|
|
set_spec_name_override(&spec_name.clone());
|
2018-07-02 11:53:50 +02:00
|
|
|
|
}
|
|
|
|
|
PLEASE_RESTART_EXIT_CODE
|
|
|
|
|
} else {
|
|
|
|
|
if lock.panicking {
|
|
|
|
|
1
|
|
|
|
|
} else {
|
|
|
|
|
0
|
|
|
|
|
}
|
2018-04-13 14:21:15 +02:00
|
|
|
|
}
|
2016-12-10 23:58:39 +01:00
|
|
|
|
},
|
2018-05-09 08:47:21 +02:00
|
|
|
|
},
|
|
|
|
|
Err(err) => {
|
2019-02-01 19:31:02 +01:00
|
|
|
|
// error occured during start up
|
|
|
|
|
// if this is a daemon, detach from the parent process
|
|
|
|
|
if let Some(mut handle) = handle {
|
|
|
|
|
handle.detach_with_msg(format!("{}", Colour::Red.paint(&err)))
|
|
|
|
|
}
|
2018-05-09 08:47:21 +02:00
|
|
|
|
writeln!(&mut stdio::stderr(), "{}", err).expect("StdErr available; qed");
|
|
|
|
|
1
|
|
|
|
|
},
|
2017-03-23 13:25:31 +01:00
|
|
|
|
};
|
2018-05-09 08:47:21 +02:00
|
|
|
|
|
2017-03-23 13:25:31 +01:00
|
|
|
|
global_cleanup();
|
|
|
|
|
res
|
2016-09-06 15:31:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-10 23:58:39 +01:00
|
|
|
|
fn println_trace_main(s: String) {
|
|
|
|
|
if env::var("RUST_LOG").ok().and_then(|s| s.find("main=trace")).is_some() {
|
|
|
|
|
println!("{}", s);
|
2016-07-26 00:21:08 +02:00
|
|
|
|
}
|
2016-12-10 23:58:39 +01:00
|
|
|
|
}
|
2016-07-26 00:21:08 +02:00
|
|
|
|
|
2016-12-10 23:58:39 +01:00
|
|
|
|
macro_rules! trace_main {
|
|
|
|
|
($arg:expr) => (println_trace_main($arg.into()));
|
|
|
|
|
($($arg:tt)*) => (println_trace_main(format!("{}", format_args!($($arg)*))));
|
|
|
|
|
}
|
2016-08-24 18:35:38 +02:00
|
|
|
|
|
2016-12-10 23:58:39 +01:00
|
|
|
|
fn main() {
|
2018-07-02 11:53:50 +02:00
|
|
|
|
panic_hook::set_abort();
|
2016-12-10 23:58:39 +01:00
|
|
|
|
|
2018-07-10 12:17:53 +02:00
|
|
|
|
// the user has specified to run its originally installed binary (not via `parity-updater`)
|
2016-12-10 23:58:39 +01:00
|
|
|
|
let force_direct = std::env::args().any(|arg| arg == "--force-direct");
|
2018-07-11 13:35:10 +02:00
|
|
|
|
|
2018-07-10 12:17:53 +02:00
|
|
|
|
// absolute path to the current `binary`
|
|
|
|
|
let exe_path = std::env::current_exe().ok();
|
2018-07-11 13:35:10 +02:00
|
|
|
|
|
2018-07-10 12:17:53 +02:00
|
|
|
|
// the binary is named `target/xx/yy`
|
|
|
|
|
let development = exe_path
|
|
|
|
|
.as_ref()
|
|
|
|
|
.and_then(|p| {
|
|
|
|
|
p.parent()
|
|
|
|
|
.and_then(|p| p.parent())
|
|
|
|
|
.and_then(|p| p.file_name())
|
|
|
|
|
.map(|n| n == "target")
|
|
|
|
|
})
|
|
|
|
|
.unwrap_or(false);
|
2018-07-11 13:35:10 +02:00
|
|
|
|
|
2018-07-10 12:17:53 +02:00
|
|
|
|
// the binary is named `parity`
|
|
|
|
|
let same_name = exe_path
|
|
|
|
|
.as_ref()
|
2018-07-11 13:35:10 +02:00
|
|
|
|
.map_or(false, |p| {
|
|
|
|
|
p.file_stem().map_or(false, |n| n == PARITY_EXECUTABLE_NAME)
|
2018-07-10 12:17:53 +02:00
|
|
|
|
});
|
|
|
|
|
|
2018-07-11 13:35:10 +02:00
|
|
|
|
trace_main!("Starting up {} (force-direct: {}, development: {}, same-name: {})",
|
|
|
|
|
std::env::current_exe().ok().map_or_else(|| "<unknown>".into(), |x| format!("{}", x.display())),
|
|
|
|
|
force_direct,
|
|
|
|
|
development,
|
2018-07-10 12:17:53 +02:00
|
|
|
|
same_name);
|
|
|
|
|
|
2017-01-17 23:34:46 +01:00
|
|
|
|
if !force_direct && !development && same_name {
|
2018-07-11 13:35:10 +02:00
|
|
|
|
// Try to run the latest installed version of `parity`,
|
2018-07-10 12:17:53 +02:00
|
|
|
|
// Upon failure it falls back to the locally installed version of `parity`
|
2016-12-22 18:26:39 +01:00
|
|
|
|
// Everything run inside a loop, so we'll be able to restart from the child into a new version seamlessly.
|
2016-12-10 23:58:39 +01:00
|
|
|
|
loop {
|
2018-07-10 12:17:53 +02:00
|
|
|
|
// `Path` to the latest downloaded binary
|
|
|
|
|
let latest_exe = latest_exe_path().ok();
|
2018-07-11 13:35:10 +02:00
|
|
|
|
|
2018-07-10 12:17:53 +02:00
|
|
|
|
// `Latest´ binary exist
|
2017-01-17 23:34:46 +01:00
|
|
|
|
let have_update = latest_exe.as_ref().map_or(false, |p| p.exists());
|
2018-07-11 13:35:10 +02:00
|
|
|
|
|
2018-07-10 12:17:53 +02:00
|
|
|
|
// Canonicalized path to the current binary is not the same as to latest binary
|
|
|
|
|
let canonicalized_path_not_same = exe_path
|
|
|
|
|
.as_ref()
|
|
|
|
|
.map_or(false, |exe| latest_exe.as_ref()
|
|
|
|
|
.map_or(false, |lexe| exe.canonicalize().ok() != lexe.canonicalize().ok()));
|
|
|
|
|
|
|
|
|
|
// Downloaded `binary` is newer
|
|
|
|
|
let update_is_newer = latest_binary_is_newer(&latest_exe, &exe_path);
|
|
|
|
|
trace_main!("Starting... (have-update: {}, non-updated-current: {}, update-is-newer: {})", have_update, canonicalized_path_not_same, update_is_newer);
|
|
|
|
|
|
|
|
|
|
let exit_code = if have_update && canonicalized_path_not_same && update_is_newer {
|
2018-07-11 13:35:10 +02:00
|
|
|
|
trace_main!("Attempting to run latest update ({})...",
|
2018-07-10 12:17:53 +02:00
|
|
|
|
latest_exe.as_ref().expect("guarded by have_update; latest_exe must exist for have_update; qed").display());
|
|
|
|
|
match run_parity() {
|
|
|
|
|
Ok(_) => 0,
|
|
|
|
|
// Restart parity
|
|
|
|
|
Err(Error::Restart) => PLEASE_RESTART_EXIT_CODE,
|
|
|
|
|
// Fall back to local version
|
|
|
|
|
Err(e) => {
|
2018-07-11 13:35:10 +02:00
|
|
|
|
error!(target: "updater", "Updated binary could not be executed error: {:?}. Falling back to local version", e);
|
2018-07-10 12:17:53 +02:00
|
|
|
|
main_direct(true)
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-17 23:34:46 +01:00
|
|
|
|
} else {
|
|
|
|
|
trace_main!("No latest update. Attempting to direct...");
|
|
|
|
|
main_direct(true)
|
|
|
|
|
};
|
2018-07-10 12:17:53 +02:00
|
|
|
|
trace_main!("Latest binary exited with exit code: {}", exit_code);
|
2016-12-10 23:58:39 +01:00
|
|
|
|
if exit_code != PLEASE_RESTART_EXIT_CODE {
|
|
|
|
|
trace_main!("Quitting...");
|
|
|
|
|
process::exit(exit_code);
|
|
|
|
|
}
|
2018-07-10 12:17:53 +02:00
|
|
|
|
trace!(target: "updater", "Re-running updater loop");
|
2016-05-24 20:29:19 +02:00
|
|
|
|
}
|
2016-12-10 23:58:39 +01:00
|
|
|
|
} else {
|
|
|
|
|
trace_main!("Running direct");
|
|
|
|
|
// Otherwise, we're presumably running the version we want. Just run and fall-through.
|
2018-04-13 14:21:15 +02:00
|
|
|
|
process::exit(main_direct(false));
|
2016-05-24 20:29:19 +02:00
|
|
|
|
}
|
2016-06-07 17:21:19 +02:00
|
|
|
|
}
|