1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 use std::c_str::CString;
17 use std::rt::rtio::{IoResult, IoError};
23 use super::file::fd_t;
25 fn unix_socket(ty: libc::c_int) -> IoResult<fd_t> {
26 match unsafe { libc::socket(libc::AF_UNIX, ty, 0) } {
27 -1 => Err(super::last_error()),
32 fn addr_to_sockaddr_un(addr: &CString) -> IoResult<(libc::sockaddr_storage, uint)> {
33 // the sun_path length is limited to SUN_LEN (with null)
34 assert!(mem::size_of::<libc::sockaddr_storage>() >=
35 mem::size_of::<libc::sockaddr_un>());
36 let mut storage: libc::sockaddr_storage = unsafe { mem::zeroed() };
37 let s: &mut libc::sockaddr_un = unsafe { mem::transmute(&mut storage) };
40 if len > s.sun_path.len() - 1 {
41 #[cfg(unix)] use ERROR = libc::EINVAL;
42 #[cfg(windows)] use ERROR = libc::WSAEINVAL;
46 detail: Some("path must be smaller than SUN_LEN".to_string()),
49 s.sun_family = libc::AF_UNIX as libc::sa_family_t;
50 for (slot, value) in s.sun_path.mut_iter().zip(addr.iter()) {
54 // count the null terminator
55 let len = mem::size_of::<libc::sa_family_t>() + len + 1;
56 return Ok((storage, len));
62 // Unused on Linux, where this lock is not necessary.
64 lock: mutex::NativeMutex
68 fn new(fd: fd_t) -> Inner {
69 Inner { fd: fd, lock: unsafe { mutex::NativeMutex::new() } }
74 fn drop(&mut self) { unsafe { let _ = libc::close(self.fd); } }
77 fn connect(addr: &CString, ty: libc::c_int,
78 timeout: Option<u64>) -> IoResult<Inner> {
79 let (addr, len) = try!(addr_to_sockaddr_un(addr));
80 let inner = Inner::new(try!(unix_socket(ty)));
81 let addrp = &addr as *const _ as *const libc::sockaddr;
82 let len = len as libc::socklen_t;
86 match retry(|| unsafe { libc::connect(inner.fd, addrp, len) }) {
87 -1 => Err(super::last_error()),
92 try!(util::connect_timeout(inner.fd, addrp, len, timeout_ms));
98 fn bind(addr: &CString, ty: libc::c_int) -> IoResult<Inner> {
99 let (addr, len) = try!(addr_to_sockaddr_un(addr));
100 let inner = Inner::new(try!(unix_socket(ty)));
101 let addrp = &addr as *const _;
103 libc::bind(inner.fd, addrp as *const _, len as libc::socklen_t)
105 -1 => Err(super::last_error()),
110 ////////////////////////////////////////////////////////////////////////////////
112 ////////////////////////////////////////////////////////////////////////////////
114 pub struct UnixStream {
121 pub fn connect(addr: &CString,
122 timeout: Option<u64>) -> IoResult<UnixStream> {
123 connect(addr, libc::SOCK_STREAM, timeout).map(|inner| {
124 UnixStream::new(Arc::new(inner))
128 fn new(inner: Arc<Inner>) -> UnixStream {
136 fn fd(&self) -> fd_t { self.inner.fd }
138 #[cfg(target_os = "linux")]
139 fn lock_nonblocking(&self) {}
141 #[cfg(not(target_os = "linux"))]
142 fn lock_nonblocking<'a>(&'a self) -> net::Guard<'a> {
143 let ret = net::Guard {
145 guard: unsafe { self.inner.lock.lock() },
147 assert!(util::set_nonblocking(self.fd(), true).is_ok());
152 impl rtio::RtioPipe for UnixStream {
153 fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
155 let dolock = || self.lock_nonblocking();
156 let doread = |nb| unsafe {
157 let flags = if nb {c::MSG_DONTWAIT} else {0};
159 buf.as_mut_ptr() as *mut libc::c_void,
160 buf.len() as libc::size_t,
161 flags) as libc::c_int
163 net::read(fd, self.read_deadline, dolock, doread)
166 fn write(&mut self, buf: &[u8]) -> IoResult<()> {
168 let dolock = || self.lock_nonblocking();
169 let dowrite = |nb: bool, buf: *const u8, len: uint| unsafe {
170 let flags = if nb {c::MSG_DONTWAIT} else {0};
172 buf as *mut libc::c_void,
176 match net::write(fd, self.write_deadline, buf, true, dolock, dowrite) {
182 fn clone(&self) -> Box<rtio::RtioPipe + Send> {
183 box UnixStream::new(self.inner.clone()) as Box<rtio::RtioPipe + Send>
186 fn close_write(&mut self) -> IoResult<()> {
187 super::mkerr_libc(unsafe { libc::shutdown(self.fd(), libc::SHUT_WR) })
189 fn close_read(&mut self) -> IoResult<()> {
190 super::mkerr_libc(unsafe { libc::shutdown(self.fd(), libc::SHUT_RD) })
192 fn set_timeout(&mut self, timeout: Option<u64>) {
193 let deadline = timeout.map(|a| ::io::timer::now() + a).unwrap_or(0);
194 self.read_deadline = deadline;
195 self.write_deadline = deadline;
197 fn set_read_timeout(&mut self, timeout: Option<u64>) {
198 self.read_deadline = timeout.map(|a| ::io::timer::now() + a).unwrap_or(0);
200 fn set_write_timeout(&mut self, timeout: Option<u64>) {
201 self.write_deadline = timeout.map(|a| ::io::timer::now() + a).unwrap_or(0);
205 ////////////////////////////////////////////////////////////////////////////////
207 ////////////////////////////////////////////////////////////////////////////////
209 pub struct UnixListener {
215 pub fn bind(addr: &CString) -> IoResult<UnixListener> {
216 bind(addr, libc::SOCK_STREAM).map(|fd| {
217 UnixListener { inner: fd, path: addr.clone() }
221 fn fd(&self) -> fd_t { self.inner.fd }
223 pub fn native_listen(self, backlog: int) -> IoResult<UnixAcceptor> {
224 match unsafe { libc::listen(self.fd(), backlog as libc::c_int) } {
225 -1 => Err(super::last_error()),
226 _ => Ok(UnixAcceptor { listener: self, deadline: 0 })
231 impl rtio::RtioUnixListener for UnixListener {
232 fn listen(self: Box<UnixListener>)
233 -> IoResult<Box<rtio::RtioUnixAcceptor + Send>> {
234 self.native_listen(128).map(|a| {
235 box a as Box<rtio::RtioUnixAcceptor + Send>
240 pub struct UnixAcceptor {
241 listener: UnixListener,
246 fn fd(&self) -> fd_t { self.listener.fd() }
248 pub fn native_accept(&mut self) -> IoResult<UnixStream> {
249 if self.deadline != 0 {
250 try!(util::await(self.fd(), Some(self.deadline), util::Readable));
252 let mut storage: libc::sockaddr_storage = unsafe { mem::zeroed() };
253 let storagep = &mut storage as *mut libc::sockaddr_storage;
254 let size = mem::size_of::<libc::sockaddr_storage>();
255 let mut size = size as libc::socklen_t;
256 match retry(|| unsafe {
257 libc::accept(self.fd(),
258 storagep as *mut libc::sockaddr,
259 &mut size as *mut libc::socklen_t) as libc::c_int
261 -1 => Err(super::last_error()),
262 fd => Ok(UnixStream::new(Arc::new(Inner::new(fd))))
267 impl rtio::RtioUnixAcceptor for UnixAcceptor {
268 fn accept(&mut self) -> IoResult<Box<rtio::RtioPipe + Send>> {
269 self.native_accept().map(|s| box s as Box<rtio::RtioPipe + Send>)
271 fn set_timeout(&mut self, timeout: Option<u64>) {
272 self.deadline = timeout.map(|a| ::io::timer::now() + a).unwrap_or(0);
276 impl Drop for UnixListener {
278 // Unlink the path to the socket to ensure that it doesn't linger. We're
279 // careful to unlink the path before we close the file descriptor to
280 // prevent races where we unlink someone else's path.
282 let _ = libc::unlink(self.path.as_ptr());