Adapt new process API to windows.

This commit is contained in:
Federico Terzi 2020-05-26 20:17:47 +02:00
parent 921c39ba4e
commit 7677615bae
2 changed files with 13 additions and 20 deletions

View File

@ -384,7 +384,8 @@ fn daemon_main(config_set: ConfigSet) {
let mut child = crate::process::spawn_process( let mut child = crate::process::spawn_process(
&espanso_path.to_string_lossy().to_string(), &espanso_path.to_string_lossy().to_string(),
&vec!["worker".to_owned()], &vec!["worker".to_owned()],
).expect("unable to create worker process"); )
.expect("unable to create worker process");
// Create a monitor thread that will exit with the same non-zero code if // Create a monitor thread that will exit with the same non-zero code if
// the worker thread exits // the worker thread exits
@ -395,7 +396,10 @@ fn daemon_main(config_set: ConfigSet) {
if let Ok(status) = result { if let Ok(status) = result {
if let Some(code) = status.code() { if let Some(code) = status.code() {
if code != 0 { if code != 0 {
error!("worker process exited with non-zero code: {}, exiting", code); error!(
"worker process exited with non-zero code: {}, exiting",
code
);
std::process::exit(code); std::process::exit(code);
} }
} }

View File

@ -18,27 +18,16 @@
*/ */
use log::warn; use log::warn;
use std::process::{Command, Stdio, Child};
use widestring::WideCString;
use std::io; use std::io;
use std::process::{Child, Command, Stdio};
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
pub fn spawn_process(cmd: &str, args: &Vec<String>) { pub fn spawn_process(cmd: &str, args: &Vec<String>) -> io::Result<Child> {
// TODO: modify with https://doc.rust-lang.org/std/os/windows/process/trait.CommandExt.html use std::os::windows::process::CommandExt;
let quoted_args: Vec<String> = args.iter().map(|arg| format!("\"{}\"", arg)).collect(); Command::new(cmd)
let quoted_args = quoted_args.join(" "); .creation_flags(0x00000008) // Detached Process
let final_cmd = format!("\"{}\" {}", cmd, quoted_args); .args(args)
unsafe { .spawn()
let cmd_wstr = WideCString::from_str(&final_cmd);
if let Ok(string) = cmd_wstr {
let res = crate::bridge::windows::start_process(string.as_ptr());
if res < 0 {
warn!("unable to start process: {}", final_cmd);
}
} else {
warn!("unable to convert process string into wide format")
}
}
} }
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]