Skip to content

Commit aed8e16

Browse files
committed
add signal handling utilities to Mount
1 parent d087798 commit aed8e16

File tree

7 files changed

+43
-50
lines changed

7 files changed

+43
-50
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ either = "1"
2323
libc = "0.2"
2424
num_cpus = "1.17"
2525
polyfuse-kernel = { version = "0.3.0-dev", path = "crates/polyfuse-kernel" }
26-
rustix = { version = "1", features = [ "fs", "mount", "net", "param", "pipe", "process", "thread", "use-libc" ] }
26+
rustix = { version = "1", features = [ "fs", "mount", "net", "param", "pipe", "process", "io_uring", "thread", "use-libc" ] }
27+
signal-hook = "0.3.18"
2728
thiserror = "2"
2829
tracing = "0.1"
2930
zerocopy = "0.8.26"
@@ -34,7 +35,6 @@ bytes = "1.10.1"
3435
chrono = "0.4.41"
3536
dashmap = "6.1"
3637
pico-args = "0.5"
37-
signal-hook = "0.3.18"
3838
slab = ">=0.4.11"
3939
tempfile = "3.23.0"
4040
tokio = { version = ">=1.8.4", features = [ "macros", "fs", "io-util", "net", "rt-multi-thread", "sync", "time" ] }

examples/basic.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ use polyfuse::{
1010
};
1111

1212
use anyhow::{ensure, Context as _, Result};
13-
use libc::{SIGHUP, SIGINT, SIGTERM};
1413
use rustix::{
1514
io::Errno,
1615
process::{getgid, getuid},
1716
};
18-
use signal_hook::iterator::Signals;
1917
use std::{io, path::PathBuf, thread, time::Duration};
2018

2119
const CONTENT: &[u8] = b"Hello from FUSE!\n";
@@ -33,12 +31,8 @@ fn main() -> Result<()> {
3331
polyfuse::connect(mountpoint, MountOptions::new(), KernelConfig::new())?;
3432

3533
thread::scope(|scope| -> Result<()> {
36-
let mut signals = Signals::new([SIGTERM, SIGHUP, SIGINT])?;
37-
scope.spawn(move || {
38-
if let Some(_sig) = signals.forever().next() {
39-
let _ = mount.unmount();
40-
}
41-
});
34+
let mount_handle = mount.handle();
35+
scope.spawn(move || mount.unmount_after_interrupted());
4236

4337
// Receive an incoming FUSE request from the kernel.
4438
let mut buf = session.new_fallback_buffer();
@@ -54,6 +48,8 @@ fn main() -> Result<()> {
5448
};
5549
}
5650

51+
mount_handle.close();
52+
5753
Ok(())
5854
})?;
5955

examples/heartbeat.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ use polyfuse::{
2121
use anyhow::{anyhow, ensure, Context as _, Result};
2222
use chrono::Local;
2323
use dashmap::DashMap;
24-
use libc::{SIGHUP, SIGINT, SIGTERM};
2524
use rustix::{io::Errno, param::page_size};
26-
use signal_hook::iterator::Signals;
2725
use std::{
2826
io::{self, prelude::*},
2927
path::PathBuf,
@@ -54,12 +52,8 @@ fn main() -> Result<()> {
5452
let session = &session;
5553
let fs = &fs;
5654
thread::scope(|scope| -> Result<()> {
57-
let mut signals = Signals::new([SIGTERM, SIGHUP, SIGINT])?;
58-
scope.spawn(move || {
59-
if let Some(_sig) = signals.forever().next() {
60-
let _ = mount.unmount();
61-
}
62-
});
55+
let mount_handle = mount.handle();
56+
scope.spawn(move || mount.unmount_after_interrupted());
6357

6458
// Spawn a task that beats the heart.
6559
scope.spawn(move || -> Result<()> {
@@ -131,6 +125,8 @@ fn main() -> Result<()> {
131125
}
132126
}
133127

128+
mount_handle.close();
129+
134130
Ok(())
135131
})?;
136132

examples/heartbeat_entry.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ use polyfuse::{
2020

2121
use anyhow::{ensure, Context as _, Result};
2222
use chrono::Local;
23-
use libc::{SIGHUP, SIGINT, SIGTERM};
2423
use rustix::io::Errno;
25-
use signal_hook::iterator::Signals;
2624
use std::{io, mem, os::unix::prelude::*, path::PathBuf, sync::Mutex, thread, time::Duration};
2725

2826
const FILE_INO: NodeID = match NodeID::from_raw(2) {
@@ -62,12 +60,8 @@ fn main() -> Result<()> {
6260
let conn = &conn;
6361
let fs = &fs;
6462
thread::scope(|scope| -> Result<()> {
65-
let mut signals = Signals::new([SIGTERM, SIGHUP, SIGINT])?;
66-
scope.spawn(move || {
67-
if let Some(_sig) = signals.forever().next() {
68-
let _ = mount.unmount();
69-
}
70-
});
63+
let mount_handle = mount.handle();
64+
scope.spawn(move || mount.unmount_after_interrupted());
7165

7266
// spawn heartbeat thread.
7367
scope.spawn(move || fs.heartbeat(session, conn));
@@ -149,6 +143,8 @@ fn main() -> Result<()> {
149143
}
150144
}
151145

146+
mount_handle.close();
147+
152148
Ok(())
153149
})?;
154150

examples/hello.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ use polyfuse::{
1111
};
1212

1313
use anyhow::{ensure, Context as _, Result};
14-
use libc::{SIGHUP, SIGINT, SIGTERM};
1514
use rustix::{
1615
fs::{Gid, Uid},
1716
io::Errno,
1817
process::{getgid, getuid},
1918
};
20-
use signal_hook::iterator::Signals;
2119
use std::{os::unix::prelude::*, path::PathBuf, thread, time::Duration};
2220

2321
const TTL: Duration = Duration::from_secs(60 * 60 * 24 * 365);
@@ -42,12 +40,8 @@ fn main() -> Result<()> {
4240
let fs = Hello::new();
4341

4442
thread::scope(|scope| -> Result<()> {
45-
let mut signals = Signals::new([SIGTERM, SIGHUP, SIGINT])?;
46-
scope.spawn(move || {
47-
if let Some(_sig) = signals.forever().next() {
48-
let _ = mount.unmount();
49-
}
50-
});
43+
let mount_handle = mount.handle();
44+
scope.spawn(move || mount.unmount_after_interrupted());
5145

5246
let mut buf = session.new_splice_buffer()?;
5347
while session.recv_request(&conn, &mut buf)? {
@@ -120,6 +114,8 @@ fn main() -> Result<()> {
120114
}
121115
}
122116

117+
mount_handle.close();
118+
123119
Ok(())
124120
})?;
125121

examples/poll.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ use polyfuse::{
1212
};
1313

1414
use anyhow::{ensure, Context as _, Result};
15-
use libc::{SIGHUP, SIGINT, SIGTERM};
1615
use rustix::{
1716
io::Errno,
1817
process::{getgid, getuid},
1918
};
20-
use signal_hook::iterator::Signals;
2119
use std::{
2220
collections::HashMap,
2321
path::PathBuf,
@@ -54,12 +52,8 @@ fn main() -> Result<()> {
5452
let session = &session;
5553
let fs = &fs;
5654
thread::scope(|scope| -> Result<()> {
57-
let mut signals = Signals::new([SIGTERM, SIGHUP, SIGINT])?;
58-
scope.spawn(move || {
59-
if let Some(_sig) = signals.forever().next() {
60-
let _ = mount.unmount();
61-
}
62-
});
55+
let mount_handle = mount.handle();
56+
scope.spawn(move || mount.unmount_after_interrupted());
6357

6458
let mut buf = session.new_splice_buffer()?;
6559
while session.recv_request(conn, &mut buf)? {
@@ -179,6 +173,8 @@ fn main() -> Result<()> {
179173
}
180174
}
181175

176+
mount_handle.close();
177+
182178
Ok(())
183179
})?;
184180

src/mount.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ mod unpriv_mount;
33

44
use self::{priv_mount::PrivMount, unpriv_mount::UnprivMount};
55
use bitflags::{bitflags, bitflags_match};
6+
use libc::{SIGHUP, SIGINT, SIGTERM};
67
use rustix::{io::Errno, mount::MountFlags};
8+
use signal_hook::iterator::Signals;
79
use std::{borrow::Cow, io, mem, os::fd::OwnedFd, path::Path};
810

911
// refs:
@@ -148,6 +150,7 @@ pub struct Mount {
148150
kind: MountKind,
149151
mountpoint: Cow<'static, Path>,
150152
mountopts: MountOptions,
153+
signals: Signals,
151154
}
152155

153156
#[derive(Debug)]
@@ -174,8 +177,21 @@ impl Mount {
174177
}
175178

176179
#[inline]
177-
pub fn unmount(mut self) -> io::Result<()> {
178-
self.unmount_()
180+
pub fn handle(&self) -> signal_hook::iterator::Handle {
181+
self.signals.handle()
182+
}
183+
184+
#[inline]
185+
pub fn unmount_after_interrupted(mut self) -> io::Result<()> {
186+
match self.signals.wait().next() {
187+
Some(sig) => tracing::debug!(
188+
"caught the interruption signal: {:?}",
189+
rustix::io_uring::Signal::from_named_raw(sig)
190+
),
191+
None => tracing::debug!("caught the closing event from the handle"),
192+
}
193+
self.unmount_()?;
194+
Ok(())
179195
}
180196

181197
fn unmount_(&mut self) -> io::Result<()> {
@@ -187,12 +203,6 @@ impl Mount {
187203
}
188204
}
189205

190-
impl Drop for Mount {
191-
fn drop(&mut self) {
192-
let _ = self.unmount_();
193-
}
194-
}
195-
196206
pub(crate) fn mount(
197207
mountpoint: Cow<'static, Path>,
198208
mut mountopts: MountOptions,
@@ -225,12 +235,15 @@ pub(crate) fn mount(
225235
},
226236
};
227237

238+
let signals = Signals::new([SIGHUP, SIGTERM, SIGINT])?;
239+
228240
Ok((
229241
fd,
230242
Mount {
231243
kind,
232244
mountpoint,
233245
mountopts,
246+
signals,
234247
},
235248
))
236249
}

0 commit comments

Comments
 (0)