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
//
// Syd: rock-solid application kernel
// src/retry.rs: Utilities to handle restarting syscalls
//
// Copyright (c) 2025 Ali Polatel <alip@chesswob.org>
//
// SPDX-License-Identifier: GPL-3.0
//! Utilities to handle restarting syscalls
// SAFETY: This module has been liberated from unsafe code!
#![forbid(unsafe_code)]
use nix::errno::Errno;
/// Retries a closure on `EAGAIN` and `EINTR` errors.
///
/// This function will call the provided closure, and if the closure
/// returns `EAGAIN` or `EINTR` error, it will retry the operation until it
/// succeeds or fails with a different error.
pub fn retry_on_intr<F, T>(mut f: F) -> Result<T, Errno>
where
F: FnMut() -> Result<T, Errno>,
{
loop {
match f() {
Err(Errno::EAGAIN | Errno::EINTR) => continue,
result => return result,
}
}
}
/// Retries a closure on `EINTR` errors.
///
/// This function will call the provided closure, and if the closure
/// returns `EINTR` error, it will retry the operation until it
/// succeeds or fails with a different error.
pub fn retry_on_eintr<F, T>(mut f: F) -> Result<T, Errno>
where
F: FnMut() -> Result<T, Errno>,
{
loop {
match f() {
Err(Errno::EINTR) => continue,
result => return result,
}
}
}
/// Retries a closure on `EAGAIN` errors.
///
/// This function will call the provided closure, and if the closure
/// returns `EAGAIN` error, it will retry the operation until it
/// succeeds or fails with a different error.
pub fn retry_on_eagain<F, T>(mut f: F) -> Result<T, Errno>
where
F: FnMut() -> Result<T, Errno>,
{
loop {
match f() {
Err(Errno::EAGAIN) => continue,
result => return result,
}
}
}
/// write! which retries on EINTR and EAGAIN.
#[macro_export]
macro_rules! rwrite {
($dst:expr, $($arg:tt)*) => {{
$crate::retry::retry_on_intr(|| {
$dst.write_fmt(format_args!($($arg)*))
.map_err(|err| $crate::err::err2no(&err))
})
}};
}
/// writeln! which retries on EINTR and EAGAIN.
#[macro_export]
macro_rules! rwriteln {
($dst:expr $(, $($arg:tt)*)?) => {{
$crate::retry::retry_on_intr(|| {
let () = $dst
.write_fmt(format_args!($($($arg)*)?))
.map_err(|err| $crate::err::err2no(&err))?;
$dst
.write_all(b"\n")
.map_err(|err| $crate::err::err2no(&err))
})
}};
}