Skip to content

Commit 49b2165

Browse files
committedApr 23, 2014
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.
2 parents 34ff34d + f1fb57a commit 49b2165

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed
 

‎src/libnative/io/pipe_unix.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,14 @@ impl UnixDatagram {
232232

233233
pub struct UnixListener {
234234
inner: Inner,
235+
path: CString,
235236
}
236237

237238
impl 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+
}

‎src/libstd/io/net/unix.rs

+16
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)