summaryrefslogtreecommitdiffstats
path: root/src/recite/ps.rs
diff options
context:
space:
mode:
authorRory Dudley2024-02-27 03:49:01 -0700
committerRory Dudley2024-02-27 03:49:01 -0700
commite94d09da9449cabd7ece2acd98d52b1946a922a4 (patch)
treef0fb980662c01bf0c37bc2cfe30dcc5f7952f869 /src/recite/ps.rs
parent0548e74cb3227716cf445f27bd64b8c0b4d0f981 (diff)
downloaddwarvish-e94d09da9449cabd7ece2acd98d52b1946a922a4.tar.gz
Remove custom errors and fix background forking
Removes the custom errors in src/recite/erro.rs, and replaces them with std::io::Errors throughout (recite(), incant_, macros). Fixed a bug with the way forking to the background is handled, where registering the signal handler in main for all processes would break couplets (i.e. pipes). Instead, this sets up a new signal handler each time a process is forked into the background. It uses a Vec<i32> to keep track of all the background processes.
Notes
Notes: First off, there is some defunct code in the main repl loop, which is an example of killing zombie processes after each prompt. This should be removed, but I kept it in, just in case I go back to it for some reason. To be honest, I have no clue why this code works. In theory, I should have to remove the pid from the pids: Vec<i32> if waitpid returns a positive integer. However, when I tried this, it completely broke the program. ¯\_(ツ)_/¯ Also, it's worth noting that registering a signal handler with signal_hook::low_level::register, is somewhat costly, according to their docs. Given that this only occurs for background processes that are forked, however, I think it is acceptable. Finally, we never unregister the signal handler, so I'm not sure if that's still hanging out in memory somewhere or no.
Diffstat (limited to 'src/recite/ps.rs')
-rw-r--r--src/recite/ps.rs44
1 files changed, 8 insertions, 36 deletions
diff --git a/src/recite/ps.rs b/src/recite/ps.rs
index 3557505..30c3d7c 100644
--- a/src/recite/ps.rs
+++ b/src/recite/ps.rs
@@ -5,30 +5,15 @@ macro_rules! task {
let mut child = Command::new($verse.verb())
.args($verse.clause())
.stdin(Stdio::piped())
- .spawn()
- .map_err(|e| match e.kind() {
- io::ErrorKind::PermissionDenied => Mishap::PermissionDenied($verse.verb()),
- _ => Mishap::Else($verse.verb()),
- })?;
+ .spawn()?;
- let stdin = child
- .stdin
- .as_mut()
- .ok_or(Mishap::BrokenPipe($verse.verb()))?;
- stdin
- .write_all(&$out.as_bytes())
- .map_err(|_| Mishap::BrokenPipe($verse.verb()))?;
+ let stdin = child.stdin.as_mut().ok_or(io::ErrorKind::BrokenPipe)?;
+ stdin.write_all(&$out.as_bytes())?;
$out.clear();
child
} else {
- Command::new($verse.verb())
- .args($verse.clause())
- .spawn()
- .map_err(|e| match e.kind() {
- io::ErrorKind::PermissionDenied => Mishap::PermissionDenied($verse.verb()),
- _ => Mishap::Else($verse.verb()),
- })?
+ Command::new($verse.verb()).args($verse.clause()).spawn()?
}
};
}
@@ -41,19 +26,10 @@ macro_rules! ctask {
.args($verse.clause())
.stdin(Stdio::piped())
.stdout(Stdio::piped())
- .spawn()
- .map_err(|e| match e.kind() {
- io::ErrorKind::PermissionDenied => Mishap::PermissionDenied($verse.verb()),
- _ => Mishap::Else($verse.verb()),
- })?;
+ .spawn()?;
- let stdin = child
- .stdin
- .as_mut()
- .ok_or(Mishap::BrokenPipe($verse.verb()))?;
- stdin
- .write_all(&$out.as_bytes())
- .map_err(|_| Mishap::BrokenPipe($verse.verb()))?;
+ let stdin = child.stdin.as_mut().ok_or(io::ErrorKind::BrokenPipe)?;
+ stdin.write_all(&$out.as_bytes())?;
$out.clear();
child
@@ -61,11 +37,7 @@ macro_rules! ctask {
Command::new($verse.verb())
.args($verse.clause())
.stdout(Stdio::piped())
- .spawn()
- .map_err(|e| match e.kind() {
- io::ErrorKind::PermissionDenied => Mishap::PermissionDenied($verse.verb()),
- _ => Mishap::Else($verse.verb()),
- })?
+ .spawn()?
}
};
}