2014-02-07 18:10:48 +00:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-04-23 01:38:59 +00:00
|
|
|
use libc;
|
2014-02-07 18:37:58 +00:00
|
|
|
use std::c_str::CString;
|
2014-04-23 01:38:59 +00:00
|
|
|
use std::intrinsics;
|
2014-02-07 18:37:58 +00:00
|
|
|
use std::io;
|
|
|
|
use std::mem;
|
|
|
|
use std::rt::rtio;
|
|
|
|
use std::sync::arc::UnsafeArc;
|
2014-04-26 03:50:22 +00:00
|
|
|
use std::unstable::mutex;
|
2014-02-07 18:37:58 +00:00
|
|
|
|
2014-04-26 03:50:22 +00:00
|
|
|
use super::{IoResult, retry};
|
|
|
|
use super::net;
|
2014-04-23 01:38:59 +00:00
|
|
|
use super::util;
|
2014-04-26 03:50:22 +00:00
|
|
|
use super::c;
|
2014-02-26 20:57:00 +00:00
|
|
|
use super::file::fd_t;
|
2014-02-07 18:37:58 +00:00
|
|
|
|
|
|
|
fn unix_socket(ty: libc::c_int) -> IoResult<fd_t> {
|
|
|
|
match unsafe { libc::socket(libc::AF_UNIX, ty, 0) } {
|
|
|
|
-1 => Err(super::last_error()),
|
|
|
|
fd => Ok(fd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn addr_to_sockaddr_un(addr: &CString) -> IoResult<(libc::sockaddr_storage, uint)> {
|
|
|
|
// the sun_path length is limited to SUN_LEN (with null)
|
|
|
|
assert!(mem::size_of::<libc::sockaddr_storage>() >=
|
|
|
|
mem::size_of::<libc::sockaddr_un>());
|
|
|
|
let mut storage: libc::sockaddr_storage = unsafe { intrinsics::init() };
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 17:34:51 +00:00
|
|
|
let s: &mut libc::sockaddr_un = unsafe { mem::transmute(&mut storage) };
|
2014-02-07 18:37:58 +00:00
|
|
|
|
|
|
|
let len = addr.len();
|
|
|
|
if len > s.sun_path.len() - 1 {
|
|
|
|
return Err(io::IoError {
|
|
|
|
kind: io::InvalidInput,
|
|
|
|
desc: "path must be smaller than SUN_LEN",
|
|
|
|
detail: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
s.sun_family = libc::AF_UNIX as libc::sa_family_t;
|
|
|
|
for (slot, value) in s.sun_path.mut_iter().zip(addr.iter()) {
|
|
|
|
*slot = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// count the null terminator
|
|
|
|
let len = mem::size_of::<libc::sa_family_t>() + len + 1;
|
|
|
|
return Ok((storage, len));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Inner {
|
|
|
|
fd: fd_t,
|
2014-04-26 03:50:22 +00:00
|
|
|
lock: mutex::NativeMutex,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Inner {
|
|
|
|
fn new(fd: fd_t) -> Inner {
|
|
|
|
Inner { fd: fd, lock: unsafe { mutex::NativeMutex::new() } }
|
|
|
|
}
|
2014-02-07 18:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Inner {
|
|
|
|
fn drop(&mut self) { unsafe { let _ = libc::close(self.fd); } }
|
|
|
|
}
|
|
|
|
|
2014-04-23 01:38:59 +00:00
|
|
|
fn connect(addr: &CString, ty: libc::c_int,
|
|
|
|
timeout: Option<u64>) -> IoResult<Inner> {
|
2014-02-19 18:07:49 +00:00
|
|
|
let (addr, len) = try!(addr_to_sockaddr_un(addr));
|
2014-04-26 03:50:22 +00:00
|
|
|
let inner = Inner::new(try!(unix_socket(ty)));
|
2014-04-23 01:38:59 +00:00
|
|
|
let addrp = &addr as *_ as *libc::sockaddr;
|
|
|
|
let len = len as libc::socklen_t;
|
|
|
|
|
|
|
|
match timeout {
|
|
|
|
None => {
|
|
|
|
match retry(|| unsafe { libc::connect(inner.fd, addrp, len) }) {
|
|
|
|
-1 => Err(super::last_error()),
|
|
|
|
_ => Ok(inner)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(timeout_ms) => {
|
|
|
|
try!(util::connect_timeout(inner.fd, addrp, len, timeout_ms));
|
|
|
|
Ok(inner)
|
|
|
|
}
|
2014-02-07 18:37:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bind(addr: &CString, ty: libc::c_int) -> IoResult<Inner> {
|
2014-02-19 18:07:49 +00:00
|
|
|
let (addr, len) = try!(addr_to_sockaddr_un(addr));
|
2014-04-26 03:50:22 +00:00
|
|
|
let inner = Inner::new(try!(unix_socket(ty)));
|
2014-02-07 18:37:58 +00:00
|
|
|
let addrp = &addr as *libc::sockaddr_storage;
|
|
|
|
match unsafe {
|
|
|
|
libc::bind(inner.fd, addrp as *libc::sockaddr, len as libc::socklen_t)
|
|
|
|
} {
|
|
|
|
-1 => Err(super::last_error()),
|
|
|
|
_ => Ok(inner)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 18:10:48 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2014-02-07 18:37:58 +00:00
|
|
|
// Unix Streams
|
2014-02-07 18:10:48 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
pub struct UnixStream {
|
2014-03-27 22:10:28 +00:00
|
|
|
inner: UnsafeArc<Inner>,
|
2014-04-26 03:50:22 +00:00
|
|
|
read_deadline: u64,
|
|
|
|
write_deadline: u64,
|
2014-02-07 18:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl UnixStream {
|
2014-04-23 01:38:59 +00:00
|
|
|
pub fn connect(addr: &CString,
|
|
|
|
timeout: Option<u64>) -> IoResult<UnixStream> {
|
|
|
|
connect(addr, libc::SOCK_STREAM, timeout).map(|inner| {
|
2014-04-26 03:50:22 +00:00
|
|
|
UnixStream::new(UnsafeArc::new(inner))
|
2014-02-07 18:10:48 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-04-26 03:50:22 +00:00
|
|
|
fn new(inner: UnsafeArc<Inner>) -> UnixStream {
|
|
|
|
UnixStream {
|
|
|
|
inner: inner,
|
|
|
|
read_deadline: 0,
|
|
|
|
write_deadline: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 18:37:58 +00:00
|
|
|
fn fd(&self) -> fd_t { unsafe { (*self.inner.get()).fd } }
|
2014-04-26 03:50:22 +00:00
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
fn lock_nonblocking(&self) {}
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "linux"))]
|
|
|
|
fn lock_nonblocking<'a>(&'a self) -> net::Guard<'a> {
|
|
|
|
let ret = net::Guard {
|
|
|
|
fd: self.fd(),
|
|
|
|
guard: unsafe { (*self.inner.get()).lock.lock() },
|
|
|
|
};
|
|
|
|
assert!(util::set_nonblocking(self.fd(), true).is_ok());
|
|
|
|
ret
|
|
|
|
}
|
2014-02-07 18:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl rtio::RtioPipe for UnixStream {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
|
2014-04-26 03:50:22 +00:00
|
|
|
let fd = self.fd();
|
|
|
|
let dolock = || self.lock_nonblocking();
|
|
|
|
let doread = |nb| unsafe {
|
|
|
|
let flags = if nb {c::MSG_DONTWAIT} else {0};
|
|
|
|
libc::recv(fd,
|
|
|
|
buf.as_mut_ptr() as *mut libc::c_void,
|
2014-02-07 18:37:58 +00:00
|
|
|
buf.len() as libc::size_t,
|
2014-04-26 03:50:22 +00:00
|
|
|
flags) as libc::c_int
|
|
|
|
};
|
|
|
|
net::read(fd, self.read_deadline, dolock, doread)
|
2014-02-07 18:10:48 +00:00
|
|
|
}
|
2014-02-07 18:37:58 +00:00
|
|
|
|
2014-02-07 18:10:48 +00:00
|
|
|
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
|
2014-04-26 03:50:22 +00:00
|
|
|
let fd = self.fd();
|
|
|
|
let dolock = || self.lock_nonblocking();
|
|
|
|
let dowrite = |nb: bool, buf: *u8, len: uint| unsafe {
|
|
|
|
let flags = if nb {c::MSG_DONTWAIT} else {0};
|
|
|
|
libc::send(fd,
|
2014-02-07 18:37:58 +00:00
|
|
|
buf as *mut libc::c_void,
|
|
|
|
len as libc::size_t,
|
2014-05-08 06:39:56 +00:00
|
|
|
flags) as i64
|
2014-04-26 03:50:22 +00:00
|
|
|
};
|
|
|
|
match net::write(fd, self.write_deadline, buf, true, dolock, dowrite) {
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(e) => Err(e)
|
2014-02-07 18:10:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-06 01:56:44 +00:00
|
|
|
fn clone(&self) -> Box<rtio::RtioPipe:Send> {
|
2014-04-26 03:50:22 +00:00
|
|
|
box UnixStream::new(self.inner.clone()) as Box<rtio::RtioPipe:Send>
|
2014-02-07 18:37:58 +00:00
|
|
|
}
|
2014-04-25 01:48:21 +00:00
|
|
|
|
|
|
|
fn close_write(&mut self) -> IoResult<()> {
|
|
|
|
super::mkerr_libc(unsafe { libc::shutdown(self.fd(), libc::SHUT_WR) })
|
|
|
|
}
|
|
|
|
fn close_read(&mut self) -> IoResult<()> {
|
|
|
|
super::mkerr_libc(unsafe { libc::shutdown(self.fd(), libc::SHUT_RD) })
|
|
|
|
}
|
2014-04-26 03:50:22 +00:00
|
|
|
fn set_timeout(&mut self, timeout: Option<u64>) {
|
|
|
|
let deadline = timeout.map(|a| ::io::timer::now() + a).unwrap_or(0);
|
|
|
|
self.read_deadline = deadline;
|
|
|
|
self.write_deadline = deadline;
|
|
|
|
}
|
|
|
|
fn set_read_timeout(&mut self, timeout: Option<u64>) {
|
|
|
|
self.read_deadline = timeout.map(|a| ::io::timer::now() + a).unwrap_or(0);
|
|
|
|
}
|
|
|
|
fn set_write_timeout(&mut self, timeout: Option<u64>) {
|
|
|
|
self.write_deadline = timeout.map(|a| ::io::timer::now() + a).unwrap_or(0);
|
|
|
|
}
|
2014-02-07 18:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Unix Listener
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
pub struct UnixListener {
|
2014-03-27 22:10:28 +00:00
|
|
|
inner: Inner,
|
2014-04-22 20:21:30 +00:00
|
|
|
path: CString,
|
2014-02-07 18:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl UnixListener {
|
|
|
|
pub fn bind(addr: &CString) -> IoResult<UnixListener> {
|
2014-04-22 20:21:30 +00:00
|
|
|
bind(addr, libc::SOCK_STREAM).map(|fd| {
|
|
|
|
UnixListener { inner: fd, path: addr.clone() }
|
|
|
|
})
|
2014-02-07 18:10:48 +00:00
|
|
|
}
|
|
|
|
|
2014-02-07 18:37:58 +00:00
|
|
|
fn fd(&self) -> fd_t { self.inner.fd }
|
2014-02-07 18:10:48 +00:00
|
|
|
|
|
|
|
pub fn native_listen(self, backlog: int) -> IoResult<UnixAcceptor> {
|
2014-02-07 18:37:58 +00:00
|
|
|
match unsafe { libc::listen(self.fd(), backlog as libc::c_int) } {
|
2014-02-07 18:10:48 +00:00
|
|
|
-1 => Err(super::last_error()),
|
2014-04-23 01:38:59 +00:00
|
|
|
_ => Ok(UnixAcceptor { listener: self, deadline: 0 })
|
2014-02-07 18:10:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl rtio::RtioUnixListener for UnixListener {
|
2014-05-06 01:56:44 +00:00
|
|
|
fn listen(~self) -> IoResult<Box<rtio::RtioUnixAcceptor:Send>> {
|
|
|
|
self.native_listen(128).map(|a| {
|
|
|
|
box a as Box<rtio::RtioUnixAcceptor:Send>
|
|
|
|
})
|
2014-02-07 18:10:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct UnixAcceptor {
|
2014-03-27 22:10:28 +00:00
|
|
|
listener: UnixListener,
|
2014-04-23 01:38:59 +00:00
|
|
|
deadline: u64,
|
2014-02-07 18:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl UnixAcceptor {
|
2014-02-07 18:37:58 +00:00
|
|
|
fn fd(&self) -> fd_t { self.listener.fd() }
|
2014-02-07 18:10:48 +00:00
|
|
|
|
|
|
|
pub fn native_accept(&mut self) -> IoResult<UnixStream> {
|
2014-04-23 01:38:59 +00:00
|
|
|
if self.deadline != 0 {
|
2014-04-26 03:50:22 +00:00
|
|
|
try!(util::await(self.fd(), Some(self.deadline), util::Readable));
|
2014-04-23 01:38:59 +00:00
|
|
|
}
|
2014-02-07 18:10:48 +00:00
|
|
|
let mut storage: libc::sockaddr_storage = unsafe { intrinsics::init() };
|
|
|
|
let storagep = &mut storage as *mut libc::sockaddr_storage;
|
|
|
|
let size = mem::size_of::<libc::sockaddr_storage>();
|
|
|
|
let mut size = size as libc::socklen_t;
|
|
|
|
match retry(|| unsafe {
|
|
|
|
libc::accept(self.fd(),
|
|
|
|
storagep as *mut libc::sockaddr,
|
|
|
|
&mut size as *mut libc::socklen_t) as libc::c_int
|
2014-02-07 18:37:58 +00:00
|
|
|
}) {
|
2014-02-07 18:10:48 +00:00
|
|
|
-1 => Err(super::last_error()),
|
2014-04-26 03:50:22 +00:00
|
|
|
fd => Ok(UnixStream::new(UnsafeArc::new(Inner::new(fd))))
|
2014-02-07 18:10:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl rtio::RtioUnixAcceptor for UnixAcceptor {
|
2014-05-06 01:56:44 +00:00
|
|
|
fn accept(&mut self) -> IoResult<Box<rtio::RtioPipe:Send>> {
|
|
|
|
self.native_accept().map(|s| box s as Box<rtio::RtioPipe:Send>)
|
2014-02-07 18:10:48 +00:00
|
|
|
}
|
2014-04-23 01:38:59 +00:00
|
|
|
fn set_timeout(&mut self, timeout: Option<u64>) {
|
|
|
|
self.deadline = timeout.map(|a| ::io::timer::now() + a).unwrap_or(0);
|
|
|
|
}
|
2014-02-07 18:10:48 +00:00
|
|
|
}
|
2014-04-22 20:21:30 +00:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|