1
Fork 0

auto merge of #13690 : alexcrichton/rust/unlink-unix-pipe, r=brson

This prevents unix sockets from remaining on the system all over the place, and
more closely mirrors the behavior of libuv and windows pipes.
This commit is contained in:
bors 2014-04-23 02:11:32 -07:00
commit 49b216539f
2 changed files with 31 additions and 1 deletions

View file

@ -232,11 +232,14 @@ impl UnixDatagram {
pub struct UnixListener { pub struct UnixListener {
inner: Inner, inner: Inner,
path: CString,
} }
impl UnixListener { impl UnixListener {
pub fn bind(addr: &CString) -> IoResult<UnixListener> { pub fn bind(addr: &CString) -> IoResult<UnixListener> {
bind(addr, libc::SOCK_STREAM).map(|fd| UnixListener { inner: fd }) bind(addr, libc::SOCK_STREAM).map(|fd| {
UnixListener { inner: fd, path: addr.clone() }
})
} }
fn fd(&self) -> fd_t { self.inner.fd } fn fd(&self) -> fd_t { self.inner.fd }
@ -283,3 +286,14 @@ impl rtio::RtioUnixAcceptor for UnixAcceptor {
self.native_accept().map(|s| ~s as ~rtio::RtioPipe:Send) self.native_accept().map(|s| ~s as ~rtio::RtioPipe:Send)
} }
} }
impl Drop for UnixListener {
fn drop(&mut self) {
// Unlink the path to the socket to ensure that it doesn't linger. We're
// careful to unlink the path before we close the file descriptor to
// prevent races where we unlink someone else's path.
unsafe {
let _ = libc::unlink(self.path.with_ref(|p| p));
}
}
}

View file

@ -355,4 +355,20 @@ mod tests {
rx.recv(); rx.recv();
}) })
iotest!(fn drop_removes_listener_path() {
let path = next_test_unix();
let l = UnixListener::bind(&path).unwrap();
assert!(path.exists());
drop(l);
assert!(!path.exists());
} #[cfg(not(windows))])
iotest!(fn drop_removes_acceptor_path() {
let path = next_test_unix();
let l = UnixListener::bind(&path).unwrap();
assert!(path.exists());
drop(l.listen().unwrap());
assert!(!path.exists());
} #[cfg(not(windows))])
} }