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
//! Process stdio duplicates share open-file descriptions, even across loops.
//! Snapshot all three before changing any: stdout and stderr may be aliases.
use super::poller::last_error;
use crate::{Error, ErrorKind, Result};
use std::{
os::fd::{AsRawFd, FromRawFd, OwnedFd},
sync::Mutex,
};
static STATE: Mutex<Option<State>> = Mutex::new(None);
struct Saved {
fd: OwnedFd,
flags: i32,
mode: Option<libc::termios>,
}
struct State {
users: usize,
saved: [Option<Saved>; 3],
}
/// Held by the native resource until all its operations have quiesced.
pub(super) struct Lease;
impl Lease {
pub(super) fn acquire(raw: i32) -> Result<(Self, Option<libc::termios>)> {
let mut state = STATE.lock().unwrap_or_else(|e| e.into_inner());
if let Some(state) = state.as_mut() {
state.users = state
.users
.checked_add(1)
.ok_or_else(|| Error::new(ErrorKind::ResourceLimit))?;
} else {
let mut saved = [None, None, None];
for (raw, slot) in saved.iter_mut().enumerate() {
// SAFETY: duplicates a borrowed descriptor. Start above stdio so
// a closed standard descriptor is never accidentally populated.
let dup = unsafe { libc::fcntl(raw as i32, libc::F_DUPFD_CLOEXEC, 3) };
if dup < 0 {
let error = std::io::Error::last_os_error();
if error.raw_os_error() == Some(libc::EBADF) {
continue;
}
return Err(error.into());
}
// SAFETY: successful fcntl returned a fresh owned descriptor.
let fd = unsafe { OwnedFd::from_raw_fd(dup) };
// SAFETY: integer-only command on a live descriptor.
let flags = unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_GETFL) };
if flags < 0 {
return Err(last_error());
}
// SAFETY: termios is plain C output storage.
let mut mode = unsafe { std::mem::zeroed() };
// SAFETY: live descriptor and writable termios storage.
let mode =
(unsafe { libc::tcgetattr(fd.as_raw_fd(), &mut mode) } == 0).then_some(mode);
*slot = Some(Saved { fd, flags, mode });
}
*state = Some(State { users: 1, saved });
}
let mode = state
.as_ref()
.and_then(|s| s.saved[raw as usize].as_ref())
.and_then(|s| s.mode);
Ok((Self, mode))
}
}
impl Drop for Lease {
fn drop(&mut self) {
let mut state = STATE.lock().unwrap_or_else(|e| e.into_inner());
let Some(active) = state.as_mut() else {
return;
};
active.users -= 1;
if active.users != 0 {
return;
}
// Serialize final restoration with the next acquisition. An Arc/Weak
// registry alone would let a new cohort snapshot flags while the last
// old owner is still restoring them in its destructor.
for saved in active.saved.iter().flatten() {
if let Some(mode) = &saved.mode {
// SAFETY: retained descriptor and previously captured termios.
unsafe { libc::tcsetattr(saved.fd.as_raw_fd(), libc::TCSANOW, mode) };
}
// SAFETY: retained descriptor and flags from F_GETFL.
unsafe { libc::fcntl(saved.fd.as_raw_fd(), libc::F_SETFL, saved.flags) };
}
*state = None;
}
}