@@ -3,7 +3,9 @@ mod unpriv_mount;
33
44use self :: { priv_mount:: PrivMount , unpriv_mount:: UnprivMount } ;
55use bitflags:: { bitflags, bitflags_match} ;
6+ use libc:: { SIGHUP , SIGINT , SIGTERM } ;
67use rustix:: { io:: Errno , mount:: MountFlags } ;
8+ use signal_hook:: iterator:: Signals ;
79use 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-
196206pub ( 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