Skip to content

native: Unlink unix socket paths on drop #13690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/libnative/io/pipe_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,14 @@ impl UnixDatagram {

pub struct UnixListener {
inner: Inner,
path: CString,
}

impl 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 }
Expand Down Expand Up @@ -283,3 +286,14 @@ impl rtio::RtioUnixAcceptor for UnixAcceptor {
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));
}
}
}
16 changes: 16 additions & 0 deletions src/libstd/io/net/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,20 @@ mod tests {

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))])
}