1pub type Errno = libc::c_int;
21
22#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
24#[cfg_attr(feature = "tester", derive(bincode::Encode, bincode::Decode))]
25pub struct Error(ErrorKind);
26
27impl Error {
28 pub fn kind(&self) -> ErrorKind {
29 self.0
30 }
31}
32
33#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
35#[cfg_attr(feature = "tester", derive(bincode::Encode, bincode::Decode))]
36pub enum ErrorKind {
37 Fork(Errno),
38 Wait(Errno),
39 DetachSession(Errno),
40 GroupNotFound,
41 GroupContainsNul,
42 SetGroup(Errno),
43 UserNotFound,
44 UserContainsNul,
45 SetUser(Errno),
46 ChangeDirectory(Errno),
47 PathContainsNul,
48 OpenPidfile(Errno),
49 GetPidfileFlags(Errno),
50 SetPidfileFlags(Errno),
51 LockPidfile(Errno),
52 ChownPidfile(Errno),
53 OpenDevnull(Errno),
54 RedirectStreams(Errno),
55 CloseDevnull(Errno),
56 TruncatePidfile(Errno),
57 WritePid(Errno),
58 WritePidUnspecifiedError,
59 Chroot(Errno),
60}
61
62impl ErrorKind {
63 fn description(&self) -> &str {
64 match self {
65 ErrorKind::Fork(_) => "unable to fork",
66 ErrorKind::Wait(_) => "wait failed",
67 ErrorKind::DetachSession(_) => "unable to create new session",
68 ErrorKind::GroupNotFound => "unable to resolve group name to group id",
69 ErrorKind::GroupContainsNul => "group option contains NUL",
70 ErrorKind::SetGroup(_) => "unable to set group",
71 ErrorKind::UserNotFound => "unable to resolve user name to user id",
72 ErrorKind::UserContainsNul => "user option contains NUL",
73 ErrorKind::SetUser(_) => "unable to set user",
74 ErrorKind::ChangeDirectory(_) => "unable to change directory",
75 ErrorKind::PathContainsNul => "pid_file option contains NUL",
76 ErrorKind::OpenPidfile(_) => "unable to open pid file",
77 ErrorKind::GetPidfileFlags(_) => "unable get pid file flags",
78 ErrorKind::SetPidfileFlags(_) => "unable set pid file flags",
79 ErrorKind::LockPidfile(_) => "unable to lock pid file",
80 ErrorKind::ChownPidfile(_) => "unable to chown pid file",
81 ErrorKind::OpenDevnull(_) => "unable to open /dev/null",
82 ErrorKind::RedirectStreams(_) => "unable to redirect standard streams to /dev/null",
83 ErrorKind::CloseDevnull(_) => "unable to close /dev/null",
84 ErrorKind::TruncatePidfile(_) => "unable to truncate pid file",
85 ErrorKind::WritePid(_) => "unable to write self pid to pid file",
86 ErrorKind::WritePidUnspecifiedError => {
87 "unable to write self pid to pid file due to unknown reason"
88 }
89 ErrorKind::Chroot(_) => "unable to chroot into directory",
90 }
91 }
92
93 fn errno(&self) -> Option<Errno> {
94 match self {
95 ErrorKind::Fork(errno) => Some(*errno),
96 ErrorKind::Wait(errno) => Some(*errno),
97 ErrorKind::DetachSession(errno) => Some(*errno),
98 ErrorKind::GroupNotFound => None,
99 ErrorKind::GroupContainsNul => None,
100 ErrorKind::SetGroup(errno) => Some(*errno),
101 ErrorKind::UserNotFound => None,
102 ErrorKind::UserContainsNul => None,
103 ErrorKind::SetUser(errno) => Some(*errno),
104 ErrorKind::ChangeDirectory(errno) => Some(*errno),
105 ErrorKind::PathContainsNul => None,
106 ErrorKind::OpenPidfile(errno) => Some(*errno),
107 ErrorKind::GetPidfileFlags(errno) => Some(*errno),
108 ErrorKind::SetPidfileFlags(errno) => Some(*errno),
109 ErrorKind::LockPidfile(errno) => Some(*errno),
110 ErrorKind::ChownPidfile(errno) => Some(*errno),
111 ErrorKind::OpenDevnull(errno) => Some(*errno),
112 ErrorKind::RedirectStreams(errno) => Some(*errno),
113 ErrorKind::CloseDevnull(errno) => Some(*errno),
114 ErrorKind::TruncatePidfile(errno) => Some(*errno),
115 ErrorKind::WritePid(errno) => Some(*errno),
116 ErrorKind::WritePidUnspecifiedError => None,
117 ErrorKind::Chroot(errno) => Some(*errno),
118 }
119 }
120}
121
122impl std::fmt::Display for ErrorKind {
123 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
124 f.write_str(self.description())?;
125 if let Some(errno) = self.errno() {
126 write!(f, ", errno {}", errno)?
127 }
128 Ok(())
129 }
130}
131
132impl std::error::Error for ErrorKind {}
133
134impl std::fmt::Display for Error {
135 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
136 write!(f, "{}", self.kind())
137 }
138}
139
140impl std::error::Error for Error {}
141
142impl From<ErrorKind> for Error {
143 fn from(kind: ErrorKind) -> Self {
144 Self(kind)
145 }
146}
147
148pub trait Num {
149 fn is_err(&self) -> bool;
150}
151
152impl Num for i8 {
153 fn is_err(&self) -> bool {
154 *self == -1
155 }
156}
157
158impl Num for i16 {
159 fn is_err(&self) -> bool {
160 *self == -1
161 }
162}
163
164impl Num for i32 {
165 fn is_err(&self) -> bool {
166 *self == -1
167 }
168}
169
170impl Num for i64 {
171 fn is_err(&self) -> bool {
172 *self == -1
173 }
174}
175
176impl Num for isize {
177 fn is_err(&self) -> bool {
178 *self == -1
179 }
180}
181
182pub fn check_err<N: Num, F: FnOnce(Errno) -> ErrorKind>(ret: N, f: F) -> Result<N, ErrorKind> {
183 if ret.is_err() {
184 Err(f(errno::errno().into()))
185 } else {
186 Ok(ret)
187 }
188}