Skip to content

Commit

Permalink
Add Win32 Job support so child processes automatically get cleaned up.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGoddessInari committed Feb 19, 2020
1 parent af39bb2 commit cc020e0
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 4 deletions.
93 changes: 92 additions & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[package]
name = "wslwrap"
version = "0.2.0"
version = "0.2.1"
authors = ["TheGoddessInari <thegoddessinari@gmail.com>"]
edition = "2018"

[dependencies]
win32job = "1"

[profile.release]
lto = true
Expand Down
21 changes: 19 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::env;
use std::option::Option;
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus};
use win32job::Job;

#[derive(Debug, PartialEq)]
enum UnixPathType {
Expand Down Expand Up @@ -134,14 +135,30 @@ fn escape(strings: &[String]) -> Vec<String> {
escaped_strings
}

fn main() -> Result<(), ExitStatus> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
let job = Job::create()?;
let mut info = job.query_extended_limit_info()?;

info.limit_kill_on_job_close();

job.set_extended_limit_info(&mut info)?;

job.assign_current_process()?;
let args: Vec<String> = env::args().collect();
let status = Command::new("wsl.exe")
.args(escape(&args))
.status()
.expect("failed to execute WSL");

std::process::exit(status.code().unwrap_or(0))
if status.success() {
Ok(())
} else {
Err(Box::from(format!(
"{} returned a status code of {:?}.",
args[0],
status.code()
)))
}
}

#[cfg(test)]
Expand Down

0 comments on commit cc020e0

Please sign in to comment.