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
use crate::channel::Channel;
use bytes::Bytes;
use futures::{
future::{Future, FutureExt},
select,
};
use libc::c_int;
use polyfuse::{request::BytesBuffer, Filesystem, Session, SessionInitializer};
use std::{ffi::OsStr, io, path::Path, sync::Arc};
use tokio::signal::unix::{signal, SignalKind};
#[derive(Debug)]
pub struct Server {
session: Arc<Session>,
notifier: Arc<polyfuse::Notifier<Bytes>>,
channel: Channel,
}
impl Server {
pub async fn mount(mountpoint: impl AsRef<Path>, mountopts: &[&OsStr]) -> io::Result<Self> {
let mut channel = Channel::open(mountpoint.as_ref(), mountopts)?;
let session = SessionInitializer::default()
.init(&mut channel)
.await?;
Ok(Server {
session: Arc::new(session),
notifier: Arc::new(polyfuse::Notifier::new()),
channel,
})
}
pub fn notifier(&mut self) -> io::Result<Notifier> {
let channel = self.channel.try_clone()?;
Ok(Notifier {
session: self.session.clone(),
notifier: self.notifier.clone(),
channel,
})
}
pub async fn run<F>(self, fs: F) -> io::Result<()>
where
F: Filesystem<Bytes> + Send + 'static,
{
let sig = default_shutdown_signal()?;
let _sig = self.run_until(fs, sig).await?;
Ok(())
}
#[allow(clippy::unnecessary_mut_passed)]
pub async fn run_until<F, S>(self, fs: F, sig: S) -> io::Result<Option<S::Output>>
where
F: Filesystem<Bytes> + Send + 'static,
S: Future + Unpin,
{
let Self {
session,
notifier,
mut channel,
} = self;
let fs = Arc::new(fs);
let mut sig = sig.fuse();
let mut main_loop = Box::pin(async move {
loop {
let mut buf = BytesBuffer::new(session.buffer_size());
if let Err(err) = session.receive(&mut channel, &mut buf, ¬ifier).await {
match err.raw_os_error() {
Some(libc::ENODEV) => {
tracing::debug!("connection was closed by the kernel");
return Ok(());
}
_ => return Err(err),
}
}
let session = session.clone();
let fs = fs.clone();
let mut writer = channel.try_clone()?;
tokio::spawn(async move {
if let Err(e) = session.process(&*fs, &mut buf, &mut writer).await {
tracing::error!("error during handling a request: {}", e);
}
});
}
})
.fuse();
select! {
_ = main_loop => Ok(None),
sig = sig => Ok(Some(sig)),
}
}
}
#[derive(Debug)]
pub struct Notifier {
session: Arc<Session>,
notifier: Arc<polyfuse::Notifier<Bytes>>,
channel: Channel,
}
impl Notifier {
pub fn try_clone(&self) -> io::Result<Self> {
Ok(Self {
session: self.session.clone(),
notifier: self.notifier.clone(),
channel: self.channel.try_clone()?,
})
}
pub async fn inval_inode(&mut self, ino: u64, off: i64, len: i64) -> io::Result<()> {
self.notifier
.inval_inode(&mut self.channel, &*self.session, ino, off, len)
.await
}
pub async fn inval_entry(&mut self, parent: u64, name: impl AsRef<OsStr>) -> io::Result<()> {
self.notifier
.inval_entry(&mut self.channel, &*self.session, parent, name)
.await
}
pub async fn delete(
&mut self,
parent: u64,
child: u64,
name: impl AsRef<OsStr>,
) -> io::Result<()> {
self.notifier
.delete(&mut self.channel, &*self.session, parent, child, name)
.await
}
pub async fn store(&mut self, ino: u64, offset: u64, data: &[&[u8]]) -> io::Result<()> {
self.notifier
.store(&mut self.channel, &*self.session, ino, offset, data)
.await
}
pub async fn retrieve(&mut self, ino: u64, offset: u64, size: u32) -> io::Result<Bytes> {
let handle = self
.notifier
.retrieve(&mut self.channel, &*self.session, ino, offset, size)
.await?;
let (in_offset, data) = handle.await;
debug_assert_eq!(offset, in_offset);
Ok(data)
}
pub async fn poll_wakeup(&mut self, kh: u64) -> io::Result<()> {
self.notifier
.poll_wakeup(&mut self.channel, &*self.session, kh)
.await
}
}
#[allow(clippy::unnecessary_mut_passed)]
fn default_shutdown_signal() -> io::Result<impl Future<Output = c_int> + Unpin> {
let mut sighup = signal(SignalKind::hangup())?;
let mut sigint = signal(SignalKind::interrupt())?;
let mut sigterm = signal(SignalKind::terminate())?;
let mut sigpipe = signal(SignalKind::pipe())?;
Ok(Box::pin(async move {
let mut sighup = Box::pin(sighup.recv()).fuse();
let mut sigint = Box::pin(sigint.recv()).fuse();
let mut sigterm = Box::pin(sigterm.recv()).fuse();
let mut sigpipe = Box::pin(sigpipe.recv()).fuse();
loop {
select! {
_ = sighup => {
tracing::debug!("Got SIGHUP");
return libc::SIGHUP;
},
_ = sigint => {
tracing::debug!("Got SIGINT");
return libc::SIGINT;
},
_ = sigterm => {
tracing::debug!("Got SIGTERM");
return libc::SIGTERM;
},
_ = sigpipe => {
tracing::debug!("Got SIGPIPE (and ignored)");
continue
}
}
}
}))
}