Skip to content

Commit

Permalink
Run application and open command detached from server process
Browse files Browse the repository at this point in the history
Fixes server being unkillable because of invoked application that is still running
  • Loading branch information
Exidex committed Aug 18, 2024
1 parent cafb29f commit e8c45b8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions rust/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ bytes = "1.6.0"
scenario_runner = { path = "../scenario_runner", optional = true }
itertools = "0.10.5"

[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies]
libc = "0.2.153"

[target.'cfg(target_os = "linux")'.dependencies]
freedesktop_entry_parser = "1.3"
freedesktop-icons = "0.2"
Expand Down
5 changes: 2 additions & 3 deletions rust/server/src/plugins/applications/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use image::ImageFormat;
use image::imageops::FilterType;

#[cfg(target_os = "linux")]
mod linux;

use image::ImageFormat;
use image::imageops::FilterType;
#[cfg(target_os = "linux")]
pub use linux::get_apps;

Expand Down
50 changes: 44 additions & 6 deletions rust/server/src/plugins/js/plugins/applications.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use std::path::Path;

use deno_core::op;
use tokio::task::spawn_blocking;

use crate::plugins::applications::{DesktopEntry, get_apps};

#[op]
Expand All @@ -15,9 +12,50 @@ fn open_application(command: Vec<String>) -> anyhow::Result<()> {
let path = &command[0];
let args = &command[1..];

std::process::Command::new(Path::new(path))
.args(args)
.spawn()?;
#[cfg(not(windows))]
spawn_detached(path, args)?;

Ok(())
}

#[cfg(not(windows))]
pub fn spawn_detached<I, S>(
path: &str,
args: I,
) -> std::io::Result<()>
where
I: IntoIterator<Item = S> + Copy,
S: AsRef<std::ffi::OsStr>,
{
// from https://github.com/alacritty/alacritty/blob/5abb4b73937b17fe501b9ca20b602950f1218b96/alacritty/src/daemon.rs#L65
use std::os::unix::prelude::CommandExt;
use std::process::{Command, Stdio};

let mut command = Command::new(path);

command
.args(args)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null());

unsafe {
command
.pre_exec(|| {
match libc::fork() {
-1 => return Err(std::io::Error::last_os_error()),
0 => (),
_ => libc::_exit(0),
}

if libc::setsid() == -1 {
return Err(std::io::Error::last_os_error());
}

Ok(())
})
.spawn()?
.wait()
.map(|_| ())
}
}
2 changes: 1 addition & 1 deletion rust/server/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ impl ApplicationManager {
}

pub fn handle_open(&self, href: String) {
match open::that(&href) {
match open::that_detached(&href) {
Ok(()) => tracing::info!("Opened '{}' successfully.", href),
Err(err) => tracing::error!("An error occurred when opening '{}': {}", href, err),
}
Expand Down

0 comments on commit e8c45b8

Please sign in to comment.