File tree 2 files changed +31
-1
lines changed
2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -232,11 +232,14 @@ impl UnixDatagram {
232
232
233
233
pub struct UnixListener {
234
234
inner : Inner ,
235
+ path : CString ,
235
236
}
236
237
237
238
impl UnixListener {
238
239
pub fn bind ( addr : & CString ) -> IoResult < UnixListener > {
239
- bind ( addr, libc:: SOCK_STREAM ) . map ( |fd| UnixListener { inner : fd } )
240
+ bind ( addr, libc:: SOCK_STREAM ) . map ( |fd| {
241
+ UnixListener { inner : fd, path : addr. clone ( ) }
242
+ } )
240
243
}
241
244
242
245
fn fd ( & self ) -> fd_t { self . inner . fd }
@@ -283,3 +286,14 @@ impl rtio::RtioUnixAcceptor for UnixAcceptor {
283
286
self . native_accept ( ) . map ( |s| ~s as ~rtio:: RtioPipe : Send )
284
287
}
285
288
}
289
+
290
+ impl Drop for UnixListener {
291
+ fn drop ( & mut self ) {
292
+ // Unlink the path to the socket to ensure that it doesn't linger. We're
293
+ // careful to unlink the path before we close the file descriptor to
294
+ // prevent races where we unlink someone else's path.
295
+ unsafe {
296
+ let _ = libc:: unlink ( self . path . with_ref ( |p| p) ) ;
297
+ }
298
+ }
299
+ }
Original file line number Diff line number Diff line change @@ -355,4 +355,20 @@ mod tests {
355
355
356
356
rx. recv( ) ;
357
357
} )
358
+
359
+ iotest ! ( fn drop_removes_listener_path( ) {
360
+ let path = next_test_unix( ) ;
361
+ let l = UnixListener :: bind( & path) . unwrap( ) ;
362
+ assert!( path. exists( ) ) ;
363
+ drop( l) ;
364
+ assert!( !path. exists( ) ) ;
365
+ } #[ cfg( not( windows) ) ] )
366
+
367
+ iotest ! ( fn drop_removes_acceptor_path( ) {
368
+ let path = next_test_unix( ) ;
369
+ let l = UnixListener :: bind( & path) . unwrap( ) ;
370
+ assert!( path. exists( ) ) ;
371
+ drop( l. listen( ) . unwrap( ) ) ;
372
+ assert!( !path. exists( ) ) ;
373
+ } #[ cfg( not( windows) ) ] )
358
374
}
You can’t perform that action at this time.
0 commit comments