-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Windows creation_flags #70
Comments
This is pretty much exactly the sort of thing that |
It's probably not a big deal either way, but my thoughts before opening the issue:
I haven't started using After reviewing the creation flags - I do think many would work just fine with |
Gotcha. Please let me know if the |
(Sorry to hijack this old conversation.) // prevent child processes from being terminated by ctrl-c
cfg_if! {
if #[cfg(windows)] {
pub fn prevent_being_killed(cmd: &mut Command) -> std::io::Result<()> {
cmd.creation_flags(CREATE_NEW_PROCESS_GROUP);
Ok(())
}
} else {
pub fn prevent_being_killed(_cmd: &mut Command) -> std::io::Result<()> {
// what to do on linux/mac?
Ok(())
}
}
} I'm using it with |
@Boscop something like this using use std::os::unix::process::CommandExt;
fn main() {
unsafe {
std::process::Command::new("sleep")
.arg("100")
.pre_exec(|| {
libc::setpgid(0, 0); // technically this can fail and I'm not checking
Ok(())
})
.status()
.unwrap();
}
} When I Ctrl-C that program, I see that |
Thanks! I now wrote it like this to handle errors: pub fn prevent_being_killed(cmd: &mut Command) -> std::io::Result<()> {
use nix::{Error, unistd::{Pid, setpgid}};
use std::{io, os::unix::process::CommandExt};
unsafe {
cmd.pre_exec(|| {
match setpgid(Pid::from_raw(0), Pid::from_raw(0)) {
Ok(()) => Ok(()),
Err(Error::Sys(errno)) => Err(io::Error::from_raw_os_error(errno as _)),
Err(e) => Err(
io::Error::new(io::ErrorKind::Other,
format!("failed to create new process group for child process: {}", e)
)
),
}
});
}
Ok(())
} Btw, how could I intercept |
@Boscop suppressing a signal in the child is relatively simple. You use the POSIX Handling the signal in the parent is substantially more involved. I think the |
There's also https://man7.org/linux/man-pages/man2/setsid.2.html. |
I need to use std::os::windows::process::CommandExt::creation_flags when starting a process.
It looks like I could use
before_spawn
, but it would be nice to have a more direct api.The text was updated successfully, but these errors were encountered: