Properly fork the process on Linux and Mac

On Windows the subsystem is set to Windows, so no console is created.
The errors are always reported through the GUI.
This commit is contained in:
Fred Sundvik 2023-10-12 00:40:43 +03:00
parent fcb6d78deb
commit 8ecc899f00
4 changed files with 51 additions and 68 deletions

70
Cargo.lock generated
View file

@ -46,9 +46,9 @@ dependencies = [
[[package]]
name = "aho-corasick"
version = "1.1.1"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea5d730647d4fadd988536d06fecce94b7b4f2a7efdae548f1cf4b63205518ab"
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
dependencies = [
"memchr",
]
@ -671,25 +671,14 @@ checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "errno"
version = "0.3.4"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "add4f07d43996f76ef320709726a556a9d4f965d9410d8d0271132d2f8293480"
checksum = "ac3e13f66a2f95e32a39eaa81f6b95d42878ca0e1db0c7543723dfe12557e860"
dependencies = [
"errno-dragonfly",
"libc",
"windows-sys 0.48.0",
]
[[package]]
name = "errno-dragonfly"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
dependencies = [
"cc",
"libc",
]
[[package]]
name = "euclid"
version = "0.22.9"
@ -801,6 +790,15 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b"
[[package]]
name = "fork"
version = "0.1.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf2ca97a59201425e7ee4d197c9c4fea282fe87a97d666a580bda889b95b8e88"
dependencies = [
"libc",
]
[[package]]
name = "form_urlencoded"
version = "1.2.0"
@ -1190,9 +1188,9 @@ checksum = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130"
[[package]]
name = "jobserver"
version = "0.1.26"
version = "0.1.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "936cfd212a0155903bcbc060e316fb6cc7cbf2e1907329391ebadc1fe0ce77c2"
checksum = "8c37f63953c4c63420ed5fd3d6d398c719489b9f872b9fa683262f8edd363c7d"
dependencies = [
"libc",
]
@ -1258,9 +1256,9 @@ dependencies = [
[[package]]
name = "linux-raw-sys"
version = "0.4.8"
version = "0.4.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3852614a3bd9ca9804678ba6be5e3b8ce76dfc902cae004e3e0c44051b6e88db"
checksum = "da2479e8c062e40bf0066ffa0bc823de0a9368974af99c9f6df941d2c231e03f"
[[package]]
name = "lock_api"
@ -1429,6 +1427,7 @@ dependencies = [
"dirs",
"euclid",
"flexi_logger",
"fork",
"futures 0.3.28",
"gl",
"glutin",
@ -1606,9 +1605,9 @@ dependencies = [
[[package]]
name = "num-traits"
version = "0.2.16"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2"
checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c"
dependencies = [
"autocfg",
]
@ -1991,9 +1990,9 @@ dependencies = [
[[package]]
name = "proc-macro2"
version = "1.0.68"
version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b1106fec09662ec6dd98ccac0f81cef56984d0b49f75c92d8cbad76e20c005c"
checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da"
dependencies = [
"unicode-ident",
]
@ -2124,9 +2123,9 @@ dependencies = [
[[package]]
name = "regex"
version = "1.9.6"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebee201405406dbf528b8b672104ae6d6d63e6d118cb10e4d51abbc7b58044ff"
checksum = "d119d7c7ca818f8a53c300863d4f87566aac09943aef5b355bb83969dae75d87"
dependencies = [
"aho-corasick",
"memchr",
@ -2136,9 +2135,9 @@ dependencies = [
[[package]]
name = "regex-automata"
version = "0.3.9"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59b23e92ee4318893fa3fe3e6fb365258efbfe6ac6ab30f090cdcbb7aa37efa9"
checksum = "465c6fc0621e4abc4187a2bda0937bfd4f722c2730b29562e19689ea796c9a4b"
dependencies = [
"aho-corasick",
"memchr",
@ -2147,9 +2146,9 @@ dependencies = [
[[package]]
name = "regex-syntax"
version = "0.7.5"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da"
checksum = "56d84fdd47036b038fc80dd333d10b6aab10d5d31f4a366e20014def75328d33"
[[package]]
name = "ring"
@ -2201,9 +2200,9 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]]
name = "rustix"
version = "0.38.17"
version = "0.38.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f25469e9ae0f3d0047ca8b93fc56843f38e6774f0914a107ff8b41be8be8e0b7"
checksum = "5a74ee2d7c2581cd139b42447d7d9389b889bdaad3a73f1ebb16f2a3237bb19c"
dependencies = [
"bitflags 2.4.0",
"errno",
@ -2698,9 +2697,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]]
name = "tokio"
version = "1.32.0"
version = "1.33.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17ed6077ed6cd6c74735e21f37eb16dc3935f96878b1fe961074089cc80893f9"
checksum = "4f38200e3ef7995e5ef13baec2f432a6da0aa9ac495b2c0e8f3b7eec2c92d653"
dependencies = [
"backtrace",
"bytes 1.5.0",
@ -3147,12 +3146,11 @@ dependencies = [
[[package]]
name = "web-time"
version = "0.2.0"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19353897b48e2c4d849a2d73cb0aeb16dc2be4e00c565abfc11eb65a806e47de"
checksum = "8208e3fdbc243c8fd30805721869242a7f6de3e2e9f3b057652ab36e52ae1e87"
dependencies = [
"js-sys",
"once_cell",
"wasm-bindgen",
]

View file

@ -74,9 +74,11 @@ winapi = { version = "0.3.9", features = ["winuser", "wincon", "winerror", "dwma
[target.'cfg(target_os = "macos")'.dependencies]
cocoa = "0.24.0"
fork = "0.1.22"
objc = "0.2.7"
[target.'cfg(target_os = "linux")'.dependencies]
fork = "0.1.22"
nix = { version = "0.26.2", features = ["poll"] }
wayland-client = "0.30.2"
wayland-sys = "0.30.1"

View file

@ -1,4 +1,4 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
#![cfg_attr(not(test), windows_subsystem = "windows")]
// Test naming occasionally uses camelCase with underscores to separate sections of
// the test name.
#![cfg_attr(test, allow(non_snake_case))]
@ -83,7 +83,6 @@ fn main() -> NeovideExitCode {
#[cfg(target_os = "windows")]
{
windows_fix_dpi();
windows_attach_to_console();
}
let event_loop = create_event_loop();
@ -167,6 +166,7 @@ fn setup() -> Result<(WindowSize, NeovimRuntime)> {
//Will exit if -h or -v
cmd_line::handle_command_line_arguments(args().collect())?;
#[cfg(not(target_os = "windows"))]
maybe_disown();
startup_profiler();
@ -210,8 +210,10 @@ pub fn init_logger() {
logger.start().expect("Could not start logger");
}
#[cfg(not(target_os = "windows"))]
fn maybe_disown() {
use std::process;
use fork::{daemon, Fork};
use std::process::exit;
let settings = SETTINGS.get::<CmdLineSettings>();
@ -219,21 +221,16 @@ fn maybe_disown() {
return;
}
#[cfg(target_os = "windows")]
windows_detach_from_console();
if let Ok(current_exe) = env::current_exe() {
assert!(process::Command::new(current_exe)
.stdin(process::Stdio::null())
.stdout(process::Stdio::null())
.stderr(process::Stdio::null())
.arg("--no-fork")
.args(env::args().skip(1))
.spawn()
.is_ok());
process::exit(0);
} else {
eprintln!("error in disowning process, cannot obtain the path for the current executable, continuing without disowning...");
let nochdir = true;
let noclose = false;
match daemon(nochdir, noclose) {
Ok(Fork::Child) => {}
Ok(Fork::Parent(_)) => {
exit(0);
}
Err(_) => {
eprintln!("error in disowning process, cannot obtain the path for the current executable, continuing without disowning...");
}
}
}

View file

@ -10,7 +10,6 @@ use winapi::{
},
um::{
libloaderapi::GetModuleFileNameA,
wincon::{AttachConsole, FreeConsole, ATTACH_PARENT_PROCESS},
winnt::{KEY_WRITE, REG_OPTION_NON_VOLATILE, REG_SZ},
winreg::{RegCloseKey, RegCreateKeyExA, RegDeleteTreeA, RegSetValueExA, HKEY_CURRENT_USER},
winuser::SetProcessDpiAwarenessContext,
@ -224,16 +223,3 @@ pub fn windows_fix_dpi() {
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
}
}
pub fn windows_attach_to_console() {
// Attach to parent console tip found here: https://github.com/rust-lang/rust/issues/67159#issuecomment-987882771
unsafe {
AttachConsole(ATTACH_PARENT_PROCESS);
}
}
pub fn windows_detach_from_console() {
unsafe {
FreeConsole();
}
}