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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
//! The error type: every failure says what failed and what to do.
use core::fmt;
use std::{io, path::PathBuf, time::Duration};
/// Everything that can go wrong in sys-lib.
///
/// Every variant's message is one sentence saying what failed, followed by
/// what to do about it. No variant ever carries file content, registry content,
/// or identifier values: [`Error::Malformed`] names the source (a constant
/// path) and a line number only.
///
/// # Examples
///
/// ```
/// use sys_lib as sys;
///
/// match sys::host::name() {
/// Ok(name) => println!("host {name}"),
/// Err(e) if e.is_unavailable() => println!("host name unknown here: {e}"),
/// Err(e) => return Err(e),
/// }
/// # Ok::<(), sys_lib::Error>(())
/// ```
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
/// This reading does not exist on this platform. Treat the value as unknown.
Unsupported {
/// The reading.
what: &'static str,
/// The platform that lacks it.
platform: &'static str,
},
/// The OS refused (root-only files, a sandbox, SELinux, seccomp).
PermissionDenied {
/// The reading.
what: &'static str,
/// The permission that would allow it.
hint: &'static str,
},
/// A file or key that should exist does not (a container with a reduced `/proc`).
NotFound {
/// The reading.
what: &'static str,
/// The file, key, or call that was missing.
source: &'static str,
},
/// The OS returned data that does not parse.
Malformed {
/// The reading.
what: &'static str,
/// The file, key, or call the data came from.
source: &'static str,
/// The line, when the data is line-based.
line: Option<u32>,
},
/// A file, a list, or a caller's buffer exceeded its budget.
TooLarge {
/// The reading.
what: &'static str,
/// The budget that was exceeded.
limit: u64,
},
/// A sampler was asked again before any time passed on the OS clock.
TooSoon {
/// The time since the previous sample.
elapsed: Duration,
/// The shortest interval that gives a meaningful value.
minimum: Duration,
},
/// A system call failed.
Os {
/// The reading.
what: &'static str,
/// The call that failed.
call: &'static str,
/// The OS error code (`errno`, a Win32 error, an `NTSTATUS`, a `kern_return_t`).
code: i32,
},
/// File I/O failed outside the cases above.
Io {
/// The reading.
what: &'static str,
/// The underlying error.
source: io::Error,
},
/// Invalid input from the caller.
InvalidInput {
/// The input.
what: &'static str,
/// The rule it broke.
reason: &'static str,
},
/// The process guard found another copy holding the lock.
AlreadyRunning {
/// The other copy's process id, when its pid file is readable.
pid: Option<u32>,
/// The lock file.
lock_file: PathBuf,
},
/// The process guard's lock directory cannot be created, written, or trusted.
RunDirUnavailable {
/// The directory.
path: PathBuf,
/// Why, as an I/O error (the kind and a message naming the fix).
source: io::Error,
},
/// No usable identifier exists for the requested basis. See
/// `device::report()` for the state of each source.
#[cfg(feature = "device-id")]
#[cfg_attr(docsrs, doc(cfg(feature = "device-id")))]
NoIdentifierSource {
/// The basis asked for.
basis: crate::device::Basis,
},
/// The hard open-file limit is below the process guard's minimum.
OpenFilesTooLow {
/// The hard limit the process has.
have: u64,
/// The minimum the guard was asked for.
need: u64,
},
}
/// Where unexpected OS data is reported.
const ISSUES: &str = "https://github.com/jamesgober/sys-lib/issues";
/// The result type used throughout sys-lib.
pub type Result<T, E = Error> = core::result::Result<T, E>;
impl Error {
/// True for the errors that mean "this fact is not available here", which a
/// caller usually treats as unknown: [`Unsupported`](Error::Unsupported),
/// [`PermissionDenied`](Error::PermissionDenied), and [`NotFound`](Error::NotFound).
///
/// ```
/// let e = sys_lib::Error::Unsupported { what: "load average", platform: "windows" };
/// assert!(e.is_unavailable());
/// ```
#[must_use]
pub fn is_unavailable(&self) -> bool {
matches!(
self,
Self::Unsupported { .. } | Self::PermissionDenied { .. } | Self::NotFound { .. }
)
}
/// An [`Io`](Error::Io) error, or the more specific variant its kind maps to.
#[cfg_attr(
not(any(target_os = "linux", target_os = "android", test)),
allow(dead_code)
)]
pub(crate) fn io(what: &'static str, source: &'static str, err: io::Error) -> Self {
match err.kind() {
io::ErrorKind::NotFound => Self::NotFound { what, source },
io::ErrorKind::PermissionDenied => Self::PermissionDenied {
what,
hint: "run as a user that may read it, or treat the value as unknown",
},
_ => Self::Io { what, source: err },
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Unsupported { what, platform } => write!(
f,
"{what} is not available on {platform}; treat the value as unknown"
),
Self::PermissionDenied { what, hint } => {
write!(f, "reading {what} was refused by the OS; {hint}")
}
Self::NotFound { what, source } => write!(
f,
"{what} could not be read: {source} does not exist; treat the value as unknown \
(minimal containers and sandboxes often lack it)"
),
Self::Malformed { what, source, line } => {
write!(f, "{what} could not be read: {source} does not parse")?;
if let Some(line) = line {
write!(f, " (line {line})")?;
}
write!(
f,
"; treat the value as unknown, and report it at {ISSUES} with the file \
named here"
)
}
Self::TooLarge { what, limit } => write!(
f,
"{what} exceeded its budget of {limit}; treat the value as unknown, and if the \
machine is genuinely that large, report it at {ISSUES}"
),
Self::TooSoon { elapsed, minimum } => write!(
f,
"sampled again after {elapsed:?}, before the OS clock moved; sample at least \
{minimum:?} apart"
),
Self::Os { what, call, code } => write!(
f,
"{what} could not be read: {call} failed with OS error {code}; the code says \
why, and if it is unexpected, report it at {ISSUES}"
),
Self::Io { what, source } => write!(
f,
"{what} could not be read: {source}; treat the value as unknown, and if it is \
unexpected, report it at {ISSUES}"
),
Self::InvalidInput { what, reason } => write!(f, "invalid {what}: {reason}"),
Self::AlreadyRunning { pid, lock_file } => {
write!(f, "another copy is already running")?;
if let Some(pid) = pid {
write!(f, " as process {pid}")?;
}
write!(
f,
" and holds {}; stop it, or give this copy another lock directory",
lock_file.display()
)
}
Self::RunDirUnavailable { path, source } => write!(
f,
"the lock directory {} cannot be used: {source}",
path.display()
),
#[cfg(feature = "device-id")]
Self::NoIdentifierSource { basis } => write!(
f,
"no usable device identifier for {basis:?}: device::report() shows the state of each source"
),
Self::OpenFilesTooLow { have, need } => write!(
f,
"the hard open-file limit is {have}, below the required {need}; raise it \
(LimitNOFILE= in the systemd unit, `ulimit -Hn`, or launchd's HardResourceLimits)"
),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io { source, .. } | Self::RunDirUnavailable { source, .. } => Some(source),
_ => None,
}
}
}
#[cfg(test)]
mod tests {
use std::error::Error as _;
use super::*;
const fn assert_send_sync<T: Send + Sync + 'static>() {}
const _: () = assert_send_sync::<Error>();
fn every_variant() -> Vec<Error> {
vec![
Error::Unsupported {
what: "load average",
platform: "windows",
},
Error::PermissionDenied {
what: "board serial",
hint: "run as root",
},
Error::NotFound {
what: "os-release",
source: "/etc/os-release",
},
Error::Malformed {
what: "cpu list",
source: "/proc/self/status",
line: Some(7),
},
Error::Malformed {
what: "cpu list",
source: "/proc/self/status",
line: None,
},
Error::TooLarge {
what: "mount list",
limit: 4096,
},
Error::TooSoon {
elapsed: Duration::from_millis(1),
minimum: Duration::from_millis(10),
},
Error::Os {
what: "host name",
call: "GetComputerNameExW",
code: 5,
},
Error::Io {
what: "status",
source: io::Error::other("disk on fire"),
},
Error::InvalidInput {
what: "guard name",
reason: "must not contain '/'",
},
Error::AlreadyRunning {
pid: Some(4821),
lock_file: PathBuf::from("/run/site/site.lock"),
},
Error::AlreadyRunning {
pid: None,
lock_file: PathBuf::from("/run/site/site.lock"),
},
Error::RunDirUnavailable {
path: PathBuf::from("/tmp"),
source: io::Error::other("writable by other users; chmod 700 it"),
},
Error::OpenFilesTooLow {
have: 256,
need: 1024,
},
]
}
#[test]
fn test_error_display_every_variant_says_what_to_do() {
// NFR-15: every message names the failure and a next step.
for e in every_variant() {
let text = e.to_string();
assert!(!text.is_empty());
assert!(
text.contains(';') || text.contains(':'),
"no explanation in {text:?}"
);
assert!(!text.ends_with(' '), "trailing space in {text:?}");
}
}
#[test]
fn test_error_display_includes_the_specific_values() {
let e = Error::AlreadyRunning {
pid: Some(4821),
lock_file: PathBuf::from("site.lock"),
};
assert!(e.to_string().contains("process 4821"));
let e = Error::OpenFilesTooLow {
have: 256,
need: 1024,
};
assert!(e.to_string().contains("256") && e.to_string().contains("1024"));
let e = Error::Malformed {
what: "x",
source: "/proc/stat",
line: Some(3),
};
assert!(e.to_string().contains("line 3"));
}
#[test]
fn test_error_is_unavailable_only_for_unknown_facts() {
for e in every_variant() {
let expected = matches!(
e,
Error::Unsupported { .. } | Error::PermissionDenied { .. } | Error::NotFound { .. }
);
assert_eq!(e.is_unavailable(), expected, "{e:?}");
}
}
#[test]
fn test_error_source_only_for_wrapped_io() {
for e in every_variant() {
let wraps = matches!(e, Error::Io { .. } | Error::RunDirUnavailable { .. });
assert_eq!(e.source().is_some(), wraps, "{e:?}");
}
}
#[test]
fn test_error_io_maps_not_found_and_permission_denied() {
let e = Error::io("x", "/proc/x", io::Error::from(io::ErrorKind::NotFound));
assert!(matches!(
e,
Error::NotFound {
source: "/proc/x",
..
}
));
let e = Error::io(
"x",
"/proc/x",
io::Error::from(io::ErrorKind::PermissionDenied),
);
assert!(matches!(e, Error::PermissionDenied { .. }));
let e = Error::io("x", "/proc/x", io::Error::from(io::ErrorKind::Interrupted));
assert!(matches!(e, Error::Io { .. }));
}
}