-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Call kic-discover-visa if conditions are right
- Loading branch information
Showing
4 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use std::{io::ErrorKind, path::PathBuf}; | ||
|
||
#[derive(Debug)] | ||
pub struct Process { | ||
path: PathBuf, | ||
args: Vec<String>, | ||
} | ||
impl Process { | ||
pub fn new<I>(path: PathBuf, args: I) -> Self | ||
where | ||
I: IntoIterator, | ||
I::Item: AsRef<str>, | ||
{ | ||
Self { | ||
path, | ||
args: args.into_iter().map(|s| s.as_ref().to_string()).collect(), | ||
} | ||
} | ||
|
||
pub fn exec_replace(self) -> anyhow::Result<()> { | ||
imp::exec_replace(&self) | ||
} | ||
|
||
#[cfg_attr(unix, allow(dead_code))] | ||
pub fn exec(&self) -> anyhow::Result<()> { | ||
let exit = std::process::Command::new(&self.path) | ||
.args(&self.args) | ||
.spawn()? | ||
.wait()?; | ||
if exit.success() { | ||
Ok(()) | ||
} else { | ||
Err(std::io::Error::new( | ||
ErrorKind::Other, | ||
format!( | ||
"child process did not exit successfully: {}", | ||
self.path.display() | ||
), | ||
) | ||
.into()) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(windows)] | ||
mod imp { | ||
use std::io::ErrorKind; | ||
|
||
use super::Process; | ||
use windows_sys::Win32::{ | ||
Foundation::{BOOL, FALSE, TRUE}, | ||
System::Console::SetConsoleCtrlHandler, | ||
}; | ||
|
||
#[allow(clippy::missing_const_for_fn)] // This lint seems to be broken for this specific case | ||
unsafe extern "system" fn ctrlc_handler(_: u32) -> BOOL { | ||
TRUE | ||
} | ||
|
||
pub(super) fn exec_replace(process: &Process) -> anyhow::Result<()> { | ||
//Safety: This is an external handler that calls into the windows API. It is | ||
// expected to be safe. | ||
unsafe { | ||
if SetConsoleCtrlHandler(Some(ctrlc_handler), TRUE) == FALSE { | ||
return Err(std::io::Error::new( | ||
ErrorKind::Other, | ||
"Unable to set Ctrl+C Handler".to_string(), | ||
) | ||
.into()); | ||
} | ||
} | ||
|
||
process.exec() | ||
} | ||
} | ||
|
||
#[cfg(unix)] | ||
mod imp { | ||
use crate::Process; | ||
use std::os::unix::process::CommandExt; | ||
|
||
pub(super) fn exec_replace(process: &Process) -> anyhow::Result<()> { | ||
let mut command = std::process::Command::new(&process.path); | ||
command.args(&process.args); | ||
Err(command.exec().into()) // Exec replaces the current application's program memory, therefore execution will | ||
} | ||
} |