1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
extern crate nix;
mod pipe;
pub mod process;
use std::path::PathBuf;
use std::time::{Duration, Instant};
use pipe::Pipe;
#[derive(Copy, Clone, PartialEq, std::fmt::Debug)]
pub enum ShellProcState {
Idle,
SubprocessRunning,
Terminated
}
#[derive(Copy, Clone, PartialEq, std::fmt::Debug)]
pub enum ShellError {
CouldNotStartProcess,
InvalidData,
IoTimeout,
ShellRunning,
ShellTerminated,
CouldNotKill,
PipeError(nix::errno::Errno)
}
#[derive(std::fmt::Debug)]
pub struct ShellProc {
pub state: ShellProcState,
pub exit_status: u8,
pub pid: i32,
pub wrkdir: PathBuf,
pub exec_time: Duration,
rc: u8,
uuid: String,
start_time: Instant,
stdout_cache: Option<String>,
echo_command: String,
stdin_pipe: Pipe,
stdout_pipe: Pipe,
stderr_pipe: Pipe
}
impl std::fmt::Display for ShellError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let code_str: String = match self {
ShellError::CouldNotStartProcess => String::from("Could not start process"),
ShellError::InvalidData => String::from("Invalid data from process"),
ShellError::IoTimeout => String::from("I/O timeout"),
ShellError::ShellTerminated => String::from("Shell has terminated"),
ShellError::ShellRunning => String::from("Tried to clean shell up while still running"),
ShellError::CouldNotKill => String::from("Could not send signal to shell process"),
ShellError::PipeError(errno) => format!("Pipe error: {}", errno),
};
write!(f, "{}", code_str)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_proc_fmt_shell_error() {
assert_eq!(format!("{}", ShellError::CouldNotStartProcess), String::from("Could not start process"));
assert_eq!(format!("{}", ShellError::InvalidData), String::from("Invalid data from process"));
assert_eq!(format!("{}", ShellError::IoTimeout), String::from("I/O timeout"));
assert_eq!(format!("{}", ShellError::ShellTerminated), String::from("Shell has terminated"));
assert_eq!(format!("{}", ShellError::ShellRunning), String::from("Tried to clean shell up while still running"));
assert_eq!(format!("{}", ShellError::CouldNotKill), String::from("Could not send signal to shell process"));
assert_eq!(format!("{}", ShellError::PipeError(nix::errno::Errno::EACCES)), format!("Pipe error: {}", nix::errno::Errno::EACCES));
}
}