From f5ea47a7b2533c7c534ff6b0c921e6940325f427 Mon Sep 17 00:00:00 2001 From: Arkadiy Paronyan Date: Thu, 23 Mar 2017 13:25:31 +0100 Subject: [PATCH] Various installer and tray apps fixes (#4970) * Mac tray app fixes * Windows restarting fixed --- Cargo.lock | 1 + Cargo.toml | 1 + mac/Parity/AppDelegate.swift | 6 +---- parity/main.rs | 43 +++++++++++++++++++++++++++--------- 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 99ebbe7e3..974206727 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -52,6 +52,7 @@ dependencies = [ "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index f0978f420..8420c5459 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ serde = "0.9" serde_json = "0.9" app_dirs = "1.1.1" fdlimit = "0.1" +ws2_32-sys = "0.2" hyper = { default-features = false, git = "https://github.com/paritytech/hyper" } ctrlc = { git = "https://github.com/paritytech/rust-ctrlc.git" } jsonrpc-core = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.7" } diff --git a/mac/Parity/AppDelegate.swift b/mac/Parity/AppDelegate.swift index 3bf3bdd31..c017e79bd 100644 --- a/mac/Parity/AppDelegate.swift +++ b/mac/Parity/AppDelegate.swift @@ -26,7 +26,6 @@ class AppDelegate: NSObject, NSApplicationDelegate { let statusItem = NSStatusBar.system().statusItem(withLength: NSVariableStatusItemLength) var parityPid: Int32? = nil var commandLine: [String] = [] - let defaultConfig = "[network]\nwarp = true" let defaultDefaults = "{\"fat_db\":false,\"mode\":\"passive\",\"mode.alarm\":3600,\"mode.timeout\":300,\"pruning\":\"fast\",\"tracing\":false}" func menuAppPath() -> String { @@ -51,7 +50,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { func killParity() { if let pid = self.parityPid { - kill(pid, SIGINT) + kill(pid, SIGKILL) } } @@ -81,9 +80,6 @@ class AppDelegate: NSObject, NSApplicationDelegate { } let configFile = basePath?.appendingPathComponent("config.toml") - if !FileManager.default.fileExists(atPath: configFile!.path) { - try defaultConfig.write(to: configFile!, atomically: false, encoding: String.Encoding.utf8) - } } catch {} } diff --git a/parity/main.rs b/parity/main.rs index 3e499d483..2044b3ee0 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -75,6 +75,9 @@ extern crate ethcore_secretstore; #[cfg(feature = "dapps")] extern crate ethcore_dapps; +#[cfg(windows)] extern crate ws2_32; +#[cfg(windows)] extern crate winapi; + macro_rules! dependency { ($dep_ty:ident, $url:expr) => { { @@ -123,7 +126,7 @@ mod stratum; use std::{process, env}; use std::collections::HashMap; use std::io::{self as stdio, BufReader, Read, Write}; -use std::fs::{remove_file, metadata, File}; +use std::fs::{remove_file, metadata, File, create_dir_all}; use std::path::PathBuf; use util::sha3::sha3; use cli::Args; @@ -210,10 +213,11 @@ fn latest_exe_path() -> Option { } fn set_spec_name_override(spec_name: String) { - if let Err(e) = File::create(updates_path("spec_name_overide")) - .and_then(|mut f| f.write_all(spec_name.as_bytes())) + if let Err(e) = create_dir_all(default_hypervisor_path()) + .and_then(|_| File::create(updates_path("spec_name_overide")) + .and_then(|mut f| f.write_all(spec_name.as_bytes()))) { - warn!("Couldn't override chain spec: {}", e); + warn!("Couldn't override chain spec: {} at {:?}", e, updates_path("spec_name_overide")); } } @@ -227,12 +231,24 @@ fn take_spec_name_override() -> Option { #[cfg(windows)] fn global_cleanup() { - extern "system" { pub fn WSACleanup() -> i32; } // We need to cleanup all sockets before spawning another Parity process. This makes shure everything is cleaned up. // The loop is required because of internal refernce counter for winsock dll. We don't know how many crates we use do // initialize it. There's at least 2 now. for _ in 0.. 10 { - unsafe { WSACleanup(); } + unsafe { ::ws2_32::WSACleanup(); } + } +} + +#[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; + let mut wsdata: ::winapi::winsock2::WSADATA = ::std::mem::zeroed(); + ::ws2_32::WSAStartup(WS_VERSION, &mut wsdata); } } @@ -241,15 +257,17 @@ fn global_cleanup() {} // Starts ~/.parity-updates/parity and returns the code it exits with. fn run_parity() -> Option { - global_cleanup(); + global_init(); use ::std::ffi::OsString; let prefix = vec![OsString::from("--can-restart"), OsString::from("--force-direct")]; - latest_exe_path().and_then(|exe| process::Command::new(exe) + let res = latest_exe_path().and_then(|exe| process::Command::new(exe) .args(&(env::args_os().skip(1).chain(prefix.into_iter()).collect::>())) .status() .map(|es| es.code().unwrap_or(128)) .ok() - ) + ); + global_cleanup(); + res } const PLEASE_RESTART_EXIT_CODE: i32 = 69; @@ -257,10 +275,11 @@ const PLEASE_RESTART_EXIT_CODE: i32 = 69; // Run our version of parity. // Returns the exit error code. fn main_direct(can_restart: bool) -> i32 { + global_init(); let mut alt_mains = HashMap::new(); sync_main(&mut alt_mains); stratum_main(&mut alt_mains); - if let Some(f) = std::env::args().nth(1).and_then(|arg| alt_mains.get(&arg.to_string())) { + let res = if let Some(f) = std::env::args().nth(1).and_then(|arg| alt_mains.get(&arg.to_string())) { f(); 0 } else { @@ -280,7 +299,9 @@ fn main_direct(can_restart: bool) -> i32 { 1 }, } - } + }; + global_cleanup(); + res } fn println_trace_main(s: String) {