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
use crate::{types::message::BuildError, Address, LazyLoadBlob, Request};
#[derive(Clone, Debug)]
pub enum OnExit {
None,
Restart,
Requests(Vec<Request>),
}
impl OnExit {
/// Call the kernel to get the current set `OnExit` behavior.
pub fn get() -> Self {
match crate::kinode::process::standard::get_on_exit() {
crate::kinode::process::standard::OnExit::None => OnExit::None,
crate::kinode::process::standard::OnExit::Restart => OnExit::Restart,
crate::kinode::process::standard::OnExit::Requests(reqs) => {
let mut requests: Vec<Request> = Vec::with_capacity(reqs.len());
for req in reqs {
requests.push(Request {
target: Some(req.0),
inherit: req.1.inherit,
timeout: req.1.expects_response,
body: Some(req.1.body),
metadata: req.1.metadata,
blob: req.2,
context: None,
capabilities: req.1.capabilities,
});
}
OnExit::Requests(requests)
}
}
}
/// Check if this `OnExit` is [`OnExit::None`].
pub fn is_none(&self) -> bool {
match self {
OnExit::None => true,
OnExit::Restart => false,
OnExit::Requests(_) => false,
}
}
/// Check if this `OnExit` is [`OnExit::Restart`].
pub fn is_restart(&self) -> bool {
match self {
OnExit::None => false,
OnExit::Restart => true,
OnExit::Requests(_) => false,
}
}
/// Check if this `OnExit` is [`OnExit::Requests`].
pub fn is_requests(&self) -> bool {
match self {
OnExit::None => false,
OnExit::Restart => false,
OnExit::Requests(_) => true,
}
}
/// Get the [`OnExit::Requests`] variant of this `OnExit`, if it is one.
pub fn get_requests(&self) -> Option<&[Request]> {
match self {
OnExit::None => None,
OnExit::Restart => None,
OnExit::Requests(reqs) => Some(reqs),
}
}
/// Add a [`Request`] to this `OnExit` if it is of variant [`OnExit::Requests`].
pub fn add_request(&mut self, new: Request) {
if let OnExit::Requests(ref mut reqs) = self {
reqs.push(new);
}
}
/// Set the `OnExit` behavior for this process.
///
/// Will return a [`BuildError`] if any requests within the [`OnExit::Requests`] behavior are
/// not valid (by not having a `body` and/or `target` set).
pub fn set(self) -> Result<(), BuildError> {
crate::kinode::process::standard::set_on_exit(&self._to_standard()?);
Ok(())
}
/// Convert this `OnExit` to the kernel's `OnExit` type.
///
/// Will return a [`BuildError`] if any requests within the [`OnExit::Requests`] behavior are
/// not valid (by not having a `body` and/or `target` set).
pub fn _to_standard(self) -> Result<crate::kinode::process::standard::OnExit, BuildError> {
match self {
OnExit::None => Ok(crate::kinode::process::standard::OnExit::None),
OnExit::Restart => Ok(crate::kinode::process::standard::OnExit::Restart),
OnExit::Requests(reqs) => {
let mut kernel_reqs: Vec<(
Address,
crate::kinode::process::standard::Request,
Option<LazyLoadBlob>,
)> = Vec::with_capacity(reqs.len());
for req in reqs {
kernel_reqs.push((
req.target.ok_or(BuildError::NoTarget)?,
crate::kinode::process::standard::Request {
inherit: req.inherit,
expects_response: None,
body: req.body.ok_or(BuildError::NoBody)?,
metadata: req.metadata,
capabilities: req.capabilities,
},
req.blob,
));
}
Ok(crate::kinode::process::standard::OnExit::Requests(
kernel_reqs,
))
}
}
}
}