2013-04-17 17:55:21 -07:00
|
|
|
// Copyright 2013 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.
|
|
|
|
|
2013-10-15 19:44:08 -07:00
|
|
|
/*!
|
|
|
|
|
|
|
|
Named pipes
|
|
|
|
|
|
|
|
This module contains the ability to communicate over named pipes with
|
|
|
|
synchronous I/O. On windows, this corresponds to talking over a Named Pipe,
|
|
|
|
while on Unix it corresponds to UNIX domain sockets.
|
|
|
|
|
|
|
|
These pipes are similar to TCP in the sense that you can have both a stream to a
|
|
|
|
server and a server itself. The server provided accepts other `UnixStream`
|
|
|
|
instances as clients.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2013-04-17 17:55:21 -07:00
|
|
|
use prelude::*;
|
2013-10-15 19:44:08 -07:00
|
|
|
|
2013-10-16 16:48:30 -07:00
|
|
|
use c_str::ToCStr;
|
2014-01-22 19:32:16 -08:00
|
|
|
use clone::Clone;
|
2013-12-05 17:25:48 -08:00
|
|
|
use rt::rtio::{IoFactory, LocalIo, RtioUnixListener};
|
2013-10-17 12:13:29 -07:00
|
|
|
use rt::rtio::{RtioUnixAcceptor, RtioPipe};
|
2013-11-10 22:46:32 -08:00
|
|
|
use io::pipe::PipeStream;
|
2014-01-29 16:33:57 -08:00
|
|
|
use io::{Listener, Acceptor, Reader, Writer, IoResult};
|
2013-04-17 17:55:21 -07:00
|
|
|
|
2013-10-15 19:44:08 -07:00
|
|
|
/// A stream which communicates over a named pipe.
|
|
|
|
pub struct UnixStream {
|
|
|
|
priv obj: PipeStream,
|
|
|
|
}
|
2013-04-17 17:55:21 -07:00
|
|
|
|
|
|
|
impl UnixStream {
|
2013-10-16 14:48:05 -07:00
|
|
|
fn new(obj: ~RtioPipe) -> UnixStream {
|
2013-10-18 14:01:22 -07:00
|
|
|
UnixStream { obj: PipeStream::new(obj) }
|
2013-10-15 19:44:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Connect to a pipe named by `path`. This will attempt to open a
|
|
|
|
/// connection to the underlying socket.
|
|
|
|
///
|
|
|
|
/// The returned stream will be closed when the object falls out of scope.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2014-01-29 16:33:57 -08:00
|
|
|
/// ```rust
|
|
|
|
/// # #[allow(unused_must_use)];
|
|
|
|
/// use std::io::net::unix::UnixStream;
|
2013-10-15 19:44:08 -07:00
|
|
|
///
|
2014-01-29 16:33:57 -08:00
|
|
|
/// let server = Path::new("path/to/my/socket");
|
|
|
|
/// let mut stream = UnixStream::connect(&server);
|
|
|
|
/// stream.write([1, 2, 3]);
|
|
|
|
/// ```
|
|
|
|
pub fn connect<P: ToCStr>(path: &P) -> IoResult<UnixStream> {
|
2013-12-12 17:30:41 -08:00
|
|
|
LocalIo::maybe_raise(|io| {
|
|
|
|
io.unix_connect(&path.to_c_str()).map(UnixStream::new)
|
|
|
|
})
|
2013-04-17 17:55:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-22 19:32:16 -08:00
|
|
|
impl Clone for UnixStream {
|
|
|
|
fn clone(&self) -> UnixStream {
|
|
|
|
UnixStream { obj: self.obj.clone() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-17 17:55:21 -07:00
|
|
|
impl Reader for UnixStream {
|
2014-01-29 16:33:57 -08:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> { self.obj.read(buf) }
|
2013-04-17 17:55:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Writer for UnixStream {
|
2014-01-29 16:33:57 -08:00
|
|
|
fn write(&mut self, buf: &[u8]) -> IoResult<()> { self.obj.write(buf) }
|
2013-04-17 17:55:21 -07:00
|
|
|
}
|
|
|
|
|
2013-10-15 19:44:08 -07:00
|
|
|
pub struct UnixListener {
|
2013-10-17 12:13:29 -07:00
|
|
|
priv obj: ~RtioUnixListener,
|
2013-10-15 19:44:08 -07:00
|
|
|
}
|
2013-04-17 17:55:21 -07:00
|
|
|
|
|
|
|
impl UnixListener {
|
2013-10-15 19:44:08 -07:00
|
|
|
|
|
|
|
/// Creates a new listener, ready to receive incoming connections on the
|
|
|
|
/// specified socket. The server will be named by `path`.
|
|
|
|
///
|
|
|
|
/// This listener will be closed when it falls out of scope.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2014-01-29 16:33:57 -08:00
|
|
|
/// ```
|
2014-02-14 23:44:22 -08:00
|
|
|
/// # fn main() {}
|
|
|
|
/// # fn foo() {
|
|
|
|
/// # #[allow(unused_must_use)];
|
2014-01-29 16:33:57 -08:00
|
|
|
/// use std::io::net::unix::UnixListener;
|
2014-02-14 23:44:22 -08:00
|
|
|
/// use std::io::{Listener, Acceptor};
|
2013-10-15 19:44:08 -07:00
|
|
|
///
|
2014-02-14 23:44:22 -08:00
|
|
|
/// let server = Path::new("/path/to/my/socket");
|
|
|
|
/// let stream = UnixListener::bind(&server);
|
|
|
|
/// for mut client in stream.listen().incoming() {
|
2014-01-29 16:33:57 -08:00
|
|
|
/// client.write([1, 2, 3, 4]);
|
|
|
|
/// }
|
2014-02-14 23:44:22 -08:00
|
|
|
/// # }
|
2014-01-29 16:33:57 -08:00
|
|
|
/// ```
|
|
|
|
pub fn bind<P: ToCStr>(path: &P) -> IoResult<UnixListener> {
|
2013-12-12 17:30:41 -08:00
|
|
|
LocalIo::maybe_raise(|io| {
|
|
|
|
io.unix_bind(&path.to_c_str()).map(|s| UnixListener { obj: s })
|
|
|
|
})
|
2013-04-17 17:55:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-27 10:01:17 -07:00
|
|
|
impl Listener<UnixStream, UnixAcceptor> for UnixListener {
|
2014-01-29 16:33:57 -08:00
|
|
|
fn listen(self) -> IoResult<UnixAcceptor> {
|
|
|
|
self.obj.listen().map(|obj| UnixAcceptor { obj: obj })
|
2013-10-15 19:44:08 -07:00
|
|
|
}
|
2013-08-27 10:01:17 -07:00
|
|
|
}
|
|
|
|
|
2013-10-15 19:44:08 -07:00
|
|
|
pub struct UnixAcceptor {
|
2013-10-16 14:48:05 -07:00
|
|
|
priv obj: ~RtioUnixAcceptor,
|
2013-10-15 19:44:08 -07:00
|
|
|
}
|
2013-08-27 10:01:17 -07:00
|
|
|
|
|
|
|
impl Acceptor<UnixStream> for UnixAcceptor {
|
2014-01-29 16:33:57 -08:00
|
|
|
fn accept(&mut self) -> IoResult<UnixStream> {
|
|
|
|
self.obj.accept().map(UnixStream::new)
|
2013-10-15 19:44:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use prelude::*;
|
|
|
|
use super::*;
|
2013-11-10 22:46:32 -08:00
|
|
|
use io::*;
|
2013-12-12 21:38:57 -08:00
|
|
|
use io::test::*;
|
2013-10-15 19:44:08 -07:00
|
|
|
|
2014-02-07 10:37:58 -08:00
|
|
|
pub fn smalltest(server: proc(UnixStream), client: proc(UnixStream)) {
|
2013-12-12 17:20:58 -08:00
|
|
|
let path1 = next_test_unix();
|
|
|
|
let path2 = path1.clone();
|
2014-02-26 12:55:23 -08:00
|
|
|
|
|
|
|
let mut acceptor = UnixListener::bind(&path1).listen();
|
2013-10-15 19:44:08 -07:00
|
|
|
|
2014-01-26 22:42:26 -05:00
|
|
|
spawn(proc() {
|
2014-02-26 12:55:23 -08:00
|
|
|
match UnixStream::connect(&path2) {
|
|
|
|
Ok(c) => client(c),
|
|
|
|
Err(e) => fail!("failed connect: {}", e),
|
|
|
|
}
|
2014-01-26 22:42:26 -05:00
|
|
|
});
|
2013-12-12 17:20:58 -08:00
|
|
|
|
2014-02-26 12:55:23 -08:00
|
|
|
match acceptor.accept() {
|
|
|
|
Ok(c) => server(c),
|
|
|
|
Err(e) => fail!("failed accept: {}", e),
|
|
|
|
}
|
2013-10-15 19:44:08 -07:00
|
|
|
}
|
|
|
|
|
2014-02-07 10:37:58 -08:00
|
|
|
iotest!(fn bind_error() {
|
|
|
|
let path = "path/to/nowhere";
|
|
|
|
match UnixListener::bind(&path) {
|
2014-01-30 14:10:53 -08:00
|
|
|
Ok(..) => fail!(),
|
2014-02-07 10:37:58 -08:00
|
|
|
Err(e) => {
|
|
|
|
assert!(e.kind == PermissionDenied || e.kind == FileNotFound ||
|
|
|
|
e.kind == InvalidInput);
|
|
|
|
}
|
2014-01-30 14:10:53 -08:00
|
|
|
}
|
2014-02-07 10:37:58 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
iotest!(fn connect_error() {
|
|
|
|
let path = if cfg!(windows) {
|
|
|
|
r"\\.\pipe\this_should_not_exist_ever"
|
|
|
|
} else {
|
|
|
|
"path/to/nowhere"
|
|
|
|
};
|
|
|
|
match UnixStream::connect(&path) {
|
2014-01-30 14:10:53 -08:00
|
|
|
Ok(..) => fail!(),
|
2014-02-07 10:37:58 -08:00
|
|
|
Err(e) => {
|
|
|
|
assert!(e.kind == FileNotFound || e.kind == OtherIoError);
|
|
|
|
}
|
2014-01-30 14:10:53 -08:00
|
|
|
}
|
2014-02-07 10:37:58 -08:00
|
|
|
})
|
2013-10-15 19:44:08 -07:00
|
|
|
|
2014-02-07 10:37:58 -08:00
|
|
|
iotest!(fn smoke() {
|
2013-11-21 23:36:52 -08:00
|
|
|
smalltest(proc(mut server) {
|
2013-10-15 19:44:08 -07:00
|
|
|
let mut buf = [0];
|
2014-01-30 14:10:53 -08:00
|
|
|
server.read(buf).unwrap();
|
2013-10-15 19:44:08 -07:00
|
|
|
assert!(buf[0] == 99);
|
2013-11-21 23:36:52 -08:00
|
|
|
}, proc(mut client) {
|
2014-01-30 14:10:53 -08:00
|
|
|
client.write([99]).unwrap();
|
2013-10-15 19:44:08 -07:00
|
|
|
})
|
2014-02-07 10:37:58 -08:00
|
|
|
})
|
2013-10-15 19:44:08 -07:00
|
|
|
|
2014-02-07 10:37:58 -08:00
|
|
|
iotest!(fn read_eof() {
|
2013-11-21 23:36:52 -08:00
|
|
|
smalltest(proc(mut server) {
|
2013-10-15 19:44:08 -07:00
|
|
|
let mut buf = [0];
|
2014-01-30 14:10:53 -08:00
|
|
|
assert!(server.read(buf).is_err());
|
|
|
|
assert!(server.read(buf).is_err());
|
2013-11-21 23:36:52 -08:00
|
|
|
}, proc(_client) {
|
2013-10-15 19:44:08 -07:00
|
|
|
// drop the client
|
|
|
|
})
|
2014-02-24 09:15:05 -08:00
|
|
|
} #[ignore(cfg(windows))]) // FIXME(#12516)
|
2013-10-15 19:44:08 -07:00
|
|
|
|
2014-02-07 10:37:58 -08:00
|
|
|
iotest!(fn write_begone() {
|
2013-11-21 23:36:52 -08:00
|
|
|
smalltest(proc(mut server) {
|
2013-10-15 19:44:08 -07:00
|
|
|
let buf = [0];
|
2014-01-30 14:10:53 -08:00
|
|
|
loop {
|
|
|
|
match server.write(buf) {
|
|
|
|
Ok(..) => {}
|
|
|
|
Err(e) => {
|
2014-02-07 10:37:58 -08:00
|
|
|
assert!(e.kind == BrokenPipe ||
|
|
|
|
e.kind == NotConnected ||
|
|
|
|
e.kind == ConnectionReset,
|
2014-01-30 14:10:53 -08:00
|
|
|
"unknown error {:?}", e);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-10-15 19:44:08 -07:00
|
|
|
}
|
2013-11-21 23:36:52 -08:00
|
|
|
}, proc(_client) {
|
2013-10-15 19:44:08 -07:00
|
|
|
// drop the client
|
|
|
|
})
|
2014-02-07 10:37:58 -08:00
|
|
|
})
|
2013-10-15 19:44:08 -07:00
|
|
|
|
2014-02-07 10:37:58 -08:00
|
|
|
iotest!(fn accept_lots() {
|
2013-12-12 17:20:58 -08:00
|
|
|
let times = 10;
|
|
|
|
let path1 = next_test_unix();
|
|
|
|
let path2 = path1.clone();
|
2013-12-12 21:38:57 -08:00
|
|
|
let (port, chan) = Chan::new();
|
2013-10-15 19:44:08 -07:00
|
|
|
|
2014-01-26 22:42:26 -05:00
|
|
|
spawn(proc() {
|
2013-12-05 18:19:06 -08:00
|
|
|
port.recv();
|
2014-01-30 11:20:34 +11:00
|
|
|
for _ in range(0, times) {
|
2013-12-05 18:19:06 -08:00
|
|
|
let mut stream = UnixStream::connect(&path2);
|
2014-02-07 10:37:58 -08:00
|
|
|
match stream.write([100]) {
|
|
|
|
Ok(..) => {}
|
|
|
|
Err(e) => fail!("failed write: {}", e)
|
|
|
|
}
|
2014-01-30 11:20:34 +11:00
|
|
|
}
|
2014-01-26 22:42:26 -05:00
|
|
|
});
|
2013-12-12 17:20:58 -08:00
|
|
|
|
2014-02-07 10:37:58 -08:00
|
|
|
let mut acceptor = match UnixListener::bind(&path1).listen() {
|
|
|
|
Ok(a) => a,
|
|
|
|
Err(e) => fail!("failed listen: {}", e),
|
|
|
|
};
|
2013-12-12 17:20:58 -08:00
|
|
|
chan.send(());
|
2014-01-30 11:20:34 +11:00
|
|
|
for _ in range(0, times) {
|
2013-12-12 17:20:58 -08:00
|
|
|
let mut client = acceptor.accept();
|
|
|
|
let mut buf = [0];
|
2014-02-07 10:37:58 -08:00
|
|
|
match client.read(buf) {
|
|
|
|
Ok(..) => {}
|
|
|
|
Err(e) => fail!("failed read/accept: {}", e),
|
|
|
|
}
|
2013-12-12 17:20:58 -08:00
|
|
|
assert_eq!(buf[0], 100);
|
2014-01-30 11:20:34 +11:00
|
|
|
}
|
2014-02-07 10:37:58 -08:00
|
|
|
})
|
2013-10-15 19:44:08 -07:00
|
|
|
|
2014-02-07 10:37:58 -08:00
|
|
|
#[cfg(unix)]
|
|
|
|
iotest!(fn path_exists() {
|
2013-12-12 17:20:58 -08:00
|
|
|
let path = next_test_unix();
|
|
|
|
let _acceptor = UnixListener::bind(&path).listen();
|
|
|
|
assert!(path.exists());
|
2014-02-07 10:37:58 -08:00
|
|
|
})
|
2014-01-22 19:32:16 -08:00
|
|
|
|
2014-02-07 10:37:58 -08:00
|
|
|
iotest!(fn unix_clone_smoke() {
|
2014-01-22 19:32:16 -08:00
|
|
|
let addr = next_test_unix();
|
|
|
|
let mut acceptor = UnixListener::bind(&addr).listen();
|
|
|
|
|
|
|
|
spawn(proc() {
|
|
|
|
let mut s = UnixStream::connect(&addr);
|
|
|
|
let mut buf = [0, 0];
|
2014-02-07 10:37:58 -08:00
|
|
|
debug!("client reading");
|
2014-01-22 19:32:16 -08:00
|
|
|
assert_eq!(s.read(buf), Ok(1));
|
|
|
|
assert_eq!(buf[0], 1);
|
2014-02-07 10:37:58 -08:00
|
|
|
debug!("client writing");
|
2014-01-22 19:32:16 -08:00
|
|
|
s.write([2]).unwrap();
|
2014-02-07 10:37:58 -08:00
|
|
|
debug!("client dropping");
|
2014-01-22 19:32:16 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
let mut s1 = acceptor.accept().unwrap();
|
|
|
|
let s2 = s1.clone();
|
|
|
|
|
|
|
|
let (p1, c1) = Chan::new();
|
|
|
|
let (p2, c2) = Chan::new();
|
|
|
|
spawn(proc() {
|
|
|
|
let mut s2 = s2;
|
|
|
|
p1.recv();
|
2014-02-07 10:37:58 -08:00
|
|
|
debug!("writer writing");
|
2014-01-22 19:32:16 -08:00
|
|
|
s2.write([1]).unwrap();
|
2014-02-07 10:37:58 -08:00
|
|
|
debug!("writer done");
|
2014-01-22 19:32:16 -08:00
|
|
|
c2.send(());
|
|
|
|
});
|
|
|
|
c1.send(());
|
|
|
|
let mut buf = [0, 0];
|
2014-02-07 10:37:58 -08:00
|
|
|
debug!("reader reading");
|
2014-01-22 19:32:16 -08:00
|
|
|
assert_eq!(s1.read(buf), Ok(1));
|
2014-02-07 10:37:58 -08:00
|
|
|
debug!("reader done");
|
2014-01-22 19:32:16 -08:00
|
|
|
p2.recv();
|
2014-02-07 10:37:58 -08:00
|
|
|
})
|
2014-01-22 19:32:16 -08:00
|
|
|
|
2014-02-07 10:37:58 -08:00
|
|
|
iotest!(fn unix_clone_two_read() {
|
2014-01-22 19:32:16 -08:00
|
|
|
let addr = next_test_unix();
|
|
|
|
let mut acceptor = UnixListener::bind(&addr).listen();
|
2014-02-11 16:33:34 -08:00
|
|
|
let (p, c) = Chan::new();
|
2014-01-22 19:32:16 -08:00
|
|
|
let c2 = c.clone();
|
|
|
|
|
|
|
|
spawn(proc() {
|
|
|
|
let mut s = UnixStream::connect(&addr);
|
|
|
|
s.write([1]).unwrap();
|
|
|
|
p.recv();
|
|
|
|
s.write([2]).unwrap();
|
|
|
|
p.recv();
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut s1 = acceptor.accept().unwrap();
|
|
|
|
let s2 = s1.clone();
|
|
|
|
|
|
|
|
let (p, done) = Chan::new();
|
|
|
|
spawn(proc() {
|
|
|
|
let mut s2 = s2;
|
|
|
|
let mut buf = [0, 0];
|
|
|
|
s2.read(buf).unwrap();
|
|
|
|
c2.send(());
|
|
|
|
done.send(());
|
|
|
|
});
|
|
|
|
let mut buf = [0, 0];
|
|
|
|
s1.read(buf).unwrap();
|
|
|
|
c.send(());
|
|
|
|
|
|
|
|
p.recv();
|
2014-02-07 10:37:58 -08:00
|
|
|
})
|
2014-01-22 19:32:16 -08:00
|
|
|
|
2014-02-07 10:37:58 -08:00
|
|
|
iotest!(fn unix_clone_two_write() {
|
2014-01-22 19:32:16 -08:00
|
|
|
let addr = next_test_unix();
|
|
|
|
let mut acceptor = UnixListener::bind(&addr).listen();
|
|
|
|
|
|
|
|
spawn(proc() {
|
|
|
|
let mut s = UnixStream::connect(&addr);
|
|
|
|
let mut buf = [0, 1];
|
|
|
|
s.read(buf).unwrap();
|
|
|
|
s.read(buf).unwrap();
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut s1 = acceptor.accept().unwrap();
|
|
|
|
let s2 = s1.clone();
|
|
|
|
|
|
|
|
let (p, done) = Chan::new();
|
|
|
|
spawn(proc() {
|
|
|
|
let mut s2 = s2;
|
|
|
|
s2.write([1]).unwrap();
|
|
|
|
done.send(());
|
|
|
|
});
|
|
|
|
s1.write([2]).unwrap();
|
|
|
|
|
|
|
|
p.recv();
|
2014-02-07 10:37:58 -08:00
|
|
|
})
|
2013-04-17 17:55:21 -07:00
|
|
|
}
|