File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -232,11 +232,14 @@ impl UnixDatagram {
232232
233233pub struct UnixListener {
234234 inner : Inner ,
235+ path : CString ,
235236}
236237
237238impl UnixListener {
238239 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+ } )
240243 }
241244
242245 fn fd ( & self ) -> fd_t { self . inner . fd }
@@ -283,3 +286,14 @@ impl rtio::RtioUnixAcceptor for UnixAcceptor {
283286 self . native_accept ( ) . map ( |s| ~s as ~rtio:: RtioPipe : Send )
284287 }
285288}
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 {
355355
356356 rx. recv( ) ;
357357 } )
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) ) ] )
358374}
You can’t perform that action at this time.
0 commit comments