1 // Copyright 2016 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.
11 #![stable(feature = "unix_socket", since = "1.10.0")]
13 //! Unix-specific networking functionality
23 use os::unix::ffi::OsStrExt;
24 use os::unix::io::{RawFd, AsRawFd, FromRawFd, IntoRawFd};
29 use sys_common::{AsInner, FromInner, IntoInner};
31 #[cfg(any(target_os = "linux", target_os = "android",
32 target_os = "dragonfly", target_os = "freebsd",
33 target_os = "openbsd", target_os = "netbsd",
34 target_os = "haiku", target_os = "bitrig"))]
35 use libc::MSG_NOSIGNAL;
36 #[cfg(not(any(target_os = "linux", target_os = "android",
37 target_os = "dragonfly", target_os = "freebsd",
38 target_os = "openbsd", target_os = "netbsd",
39 target_os = "haiku", target_os = "bitrig")))]
40 const MSG_NOSIGNAL: libc::c_int = 0x0;
42 fn sun_path_offset() -> usize {
44 // Work with an actual instance of the type since using a null pointer is UB
45 let addr: libc::sockaddr_un = mem::uninitialized();
46 let base = &addr as *const _ as usize;
47 let path = &addr.sun_path as *const _ as usize;
52 unsafe fn sockaddr_un(path: &Path) -> io::Result<(libc::sockaddr_un, libc::socklen_t)> {
53 let mut addr: libc::sockaddr_un = mem::zeroed();
54 addr.sun_family = libc::AF_UNIX as libc::sa_family_t;
56 let bytes = path.as_os_str().as_bytes();
58 if bytes.contains(&0) {
59 return Err(io::Error::new(io::ErrorKind::InvalidInput,
60 "paths may not contain interior null bytes"));
63 if bytes.len() >= addr.sun_path.len() {
64 return Err(io::Error::new(io::ErrorKind::InvalidInput,
65 "path must be shorter than SUN_LEN"));
67 for (dst, src) in addr.sun_path.iter_mut().zip(bytes.iter()) {
68 *dst = *src as libc::c_char;
70 // null byte for pathname addresses is already there because we zeroed the
73 let mut len = sun_path_offset() + bytes.len();
78 Ok((addr, len as libc::socklen_t))
81 enum AddressKind<'a> {
87 /// An address associated with a Unix socket.
92 /// use std::os::unix::net::UnixListener;
94 /// let socket = match UnixListener::bind("/tmp/sock") {
97 /// println!("Couldn't bind: {:?}", e);
101 /// let addr = socket.local_addr().expect("Couldn't get local address");
104 #[stable(feature = "unix_socket", since = "1.10.0")]
105 pub struct SocketAddr {
106 addr: libc::sockaddr_un,
107 len: libc::socklen_t,
111 fn new<F>(f: F) -> io::Result<SocketAddr>
112 where F: FnOnce(*mut libc::sockaddr, *mut libc::socklen_t) -> libc::c_int
115 let mut addr: libc::sockaddr_un = mem::zeroed();
116 let mut len = mem::size_of::<libc::sockaddr_un>() as libc::socklen_t;
117 cvt(f(&mut addr as *mut _ as *mut _, &mut len))?;
118 SocketAddr::from_parts(addr, len)
122 fn from_parts(addr: libc::sockaddr_un, mut len: libc::socklen_t) -> io::Result<SocketAddr> {
124 // When there is a datagram from unnamed unix socket
125 // linux returns zero bytes of address
126 len = sun_path_offset() as libc::socklen_t; // i.e. zero-length address
127 } else if addr.sun_family != libc::AF_UNIX as libc::sa_family_t {
128 return Err(io::Error::new(io::ErrorKind::InvalidInput,
129 "file descriptor did not correspond to a Unix socket"));
138 /// Returns true if and only if the address is unnamed.
145 /// use std::os::unix::net::UnixListener;
147 /// let socket = UnixListener::bind("/tmp/sock").unwrap();
148 /// let addr = socket.local_addr().expect("Couldn't get local address");
149 /// assert_eq!(addr.is_unnamed(), false);
152 /// An unnamed address:
155 /// use std::os::unix::net::UnixDatagram;
157 /// let socket = UnixDatagram::unbound().unwrap();
158 /// let addr = socket.local_addr().expect("Couldn't get local address");
159 /// assert_eq!(addr.is_unnamed(), true);
161 #[stable(feature = "unix_socket", since = "1.10.0")]
162 pub fn is_unnamed(&self) -> bool {
163 if let AddressKind::Unnamed = self.address() {
170 /// Returns the contents of this address if it is a `pathname` address.
177 /// use std::os::unix::net::UnixListener;
178 /// use std::path::Path;
180 /// let socket = UnixListener::bind("/tmp/sock").unwrap();
181 /// let addr = socket.local_addr().expect("Couldn't get local address");
182 /// assert_eq!(addr.as_pathname(), Some(Path::new("/tmp/sock")));
185 /// Without a pathname:
188 /// use std::os::unix::net::UnixDatagram;
190 /// let socket = UnixDatagram::unbound().unwrap();
191 /// let addr = socket.local_addr().expect("Couldn't get local address");
192 /// assert_eq!(addr.as_pathname(), None);
194 #[stable(feature = "unix_socket", since = "1.10.0")]
195 pub fn as_pathname(&self) -> Option<&Path> {
196 if let AddressKind::Pathname(path) = self.address() {
203 fn address<'a>(&'a self) -> AddressKind<'a> {
204 let len = self.len as usize - sun_path_offset();
205 let path = unsafe { mem::transmute::<&[libc::c_char], &[u8]>(&self.addr.sun_path) };
207 // macOS seems to return a len of 16 and a zeroed sun_path for unnamed addresses
208 if len == 0 || (cfg!(not(target_os = "linux")) && self.addr.sun_path[0] == 0) {
210 } else if self.addr.sun_path[0] == 0 {
211 AddressKind::Abstract(&path[1..len])
213 AddressKind::Pathname(OsStr::from_bytes(&path[..len - 1]).as_ref())
218 #[stable(feature = "unix_socket", since = "1.10.0")]
219 impl fmt::Debug for SocketAddr {
220 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
221 match self.address() {
222 AddressKind::Unnamed => write!(fmt, "(unnamed)"),
223 AddressKind::Abstract(name) => write!(fmt, "{} (abstract)", AsciiEscaped(name)),
224 AddressKind::Pathname(path) => write!(fmt, "{:?} (pathname)", path),
229 struct AsciiEscaped<'a>(&'a [u8]);
231 impl<'a> fmt::Display for AsciiEscaped<'a> {
232 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
234 for byte in self.0.iter().cloned().flat_map(ascii::escape_default) {
235 write!(fmt, "{}", byte as char)?;
241 /// A Unix stream socket.
246 /// use std::os::unix::net::UnixStream;
247 /// use std::io::prelude::*;
249 /// let mut stream = UnixStream::connect("/path/to/my/socket").unwrap();
250 /// stream.write_all(b"hello world").unwrap();
251 /// let mut response = String::new();
252 /// stream.read_to_string(&mut response).unwrap();
253 /// println!("{}", response);
255 #[stable(feature = "unix_socket", since = "1.10.0")]
256 pub struct UnixStream(Socket);
258 #[stable(feature = "unix_socket", since = "1.10.0")]
259 impl fmt::Debug for UnixStream {
260 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
261 let mut builder = fmt.debug_struct("UnixStream");
262 builder.field("fd", self.0.as_inner());
263 if let Ok(addr) = self.local_addr() {
264 builder.field("local", &addr);
266 if let Ok(addr) = self.peer_addr() {
267 builder.field("peer", &addr);
274 /// Connects to the socket named by `path`.
279 /// use std::os::unix::net::UnixStream;
281 /// let socket = match UnixStream::connect("/tmp/sock") {
282 /// Ok(sock) => sock,
284 /// println!("Couldn't connect: {:?}", e);
289 #[stable(feature = "unix_socket", since = "1.10.0")]
290 pub fn connect<P: AsRef<Path>>(path: P) -> io::Result<UnixStream> {
291 fn inner(path: &Path) -> io::Result<UnixStream> {
293 let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
294 let (addr, len) = sockaddr_un(path)?;
296 cvt(libc::connect(*inner.as_inner(), &addr as *const _ as *const _, len))?;
297 Ok(UnixStream(inner))
303 /// Creates an unnamed pair of connected sockets.
305 /// Returns two `UnixStream`s which are connected to each other.
310 /// use std::os::unix::net::UnixStream;
312 /// let (sock1, sock2) = match UnixStream::pair() {
313 /// Ok((sock1, sock2)) => (sock1, sock2),
315 /// println!("Couldn't create a pair of sockets: {:?}", e);
320 #[stable(feature = "unix_socket", since = "1.10.0")]
321 pub fn pair() -> io::Result<(UnixStream, UnixStream)> {
322 let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_STREAM)?;
323 Ok((UnixStream(i1), UnixStream(i2)))
326 /// Creates a new independently owned handle to the underlying socket.
328 /// The returned `UnixStream` is a reference to the same stream that this
329 /// object references. Both handles will read and write the same stream of
330 /// data, and options set on one stream will be propogated to the other
336 /// use std::os::unix::net::UnixStream;
338 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
339 /// let sock_copy = socket.try_clone().expect("Couldn't clone socket");
341 #[stable(feature = "unix_socket", since = "1.10.0")]
342 pub fn try_clone(&self) -> io::Result<UnixStream> {
343 self.0.duplicate().map(UnixStream)
346 /// Returns the socket address of the local half of this connection.
351 /// use std::os::unix::net::UnixStream;
353 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
354 /// let addr = socket.local_addr().expect("Couldn't get local address");
356 #[stable(feature = "unix_socket", since = "1.10.0")]
357 pub fn local_addr(&self) -> io::Result<SocketAddr> {
358 SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
361 /// Returns the socket address of the remote half of this connection.
366 /// use std::os::unix::net::UnixStream;
368 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
369 /// let addr = socket.peer_addr().expect("Couldn't get peer address");
371 #[stable(feature = "unix_socket", since = "1.10.0")]
372 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
373 SocketAddr::new(|addr, len| unsafe { libc::getpeername(*self.0.as_inner(), addr, len) })
376 /// Sets the read timeout for the socket.
378 /// If the provided value is [`None`], then [`read`] calls will block
379 /// indefinitely. It is an error to pass the zero [`Duration`] to this
382 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
383 /// [`read`]: ../../../../std/io/trait.Read.html#tymethod.read
384 /// [`Duration`]: ../../../../std/time/struct.Duration.html
389 /// use std::os::unix::net::UnixStream;
390 /// use std::time::Duration;
392 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
393 /// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
395 #[stable(feature = "unix_socket", since = "1.10.0")]
396 pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
397 self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
400 /// Sets the write timeout for the socket.
402 /// If the provided value is [`None`], then [`write`] calls will block
403 /// indefinitely. It is an error to pass the zero [`Duration`] to this
406 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
407 /// [`read`]: ../../../../std/io/trait.Write.html#tymethod.write
408 /// [`Duration`]: ../../../../std/time/struct.Duration.html
413 /// use std::os::unix::net::UnixStream;
414 /// use std::time::Duration;
416 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
417 /// socket.set_write_timeout(Some(Duration::new(1, 0))).expect("Couldn't set write timeout");
419 #[stable(feature = "unix_socket", since = "1.10.0")]
420 pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
421 self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
424 /// Returns the read timeout of this socket.
429 /// use std::os::unix::net::UnixStream;
430 /// use std::time::Duration;
432 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
433 /// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
434 /// assert_eq!(socket.read_timeout().unwrap(), Some(Duration::new(1, 0)));
436 #[stable(feature = "unix_socket", since = "1.10.0")]
437 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
438 self.0.timeout(libc::SO_RCVTIMEO)
441 /// Returns the write timeout of this socket.
446 /// use std::os::unix::net::UnixStream;
447 /// use std::time::Duration;
449 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
450 /// socket.set_write_timeout(Some(Duration::new(1, 0))).expect("Couldn't set write timeout");
451 /// assert_eq!(socket.write_timeout().unwrap(), Some(Duration::new(1, 0)));
453 #[stable(feature = "unix_socket", since = "1.10.0")]
454 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
455 self.0.timeout(libc::SO_SNDTIMEO)
458 /// Moves the socket into or out of nonblocking mode.
463 /// use std::os::unix::net::UnixStream;
465 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
466 /// socket.set_nonblocking(true).expect("Couldn't set nonblocking");
468 #[stable(feature = "unix_socket", since = "1.10.0")]
469 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
470 self.0.set_nonblocking(nonblocking)
473 /// Returns the value of the `SO_ERROR` option.
478 /// use std::os::unix::net::UnixStream;
480 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
481 /// if let Ok(Some(err)) = socket.take_error() {
482 /// println!("Got error: {:?}", err);
485 #[stable(feature = "unix_socket", since = "1.10.0")]
486 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
490 /// Shuts down the read, write, or both halves of this connection.
492 /// This function will cause all pending and future I/O calls on the
493 /// specified portions to immediately return with an appropriate value
494 /// (see the documentation of [`Shutdown`]).
496 /// [`Shutdown`]: ../../../../std/net/enum.Shutdown.html
501 /// use std::os::unix::net::UnixStream;
502 /// use std::net::Shutdown;
504 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
505 /// socket.shutdown(Shutdown::Both).expect("shutdown function failed");
507 #[stable(feature = "unix_socket", since = "1.10.0")]
508 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
513 #[stable(feature = "unix_socket", since = "1.10.0")]
514 impl io::Read for UnixStream {
515 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
516 io::Read::read(&mut &*self, buf)
519 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
520 io::Read::read_to_end(&mut &*self, buf)
524 #[stable(feature = "unix_socket", since = "1.10.0")]
525 impl<'a> io::Read for &'a UnixStream {
526 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
530 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
531 self.0.read_to_end(buf)
535 #[stable(feature = "unix_socket", since = "1.10.0")]
536 impl io::Write for UnixStream {
537 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
538 io::Write::write(&mut &*self, buf)
541 fn flush(&mut self) -> io::Result<()> {
542 io::Write::flush(&mut &*self)
546 #[stable(feature = "unix_socket", since = "1.10.0")]
547 impl<'a> io::Write for &'a UnixStream {
548 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
552 fn flush(&mut self) -> io::Result<()> {
557 #[stable(feature = "unix_socket", since = "1.10.0")]
558 impl AsRawFd for UnixStream {
559 fn as_raw_fd(&self) -> RawFd {
564 #[stable(feature = "unix_socket", since = "1.10.0")]
565 impl FromRawFd for UnixStream {
566 unsafe fn from_raw_fd(fd: RawFd) -> UnixStream {
567 UnixStream(Socket::from_inner(fd))
571 #[stable(feature = "unix_socket", since = "1.10.0")]
572 impl IntoRawFd for UnixStream {
573 fn into_raw_fd(self) -> RawFd {
578 /// A structure representing a Unix domain socket server.
584 /// use std::os::unix::net::{UnixStream, UnixListener};
586 /// fn handle_client(stream: UnixStream) {
590 /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
592 /// // accept connections and process them, spawning a new thread for each one
593 /// for stream in listener.incoming() {
596 /// /* connection succeeded */
597 /// thread::spawn(|| handle_client(stream));
600 /// /* connection failed */
606 #[stable(feature = "unix_socket", since = "1.10.0")]
607 pub struct UnixListener(Socket);
609 #[stable(feature = "unix_socket", since = "1.10.0")]
610 impl fmt::Debug for UnixListener {
611 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
612 let mut builder = fmt.debug_struct("UnixListener");
613 builder.field("fd", self.0.as_inner());
614 if let Ok(addr) = self.local_addr() {
615 builder.field("local", &addr);
622 /// Creates a new `UnixListener` bound to the specified socket.
627 /// use std::os::unix::net::UnixListener;
629 /// let listener = match UnixListener::bind("/path/to/the/socket") {
630 /// Ok(sock) => sock,
632 /// println!("Couldn't connect: {:?}", e);
637 #[stable(feature = "unix_socket", since = "1.10.0")]
638 pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixListener> {
639 fn inner(path: &Path) -> io::Result<UnixListener> {
641 let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
642 let (addr, len) = sockaddr_un(path)?;
644 cvt(libc::bind(*inner.as_inner(), &addr as *const _ as *const _, len))?;
645 cvt(libc::listen(*inner.as_inner(), 128))?;
647 Ok(UnixListener(inner))
653 /// Accepts a new incoming connection to this listener.
655 /// This function will block the calling thread until a new Unix connection
656 /// is established. When established, the corersponding [`UnixStream`] and
657 /// the remote peer's address will be returned.
659 /// [`UnixStream`]: ../../../../std/os/unix/net/struct.UnixStream.html
664 /// use std::os::unix::net::UnixListener;
666 /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
668 /// match listener.accept() {
669 /// Ok((socket, addr)) => println!("Got a client: {:?}", addr),
670 /// Err(e) => println!("accept function failed: {:?}", e),
673 #[stable(feature = "unix_socket", since = "1.10.0")]
674 pub fn accept(&self) -> io::Result<(UnixStream, SocketAddr)> {
675 let mut storage: libc::sockaddr_un = unsafe { mem::zeroed() };
676 let mut len = mem::size_of_val(&storage) as libc::socklen_t;
677 let sock = self.0.accept(&mut storage as *mut _ as *mut _, &mut len)?;
678 let addr = SocketAddr::from_parts(storage, len)?;
679 Ok((UnixStream(sock), addr))
682 /// Creates a new independently owned handle to the underlying socket.
684 /// The returned `UnixListener` is a reference to the same socket that this
685 /// object references. Both handles can be used to accept incoming
686 /// connections and options set on one listener will affect the other.
691 /// use std::os::unix::net::UnixListener;
693 /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
695 /// let listener_copy = listener.try_clone().expect("try_clone failed");
697 #[stable(feature = "unix_socket", since = "1.10.0")]
698 pub fn try_clone(&self) -> io::Result<UnixListener> {
699 self.0.duplicate().map(UnixListener)
702 /// Returns the local socket address of this listener.
707 /// use std::os::unix::net::UnixListener;
709 /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
711 /// let addr = listener.local_addr().expect("Couldn't get local address");
713 #[stable(feature = "unix_socket", since = "1.10.0")]
714 pub fn local_addr(&self) -> io::Result<SocketAddr> {
715 SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
718 /// Moves the socket into or out of nonblocking mode.
723 /// use std::os::unix::net::UnixListener;
725 /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
727 /// listener.set_nonblocking(true).expect("Couldn't set non blocking");
729 #[stable(feature = "unix_socket", since = "1.10.0")]
730 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
731 self.0.set_nonblocking(nonblocking)
734 /// Returns the value of the `SO_ERROR` option.
739 /// use std::os::unix::net::UnixListener;
741 /// let listener = UnixListener::bind("/tmp/sock").unwrap();
743 /// if let Ok(Some(err)) = listener.take_error() {
744 /// println!("Got error: {:?}", err);
747 #[stable(feature = "unix_socket", since = "1.10.0")]
748 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
752 /// Returns an iterator over incoming connections.
754 /// The iterator will never return [`None`] and will also not yield the
755 /// peer's [`SocketAddr`] structure.
757 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
758 /// [`SocketAddr`]: struct.SocketAddr.html
764 /// use std::os::unix::net::{UnixStream, UnixListener};
766 /// fn handle_client(stream: UnixStream) {
770 /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
772 /// for stream in listener.incoming() {
775 /// thread::spawn(|| handle_client(stream));
783 #[stable(feature = "unix_socket", since = "1.10.0")]
784 pub fn incoming<'a>(&'a self) -> Incoming<'a> {
785 Incoming { listener: self }
789 #[stable(feature = "unix_socket", since = "1.10.0")]
790 impl AsRawFd for UnixListener {
791 fn as_raw_fd(&self) -> RawFd {
796 #[stable(feature = "unix_socket", since = "1.10.0")]
797 impl FromRawFd for UnixListener {
798 unsafe fn from_raw_fd(fd: RawFd) -> UnixListener {
799 UnixListener(Socket::from_inner(fd))
803 #[stable(feature = "unix_socket", since = "1.10.0")]
804 impl IntoRawFd for UnixListener {
805 fn into_raw_fd(self) -> RawFd {
810 #[stable(feature = "unix_socket", since = "1.10.0")]
811 impl<'a> IntoIterator for &'a UnixListener {
812 type Item = io::Result<UnixStream>;
813 type IntoIter = Incoming<'a>;
815 fn into_iter(self) -> Incoming<'a> {
820 /// An iterator over incoming connections to a [`UnixListener`].
822 /// It will never return [`None`].
824 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
825 /// [`UnixListener`]: struct.UnixListener.html
831 /// use std::os::unix::net::{UnixStream, UnixListener};
833 /// fn handle_client(stream: UnixStream) {
837 /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
839 /// for stream in listener.incoming() {
842 /// thread::spawn(|| handle_client(stream));
851 #[stable(feature = "unix_socket", since = "1.10.0")]
852 pub struct Incoming<'a> {
853 listener: &'a UnixListener,
856 #[stable(feature = "unix_socket", since = "1.10.0")]
857 impl<'a> Iterator for Incoming<'a> {
858 type Item = io::Result<UnixStream>;
860 fn next(&mut self) -> Option<io::Result<UnixStream>> {
861 Some(self.listener.accept().map(|s| s.0))
864 fn size_hint(&self) -> (usize, Option<usize>) {
865 (usize::max_value(), None)
869 /// A Unix datagram socket.
874 /// use std::os::unix::net::UnixDatagram;
876 /// let socket = UnixDatagram::bind("/path/to/my/socket").unwrap();
877 /// socket.send_to(b"hello world", "/path/to/other/socket").unwrap();
878 /// let mut buf = [0; 100];
879 /// let (count, address) = socket.recv_from(&mut buf).unwrap();
880 /// println!("socket {:?} sent {:?}", address, &buf[..count]);
882 #[stable(feature = "unix_socket", since = "1.10.0")]
883 pub struct UnixDatagram(Socket);
885 #[stable(feature = "unix_socket", since = "1.10.0")]
886 impl fmt::Debug for UnixDatagram {
887 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
888 let mut builder = fmt.debug_struct("UnixDatagram");
889 builder.field("fd", self.0.as_inner());
890 if let Ok(addr) = self.local_addr() {
891 builder.field("local", &addr);
893 if let Ok(addr) = self.peer_addr() {
894 builder.field("peer", &addr);
901 /// Creates a Unix datagram socket bound to the given path.
906 /// use std::os::unix::net::UnixDatagram;
908 /// let sock = match UnixDatagram::bind("/path/to/the/socket") {
909 /// Ok(sock) => sock,
911 /// println!("Couldn't bind: {:?}", e);
916 #[stable(feature = "unix_socket", since = "1.10.0")]
917 pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixDatagram> {
918 fn inner(path: &Path) -> io::Result<UnixDatagram> {
920 let socket = UnixDatagram::unbound()?;
921 let (addr, len) = sockaddr_un(path)?;
923 cvt(libc::bind(*socket.0.as_inner(), &addr as *const _ as *const _, len))?;
931 /// Creates a Unix Datagram socket which is not bound to any address.
936 /// use std::os::unix::net::UnixDatagram;
938 /// let sock = match UnixDatagram::unbound() {
939 /// Ok(sock) => sock,
941 /// println!("Couldn't unbound: {:?}", e);
946 #[stable(feature = "unix_socket", since = "1.10.0")]
947 pub fn unbound() -> io::Result<UnixDatagram> {
948 let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_DGRAM)?;
949 Ok(UnixDatagram(inner))
952 /// Create an unnamed pair of connected sockets.
954 /// Returns two `UnixDatagrams`s which are connected to each other.
959 /// use std::os::unix::net::UnixDatagram;
961 /// let (sock1, sock2) = match UnixDatagram::pair() {
962 /// Ok((sock1, sock2)) => (sock1, sock2),
964 /// println!("Couldn't unbound: {:?}", e);
969 #[stable(feature = "unix_socket", since = "1.10.0")]
970 pub fn pair() -> io::Result<(UnixDatagram, UnixDatagram)> {
971 let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_DGRAM)?;
972 Ok((UnixDatagram(i1), UnixDatagram(i2)))
975 /// Connects the socket to the specified address.
977 /// The [`send`] method may be used to send data to the specified address.
978 /// [`recv`] and [`recv_from`] will only receive data from that address.
980 /// [`send`]: #method.send
981 /// [`recv`]: #method.recv
982 /// [`recv_from`]: #method.recv_from
987 /// use std::os::unix::net::UnixDatagram;
989 /// let sock = UnixDatagram::unbound().unwrap();
990 /// match sock.connect("/path/to/the/socket") {
991 /// Ok(sock) => sock,
993 /// println!("Couldn't connect: {:?}", e);
998 #[stable(feature = "unix_socket", since = "1.10.0")]
999 pub fn connect<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
1000 fn inner(d: &UnixDatagram, path: &Path) -> io::Result<()> {
1002 let (addr, len) = sockaddr_un(path)?;
1004 cvt(libc::connect(*d.0.as_inner(), &addr as *const _ as *const _, len))?;
1009 inner(self, path.as_ref())
1012 /// Creates a new independently owned handle to the underlying socket.
1014 /// The returned `UnixDatagram` is a reference to the same socket that this
1015 /// object references. Both handles can be used to accept incoming
1016 /// connections and options set on one side will affect the other.
1021 /// use std::os::unix::net::UnixDatagram;
1023 /// let sock = UnixDatagram::bind("/path/to/the/socket").unwrap();
1025 /// let sock_copy = sock.try_clone().expect("try_clone failed");
1027 #[stable(feature = "unix_socket", since = "1.10.0")]
1028 pub fn try_clone(&self) -> io::Result<UnixDatagram> {
1029 self.0.duplicate().map(UnixDatagram)
1032 /// Returns the address of this socket.
1037 /// use std::os::unix::net::UnixDatagram;
1039 /// let sock = UnixDatagram::bind("/path/to/the/socket").unwrap();
1041 /// let addr = sock.local_addr().expect("Couldn't get local address");
1043 #[stable(feature = "unix_socket", since = "1.10.0")]
1044 pub fn local_addr(&self) -> io::Result<SocketAddr> {
1045 SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
1048 /// Returns the address of this socket's peer.
1050 /// The [`connect`] method will connect the socket to a peer.
1052 /// [`connect`]: #method.connect
1057 /// use std::os::unix::net::UnixDatagram;
1059 /// let sock = UnixDatagram::unbound().unwrap();
1060 /// sock.connect("/path/to/the/socket").unwrap();
1062 /// let addr = sock.peer_addr().expect("Couldn't get peer address");
1064 #[stable(feature = "unix_socket", since = "1.10.0")]
1065 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
1066 SocketAddr::new(|addr, len| unsafe { libc::getpeername(*self.0.as_inner(), addr, len) })
1069 /// Receives data from the socket.
1071 /// On success, returns the number of bytes read and the address from
1072 /// whence the data came.
1077 /// use std::os::unix::net::UnixDatagram;
1079 /// let sock = UnixDatagram::unbound().unwrap();
1080 /// let mut buf = vec![0; 10];
1081 /// match sock.recv_from(buf.as_mut_slice()) {
1082 /// Ok((size, sender)) => println!("received {} bytes from {:?}", size, sender),
1083 /// Err(e) => println!("recv_from function failed: {:?}", e),
1086 #[stable(feature = "unix_socket", since = "1.10.0")]
1087 pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
1089 let addr = SocketAddr::new(|addr, len| {
1091 count = libc::recvfrom(*self.0.as_inner(),
1092 buf.as_mut_ptr() as *mut _,
1099 } else if count == 0 {
1107 Ok((count as usize, addr))
1110 /// Receives data from the socket.
1112 /// On success, returns the number of bytes read.
1117 /// use std::os::unix::net::UnixDatagram;
1119 /// let sock = UnixDatagram::bind("/path/to/the/socket").unwrap();
1120 /// let mut buf = vec![0; 10];
1121 /// sock.recv(buf.as_mut_slice()).expect("recv function failed");
1123 #[stable(feature = "unix_socket", since = "1.10.0")]
1124 pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
1128 /// Sends data on the socket to the specified address.
1130 /// On success, returns the number of bytes written.
1135 /// use std::os::unix::net::UnixDatagram;
1137 /// let sock = UnixDatagram::unbound().unwrap();
1138 /// sock.send_to(b"omelette au fromage", "/some/sock").expect("send_to function failed");
1140 #[stable(feature = "unix_socket", since = "1.10.0")]
1141 pub fn send_to<P: AsRef<Path>>(&self, buf: &[u8], path: P) -> io::Result<usize> {
1142 fn inner(d: &UnixDatagram, buf: &[u8], path: &Path) -> io::Result<usize> {
1144 let (addr, len) = sockaddr_un(path)?;
1146 let count = cvt(libc::sendto(*d.0.as_inner(),
1147 buf.as_ptr() as *const _,
1150 &addr as *const _ as *const _,
1155 inner(self, buf, path.as_ref())
1158 /// Sends data on the socket to the socket's peer.
1160 /// The peer address may be set by the `connect` method, and this method
1161 /// will return an error if the socket has not already been connected.
1163 /// On success, returns the number of bytes written.
1168 /// use std::os::unix::net::UnixDatagram;
1170 /// let sock = UnixDatagram::unbound().unwrap();
1171 /// sock.connect("/some/sock").expect("Couldn't connect");
1172 /// sock.send(b"omelette au fromage").expect("send_to function failed");
1174 #[stable(feature = "unix_socket", since = "1.10.0")]
1175 pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
1179 /// Sets the read timeout for the socket.
1181 /// If the provided value is [`None`], then [`recv`] and [`recv_from`] calls will
1182 /// block indefinitely. It is an error to pass the zero [`Duration`] to this
1185 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
1186 /// [`recv`]: #method.recv
1187 /// [`recv_from`]: #method.recv_from
1188 /// [`Duration`]: ../../../../std/time/struct.Duration.html
1193 /// use std::os::unix::net::UnixDatagram;
1194 /// use std::time::Duration;
1196 /// let sock = UnixDatagram::unbound().unwrap();
1197 /// sock.set_read_timeout(Some(Duration::new(1, 0))).expect("set_read_timeout function failed");
1199 #[stable(feature = "unix_socket", since = "1.10.0")]
1200 pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
1201 self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
1204 /// Sets the write timeout for the socket.
1206 /// If the provided value is [`None`], then [`send`] and [`send_to`] calls will
1207 /// block indefinitely. It is an error to pass the zero [`Duration`] to this
1210 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
1211 /// [`send`]: #method.send
1212 /// [`send_to`]: #method.send_to
1213 /// [`Duration`]: ../../../../std/time/struct.Duration.html
1218 /// use std::os::unix::net::UnixDatagram;
1219 /// use std::time::Duration;
1221 /// let sock = UnixDatagram::unbound().unwrap();
1222 /// sock.set_write_timeout(Some(Duration::new(1, 0)))
1223 /// .expect("set_write_timeout function failed");
1225 #[stable(feature = "unix_socket", since = "1.10.0")]
1226 pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
1227 self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
1230 /// Returns the read timeout of this socket.
1235 /// use std::os::unix::net::UnixDatagram;
1236 /// use std::time::Duration;
1238 /// let sock = UnixDatagram::unbound().unwrap();
1239 /// sock.set_read_timeout(Some(Duration::new(1, 0))).expect("set_read_timeout function failed");
1240 /// assert_eq!(sock.read_timeout().unwrap(), Some(Duration::new(1, 0)));
1242 #[stable(feature = "unix_socket", since = "1.10.0")]
1243 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
1244 self.0.timeout(libc::SO_RCVTIMEO)
1247 /// Returns the write timeout of this socket.
1252 /// use std::os::unix::net::UnixDatagram;
1253 /// use std::time::Duration;
1255 /// let sock = UnixDatagram::unbound().unwrap();
1256 /// sock.set_write_timeout(Some(Duration::new(1, 0)))
1257 /// .expect("set_write_timeout function failed");
1258 /// assert_eq!(sock.write_timeout().unwrap(), Some(Duration::new(1, 0)));
1260 #[stable(feature = "unix_socket", since = "1.10.0")]
1261 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
1262 self.0.timeout(libc::SO_SNDTIMEO)
1265 /// Moves the socket into or out of nonblocking mode.
1270 /// use std::os::unix::net::UnixDatagram;
1272 /// let sock = UnixDatagram::unbound().unwrap();
1273 /// sock.set_nonblocking(true).expect("set_nonblocking function failed");
1275 #[stable(feature = "unix_socket", since = "1.10.0")]
1276 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
1277 self.0.set_nonblocking(nonblocking)
1280 /// Returns the value of the `SO_ERROR` option.
1285 /// use std::os::unix::net::UnixDatagram;
1287 /// let sock = UnixDatagram::unbound().unwrap();
1288 /// if let Ok(Some(err)) = sock.take_error() {
1289 /// println!("Got error: {:?}", err);
1292 #[stable(feature = "unix_socket", since = "1.10.0")]
1293 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
1297 /// Shut down the read, write, or both halves of this connection.
1299 /// This function will cause all pending and future I/O calls on the
1300 /// specified portions to immediately return with an appropriate value
1301 /// (see the documentation of [`Shutdown`]).
1303 /// [`Shutdown`]: ../../../../std/net/enum.Shutdown.html
1306 /// use std::os::unix::net::UnixDatagram;
1307 /// use std::net::Shutdown;
1309 /// let sock = UnixDatagram::unbound().unwrap();
1310 /// sock.shutdown(Shutdown::Both).expect("shutdown function failed");
1312 #[stable(feature = "unix_socket", since = "1.10.0")]
1313 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
1314 self.0.shutdown(how)
1318 #[stable(feature = "unix_socket", since = "1.10.0")]
1319 impl AsRawFd for UnixDatagram {
1320 fn as_raw_fd(&self) -> RawFd {
1325 #[stable(feature = "unix_socket", since = "1.10.0")]
1326 impl FromRawFd for UnixDatagram {
1327 unsafe fn from_raw_fd(fd: RawFd) -> UnixDatagram {
1328 UnixDatagram(Socket::from_inner(fd))
1332 #[stable(feature = "unix_socket", since = "1.10.0")]
1333 impl IntoRawFd for UnixDatagram {
1334 fn into_raw_fd(self) -> RawFd {
1339 #[cfg(all(test, not(target_os = "emscripten")))]
1345 use sys_common::io::test::tmpdir;
1349 macro_rules! or_panic {
1353 Err(e) => panic!("{}", e),
1361 let socket_path = dir.path().join("sock");
1362 let msg1 = b"hello";
1363 let msg2 = b"world!";
1365 let listener = or_panic!(UnixListener::bind(&socket_path));
1366 let thread = thread::spawn(move || {
1367 let mut stream = or_panic!(listener.accept()).0;
1368 let mut buf = [0; 5];
1369 or_panic!(stream.read(&mut buf));
1370 assert_eq!(&msg1[..], &buf[..]);
1371 or_panic!(stream.write_all(msg2));
1374 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1375 assert_eq!(Some(&*socket_path),
1376 stream.peer_addr().unwrap().as_pathname());
1377 or_panic!(stream.write_all(msg1));
1378 let mut buf = vec![];
1379 or_panic!(stream.read_to_end(&mut buf));
1380 assert_eq!(&msg2[..], &buf[..]);
1383 thread.join().unwrap();
1388 let msg1 = b"hello";
1389 let msg2 = b"world!";
1391 let (mut s1, mut s2) = or_panic!(UnixStream::pair());
1392 let thread = thread::spawn(move || {
1393 // s1 must be moved in or the test will hang!
1394 let mut buf = [0; 5];
1395 or_panic!(s1.read(&mut buf));
1396 assert_eq!(&msg1[..], &buf[..]);
1397 or_panic!(s1.write_all(msg2));
1400 or_panic!(s2.write_all(msg1));
1401 let mut buf = vec![];
1402 or_panic!(s2.read_to_end(&mut buf));
1403 assert_eq!(&msg2[..], &buf[..]);
1406 thread.join().unwrap();
1412 let socket_path = dir.path().join("sock");
1413 let msg1 = b"hello";
1414 let msg2 = b"world";
1416 let listener = or_panic!(UnixListener::bind(&socket_path));
1417 let thread = thread::spawn(move || {
1418 let mut stream = or_panic!(listener.accept()).0;
1419 or_panic!(stream.write_all(msg1));
1420 or_panic!(stream.write_all(msg2));
1423 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1424 let mut stream2 = or_panic!(stream.try_clone());
1426 let mut buf = [0; 5];
1427 or_panic!(stream.read(&mut buf));
1428 assert_eq!(&msg1[..], &buf[..]);
1429 or_panic!(stream2.read(&mut buf));
1430 assert_eq!(&msg2[..], &buf[..]);
1432 thread.join().unwrap();
1438 let socket_path = dir.path().join("sock");
1440 let listener = or_panic!(UnixListener::bind(&socket_path));
1441 let thread = thread::spawn(move || {
1442 for stream in listener.incoming().take(2) {
1443 let mut stream = or_panic!(stream);
1445 or_panic!(stream.read(&mut buf));
1450 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1451 or_panic!(stream.write_all(&[0]));
1454 thread.join().unwrap();
1460 let socket_path = dir.path()
1461 .join("asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfa\
1462 sasdfasdfasdasdfasdfasdfadfasdfasdfasdfasdfasdf");
1463 match UnixStream::connect(&socket_path) {
1464 Err(ref e) if e.kind() == io::ErrorKind::InvalidInput => {}
1465 Err(e) => panic!("unexpected error {}", e),
1466 Ok(_) => panic!("unexpected success"),
1469 match UnixListener::bind(&socket_path) {
1470 Err(ref e) if e.kind() == io::ErrorKind::InvalidInput => {}
1471 Err(e) => panic!("unexpected error {}", e),
1472 Ok(_) => panic!("unexpected success"),
1475 match UnixDatagram::bind(&socket_path) {
1476 Err(ref e) if e.kind() == io::ErrorKind::InvalidInput => {}
1477 Err(e) => panic!("unexpected error {}", e),
1478 Ok(_) => panic!("unexpected success"),
1485 let socket_path = dir.path().join("sock");
1487 let _listener = or_panic!(UnixListener::bind(&socket_path));
1489 let stream = or_panic!(UnixStream::connect(&socket_path));
1490 let dur = Duration::new(15410, 0);
1492 assert_eq!(None, or_panic!(stream.read_timeout()));
1494 or_panic!(stream.set_read_timeout(Some(dur)));
1495 assert_eq!(Some(dur), or_panic!(stream.read_timeout()));
1497 assert_eq!(None, or_panic!(stream.write_timeout()));
1499 or_panic!(stream.set_write_timeout(Some(dur)));
1500 assert_eq!(Some(dur), or_panic!(stream.write_timeout()));
1502 or_panic!(stream.set_read_timeout(None));
1503 assert_eq!(None, or_panic!(stream.read_timeout()));
1505 or_panic!(stream.set_write_timeout(None));
1506 assert_eq!(None, or_panic!(stream.write_timeout()));
1510 fn test_read_timeout() {
1512 let socket_path = dir.path().join("sock");
1514 let _listener = or_panic!(UnixListener::bind(&socket_path));
1516 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1517 or_panic!(stream.set_read_timeout(Some(Duration::from_millis(1000))));
1519 let mut buf = [0; 10];
1520 let kind = stream.read(&mut buf).err().expect("expected error").kind();
1521 assert!(kind == io::ErrorKind::WouldBlock || kind == io::ErrorKind::TimedOut);
1525 fn test_read_with_timeout() {
1527 let socket_path = dir.path().join("sock");
1529 let listener = or_panic!(UnixListener::bind(&socket_path));
1531 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1532 or_panic!(stream.set_read_timeout(Some(Duration::from_millis(1000))));
1534 let mut other_end = or_panic!(listener.accept()).0;
1535 or_panic!(other_end.write_all(b"hello world"));
1537 let mut buf = [0; 11];
1538 or_panic!(stream.read(&mut buf));
1539 assert_eq!(b"hello world", &buf[..]);
1541 let kind = stream.read(&mut buf).err().expect("expected error").kind();
1542 assert!(kind == io::ErrorKind::WouldBlock || kind == io::ErrorKind::TimedOut);
1546 fn test_unix_datagram() {
1548 let path1 = dir.path().join("sock1");
1549 let path2 = dir.path().join("sock2");
1551 let sock1 = or_panic!(UnixDatagram::bind(&path1));
1552 let sock2 = or_panic!(UnixDatagram::bind(&path2));
1554 let msg = b"hello world";
1555 or_panic!(sock1.send_to(msg, &path2));
1556 let mut buf = [0; 11];
1557 or_panic!(sock2.recv_from(&mut buf));
1558 assert_eq!(msg, &buf[..]);
1562 fn test_unnamed_unix_datagram() {
1564 let path1 = dir.path().join("sock1");
1566 let sock1 = or_panic!(UnixDatagram::bind(&path1));
1567 let sock2 = or_panic!(UnixDatagram::unbound());
1569 let msg = b"hello world";
1570 or_panic!(sock2.send_to(msg, &path1));
1571 let mut buf = [0; 11];
1572 let (usize, addr) = or_panic!(sock1.recv_from(&mut buf));
1573 assert_eq!(usize, 11);
1574 assert!(addr.is_unnamed());
1575 assert_eq!(msg, &buf[..]);
1579 fn test_connect_unix_datagram() {
1581 let path1 = dir.path().join("sock1");
1582 let path2 = dir.path().join("sock2");
1584 let bsock1 = or_panic!(UnixDatagram::bind(&path1));
1585 let bsock2 = or_panic!(UnixDatagram::bind(&path2));
1586 let sock = or_panic!(UnixDatagram::unbound());
1587 or_panic!(sock.connect(&path1));
1590 let msg = b"hello there";
1591 or_panic!(sock.send(msg));
1592 let mut buf = [0; 11];
1593 let (usize, addr) = or_panic!(bsock1.recv_from(&mut buf));
1594 assert_eq!(usize, 11);
1595 assert!(addr.is_unnamed());
1596 assert_eq!(msg, &buf[..]);
1598 // Changing default socket works too
1599 or_panic!(sock.connect(&path2));
1600 or_panic!(sock.send(msg));
1601 or_panic!(bsock2.recv_from(&mut buf));
1605 fn test_unix_datagram_recv() {
1607 let path1 = dir.path().join("sock1");
1609 let sock1 = or_panic!(UnixDatagram::bind(&path1));
1610 let sock2 = or_panic!(UnixDatagram::unbound());
1611 or_panic!(sock2.connect(&path1));
1613 let msg = b"hello world";
1614 or_panic!(sock2.send(msg));
1615 let mut buf = [0; 11];
1616 let size = or_panic!(sock1.recv(&mut buf));
1617 assert_eq!(size, 11);
1618 assert_eq!(msg, &buf[..]);
1622 fn datagram_pair() {
1623 let msg1 = b"hello";
1624 let msg2 = b"world!";
1626 let (s1, s2) = or_panic!(UnixDatagram::pair());
1627 let thread = thread::spawn(move || {
1628 // s1 must be moved in or the test will hang!
1629 let mut buf = [0; 5];
1630 or_panic!(s1.recv(&mut buf));
1631 assert_eq!(&msg1[..], &buf[..]);
1632 or_panic!(s1.send(msg2));
1635 or_panic!(s2.send(msg1));
1636 let mut buf = [0; 6];
1637 or_panic!(s2.recv(&mut buf));
1638 assert_eq!(&msg2[..], &buf[..]);
1641 thread.join().unwrap();
1645 fn abstract_namespace_not_allowed() {
1646 assert!(UnixStream::connect("\0asdf").is_err());